CustusX  15.3.4-beta
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  mCameraControl = viewService()->getCameraControl();
63 
64  mMinPadSize = QSize(50,50);
65  mMinBarSize = QSize(20,50);
66 
67  mTopLayout = new QVBoxLayout(this);
68 
69  this->createStandard3DViewActions();
70  this->definePanLayout();
71  this->defineRotateLayout();
72 }
73 
75 {
76  return "<html>"
77  "<h3>Trackpad for touch screen devices</h3>"
78  "<p>Helps the user control the camera on a touch screen.</p>"
79  "<p><i></i></p>"
80  "</html>";
81 }
82 
83 void TrackPadWidget::createStandard3DViewActions()
84 {
85  QActionGroup* group = mCameraControl->createStandard3DViewActions();
86 
87  QToolBar* toolBar = new QToolBar(this);
88  mTopLayout->addWidget(toolBar);
89  toolBar->addActions(group->actions());
90  toolBar->addSeparator();
91 }
92 
93 void TrackPadWidget::defineRotateLayout()
94 {
95  QGroupBox* group = new QGroupBox("rotate", this);
96  group->setFlat(true);
97  mTopLayout->addWidget(group);
98 
99  QHBoxLayout* layout = new QHBoxLayout;
100  layout->setMargin(4);
101  group->setLayout(layout);
102 
103  MousePadWidget* rotateWidget = new MousePadWidget(this, mMinPadSize);
104  connect(rotateWidget, SIGNAL(mouseMoved(QPointF)), this, SLOT(rotateXZSlot(QPointF)));
105  layout->addWidget(rotateWidget, 4);
106 
107  MousePadWidget* rotateYWidget = new MousePadWidget(this, mMinBarSize);
108  rotateYWidget->setFixedXPos(true);
109  connect(rotateYWidget, SIGNAL(mouseMoved(QPointF)), this, SLOT(rotateYSlot(QPointF)));
110  layout->addWidget(rotateYWidget, 1);
111 }
112 
113 
114 void TrackPadWidget::definePanLayout()
115 {
116  QGroupBox* group = new QGroupBox("pan", this);
117  group->setFlat(true);
118  mTopLayout->addWidget(group);
119 
120  QHBoxLayout* panLayout = new QHBoxLayout;
121  panLayout->setMargin(4);
122  group->setLayout(panLayout);
123 
124  MousePadWidget* panWidget = new MousePadWidget(this, mMinPadSize);
125  connect(panWidget, SIGNAL(mouseMoved(QPointF)), this, SLOT(panXZSlot(QPointF)));
126  panLayout->addWidget(panWidget, 4);
127 
128  MousePadWidget* dollyWidget = new MousePadWidget(this, mMinBarSize);
129  dollyWidget->setFixedXPos(true);
130  connect(dollyWidget, SIGNAL(mouseMoved(QPointF)), this, SLOT(dollySlot(QPointF)));
131  panLayout->addWidget(dollyWidget, 1);
132 }
133 
134 vtkCameraPtr TrackPadWidget::getCamera() const
135 {
136  return viewService()->get3DView()->getRenderer()->GetActiveCamera();
137 }
138 
139 void TrackPadWidget::rotateYSlot(QPointF delta)
140 {
141  double scale = 180;
142  double factor = scale * delta.y();
143 
144  this->getCamera()->Roll(factor);
145 }
146 
147 void TrackPadWidget::rotateXZSlot(QPointF delta)
148 {
149  vtkCameraPtr camera = this->getCamera();
150  double scale = 180;
151 
152  camera->Azimuth(-scale * delta.x());
153  camera->Elevation(scale * delta.y());
154  camera->OrthogonalizeViewUp(); // needed when using azimuth, according to docs (failure to do this causes strange zooming effects)
155 }
156 
157 void TrackPadWidget::dollySlot(QPointF delta)
158 {
159  double factor = 1 + delta.y();
160  this->getCamera()->Dolly(factor);
161  viewService()->get3DView()->getRenderer()->ResetCameraClippingRange();
162 }
163 
164 void TrackPadWidget::panXZSlot(QPointF delta)
165 {
166  vtkCameraPtr camera = this->getCamera();
167  Vector3D position(camera->GetPosition());
168  Vector3D focus(camera->GetFocalPoint());
169  Vector3D vup(camera->GetViewUp());
170 
171  Vector3D e_x = cross(focus-position, vup).normal();
172  Vector3D e_y = vup.normal();
173 
174  DoubleBoundingBox3D bb(viewService()->get3DView()->getRenderer()->ComputeVisiblePropBounds());
175 
176  double volSize = bb.range().length() / pow(3, 1.0/3.0); // mm size of volume
177  double scale = volSize;
178  Vector3D t = scale * (-delta.x() * e_x + delta.y() * e_y);
179 
180  position += t;
181  focus += t;
182 
183  camera->SetPosition(position.begin());
184  camera->SetFocalPoint(focus.begin());
185 }
186 
188 {
189 }
190 
191 void TrackPadWidget::showEvent(QShowEvent* event)
192 {
193  QWidget::showEvent(event);
194 }
195 
196 void TrackPadWidget::hideEvent(QCloseEvent* event)
197 {
198  QWidget::closeEvent(event);
199 }
200 
201 
202 }//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
virtual QString defaultWhatsThis() const
Returns a short description of what this widget will do for you.
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