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