Salome HOME
Merge branch 'master' into Dev_1.1.0
[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 #include <QAction>
16
17 //! Extends given feature with previously created context menu.
18 //! \param theId - Id of the feature to add \a theMenu
19 //! \param theMenu - Enables or disables menu feature
20 XGUI_HistoryMenu::XGUI_HistoryMenu(QAction* theParent)
21  : QMenu(NULL),
22    myHistoryList(NULL)
23 {
24   theParent->setMenu(this);
25   initMenu();
26
27   connect(theParent, SIGNAL(destroyed()), this, SLOT(deleteLater()));
28 }
29
30 //! Extends given feature with previously created context menu.
31 //! \param theId - Id of the feature to add \a theMenu
32 //! \param theMenu - Enables or disables menu feature
33 XGUI_HistoryMenu::XGUI_HistoryMenu(QToolButton* theParent)
34  : QMenu(theParent),
35    myHistoryList(NULL)
36 {
37   theParent->setMenu(this);
38   theParent->setPopupMode(QToolButton::MenuButtonPopup);
39
40   initMenu();
41 }
42
43 void XGUI_HistoryMenu::initMenu()
44 {
45   myHistoryList = new QListWidget(this);
46   QWidgetAction* aListAction = new QWidgetAction(this);
47   aListAction->setDefaultWidget(myHistoryList);
48   this->addAction(aListAction);
49   myHistoryList->setMouseTracking(true);  // track mouse hover
50   myHistoryList->setSelectionMode(QAbstractItemView::ExtendedSelection);
51   connect(myHistoryList, SIGNAL(itemEntered(QListWidgetItem *)), this,
52           SLOT(setStackSelectedTo(QListWidgetItem *)));
53   connect(myHistoryList, SIGNAL(itemClicked(QListWidgetItem *)), this,
54           SLOT(onItemPressed(QListWidgetItem *)));
55 }
56
57 XGUI_HistoryMenu::~XGUI_HistoryMenu()
58 {
59 }
60
61 void XGUI_HistoryMenu::setHistory(const QList<ActionInfo>& theActions)
62 {
63   myHistoryList->clear();
64   foreach(ActionInfo anAct, theActions) {
65     QListWidgetItem* anItem = new QListWidgetItem(anAct.icon, anAct.text, myHistoryList);
66   }
67 }
68
69
70 void XGUI_HistoryMenu::setStackSelectedTo(QListWidgetItem * theItem)
71 {
72   if (!theItem)
73     return;
74
75   QListWidgetItem* eachItem = NULL;
76   bool isSelect = true;
77   for(int aRow = 0; aRow < myHistoryList->count(); ++aRow) {
78     eachItem = myHistoryList->item(aRow);
79     myHistoryList->setItemSelected(eachItem, isSelect);
80     // Deselect items below hovered
81     if (eachItem == theItem) {
82       isSelect = false;
83     }
84   }
85 }
86
87 void XGUI_HistoryMenu::onItemPressed(QListWidgetItem * theItem)
88 {
89   int selectedSize = myHistoryList->row(theItem) + 1;
90   emit actionSelected(selectedSize);
91   hide();
92   myHistoryList->clear();
93 }