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