From 20f9bf431811948cf0eef4e8067a36efade12ecc Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Tue, 8 Jan 2019 14:51:45 +0100 Subject: [PATCH] #3935 Measurements : Add Esc button to abort measurement mode --- .../RicToggleMeasurementModeFeature.cpp | 18 +++++ .../RicToggleMeasurementModeFeature.h | 8 +- .../Measurement/CMakeLists_files.cmake | 3 + .../Measurement/RimMeasurement.cpp | 16 ++++ .../Measurement/RimMeasurement.h | 5 ++ .../Measurement/RiuMeasurementEventFilter.cpp | 78 +++++++++++++++++++ .../Measurement/RiuMeasurementEventFilter.h | 46 +++++++++++ .../UserInterface/RiuMainWindow.cpp | 28 +++---- 8 files changed, 184 insertions(+), 18 deletions(-) create mode 100644 ApplicationCode/ProjectDataModel/Measurement/RiuMeasurementEventFilter.cpp create mode 100644 ApplicationCode/ProjectDataModel/Measurement/RiuMeasurementEventFilter.h diff --git a/ApplicationCode/Commands/MeasurementCommands/RicToggleMeasurementModeFeature.cpp b/ApplicationCode/Commands/MeasurementCommands/RicToggleMeasurementModeFeature.cpp index 240b12a8e6..0d2d0463e5 100644 --- a/ApplicationCode/Commands/MeasurementCommands/RicToggleMeasurementModeFeature.cpp +++ b/ApplicationCode/Commands/MeasurementCommands/RicToggleMeasurementModeFeature.cpp @@ -55,6 +55,7 @@ void RicToggleMeasurementModeFeature::onActionTriggered(bool isChecked) { auto meas = measurement(); meas->setMeasurementMode(!meas->isInMeasurementMode()); + refreshActionLook(); } @@ -64,12 +65,29 @@ void RicToggleMeasurementModeFeature::onActionTriggered(bool isChecked) void RicToggleMeasurementModeFeature::setupActionLook(QAction* actionToSetup) { actionToSetup->setText("Measurement Mode"); + actionToSetup->setIcon(QIcon(":/Ruler16x16.png")); +/* auto* meas = measurement(); if (meas && meas->isInMeasurementMode()) actionToSetup->setIcon(QIcon(":/NoRuler16x16.png")); else actionToSetup->setIcon(QIcon(":/Ruler16x16.png")); +*/ +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool RicToggleMeasurementModeFeature::isCommandChecked() +{ + auto meas = measurement(); + if (meas) + { + return meas->isInMeasurementMode(); + } + + return false; } //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationCode/Commands/MeasurementCommands/RicToggleMeasurementModeFeature.h b/ApplicationCode/Commands/MeasurementCommands/RicToggleMeasurementModeFeature.h index a6320ee932..ec136b3304 100644 --- a/ApplicationCode/Commands/MeasurementCommands/RicToggleMeasurementModeFeature.h +++ b/ApplicationCode/Commands/MeasurementCommands/RicToggleMeasurementModeFeature.h @@ -33,14 +33,14 @@ class RicToggleMeasurementModeFeature : public caf::CmdFeature { CAF_CMD_HEADER_INIT; -public: - void refreshActionLook(); - protected: - // Overrides bool isCommandEnabled() override; void onActionTriggered( bool isChecked ) override; void setupActionLook(QAction* actionToSetup) override; + bool isCommandChecked() override; + +private: + void refreshActionLook(); private: RimMeasurement* measurement() const; diff --git a/ApplicationCode/ProjectDataModel/Measurement/CMakeLists_files.cmake b/ApplicationCode/ProjectDataModel/Measurement/CMakeLists_files.cmake index 604b4ed9e4..5f9c9c78dc 100644 --- a/ApplicationCode/ProjectDataModel/Measurement/CMakeLists_files.cmake +++ b/ApplicationCode/ProjectDataModel/Measurement/CMakeLists_files.cmake @@ -1,10 +1,12 @@ set (SOURCE_GROUP_HEADER_FILES ${CMAKE_CURRENT_LIST_DIR}/RimMeasurement.h +${CMAKE_CURRENT_LIST_DIR}/RiuMeasurementEventFilter.h ) set (SOURCE_GROUP_SOURCE_FILES ${CMAKE_CURRENT_LIST_DIR}/RimMeasurement.cpp +${CMAKE_CURRENT_LIST_DIR}/RiuMeasurementEventFilter.cpp ) list(APPEND CODE_HEADER_FILES @@ -16,6 +18,7 @@ ${SOURCE_GROUP_SOURCE_FILES} ) set (QT_MOC_HEADERS +${CMAKE_CURRENT_LIST_DIR}/RiuMeasurementEventFilter.h ${QT_MOC_HEADERS} ) diff --git a/ApplicationCode/ProjectDataModel/Measurement/RimMeasurement.cpp b/ApplicationCode/ProjectDataModel/Measurement/RimMeasurement.cpp index 5afe0815e6..c439a37c95 100644 --- a/ApplicationCode/ProjectDataModel/Measurement/RimMeasurement.cpp +++ b/ApplicationCode/ProjectDataModel/Measurement/RimMeasurement.cpp @@ -24,9 +24,11 @@ #include "MeasurementCommands/RicMeasurementPickEventHandler.h" +#include "RiuMeasurementEventFilter.h" #include "RiuViewerCommands.h" #include "cvfGeometryTools.h" +#include "RiuMainWindow.h" CAF_PDM_SOURCE_INIT(RimMeasurement, "RimMeasurement"); @@ -52,12 +54,26 @@ void RimMeasurement::setMeasurementMode(bool measurementMode) m_isInMeasurementMode = measurementMode; if (m_isInMeasurementMode) + { RiuViewerCommands::setPickEventHandler(RicMeasurementPickEventHandler::instance()); + + m_eventFilter = new RiuMeasurementEventFilter(this); + m_eventFilter->registerFilter(); + } else { RiuViewerCommands::removePickEventHandlerIfActive(RicMeasurementPickEventHandler::instance()); removeAllPoints(); + + if (m_eventFilter) + { + m_eventFilter->unregisterFilter(); + m_eventFilter->deleteLater(); + m_eventFilter = nullptr; + } } + + RiuMainWindow::instance()->refreshViewActions(); } //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationCode/ProjectDataModel/Measurement/RimMeasurement.h b/ApplicationCode/ProjectDataModel/Measurement/RimMeasurement.h index 31edc0ea21..30568bd678 100644 --- a/ApplicationCode/ProjectDataModel/Measurement/RimMeasurement.h +++ b/ApplicationCode/ProjectDataModel/Measurement/RimMeasurement.h @@ -22,7 +22,10 @@ #include "cvfBase.h" #include "cvfVector3.h" +#include + class Rim3dView; +class RiuMeasurementEventFilter; //================================================================================================== /// @@ -76,4 +79,6 @@ private: bool m_isInMeasurementMode; std::vector m_pointsInDomainCoords; caf::PdmPointer m_sourceView; + + QPointer m_eventFilter; }; diff --git a/ApplicationCode/ProjectDataModel/Measurement/RiuMeasurementEventFilter.cpp b/ApplicationCode/ProjectDataModel/Measurement/RiuMeasurementEventFilter.cpp new file mode 100644 index 0000000000..d15535e5c9 --- /dev/null +++ b/ApplicationCode/ProjectDataModel/Measurement/RiuMeasurementEventFilter.cpp @@ -0,0 +1,78 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Copyright (C) 2019- Equinor ASA +// +// ResInsight 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 3 of the License, or +// (at your option) any later version. +// +// ResInsight 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 at +// for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +#include "RiuMeasurementEventFilter.h" + +#include "RiaApplication.h" + +#include "RimMeasurement.h" + +#include "RiuMainWindow.h" +#include + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +RiuMeasurementEventFilter::RiuMeasurementEventFilter(RimMeasurement* parent) + : QObject(nullptr) + , m_parent(parent) +{ +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuMeasurementEventFilter::registerFilter() +{ + RiaApplication::instance()->installEventFilter(this); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuMeasurementEventFilter::unregisterFilter() +{ + RiaApplication::instance()->removeEventFilter(this); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool RiuMeasurementEventFilter::eventFilter(QObject* obj, QEvent* event) +{ + if (event->type() == QEvent::KeyPress) + { + QKeyEvent* keyEvent = static_cast(event); + + if (keyEvent->key() == Qt::Key_Escape) + { + keyEvent->setAccepted(true); + + unregisterFilter(); + + if (m_parent) + { + m_parent->setMeasurementMode(false); + } + + return true; + } + } + + return QObject::eventFilter(obj, event); +} diff --git a/ApplicationCode/ProjectDataModel/Measurement/RiuMeasurementEventFilter.h b/ApplicationCode/ProjectDataModel/Measurement/RiuMeasurementEventFilter.h new file mode 100644 index 0000000000..65d09baab2 --- /dev/null +++ b/ApplicationCode/ProjectDataModel/Measurement/RiuMeasurementEventFilter.h @@ -0,0 +1,46 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Copyright (C) 2019- Equinor ASA +// +// ResInsight 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 3 of the License, or +// (at your option) any later version. +// +// ResInsight 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 at +// for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +#pragma once + + +#include "cafPdmPointer.h" + +#include + +class QEvent; +class RimMeasurement; + +//-------------------------------------------------------------------------------------------------- +// +//-------------------------------------------------------------------------------------------------- +class RiuMeasurementEventFilter : public QObject +{ + Q_OBJECT +public: + explicit RiuMeasurementEventFilter(RimMeasurement* parent); + + void registerFilter(); + void unregisterFilter(); + +protected: + bool eventFilter(QObject *obj, QEvent *event) override; + +private: + caf::PdmPointer m_parent; +}; diff --git a/ApplicationCode/UserInterface/RiuMainWindow.cpp b/ApplicationCode/UserInterface/RiuMainWindow.cpp index a6d5a85b62..f5ea157c61 100644 --- a/ApplicationCode/UserInterface/RiuMainWindow.cpp +++ b/ApplicationCode/UserInterface/RiuMainWindow.cpp @@ -871,21 +871,21 @@ void RiuMainWindow::slotRefreshViewActions() updateScaleValue(); - QStringList commandIds; - commandIds << "RicLinkVisibleViewsFeature" - << "RicTileWindowsFeature" - << "RicTogglePerspectiveViewFeature" - << "RicViewZoomAllFeature"; - - caf::CmdFeatureManager::instance()->refreshEnabledState(commandIds); - - - caf::CmdFeatureManager* cmdFeatureMgr = caf::CmdFeatureManager::instance(); - auto feature = dynamic_cast( - cmdFeatureMgr->getCommandFeature("RicToggleMeasurementModeFeature")); - if (feature) { - feature->refreshActionLook(); + QStringList commandIds; + commandIds << "RicLinkVisibleViewsFeature" + << "RicTileWindowsFeature" + << "RicTogglePerspectiveViewFeature" + << "RicViewZoomAllFeature"; + + caf::CmdFeatureManager::instance()->refreshEnabledState(commandIds); + } + + { + QStringList commandIds; + commandIds << "RicToggleMeasurementModeFeature"; + + caf::CmdFeatureManager::instance()->refreshCheckedState(commandIds); } }