| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include "rs_eventhandler.h" |
| | #include <QMouseEvent> |
| |
|
| | #include "lc_coordinates_parser.h" |
| | #include "rs_actioninterface.h" |
| | #include "rs_commandevent.h" |
| | #include "rs_debug.h" |
| | #include "rs_dialogfactory.h" |
| | #include "rs_dialogfactoryinterface.h" |
| | #include "rs_graphicview.h" |
| |
|
| | namespace { |
| | bool isActive(const std::shared_ptr<RS_ActionInterface>& action) { |
| | return action != nullptr && !action->isFinished(); |
| | } |
| |
|
| | bool isInactive(const std::shared_ptr<RS_ActionInterface>& action) { |
| | return ! isActive(action); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | RS_EventHandler::RS_EventHandler(RS_GraphicView *parent):QObject(parent), m_coordinatesParser{std::make_unique<LC_CoordinatesParser>(parent)}, |
| | m_graphicView{parent}{ |
| | } |
| | |
| | |
| | |
| | RS_EventHandler::~RS_EventHandler() { |
| | RS_DEBUG->print("RS_EventHandler::~RS_EventHandler"); |
| | m_defaultAction.reset(); |
| |
|
| | RS_DEBUG->print("RS_EventHandler::~RS_EventHandler: Deleting all actions.."); |
| | m_currentActions.clear(); |
| | RS_DEBUG->print("RS_EventHandler::~RS_EventHandler: Deleting all actions..: OK"); |
| | RS_DEBUG->print("RS_EventHandler::~RS_EventHandler: OK"); |
| | } |
| |
|
| | void RS_EventHandler::uncheckQAction(){ |
| | if (m_QAction != nullptr) { |
| | m_QAction->setChecked(false); |
| | m_QAction = nullptr; |
| | } |
| | if (hasAction()){ |
| | auto lastAction = m_currentActions.last(); |
| | |
| | } |
| | else { |
| | |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void RS_EventHandler::back() { |
| | QMouseEvent e(QEvent::MouseButtonRelease, QPoint(0,0), QPoint{0, 0}, |
| | Qt::RightButton, Qt::RightButton, Qt::NoModifier); |
| | mouseReleaseEvent(&e); |
| | uncheckQAction(); |
| | } |
| |
|
| | |
| | |
| | |
| | void RS_EventHandler::enter() { |
| | QKeyEvent e(QEvent::KeyPress, Qt::Key_Enter, {}); |
| | keyPressEvent(&e); |
| | } |
| |
|
| | |
| | |
| | |
| | void RS_EventHandler::mousePressEvent(QMouseEvent* e) { |
| | if(hasAction()){ |
| | m_currentActions.last()->mousePressEvent(e); |
| | e->accept(); |
| | } else { |
| | if (m_defaultAction) { |
| | m_defaultAction->mousePressEvent(e); |
| | e->accept(); |
| | } else { |
| | RS_DEBUG->print("currently no action defined"); |
| | e->ignore(); |
| | } |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void RS_EventHandler::mouseReleaseEvent(QMouseEvent* e) { |
| | if(hasAction()){ |
| | |
| | |
| | std::shared_ptr<RS_ActionInterface> &lastAction = m_currentActions.last(); |
| | |
| |
|
| | lastAction->mouseReleaseEvent(e); |
| |
|
| | |
| | checkLastActionCompletedAndUncheckQAction(lastAction); |
| | |
| | cleanUp(); |
| | e->accept(); |
| | } else { |
| | if (m_defaultAction) { |
| | m_defaultAction->mouseReleaseEvent(e); |
| | } else { |
| | e->ignore(); |
| | } |
| | } |
| | } |
| |
|
| | void RS_EventHandler::notifyLastActionFinished() { |
| | std::shared_ptr<RS_ActionInterface> &lastAction = m_currentActions.last(); |
| | int lastActionStatus = lastAction->getStatus(); |
| | if (lastActionStatus < 0){ |
| | uncheckQAction(); |
| | } |
| | cleanUp(); |
| | } |
| |
|
| | void RS_EventHandler::checkLastActionCompletedAndUncheckQAction(const std::shared_ptr<RS_ActionInterface> &lastAction) { |
| | int lastActionStatus = lastAction->getStatus(); |
| | if (lastActionStatus < 0){ |
| | uncheckQAction(); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void RS_EventHandler::mouseMoveEvent(QMouseEvent* e){ |
| | if(hasAction()) { |
| | std::shared_ptr<RS_ActionInterface> &lastAction = m_currentActions.last(); |
| | lastAction->mouseMoveEvent(e); |
| | checkLastActionCompletedAndUncheckQAction(lastAction); |
| | cleanUp(); |
| | e->accept(); |
| | } |
| | else if (m_defaultAction) { |
| | m_defaultAction->mouseMoveEvent(e); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void RS_EventHandler::mouseLeaveEvent() { |
| | if(hasAction()){ |
| | m_currentActions.last()->suspend(); |
| | } else { |
| | if (m_defaultAction) { |
| | m_defaultAction->suspend(); |
| | } |
| | |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void RS_EventHandler::mouseEnterEvent() { |
| | if(hasAction()){ |
| | cleanUp(); |
| | debugActions(); |
| | |
| | m_currentActions.last()->resume(); |
| | } else { |
| | if (m_defaultAction) { |
| | m_defaultAction->resume(); |
| | } |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void RS_EventHandler::keyPressEvent(QKeyEvent* e) { |
| | if(hasAction()){ |
| | std::shared_ptr<RS_ActionInterface> &lastAction = m_currentActions.last(); |
| | lastAction->keyPressEvent(e); |
| | checkLastActionCompletedAndUncheckQAction(lastAction); |
| | } else { |
| | if (m_defaultAction) { |
| | m_defaultAction->keyPressEvent(e); |
| | } |
| | else { |
| | e->ignore(); |
| | } |
| |
|
| | |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void RS_EventHandler::keyReleaseEvent(QKeyEvent* e) { |
| | if(hasAction()){ |
| | m_currentActions.last()->keyReleaseEvent(e); |
| | } else { |
| | if (m_defaultAction) { |
| | m_defaultAction->keyReleaseEvent(e); |
| | } |
| | else { |
| | e->ignore(); |
| | } |
| | |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void RS_EventHandler::commandEvent(RS_CommandEvent* e) { |
| | RS_DEBUG->print("RS_EventHandler::commandEvent"); |
| |
|
| | if (m_coordinateInputEnabled) { |
| | if (!e->isAccepted()) { |
| | if(hasAction()) { |
| | bool commandContainsCoordinate = false; |
| | QString command = e->getCommand(); |
| | auto coordinateEvent = m_coordinatesParser->parseCoordinate(command, commandContainsCoordinate); |
| | if (commandContainsCoordinate) { |
| | if (coordinateEvent.isValid()) { |
| | m_currentActions.last()->coordinateEvent(&coordinateEvent); |
| | } |
| | else { |
| | RS_DIALOGFACTORY->commandMessage("Expression Syntax Error"); |
| | } |
| | e->accept(); |
| | } |
| | else { |
| | |
| | std::shared_ptr<RS_ActionInterface> &lastAction = m_currentActions.last(); |
| | lastAction->commandEvent(e); |
| | if (e->isAccepted()) { |
| | checkLastActionCompletedAndUncheckQAction(lastAction); |
| | cleanUp(); |
| | } |
| | } |
| | }else{ |
| | |
| | if (m_defaultAction) { |
| | m_defaultAction->commandEvent(e); |
| | } |
| | } |
| | |
| | |
| | } |
| | } |
| |
|
| | RS_DEBUG->print("RS_EventHandler::commandEvent: OK"); |
| | } |
| | |
| | |
| | |
| | void RS_EventHandler::enableCoordinateInput() { |
| | m_coordinateInputEnabled = true; |
| | } |
| |
|
| | |
| | |
| | |
| | void RS_EventHandler::disableCoordinateInput() { |
| | m_coordinateInputEnabled = false; |
| | } |
| |
|
| | |
| | |
| | |
| | RS_ActionInterface* RS_EventHandler::getCurrentAction(){ |
| | if(hasAction()){ |
| | return m_currentActions.last().get(); |
| | } else { |
| | return m_defaultAction.get(); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | RS_ActionInterface* RS_EventHandler::getDefaultAction() const{ |
| | return m_defaultAction.get(); |
| | } |
| |
|
| | |
| | |
| | |
| | void RS_EventHandler::setDefaultAction(RS_ActionInterface* action) { |
| | if (m_defaultAction) { |
| | m_defaultAction->finish(); |
| | } |
| |
|
| | m_defaultAction.reset(action); |
| | } |
| |
|
| | |
| | |
| | |
| | bool RS_EventHandler::setCurrentAction(std::shared_ptr<RS_ActionInterface> action) { |
| | RS_DEBUG->print("RS_EventHandler::setCurrentAction"); |
| | if (action==nullptr) { |
| | return false; |
| | } |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | LC_LOG<<"RS_EventHandler::setCurrentAction " << action->getName(); |
| | |
| | auto& predecessor = hasAction() ? m_currentActions.last() : m_defaultAction; |
| | |
| | if (predecessor != nullptr) { |
| | predecessor->suspend(); |
| | predecessor->hideOptions(); |
| | } |
| |
|
| |
|
| | |
| | m_currentActions.push_back(action); |
| | |
| | |
| | |
| |
|
| | |
| | RS_DEBUG->print("RS_EventHandler::setCurrentAction: init current action"); |
| | action->init(0); |
| | |
| | bool passedActionIsNotFinished = false; |
| | if (!action->isFinished()) { |
| | RS_DEBUG->print("RS_EventHandler::setCurrentAction: show options"); |
| | action->showOptions(); |
| | RS_DEBUG->print("RS_EventHandler::setCurrentAction: set predecessor"); |
| | action->setPredecessor(predecessor); |
| | passedActionIsNotFinished = true; |
| | } |
| |
|
| | RS_DEBUG->print("RS_EventHandler::setCurrentAction: cleaning up.."); |
| | cleanUp(); |
| |
|
| | RS_DEBUG->print("RS_EventHandler::setCurrentAction: debugging actions"); |
| | debugActions(); |
| | RS_DEBUG->print("RS_GraphicView::setCurrentAction: OK"); |
| | |
| | |
| | if (m_QAction){ |
| | bool hasActionToCheck = hasAction(); |
| | m_QAction->setChecked(hasActionToCheck); |
| | if (!hasActionToCheck) { |
| | |
| | } |
| | } |
| | return passedActionIsNotFinished; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | void RS_EventHandler::killSelectActions() { |
| | for (auto it=m_currentActions.begin();it != m_currentActions.end();){ |
| | RS2::ActionType rtti = (*it)->rtti(); |
| | if (rtti == RS2::ActionSelectSingle || |
| | rtti == RS2::ActionSelectContour || |
| | rtti == RS2::ActionSelectWindow || |
| | rtti == RS2::ActionSelectIntersected || |
| | rtti == RS2::ActionSelectLayer) { |
| | if (isActive(*it)) { |
| | (*it)->finish(); |
| | } |
| | it= m_currentActions.erase(it); |
| | }else{ |
| | it++; |
| | } |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void RS_EventHandler::killAllActions() |
| | { |
| | RS_DEBUG->print(__FILE__ ": %s: line %d: begin\n", __func__, __LINE__); |
| |
|
| | if (m_QAction) { |
| | m_QAction->setChecked(false); |
| | m_QAction = nullptr; |
| | |
| | } |
| |
|
| | for(auto& p: m_currentActions){ |
| | if (isActive(p)){ |
| | p->finish(); |
| | } |
| | } |
| | m_currentActions.clear(); |
| |
|
| | if (!m_defaultAction->isFinished()) { |
| | m_defaultAction->finish(); |
| | } |
| |
|
| | RS_DEBUG->print(__FILE__ ": %s: line %d: begin\n", __func__, __LINE__); |
| | m_defaultAction->init(0); |
| | } |
| |
|
| | |
| | |
| | |
| | bool RS_EventHandler::isValid(RS_ActionInterface* action) const{ |
| | return action != nullptr && std::any_of(m_currentActions.cbegin(), m_currentActions.cend(), |
| | [action](const std::shared_ptr<RS_ActionInterface>& entry){ |
| | return entry.get() == action;}); |
| | } |
| |
|
| | |
| | |
| | |
| | bool RS_EventHandler::hasAction(){ |
| | auto it = std::remove_if(m_currentActions.begin(), m_currentActions.end(), isInactive); |
| | m_currentActions.erase(it, m_currentActions.end()); |
| | return !m_currentActions.empty(); |
| | } |
| |
|
| | |
| | |
| | |
| | void RS_EventHandler::cleanUp() { |
| | RS_DEBUG->print("RS_EventHandler::cleanUp"); |
| |
|
| | if (hasAction()) { |
| | auto lastAction = m_currentActions.last(); |
| | lastAction->resume(); |
| | lastAction->showOptions(); |
| | } else { |
| | if (m_defaultAction) { |
| | m_defaultAction->resume(); |
| | m_defaultAction->showOptions(); |
| | } |
| | } |
| | RS_DEBUG->print("RS_EventHandler::cleanUp: OK"); |
| | } |
| |
|
| | |
| | |
| | |
| | void RS_EventHandler::setSnapMode(RS_SnapMode sm) { |
| | for(auto& a: m_currentActions){ |
| | if(isActive(a)) { |
| | a->setSnapMode(sm); |
| | } |
| | } |
| |
|
| | if (m_defaultAction) { |
| | m_defaultAction->setSnapMode(sm); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void RS_EventHandler::setSnapRestriction(RS2::SnapRestriction sr) { |
| | for(auto& a: m_currentActions){ |
| | if(isActive(a)) { |
| | a->setSnapRestriction(sr); |
| | } |
| | } |
| |
|
| | if (m_defaultAction) { |
| | m_defaultAction->setSnapRestriction(sr); |
| | } |
| | } |
| |
|
| | void RS_EventHandler::debugActions() const{ |
| | |
| | RS_DEBUG->print("---"); |
| | for(int i=0;i<m_currentActions.size();++i){ |
| | if (i == m_currentActions.size() - 1 ) { |
| | RS_DEBUG->print("Current"); |
| | } |
| | RS_DEBUG->print("Action %03d: %s [%s]", |
| | i, m_currentActions.at(i)->getName().toLatin1().data(), |
| | m_currentActions.at(i)->isFinished() ? "finished" : "active"); |
| | } |
| | } |
| |
|
| | QAction* RS_EventHandler::getQAction(){ |
| | return m_QAction; |
| | } |
| |
|
| | void RS_EventHandler::setQAction(QAction* action, bool killOtherActions) { |
| | |
| | debugActions(); |
| | if (killOtherActions) { |
| | if (m_QAction != nullptr && action != nullptr && m_QAction != action) { |
| | auto property = action->property("RS2:actionType"); |
| | if (property.isValid()) { |
| | int rtti = property.toInt(); |
| | if (rtti != RS2::ActionZoomPan && rtti != RS2::ActionSetRelativeZero) { |
| | killAllActions(); |
| | } |
| | } |
| | else { |
| | killAllActions(); |
| | } |
| | } |
| | } |
| | m_QAction = action; |
| | } |
| |
|
| | bool RS_EventHandler::inSelectionMode() { |
| | switch (getCurrentAction()->rtti()) { |
| | case RS2::ActionDefault: |
| | case RS2::ActionSelectSingle: |
| | case RS2::ActionSelectWindow: |
| | case RS2::ActionDeselectWindow: |
| | case RS2::ActionSelectContour: |
| | case RS2::ActionSelectIntersected: |
| | case RS2::ActionDeselectIntersected: |
| | case RS2::ActionSelectLayer: |
| | return true; |
| | default: |
| | return false; |
| | } |
| | } |
| |
|