From e41462efa92b485f31903e3653d523b0a77a5296 Mon Sep 17 00:00:00 2001 From: sbh Date: Mon, 9 Feb 2015 11:52:16 +0300 Subject: [PATCH] Undo/Redo list for salome mode --- src/XGUI/XGUI_HistoryMenu.cpp | 35 +++++++++++++++++++++------- src/XGUI/XGUI_HistoryMenu.h | 4 +++- src/XGUI/XGUI_Workshop.cpp | 43 +++++++++++++++++++++++------------ src/XGUI/XGUI_Workshop.h | 7 ++++++ 4 files changed, 66 insertions(+), 23 deletions(-) diff --git a/src/XGUI/XGUI_HistoryMenu.cpp b/src/XGUI/XGUI_HistoryMenu.cpp index 2aa5cbc88..7ae27d0b3 100644 --- a/src/XGUI/XGUI_HistoryMenu.cpp +++ b/src/XGUI/XGUI_HistoryMenu.cpp @@ -12,27 +12,46 @@ #include #include #include +#include + +//! 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(); } diff --git a/src/XGUI/XGUI_HistoryMenu.h b/src/XGUI/XGUI_HistoryMenu.h index 9dd2951ac..bc45f7a4d 100644 --- a/src/XGUI/XGUI_HistoryMenu.h +++ b/src/XGUI/XGUI_HistoryMenu.h @@ -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&); @@ -33,6 +34,7 @@ class XGUI_EXPORT XGUI_HistoryMenu : public QMenu protected slots: void setStackSelectedTo(QListWidgetItem *); void onItemPressed(QListWidgetItem *); + void initMenu(); private: QListWidget* myHistoryList; diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index de72077be..fd63e89e8 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -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&)), 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&)), 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(aGroup->widget(aUndoId)); - XGUI_HistoryMenu* aUndoMenu = new XGUI_HistoryMenu(aToolBtn); - connect(this, SIGNAL(updateUndoHistory(const QList&)), - aUndoMenu, SLOT(setHistory(const QList&))); - connect(aUndoMenu, SIGNAL(actionsSelected(int)), - this, SLOT(onUndo(int))); + QToolButton* aUndoButton = qobject_cast(aGroup->widget(aUndoId)); + addHistoryMenu(aUndoButton, + SIGNAL(updateUndoHistory(const QList&)), + 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(aGroup->widget(aRedoId)); - XGUI_HistoryMenu* aRedoMenu = new XGUI_HistoryMenu(aToolBtn); - connect(this, SIGNAL(updateUndoHistory(const QList&)), - aRedoMenu, SLOT(setHistory(const QList&))); - connect(aRedoMenu, SIGNAL(actionsSelected(int)), - this, SLOT(onUndo(int))); + QToolButton* aRedoButton = qobject_cast(aGroup->widget(aRedoId)); + addHistoryMenu(aRedoButton, + SIGNAL(updateRedoHistory(const QList&)), + 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(theObject); + if (!anAction) + return; + aMenu = new XGUI_HistoryMenu(anAction); + } else { + QToolButton* aButton = qobject_cast(theObject); + aMenu = new XGUI_HistoryMenu(aButton); + } + connect(this, theSignal, aMenu, SLOT(setHistory(const QList&))); + connect(aMenu, SIGNAL(actionSelected(int)), this, theSlot); + +} diff --git a/src/XGUI/XGUI_Workshop.h b/src/XGUI/XGUI_Workshop.h index a79b8e209..1cf400d0f 100644 --- a/src/XGUI/XGUI_Workshop.h +++ b/src/XGUI/XGUI_Workshop.h @@ -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; -- 2.39.2