Salome HOME
Undo/Redo list for salome mode
authorsbh <sergey.belash@opencascade.com>
Mon, 9 Feb 2015 08:52:16 +0000 (11:52 +0300)
committersbh <sergey.belash@opencascade.com>
Mon, 9 Feb 2015 08:52:16 +0000 (11:52 +0300)
src/XGUI/XGUI_HistoryMenu.cpp
src/XGUI/XGUI_HistoryMenu.h
src/XGUI/XGUI_Workshop.cpp
src/XGUI/XGUI_Workshop.h

index 2aa5cbc88a74b53eea5db933dc5fd67c22e381b3..7ae27d0b3c2946413a3a9e37902b4853f6666e5a 100644 (file)
 #include <QListWidget>
 #include <QWidgetAction>
 #include <QToolButton>
+#include <QAction>
+
+//! Extends given feature with previously created context menu.
+//! \param theId - Id of the feature to add \a theMenu
+//! \param theMenu - Enables or disables menu feature
+XGUI_HistoryMenu::XGUI_HistoryMenu(QAction* theParent)
+ : QMenu(NULL),
+   myHistoryList(NULL)
+{
+  theParent->setMenu(this);
+  initMenu();
+
+  connect(theParent, SIGNAL(destroyed()), this, SLOT(deleteLater()));
+}
 
 //! Extends given feature with previously created context menu.
 //! \param theId - Id of the feature to add \a theMenu
 //! \param theMenu - Enables or disables menu feature
 XGUI_HistoryMenu::XGUI_HistoryMenu(QToolButton* theParent)
  : QMenu(theParent),
-   myHistoryList(new QListWidget(this))
+   myHistoryList(NULL)
 {
   theParent->setMenu(this);
   theParent->setPopupMode(QToolButton::MenuButtonPopup);
 
+  initMenu();
+}
+
+void XGUI_HistoryMenu::initMenu()
+{
+  myHistoryList = new QListWidget(this);
   QWidgetAction* aListAction = new QWidgetAction(this);
   aListAction->setDefaultWidget(myHistoryList);
   this->addAction(aListAction);
-
-  myHistoryList->setMouseTracking(true); // track mouse hover
+  myHistoryList->setMouseTracking(true);  // track mouse hover
   myHistoryList->setSelectionMode(QAbstractItemView::ExtendedSelection);
-  connect(myHistoryList, SIGNAL(itemEntered(QListWidgetItem *)),
-          this,          SLOT(setStackSelectedTo(QListWidgetItem *)));
-  connect(myHistoryList, SIGNAL(itemClicked(QListWidgetItem *)),
-          this,          SLOT(onItemPressed(QListWidgetItem *)));
+  connect(myHistoryList, SIGNAL(itemEntered(QListWidgetItem *)), this,
+          SLOT(setStackSelectedTo(QListWidgetItem *)));
+  connect(myHistoryList, SIGNAL(itemClicked(QListWidgetItem *)), this,
+          SLOT(onItemPressed(QListWidgetItem *)));
 }
 
 XGUI_HistoryMenu::~XGUI_HistoryMenu()
@@ -68,7 +87,7 @@ void XGUI_HistoryMenu::setStackSelectedTo(QListWidgetItem * theItem)
 void XGUI_HistoryMenu::onItemPressed(QListWidgetItem * theItem)
 {
   int selectedSize = myHistoryList->row(theItem) + 1;
-  emit actionsSelected(selectedSize);
+  emit actionSelected(selectedSize);
   hide();
   myHistoryList->clear();
 }
index 9dd2951acc579de46bb27aca188100592571ea4a..bc45f7a4d9c14b9db5b29bafaa2655eb789068a5 100644 (file)
@@ -22,10 +22,11 @@ class XGUI_EXPORT XGUI_HistoryMenu : public QMenu
   Q_OBJECT
  public:
   explicit XGUI_HistoryMenu(QToolButton* theParent);
+  explicit XGUI_HistoryMenu(QAction* theParent);
   virtual ~XGUI_HistoryMenu();
 
  signals:
-  void actionsSelected(int);
+  void actionSelected(int);
 
  public slots:
   void setHistory(const QList<ActionInfo>&);
@@ -33,6 +34,7 @@ class XGUI_EXPORT XGUI_HistoryMenu : public QMenu
  protected slots:
   void setStackSelectedTo(QListWidgetItem *);
   void onItemPressed(QListWidgetItem *);
+  void initMenu();
 
  private:
   QListWidget* myHistoryList;
index de72077be8306b0609e8f0bef15abd45ef41a69f..fd63e89e812a8ad3d4f606530327e2e3140dabca 100644 (file)
@@ -226,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(),
@@ -264,25 +268,19 @@ void XGUI_Workshop::initMenu()
   aCommand = aGroup->addFeature(aUndoId, tr("Undo"), tr("Undo last command"),
                                 QIcon(":pictures/undo.png"), QKeySequence::Undo);
   aCommand->connectTo(this, SLOT(onUndo()));
-
-  QToolButton* aToolBtn = qobject_cast<QToolButton*>(aGroup->widget(aUndoId));
-  XGUI_HistoryMenu* aUndoMenu = new XGUI_HistoryMenu(aToolBtn);
-  connect(this,  SIGNAL(updateUndoHistory(const QList<ActionInfo>&)),
-          aUndoMenu, SLOT(setHistory(const QList<ActionInfo>&)));
-  connect(aUndoMenu, SIGNAL(actionsSelected(int)),
-          this,  SLOT(onUndo(int)));
+  QToolButton* aUndoButton = qobject_cast<QToolButton*>(aGroup->widget(aUndoId));
+  addHistoryMenu(aUndoButton,
+                 SIGNAL(updateUndoHistory(const QList<ActionInfo>&)),
+                 SLOT(onUndo(int)));
 
   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()));
-
-  aToolBtn = qobject_cast<QToolButton*>(aGroup->widget(aRedoId));
-  XGUI_HistoryMenu* aRedoMenu = new XGUI_HistoryMenu(aToolBtn);
-  connect(this,  SIGNAL(updateUndoHistory(const QList<ActionInfo>&)),
-          aRedoMenu, SLOT(setHistory(const QList<QAction*>&)));
-  connect(aRedoMenu, SIGNAL(actionsSelected(int)),
-          this,  SLOT(onUndo(int)));
+  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());
@@ -1474,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);
+
+}
index a79b8e20970b8477fd6fafb4161d5f22d3820b7f..1cf400d0f89a52956cf8e9d05516641cfea30c80 100644 (file)
@@ -385,6 +385,13 @@ signals:
   /// Displaus object and fit all viewer if the object is first (update viewer will not be called)
   void displayObject(ObjectPtr theObj);
 
+  //! Extends undo/redo toolbutton's with history menu
+  //! \param theObject - in the OpenParts it is a QToolButton by itself,
+  //! in salome mode - QAction that creates a button.
+  //! \param theSignal - void "updateUndoHistory" or "updateRedoHistory" SIGNAL;
+  //! \param theSlot - onUndo(int) or onRedo(int) SLOT
+  void addHistoryMenu(QObject* theObject, const char* theSignal, const char* theSlot);
+
 private:
   AppElements_MainWindow* myMainWindow;
   ModuleBase_IModule* myModule;