Salome HOME
Issue #2159 Hide all incomplete behavior
[modules/shaper.git] / src / XGUI / XGUI_HistoryMenu.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2 /*
3  * XGUI_HistoryMenu.cpp
4  *
5  *  Created on: Feb 2, 2015
6  *      Author: sbh
7  */
8
9 #include <XGUI_HistoryMenu.h>
10
11 #include <ModelAPI_Session.h>
12
13 #include <QListWidget>
14 #include <QWidgetAction>
15 #include <QToolButton>
16 #include <QAction>
17
18 XGUI_HistoryMenu::XGUI_HistoryMenu(QAction* theParent)
19  : QMenu(NULL),
20    myHistoryList(NULL)
21 {
22   theParent->setMenu(this);
23   initMenu();
24
25   connect(theParent, SIGNAL(destroyed()), this, SLOT(deleteLater()));
26 }
27
28 XGUI_HistoryMenu::XGUI_HistoryMenu(QToolButton* theParent)
29  : QMenu(theParent),
30    myHistoryList(NULL)
31 {
32   theParent->setMenu(this);
33   theParent->setPopupMode(QToolButton::MenuButtonPopup);
34
35   initMenu();
36 }
37
38 void XGUI_HistoryMenu::initMenu()
39 {
40   myHistoryList = new QListWidget(this);
41   QWidgetAction* aListAction = new QWidgetAction(this);
42   aListAction->setDefaultWidget(myHistoryList);
43   this->addAction(aListAction);
44   myHistoryList->setMouseTracking(true);  // track mouse hover
45   myHistoryList->setSelectionMode(QAbstractItemView::ExtendedSelection);
46   connect(myHistoryList, SIGNAL(itemEntered(QListWidgetItem *)), this,
47           SLOT(setStackSelectedTo(QListWidgetItem *)));
48   connect(myHistoryList, SIGNAL(itemClicked(QListWidgetItem *)), this,
49           SLOT(onItemPressed(QListWidgetItem *)));
50 }
51
52 XGUI_HistoryMenu::~XGUI_HistoryMenu()
53 {
54 }
55
56 void XGUI_HistoryMenu::setHistory(const QList<ActionInfo>& theActions)
57 {
58   myHistoryList->clear();
59   foreach(ActionInfo anAct, theActions) {
60     QListWidgetItem* anItem = new QListWidgetItem(anAct.icon, anAct.text, myHistoryList);
61   }
62 }
63
64 void XGUI_HistoryMenu::leaveEvent(QEvent* theEvent)
65 {
66   setStackSelectedTo(NULL);
67   QMenu::leaveEvent(theEvent);
68 }
69
70 void XGUI_HistoryMenu::setStackSelectedTo(QListWidgetItem * theItem)
71 {
72   QListWidgetItem* eachItem = NULL;
73   bool isSelect = theItem != NULL;
74   for(int aRow = 0; aRow < myHistoryList->count(); ++aRow) {
75     eachItem = myHistoryList->item(aRow);
76     myHistoryList->setItemSelected(eachItem, isSelect);
77     // Deselect items below hovered
78     if (eachItem == theItem) {
79       isSelect = false;
80     }
81   }
82   // to avoid blinking caused by QMenu paint event (paints on top of the list)
83   myHistoryList->repaint();
84 }
85
86 void hideUpToMenuBar( QMenu* theMenu )
87 {
88   theMenu->hide();
89   foreach( QWidget* aWidget, theMenu->menuAction()->associatedWidgets() )
90   {
91     QMenu* aMenu = qobject_cast<QMenu*>( aWidget );
92     if( aMenu )
93     {
94       aMenu->hide();
95       hideUpToMenuBar( aMenu );
96     }
97   }
98 }
99
100 void XGUI_HistoryMenu::onItemPressed(QListWidgetItem * theItem)
101 {
102   int selectedSize = myHistoryList->row(theItem) + 1;
103   emit actionSelected(selectedSize);
104   hideUpToMenuBar( this );
105 }