]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Workbench.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom
[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   addSeparator();
88   myLayout->addStretch();
89   myGroups.append(aGroup);
90   return aGroup;
91 }
92
93 /*
94  * Searches for already created group with given name.
95  */
96 XGUI_MenuGroupPanel* XGUI_Workbench::findGroup(const QString& theId)
97 {
98   XGUI_MenuGroupPanel* aPanel;
99   foreach(aPanel, myGroups)
100   {
101     if (aPanel->objectName() == theId) {
102       return aPanel;
103     }
104   }
105   return NULL;
106 }
107
108 void XGUI_Workbench::addSeparator()
109 {
110   QFrame* aLine = new QFrame(myChildWidget);
111   aLine->setFrameShape(QFrame::VLine);
112   aLine->setFrameShadow(QFrame::Sunken);
113   myLayout->addWidget(aLine);
114 }
115
116 void XGUI_Workbench::resizeEvent(QResizeEvent* theEvent)
117 {
118   QWidget::resizeEvent(theEvent);
119   QSize aSize = theEvent->size();
120   myLeftButton->setMinimumHeight(aSize.height() - 2);
121   myRightButton->setMinimumHeight(aSize.height() - 2);
122
123   QSize aS = myChildWidget->sizeHint();
124   int aW = myChildWidget->width();
125   if (aW < aS.width())
126     myChildWidget->resize(aS.width(), myChildWidget->height());
127
128   myLeftButton->setVisible(isExceedsLeft());
129   myRightButton->setVisible(isExceedsRight());
130 }
131
132 void XGUI_Workbench::onLeftScroll()
133 {
134   if (!isExceedsLeft())
135     return;
136   myChildWidget->move(myChildWidget->pos().x() + SCROLL_STEP, 0);
137   myLeftButton->setVisible(isExceedsLeft());
138   myRightButton->setVisible(isExceedsRight());
139 }
140
141 void XGUI_Workbench::onRightScroll()
142 {
143   if (!isExceedsRight())
144     return;
145   myChildWidget->move(myChildWidget->pos().x() - SCROLL_STEP, 0);
146   myLeftButton->setVisible(isExceedsLeft());
147   myRightButton->setVisible(isExceedsRight());
148 }
149
150 bool XGUI_Workbench::isExceedsLeft()
151 {
152   QPoint aPos = myChildWidget->pos();
153   return (aPos.x() < 0);
154 }
155
156 bool XGUI_Workbench::isExceedsRight()
157 {
158   QPoint aPos = myChildWidget->pos();
159   int aVPWidth = myCommandsArea->viewport()->rect().width();
160   int aWgtWidth = myChildWidget->childrenRect().width();
161   return ((aVPWidth - aPos.x()) < aWgtWidth);
162 }
163
164 bool XGUI_Workbench::eventFilter(QObject *theObj, QEvent *theEvent)
165 {
166   if (theObj == myCommandsArea->viewport()) {
167     if (theEvent->type() == QEvent::Resize) {
168       myLeftButton->setVisible(isExceedsLeft());
169       myRightButton->setVisible(isExceedsRight());
170     }
171   }
172   return QWidget::eventFilter(theObj, theEvent);
173 }
174
175 XGUI_Command* XGUI_Workbench::feature(const QString& theId) const
176 {
177   QList<XGUI_MenuGroupPanel*>::const_iterator aIt;
178   for (aIt = myGroups.constBegin(); aIt != myGroups.constEnd(); ++aIt) {
179     XGUI_Command* aCmd = (*aIt)->feature(theId);
180     if (aCmd)
181       return aCmd;
182   }
183   return 0;
184 }
185
186 QList<XGUI_Command*> XGUI_Workbench::features() const
187 {
188   QList<XGUI_Command*> aList;
189   QList<XGUI_MenuGroupPanel*>::const_iterator aIt;
190   for (aIt = myGroups.constBegin(); aIt != myGroups.constEnd(); ++aIt) 
191     aList.append((*aIt)->features());
192   return aList;
193 }