1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include <XGUI_HistoryMenu.h>
22 #include <ModelAPI_Session.h>
24 #include <QListWidget>
25 #include <QWidgetAction>
26 #include <QToolButton>
29 XGUI_HistoryMenu::XGUI_HistoryMenu(QAction* theParent)
33 theParent->setMenu(this);
36 connect(theParent, SIGNAL(destroyed()), this, SLOT(deleteLater()));
39 XGUI_HistoryMenu::XGUI_HistoryMenu(QToolButton* theParent)
43 theParent->setMenu(this);
44 theParent->setPopupMode(QToolButton::MenuButtonPopup);
49 void XGUI_HistoryMenu::initMenu()
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 *)));
63 XGUI_HistoryMenu::~XGUI_HistoryMenu()
67 void XGUI_HistoryMenu::setHistory(const QList<ActionInfo>& theActions)
69 myHistoryList->clear();
70 foreach(ActionInfo anAct, theActions) {
71 QListWidgetItem* anItem = new QListWidgetItem(anAct.icon, anAct.text, myHistoryList);
75 void XGUI_HistoryMenu::leaveEvent(QEvent* theEvent)
77 setStackSelectedTo(NULL);
78 QMenu::leaveEvent(theEvent);
81 void XGUI_HistoryMenu::setStackSelectedTo(QListWidgetItem * theItem)
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) {
93 // to avoid blinking caused by QMenu paint event (paints on top of the list)
94 myHistoryList->repaint();
97 void hideUpToMenuBar( QMenu* theMenu )
100 foreach( QWidget* aWidget, theMenu->menuAction()->associatedWidgets() )
102 QMenu* aMenu = qobject_cast<QMenu*>( aWidget );
106 hideUpToMenuBar( aMenu );
111 void XGUI_HistoryMenu::onItemPressed(QListWidgetItem * theItem)
113 int selectedSize = myHistoryList->row(theItem) + 1;
114 emit actionSelected(selectedSize);
115 hideUpToMenuBar( this );