1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <XGUI_HistoryMenu.h>
23 #include <ModelAPI_Session.h>
25 #include <QListWidget>
26 #include <QWidgetAction>
27 #include <QToolButton>
30 XGUI_HistoryMenu::XGUI_HistoryMenu(QAction* theParent)
34 theParent->setMenu(this);
37 connect(theParent, SIGNAL(destroyed()), this, SLOT(deleteLater()));
40 XGUI_HistoryMenu::XGUI_HistoryMenu(QToolButton* theParent)
44 theParent->setMenu(this);
45 theParent->setPopupMode(QToolButton::MenuButtonPopup);
50 void XGUI_HistoryMenu::initMenu()
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 *)));
64 XGUI_HistoryMenu::~XGUI_HistoryMenu()
68 void XGUI_HistoryMenu::setHistory(const QList<ActionInfo>& theActions)
70 myHistoryList->clear();
71 foreach(ActionInfo anAct, theActions) {
72 QListWidgetItem* anItem = new QListWidgetItem(anAct.icon, anAct.text, myHistoryList);
76 void XGUI_HistoryMenu::leaveEvent(QEvent* theEvent)
78 setStackSelectedTo(NULL);
79 QMenu::leaveEvent(theEvent);
82 void XGUI_HistoryMenu::setStackSelectedTo(QListWidgetItem * theItem)
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) {
94 // to avoid blinking caused by QMenu paint event (paints on top of the list)
95 myHistoryList->repaint();
98 void hideUpToMenuBar( QMenu* theMenu )
101 foreach( QWidget* aWidget, theMenu->menuAction()->associatedWidgets() )
103 QMenu* aMenu = qobject_cast<QMenu*>( aWidget );
107 hideUpToMenuBar( aMenu );
112 void XGUI_HistoryMenu::onItemPressed(QListWidgetItem * theItem)
114 int selectedSize = myHistoryList->row(theItem) + 1;
115 emit actionSelected(selectedSize);
116 hideUpToMenuBar( this );