| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| |
|
| | #include <QMouseEvent> |
| |
|
| | #include "lc_coordinates_parser.h" |
| | #include "lc_eventhandler.h" |
| | #include "rs_actioninterface.h" |
| | #include "rs_commandevent.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(); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | LC_EventHandler::LC_EventHandler(RS_GraphicView *parent):QObject(parent), m_coordinatesParser{std::make_unique<LC_CoordinatesParser>(parent)}, |
| | m_graphicView{parent}{ |
| | } |
| | |
| | |
| | |
| | LC_EventHandler::~LC_EventHandler() { |
| | m_defaultAction.reset(); |
| | m_currentAction.reset(); |
| | } |
| |
|
| | |
| | |
| | |
| | void LC_EventHandler::back() { |
| | QMouseEvent e(QEvent::MouseButtonRelease, QPoint(0,0), QPoint{0, 0}, |
| | Qt::RightButton, Qt::RightButton, Qt::NoModifier); |
| | mouseReleaseEvent(&e); |
| | uncheckQAction(); |
| | } |
| |
|
| | |
| | |
| | |
| | void LC_EventHandler::enter() { |
| | QKeyEvent e(QEvent::KeyPress, Qt::Key_Enter, {}); |
| | keyPressEvent(&e); |
| | } |
| |
|
| | void LC_EventHandler::mousePressEvent(QMouseEvent* e) { |
| | if (hasAction()) { |
| | m_currentAction->mousePressEvent(e); |
| | e->accept(); |
| | } |
| | else { |
| | if (m_defaultAction) { |
| | m_defaultAction->mousePressEvent(e); |
| | e->accept(); |
| | } |
| | else { |
| | e->ignore(); |
| | } |
| | } |
| | } |
| |
|
| | void LC_EventHandler::mouseReleaseEvent(QMouseEvent* e) { |
| | if (hasAction()) { |
| | m_currentAction->mouseReleaseEvent(e); |
| | |
| | checkLastActionFinishedAndUncheckQAction(); |
| | e->accept(); |
| | } |
| | else { |
| | if (m_defaultAction) { |
| | m_defaultAction->mouseReleaseEvent(e); |
| | } |
| | else { |
| | e->ignore(); |
| | } |
| | } |
| | } |
| |
|
| | void LC_EventHandler::mouseMoveEvent(QMouseEvent* e){ |
| | if(hasAction()) { |
| | m_currentAction->mouseMoveEvent(e); |
| | checkLastActionFinishedAndUncheckQAction(); |
| | e->accept(); |
| | } |
| | else if (m_defaultAction) { |
| | m_defaultAction->mouseMoveEvent(e); |
| | } |
| | } |
| |
|
| | void LC_EventHandler::mouseLeaveEvent() { |
| | if(hasAction()){ |
| | m_currentAction->suspend(); |
| | } else { |
| | if (m_defaultAction) { |
| | m_defaultAction->suspend(); |
| | } |
| | } |
| | } |
| |
|
| | void LC_EventHandler::mouseEnterEvent() { |
| | if(hasAction()){ |
| | m_currentAction->resume(); |
| | } else { |
| | if (m_defaultAction) { |
| | m_defaultAction->resume(); |
| | } |
| | } |
| | } |
| |
|
| | void LC_EventHandler::keyPressEvent(QKeyEvent* e) { |
| | if(hasAction()){ |
| | m_currentAction->keyPressEvent(e); |
| | checkLastActionFinishedAndUncheckQAction(); |
| | } else { |
| | if (m_defaultAction) { |
| | m_defaultAction->keyPressEvent(e); |
| | } |
| | else { |
| | e->ignore(); |
| | } |
| | } |
| | } |
| |
|
| | void LC_EventHandler::keyReleaseEvent(QKeyEvent* e) { |
| | if(hasAction()){ |
| | m_currentAction->keyReleaseEvent(e); |
| | checkLastActionFinishedAndUncheckQAction(); |
| | } else { |
| | if (m_defaultAction) { |
| | m_defaultAction->keyReleaseEvent(e); |
| | } |
| | else { |
| | e->ignore(); |
| | } |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void LC_EventHandler::commandEvent(RS_CommandEvent* e) { |
| | 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_currentAction->coordinateEvent(&coordinateEvent); |
| | } |
| | else { |
| | RS_DIALOGFACTORY->commandMessage("Expression Syntax Error"); |
| | } |
| | e->accept(); |
| | } |
| | else { |
| | |
| | m_currentAction->commandEvent(e); |
| | if (e->isAccepted()) { |
| | checkLastActionFinishedAndUncheckQAction(); |
| | } |
| | } |
| | } |
| | else { |
| | |
| | if (m_defaultAction) { |
| | m_defaultAction->commandEvent(e); |
| | } |
| | } |
| | |
| | |
| | } |
| | } |
| | } |
| |
|
| |
|
| | bool LC_EventHandler::checkLastActionFinishedAndUncheckQAction() { |
| | int lastActionStatus = m_currentAction->getStatus(); |
| | bool result = false; |
| | if (lastActionStatus < 0 || m_currentAction->isFinished()){ |
| | if (m_QAction != nullptr) { |
| | m_QAction->setChecked(false); |
| | m_QAction = nullptr; |
| | } |
| | auto predecessor = m_currentAction->getPredecessor(); |
| | if (predecessor != nullptr) { |
| | RS2::ActionType actionType = predecessor->rtti(); |
| | m_currentAction = predecessor; |
| | m_graphicView->notifyCurrentActionChanged(actionType); |
| | resumeAction(m_currentAction); |
| | } |
| | else { |
| | switchToDefaultAction(); |
| | } |
| | result = true; |
| | } |
| | return result; |
| | } |
| |
|
| | void LC_EventHandler::switchToDefaultAction() { |
| | m_currentAction.reset(); |
| | if (m_QAction != nullptr){ |
| | m_QAction->setChecked(false); |
| | m_QAction = nullptr; |
| | } |
| | m_graphicView->notifyCurrentActionChanged(RS2::ActionNone); |
| | if (m_defaultAction != nullptr) { |
| | resumeAction(m_defaultAction); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | bool LC_EventHandler::setCurrentAction(std::shared_ptr<RS_ActionInterface> action) { |
| | if (action==nullptr) { |
| | return false; |
| | } |
| | |
| | |
| | if (isValid(action.get())) { |
| | return false; |
| | } |
| |
|
| | bool hasNonDefaultAction = hasAction(); |
| | |
| | auto predecessor = hasNonDefaultAction ? m_currentAction : m_defaultAction; |
| | |
| | if (predecessor != nullptr) { |
| | predecessor->suspend(); |
| | predecessor->hideOptions(); |
| | } |
| | |
| | bool passedActionIsNotFinished = false; |
| |
|
| | uncheckQAction(); |
| |
|
| | RS2::ActionType actionType = action->rtti(); |
| | m_graphicView->notifyCurrentActionChanged(actionType); |
| |
|
| | action->init(RS_ActionInterface::InitialActionStatus); |
| |
|
| | if (action->isFinished()) { |
| | |
| | |
| | if (action->isSupportsPredecessorAction()) { |
| | if (hasNonDefaultAction) { |
| | actionType = m_currentAction->rtti(); |
| | m_graphicView->notifyCurrentActionChanged(actionType); |
| | resumeAction(m_currentAction); |
| | } |
| | else { |
| | switchToDefaultAction(); |
| | } |
| | } |
| | else { |
| | switchToDefaultAction(); |
| | } |
| | } |
| | else{ |
| | if (hasNonDefaultAction && action->isSupportsPredecessorAction()) { |
| | action->setPredecessor(predecessor); |
| | } |
| | |
| | m_currentAction = action; |
| | passedActionIsNotFinished = true; |
| | resumeAction(action); |
| | } |
| | return passedActionIsNotFinished; |
| | } |
| |
|
| | void LC_EventHandler::resumeAction(const std::shared_ptr<RS_ActionInterface>& action) { |
| | action->resume(); |
| | action->showOptions(); |
| | } |
| |
|
| | void LC_EventHandler::notifyLastActionFinished() { |
| | |
| | int lastActionStatus = m_currentAction->getStatus(); |
| | if (lastActionStatus < 0 || m_currentAction->isFinished()){ |
| | uncheckQAction(); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void LC_EventHandler::enableCoordinateInput() { |
| | m_coordinateInputEnabled = true; |
| | } |
| |
|
| | |
| | |
| | |
| | void LC_EventHandler::disableCoordinateInput() { |
| | m_coordinateInputEnabled = false; |
| | } |
| |
|
| | |
| | |
| | |
| | RS_ActionInterface* LC_EventHandler::getCurrentAction(){ |
| | if(hasAction()){ |
| | return m_currentAction.get(); |
| | } else { |
| | return m_defaultAction.get(); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | RS_ActionInterface* LC_EventHandler::getDefaultAction() const{ |
| | return m_defaultAction.get(); |
| | } |
| |
|
| | |
| | |
| | |
| | void LC_EventHandler::setDefaultAction(RS_ActionInterface* action) { |
| | if (m_defaultAction) { |
| | m_defaultAction->finish(); |
| | } |
| | m_defaultAction.reset(action); |
| | } |
| |
|
| | |
| | |
| | |
| | void LC_EventHandler::killAllActions(){ |
| | bool mayTerminate = true; |
| | if (m_currentAction != nullptr) { |
| | if (!m_currentAction->isFinished()) { |
| | mayTerminate = m_currentAction->mayBeTerminatedExternally(); |
| | } |
| | } |
| | if (mayTerminate) { |
| | if (isActive(m_currentAction)){ |
| | m_currentAction->finish(); |
| | m_currentAction.reset(); |
| | } |
| |
|
| | if (m_QAction) { |
| | m_QAction->setChecked(false); |
| | m_QAction = nullptr; |
| | m_graphicView->notifyCurrentActionChanged(RS2::ActionNone); |
| | } |
| |
|
| | if (!m_defaultAction->isFinished()) { |
| | m_defaultAction->finish(); |
| | } |
| | m_defaultAction->init(0); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | bool LC_EventHandler::isValid(RS_ActionInterface* action) const{ |
| | return m_currentAction != nullptr && m_currentAction.get() == action; |
| | } |
| |
|
| | |
| | |
| | |
| | bool LC_EventHandler::hasAction(){ |
| | return m_currentAction != nullptr; |
| | } |
| |
|
| | |
| | |
| | |
| | void LC_EventHandler::setSnapMode(RS_SnapMode sm) { |
| | if (isActive(m_currentAction)) { |
| | m_currentAction->setSnapMode(sm); |
| | } |
| |
|
| | if (m_defaultAction) { |
| | m_defaultAction->setSnapMode(sm); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void LC_EventHandler::setSnapRestriction(RS2::SnapRestriction sr) { |
| | if (isActive(m_currentAction)) { |
| | m_currentAction->setSnapRestriction(sr); |
| | } |
| |
|
| | if (m_defaultAction) { |
| | m_defaultAction->setSnapRestriction(sr); |
| | } |
| | } |
| |
|
| | QAction* LC_EventHandler::getQAction(){ |
| | return m_QAction; |
| | } |
| |
|
| | void LC_EventHandler::setQAction(QAction* action) { |
| | if (action->isCheckable()) { |
| | if (!action->isChecked()) { |
| | action->setChecked(true); |
| | } |
| | } |
| | m_QAction = action; |
| | } |
| |
|
| | void LC_EventHandler::uncheckQAction(){ |
| | if (m_QAction != nullptr) { |
| | m_QAction->setChecked(false); |
| | m_QAction = nullptr; |
| | } |
| | } |
| |
|