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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 * XGUI_HistoryMenu.cpp
23 * Created on: Feb 2, 2015
27 #include <XGUI_HistoryMenu.h>
29 #include <ModelAPI_Session.h>
31 #include <QListWidget>
32 #include <QWidgetAction>
33 #include <QToolButton>
36 XGUI_HistoryMenu::XGUI_HistoryMenu(QAction* theParent)
40 theParent->setMenu(this);
43 connect(theParent, SIGNAL(destroyed()), this, SLOT(deleteLater()));
46 XGUI_HistoryMenu::XGUI_HistoryMenu(QToolButton* theParent)
50 theParent->setMenu(this);
51 theParent->setPopupMode(QToolButton::MenuButtonPopup);
56 void XGUI_HistoryMenu::initMenu()
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 *)));
70 XGUI_HistoryMenu::~XGUI_HistoryMenu()
74 void XGUI_HistoryMenu::setHistory(const QList<ActionInfo>& theActions)
76 myHistoryList->clear();
77 foreach(ActionInfo anAct, theActions) {
78 QListWidgetItem* anItem = new QListWidgetItem(anAct.icon, anAct.text, myHistoryList);
82 void XGUI_HistoryMenu::leaveEvent(QEvent* theEvent)
84 setStackSelectedTo(NULL);
85 QMenu::leaveEvent(theEvent);
88 void XGUI_HistoryMenu::setStackSelectedTo(QListWidgetItem * theItem)
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) {
100 // to avoid blinking caused by QMenu paint event (paints on top of the list)
101 myHistoryList->repaint();
104 void hideUpToMenuBar( QMenu* theMenu )
107 foreach( QWidget* aWidget, theMenu->menuAction()->associatedWidgets() )
109 QMenu* aMenu = qobject_cast<QMenu*>( aWidget );
113 hideUpToMenuBar( aMenu );
118 void XGUI_HistoryMenu::onItemPressed(QListWidgetItem * theItem)
120 int selectedSize = myHistoryList->row(theItem) + 1;
121 emit actionSelected(selectedSize);
122 hideUpToMenuBar( this );