Salome HOME
61da43fa236f0fcb7f2ee8c7d3ed3bdf535fd31b
[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
9 #define SCROLL_STEP 20
10
11 //**************************************************
12 class CommandsArea : public QScrollArea
13 {
14 public:
15     CommandsArea(QWidget* theParent) : QScrollArea(theParent) {}
16
17 protected:
18     virtual void resizeEvent(QResizeEvent * theEvent);
19 };
20
21 void CommandsArea::resizeEvent(QResizeEvent* theEvent)
22 {
23     int x = widget()->x();
24     QScrollArea::resizeEvent(theEvent);
25     QRect aRect = widget()->childrenRect();
26     QSize aNewSize = theEvent->size();
27     if (aRect.width() > aNewSize.width())
28         aNewSize.setWidth(aRect.width() * 2);
29     widget()->resize(aNewSize);
30     widget()->move(x, 0);
31 }
32
33
34 //**************************************************
35 XGUI_Workbench::XGUI_Workbench(QWidget *theParent) :
36     QWidget(theParent)
37 {
38     setMinimumHeight(30);
39     QHBoxLayout* aMainLayout = new QHBoxLayout(this);
40     aMainLayout->setSpacing(0);
41     aMainLayout->setMargin(0);
42     aMainLayout->setContentsMargins(0,0,0,0);
43
44     myLeftButton = new QPushButton("<", this);
45     myLeftButton->setMaximumWidth(14);
46     myLeftButton->setVisible(false);
47     connect(myLeftButton,SIGNAL(clicked()), this, SLOT(onLeftScroll()));
48     aMainLayout->addWidget(myLeftButton);
49
50     myCommandsArea = new CommandsArea(this);
51     aMainLayout->addWidget(myCommandsArea);
52
53     myChildWidget = new QWidget(myCommandsArea);
54     myCommandsArea->setWidget(myChildWidget);
55     myCommandsArea->setAlignment(Qt::AlignLeft | Qt::AlignTop);
56     myCommandsArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
57     myCommandsArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
58
59     myLayout = new QHBoxLayout(myChildWidget);
60     myLayout->setSpacing(0);
61     myLayout->setMargin(0);
62     myLayout->setContentsMargins(0,0,0,0);
63
64     myRightButton = new QPushButton(">", this);
65     myRightButton->setMaximumWidth(14);
66     myRightButton->setVisible(false);
67     connect(myRightButton,SIGNAL(clicked()), this, SLOT(onRightScroll()));
68     aMainLayout->addWidget(myRightButton);
69
70 }
71
72 /*
73  * Creates a new group in the workbench with given object name.
74  */
75 XGUI_MenuGroupPanel* XGUI_Workbench::addGroup(const QString& theId)
76 {
77     if (!myLayout->isEmpty()) {
78         int aNb = myLayout->count();
79         QLayoutItem* aItem = myLayout->itemAt(aNb - 1);
80         myLayout->removeItem(aItem);
81     }
82     XGUI_MenuGroupPanel* aGroup = new XGUI_MenuGroupPanel(myChildWidget);
83     aGroup->setObjectName(theId);
84     myLayout->addWidget(aGroup);
85     addSeparator();
86     myLayout->addStretch();
87     myGroups.append(aGroup);
88     return aGroup;
89 }
90
91 /*
92  * Searches for already created group with given name.
93  */
94 XGUI_MenuGroupPanel* XGUI_Workbench::findGroup(const QString& theId)
95 {
96   XGUI_MenuGroupPanel* aPanel;
97   foreach(aPanel, myGroups) {
98     if(aPanel->objectName() == theId) {
99       return aPanel;
100     }
101   }
102   return NULL;
103 }
104
105
106 void XGUI_Workbench::addSeparator()
107 {
108     QFrame* aLine = new QFrame(myChildWidget);
109     aLine->setFrameShape(QFrame::VLine);
110     aLine->setFrameShadow(QFrame::Sunken);
111     myLayout->addWidget(aLine);
112 }
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
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
142 void XGUI_Workbench::onRightScroll()
143 {
144     if (!isExceedsRight())
145                 return;
146     myChildWidget->move( myChildWidget->pos().x() - SCROLL_STEP, 0 );
147     myLeftButton->setVisible(isExceedsLeft());
148     myRightButton->setVisible(isExceedsRight());
149 }
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 }