Salome HOME
Merge branch 'master' into Dev_1.1.0
[modules/shaper.git] / src / XGUI / XGUI_Workshop.cpp
index 90ea84c529973b4c67511b62738ac74cbc61c7c8..84d3d6a10354f5d6d8f7117f623b79823ff0cfb5 100644 (file)
@@ -16,6 +16,7 @@
 #include "XGUI_ContextMenuMgr.h"
 #include "XGUI_ModuleConnector.h"
 #include <XGUI_QtEvents.h>
+#include <XGUI_HistoryMenu.h>
 
 #include <AppElements_Workbench.h>
 #include <AppElements_Viewer.h>
@@ -68,6 +69,9 @@
 #include <QLayout>
 #include <QThread>
 #include <QObject>
+#include <QMenu>
+#include <QToolButton>
+#include <QAction>
 
 #ifdef _DEBUG
 #include <QDebug>
@@ -222,10 +226,14 @@ void XGUI_Workshop::initMenu()
                                                          QIcon(":pictures/undo.png"),
                                                          QKeySequence::Undo, false, "MEN_DESK_EDIT");
     connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
+    addHistoryMenu(aAction, SIGNAL(updateUndoHistory(const QList<ActionInfo>&)), SLOT(onUndo(int)));
+
     aAction = salomeConnector()->addDesktopCommand("REDO_CMD", tr("Redo"), tr("Redo last command"),
                                                 QIcon(":pictures/redo.png"), QKeySequence::Redo,
                                                 false, "MEN_DESK_EDIT");
     connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
+    addHistoryMenu(aAction, SIGNAL(updateRedoHistory(const QList<ActionInfo>&)), SLOT(onRedo(int)));
+
     salomeConnector()->addDesktopMenuSeparator("MEN_DESK_EDIT");
     aAction = salomeConnector()->addDesktopCommand("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
                                                 QIcon(":pictures/rebuild.png"), QKeySequence(),
@@ -256,13 +264,23 @@ void XGUI_Workshop::initMenu()
   aCommand->connectTo(this, SLOT(onSave()));
   //aCommand->disable();
 
-  aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
+  QString aUndoId = "UNDO_CMD";
+  aCommand = aGroup->addFeature(aUndoId, tr("Undo"), tr("Undo last command"),
                                 QIcon(":pictures/undo.png"), QKeySequence::Undo);
   aCommand->connectTo(this, SLOT(onUndo()));
+  QToolButton* aUndoButton = qobject_cast<QToolButton*>(aGroup->widget(aUndoId));
+  addHistoryMenu(aUndoButton,
+                 SIGNAL(updateUndoHistory(const QList<ActionInfo>&)),
+                 SLOT(onUndo(int)));
 
-  aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
+  QString aRedoId = "REDO_CMD";
+  aCommand = aGroup->addFeature(aRedoId, tr("Redo"), tr("Redo last command"),
                                 QIcon(":pictures/redo.png"), QKeySequence::Redo);
   aCommand->connectTo(this, SLOT(onRedo()));
+  QToolButton* aRedoButton = qobject_cast<QToolButton*>(aGroup->widget(aRedoId));
+  addHistoryMenu(aRedoButton,
+                 SIGNAL(updateRedoHistory(const QList<ActionInfo>&)),
+                 SLOT(onRedo(int)));
 
   aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
     QIcon(":pictures/rebuild.png"), QKeySequence());
@@ -672,28 +690,24 @@ 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;
 
-  //Find or create Workbench
   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(theMessage->icon().c_str()),
-                                                     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);
   } else {
-
+    //Find or create Workbench
     AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
     AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
     if (!aPage) {
@@ -705,19 +719,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);
   }
@@ -894,24 +903,28 @@ bool XGUI_Workshop::onSaveAs()
 }
 
 //******************************************************
-void XGUI_Workshop::onUndo()
+void XGUI_Workshop::onUndo(int theTimes)
 {
   objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
   SessionPtr aMgr = ModelAPI_Session::get();
   if (aMgr->isOperation())
     operationMgr()->onAbortOperation();
-  aMgr->undo();
+  for (int i = 0; i < theTimes; ++i) {
+    aMgr->undo();
+  }
   updateCommandStatus();
 }
 
 //******************************************************
-void XGUI_Workshop::onRedo()
+void XGUI_Workshop::onRedo(int theTimes)
 {
   objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
   SessionPtr aMgr = ModelAPI_Session::get();
   if (aMgr->isOperation())
     operationMgr()->onAbortOperation();
-  aMgr->redo();
+  for (int i = 0; i < theTimes; ++i) {
+    aMgr->redo();
+  }
   updateCommandStatus();
 }
 
@@ -921,7 +934,7 @@ void XGUI_Workshop::onRebuild()
   SessionPtr aMgr = ModelAPI_Session::get();
   bool aWasOperation = aMgr->isOperation(); // keep this value
   if (!aWasOperation) {
-    aMgr->startOperation();
+    aMgr->startOperation("Rebuild");
   }
   static const Events_ID aRebuildEvent = Events_Loop::loop()->eventByName("Rebuild");
   Events_Loop::loop()->send(std::shared_ptr<Events_Message>(
@@ -1050,6 +1063,9 @@ void XGUI_Workshop::updateCommandStatus()
     }
     aUndoCmd->setEnabled(aMgr->canUndo() && !aMgr->isOperation());
     aRedoCmd->setEnabled(aMgr->canRedo() && !aMgr->isOperation());
+
+    updateHistory();
+
   } else {
     foreach(QAction* aCmd, aCommands) {
       QString aId = aCmd->data().toString();
@@ -1065,6 +1081,33 @@ void XGUI_Workshop::updateCommandStatus()
   emit commandStatusUpdated();
 }
 
+void XGUI_Workshop::updateHistory()
+{
+  std::list<std::string> aUndoList = ModelAPI_Session::get()->undoList();
+  std::list<std::string>::iterator it = aUndoList.begin();
+  QList<ActionInfo> aUndoRes;
+  for ( ; it != aUndoList.end(); it++) {
+    QString anId = QString::fromStdString(*it);
+    QIcon aIcon;
+    if (myIcons.contains(anId))
+      aIcon = QIcon(myIcons[anId]);
+    aUndoRes << ActionInfo(aIcon, anId);
+  }
+  emit updateUndoHistory(aUndoRes);
+
+  std::list<std::string> aRedoList = ModelAPI_Session::get()->redoList();
+  it = aRedoList.begin();
+  QList<ActionInfo> aRedoRes;
+  for ( ; it != aRedoList.end(); it++) {
+    QString anId = QString::fromStdString(*it);
+    QIcon aIcon;
+    if (myIcons.contains(anId))
+      aIcon = QIcon(myIcons[anId]);
+    aRedoRes << ActionInfo(aIcon, anId);
+  }
+  emit updateRedoHistory(aUndoRes);
+}
+
 //******************************************************
 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
 {
@@ -1302,7 +1345,7 @@ These features will be deleted also. Would you like to continue?")).arg(aNames),
   }
 
   SessionPtr aMgr = ModelAPI_Session::get();
-  aMgr->startOperation();
+  aMgr->startOperation("DeleteObjects");
   std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
                                        aLast = aRefFeatures.end();
   for (; anIt != aLast; anIt++) {
@@ -1429,3 +1472,20 @@ void XGUI_Workshop::displayObject(ObjectPtr theObj)
   } else 
     myDisplayer->display(theObj, false);
 }
+
+void XGUI_Workshop::addHistoryMenu(QObject* theObject, const char* theSignal, const char* theSlot)
+{
+  XGUI_HistoryMenu* aMenu = NULL;
+  if (isSalomeMode()) {
+    QAction* anAction = qobject_cast<QAction*>(theObject);
+    if (!anAction)
+      return;
+    aMenu = new XGUI_HistoryMenu(anAction);
+  } else {
+    QToolButton* aButton =  qobject_cast<QToolButton*>(theObject);
+    aMenu = new XGUI_HistoryMenu(aButton);
+  }
+  connect(this, theSignal, aMenu, SLOT(setHistory(const QList<ActionInfo>&)));
+  connect(aMenu, SIGNAL(actionSelected(int)), this, theSlot);
+
+}