GuiApplicationNativeEventAware.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "PreCompiled.h"
00024
00025 #include <QGlobalStatic>
00026 #ifdef Q_WS_X11
00027 #include <QX11Info>
00028 #endif
00029 #include <QMainWindow>
00030 #include <QWidget>
00031 #include <FCConfig.h>
00032 #include <Base/Console.h>
00033 #include "GuiApplicationNativeEventAware.h"
00034 #include "SpaceballEvent.h"
00035
00036
00037 #ifdef Q_WS_X11
00038 #ifdef SPNAV_FOUND
00039 #include <spnav.h>
00040 #endif
00041 #endif
00042
00043 #ifdef Q_WS_WIN
00044 #include <windows.h>
00045 #endif
00046
00047
00048 Gui::GUIApplicationNativeEventAware::GUIApplicationNativeEventAware(int &argc, char *argv[]) :
00049 QApplication (argc, argv), spaceballPresent(false)
00050 {
00051 mainWindow = 0;
00052 }
00053
00054 Gui::GUIApplicationNativeEventAware::~GUIApplicationNativeEventAware()
00055 {
00056 #ifdef SPNAV_FOUND
00057 if (spnav_close())
00058 Base::Console().Log("Couldn't disconnect from spacenav daemon\n");
00059 else
00060 Base::Console().Log("Disconnected from spacenav daemon\n");
00061 #endif
00062 }
00063
00064 void Gui::GUIApplicationNativeEventAware::initSpaceball(QMainWindow *window)
00065 {
00066 mainWindow = window;
00067 #ifdef SPNAV_FOUND
00068 if (spnav_x11_open(QX11Info::display(), window->winId()) == -1)
00069 Base::Console().Log("Couldn't connect to spacenav daemon\n");
00070 else
00071 {
00072 Base::Console().Log("Connected to spacenav daemon\n");
00073 spaceballPresent = true;
00074 }
00075 #endif
00076
00077 Spaceball::MotionEvent::MotionEventType = QEvent::registerEventType();
00078 Spaceball::ButtonEvent::ButtonEventType = QEvent::registerEventType();
00079 }
00080
00081 bool Gui::GUIApplicationNativeEventAware::processSpaceballEvent(QObject *object, QEvent *event)
00082 {
00083 Spaceball::ButtonEvent *ballEvent = dynamic_cast<Spaceball::ButtonEvent *>(event);
00084 if (!ballEvent)
00085 return true;
00086 QApplication::notify(object, ballEvent);
00087 if (!ballEvent->isHandled())
00088 {
00089
00090 Spaceball::ButtonEvent *newEvent = new Spaceball::ButtonEvent(*ballEvent);
00091 postEvent(object->parent(), newEvent);
00092 }
00093 return true;
00094 }
00095
00096 #ifdef Q_WS_X11
00097 bool Gui::GUIApplicationNativeEventAware::x11EventFilter(XEvent *event)
00098 {
00099 #ifdef SPNAV_FOUND
00100 spnav_event navEvent;
00101 if (!spnav_x11_event(event, &navEvent))
00102 return false;
00103
00104 QWidget *currentWidget = this->focusWidget();
00105 if (!currentWidget)
00106 currentWidget = mainWindow;
00107
00108 if (navEvent.type == SPNAV_EVENT_MOTION)
00109 {
00110 Spaceball::MotionEvent *motionEvent = new Spaceball::MotionEvent();
00111 motionEvent->setTranslations(navEvent.motion.x, navEvent.motion.y, navEvent.motion.z);
00112 motionEvent->setRotations(navEvent.motion.rx, navEvent.motion.ry, navEvent.motion.rz);
00113 this->postEvent(currentWidget, motionEvent);
00114 return true;
00115 }
00116
00117 if (navEvent.type == SPNAV_EVENT_BUTTON)
00118 {
00119 Spaceball::ButtonEvent *buttonEvent = new Spaceball::ButtonEvent();
00120 buttonEvent->setButtonNumber(navEvent.button.bnum);
00121 if (navEvent.button.press)
00122 buttonEvent->setButtonStatus(Spaceball::BUTTON_PRESSED);
00123 else
00124 buttonEvent->setButtonStatus(Spaceball::BUTTON_RELEASED);
00125 this->postEvent(currentWidget, buttonEvent);
00126 return true;
00127 }
00128
00129 Base::Console().Log("Unknown spaceball event\n");
00130 return true;
00131 #else
00132 return false;
00133 #endif
00134 }
00135 #endif
00136
00137 #ifdef Q_WS_WIN
00138 bool Gui::GUIApplicationNativeEventAware::winEventFilter(MSG *msg, long *result)
00139 {
00140 return false;
00141 }
00142 #endif
00143
00144 #include "moc_GuiApplicationNativeEventAware.cpp"