Salome HOME
updated copyright message
[modules/shaper.git] / src / XGUI / XGUI_HistoryMenu.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <XGUI_HistoryMenu.h>
21
22 #include <ModelAPI_Session.h>
23
24 #include <QListWidget>
25 #include <QWidgetAction>
26 #include <QToolButton>
27 #include <QAction>
28
29 XGUI_HistoryMenu::XGUI_HistoryMenu(QAction* theParent)
30  : QMenu(NULL),
31    myHistoryList(NULL)
32 {
33   theParent->setMenu(this);
34   initMenu();
35
36   connect(theParent, SIGNAL(destroyed()), this, SLOT(deleteLater()));
37 }
38
39 XGUI_HistoryMenu::XGUI_HistoryMenu(QToolButton* theParent)
40  : QMenu(theParent),
41    myHistoryList(NULL)
42 {
43   theParent->setMenu(this);
44   theParent->setPopupMode(QToolButton::MenuButtonPopup);
45
46   initMenu();
47 }
48
49 void XGUI_HistoryMenu::initMenu()
50 {
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 *)));
61 }
62
63 XGUI_HistoryMenu::~XGUI_HistoryMenu()
64 {
65 }
66
67 void XGUI_HistoryMenu::setHistory(const QList<ActionInfo>& theActions)
68 {
69   myHistoryList->clear();
70   foreach(ActionInfo anAct, theActions) {
71 #ifdef _DEBUG
72     QListWidgetItem* anItem =
73 #endif
74       new QListWidgetItem(anAct.icon, anAct.text, myHistoryList);
75   }
76 }
77
78 void XGUI_HistoryMenu::leaveEvent(QEvent* theEvent)
79 {
80   setStackSelectedTo(NULL);
81   QMenu::leaveEvent(theEvent);
82 }
83
84 void XGUI_HistoryMenu::setStackSelectedTo(QListWidgetItem * theItem)
85 {
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) {
93       isSelect = false;
94     }
95   }
96   // to avoid blinking caused by QMenu paint event (paints on top of the list)
97   myHistoryList->repaint();
98 }
99
100 void hideUpToMenuBar( QMenu* theMenu )
101 {
102   theMenu->hide();
103   foreach( QWidget* aWidget, theMenu->menuAction()->associatedWidgets() )
104   {
105     QMenu* aMenu = qobject_cast<QMenu*>( aWidget );
106     if( aMenu )
107     {
108       aMenu->hide();
109       hideUpToMenuBar( aMenu );
110     }
111   }
112 }
113
114 void XGUI_HistoryMenu::onItemPressed(QListWidgetItem * theItem)
115 {
116   int selectedSize = myHistoryList->row(theItem) + 1;
117   emit actionSelected(selectedSize);
118   hideUpToMenuBar( this );
119 }