4 * Created on: Feb 2, 2015
8 #include <XGUI_HistoryMenu.h>
10 #include <ModelAPI_Session.h>
12 #include <QListWidget>
13 #include <QWidgetAction>
14 #include <QToolButton>
17 XGUI_HistoryMenu::XGUI_HistoryMenu(QAction* theParent)
21 theParent->setMenu(this);
24 connect(theParent, SIGNAL(destroyed()), this, SLOT(deleteLater()));
27 XGUI_HistoryMenu::XGUI_HistoryMenu(QToolButton* theParent)
31 theParent->setMenu(this);
32 theParent->setPopupMode(QToolButton::MenuButtonPopup);
37 void XGUI_HistoryMenu::initMenu()
39 myHistoryList = new QListWidget(this);
40 QWidgetAction* aListAction = new QWidgetAction(this);
41 aListAction->setDefaultWidget(myHistoryList);
42 this->addAction(aListAction);
43 myHistoryList->setMouseTracking(true); // track mouse hover
44 myHistoryList->setSelectionMode(QAbstractItemView::ExtendedSelection);
45 connect(myHistoryList, SIGNAL(itemEntered(QListWidgetItem *)), this,
46 SLOT(setStackSelectedTo(QListWidgetItem *)));
47 connect(myHistoryList, SIGNAL(itemClicked(QListWidgetItem *)), this,
48 SLOT(onItemPressed(QListWidgetItem *)));
51 XGUI_HistoryMenu::~XGUI_HistoryMenu()
55 void XGUI_HistoryMenu::setHistory(const QList<ActionInfo>& theActions)
57 myHistoryList->clear();
58 foreach(ActionInfo anAct, theActions) {
59 QListWidgetItem* anItem = new QListWidgetItem(anAct.icon, anAct.text, myHistoryList);
63 void XGUI_HistoryMenu::leaveEvent(QEvent* theEvent)
65 setStackSelectedTo(NULL);
66 QMenu::leaveEvent(theEvent);
69 void XGUI_HistoryMenu::setStackSelectedTo(QListWidgetItem * theItem)
71 QListWidgetItem* eachItem = NULL;
72 bool isSelect = theItem != NULL;
73 for(int aRow = 0; aRow < myHistoryList->count(); ++aRow) {
74 eachItem = myHistoryList->item(aRow);
75 myHistoryList->setItemSelected(eachItem, isSelect);
76 // Deselect items below hovered
77 if (eachItem == theItem) {
81 // to avoid blinking caused by QMenu paint event (paints on top of the list)
82 myHistoryList->repaint();
85 void hideUpToMenuBar( QMenu* theMenu )
88 foreach( QWidget* aWidget, theMenu->menuAction()->associatedWidgets() )
90 QMenu* aMenu = qobject_cast<QMenu*>( aWidget );
94 hideUpToMenuBar( aMenu );
99 void XGUI_HistoryMenu::onItemPressed(QListWidgetItem * theItem)
101 int selectedSize = myHistoryList->row(theItem) + 1;
102 emit actionSelected(selectedSize);
103 hideUpToMenuBar( this );