+++ /dev/null
-// eventhandler.cpp
-//
-// Part of the Architektonas Project
-// Originally part of QCad Community Edition by Andrew Mustun
-// Extensively rewritten and refactored by James L. Hammons
-// Portions copyright (C) 2001-2003 RibbonSoft
-// Copyright (C) 2010 Underground Software
-// See the README and GPLv2 files for licensing and warranty information
-//
-// JLH = James L. Hammons <jlhamm@acm.org>
-//
-// Who When What
-// --- ---------- -----------------------------------------------------------
-// JLH 05/28/2010 Added this text. :-)
-//
-
-// This is only used by the GraphicView class...
-
-#include "eventhandler.h"
-
-#include "actioninterface.h"
-#include "commandevent.h"
-#include "debug.h"
-#include "dialogfactory.h"
-#include "graphicview.h"
-#include "mathextra.h"
-
-/**
- * Constructor.
- */
-EventHandler::EventHandler()://GraphicView * graphicView)
- defaultAction(NULL), actionIndex(-1), coordinateInputEnabled(true)
-{
-// this->graphicView = graphicView;
-// actionIndex = -1;
-
- for(int i=0; i<RS_MAXACTIONS; i++)
- currentActions[i] = NULL;
-
-// coordinateInputEnabled = true;
-// defaultAction = NULL;
-}
-
-/**
- * Destructor.
- */
-EventHandler::~EventHandler()
-{
- DEBUG->print("EventHandler::~EventHandler");
-
- if (defaultAction)
- {
- defaultAction->finish();
- delete defaultAction;
- defaultAction = NULL;
- }
-
- KillAllActions();
-
- DEBUG->print("EventHandler::~EventHandler: Deleting all actions..");
-
- for(int i=0; i<RS_MAXACTIONS; ++i)
- {
- if (currentActions[i] != NULL)
- {
- currentActions[i]->setFinished();
- //delete currentActions[i];
- //currentActions[i] = NULL;
- }
- }
-
- CleanUp();
- DEBUG->print("EventHandler::~EventHandler: Deleting all actions..: OK");
- DEBUG->print("EventHandler::~EventHandler: OK");
-}
-
-/**
- * Go back in current action.
- */
-void EventHandler::Back()
-{
- QMouseEvent e(QEvent::MouseButtonRelease, QPoint(0, 0), Qt::RightButton, Qt::RightButton,
- Qt::NoModifier);
- MouseReleaseEvent(&e);
-}
-
-/**
- * Go enter pressed event for current action.
- */
-void EventHandler::Enter()
-{
-// QKeyEvent e(QEvent::KeyPress, Qt::Key_Enter, '\n', 0);
- QKeyEvent e(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier, "\n", false, 0);
- KeyPressEvent(&e);
-}
-
-/**
- * Called by GraphicView
- */
-void EventHandler::MousePressEvent(QMouseEvent * e)
-{
- if (actionIndex >= 0 && currentActions[actionIndex] != NULL)
- {
- currentActions[actionIndex]->mousePressEvent(e);
- e->accept();
- }
- else
- {
- if (defaultAction)
- {
- defaultAction->mousePressEvent(e);
- e->accept();
- }
- else
- {
- DEBUG->print("currently no action defined");
- e->ignore();
- }
- }
-}
-
-/**
- * Called by GraphicView
- */
-void EventHandler::MouseReleaseEvent(QMouseEvent * e)
-{
- if (actionIndex >= 0 && currentActions[actionIndex] != NULL
- && !currentActions[actionIndex]->isFinished())
- {
- DEBUG->print("call action %s", currentActions[actionIndex]->getName().toLatin1().data());
- currentActions[actionIndex]->mouseReleaseEvent(e);
-
- // Clean up actions - one might be finished now
- CleanUp();
- e->accept();
- }
- else
- {
- if (defaultAction)
- defaultAction->mouseReleaseEvent(e);
- else
- e->ignore();
- }
-}
-
-/**
- * Called by GraphicView
- */
-void EventHandler::MouseMoveEvent(QMouseEvent * e)
-{
- if (actionIndex >= 0 && currentActions[actionIndex]
- && !currentActions[actionIndex]->isFinished())
- {
- currentActions[actionIndex]->mouseMoveEvent(e);
- e->accept();
- }
- else
- {
- if (defaultAction)
- {
- defaultAction->mouseMoveEvent(e);
- e->accept();
- }
- else
- e->ignore();
- //DEBUG->print("currently no action defined");
- }
-}
-
-#if 0
-Small problem with this approach: Resumes can happen before suspend actions!
-This can wreak havoc with things like snapper/preview states and the like...
-
-Actually, this stuff seems to be pretty useless. Not sure what is accomplished
-by this crap. The only thing I can think of is that it would cause a preview
-to be removed if the mouse goes out of the drawing window, could be a legit
-purpose but this seems too retarded for that. Why wouldn't you just code that
-into GraphicView itself and be done with it?
-#endif
-/**
- * Called by GraphicView
- */
-void EventHandler::MouseLeaveEvent()
-{
-#if 0
- if (actionIndex >= 0 && currentActions[actionIndex]
- && !currentActions[actionIndex]->isFinished())
- {
- currentActions[actionIndex]->suspend();
- }
- else
- {
- if (defaultAction)
- defaultAction->suspend();
- //DEBUG->print("currently no action defined");
- }
-#endif
-}
-
-/**
- * Called by GraphicView
- */
-void EventHandler::MouseEnterEvent()
-{
-#if 0
- if (actionIndex >= 0 && currentActions[actionIndex]
- && !currentActions[actionIndex]->isFinished())
- {
- currentActions[actionIndex]->resume();
- }
- else
- {
- if (defaultAction)
- defaultAction->resume();
- }
-#endif
-}
-
-/**
- * Called by GraphicView
- */
-void EventHandler::KeyPressEvent(QKeyEvent * e)
-{
- if (actionIndex >= 0 && currentActions[actionIndex]
- && !currentActions[actionIndex]->isFinished())
- {
- currentActions[actionIndex]->keyPressEvent(e);
- }
- else
- {
- if (defaultAction)
- defaultAction->keyPressEvent(e);
- else
- e->ignore();
-
- //DEBUG->print("currently no action defined");
- }
-}
-
-/**
- * Called by GraphicView
- */
-void EventHandler::KeyReleaseEvent(QKeyEvent * e)
-{
- if (actionIndex >= 0 && currentActions[actionIndex]
- && !currentActions[actionIndex]->isFinished())
- {
- currentActions[actionIndex]->keyReleaseEvent(e);
- }
- else
- {
- if (defaultAction)
- defaultAction->keyReleaseEvent(e);
- else
- e->ignore();
-
- //DEBUG->print("currently no action defined");
- }
-}
-
-/**
- * Handles command line events.
- */
-void EventHandler::HandleCommandEvent(GraphicView * graphicView, CommandEvent * e)
-{
- DEBUG->print("EventHandler::commandEvent");
- QString cmd = e->getCommand();
-
- if (coordinateInputEnabled)
- {
- if (!e->isAccepted())
- {
- // handle absolute cartesian coordinate input:
- if (cmd.contains(',') && cmd.at(0) != '@')
- {
- if (actionIndex >= 0 && currentActions[actionIndex] != NULL
- && !currentActions[actionIndex]->isFinished())
- {
- int commaPos = cmd.indexOf(',');
- DEBUG->print("EventHandler::commandEvent: 001");
- bool ok1, ok2;
- DEBUG->print("EventHandler::commandEvent: 002");
- double x = Math::eval(cmd.left(commaPos), &ok1);
- DEBUG->print("EventHandler::commandEvent: 003a");
- double y = Math::eval(cmd.mid(commaPos + 1), &ok2);
- DEBUG->print("EventHandler::commandEvent: 004");
-
- if (ok1 && ok2)
- {
- DEBUG->print("EventHandler::commandEvent: 006");
- Vector ce(x, y);
- currentActions[actionIndex]->coordinateEvent(&ce);
- }
- else
- {
- if (DIALOGFACTORY)
- DIALOGFACTORY->commandMessage("Expression Syntax Error");
- }
-
- e->accept();
- }
- }
- }
-
- // handle relative cartesian coordinate input:
- if (!e->isAccepted())
- {
- if (cmd.contains(',') && cmd.at(0) == '@')
- {
- if (actionIndex >= 0 && currentActions[actionIndex] != NULL
- && !currentActions[actionIndex]->isFinished())
- {
- int commaPos = cmd.indexOf(',');
- bool ok1, ok2;
- double x = Math::eval(cmd.mid(1, commaPos - 1), &ok1);
- double y = Math::eval(cmd.mid(commaPos + 1), &ok2);
-
- if (ok1 && ok2)
- {
- Vector ce(Vector(x,y) + graphicView->getRelativeZero());
- currentActions[actionIndex]->coordinateEvent(&ce);
- }
- else
- {
- if (DIALOGFACTORY)
- DIALOGFACTORY->commandMessage("Expression Syntax Error");
- }
-
- e->accept();
- }
- }
- }
-
- // handle absolute polar coordinate input:
- if (!e->isAccepted())
- {
- if (cmd.contains('<') && cmd.at(0) != '@')
- {
- if (actionIndex >= 0 && currentActions[actionIndex] != NULL
- && !currentActions[actionIndex]->isFinished())
- {
- int commaPos = cmd.indexOf('<');
- bool ok1, ok2;
- double r = Math::eval(cmd.left(commaPos), &ok1);
- double a = Math::eval(cmd.mid(commaPos + 1), &ok2);
-
- if (ok1 && ok2)
- {
- Vector pos;
- pos.setPolar(r,Math::deg2rad(a));
- currentActions[actionIndex]->coordinateEvent(&pos);
- }
- else
- {
- if (DIALOGFACTORY)
- DIALOGFACTORY->commandMessage("Expression Syntax Error");
- }
-
- e->accept();
- }
- }
- }
-
- // handle relative polar coordinate input:
- if (!e->isAccepted())
- {
- if (cmd.contains('<') && cmd.at(0) == '@')
- {
- if (actionIndex >= 0 && currentActions[actionIndex] !=NULL
- && !currentActions[actionIndex]->isFinished())
- {
- int commaPos = cmd.indexOf('<');
- bool ok1, ok2;
- double r = Math::eval(cmd.mid(1, commaPos - 1), &ok1);
- double a = Math::eval(cmd.mid(commaPos + 1), &ok2);
-
- if (ok1 && ok2)
- {
- Vector pos;
- pos.setPolar(r,Math::deg2rad(a));
- Vector ce(pos + graphicView->getRelativeZero());
- currentActions[actionIndex]->coordinateEvent(&ce);
- }
- else
- {
- if (DIALOGFACTORY)
- DIALOGFACTORY->commandMessage("Expression Syntax Error");
- }
-
- e->accept();
- }
- }
- }
- }
-
- // Send command event directly to current action:
- if (!e->isAccepted())
- {
- if (actionIndex >= 0 && currentActions[actionIndex]
- && !currentActions[actionIndex]->isFinished())
- {
- currentActions[actionIndex]->commandEvent(e);
- e->accept();
- }
- else
- {
- if (defaultAction)
- defaultAction->commandEvent(e);
- }
- }
-
- DEBUG->print("EventHandler::commandEvent: OK");
-}
-
-/**
- * Enables coordinate input in the command line.
- */
-void EventHandler::EnableCoordinateInput()
-{
- coordinateInputEnabled = true;
-}
-
-/**
- * Enables coordinate input in the command line.
- */
-void EventHandler::DisableCoordinateInput()
-{
- coordinateInputEnabled = false;
-}
-
-/**
- * @return Current action.
- */
-ActionInterface * EventHandler::GetCurrentAction()
-{
- if (actionIndex >= 0 && currentActions[actionIndex]
- && !currentActions[actionIndex]->isFinished())
- return currentActions[actionIndex];
-
- return defaultAction;
-}
-
-/**
- * @return The current default action.
- */
-ActionInterface * EventHandler::GetDefaultAction()
-{
- return defaultAction;
-}
-
-/**
- * Sets the default action.
- */
-void EventHandler::SetDefaultAction(ActionInterface * action)
-{
- if (defaultAction)
- {
- defaultAction->finish();
- delete defaultAction;
- defaultAction = NULL;
- }
-
- defaultAction = action;
-}
-
-/**
- * Sets the current action.
- */
-void EventHandler::SetCurrentAction(ActionInterface * action)
-{
- DEBUG->print("EventHandler::setCurrentAction");
-
- if (!action)
- return;
-
- // Predecessor of the new action or NULL:
- ActionInterface * predecessor = NULL;
-
- // Suspend current action:
- if (actionIndex >= 0 && currentActions[actionIndex]
- && !currentActions[actionIndex]->isFinished())
- {
- predecessor = currentActions[actionIndex];
- predecessor->suspend();
- predecessor->hideOptions();
- }
- else
- {
- if (defaultAction)
- {
- predecessor = defaultAction;
- predecessor->suspend();
- predecessor->hideOptions();
- }
- }
-
- // Forget about the oldest action and make space for the new action:
- if (actionIndex == RS_MAXACTIONS - 1)
- {
- // Delete oldest action if necessary (usually never happens):
- if (currentActions[0])
- {
- currentActions[0]->finish();
- delete currentActions[0];
- currentActions[0] = NULL;
- }
-
- // 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;
- DEBUG->print("EventHandler::setCurrentAction: current action is: %s",
- currentActions[actionIndex]->getName().toLatin1().data());
-
- // Initialisation of our new action:
- DEBUG->print("EventHandler::setCurrentAction: init current action");
- action->init();
-
- // ## new:
- if (action->isFinished() == false)
- {
- DEBUG->print("EventHandler::setCurrentAction: show options");
- currentActions[actionIndex]->showOptions();
- DEBUG->print("EventHandler::setCurrentAction: set predecessor");
- action->setPredecessor(predecessor);
- }
-
- DEBUG->print("EventHandler::setCurrentAction: cleaning up..");
- CleanUp();
-
- DEBUG->print("EventHandler::setCurrentAction: debugging actions");
- DebugActions();
- DEBUG->print("GraphicView::setCurrentAction: OK");
-}
-
-/**
- * Kills all running selection actions. Called when a selection action
- * is launched to reduce confusion.
- */
-void EventHandler::KillSelectActions()
-{
- for(int c=0; c<RS_MAXACTIONS; ++c)
- {
- if (currentActions[c])
- {
- if (currentActions[c]->rtti() == RS2::ActionSelectSingle
- || currentActions[c]->rtti() == RS2::ActionSelectContour
- || currentActions[c]->rtti() == RS2::ActionSelectWindow
- || currentActions[c]->rtti() == RS2::ActionSelectIntersected
- || currentActions[c]->rtti() == RS2::ActionSelectLayer)
- currentActions[c]->finish();
- }
- }
-}
-
-/**
- * Kills all running actions. Called when a window is closed.
- * Actually: It does NOTHING
- */
-void EventHandler::KillAllActions()
-{
- /*
- for (int c=0; c<RS_MAXACTIONS; ++c) {
- if (currentActions[c]!=NULL) {
- currentActions[c]->finish();
- }
- }
- cleanUp();
- */
-}
-
-/**
- * @return true if there is at least one action in the action stack.
- */
-bool EventHandler::HasAction()
-{
- if (actionIndex != -1 || defaultAction)
- return true;
-
- return false;
-}
-
-/**
- * Garbage collector for actions.
- */
-void EventHandler::CleanUp()
-{
- DEBUG->print("EventHandler::cleanUp");
-
- int oldIndex = 0; // old index
- int newIndex = 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 (oldIndex)
- while (currentActions[oldIndex] == NULL && oldIndex < RS_MAXACTIONS)
- oldIndex++;
-
- // Delete action if it is finished
- if (oldIndex < RS_MAXACTIONS && currentActions[oldIndex] != NULL
- && currentActions[oldIndex]->isFinished())
- {
- delete currentActions[oldIndex];
- currentActions[oldIndex] = NULL;
- doResume = true;
- }
-
- // Move a running action up in the stack
- if (oldIndex < RS_MAXACTIONS && currentActions[oldIndex] != NULL)
- {
- if (newIndex != oldIndex)
- {
- currentActions[newIndex] = currentActions[oldIndex];
- resume = newIndex;
- currentActions[oldIndex] = NULL;
- }
- else
- {
- if (oldIndex < RS_MAXACTIONS)
- oldIndex++;
- }
-
- actionIndex = newIndex;
-
- if (newIndex < RS_MAXACTIONS - 1)
- newIndex++;
- }
- }
- while (oldIndex < 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();
- }
- }
- }
-
- DEBUG->print("EventHandler::cleanUp: OK");
-}
-
-void EventHandler::DebugActions()
-{
- DEBUG->print("---");
-
- for(int c=0; c<RS_MAXACTIONS; ++c)
- {
- if (c == actionIndex)
- DEBUG->print("Current");
-
- if (currentActions[c])
- DEBUG->print("Action %03d: %s [%s]",
- c, currentActions[c]->getName().toLatin1().data(),
- currentActions[c]->isFinished() ? "finished" : "active");
- else
- DEBUG->print("Action %03d: NULL", c);
- }
-}