NorMIT-nav  22.09
An IGT application
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 #include "cxTrackPadWidget.h"
12 
13 #include <QVBoxLayout>
14 #include <QScrollBar>
15 #include <QTouchEvent>
16 
17 #include <QAction>
18 #include <QRadialGradient>
19 #include "vtkRenderer.h"
20 #include "vtkCamera.h"
21 #include "vtkSmartPointer.h"
22 #include "cxDataInterface.h"
23 #include "cxCameraControl.h"
24 #include "cxBoundingBox3D.h"
25 #include "cxView.h"
26 #include "cxViewService.h"
27 #include "cxLogger.h"
28 
29 namespace cx
30 {
31 
35 
36 TrackPadWidget::TrackPadWidget(ViewServicePtr viewService, QWidget* parent) :
37  BaseWidget(parent, "track_pad_widget", "Camera Control"),
38  mViewService(viewService)
39 {
40  this->setToolTip("Track pad camera control");
41  mCameraControl = viewService->getCameraControl();
42 
43  mMinPadSize = QSize(50,50);
44  mMinBarSize = QSize(20,50);
45 
46  mTopLayout = new QVBoxLayout(this);
47 
48  this->createStandard3DViewActions();
49  this->definePanLayout();
50  this->defineRotateLayout();
51 }
52 
53 void TrackPadWidget::createStandard3DViewActions()
54 {
55  if(!mCameraControl)
56  {
57  CX_LOG_WARNING() << "TrackPadWidget::createStandard3DViewActions: Got no mCameraControl";
58  return;
59  }
60 
61  QActionGroup* group = mCameraControl->createStandard3DViewActions();
62 
63  QToolBar* toolBar = new QToolBar(this);
64  mTopLayout->addWidget(toolBar);
65  toolBar->addActions(group->actions());
66  toolBar->addSeparator();
67 }
68 
69 void TrackPadWidget::defineRotateLayout()
70 {
71  QGroupBox* group = new QGroupBox("rotate", this);
72  group->setFlat(true);
73  mTopLayout->addWidget(group);
74 
75  QHBoxLayout* layout = new QHBoxLayout;
76  layout->setMargin(4);
77  group->setLayout(layout);
78 
79  MousePadWidget* rotateWidget = new MousePadWidget(this, mMinPadSize);
80  connect(rotateWidget, SIGNAL(mouseMoved(QPointF)), this, SLOT(rotateXZSlot(QPointF)));
81  layout->addWidget(rotateWidget, 4);
82 
83  MousePadWidget* rotateYWidget = new MousePadWidget(this, mMinBarSize);
84  rotateYWidget->setFixedXPos(true);
85  connect(rotateYWidget, SIGNAL(mouseMoved(QPointF)), this, SLOT(rotateYSlot(QPointF)));
86  layout->addWidget(rotateYWidget, 1);
87 }
88 
89 
90 void TrackPadWidget::definePanLayout()
91 {
92  QGroupBox* group = new QGroupBox("pan", this);
93  group->setFlat(true);
94  mTopLayout->addWidget(group);
95 
96  QHBoxLayout* panLayout = new QHBoxLayout;
97  panLayout->setMargin(4);
98  group->setLayout(panLayout);
99 
100  MousePadWidget* panWidget = new MousePadWidget(this, mMinPadSize);
101  connect(panWidget, SIGNAL(mouseMoved(QPointF)), this, SLOT(panXZSlot(QPointF)));
102  panLayout->addWidget(panWidget, 4);
103 
104  MousePadWidget* dollyWidget = new MousePadWidget(this, mMinBarSize);
105  dollyWidget->setFixedXPos(true);
106  connect(dollyWidget, SIGNAL(mouseMoved(QPointF)), this, SLOT(dollySlot(QPointF)));
107  panLayout->addWidget(dollyWidget, 1);
108 }
109 
110 vtkCameraPtr TrackPadWidget::getCamera() const
111 {
112  return mViewService->get3DView()->getRenderer()->GetActiveCamera();
113 }
114 
115 void TrackPadWidget::rotateYSlot(QPointF delta)
116 {
117  double scale = 180;
118  double factor = scale * delta.y();
119 
120  this->getCamera()->Roll(factor);
121 }
122 
123 void TrackPadWidget::rotateXZSlot(QPointF delta)
124 {
125  vtkCameraPtr camera = this->getCamera();
126  double scale = 180;
127 
128  camera->Azimuth(-scale * delta.x());
129  camera->Elevation(scale * delta.y());
130  camera->OrthogonalizeViewUp(); // needed when using azimuth, according to docs (failure to do this causes strange zooming effects)
131 }
132 
133 void TrackPadWidget::dollySlot(QPointF delta)
134 {
135  double factor = 1 + delta.y();
136  this->getCamera()->Dolly(factor);
137  mViewService->get3DView()->getRenderer()->ResetCameraClippingRange();
138 }
139 
140 void TrackPadWidget::panXZSlot(QPointF delta)
141 {
142  vtkCameraPtr camera = this->getCamera();
143  Vector3D position(camera->GetPosition());
144  Vector3D focus(camera->GetFocalPoint());
145  Vector3D vup(camera->GetViewUp());
146 
147  Vector3D e_x = cross(focus-position, vup).normal();
148  Vector3D e_y = vup.normal();
149 
150  DoubleBoundingBox3D bb(mViewService->get3DView()->getRenderer()->ComputeVisiblePropBounds());
151 
152  double volSize = bb.range().length() / pow(3, 1.0/3.0); // mm size of volume
153  double scale = volSize;
154  Vector3D t = scale * (-delta.x() * e_x + delta.y() * e_y);
155 
156  position += t;
157  focus += t;
158 
159  camera->SetPosition(position.begin());
160  camera->SetFocalPoint(focus.begin());
161 }
162 
164 {
165 }
166 
167 void TrackPadWidget::showEvent(QShowEvent* event)
168 {
169  QWidget::showEvent(event);
170 }
171 
172 void TrackPadWidget::hideEvent(QCloseEvent* event)
173 {
174  QWidget::closeEvent(event);
175 }
176 
177 
178 }//end namespace cx
cxLogger.h
cx::DoubleBoundingBox3D
Representation of a floating-point bounding box in 3D. The data are stored as {xmin,...
Definition: cxBoundingBox3D.h:63
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::BaseWidget
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:88
cx::TrackPadWidget::showEvent
virtual void showEvent(QShowEvent *event)
updates internal info before showing the widget
Definition: cxTrackPadWidget.cpp:167
cxBoundingBox3D.h
cx::TrackPadWidget::rotateYSlot
void rotateYSlot(QPointF delta)
Definition: cxTrackPadWidget.cpp:115
cx::TrackPadWidget::panXZSlot
void panXZSlot(QPointF delta)
Definition: cxTrackPadWidget.cpp:140
cx::cross
Vector3D cross(const Vector3D &a, const Vector3D &b)
compute cross product of a and b.
Definition: cxVector3D.cpp:41
cxDataInterface.h
cx::TrackPadWidget::dollySlot
void dollySlot(QPointF delta)
Definition: cxTrackPadWidget.cpp:133
cxCameraControl.h
cxViewService.h
cx::ViewServicePtr
boost::shared_ptr< class ViewService > ViewServicePtr
Definition: cxLogicManager.h:28
cx::TrackPadWidget::rotateXZSlot
void rotateXZSlot(QPointF delta)
Definition: cxTrackPadWidget.cpp:123
cxView.h
cxTrackPadWidget.h
vtkCameraPtr
vtkSmartPointer< class vtkCamera > vtkCameraPtr
Definition: vtkForwardDeclarations.h:39
cx::TrackPadWidget::hideEvent
virtual void hideEvent(QCloseEvent *event)
disconnects stuff
Definition: cxTrackPadWidget.cpp:172
cx::DoubleBoundingBox3D::range
Vector3D range() const
Definition: cxBoundingBox3D.cpp:200
cx::TrackPadWidget::~TrackPadWidget
virtual ~TrackPadWidget()
Definition: cxTrackPadWidget.cpp:163
CX_LOG_WARNING
#define CX_LOG_WARNING
Definition: cxLogger.h:98
cx::Vector3D
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
cx::TrackPadWidget::TrackPadWidget
TrackPadWidget(ViewServicePtr viewService, QWidget *parent)
Definition: cxTrackPadWidget.cpp:36