Salome HOME
Auto-formatting according to the defined code standard.
[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
11 XGUI_MenuGroupPanel::XGUI_MenuGroupPanel(QWidget *parent)
12     : QWidget(parent), myNewRow(0), myNewCol(0), myMaxRow(1)
13 {
14   myLayout = new QGridLayout(this);
15   myLayout->setSpacing(0);
16   myLayout->setMargin(0);
17   myLayout->setContentsMargins(0, 0, 0, 0);
18 }
19
20 void XGUI_MenuGroupPanel::addCommand(XGUI_Command* theAction)
21 {
22   myActions[theAction] = theAction->requestWidget(this);
23   addWidget(myActions[theAction]);
24 }
25
26 void XGUI_MenuGroupPanel::placeWidget(QWidget* theWgt)
27 {
28   if (myMaxRow == myNewRow) {
29     myNewRow = 0;
30     myNewCol++;
31   }
32   myLayout->addWidget(theWgt, myNewRow, myNewCol, Qt::AlignLeft);
33   myNewRow++;
34 }
35
36 void XGUI_MenuGroupPanel::addWidget(QWidget* theWgt)
37 {
38   placeWidget(theWgt);
39 }
40
41 void XGUI_MenuGroupPanel::resizeEvent(QResizeEvent* theEvent)
42 {
43   QWidget::resizeEvent(theEvent);
44   if (myActions.size() == 0)
45     return;
46
47   int aH = theEvent->size().height();
48   int aMaxRow = (int) floor(double(aH / MIN_BUTTON_HEIGHT));
49   if (aMaxRow == myMaxRow)
50     return;
51
52   myMaxRow = aMaxRow;
53   QListIterator<QWidget*> aIt(myActions.values());
54   myNewRow = 0;
55   myNewCol = 0;
56   while(aIt.hasNext()) {
57     placeWidget(aIt.next());
58   }
59 }
60
61 XGUI_Command* XGUI_MenuGroupPanel::addFeature(const QString& theId, const QString& theTitle,
62                                               const QString& theTip, const QIcon& theIcon,
63                                               const QKeySequence& theKeys)
64 {
65   XGUI_Command* aCommand = new XGUI_Command(theId, theIcon, theTitle, this);
66   aCommand->setToolTip(theTip);
67   if (!theKeys.isEmpty())
68     aCommand->setShortcut(theKeys);
69
70   addCommand(aCommand);
71   return aCommand;
72 }