File size: 12,383 Bytes
a5ffdcd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 | /*
* ********************************************************************************
* This file is part of the LibreCAD project, a 2D CAD program
*
* Copyright (C) 2025 LibreCAD.org
* Copyright (C) 2025 sand1024
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* ********************************************************************************
*/
#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();
}
}
/**
* Constructor.
*/
LC_EventHandler::LC_EventHandler(RS_GraphicView *parent):QObject(parent), m_coordinatesParser{std::make_unique<LC_CoordinatesParser>(parent)},
m_graphicView{parent}{
}
/**
* Destructor.
*/
LC_EventHandler::~LC_EventHandler() {
m_defaultAction.reset();
m_currentAction.reset();
}
/**
* Go back in current action.
*/
void LC_EventHandler::back() {
QMouseEvent e(QEvent::MouseButtonRelease, QPoint(0,0), QPoint{0, 0},
Qt::RightButton, Qt::RightButton, Qt::NoModifier);
mouseReleaseEvent(&e);
uncheckQAction();
}
/**
* Go enter pressed event for current action.
*/
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);
// action may be completed by click. Check this and if it is so, uncheck the action
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();
}
}
}
/**
* Handles command line events.
*/
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"); // fixme - sand - remove static
}
e->accept();
}
else {
// send command event directly to current action:
m_currentAction->commandEvent(e);
if (e->isAccepted()) {
checkLastActionFinishedAndUncheckQAction();
}
}
}
else {
//send the command to default action
if (m_defaultAction) {
m_defaultAction->commandEvent(e);
}
}
// do not accept command here. Actions themselves should be responsible to accept commands
// e->accept();
}
}
}
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);
}
}
/**
* Sets the current action.
*/
bool LC_EventHandler::setCurrentAction(std::shared_ptr<RS_ActionInterface> action) {
if (action==nullptr) {
return false;
}
// Do not initialize action if it's already the last one.
// This is attempt to fix crashes of dialogs (like properties) which are called from actions
if (isValid(action.get())) {
return false;
}
bool hasNonDefaultAction = hasAction();
// Predecessor of the new action or NULL:
auto predecessor = hasNonDefaultAction ? m_currentAction : m_defaultAction;
// Suspend current action:
if (predecessor != nullptr) {
predecessor->suspend();
predecessor->hideOptions();
}
// Initialisation of our new action:
bool passedActionIsNotFinished = false;
uncheckQAction();
RS2::ActionType actionType = action->rtti();
m_graphicView->notifyCurrentActionChanged(actionType);
action->init(RS_ActionInterface::InitialActionStatus);
if (action->isFinished()) {
// For some actions: action->init() may call finish() within init()
// If so, the q_action shouldn't be checked
if (action->isSupportsPredecessorAction()) { // we'll not change current action for one-shoot call
if (hasNonDefaultAction) {
actionType = m_currentAction->rtti();
m_graphicView->notifyCurrentActionChanged(actionType);
resumeAction(m_currentAction);
}
else {
switchToDefaultAction();
}
}
else { // one-shoot finished action, return to default
switchToDefaultAction();
}
}
else{ // this is multi-state action, so switch to it
if (hasNonDefaultAction && action->isSupportsPredecessorAction()) {
action->setPredecessor(predecessor);
}
// Set current action:
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() {
// fixme - sand check that action is not null!!!
int lastActionStatus = m_currentAction->getStatus();
if (lastActionStatus < 0 || m_currentAction->isFinished()){
uncheckQAction();
}
}
/**
* Enables coordinate input in the command line.
*/
void LC_EventHandler::enableCoordinateInput() {
m_coordinateInputEnabled = true;
}
/**
* Enables coordinate input in the command line.
*/
void LC_EventHandler::disableCoordinateInput() {
m_coordinateInputEnabled = false;
}
/**
* @return Current action.
*/
RS_ActionInterface* LC_EventHandler::getCurrentAction(){
if(hasAction()){
return m_currentAction.get();
} else {
return m_defaultAction.get();
}
}
/**
* @return The current default action.
*/
RS_ActionInterface* LC_EventHandler::getDefaultAction() const{
return m_defaultAction.get();
}
/**
* Sets the default action.
*/
void LC_EventHandler::setDefaultAction(RS_ActionInterface* action) {
if (m_defaultAction) {
m_defaultAction->finish();
}
m_defaultAction.reset(action);
}
/**
* Kills all running actions. Called when a window is closed.
*/
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);
}
}
/**
* @return true if the action is within currentActions
*/
bool LC_EventHandler::isValid(RS_ActionInterface* action) const{
return m_currentAction != nullptr && m_currentAction.get() == action;
}
/**
* @return true if there is at least one action in the action stack.
*/
bool LC_EventHandler::hasAction(){
return m_currentAction != nullptr;
}
/**
* Sets the snap mode for all currently active actions.
*/
void LC_EventHandler::setSnapMode(RS_SnapMode sm) {
if (isActive(m_currentAction)) {
m_currentAction->setSnapMode(sm);
}
if (m_defaultAction) {
m_defaultAction->setSnapMode(sm);
}
}
/**
* Sets the snap restriction for all currently active actions.
*/
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;
}
}
|