Salome HOME
"GUI" stubs for testing of CMake building procedures, preliminary examples of GUI
[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     QScrollArea::resizeEvent(theEvent);
24     QRect aRect = widget()->childrenRect();
25     QSize aNewSize = theEvent->size();
26     if (aRect.width() > aNewSize.width())
27         aNewSize.setWidth(aRect.width() * 2);
28     widget()->resize(aNewSize);
29 }
30
31
32 //**************************************************
33 XGUI_Workbench::XGUI_Workbench(QWidget *theParent) :
34     QWidget(theParent)
35 {
36     setMinimumHeight(30);
37     QHBoxLayout* aMainLayout = new QHBoxLayout(this);
38     aMainLayout->setSpacing(0);
39     aMainLayout->setMargin(0);
40     aMainLayout->setContentsMargins(0,0,0,0);
41
42     myLeftButton = new QPushButton("<", this);
43     myLeftButton->setMaximumWidth(14);
44     //myLeftButton->setEnabled(false);
45     myLeftButton->setVisible(false);
46     connect(myLeftButton,SIGNAL(clicked()), this, SLOT(onLeftScroll()));
47     aMainLayout->addWidget(myLeftButton);
48
49     myCommandsArea = new CommandsArea(this);
50     aMainLayout->addWidget(myCommandsArea);
51
52     myChildWidget = new QWidget(myCommandsArea);
53     myCommandsArea->setWidget(myChildWidget);
54     myCommandsArea->setAlignment(Qt::AlignLeft | Qt::AlignTop);
55     myCommandsArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
56     myCommandsArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
57
58     myLayout = new QHBoxLayout(myChildWidget);
59     myLayout->setSpacing(0);
60     myLayout->setMargin(0);
61     myLayout->setContentsMargins(0,0,0,0);
62
63     myRightButton = new QPushButton(">", this);
64     myRightButton->setMaximumWidth(14);
65     //myRightButton->setEnabled(false);
66     myRightButton->setVisible(false);
67     connect(myRightButton,SIGNAL(clicked()), this, SLOT(onRightScroll()));
68     aMainLayout->addWidget(myRightButton);
69
70 }
71
72 int XGUI_Workbench::addGroup()
73 {
74     if (!myLayout->isEmpty()) {
75         int aNb = myLayout->count();
76         QLayoutItem* aItem = myLayout->itemAt(aNb - 1);
77         myLayout->removeItem(aItem);
78     }
79     XGUI_MenuGroupPanel* aGroup = new XGUI_MenuGroupPanel(myChildWidget);
80     myLayout->addWidget(aGroup);
81     addSeparator();
82     myLayout->addStretch();
83     myGroups.append(aGroup);
84     return myGroups.size() - 1;
85 }
86
87 void XGUI_Workbench::addSeparator()
88 {
89     QFrame* aLine = new QFrame(myChildWidget);
90     aLine->setFrameShape(QFrame::VLine);
91     aLine->setFrameShadow(QFrame::Sunken);
92     myLayout->addWidget(aLine);
93 }
94
95 void XGUI_Workbench::addCommand(int theGroupId, XGUI_Command* theCommand)
96 {
97     XGUI_MenuGroupPanel* aGroup = myGroups.at(theGroupId);
98     aGroup->addCommand(theCommand);
99 }
100
101
102 void XGUI_Workbench::resizeEvent(QResizeEvent* theEvent)
103 {
104     QWidget::resizeEvent(theEvent);
105     QSize aSize = theEvent->size();
106     myLeftButton->setMinimumHeight(aSize.height() - 2);
107     myRightButton->setMinimumHeight(aSize.height() - 2);
108
109     QSize aS = myChildWidget->sizeHint();
110         int aW = myChildWidget->width();
111         if (aW < aS.width())
112                 myChildWidget->resize(aS.width(), myChildWidget->height());
113
114     //myLeftButton->setEnabled(isExceedsLeft());
115     //myRightButton->setEnabled(isExceedsRight());
116     myLeftButton->setVisible(isExceedsLeft());
117     myRightButton->setVisible(isExceedsRight());
118 }
119
120
121 void XGUI_Workbench::onLeftScroll()
122 {
123     if (!isExceedsLeft())
124                 return;
125     myChildWidget->move( myChildWidget->pos().x() + SCROLL_STEP, 0 );
126     myLeftButton->setVisible(isExceedsLeft());
127     myRightButton->setVisible(isExceedsRight());
128     //myLeftButton->setEnabled(isExceedsLeft());
129     //myRightButton->setEnabled(isExceedsRight());
130 }
131
132
133 void XGUI_Workbench::onRightScroll()
134 {
135     if (!isExceedsRight())
136                 return;
137     myChildWidget->move( myChildWidget->pos().x() - SCROLL_STEP, 0 );
138     //myLeftButton->setEnabled(isExceedsLeft());
139     //myRightButton->setEnabled(isExceedsRight());
140     myLeftButton->setVisible(isExceedsLeft());
141     myRightButton->setVisible(isExceedsRight());
142 }
143
144
145 bool XGUI_Workbench::isExceedsLeft()
146 {
147     QPoint aPos = myChildWidget->pos();
148     return (aPos.x() < 0);
149 }
150
151 bool XGUI_Workbench::isExceedsRight()
152 {
153     QPoint aPos = myChildWidget->pos();
154     int aVPWidth = myCommandsArea->viewport()->rect().width();
155     int aWgtWidth = myChildWidget->childrenRect().width();
156     return ((aVPWidth - aPos.x()) < aWgtWidth);
157 }