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 //! Extends given feature with previously created context menu.
18 //! \param theId - Id of the feature to add \a theMenu
19 //! \param theMenu - Enables or disables menu feature
20 XGUI_HistoryMenu::XGUI_HistoryMenu(QAction* theParent)
24 theParent->setMenu(this);
27 connect(theParent, SIGNAL(destroyed()), this, SLOT(deleteLater()));
30 //! Extends given feature with previously created context menu.
31 //! \param theId - Id of the feature to add \a theMenu
32 //! \param theMenu - Enables or disables menu feature
33 XGUI_HistoryMenu::XGUI_HistoryMenu(QToolButton* theParent)
37 theParent->setMenu(this);
38 theParent->setPopupMode(QToolButton::MenuButtonPopup);
43 void XGUI_HistoryMenu::initMenu()
45 myHistoryList = new QListWidget(this);
46 QWidgetAction* aListAction = new QWidgetAction(this);
47 aListAction->setDefaultWidget(myHistoryList);
48 this->addAction(aListAction);
49 myHistoryList->setMouseTracking(true); // track mouse hover
50 myHistoryList->setSelectionMode(QAbstractItemView::ExtendedSelection);
51 connect(myHistoryList, SIGNAL(itemEntered(QListWidgetItem *)), this,
52 SLOT(setStackSelectedTo(QListWidgetItem *)));
53 connect(myHistoryList, SIGNAL(itemClicked(QListWidgetItem *)), this,
54 SLOT(onItemPressed(QListWidgetItem *)));
57 XGUI_HistoryMenu::~XGUI_HistoryMenu()
61 void XGUI_HistoryMenu::setHistory(const QList<ActionInfo>& theActions)
63 myHistoryList->clear();
64 foreach(ActionInfo anAct, theActions) {
65 QListWidgetItem* anItem = new QListWidgetItem(anAct.icon, anAct.text, myHistoryList);
69 void XGUI_HistoryMenu::leaveEvent(QEvent* theEvent)
71 setStackSelectedTo(NULL);
72 QMenu::leaveEvent(theEvent);
75 void XGUI_HistoryMenu::setStackSelectedTo(QListWidgetItem * theItem)
77 QListWidgetItem* eachItem = NULL;
78 bool isSelect = theItem != NULL;
79 for(int aRow = 0; aRow < myHistoryList->count(); ++aRow) {
80 eachItem = myHistoryList->item(aRow);
81 myHistoryList->setItemSelected(eachItem, isSelect);
82 // Deselect items below hovered
83 if (eachItem == theItem) {
87 // to avoid blinking caused by QMenu paint event (paints on top of the list)
88 myHistoryList->repaint();
91 void XGUI_HistoryMenu::onItemPressed(QListWidgetItem * theItem)
93 int selectedSize = myHistoryList->row(theItem) + 1;
94 emit actionSelected(selectedSize);