Salome HOME
Update copyrights
[modules/shaper.git] / src / XGUI / XGUI_HistoryMenu.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <XGUI_HistoryMenu.h>
21
22 #include <ModelAPI_Session.h>
23
24 #include <QListWidget>
25 #include <QWidgetAction>
26 #include <QToolButton>
27 #include <QAction>
28
29 XGUI_HistoryMenu::XGUI_HistoryMenu(QAction* theParent)
30  : QMenu(NULL),
31    myHistoryList(NULL)
32 {
33   theParent->setMenu(this);
34   initMenu();
35
36   connect(theParent, SIGNAL(destroyed()), this, SLOT(deleteLater()));
37 }
38
39 XGUI_HistoryMenu::XGUI_HistoryMenu(QToolButton* theParent)
40  : QMenu(theParent),
41    myHistoryList(NULL)
42 {
43   theParent->setMenu(this);
44   theParent->setPopupMode(QToolButton::MenuButtonPopup);
45
46   initMenu();
47 }
48
49 void XGUI_HistoryMenu::initMenu()
50 {
51   myHistoryList = new QListWidget(this);
52   QWidgetAction* aListAction = new QWidgetAction(this);
53   aListAction->setDefaultWidget(myHistoryList);
54   this->addAction(aListAction);
55   myHistoryList->setMouseTracking(true);  // track mouse hover
56   myHistoryList->setSelectionMode(QAbstractItemView::ExtendedSelection);
57   connect(myHistoryList, SIGNAL(itemEntered(QListWidgetItem *)), this,
58           SLOT(setStackSelectedTo(QListWidgetItem *)));
59   connect(myHistoryList, SIGNAL(itemClicked(QListWidgetItem *)), this,
60           SLOT(onItemPressed(QListWidgetItem *)));
61 }
62
63 XGUI_HistoryMenu::~XGUI_HistoryMenu()
64 {
65 }
66
67 void XGUI_HistoryMenu::setHistory(const QList<ActionInfo>& theActions)
68 {
69   myHistoryList->clear();
70   foreach(ActionInfo anAct, theActions) {
71     QListWidgetItem* anItem = new QListWidgetItem(anAct.icon, anAct.text, myHistoryList);
72   }
73 }
74
75 void XGUI_HistoryMenu::leaveEvent(QEvent* theEvent)
76 {
77   setStackSelectedTo(NULL);
78   QMenu::leaveEvent(theEvent);
79 }
80
81 void XGUI_HistoryMenu::setStackSelectedTo(QListWidgetItem * theItem)
82 {
83   QListWidgetItem* eachItem = NULL;
84   bool isSelect = theItem != NULL;
85   for(int aRow = 0; aRow < myHistoryList->count(); ++aRow) {
86     eachItem = myHistoryList->item(aRow);
87     myHistoryList->setItemSelected(eachItem, isSelect);
88     // Deselect items below hovered
89     if (eachItem == theItem) {
90       isSelect = false;
91     }
92   }
93   // to avoid blinking caused by QMenu paint event (paints on top of the list)
94   myHistoryList->repaint();
95 }
96
97 void hideUpToMenuBar( QMenu* theMenu )
98 {
99   theMenu->hide();
100   foreach( QWidget* aWidget, theMenu->menuAction()->associatedWidgets() )
101   {
102     QMenu* aMenu = qobject_cast<QMenu*>( aWidget );
103     if( aMenu )
104     {
105       aMenu->hide();
106       hideUpToMenuBar( aMenu );
107     }
108   }
109 }
110
111 void XGUI_HistoryMenu::onItemPressed(QListWidgetItem * theItem)
112 {
113   int selectedSize = myHistoryList->row(theItem) + 1;
114   emit actionSelected(selectedSize);
115   hideUpToMenuBar( this );
116 }