CustusX  2023.01.05-dev+develop.0da12
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
void rotateYSlot(QPointF delta)
void setFixedXPos(bool on)
A touchpad-friendly area for performing 1D/2D scroll operations.
boost::shared_ptr< class ViewService > ViewServicePtr
void rotateXZSlot(QPointF delta)
Vector3D cross(const Vector3D &a, const Vector3D &b)
compute cross product of a and b.
Definition: cxVector3D.cpp:41
TrackPadWidget(ViewServicePtr viewService, QWidget *parent)
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:42
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:88
#define CX_LOG_WARNING
Definition: cxLogger.h:98
void dollySlot(QPointF delta)
vtkSmartPointer< class vtkCamera > vtkCameraPtr
Namespace for all CustusX production code.
virtual void hideEvent(QCloseEvent *event)
disconnects stuff