Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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
54   myChildWidget = new QWidget(myCommandsArea);
55   myCommandsArea->setWidget(myChildWidget);
56   myCommandsArea->setAlignment(Qt::AlignLeft | Qt::AlignTop);
57   myCommandsArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
58   myCommandsArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
59
60   myLayout = new QHBoxLayout(myChildWidget);
61   myLayout->setSpacing(0);
62   myLayout->setMargin(0);
63   myLayout->setContentsMargins(0, 0, 0, 0);
64
65   myRightButton = new QPushButton(">", this);
66   myRightButton->setMaximumWidth(14);
67   myRightButton->setVisible(false);
68   connect(myRightButton, SIGNAL(clicked()), this, SLOT(onRightScroll()));
69   aMainLayout->addWidget(myRightButton);
70
71 }
72
73 /*
74  * Creates a new group in the workbench with given object name.
75  */
76 XGUI_MenuGroupPanel* XGUI_Workbench::addGroup(const QString& theId)
77 {
78   if (!myLayout->isEmpty()) {
79     int aNb = myLayout->count();
80     QLayoutItem* aItem = myLayout->itemAt(aNb - 1);
81     myLayout->removeItem(aItem);
82   }
83   XGUI_MenuGroupPanel* aGroup = new XGUI_MenuGroupPanel(myChildWidget);
84   aGroup->setObjectName(theId);
85   myLayout->addWidget(aGroup);
86   addSeparator();
87   myLayout->addStretch();
88   myGroups.append(aGroup);
89   return aGroup;
90 }
91
92 /*
93  * Searches for already created group with given name.
94  */
95 XGUI_MenuGroupPanel* XGUI_Workbench::findGroup(const QString& theId)
96 {
97   XGUI_MenuGroupPanel* aPanel;
98   foreach(aPanel, myGroups)
99   {
100     if (aPanel->objectName() == theId) {
101       return aPanel;
102     }
103   }
104   return NULL;
105 }
106
107 void XGUI_Workbench::addSeparator()
108 {
109   QFrame* aLine = new QFrame(myChildWidget);
110   aLine->setFrameShape(QFrame::VLine);
111   aLine->setFrameShadow(QFrame::Sunken);
112   myLayout->addWidget(aLine);
113 }
114
115 void XGUI_Workbench::resizeEvent(QResizeEvent* theEvent)
116 {
117   QWidget::resizeEvent(theEvent);
118   QSize aSize = theEvent->size();
119   myLeftButton->setMinimumHeight(aSize.height() - 2);
120   myRightButton->setMinimumHeight(aSize.height() - 2);
121
122   QSize aS = myChildWidget->sizeHint();
123   int aW = myChildWidget->width();
124   if (aW < aS.width())
125     myChildWidget->resize(aS.width(), myChildWidget->height());
126
127   myLeftButton->setVisible(isExceedsLeft());
128   myRightButton->setVisible(isExceedsRight());
129 }
130
131 void XGUI_Workbench::onLeftScroll()
132 {
133   if (!isExceedsLeft())
134     return;
135   myChildWidget->move(myChildWidget->pos().x() + SCROLL_STEP, 0);
136   myLeftButton->setVisible(isExceedsLeft());
137   myRightButton->setVisible(isExceedsRight());
138 }
139
140 void XGUI_Workbench::onRightScroll()
141 {
142   if (!isExceedsRight())
143     return;
144   myChildWidget->move(myChildWidget->pos().x() - SCROLL_STEP, 0);
145   myLeftButton->setVisible(isExceedsLeft());
146   myRightButton->setVisible(isExceedsRight());
147 }
148
149 bool XGUI_Workbench::isExceedsLeft()
150 {
151   QPoint aPos = myChildWidget->pos();
152   return (aPos.x() < 0);
153 }
154
155 bool XGUI_Workbench::isExceedsRight()
156 {
157   QPoint aPos = myChildWidget->pos();
158   int aVPWidth = myCommandsArea->viewport()->rect().width();
159   int aWgtWidth = myChildWidget->childrenRect().width();
160   return ((aVPWidth - aPos.x()) < aWgtWidth);
161 }