1 // Copyright (C) 2014-2020 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) {
72 QListWidgetItem* anItem =
74 new QListWidgetItem(anAct.icon, anAct.text, myHistoryList);
78 void XGUI_HistoryMenu::leaveEvent(QEvent* theEvent)
80 setStackSelectedTo(NULL);
81 QMenu::leaveEvent(theEvent);
84 void XGUI_HistoryMenu::setStackSelectedTo(QListWidgetItem * theItem)
86 QListWidgetItem* eachItem = NULL;
87 bool isSelect = theItem != NULL;
88 for(int aRow = 0; aRow < myHistoryList->count(); ++aRow) {
89 eachItem = myHistoryList->item(aRow);
90 myHistoryList->setItemSelected(eachItem, isSelect);
91 // Deselect items below hovered
92 if (eachItem == theItem) {
96 // to avoid blinking caused by QMenu paint event (paints on top of the list)
97 myHistoryList->repaint();
100 void hideUpToMenuBar( QMenu* theMenu )
103 foreach( QWidget* aWidget, theMenu->menuAction()->associatedWidgets() )
105 QMenu* aMenu = qobject_cast<QMenu*>( aWidget );
109 hideUpToMenuBar( aMenu );
114 void XGUI_HistoryMenu::onItemPressed(QListWidgetItem * theItem)
116 int selectedSize = myHistoryList->row(theItem) + 1;
117 emit actionSelected(selectedSize);
118 hideUpToMenuBar( this );