]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Workbench.cpp
Salome HOME
3cecc44b9cb091fa5187392bdf9fda7dd502620e
[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->setEnabled(false);
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 int XGUI_Workbench::addGroup()
74 {
75     if (!myLayout->isEmpty()) {
76         int aNb = myLayout->count();
77         QLayoutItem* aItem = myLayout->itemAt(aNb - 1);
78         myLayout->removeItem(aItem);
79     }
80     XGUI_MenuGroupPanel* aGroup = new XGUI_MenuGroupPanel(myChildWidget);
81     myLayout->addWidget(aGroup);
82     addSeparator();
83     myLayout->addStretch();
84     myGroups.append(aGroup);
85     return myGroups.size() - 1;
86 }
87
88 void XGUI_Workbench::addSeparator()
89 {
90     QFrame* aLine = new QFrame(myChildWidget);
91     aLine->setFrameShape(QFrame::VLine);
92     aLine->setFrameShadow(QFrame::Sunken);
93     myLayout->addWidget(aLine);
94 }
95
96 void XGUI_Workbench::addCommand(int theGroupId, XGUI_Command* theCommand)
97 {
98     XGUI_MenuGroupPanel* aGroup = myGroups.at(theGroupId);
99     aGroup->addCommand(theCommand);
100 }
101
102
103 void XGUI_Workbench::resizeEvent(QResizeEvent* theEvent)
104 {
105     QWidget::resizeEvent(theEvent);
106     QSize aSize = theEvent->size();
107     myLeftButton->setMinimumHeight(aSize.height() - 2);
108     myRightButton->setMinimumHeight(aSize.height() - 2);
109
110     QSize aS = myChildWidget->sizeHint();
111         int aW = myChildWidget->width();
112         if (aW < aS.width())
113                 myChildWidget->resize(aS.width(), myChildWidget->height());
114
115     myLeftButton->setVisible(isExceedsLeft());
116     myRightButton->setVisible(isExceedsRight());
117 }
118
119
120 void XGUI_Workbench::onLeftScroll()
121 {
122     if (!isExceedsLeft())
123                 return;
124     myChildWidget->move( myChildWidget->pos().x() + SCROLL_STEP, 0 );
125     myLeftButton->setVisible(isExceedsLeft());
126     myRightButton->setVisible(isExceedsRight());
127 }
128
129
130 void XGUI_Workbench::onRightScroll()
131 {
132     if (!isExceedsRight())
133                 return;
134     myChildWidget->move( myChildWidget->pos().x() - SCROLL_STEP, 0 );
135     myLeftButton->setVisible(isExceedsLeft());
136     myRightButton->setVisible(isExceedsRight());
137 }
138
139
140 bool XGUI_Workbench::isExceedsLeft()
141 {
142     QPoint aPos = myChildWidget->pos();
143     return (aPos.x() < 0);
144 }
145
146 bool XGUI_Workbench::isExceedsRight()
147 {
148     QPoint aPos = myChildWidget->pos();
149     int aVPWidth = myCommandsArea->viewport()->rect().width();
150     int aWgtWidth = myChildWidget->childrenRect().width();
151     return ((aVPWidth - aPos.x()) < aWgtWidth);
152 }