Fraxinus  16.5.0-fx-rc1
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxTrackPadWidget.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 #include "cxTrackPadWidget.h"
33 
34 #include <QVBoxLayout>
35 #include <QScrollBar>
36 #include <QTouchEvent>
37 
38 #include <QAction>
39 #include <QRadialGradient>
40 #include "vtkRenderer.h"
41 #include "vtkCamera.h"
42 #include "vtkSmartPointer.h"
43 #include "cxDataInterface.h"
44 #include "cxCameraControl.h"
45 #include "cxBoundingBox3D.h"
46 #include "cxView.h"
47 #include "cxViewService.h"
48 
49 //TODO: remove
50 #include "cxLegacySingletons.h"
51 
52 namespace cx
53 {
54 
58 
60  BaseWidget(parent, "TrackPadWidget", "Camera Control")
61 {
62  this->setToolTip("Track pad camera control");
63  mCameraControl = viewService()->getCameraControl();
64 
65  mMinPadSize = QSize(50,50);
66  mMinBarSize = QSize(20,50);
67 
68  mTopLayout = new QVBoxLayout(this);
69 
70  this->createStandard3DViewActions();
71  this->definePanLayout();
72  this->defineRotateLayout();
73 }
74 
75 void TrackPadWidget::createStandard3DViewActions()
76 {
77  QActionGroup* group = mCameraControl->createStandard3DViewActions();
78 
79  QToolBar* toolBar = new QToolBar(this);
80  mTopLayout->addWidget(toolBar);
81  toolBar->addActions(group->actions());
82  toolBar->addSeparator();
83 }
84 
85 void TrackPadWidget::defineRotateLayout()
86 {
87  QGroupBox* group = new QGroupBox("rotate", this);
88  group->setFlat(true);
89  mTopLayout->addWidget(group);
90 
91  QHBoxLayout* layout = new QHBoxLayout;
92  layout->setMargin(4);
93  group->setLayout(layout);
94 
95  MousePadWidget* rotateWidget = new MousePadWidget(this, mMinPadSize);
96  connect(rotateWidget, SIGNAL(mouseMoved(QPointF)), this, SLOT(rotateXZSlot(QPointF)));
97  layout->addWidget(rotateWidget, 4);
98 
99  MousePadWidget* rotateYWidget = new MousePadWidget(this, mMinBarSize);
100  rotateYWidget->setFixedXPos(true);
101  connect(rotateYWidget, SIGNAL(mouseMoved(QPointF)), this, SLOT(rotateYSlot(QPointF)));
102  layout->addWidget(rotateYWidget, 1);
103 }
104 
105 
106 void TrackPadWidget::definePanLayout()
107 {
108  QGroupBox* group = new QGroupBox("pan", this);
109  group->setFlat(true);
110  mTopLayout->addWidget(group);
111 
112  QHBoxLayout* panLayout = new QHBoxLayout;
113  panLayout->setMargin(4);
114  group->setLayout(panLayout);
115 
116  MousePadWidget* panWidget = new MousePadWidget(this, mMinPadSize);
117  connect(panWidget, SIGNAL(mouseMoved(QPointF)), this, SLOT(panXZSlot(QPointF)));
118  panLayout->addWidget(panWidget, 4);
119 
120  MousePadWidget* dollyWidget = new MousePadWidget(this, mMinBarSize);
121  dollyWidget->setFixedXPos(true);
122  connect(dollyWidget, SIGNAL(mouseMoved(QPointF)), this, SLOT(dollySlot(QPointF)));
123  panLayout->addWidget(dollyWidget, 1);
124 }
125 
126 vtkCameraPtr TrackPadWidget::getCamera() const
127 {
128  return viewService()->get3DView()->getRenderer()->GetActiveCamera();
129 }
130 
131 void TrackPadWidget::rotateYSlot(QPointF delta)
132 {
133  double scale = 180;
134  double factor = scale * delta.y();
135 
136  this->getCamera()->Roll(factor);
137 }
138 
139 void TrackPadWidget::rotateXZSlot(QPointF delta)
140 {
141  vtkCameraPtr camera = this->getCamera();
142  double scale = 180;
143 
144  camera->Azimuth(-scale * delta.x());
145  camera->Elevation(scale * delta.y());
146  camera->OrthogonalizeViewUp(); // needed when using azimuth, according to docs (failure to do this causes strange zooming effects)
147 }
148 
149 void TrackPadWidget::dollySlot(QPointF delta)
150 {
151  double factor = 1 + delta.y();
152  this->getCamera()->Dolly(factor);
153  viewService()->get3DView()->getRenderer()->ResetCameraClippingRange();
154 }
155 
156 void TrackPadWidget::panXZSlot(QPointF delta)
157 {
158  vtkCameraPtr camera = this->getCamera();
159  Vector3D position(camera->GetPosition());
160  Vector3D focus(camera->GetFocalPoint());
161  Vector3D vup(camera->GetViewUp());
162 
163  Vector3D e_x = cross(focus-position, vup).normal();
164  Vector3D e_y = vup.normal();
165 
166  DoubleBoundingBox3D bb(viewService()->get3DView()->getRenderer()->ComputeVisiblePropBounds());
167 
168  double volSize = bb.range().length() / pow(3, 1.0/3.0); // mm size of volume
169  double scale = volSize;
170  Vector3D t = scale * (-delta.x() * e_x + delta.y() * e_y);
171 
172  position += t;
173  focus += t;
174 
175  camera->SetPosition(position.begin());
176  camera->SetFocalPoint(focus.begin());
177 }
178 
180 {
181 }
182 
183 void TrackPadWidget::showEvent(QShowEvent* event)
184 {
185  QWidget::showEvent(event);
186 }
187 
188 void TrackPadWidget::hideEvent(QCloseEvent* event)
189 {
190  QWidget::closeEvent(event);
191 }
192 
193 
194 }//end namespace cx
TrackPadWidget(QWidget *parent)
void rotateYSlot(QPointF delta)
void rotateXZSlot(QPointF delta)
Vector3D cross(const Vector3D &a, const Vector3D &b)
compute cross product of a and b.
Definition: cxVector3D.cpp:62
Representation of a floating-point bounding box in 3D. The data are stored as {xmin,xmax,ymin,ymax,zmin,zmax}, in order to simplify communication with vtk.
void panXZSlot(QPointF delta)
virtual void showEvent(QShowEvent *event)
updates internal info before showing the widget
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:108
cxLogicManager_EXPORT ViewServicePtr viewService()
void dollySlot(QPointF delta)
vtkSmartPointer< class vtkCamera > vtkCameraPtr
virtual void hideEvent(QCloseEvent *event)
disconnects stuff