From 3cb42fa28bb55224f71b869d20c7b369b645b422 Mon Sep 17 00:00:00 2001 From: sbh Date: Fri, 6 Feb 2015 19:40:59 +0300 Subject: [PATCH] Facilate action processing with ModuleBase_ActionInfo --- src/ModuleBase/CMakeLists.txt | 3 + src/ModuleBase/ModuleBase_ActionInfo.cpp | 102 +++++++++++++++++++++++ src/ModuleBase/ModuleBase_ActionInfo.h | 66 +++++++++++++++ src/NewGeom/NewGeom_Module.cpp | 11 +++ src/NewGeom/NewGeom_Module.h | 6 ++ src/XGUI/XGUI_ActionsMgr.cpp | 19 +++-- src/XGUI/XGUI_ActionsMgr.h | 5 ++ src/XGUI/XGUI_HistoryMenu.cpp | 9 +- src/XGUI/XGUI_HistoryMenu.h | 4 +- src/XGUI/XGUI_SalomeConnector.h | 5 ++ src/XGUI/XGUI_Workshop.cpp | 52 +++++------- src/XGUI/XGUI_Workshop.h | 6 +- 12 files changed, 243 insertions(+), 45 deletions(-) create mode 100644 src/ModuleBase/ModuleBase_ActionInfo.cpp create mode 100644 src/ModuleBase/ModuleBase_ActionInfo.h diff --git a/src/ModuleBase/CMakeLists.txt b/src/ModuleBase/CMakeLists.txt index 6d7fc7cf2..096e6eaaa 100644 --- a/src/ModuleBase/CMakeLists.txt +++ b/src/ModuleBase/CMakeLists.txt @@ -42,6 +42,7 @@ SET(PROJECT_HEADERS ModuleBase_WidgetLabel.h ModuleBase_IPrefMgr.h ModuleBase_Preferences.h + ModuleBase_ActionInfo.h ) SET(PROJECT_SOURCES @@ -74,10 +75,12 @@ SET(PROJECT_SOURCES ModuleBase_ResultPrs.cpp ModuleBase_WidgetLabel.cpp ModuleBase_Preferences.cpp + ModuleBase_ActionInfo.cpp ) SET(PROJECT_LIBRARIES Config + Events ModelAPI GeomAPI GeomAlgoAPI diff --git a/src/ModuleBase/ModuleBase_ActionInfo.cpp b/src/ModuleBase/ModuleBase_ActionInfo.cpp new file mode 100644 index 000000000..9089ecaf0 --- /dev/null +++ b/src/ModuleBase/ModuleBase_ActionInfo.cpp @@ -0,0 +1,102 @@ +/* + * ModuleBase_ActionInfo.cpp + * + * Created on: Feb 4, 2015 + * Author: sbh + */ + +#include + +ModuleBase_ActionInfo::ModuleBase_ActionInfo() +{ + initDefault(); +} + +ModuleBase_ActionInfo::ModuleBase_ActionInfo(const QString &theText) +{ + initDefault(); +} + +ModuleBase_ActionInfo::ModuleBase_ActionInfo(const QIcon & theIcon, const QString &theText) +{ + initDefault(); + icon = theIcon; + text = theText; +} + +ModuleBase_ActionInfo::~ModuleBase_ActionInfo() +{ +} + +void ModuleBase_ActionInfo::initFrom(QAction* theAction) +{ + // By convenience, QAction for a feature keeps feature's id as data (QVariant); + if (theAction->data().isValid()) { + id = theAction->data().toString(); + } + checkable = theAction->isCheckable(); + checked = theAction->isChecked(); + enabled = theAction->isEnabled(); + visible = theAction->isVisible(); + icon = theAction->icon(); + text = theAction->text(); + iconText = theAction->iconText(); + toolTip = theAction->toolTip(); + // statusTip = theAction->statusTip(); + // whatsThis = theAction->whatsThis(); + shortcut = theAction->shortcut(); + font = theAction->font(); +} + +void ModuleBase_ActionInfo::initFrom(std::shared_ptr theMessage) +{ + id = QString::fromStdString(theMessage->id()); + iconFile = QString::fromStdString(theMessage->icon()); + if (!iconFile.isEmpty()) { + icon = QIcon(iconFile); + } + text = QString::fromStdString(theMessage->text()); + toolTip = QString::fromStdString(theMessage->tooltip()); + QString aShortcutStr = QString::fromStdString(theMessage->keysequence()); + if (!aShortcutStr.isEmpty()) { + shortcut = QKeySequence(aShortcutStr); + } + // If feature requires PropertyPannel for input, it should be checkable + checkable = theMessage->isUseInput(); +} + +QAction* ModuleBase_ActionInfo::makeAction(QObject* theParent) +{ + QAction* aResult = new QAction(icon, text, theParent); + aResult->setCheckable(checkable); + aResult->setChecked(checked); + aResult->setEnabled(enabled); + aResult->setVisible(visible); + aResult->setIconText(iconText); + aResult->setToolTip(toolTip); + // aResult->setStatusTip(statusTip); + // aResult->setWhatsThis(whatsThis); + aResult->setShortcut(shortcut); + aResult->setFont(font); + // By convenience, QAction for a feature keeps feature's id as data (QVariant); + aResult->setData(id); + return aResult; +} + +void ModuleBase_ActionInfo::initDefault() +{ + id = QString(); + checkable = false; + checked = false; + enabled = true; + visible = true; + icon = QIcon(); + text = QString(); + iconText = QString(); + iconFile = QString(); + toolTip = QString(); + // statusTip = QString(); + // whatsThis = QString(); + shortcut = QKeySequence(); + font = QFont(); +} diff --git a/src/ModuleBase/ModuleBase_ActionInfo.h b/src/ModuleBase/ModuleBase_ActionInfo.h new file mode 100644 index 000000000..766a16b8c --- /dev/null +++ b/src/ModuleBase/ModuleBase_ActionInfo.h @@ -0,0 +1,66 @@ +/* + * ActionInfo.h + * + * Created on: Feb 4, 2015 + * Author: sbh + */ + +#ifndef MODULEBASE_ACTIONINFO_H_ +#define MODULEBASE_ACTIONINFO_H_ + +#include +#include + +#include +#include +#include +#include + +#include + +/*! + * Structure to pass info about QActions, AppElements_Commands, etc. + */ +struct MODULEBASE_EXPORT ModuleBase_ActionInfo +{ + QString id; + + bool checkable; //!< action's checkable state + bool checked; //!< action's checked state + bool enabled; //!< action's enabled state + bool visible; //!< action's visibility state + QIcon icon; //!< action's icon + QString text; //!< action's text + QString iconText; //!< action's descriptive icon text + QString iconFile; //!< path to icon's file. Can not be initialized from QAction + QString toolTip; //!< action's tooltip + // QString statusTip; + // QString whatsThis; + QKeySequence shortcut; //!< action's primary shortcut key + QFont font; //!< action's text font + + public: + //! Default constructor, \sa initDefault + ModuleBase_ActionInfo(); + //! Initializes structure with default values, except text + ModuleBase_ActionInfo(const QString &text); + //! Initializes structure with default values, except icon and text + ModuleBase_ActionInfo(const QIcon &icon, const QString &text); + virtual ~ModuleBase_ActionInfo(); + + //! Fills itself with info from given \param theAction + void initFrom(QAction* theAction); + //! Fills itself with info from given \param theFeatureMessage + void initFrom(std::shared_ptr theFeatureMessage); + //! Creates new QAction with given parent and data initialized from this structure + //! \param theParent - parent of created action + QAction* makeAction(QObject* theParent = 0); + + protected: + //! Initializes structure with default values, like QAction() + void initDefault(); +}; + +typedef ModuleBase_ActionInfo ActionInfo; + +#endif /* XGUI_ACTIONINFO_H_ */ diff --git a/src/NewGeom/NewGeom_Module.cpp b/src/NewGeom/NewGeom_Module.cpp index 9f5c22ceb..50279350e 100644 --- a/src/NewGeom/NewGeom_Module.cpp +++ b/src/NewGeom/NewGeom_Module.cpp @@ -325,6 +325,17 @@ QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& the return aAction; } +QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo) +{ + return addFeature(theWBName, + theInfo.id, + theInfo.text, + theInfo.toolTip, + theInfo.icon, + theInfo.shortcut); +} + + //****************************************************** QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle, const QString& theTip, const QIcon& theIcon, diff --git a/src/NewGeom/NewGeom_Module.h b/src/NewGeom/NewGeom_Module.h index 3482c12e8..36bae93af 100644 --- a/src/NewGeom/NewGeom_Module.h +++ b/src/NewGeom/NewGeom_Module.h @@ -10,6 +10,8 @@ #include #include +#include + #include #include @@ -51,6 +53,10 @@ Q_OBJECT const QKeySequence& theKeys = QKeySequence(), bool isCheckable = false); + virtual QAction* addFeature(const QString& theWBName, + const ActionInfo& theInfo); + + virtual QAction* addDesktopCommand(const QString& theId, const QString& theTitle, const QString& theTip, const QIcon& theIcon, const QKeySequence& theKeys, bool isCheckable, diff --git a/src/XGUI/XGUI_ActionsMgr.cpp b/src/XGUI/XGUI_ActionsMgr.cpp index 851e3fef0..933b73e6e 100644 --- a/src/XGUI/XGUI_ActionsMgr.cpp +++ b/src/XGUI/XGUI_ActionsMgr.cpp @@ -161,18 +161,25 @@ void XGUI_ActionsMgr::updateOnViewSelection() } } +QKeySequence XGUI_ActionsMgr::registerShortcut(const QKeySequence& theKeySequence) +{ + if (myShortcuts.contains(theKeySequence)) { + QString aMessage = tr("Shortcut %1 is already defined. Ignore."); + aMessage = aMessage.arg(theKeySequence.toString()); + Events_Error::send(aMessage.toStdString()); + return QKeySequence(); + } + myShortcuts.append(theKeySequence); + return theKeySequence; +} + QKeySequence XGUI_ActionsMgr::registerShortcut(const QString& theKeySequence) { if (theKeySequence.isEmpty()) { return QKeySequence(); } QKeySequence aResult(theKeySequence); - if (myShortcuts.contains(aResult)) { - QString aMessage = tr("Shortcut %1 is already defined. Ignore.").arg(theKeySequence); - Events_Error::send(aMessage.toStdString()); - return QKeySequence(); - } - myShortcuts.append(aResult); + registerShortcut(aResult); return aResult; } diff --git a/src/XGUI/XGUI_ActionsMgr.h b/src/XGUI/XGUI_ActionsMgr.h index 8007e9794..5c74e2b79 100644 --- a/src/XGUI/XGUI_ActionsMgr.h +++ b/src/XGUI/XGUI_ActionsMgr.h @@ -55,6 +55,11 @@ Q_OBJECT /// Registers shortcut (key sequence) for the command triggering /// \param theKeySequence a key sequence to register + QKeySequence registerShortcut(const QKeySequence& theKeySequence); + + /// This is an overloaded function. + /// Registers shortcut (key sequence) for the command triggering + /// \param theKeySequence - string that contain a key sequence to register QKeySequence registerShortcut(const QString& theKeySequence); //! Redefinition of Events_Listener method diff --git a/src/XGUI/XGUI_HistoryMenu.cpp b/src/XGUI/XGUI_HistoryMenu.cpp index 3e36f42a0..2aa5cbc88 100644 --- a/src/XGUI/XGUI_HistoryMenu.cpp +++ b/src/XGUI/XGUI_HistoryMenu.cpp @@ -39,14 +39,11 @@ XGUI_HistoryMenu::~XGUI_HistoryMenu() { } -void XGUI_HistoryMenu::setHistory(const QList& theActions) +void XGUI_HistoryMenu::setHistory(const QList& theActions) { myHistoryList->clear(); - foreach(QAction* anAct, theActions) { - QListWidgetItem* anItem = new QListWidgetItem(anAct->icon(), - anAct->text(), - myHistoryList); - anAct->deleteLater(); + foreach(ActionInfo anAct, theActions) { + QListWidgetItem* anItem = new QListWidgetItem(anAct.icon, anAct.text, myHistoryList); } } diff --git a/src/XGUI/XGUI_HistoryMenu.h b/src/XGUI/XGUI_HistoryMenu.h index dfcb256fb..9dd2951ac 100644 --- a/src/XGUI/XGUI_HistoryMenu.h +++ b/src/XGUI/XGUI_HistoryMenu.h @@ -11,6 +11,8 @@ #include #include +#include + class QListWidget; class QToolButton; class QListWidgetItem; @@ -26,7 +28,7 @@ class XGUI_EXPORT XGUI_HistoryMenu : public QMenu void actionsSelected(int); public slots: - void setHistory(const QList&); + void setHistory(const QList&); protected slots: void setStackSelectedTo(QListWidgetItem *); diff --git a/src/XGUI/XGUI_SalomeConnector.h b/src/XGUI/XGUI_SalomeConnector.h index 10d663b4f..c06bd3e52 100644 --- a/src/XGUI/XGUI_SalomeConnector.h +++ b/src/XGUI/XGUI_SalomeConnector.h @@ -8,6 +8,8 @@ #include #include +#include + class QMainWindow; class ModuleBase_IViewer; @@ -32,6 +34,9 @@ class XGUI_EXPORT XGUI_SalomeConnector const QString& theTitle, const QString& theTip, const QIcon& theIcon, const QKeySequence& theKeys, bool isCheckable) = 0; + virtual QAction* addFeature(const QString& theWBName, + const ActionInfo& theInfo) = 0; + //! Creates a command in Edit menu of SALOME desktop //! \param theId - an id of the feature //! \param theTitle - a menu item string diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index 0dbb76876..de72077be 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -267,8 +267,8 @@ void XGUI_Workshop::initMenu() QToolButton* aToolBtn = qobject_cast(aGroup->widget(aUndoId)); XGUI_HistoryMenu* aUndoMenu = new XGUI_HistoryMenu(aToolBtn); - connect(this, SIGNAL(updateUndoHistory(const QList&)), - aUndoMenu, SLOT(setHistory(const QList&))); + connect(this, SIGNAL(updateUndoHistory(const QList&)), + aUndoMenu, SLOT(setHistory(const QList&))); connect(aUndoMenu, SIGNAL(actionsSelected(int)), this, SLOT(onUndo(int))); @@ -279,7 +279,7 @@ void XGUI_Workshop::initMenu() aToolBtn = qobject_cast(aGroup->widget(aRedoId)); XGUI_HistoryMenu* aRedoMenu = new XGUI_HistoryMenu(aToolBtn); - connect(this, SIGNAL(updateUndoHistory(const QList&)), + connect(this, SIGNAL(updateUndoHistory(const QList&)), aRedoMenu, SLOT(setHistory(const QList&))); connect(aRedoMenu, SIGNAL(actionsSelected(int)), this, SLOT(onUndo(int))); @@ -692,22 +692,19 @@ void XGUI_Workshop::addFeature(const std::shared_ptr& the #endif return; } + ActionInfo aFeatureInfo; + aFeatureInfo.initFrom(theMessage); // Remember features icons - myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon()); + myIcons[QString::fromStdString(theMessage->id())] = aFeatureInfo.iconFile; QString aWchName = QString::fromStdString(theMessage->workbenchId()); - QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures()); - bool isUsePropPanel = theMessage->isUseInput(); - QString aFeatureId = QString::fromStdString(theMessage->id()); + QStringList aNestedFeatures = + QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts); + QString aDocKind = QString::fromStdString(theMessage->documentKind()); if (isSalomeMode()) { - QAction* aAction = salomeConnector()->addFeature(aWchName, aFeatureId, - QString::fromStdString(theMessage->text()), - QString::fromStdString(theMessage->tooltip()), - QIcon(QString::fromStdString(theMessage->icon())), - QKeySequence(), - isUsePropPanel); - salomeConnector()->setNestedActions(aFeatureId, aNestedFeatures.split(" ", QString::SkipEmptyParts)); - salomeConnector()->setDocumentKind(aFeatureId, QString::fromStdString(theMessage->documentKind())); + QAction* aAction = salomeConnector()->addFeature(aWchName, aFeatureInfo); + salomeConnector()->setNestedActions(aFeatureInfo.id, aNestedFeatures); + salomeConnector()->setDocumentKind(aFeatureInfo.id, aDocKind); myActionsMgr->addCommand(aAction); myModule->actionCreated(aAction); @@ -724,19 +721,14 @@ void XGUI_Workshop::addFeature(const std::shared_ptr& the if (!aGroup) { aGroup = aPage->addGroup(aGroupName); } - QString aDocKind = QString::fromStdString(theMessage->documentKind()); // Check if hotkey sequence is already defined: - QKeySequence aHotKey = myActionsMgr->registerShortcut( - QString::fromStdString(theMessage->keysequence())); + QKeySequence aHotKey = myActionsMgr->registerShortcut(aFeatureInfo.shortcut); + if(aHotKey != aFeatureInfo.shortcut) { + aFeatureInfo.shortcut = aHotKey; + } // Create feature... - AppElements_Command* aCommand = aGroup->addFeature(aFeatureId, - QString::fromStdString(theMessage->text()), - QString::fromStdString(theMessage->tooltip()), - QIcon(theMessage->icon().c_str()), - aDocKind, - aHotKey, - isUsePropPanel); - aCommand->setNestedCommands(aNestedFeatures.split(" ", QString::SkipEmptyParts)); + AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo, aDocKind); + aCommand->setNestedCommands(aNestedFeatures); myActionsMgr->addCommand(aCommand); myModule->actionCreated(aCommand); } @@ -1095,25 +1087,25 @@ void XGUI_Workshop::updateHistory() { std::list aUndoList = ModelAPI_Session::get()->undoList(); std::list::iterator it = aUndoList.begin(); - QList aUndoRes; + QList aUndoRes; for ( ; it != aUndoList.end(); it++) { QString anId = QString::fromStdString(*it); QIcon aIcon; if (myIcons.contains(anId)) aIcon = QIcon(myIcons[anId]); - aUndoRes << new QAction(aIcon, anId, NULL); + aUndoRes << ActionInfo(aIcon, anId); } emit updateUndoHistory(aUndoRes); std::list aRedoList = ModelAPI_Session::get()->redoList(); it = aRedoList.begin(); - QList aRedoRes; + QList aRedoRes; for ( ; it != aRedoList.end(); it++) { QString anId = QString::fromStdString(*it); QIcon aIcon; if (myIcons.contains(anId)) aIcon = QIcon(myIcons[anId]); - aRedoRes << new QAction(aIcon, anId, NULL); + aRedoRes << ActionInfo(aIcon, anId); } emit updateRedoHistory(aUndoRes); } diff --git a/src/XGUI/XGUI_Workshop.h b/src/XGUI/XGUI_Workshop.h index 23990f8bc..a79b8e209 100644 --- a/src/XGUI/XGUI_Workshop.h +++ b/src/XGUI/XGUI_Workshop.h @@ -10,6 +10,8 @@ #include #include +#include + #include #include #include @@ -215,8 +217,8 @@ signals: //! the application is started void applicationStarted(); - void updateUndoHistory(const QList&); - void updateRedoHistory(const QList&); + void updateUndoHistory(const QList&); + void updateRedoHistory(const QList&); public slots: /// Update of commands status -- 2.39.2