Salome HOME
Facilate action processing with ModuleBase_ActionInfo
[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<ActionInfo>& theActions)
43 {
44   myHistoryList->clear();
45   foreach(ActionInfo anAct, theActions) {
46     QListWidgetItem* anItem = new QListWidgetItem(anAct.icon, anAct.text, myHistoryList);
47   }
48 }
49
50
51 void XGUI_HistoryMenu::setStackSelectedTo(QListWidgetItem * theItem)
52 {
53   if (!theItem)
54     return;
55
56   QListWidgetItem* eachItem = NULL;
57   bool isSelect = true;
58   for(int aRow = 0; aRow < myHistoryList->count(); ++aRow) {
59     eachItem = myHistoryList->item(aRow);
60     myHistoryList->setItemSelected(eachItem, isSelect);
61     // Deselect items below hovered
62     if (eachItem == theItem) {
63       isSelect = false;
64     }
65   }
66 }
67
68 void XGUI_HistoryMenu::onItemPressed(QListWidgetItem * theItem)
69 {
70   int selectedSize = myHistoryList->row(theItem) + 1;
71   emit actionsSelected(selectedSize);
72   hide();
73   myHistoryList->clear();
74 }