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