]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Facilate action processing with ModuleBase_ActionInfo
authorsbh <sergey.belash@opencascade.com>
Fri, 6 Feb 2015 16:40:59 +0000 (19:40 +0300)
committersbh <sergey.belash@opencascade.com>
Fri, 6 Feb 2015 16:40:59 +0000 (19:40 +0300)
12 files changed:
src/ModuleBase/CMakeLists.txt
src/ModuleBase/ModuleBase_ActionInfo.cpp [new file with mode: 0644]
src/ModuleBase/ModuleBase_ActionInfo.h [new file with mode: 0644]
src/NewGeom/NewGeom_Module.cpp
src/NewGeom/NewGeom_Module.h
src/XGUI/XGUI_ActionsMgr.cpp
src/XGUI/XGUI_ActionsMgr.h
src/XGUI/XGUI_HistoryMenu.cpp
src/XGUI/XGUI_HistoryMenu.h
src/XGUI/XGUI_SalomeConnector.h
src/XGUI/XGUI_Workshop.cpp
src/XGUI/XGUI_Workshop.h

index 6d7fc7cf2afa9aa7dd63e79f81c563f5c42459f1..096e6eaaa0caa053d812baa90274c95cbfa65378 100644 (file)
@@ -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 (file)
index 0000000..9089eca
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * ModuleBase_ActionInfo.cpp
+ *
+ *  Created on: Feb 4, 2015
+ *      Author: sbh
+ */
+
+#include <ModuleBase_ActionInfo.h>
+
+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<Config_FeatureMessage> 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 (file)
index 0000000..766a16b
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * ActionInfo.h
+ *
+ *  Created on: Feb 4, 2015
+ *      Author: sbh
+ */
+
+#ifndef MODULEBASE_ACTIONINFO_H_
+#define MODULEBASE_ACTIONINFO_H_
+
+#include <ModuleBase.h>
+#include <Config_FeatureMessage.h>
+
+#include <QAction>
+#include <QIcon>
+#include <QKeySequence>
+#include <QFont>
+
+#include <memory>
+
+/*!
+ * 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<Config_FeatureMessage> 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_ */
index 9f5c22ceb3d5a3936828d8024844e96662265081..50279350e19f198496233c71ed9cde0bcdc72db3 100644 (file)
@@ -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,
index 3482c12e8ad90e3d830d4325727574957cd99bcc..36bae93af0388832b9f80a025dd97bdaed2fb0cf 100644 (file)
@@ -10,6 +10,8 @@
 #include <LightApp_Module.h>
 #include <XGUI_SalomeConnector.h>
 
+#include <ModuleBase_ActionInfo.h>
+
 #include <QStringList>
 #include <QMap>
 
@@ -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,
index 851e3fef0adff9e5feb81c9e2eb35d4f5c9b7f49..933b73e6e1195ec48e16982246bb0dd6efdd558b 100644 (file)
@@ -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;
 }
 
index 8007e979421e63275f22a73277b729eed1b78e68..5c74e2b796c0ce6c43f36438561ffdcec1d74a34 100644 (file)
@@ -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
index 3e36f42a0c76c6aa9af8c0d987eb962dd31bceac..2aa5cbc88a74b53eea5db933dc5fd67c22e381b3 100644 (file)
@@ -39,14 +39,11 @@ XGUI_HistoryMenu::~XGUI_HistoryMenu()
 {
 }
 
-void XGUI_HistoryMenu::setHistory(const QList<QAction*>& theActions)
+void XGUI_HistoryMenu::setHistory(const QList<ActionInfo>& 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);
   }
 }
 
index dfcb256fbf9d55dbc6aee297123090e78e5e10d9..9dd2951acc579de46bb27aca188100592571ea4a 100644 (file)
@@ -11,6 +11,8 @@
 #include <XGUI.h>
 #include <QMenu>
 
+#include <ModuleBase_ActionInfo.h>
+
 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<QAction*>&);
+  void setHistory(const QList<ActionInfo>&);
 
  protected slots:
   void setStackSelectedTo(QListWidgetItem *);
index 10d663b4f039298a03402a2bdbb72b7437f84912..c06bd3e5223e2b0f8293b9147eb78f092d41aab2 100644 (file)
@@ -8,6 +8,8 @@
 #include <QString>
 #include <QStringList>
 
+#include <ModuleBase_ActionInfo.h>
+
 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
index 0dbb7687697715258db406ca3179ae33e987596d..de72077be8306b0609e8f0bef15abd45ef41a69f 100644 (file)
@@ -267,8 +267,8 @@ void XGUI_Workshop::initMenu()
 
   QToolButton* aToolBtn = qobject_cast<QToolButton*>(aGroup->widget(aUndoId));
   XGUI_HistoryMenu* aUndoMenu = new XGUI_HistoryMenu(aToolBtn);
-  connect(this,  SIGNAL(updateUndoHistory(const QList<QAction*>&)),
-          aUndoMenu, SLOT(setHistory(const QList<QAction*>&)));
+  connect(this,  SIGNAL(updateUndoHistory(const QList<ActionInfo>&)),
+          aUndoMenu, SLOT(setHistory(const QList<ActionInfo>&)));
   connect(aUndoMenu, SIGNAL(actionsSelected(int)),
           this,  SLOT(onUndo(int)));
 
@@ -279,7 +279,7 @@ void XGUI_Workshop::initMenu()
 
   aToolBtn = qobject_cast<QToolButton*>(aGroup->widget(aRedoId));
   XGUI_HistoryMenu* aRedoMenu = new XGUI_HistoryMenu(aToolBtn);
-  connect(this,  SIGNAL(updateUndoHistory(const QList<QAction*>&)),
+  connect(this,  SIGNAL(updateUndoHistory(const QList<ActionInfo>&)),
           aRedoMenu, SLOT(setHistory(const QList<QAction*>&)));
   connect(aRedoMenu, SIGNAL(actionsSelected(int)),
           this,  SLOT(onUndo(int)));
@@ -692,22 +692,19 @@ void XGUI_Workshop::addFeature(const std::shared_ptr<Config_FeatureMessage>& 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<Config_FeatureMessage>& 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<std::string> aUndoList = ModelAPI_Session::get()->undoList();
   std::list<std::string>::iterator it = aUndoList.begin();
-  QList<QAction*> aUndoRes;
+  QList<ActionInfo> 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<std::string> aRedoList = ModelAPI_Session::get()->redoList();
   it = aRedoList.begin();
-  QList<QAction*> aRedoRes;
+  QList<ActionInfo> 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);
 }
index 23990f8bc9354130723298b8ac84b95a8becd787..a79b8e20970b8477fd6fafb4161d5f22d3820b7f 100644 (file)
@@ -10,6 +10,8 @@
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Feature.h>
 
+#include <ModuleBase_ActionInfo.h>
+
 #include <QObject>
 #include <QMap>
 #include <QKeySequence>
@@ -215,8 +217,8 @@ signals:
   //! the application is started
   void applicationStarted();
 
-  void updateUndoHistory(const QList<QAction*>&);
-  void updateRedoHistory(const QList<QAction*>&);
+  void updateUndoHistory(const QList<ActionInfo>&);
+  void updateRedoHistory(const QList<ActionInfo>&);
 
  public slots:
    /// Update of commands status