Salome HOME
History for undo and redo commands
[modules/shaper.git] / src / XGUI / XGUI_HistoryMenu.cpp
1 /*
2  * XGUI_HistoryMenu.cpp
3  *
4  *  Created on: Feb 2, 2015
5  *      Author: sbh
6  */
7
8 #include <XGUI_HistoryMenu.h>
9
10 #include <ModelAPI_Session.h>
11
12 #include <QListWidget>
13 #include <QWidgetAction>
14 #include <QToolButton>
15
16 //! Extends given feature with previously created context menu.
17 //! \param theId - Id of the feature to add \a theMenu
18 //! \param theMenu - Enables or disables menu feature
19 XGUI_HistoryMenu::XGUI_HistoryMenu(QToolButton* theParent)
20  : QMenu(theParent),
21    myHistoryList(new QListWidget(this))
22 {
23   theParent->setMenu(this);
24   theParent->setPopupMode(QToolButton::MenuButtonPopup);
25
26   QWidgetAction* aListAction = new QWidgetAction(this);
27   aListAction->setDefaultWidget(myHistoryList);
28   this->addAction(aListAction);
29
30   myHistoryList->setMouseTracking(true); // track mouse hover
31   myHistoryList->setSelectionMode(QAbstractItemView::ExtendedSelection);
32   connect(myHistoryList, SIGNAL(itemEntered(QListWidgetItem *)),
33           this,          SLOT(setStackSelectedTo(QListWidgetItem *)));
34   connect(myHistoryList, SIGNAL(itemClicked(QListWidgetItem *)),
35           this,          SLOT(onItemPressed(QListWidgetItem *)));
36 }
37
38 XGUI_HistoryMenu::~XGUI_HistoryMenu()
39 {
40 }
41
42 void XGUI_HistoryMenu::setHistory(const QList<QAction*>& theActions)
43 {
44   myHistoryList->clear();
45   foreach(QAction* anAct, theActions) {
46     QListWidgetItem* anItem = new QListWidgetItem(anAct->icon(),
47                                                   anAct->text(),
48                                                   myHistoryList);
49     anAct->deleteLater();
50   }
51 }
52
53
54 void XGUI_HistoryMenu::setStackSelectedTo(QListWidgetItem * theItem)
55 {
56   if (!theItem)
57     return;
58
59   QListWidgetItem* eachItem = NULL;
60   bool isSelect = true;
61   for(int aRow = 0; aRow < myHistoryList->count(); ++aRow) {
62     eachItem = myHistoryList->item(aRow);
63     myHistoryList->setItemSelected(eachItem, isSelect);
64     // Deselect items below hovered
65     if (eachItem == theItem) {
66       isSelect = false;
67     }
68   }
69 }
70
71 void XGUI_HistoryMenu::onItemPressed(QListWidgetItem * theItem)
72 {
73   int selectedSize = myHistoryList->row(theItem) + 1;
74   emit actionsSelected(selectedSize);
75   hide();
76   myHistoryList->clear();
77 }