Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / XGUI / XGUI_MenuGroupPanel.cpp
1 #include "XGUI_MenuGroupPanel.h"
2 #include "XGUI_Command.h"
3
4 #include <QLayout>
5 #include <QPushButton>
6 #include <QAction>
7 #include <QResizeEvent>
8
9 #include <math.h>
10 #include <iostream>
11
12 XGUI_MenuGroupPanel::XGUI_MenuGroupPanel(QWidget *parent)
13     : QWidget(parent), myNewRow(0), myNewCol(0), myMaxRow(1)
14 {
15   myLayout = new QGridLayout(this);
16   myLayout->setSpacing(0);
17   myLayout->setMargin(0);
18   myLayout->setContentsMargins(0, 0, 0, 0);
19 }
20
21 void XGUI_MenuGroupPanel::addCommand(XGUI_Command* theAction)
22 {
23   myActions.append(theAction);
24   QWidget* aWdg = theAction->requestWidget(this);
25   myActionWidget.append(aWdg);
26   addWidget(aWdg);
27 }
28
29 void XGUI_MenuGroupPanel::placeWidget(QWidget* theWgt)
30 {
31   if (myMaxRow == myNewRow) {
32     myNewRow = 0;
33     myNewCol++;
34   }
35   myLayout->addWidget(theWgt, myNewRow, myNewCol, Qt::AlignLeft);
36   myNewRow++;
37 }
38
39 void XGUI_MenuGroupPanel::addWidget(QWidget* theWgt)
40 {
41   placeWidget(theWgt);
42 }
43
44 void XGUI_MenuGroupPanel::resizeEvent(QResizeEvent* theEvent)
45 {
46   QWidget::resizeEvent(theEvent);
47   if (myActions.size() == 0)
48     return;
49
50   int aH = theEvent->size().height();
51   int aMaxRow = (int) floor(double(aH / MIN_BUTTON_HEIGHT));
52   if (aMaxRow == myMaxRow)
53     return;
54
55   myMaxRow = aMaxRow;
56   myNewRow = 0;
57   myNewCol = 0;
58   foreach(QWidget* eachWidget, myActionWidget) {
59     placeWidget(eachWidget);
60   }
61 }
62
63 XGUI_Command* XGUI_MenuGroupPanel::addFeature(const QString& theId, const QString& theTitle,
64                                               const QString& theTip, const QIcon& theIcon,
65                                               const QKeySequence& theKeys, bool isCheckable)
66 {
67   XGUI_Command* aCommand = new XGUI_Command(theId, theIcon, theTitle, this, isCheckable);
68   aCommand->setToolTip(theTip);
69   if (!theKeys.isEmpty())
70     aCommand->setShortcut(theKeys);
71
72   addCommand(aCommand);
73   return aCommand;
74 }
75
76
77 XGUI_Command* XGUI_MenuGroupPanel::feature(const QString& theId) const
78 {
79   QList<XGUI_Command*>::const_iterator aIt;
80   for (aIt = myActions.constBegin(); aIt != myActions.constEnd(); ++aIt)
81     if ((*aIt)->id() == theId)
82       return (*aIt);
83   return 0;
84 }