From 012e50c524967d952371267683fc096ccd1eb76e Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 9 Dec 2011 11:29:51 +0000 Subject: [PATCH] + Thomas Anderson's patch for spacenav git-svn-id: https://free-cad.svn.sourceforge.net/svnroot/free-cad/trunk@5242 e8eeb9e2-ec13-0410-a4a9-efa5cf37419d --- src/Gui/Application.cpp | 12 ++-- src/Gui/GuiApplicationNativeEventAware.cpp | 32 +++++++--- src/Gui/MainWindow.cpp | 23 ++++++++ src/Gui/SpaceballEvent.cpp | 24 ++++++-- src/Gui/SpaceballEvent.h | 20 +++++-- src/Gui/View3DInventorViewer.cpp | 68 +++++++++++----------- 6 files changed, 123 insertions(+), 56 deletions(-) diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index 342934929..c8025328b 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -205,11 +205,11 @@ void ObjectLabelObserver::slotRelabelObject(const App::DocumentObject& obj, cons // make sure that there is a name conflict otherwise we don't have to do anything if (match) { - // remove number from end to avoid lengthy names - size_t lastpos = label.length()-1; - while (label[lastpos] >= 48 && label[lastpos] <= 57) - lastpos--; - label = label.substr(0, lastpos+1); + // remove number from end to avoid lengthy names + size_t lastpos = label.length()-1; + while (label[lastpos] >= 48 && label[lastpos] <= 57) + lastpos--; + label = label.substr(0, lastpos+1); label = Base::Tools::getUniqueName(label, objectLabels, 3); this->current = &obj; const_cast(obj).Label.setValue(label); @@ -1437,7 +1437,7 @@ public: (int)event->type()); } try { - if (event->type() == Spaceball::ButtonEvent::ButtonEventType) + if (event->type() == Spaceball::ButtonEvent::ButtonEventType || Spaceball::MotionEvent::MotionEventType) return processSpaceballEvent(receiver, event); else return QApplication::notify(receiver, event); diff --git a/src/Gui/GuiApplicationNativeEventAware.cpp b/src/Gui/GuiApplicationNativeEventAware.cpp index 74b0c18c4..e741fcfa4 100644 --- a/src/Gui/GuiApplicationNativeEventAware.cpp +++ b/src/Gui/GuiApplicationNativeEventAware.cpp @@ -80,15 +80,31 @@ void Gui::GUIApplicationNativeEventAware::initSpaceball(QMainWindow *window) bool Gui::GUIApplicationNativeEventAware::processSpaceballEvent(QObject *object, QEvent *event) { - Spaceball::ButtonEvent *ballEvent = dynamic_cast(event); - if (!ballEvent) - return true; - QApplication::notify(object, ballEvent); - if (!ballEvent->isHandled()) + QApplication::notify(object, event); + if (event->type() == Spaceball::MotionEvent::MotionEventType) { - //make a new event and post to parent. - Spaceball::ButtonEvent *newEvent = new Spaceball::ButtonEvent(*ballEvent); - postEvent(object->parent(), newEvent); + Spaceball::MotionEvent *motionEvent = dynamic_cast(event); + if (!motionEvent) + return true; + if (!motionEvent->isHandled()) + { + //make a new event and post to parent. + Spaceball::MotionEvent *newEvent = new Spaceball::MotionEvent(*motionEvent); + postEvent(object->parent(), newEvent); + } + } + + if (event->type() == Spaceball::ButtonEvent::ButtonEventType) + { + Spaceball::ButtonEvent *buttonEvent = dynamic_cast(event); + if (!buttonEvent) + return true; + if (!buttonEvent->isHandled()) + { + //make a new event and post to parent. + Spaceball::ButtonEvent *newEvent = new Spaceball::ButtonEvent(*buttonEvent); + postEvent(object->parent(), newEvent); + } } return true; } diff --git a/src/Gui/MainWindow.cpp b/src/Gui/MainWindow.cpp index d534127a3..dcb916ce4 100644 --- a/src/Gui/MainWindow.cpp +++ b/src/Gui/MainWindow.cpp @@ -104,6 +104,8 @@ #include "ViewProviderExtern.h" #include "SpaceballEvent.h" +#include "View3DInventor.h" +#include "View3DInventorViewer.h" #if defined(Q_OS_WIN32) #define slots @@ -596,6 +598,27 @@ bool MainWindow::event(QEvent *e) else return true; } + else if (e->type() == Spaceball::MotionEvent::MotionEventType) { + Spaceball::MotionEvent *motionEvent = dynamic_cast(e); + if (!motionEvent) + return true; + motionEvent->setHandled(true); + Gui::Document *doc = Application::Instance->activeDocument(); + if (!doc) + return true; + View3DInventor *temp = dynamic_cast(doc->getActiveView()); + if (!temp) + return true; + View3DInventorViewer *view = temp->getViewer(); + if (!view) + return true; + QWidget *viewWidget = view->getGLWidget(); + if (viewWidget) { + Spaceball::MotionEvent anotherEvent(*motionEvent); + qApp->sendEvent(viewWidget, &anotherEvent); + } + return true; + } return QMainWindow::event(e); } diff --git a/src/Gui/SpaceballEvent.cpp b/src/Gui/SpaceballEvent.cpp index a5a43b5d8..55efa7a27 100644 --- a/src/Gui/SpaceballEvent.cpp +++ b/src/Gui/SpaceballEvent.cpp @@ -28,11 +28,27 @@ using namespace Spaceball; int MotionEvent::MotionEventType = -1; int ButtonEvent::ButtonEventType = -1; -MotionEvent::MotionEvent() : QInputEvent(static_cast(MotionEventType)), +EventBase::EventBase(QEvent::Type event) : QInputEvent(static_cast(event)), handled(false) +{ + +} + +MotionEvent::MotionEvent() : EventBase(static_cast(MotionEventType)), xTrans(0), yTrans(0), zTrans(0), xRot(0), yRot(0), zRot(0) { } +MotionEvent::MotionEvent(const MotionEvent& in) : EventBase(static_cast(MotionEventType)) +{ + xTrans = in.xTrans; + yTrans = in.yTrans; + zTrans = in.zTrans; + xRot = in.xRot; + yRot = in.yRot; + zRot = in.zRot; + handled = in.handled; +} + void MotionEvent::translations(int &xTransOut, int &yTransOut, int &zTransOut) { xTransOut = xTrans; @@ -62,12 +78,12 @@ void MotionEvent::setRotations(const int &xRotIn, const int &yRotIn, const int & } -ButtonEvent::ButtonEvent() : QInputEvent(static_cast(ButtonEventType)), - buttonState(BUTTON_NONE), button(0), handled(false) +ButtonEvent::ButtonEvent() : EventBase(static_cast(ButtonEventType)), + buttonState(BUTTON_NONE), button(0) { } -ButtonEvent::ButtonEvent(const ButtonEvent& in) : QInputEvent(static_cast(ButtonEventType)) +ButtonEvent::ButtonEvent(const ButtonEvent& in) : EventBase(static_cast(ButtonEventType)) { buttonState = in.buttonState; button = in.button; diff --git a/src/Gui/SpaceballEvent.h b/src/Gui/SpaceballEvent.h index fc7a156e9..b07113e56 100644 --- a/src/Gui/SpaceballEvent.h +++ b/src/Gui/SpaceballEvent.h @@ -28,10 +28,22 @@ namespace Spaceball { enum ButtonStateType {BUTTON_NONE = 0, BUTTON_PRESSED, BUTTON_RELEASED}; - class MotionEvent : public QInputEvent + class EventBase : public QInputEvent + { + public: + bool isHandled(){return handled;} + void setHandled(bool sig){handled = sig;} + + protected: + EventBase(QEvent::Type event); + bool handled; + }; + + class MotionEvent : public EventBase { public: MotionEvent(); + MotionEvent(const MotionEvent& in); void translations(int &xTransOut, int &yTransOut, int &zTransOut); void setTranslations(const int &xTransIn, const int &yTransIn, const int &zTransIn); int translationX(){return xTrans;} @@ -53,9 +65,10 @@ namespace Spaceball int xRot; int yRot; int zRot; + bool handled; }; - class ButtonEvent : public QInputEvent + class ButtonEvent : public EventBase { public: ButtonEvent(); @@ -64,15 +77,12 @@ namespace Spaceball void setButtonStatus(const ButtonStateType &buttonStatusIn); int buttonNumber(); void setButtonNumber(const int &buttonNumberIn); - bool isHandled(){return handled;} - void setHandled(bool in){handled = in;} static int ButtonEventType; private: ButtonStateType buttonState; int button; - bool handled; }; } #endif // SPACEBALLEVENT_H diff --git a/src/Gui/View3DInventorViewer.cpp b/src/Gui/View3DInventorViewer.cpp index 9f0fb2711..6fd035a13 100644 --- a/src/Gui/View3DInventorViewer.cpp +++ b/src/Gui/View3DInventorViewer.cpp @@ -1059,6 +1059,8 @@ void View3DInventorViewer::processEvent(QEvent * event) return; } + motionEvent->setHandled(true); + static float translationConstant(-.001f); float xTrans, yTrans, zTrans; xTrans = static_cast(motionEvent->translationX()); @@ -1379,15 +1381,15 @@ void View3DInventorViewer::viewAll() } //navigation->viewAll(); } - -void View3DInventorViewer::viewAll(float factor) -{ + +void View3DInventorViewer::viewAll(float factor) +{ SoCamera * cam = this->getCamera(); if (!cam) return; if (factor <= 0.0f) return; - - if (factor != 1.0f) { - SoSearchAction sa; + + if (factor != 1.0f) { + SoSearchAction sa; sa.setType(SoSkipBoundingGroup::getClassTypeId()); sa.setInterest(SoSearchAction::ALL); sa.apply(this->getSceneGraph()); @@ -1425,11 +1427,11 @@ void View3DInventorViewer::viewAll(float factor) graph->addChild(cube); cam->viewAll(graph, this->getViewportRegion()); graph->unref(); - } - else { - viewAll(); - } -} + } + else { + viewAll(); + } +} void View3DInventorViewer::viewSelection() { @@ -2053,16 +2055,16 @@ SoPath * View3DInventorViewer::pickFilterCB(void *viewer, const SoPickedPoint * { ViewProvider* vp = static_cast(viewer)->getViewProviderByPath(pp->getPath()); if (vp && vp->useNewSelectionModel()) { - std::string e = vp->getElement(pp); - vp->getSelectionShape(e.c_str()); - static char buf[513]; - snprintf(buf,512,"Hovered: %s (%f,%f,%f)" - ,e.c_str() - ,pp->getPoint()[0] - ,pp->getPoint()[1] - ,pp->getPoint()[2]); - - getMainWindow()->statusBar()->showMessage(QString::fromAscii(buf),3000); + std::string e = vp->getElement(pp); + vp->getSelectionShape(e.c_str()); + static char buf[513]; + snprintf(buf,512,"Hovered: %s (%f,%f,%f)" + ,e.c_str() + ,pp->getPoint()[0] + ,pp->getPoint()[1] + ,pp->getPoint()[2]); + + getMainWindow()->statusBar()->showMessage(QString::fromAscii(buf),3000); } return pp->getPath(); } @@ -2094,18 +2096,18 @@ ViewProvider* View3DInventorViewer::getViewProviderByPath(SoPath * path) const ViewProvider* View3DInventorViewer::getViewProviderByPathFromTail(SoPath * path) const { - // Make sure I'm the lowest LocHL in the pick path! - for (int i = 0; i < path->getLength(); i++) { - SoNode *node = path->getNodeFromTail(i); - if (node->isOfType(SoSeparator::getClassTypeId())) { - std::map::const_iterator it = _ViewProviderMap.find(static_cast(node)); - if (it != _ViewProviderMap.end()){ - return it->second; - } - } - } - - return 0; + // Make sure I'm the lowest LocHL in the pick path! + for (int i = 0; i < path->getLength(); i++) { + SoNode *node = path->getNodeFromTail(i); + if (node->isOfType(SoSeparator::getClassTypeId())) { + std::map::const_iterator it = _ViewProviderMap.find(static_cast(node)); + if (it != _ViewProviderMap.end()){ + return it->second; + } + } + } + + return 0; } std::vector View3DInventorViewer::getViewProvidersOfType(const Base::Type& typeId) const