Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git 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 XGUI_HistoryMenu::XGUI_HistoryMenu(QAction* theParent)
18  : QMenu(NULL),
19    myHistoryList(NULL)
20 {
21   theParent->setMenu(this);
22   initMenu();
23
24   connect(theParent, SIGNAL(destroyed()), this, SLOT(deleteLater()));
25 }
26
27 XGUI_HistoryMenu::XGUI_HistoryMenu(QToolButton* theParent)
28  : QMenu(theParent),
29    myHistoryList(NULL)
30 {
31   theParent->setMenu(this);
32   theParent->setPopupMode(QToolButton::MenuButtonPopup);
33
34   initMenu();
35 }
36
37 void XGUI_HistoryMenu::initMenu()
38 {
39   myHistoryList = new QListWidget(this);
40   QWidgetAction* aListAction = new QWidgetAction(this);
41   aListAction->setDefaultWidget(myHistoryList);
42   this->addAction(aListAction);
43   myHistoryList->setMouseTracking(true);  // track mouse hover
44   myHistoryList->setSelectionMode(QAbstractItemView::ExtendedSelection);
45   connect(myHistoryList, SIGNAL(itemEntered(QListWidgetItem *)), this,
46           SLOT(setStackSelectedTo(QListWidgetItem *)));
47   connect(myHistoryList, SIGNAL(itemClicked(QListWidgetItem *)), this,
48           SLOT(onItemPressed(QListWidgetItem *)));
49 }
50
51 XGUI_HistoryMenu::~XGUI_HistoryMenu()
52 {
53 }
54
55 void XGUI_HistoryMenu::setHistory(const QList<ActionInfo>& theActions)
56 {
57   myHistoryList->clear();
58   foreach(ActionInfo anAct, theActions) {
59     QListWidgetItem* anItem = new QListWidgetItem(anAct.icon, anAct.text, myHistoryList);
60   }
61 }
62
63 void XGUI_HistoryMenu::leaveEvent(QEvent* theEvent)
64 {
65   setStackSelectedTo(NULL);
66   QMenu::leaveEvent(theEvent);
67 }
68
69 void XGUI_HistoryMenu::setStackSelectedTo(QListWidgetItem * theItem)
70 {
71   QListWidgetItem* eachItem = NULL;
72   bool isSelect = theItem != NULL;
73   for(int aRow = 0; aRow < myHistoryList->count(); ++aRow) {
74     eachItem = myHistoryList->item(aRow);
75     myHistoryList->setItemSelected(eachItem, isSelect);
76     // Deselect items below hovered
77     if (eachItem == theItem) {
78       isSelect = false;
79     }
80   }
81   // to avoid blinking caused by QMenu paint event (paints on top of the list)
82   myHistoryList->repaint();
83 }
84
85 void XGUI_HistoryMenu::onItemPressed(QListWidgetItem * theItem)
86 {
87   int selectedSize = myHistoryList->row(theItem) + 1;
88   emit actionSelected(selectedSize);
89   hide();
90 }