ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
cc3DMouseManager.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "cc3DMouseManager.h"
9 
10 #include <QAction>
11 #include <QMainWindow>
12 #include <QMenu>
13 
14 #include "Mouse3DInput.h"
15 #include "ecvMainAppInterface.h"
16 
18  QObject* parent)
19  : QObject(parent), m_appInterface(appInterface), m3dMouseInput(nullptr) {
20  setupMenu();
21 
22  enableDevice(true, true);
23 }
24 
26  releaseDevice();
27 
28  if (m_menu) {
29  delete m_menu;
30  }
31 }
32 
33 void cc3DMouseManager::enableDevice(bool state, bool silent) {
34  if (m3dMouseInput) {
35  releaseDevice();
36  }
37 
38  if (state) {
39  m3dMouseInput = new Mouse3DInput(this);
40  if (m3dMouseInput->connect(m_appInterface->getMainWindow(),
41  "ACloudViewer")) {
42  connect(m3dMouseInput, &Mouse3DInput::sigMove3d, this,
43  &cc3DMouseManager::on3DMouseMove);
44  connect(m3dMouseInput, &Mouse3DInput::sigReleased, this,
45  &cc3DMouseManager::on3DMouseReleased);
46  connect(m3dMouseInput, &Mouse3DInput::sigOn3dmouseKeyDown, this,
47  &cc3DMouseManager::on3DMouseKeyDown);
48  connect(m3dMouseInput, &Mouse3DInput::sigOn3dmouseKeyUp, this,
49  &cc3DMouseManager::on3DMouseKeyUp);
50  connect(m3dMouseInput, &Mouse3DInput::sigOn3dmouseCMDKeyDown, this,
51  &cc3DMouseManager::on3DMouseCMDKeyDown);
52  connect(m3dMouseInput, &Mouse3DInput::sigOn3dmouseCMDKeyUp, this,
53  &cc3DMouseManager::on3DMouseCMDKeyUp);
54  } else {
55  delete m3dMouseInput;
56  m3dMouseInput = nullptr;
57 
58  if (!silent) {
60  "[3D Mouse] No device found"); // warning message has
61  // already been issued
62  // by
63  // Mouse3DInput::connect
64  }
65  state = false;
66  }
67  } else {
68  CVLog::Warning("[3D Mouse] Device has been disabled");
69  }
70 
71  m_actionEnable->blockSignals(true);
72  m_actionEnable->setChecked(state);
73  m_actionEnable->blockSignals(false);
74 }
75 
76 void cc3DMouseManager::releaseDevice() {
77  if (m3dMouseInput == nullptr) return;
78 
79  m3dMouseInput->disconnectDriver(); // disconnect from the driver
80  m3dMouseInput->disconnect(this); // disconnect from Qt ;)
81 
82  delete m3dMouseInput;
83  m3dMouseInput = nullptr;
84 }
85 
86 void cc3DMouseManager::on3DMouseKeyUp(int) {
87  // nothing right now
88 }
89 
90 void cc3DMouseManager::on3DMouseCMDKeyUp(int) {
91  // nothing right now
92 }
93 
94 void cc3DMouseManager::on3DMouseKeyDown(int key) {
95  switch (key) {
96  // CVLog::Print(QString("on3DMouseKeyDown Key = %1").arg(key));
98  // should be handled by the driver now!
99  break;
100  case Mouse3DInput::V3DK_FIT: {
101  if (m_appInterface->getSelectedEntities().empty()) {
102  m_appInterface->setGlobalZoom();
103  } else {
104  m_appInterface->zoomOnSelectedEntities();
105  }
106  } break;
108  m_appInterface->setView(CC_TOP_VIEW);
109  break;
111  m_appInterface->setView(CC_LEFT_VIEW);
112  break;
114  m_appInterface->setView(CC_RIGHT_VIEW);
115  break;
117  m_appInterface->setView(CC_FRONT_VIEW);
118  break;
120  m_appInterface->setView(CC_BOTTOM_VIEW);
121  break;
123  m_appInterface->setView(CC_BACK_VIEW);
124  break;
126  // should be handled by the driver now!
127  break;
129  // should be handled by the driver now!
130  break;
132  m_appInterface->setView(CC_ISO_VIEW_1);
133  break;
135  m_appInterface->setView(CC_ISO_VIEW_2);
136  break;
138  // should be handled by the driver now!
139  break;
141  // should be handled by the driver now!
142  break;
144  // should be handled by the driver now!
145  break;
147  case Mouse3DInput::V3DK_CCW: {
148  ccGLWindow* activeWin = m_appInterface->getActiveGLWindow();
149  if (activeWin != nullptr) {
150  CCVector3d axis(0, 0, -1);
151  CCVector3d trans(0, 0, 0);
152  ccGLMatrixd mat;
153  double angle = M_PI / 2;
154  if (key == Mouse3DInput::V3DK_CCW) {
155  angle = -angle;
156  }
157  mat.initFromParameters(angle, axis, trans);
158  activeWin->rotateBaseViewMat(mat);
159  activeWin->redraw();
160  }
161  } break;
166  default:
167  CVLog::Warning("[3D mouse] This button is not handled (yet)");
168  // TODO
169  break;
170  }
171 }
172 
173 void cc3DMouseManager::on3DMouseCMDKeyDown(int cmd) {
174  switch (cmd) {
175  // CVLog::Print(QString("on3DMouseCMDKeyDown Cmd = %1").arg(cmd));
177  if (m_appInterface->getSelectedEntities().empty()) {
178  m_appInterface->setGlobalZoom();
179  } else {
180  m_appInterface->zoomOnSelectedEntities();
181  }
182  } break;
184  m_appInterface->setView(CC_TOP_VIEW);
185  break;
187  m_appInterface->setView(CC_LEFT_VIEW);
188  break;
190  m_appInterface->setView(CC_RIGHT_VIEW);
191  break;
193  m_appInterface->setView(CC_FRONT_VIEW);
194  break;
196  m_appInterface->setView(CC_BOTTOM_VIEW);
197  break;
199  m_appInterface->setView(CC_BACK_VIEW);
200  break;
202  m_appInterface->setView(CC_ISO_VIEW_1);
203  break;
205  m_appInterface->setView(CC_ISO_VIEW_2);
206  break;
209  ccGLWindow* activeWin = m_appInterface->getActiveGLWindow();
210  if (activeWin != nullptr) {
211  CCVector3d axis(0, 0, -1);
212  CCVector3d trans(0, 0, 0);
213  ccGLMatrixd mat;
214  double angle = M_PI / 2;
216  angle = -angle;
217  }
218  mat.initFromParameters(angle, axis, trans);
219  activeWin->rotateBaseViewMat(mat);
220  activeWin->redraw();
221  }
222  } break;
225  ccGLWindow* activeWin = m_appInterface->getActiveGLWindow();
226  if (activeWin != nullptr) {
227  CCVector3d axis(0, 1, 0);
228  CCVector3d trans(0, 0, 0);
229  ccGLMatrixd mat;
230  double angle = M_PI / 2;
232  angle = -angle;
233  }
234  mat.initFromParameters(angle, axis, trans);
235  activeWin->rotateBaseViewMat(mat);
236  activeWin->redraw();
237  }
238  }
241  ccGLWindow* activeWin = m_appInterface->getActiveGLWindow();
242  if (activeWin != nullptr) {
243  CCVector3d axis(1, 0, 0);
244  CCVector3d trans(0, 0, 0);
245  ccGLMatrixd mat;
246  double angle = M_PI / 2;
248  angle = -angle;
249  }
250  mat.initFromParameters(angle, axis, trans);
251  activeWin->rotateBaseViewMat(mat);
252  activeWin->redraw();
253  }
254  } break;
255  default:
256  CVLog::Warning("[3D mouse] This button is not handled (yet)");
257  // TODO
258  break;
259  }
260 }
261 
262 void cc3DMouseManager::on3DMouseMove(std::vector<float>& vec) {
263  ccGLWindow* win = m_appInterface->getActiveGLWindow();
264  if (win == nullptr) return;
265 
266  Mouse3DInput::Apply(vec, win);
267 }
268 
269 void cc3DMouseManager::on3DMouseReleased() {
270  ccGLWindow* win = m_appInterface->getActiveGLWindow();
271  if (win == nullptr) return;
272 
273  if (win->getPivotVisibility() == ccGLWindow::PIVOT_SHOW_ON_MOVE) {
274  // we have to hide the pivot symbol!
275  win->showPivotSymbol(false);
276  win->redraw();
277  }
278 }
279 
280 void cc3DMouseManager::setupMenu() {
281  m_menu = new QMenu("3D Mouse");
282  m_menu->setIcon(QIcon(":/CC/images/im3DxLogo.png"));
283 
284  m_actionEnable = new QAction(tr("Enable"), this);
285  m_actionEnable->setCheckable(true);
286 
287  connect(m_actionEnable, &QAction::toggled,
288  [this](bool state) { enableDevice(state, false); });
289 
290  m_menu->addAction(m_actionEnable);
291 }
constexpr double M_PI
Pi.
Definition: CVConst.h:19
@ CC_ISO_VIEW_1
Definition: CVConst.h:109
@ CC_ISO_VIEW_2
Definition: CVConst.h:110
@ CC_FRONT_VIEW
Definition: CVConst.h:105
@ CC_TOP_VIEW
Definition: CVConst.h:103
@ CC_RIGHT_VIEW
Definition: CVConst.h:108
@ CC_BOTTOM_VIEW
Definition: CVConst.h:104
@ CC_BACK_VIEW
Definition: CVConst.h:106
@ CC_LEFT_VIEW
Definition: CVConst.h:107
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
Definition: CVLog.cpp:133
static bool Error(const char *format,...)
Display an error dialog with formatted message.
Definition: CVLog.cpp:143
3DxWare driver wrapper for 3D mouse handling
Definition: Mouse3DInput.h:43
void sigReleased()
void sigOn3dmouseCMDKeyDown(int virtualCMDCode)
void sigMove3d(std::vector< float > &motionData)
void sigOn3dmouseKeyUp(int virtualKeyCode)
static void Apply(const std::vector< float > &motionData, ccGLWindow *win)
Applies motion data to a given 3D window.
void disconnectDriver()
Disconnects from the 3DxWare driver.
void sigOn3dmouseKeyDown(int virtualKeyCode)
bool connect(QWidget *mainWidget, QString appName)
Attempts to connect with the 3DxWare driver.
void sigOn3dmouseCMDKeyUp(int virtualCMDCode)
cc3DMouseManager(ecvMainAppInterface *appInterface, QObject *parent)
void initFromParameters(T alpha_rad, const Vector3Tpl< T > &axis3D, const Vector3Tpl< T > &t3D)
Inits transformation from a rotation axis, an angle and a translation.
Double version of ccGLMatrixTpl.
Definition: ecvGLMatrix.h:56
Main application interface (for plugins)
virtual void setGlobalZoom()=0
virtual QMainWindow * getMainWindow()=0
Returns main window.
virtual void setView(CC_VIEW_ORIENTATION view)=0
virtual const ccHObject::Container & getSelectedEntities() const =0
Returns currently selected entities ("read only")
virtual void zoomOnSelectedEntities()=0