Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / XGUI / XGUI_HistoryMenu.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <XGUI_HistoryMenu.h>
22
23 #include <ModelAPI_Session.h>
24
25 #include <QListWidget>
26 #include <QWidgetAction>
27 #include <QToolButton>
28 #include <QAction>
29
30 XGUI_HistoryMenu::XGUI_HistoryMenu(QAction* theParent)
31  : QMenu(NULL),
32    myHistoryList(NULL)
33 {
34   theParent->setMenu(this);
35   initMenu();
36
37   connect(theParent, SIGNAL(destroyed()), this, SLOT(deleteLater()));
38 }
39
40 XGUI_HistoryMenu::XGUI_HistoryMenu(QToolButton* theParent)
41  : QMenu(theParent),
42    myHistoryList(NULL)
43 {
44   theParent->setMenu(this);
45   theParent->setPopupMode(QToolButton::MenuButtonPopup);
46
47   initMenu();
48 }
49
50 void XGUI_HistoryMenu::initMenu()
51 {
52   myHistoryList = new QListWidget(this);
53   QWidgetAction* aListAction = new QWidgetAction(this);
54   aListAction->setDefaultWidget(myHistoryList);
55   this->addAction(aListAction);
56   myHistoryList->setMouseTracking(true);  // track mouse hover
57   myHistoryList->setSelectionMode(QAbstractItemView::ExtendedSelection);
58   connect(myHistoryList, SIGNAL(itemEntered(QListWidgetItem *)), this,
59           SLOT(setStackSelectedTo(QListWidgetItem *)));
60   connect(myHistoryList, SIGNAL(itemClicked(QListWidgetItem *)), this,
61           SLOT(onItemPressed(QListWidgetItem *)));
62 }
63
64 XGUI_HistoryMenu::~XGUI_HistoryMenu()
65 {
66 }
67
68 void XGUI_HistoryMenu::setHistory(const QList<ActionInfo>& theActions)
69 {
70   myHistoryList->clear();
71   foreach(ActionInfo anAct, theActions) {
72     QListWidgetItem* anItem = new QListWidgetItem(anAct.icon, anAct.text, myHistoryList);
73   }
74 }
75
76 void XGUI_HistoryMenu::leaveEvent(QEvent* theEvent)
77 {
78   setStackSelectedTo(NULL);
79   QMenu::leaveEvent(theEvent);
80 }
81
82 void XGUI_HistoryMenu::setStackSelectedTo(QListWidgetItem * theItem)
83 {
84   QListWidgetItem* eachItem = NULL;
85   bool isSelect = theItem != NULL;
86   for(int aRow = 0; aRow < myHistoryList->count(); ++aRow) {
87     eachItem = myHistoryList->item(aRow);
88     myHistoryList->setItemSelected(eachItem, isSelect);
89     // Deselect items below hovered
90     if (eachItem == theItem) {
91       isSelect = false;
92     }
93   }
94   // to avoid blinking caused by QMenu paint event (paints on top of the list)
95   myHistoryList->repaint();
96 }
97
98 void hideUpToMenuBar( QMenu* theMenu )
99 {
100   theMenu->hide();
101   foreach( QWidget* aWidget, theMenu->menuAction()->associatedWidgets() )
102   {
103     QMenu* aMenu = qobject_cast<QMenu*>( aWidget );
104     if( aMenu )
105     {
106       aMenu->hide();
107       hideUpToMenuBar( aMenu );
108     }
109   }
110 }
111
112 void XGUI_HistoryMenu::onItemPressed(QListWidgetItem * theItem)
113 {
114   int selectedSize = myHistoryList->row(theItem) + 1;
115   emit actionSelected(selectedSize);
116   hideUpToMenuBar( this );
117 }