1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
5 * Created on: Feb 2, 2015
9 #include <XGUI_HistoryMenu.h>
11 #include <ModelAPI_Session.h>
13 #include <QListWidget>
14 #include <QWidgetAction>
15 #include <QToolButton>
18 XGUI_HistoryMenu::XGUI_HistoryMenu(QAction* theParent)
22 theParent->setMenu(this);
25 connect(theParent, SIGNAL(destroyed()), this, SLOT(deleteLater()));
28 XGUI_HistoryMenu::XGUI_HistoryMenu(QToolButton* theParent)
32 theParent->setMenu(this);
33 theParent->setPopupMode(QToolButton::MenuButtonPopup);
38 void XGUI_HistoryMenu::initMenu()
40 myHistoryList = new QListWidget(this);
41 QWidgetAction* aListAction = new QWidgetAction(this);
42 aListAction->setDefaultWidget(myHistoryList);
43 this->addAction(aListAction);
44 myHistoryList->setMouseTracking(true); // track mouse hover
45 myHistoryList->setSelectionMode(QAbstractItemView::ExtendedSelection);
46 connect(myHistoryList, SIGNAL(itemEntered(QListWidgetItem *)), this,
47 SLOT(setStackSelectedTo(QListWidgetItem *)));
48 connect(myHistoryList, SIGNAL(itemClicked(QListWidgetItem *)), this,
49 SLOT(onItemPressed(QListWidgetItem *)));
52 XGUI_HistoryMenu::~XGUI_HistoryMenu()
56 void XGUI_HistoryMenu::setHistory(const QList<ActionInfo>& theActions)
58 myHistoryList->clear();
59 foreach(ActionInfo anAct, theActions) {
60 QListWidgetItem* anItem = new QListWidgetItem(anAct.icon, anAct.text, myHistoryList);
64 void XGUI_HistoryMenu::leaveEvent(QEvent* theEvent)
66 setStackSelectedTo(NULL);
67 QMenu::leaveEvent(theEvent);
70 void XGUI_HistoryMenu::setStackSelectedTo(QListWidgetItem * theItem)
72 QListWidgetItem* eachItem = NULL;
73 bool isSelect = theItem != NULL;
74 for(int aRow = 0; aRow < myHistoryList->count(); ++aRow) {
75 eachItem = myHistoryList->item(aRow);
76 myHistoryList->setItemSelected(eachItem, isSelect);
77 // Deselect items below hovered
78 if (eachItem == theItem) {
82 // to avoid blinking caused by QMenu paint event (paints on top of the list)
83 myHistoryList->repaint();
86 void hideUpToMenuBar( QMenu* theMenu )
89 foreach( QWidget* aWidget, theMenu->menuAction()->associatedWidgets() )
91 QMenu* aMenu = qobject_cast<QMenu*>( aWidget );
95 hideUpToMenuBar( aMenu );
100 void XGUI_HistoryMenu::onItemPressed(QListWidgetItem * theItem)
102 int selectedSize = myHistoryList->row(theItem) + 1;
103 emit actionSelected(selectedSize);
104 hideUpToMenuBar( this );