Salome HOME
Merge from 'master'
[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);
36   myLayout->setRowStretch(myNewRow, 0);
37   myNewRow++;
38 }
39
40 void XGUI_MenuGroupPanel::addWidget(QWidget* theWgt)
41 {
42   placeWidget(theWgt);
43 }
44
45 void XGUI_MenuGroupPanel::resizeEvent(QResizeEvent* theEvent)
46 {
47   QWidget::resizeEvent(theEvent);
48   if (myActions.size() == 0)
49     return;
50
51   int aH = theEvent->size().height();
52   int aMaxRow = (int) floor(double(aH / MIN_BUTTON_HEIGHT));
53   if (aMaxRow == myMaxRow)
54     return;
55
56   myMaxRow = aMaxRow;
57   myNewRow = 0;
58   myNewCol = 0;
59   foreach(QWidget* eachWidget, myActionWidget) {
60     placeWidget(eachWidget);
61   }
62   myLayout->setRowStretch(myMaxRow + 1,  1);
63 }
64
65 XGUI_Command* XGUI_MenuGroupPanel::addFeature(const QString& theId, const QString& theTitle,
66                                               const QString& theTip, const QIcon& theIcon,
67                                               const QKeySequence& theKeys, bool isCheckable)
68 {
69   XGUI_Command* aCommand = new XGUI_Command(theId, theIcon, theTitle, this, isCheckable);
70   aCommand->setToolTip(theTip);
71   if (!theKeys.isEmpty())
72     aCommand->setShortcut(theKeys);
73
74   addCommand(aCommand);
75   return aCommand;
76 }
77
78
79 XGUI_Command* XGUI_MenuGroupPanel::feature(const QString& theId) const
80 {
81   foreach (XGUI_Command* aCmd, myActions) {
82     if (aCmd->data().toString() == theId)
83       return aCmd;
84   }
85   return 0;
86 }