]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Workbench.cpp
Salome HOME
Merge branch 'master' into BR_PYTHON_PLUGIN
[modules/shaper.git] / src / XGUI / XGUI_Workbench.cpp
1 #include "XGUI_Workbench.h"
2 #include "XGUI_MenuGroupPanel.h"
3
4 #include <QLayout>
5 #include <QResizeEvent>
6 #include <QPushButton>
7
8 #define SCROLL_STEP 20
9
10 //**************************************************
11 class CommandsArea : public QScrollArea
12 {
13  public:
14   CommandsArea(QWidget* theParent)
15       : QScrollArea(theParent)
16   {
17   }
18
19  protected:
20   virtual void resizeEvent(QResizeEvent * theEvent);
21 };
22
23 void CommandsArea::resizeEvent(QResizeEvent* theEvent)
24 {
25   int x = widget()->x();
26   QScrollArea::resizeEvent(theEvent);
27   QRect aRect = widget()->childrenRect();
28   QSize aNewSize = theEvent->size();
29   if (aRect.width() > aNewSize.width())
30     aNewSize.setWidth(aRect.width() * 2);
31   widget()->resize(aNewSize);
32   widget()->move(x, 0);
33 }
34
35 //**************************************************
36 XGUI_Workbench::XGUI_Workbench(QWidget *theParent)
37     : QWidget(theParent)
38 {
39   setMinimumHeight(30);
40   QHBoxLayout* aMainLayout = new QHBoxLayout(this);
41   aMainLayout->setSpacing(0);
42   aMainLayout->setMargin(0);
43   aMainLayout->setContentsMargins(0, 0, 0, 0);
44
45   myLeftButton = new QPushButton("<", this);
46   myLeftButton->setMaximumWidth(14);
47   myLeftButton->setVisible(false);
48   connect(myLeftButton, SIGNAL(clicked()), this, SLOT(onLeftScroll()));
49   aMainLayout->addWidget(myLeftButton);
50
51   myCommandsArea = new CommandsArea(this);
52   aMainLayout->addWidget(myCommandsArea);
53   myCommandsArea->viewport()->installEventFilter(this);
54
55   myChildWidget = new QWidget(myCommandsArea);
56   myCommandsArea->setWidget(myChildWidget);
57   myCommandsArea->setAlignment(Qt::AlignLeft | Qt::AlignTop);
58   myCommandsArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
59   myCommandsArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
60
61   myLayout = new QHBoxLayout(myChildWidget);
62   myLayout->setSpacing(0);
63   myLayout->setMargin(0);
64   myLayout->setContentsMargins(0, 0, 0, 0);
65
66   myRightButton = new QPushButton(">", this);
67   myRightButton->setMaximumWidth(14);
68   myRightButton->setVisible(false);
69   connect(myRightButton, SIGNAL(clicked()), this, SLOT(onRightScroll()));
70   aMainLayout->addWidget(myRightButton);
71
72 }
73
74 /*
75  * Creates a new group in the workbench with given object name.
76  */
77 XGUI_MenuGroupPanel* XGUI_Workbench::addGroup(const QString& theId)
78 {
79   if (!myLayout->isEmpty()) {
80     int aNb = myLayout->count();
81     QLayoutItem* aItem = myLayout->itemAt(aNb - 1);
82     myLayout->removeItem(aItem);
83   }
84   XGUI_MenuGroupPanel* aGroup = new XGUI_MenuGroupPanel(myChildWidget);
85   aGroup->setObjectName(theId);
86   myLayout->addWidget(aGroup);
87   if (theId != "Default") {
88     addSeparator();
89   }
90   myLayout->addStretch();
91   myGroups.append(aGroup);
92   return aGroup;
93 }
94
95 /*
96  * Searches for already created group with given name.
97  */
98 XGUI_MenuGroupPanel* XGUI_Workbench::findGroup(const QString& theId)
99 {
100   XGUI_MenuGroupPanel* aPanel;
101   foreach(aPanel, myGroups)
102   {
103     if (aPanel->objectName() == theId) {
104       return aPanel;
105     }
106   }
107   return NULL;
108 }
109
110 void XGUI_Workbench::addSeparator()
111 {
112   QFrame* aLine = new QFrame(myChildWidget);
113   aLine->setFrameShape(QFrame::VLine);
114   aLine->setFrameShadow(QFrame::Sunken);
115   myLayout->addWidget(aLine);
116 }
117
118 void XGUI_Workbench::resizeEvent(QResizeEvent* theEvent)
119 {
120   QWidget::resizeEvent(theEvent);
121   QSize aSize = theEvent->size();
122   myLeftButton->setMinimumHeight(aSize.height() - 2);
123   myRightButton->setMinimumHeight(aSize.height() - 2);
124
125   QSize aS = myChildWidget->sizeHint();
126   int aW = myChildWidget->width();
127   if (aW < aS.width())
128     myChildWidget->resize(aS.width(), myChildWidget->height());
129
130   myLeftButton->setVisible(isExceedsLeft());
131   myRightButton->setVisible(isExceedsRight());
132 }
133
134 void XGUI_Workbench::onLeftScroll()
135 {
136   if (!isExceedsLeft())
137     return;
138   myChildWidget->move(myChildWidget->pos().x() + SCROLL_STEP, 0);
139   myLeftButton->setVisible(isExceedsLeft());
140   myRightButton->setVisible(isExceedsRight());
141 }
142
143 void XGUI_Workbench::onRightScroll()
144 {
145   if (!isExceedsRight())
146     return;
147   myChildWidget->move(myChildWidget->pos().x() - SCROLL_STEP, 0);
148   myLeftButton->setVisible(isExceedsLeft());
149   myRightButton->setVisible(isExceedsRight());
150 }
151
152 bool XGUI_Workbench::isExceedsLeft()
153 {
154   QPoint aPos = myChildWidget->pos();
155   return (aPos.x() < 0);
156 }
157
158 bool XGUI_Workbench::isExceedsRight()
159 {
160   QPoint aPos = myChildWidget->pos();
161   int aVPWidth = myCommandsArea->viewport()->rect().width();
162   int aWgtWidth = myChildWidget->childrenRect().width();
163   return ((aVPWidth - aPos.x()) < aWgtWidth);
164 }
165
166 bool XGUI_Workbench::eventFilter(QObject *theObj, QEvent *theEvent)
167 {
168   if (theObj == myCommandsArea->viewport()) {
169     if (theEvent->type() == QEvent::Resize) {
170       myLeftButton->setVisible(isExceedsLeft());
171       myRightButton->setVisible(isExceedsRight());
172     }
173   }
174   return QWidget::eventFilter(theObj, theEvent);
175 }
176
177 XGUI_Command* XGUI_Workbench::feature(const QString& theId) const
178 {
179   QList<XGUI_MenuGroupPanel*>::const_iterator aIt;
180   for (aIt = myGroups.constBegin(); aIt != myGroups.constEnd(); ++aIt) {
181     XGUI_Command* aCmd = (*aIt)->feature(theId);
182     if (aCmd)
183       return aCmd;
184   }
185   return 0;
186 }
187
188 QList<XGUI_Command*> XGUI_Workbench::features() const
189 {
190   QList<XGUI_Command*> aList;
191   foreach (XGUI_MenuGroupPanel* aGroup, myGroups)
192   {
193     aList.append(aGroup->features());
194   }
195   return aList;
196 }