emit MotionEvent and ButtonEvent + display button status v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 07 Mar 2019 16:20:51 +0100
branchv_0
changeset 6 49560660d230
parent 5 6ba51911d539
child 7 b6c0e1574a8b
emit MotionEvent and ButtonEvent + display button status
src/SpacenavReceiver.h
src/SpacenavWindow.cpp
src/SpacenavWindow.h
src/SpacenavWindow.ui
src/SpacenavWrapper.cpp
src/SpacenavWrapper.h
src/spacenav-demo-qt.cpp
--- a/src/SpacenavReceiver.h	Thu Mar 07 15:41:38 2019 +0100
+++ b/src/SpacenavReceiver.h	Thu Mar 07 16:20:51 2019 +0100
@@ -22,7 +22,8 @@
 
 #include "SpacenavWrapper.h"
 
-Q_DECLARE_METATYPE(SpacenavWrapper::Event)
+Q_DECLARE_METATYPE(SpacenavWrapper::Event::MotionEvent)
+Q_DECLARE_METATYPE(SpacenavWrapper::Event::ButtonEvent)
 
 class SpacenavReceiver : public QThread {
 	Q_OBJECT
@@ -37,10 +38,12 @@
 	void run() {
 		while (true) {
 			SpacenavWrapper::Event e = spnav.waitEvent();
-			emit spacenavEvent(e);
+			if (e.type == SpacenavWrapper::Event::Type::MOTION) emit spacenavMotionEvent(e.motion);
+			if (e.type == SpacenavWrapper::Event::Type::BUTTON) emit spacenavButtonEvent(e.button);
 		}
 	}
 
 signals:
-	void spacenavEvent(SpacenavWrapper::Event e);
+	void spacenavMotionEvent(SpacenavWrapper::Event::MotionEvent e);
+	void spacenavButtonEvent(SpacenavWrapper::Event::ButtonEvent e);
 };
\ No newline at end of file
--- a/src/SpacenavWindow.cpp	Thu Mar 07 15:41:38 2019 +0100
+++ b/src/SpacenavWindow.cpp	Thu Mar 07 16:20:51 2019 +0100
@@ -20,11 +20,19 @@
 
 SpacenavWindow::SpacenavWindow() {
 	widget.setupUi(this);
+	resize(640, 480);
+	setWindowTitle("Spacenav Demo");
 }
 
 SpacenavWindow::~SpacenavWindow() {
 }
 
-void SpacenavWindow::spacenavEvent(SpacenavWrapper::Event e) {
-	setWindowTitle("event!");
+void SpacenavWindow::spacenavMotionEvent(SpacenavWrapper::Event::MotionEvent e) {
+	setWindowTitle("m event!");
 }
+
+void SpacenavWindow::spacenavButtonEvent(SpacenavWrapper::Event::ButtonEvent e) {
+	setWindowTitle("b event!");
+	if (e.number == 0) widget.button0->setChecked(e.pressed);
+	if (e.number == 1) widget.button1->setChecked(e.pressed);
+}
--- a/src/SpacenavWindow.h	Thu Mar 07 15:41:38 2019 +0100
+++ b/src/SpacenavWindow.h	Thu Mar 07 16:20:51 2019 +0100
@@ -26,7 +26,8 @@
 	SpacenavWindow();
 	virtual ~SpacenavWindow();
 public slots:
-	void spacenavEvent(SpacenavWrapper::Event e);
+	void spacenavMotionEvent(SpacenavWrapper::Event::MotionEvent e);
+	void spacenavButtonEvent(SpacenavWrapper::Event::ButtonEvent e);
 private:
 	Ui::SpacenavWindow widget;
 };
--- a/src/SpacenavWindow.ui	Thu Mar 07 15:41:38 2019 +0100
+++ b/src/SpacenavWindow.ui	Thu Mar 07 16:20:51 2019 +0100
@@ -14,17 +14,49 @@
    <string>SpacenavWindow</string>
   </property>
   <widget class="QWidget" name="centralwidget">
-   <widget class="QPushButton" name="pushButton">
+   <widget class="QDial" name="dial">
     <property name="geometry">
      <rect>
-      <x>60</x>
-      <y>60</y>
-      <width>88</width>
-      <height>34</height>
+      <x>130</x>
+      <y>40</y>
+      <width>111</width>
+      <height>111</height>
+     </rect>
+    </property>
+   </widget>
+   <widget class="QPushButton" name="button0">
+    <property name="geometry">
+     <rect>
+      <x>40</x>
+      <y>40</y>
+      <width>80</width>
+      <height>111</height>
      </rect>
     </property>
     <property name="text">
-     <string>PushButton</string>
+     <string>left</string>
+    </property>
+    <property name="checkable">
+     <bool>true</bool>
+    </property>
+    <property name="checked">
+     <bool>false</bool>
+    </property>
+   </widget>
+   <widget class="QPushButton" name="button1">
+    <property name="geometry">
+     <rect>
+      <x>260</x>
+      <y>40</y>
+      <width>80</width>
+      <height>111</height>
+     </rect>
+    </property>
+    <property name="text">
+     <string>right</string>
+    </property>
+    <property name="checkable">
+     <bool>true</bool>
     </property>
    </widget>
   </widget>
@@ -34,7 +66,7 @@
      <x>0</x>
      <y>0</y>
      <width>800</width>
-     <height>30</height>
+     <height>23</height>
     </rect>
    </property>
   </widget>
--- a/src/SpacenavWrapper.cpp	Thu Mar 07 15:41:38 2019 +0100
+++ b/src/SpacenavWrapper.cpp	Thu Mar 07 16:20:51 2019 +0100
@@ -58,19 +58,22 @@
 	spnav_event event;
 
 	if (spnav_wait_event(&event)) {
-
-		// TODO: copy fields
+		e.type = event.type == SPNAV_EVENT_MOTION ? SpacenavWrapper::Event::Type::MOTION : SpacenavWrapper::Event::Type::BUTTON;
 
-
-		// TODO: remove logging
 		if (event.type == SPNAV_EVENT_MOTION) {
-			std::wcout << L"xxx motion event: t(" << event.motion.x << L", " << event.motion.y << L", " << event.motion.z << L") ";
-			std::wcout << L"r(" << event.motion.rx << L", " << event.motion.ry << L", " << event.motion.rz << L")" << std::endl;
+			e.motion.x = event.motion.x;
+			e.motion.y = event.motion.y;
+			e.motion.z = event.motion.z;
+			e.motion.rx = event.motion.rx;
+			e.motion.ry = event.motion.ry;
+			e.motion.rz = event.motion.rz;
+			e.motion.type = event.motion.type;
+			e.motion.period = event.motion.period;
 		} else { /* SPNAV_EVENT_BUTTON */
-			std::wcout << L"xxx button " << (event.button.press ? "press" : "release") << L" event b(" << event.button.bnum << L")" << std::endl;
+			e.button.number = event.button.bnum;
+			e.button.pressed = event.button.press;
 		}
 
-
 		return e;
 	} else {
 		// TODO: throw exception
--- a/src/SpacenavWrapper.h	Thu Mar 07 15:41:38 2019 +0100
+++ b/src/SpacenavWrapper.h	Thu Mar 07 16:20:51 2019 +0100
@@ -29,6 +29,35 @@
 public:
 
 	class Event {
+	public:
+
+		enum class Type {
+			MOTION,
+			BUTTON
+		};
+
+		class MotionEvent {
+		public:
+			int x;
+			int y;
+			int z;
+			int rx;
+			int ry;
+			int rz;
+			int type;
+			unsigned int period;
+		};
+
+		class ButtonEvent {
+		public:
+			int number;
+			bool pressed;
+		};
+
+		Type type;
+		MotionEvent motion;
+		ButtonEvent button;
+
 	};
 
 	SpacenavWrapper();
--- a/src/spacenav-demo-qt.cpp	Thu Mar 07 15:41:38 2019 +0100
+++ b/src/spacenav-demo-qt.cpp	Thu Mar 07 16:20:51 2019 +0100
@@ -32,7 +32,8 @@
 	SpacenavWindow w;
 	w.show();
 
-	QObject::connect(&t, &SpacenavReceiver::spacenavEvent, &w, &SpacenavWindow::spacenavEvent, Qt::ConnectionType::QueuedConnection);
+	QObject::connect(&t, &SpacenavReceiver::spacenavMotionEvent, &w, &SpacenavWindow::spacenavMotionEvent, Qt::ConnectionType::QueuedConnection);
+	QObject::connect(&t, &SpacenavReceiver::spacenavButtonEvent, &w, &SpacenavWindow::spacenavButtonEvent, Qt::ConnectionType::QueuedConnection);
 
 	t.start();
 	int qtResultCode = app.exec();