]> Shamusworld >> Repos - architektonas/blobdiff - src/base/rs_eventhandler.cpp
In the middle of major refactoring...
[architektonas] / src / base / rs_eventhandler.cpp
index 9f68a951f1d9515b3718bc3a28f0002ce8a887a0..3484ca9f159b11e83d4dee41b4a81219706c2c3b 100644 (file)
 
 #include "rs_eventhandler.h"
 
-#include <QtGui>
-#include "rs_actioninterface.h"
-#include "rs_coordinateevent.h"
+#include "actioninterface.h"
+#include "rs_commandevent.h"
+#include "rs_debug.h"
+#include "rs_dialogfactory.h"
+#include "graphicview.h"
+#include "rs_math.h"
 
 /**
  * Constructor.
  */
-RS_EventHandler::RS_EventHandler(RS_GraphicView * graphicView)
+RS_EventHandler::RS_EventHandler(GraphicView * graphicView)
 {
        this->graphicView = graphicView;
        actionIndex = -1;
@@ -87,7 +90,7 @@ void RS_EventHandler::enter()
 }
 
 /**
- * Called by RS_GraphicView
+ * Called by GraphicView
  */
 void RS_EventHandler::mousePressEvent(QMouseEvent * e)
 {
@@ -112,7 +115,7 @@ void RS_EventHandler::mousePressEvent(QMouseEvent * e)
 }
 
 /**
- * Called by RS_GraphicView
+ * Called by GraphicView
  */
 void RS_EventHandler::mouseReleaseEvent(QMouseEvent * e)
 {
@@ -128,7 +131,7 @@ void RS_EventHandler::mouseReleaseEvent(QMouseEvent * e)
        }
        else
        {
-               if (defaultAction != NULL)
+               if (defaultAction)
                        defaultAction->mouseReleaseEvent(e);
                else
                        e->ignore();
@@ -136,11 +139,11 @@ void RS_EventHandler::mouseReleaseEvent(QMouseEvent * e)
 }
 
 /**
- * Called by RS_GraphicView
+ * Called by GraphicView
  */
 void RS_EventHandler::mouseMoveEvent(QMouseEvent * e)
 {
-       if (actionIndex >= 0 && currentActions[actionIndex] != NULL
+       if (actionIndex >= 0 && currentActions[actionIndex]
                && !currentActions[actionIndex]->isFinished())
        {
                currentActions[actionIndex]->mouseMoveEvent(e);
@@ -148,7 +151,7 @@ void RS_EventHandler::mouseMoveEvent(QMouseEvent * e)
        }
        else
        {
-               if (defaultAction!=NULL)
+               if (defaultAction)
                {
                        defaultAction->mouseMoveEvent(e);
                        e->accept();
@@ -160,53 +163,53 @@ void RS_EventHandler::mouseMoveEvent(QMouseEvent * e)
 }
 
 /**
- * Called by RS_GraphicView
+ * Called by GraphicView
  */
 void RS_EventHandler::mouseLeaveEvent()
 {
-       if (actionIndex >= 0 && currentActions[actionIndex] != NULL
+       if (actionIndex >= 0 && currentActions[actionIndex]
                && !currentActions[actionIndex]->isFinished())
        {
                currentActions[actionIndex]->suspend();
        }
        else
        {
-               if (defaultAction != NULL)
+               if (defaultAction)
                        defaultAction->suspend();
                //RS_DEBUG->print("currently no action defined");
        }
 }
 
 /**
- * Called by RS_GraphicView
+ * Called by GraphicView
  */
 void RS_EventHandler::mouseEnterEvent()
 {
-       if (actionIndex >= 0 && currentActions[actionIndex] != NULL
+       if (actionIndex >= 0 && currentActions[actionIndex]
                && !currentActions[actionIndex]->isFinished())
        {
                currentActions[actionIndex]->resume();
        }
        else
        {
-               if (defaultAction != NULL)
+               if (defaultAction)
                        defaultAction->resume();
        }
 }
 
 /**
- * Called by RS_GraphicView
+ * Called by GraphicView
  */
 void RS_EventHandler::keyPressEvent(QKeyEvent * e)
 {
-       if (actionIndex >= 0 && currentActions[actionIndex] != NULL
+       if (actionIndex >= 0 && currentActions[actionIndex]
                && !currentActions[actionIndex]->isFinished())
        {
                currentActions[actionIndex]->keyPressEvent(e);
        }
        else
        {
-               if (defaultAction != NULL)
+               if (defaultAction)
                        defaultAction->keyPressEvent(e);
                else
                        e->ignore();
@@ -216,22 +219,24 @@ void RS_EventHandler::keyPressEvent(QKeyEvent * e)
 }
 
 /**
- * Called by RS_GraphicView
+ * Called by GraphicView
  */
-void RS_EventHandler::keyReleaseEvent(QKeyEvent* e)
+void RS_EventHandler::keyReleaseEvent(QKeyEvent * e)
 {
-    if (actionIndex>=0 && currentActions[actionIndex]!=NULL &&
-            !currentActions[actionIndex]->isFinished()) {
-        currentActions[actionIndex]->keyReleaseEvent(e);
-    } else {
-        if (defaultAction!=NULL) {
-            defaultAction->keyReleaseEvent(e);
-        }
-               else {
+       if (actionIndex >= 0 && currentActions[actionIndex]
+               && !currentActions[actionIndex]->isFinished())
+       {
+               currentActions[actionIndex]->keyReleaseEvent(e);
+       }
+       else
+       {
+               if (defaultAction)
+                       defaultAction->keyReleaseEvent(e);
+               else
                        e->ignore();
-               }
-        //RS_DEBUG->print("currently no action defined");
-    }
+
+               //RS_DEBUG->print("currently no action defined");
+       }
 }
 
 /**
@@ -252,7 +257,6 @@ void RS_EventHandler::commandEvent(RS_CommandEvent * e)
                                if (actionIndex >= 0 && currentActions[actionIndex] != NULL
                                        && !currentActions[actionIndex]->isFinished())
                                {
-//                                     int commaPos = cmd.find(',');
                                        int commaPos = cmd.indexOf(',');
                                        RS_DEBUG->print("RS_EventHandler::commandEvent: 001");
                                        bool ok1, ok2;
@@ -264,9 +268,11 @@ void RS_EventHandler::commandEvent(RS_CommandEvent * e)
 
                                        if (ok1 && ok2)
                                        {
-                                               RS_DEBUG->print("RS_EventHandler::commandEvent: 005");
-                                               RS_CoordinateEvent ce(Vector(x, y));
+//                                             RS_DEBUG->print("RS_EventHandler::commandEvent: 005");
+//                                             RS_CoordinateEvent ce(Vector(x, y));
                                                RS_DEBUG->print("RS_EventHandler::commandEvent: 006");
+//                                             currentActions[actionIndex]->coordinateEvent(&ce);
+                                               Vector ce(x, y);
                                                currentActions[actionIndex]->coordinateEvent(&ce);
                                        }
                                        else
@@ -288,7 +294,6 @@ void RS_EventHandler::commandEvent(RS_CommandEvent * e)
                                if (actionIndex >= 0 && currentActions[actionIndex] != NULL
                                        && !currentActions[actionIndex]->isFinished())
                                {
-//                                     int commaPos = cmd.find(',');
                                        int commaPos = cmd.indexOf(',');
                                        bool ok1, ok2;
                                        double x = RS_Math::eval(cmd.mid(1, commaPos - 1), &ok1);
@@ -296,8 +301,9 @@ void RS_EventHandler::commandEvent(RS_CommandEvent * e)
 
                                        if (ok1 && ok2)
                                        {
-                                               RS_CoordinateEvent ce(Vector(x,y) +
-                                                       graphicView->getRelativeZero());
+//                                             RS_CoordinateEvent ce(Vector(x,y) + graphicView->getRelativeZero());
+//                                             currentActions[actionIndex]->coordinateEvent(&ce);
+                                               Vector ce(Vector(x,y) + graphicView->getRelativeZero());
                                                currentActions[actionIndex]->coordinateEvent(&ce);
                                        }
                                        else
@@ -319,7 +325,6 @@ void RS_EventHandler::commandEvent(RS_CommandEvent * e)
                                if (actionIndex >= 0 && currentActions[actionIndex] != NULL
                                        && !currentActions[actionIndex]->isFinished())
                                {
-//                                     int commaPos = cmd.find('<');
                                        int commaPos = cmd.indexOf('<');
                                        bool ok1, ok2;
                                        double r = RS_Math::eval(cmd.left(commaPos), &ok1);
@@ -329,8 +334,9 @@ void RS_EventHandler::commandEvent(RS_CommandEvent * e)
                                        {
                                                Vector pos;
                                                pos.setPolar(r,RS_Math::deg2rad(a));
-                                               RS_CoordinateEvent ce(pos);
-                                               currentActions[actionIndex]->coordinateEvent(&ce);
+//                                             RS_CoordinateEvent ce(pos);
+//                                             currentActions[actionIndex]->coordinateEvent(&ce);
+                                               currentActions[actionIndex]->coordinateEvent(&pos);
                                        }
                                        else
                                        {
@@ -361,13 +367,14 @@ void RS_EventHandler::commandEvent(RS_CommandEvent * e)
                                        {
                                                Vector pos;
                                                pos.setPolar(r,RS_Math::deg2rad(a));
-                                               RS_CoordinateEvent ce(pos +
-                                                       graphicView->getRelativeZero());
+//                                             RS_CoordinateEvent ce(pos + graphicView->getRelativeZero());
+//                                             currentActions[actionIndex]->coordinateEvent(&ce);
+                                               Vector ce(pos + graphicView->getRelativeZero());
                                                currentActions[actionIndex]->coordinateEvent(&ce);
                                        }
                                        else
                                        {
-                                               if (RS_DIALOGFACTORY != NULL)
+                                               if (RS_DIALOGFACTORY)
                                                        RS_DIALOGFACTORY->commandMessage("Expression Syntax Error");
                                        }
 
@@ -380,7 +387,7 @@ void RS_EventHandler::commandEvent(RS_CommandEvent * e)
        // send command event directly to current action:
        if (!e->isAccepted())
        {
-               if (actionIndex >= 0 && currentActions[actionIndex] != NULL
+               if (actionIndex >= 0 && currentActions[actionIndex]
                        && !currentActions[actionIndex]->isFinished())
                {
                        currentActions[actionIndex]->commandEvent(e);
@@ -388,11 +395,8 @@ void RS_EventHandler::commandEvent(RS_CommandEvent * e)
                }
                else
                {
-                       if (defaultAction != NULL)
-                       {
+                       if (defaultAction)
                                defaultAction->commandEvent(e);
-                               //e->accept();
-                       }
                }
        }
 
@@ -418,9 +422,9 @@ void RS_EventHandler::disableCoordinateInput()
 /**
  * @return Current action.
  */
-RS_ActionInterface * RS_EventHandler::getCurrentAction()
+ActionInterface * RS_EventHandler::getCurrentAction()
 {
-       if (actionIndex >= 0 && currentActions[actionIndex] != NULL
+       if (actionIndex >= 0 && currentActions[actionIndex]
                && !currentActions[actionIndex]->isFinished())
                return currentActions[actionIndex];
 
@@ -430,7 +434,7 @@ RS_ActionInterface * RS_EventHandler::getCurrentAction()
 /**
  * @return The current default action.
  */
-RS_ActionInterface * RS_EventHandler::getDefaultAction()
+ActionInterface * RS_EventHandler::getDefaultAction()
 {
        return defaultAction;
 }
@@ -438,9 +442,9 @@ RS_ActionInterface * RS_EventHandler::getDefaultAction()
 /**
  * Sets the default action.
  */
-void RS_EventHandler::setDefaultAction(RS_ActionInterface * action)
+void RS_EventHandler::setDefaultAction(ActionInterface * action)
 {
-       if (defaultAction != NULL)
+       if (defaultAction)
        {
                defaultAction->finish();
                delete defaultAction;
@@ -453,18 +457,18 @@ void RS_EventHandler::setDefaultAction(RS_ActionInterface * action)
 /**
  * Sets the current action.
  */
-void RS_EventHandler::setCurrentAction(RS_ActionInterface * action)
+void RS_EventHandler::setCurrentAction(ActionInterface * action)
 {
        RS_DEBUG->print("RS_EventHandler::setCurrentAction");
 
-       if (action == NULL)
+       if (!action)
                return;
 
        // Predecessor of the new action or NULL:
-       RS_ActionInterface* predecessor = NULL;
+       ActionInterface * predecessor = NULL;
 
        // Suspend current action:
-       if (actionIndex >= 0 && currentActions[actionIndex] != NULL
+       if (actionIndex >= 0 && currentActions[actionIndex]
                && !currentActions[actionIndex]->isFinished())
        {
                predecessor = currentActions[actionIndex];
@@ -473,7 +477,7 @@ void RS_EventHandler::setCurrentAction(RS_ActionInterface * action)
        }
        else
        {
-               if (defaultAction != NULL)
+               if (defaultAction)
                {
                        predecessor = defaultAction;
                        predecessor->suspend();
@@ -485,7 +489,7 @@ void RS_EventHandler::setCurrentAction(RS_ActionInterface * action)
        if (actionIndex == RS_MAXACTIONS - 1)
        {
                // delete oldest action if necessary (usually never happens):
-               if (currentActions[0] != NULL)
+               if (currentActions[0])
                {
                        currentActions[0]->finish();
                        delete currentActions[0];
@@ -494,14 +498,10 @@ void RS_EventHandler::setCurrentAction(RS_ActionInterface * action)
 
                // Move up actionstack (optimize):
                for(int i=0; i<RS_MAXACTIONS-1; ++i)
-               {
                        currentActions[i] = currentActions[i + 1];
-               }
        }
        else if (actionIndex < RS_MAXACTIONS - 1)
-       {
                actionIndex++;
-       }
 
        // Set current action:
        currentActions[actionIndex] = action;
@@ -526,7 +526,7 @@ void RS_EventHandler::setCurrentAction(RS_ActionInterface * action)
 
        RS_DEBUG->print("RS_EventHandler::setCurrentAction: debugging actions");
        debugActions();
-       RS_DEBUG->print("RS_GraphicView::setCurrentAction: OK");
+       RS_DEBUG->print("GraphicView::setCurrentAction: OK");
 }
 
 /**
@@ -537,7 +537,7 @@ void RS_EventHandler::killSelectActions()
 {
        for(int c=0; c<RS_MAXACTIONS; ++c)
        {
-               if (currentActions[c] != NULL)
+               if (currentActions[c])
                {
                        if (currentActions[c]->rtti() == RS2::ActionSelectSingle
                                || currentActions[c]->rtti() == RS2::ActionSelectContour
@@ -569,7 +569,7 @@ void RS_EventHandler::killAllActions()
  */
 bool RS_EventHandler::hasAction()
 {
-       if (actionIndex != -1 || defaultAction != NULL)
+       if (actionIndex != -1 || defaultAction)
                return true;
 
        return false;
@@ -580,65 +580,75 @@ bool RS_EventHandler::hasAction()
  */
 void RS_EventHandler::cleanUp()
 {
-    RS_DEBUG->print("RS_EventHandler::cleanUp");
-
-    int o=0;   // old index
-    int n=0;   // new index
-    int resume=0; // index of action to resume
-    bool doResume=false; // do we need to resume an action
-    actionIndex = -1;
-
-    debugActions();
-    do {
-        // search first used action (o)
-        while (currentActions[o]==NULL && o<RS_MAXACTIONS) {
-            o++;
-        }
+       RS_DEBUG->print("RS_EventHandler::cleanUp");
 
-        // delete action if it is finished
-        if (o<RS_MAXACTIONS && currentActions[o]!=NULL &&
-                currentActions[o]->isFinished()) {
-            delete currentActions[o];
-            currentActions[o] = NULL;
+       int o = 0;                              // old index
+       int n = 0;                              // new index
+       int resume = 0;                 // index of action to resume
+       bool doResume = false;  // do we need to resume an action
+       actionIndex = -1;
 
-            doResume = true;
-        }
+       debugActions();
 
-        // move a running action up in the stack
-        if (o<RS_MAXACTIONS && currentActions[o]!=NULL) {
-            if (n!=o) {
-                currentActions[n] = currentActions[o];
-                resume = n;
-                currentActions[o] = NULL;
-            } else {
-                if (o<RS_MAXACTIONS) {
-                    o++;
-                }
-            }
-            actionIndex = n;
-            if (n<RS_MAXACTIONS-1) {
-                n++;
-            }
-        }
-    } while (o<RS_MAXACTIONS);
-
-    debugActions();
-
-    // Resume last used action:
-    if (doResume) {
-        if (currentActions[resume]!=NULL &&
-                !currentActions[resume]->isFinished()) {
-
-            currentActions[resume]->resume();
-            currentActions[resume]->showOptions();
-        } else {
-            if (defaultAction!=NULL) {
-                defaultAction->resume();
-                       defaultAction->showOptions();
-            }
-        }
-    }
-    RS_DEBUG->print("RS_EventHandler::cleanUp: OK");
+       do
+       {
+               // search first used action (o)
+               while (currentActions[o] == NULL && o < RS_MAXACTIONS)
+                       o++;
+
+               // delete action if it is finished
+               if (o < RS_MAXACTIONS && currentActions[o] != NULL
+                       && currentActions[o]->isFinished())
+               {
+                       delete currentActions[o];
+                       currentActions[o] = NULL;
+                       doResume = true;
+               }
+
+               // move a running action up in the stack
+               if (o < RS_MAXACTIONS && currentActions[o] != NULL)
+               {
+                       if (n != o)
+                       {
+                               currentActions[n] = currentActions[o];
+                               resume = n;
+                               currentActions[o] = NULL;
+                       }
+                       else
+                       {
+                               if (o < RS_MAXACTIONS)
+                                       o++;
+                       }
+
+                       actionIndex = n;
+
+                       if (n < RS_MAXACTIONS - 1)
+                               n++;
+               }
+       }
+       while (o < RS_MAXACTIONS);
+
+       debugActions();
+
+       // Resume last used action:
+       if (doResume)
+       {
+               if (currentActions[resume] && !currentActions[resume]->isFinished())
+               {
+                       currentActions[resume]->resume();
+                       currentActions[resume]->showOptions();
+               }
+               else
+               {
+                       if (defaultAction)
+                       {
+                               defaultAction->resume();
+                               defaultAction->showOptions();
+                       }
+               }
+       }
+
+       RS_DEBUG->print("RS_EventHandler::cleanUp: OK");
 }
 
 /**
@@ -646,12 +656,17 @@ void RS_EventHandler::cleanUp()
  */
 void RS_EventHandler::setSnapMode(RS2::SnapMode sm)
 {
+#if 0
        for(int c=0; c<RS_MAXACTIONS; ++c)
-               if (currentActions[c] != NULL)
+               if (currentActions[c])
                        currentActions[c]->setSnapMode(sm);
 
-       if (defaultAction!=NULL)
+       if (defaultAction)
                defaultAction->setSnapMode(sm);
+#else
+#warning "!!! Not sure if this is going to work correctly..."
+       graphicView->snapper.setSnapMode(sm);
+#endif
 }
 
 /**
@@ -659,12 +674,17 @@ void RS_EventHandler::setSnapMode(RS2::SnapMode sm)
  */
 void RS_EventHandler::setSnapRestriction(RS2::SnapRestriction sr)
 {
+#if 0
        for(int c=0; c<RS_MAXACTIONS; ++c)
-               if (currentActions[c] != NULL)
+               if (currentActions[c])
                        currentActions[c]->setSnapRestriction(sr);
 
-       if (defaultAction != NULL)
+       if (defaultAction)
                defaultAction->setSnapRestriction(sr);
+#else
+#warning "!!! Not sure if this is going to work correctly..."
+       graphicView->snapper.setSnapRestriction(sr);
+#endif
 }
 
 void RS_EventHandler::debugActions()
@@ -676,7 +696,7 @@ void RS_EventHandler::debugActions()
                if (c == actionIndex)
                        RS_DEBUG->print("Current");
 
-               if (currentActions[c] != NULL)
+               if (currentActions[c])
                        RS_DEBUG->print("Action %03d: %s [%s]",
                                c, currentActions[c]->getName().toLatin1().data(),
                                currentActions[c]->isFinished() ? "finished" : "active");