1 #include "XGUI_Workbench.h"
2 #include "XGUI_MenuGroupPanel.h"
5 #include <QResizeEvent>
10 //**************************************************
11 class CommandsArea: public QScrollArea
14 CommandsArea(QWidget* theParent)
15 : QScrollArea(theParent)
20 virtual void resizeEvent(QResizeEvent * theEvent);
23 void CommandsArea::resizeEvent(QResizeEvent* theEvent)
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);
35 //**************************************************
36 XGUI_Workbench::XGUI_Workbench(QWidget *theParent)
40 QHBoxLayout* aMainLayout = new QHBoxLayout(this);
41 aMainLayout->setSpacing(0);
42 aMainLayout->setMargin(0);
43 aMainLayout->setContentsMargins(0, 0, 0, 0);
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);
51 myCommandsArea = new CommandsArea(this);
52 aMainLayout->addWidget(myCommandsArea);
53 myCommandsArea->viewport()->installEventFilter(this);
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);
61 myLayout = new QHBoxLayout(myChildWidget);
62 myLayout->setSpacing(0);
63 myLayout->setMargin(0);
64 myLayout->setContentsMargins(0, 0, 0, 0);
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);
75 * Creates a new group in the workbench with given object name.
77 XGUI_MenuGroupPanel* XGUI_Workbench::addGroup(const QString& theId)
79 if (!myLayout->isEmpty()) {
80 int aNb = myLayout->count();
81 QLayoutItem* aItem = myLayout->itemAt(aNb - 1);
82 myLayout->removeItem(aItem);
84 XGUI_MenuGroupPanel* aGroup = new XGUI_MenuGroupPanel(myChildWidget);
85 aGroup->setObjectName(theId);
86 myLayout->addWidget(aGroup);
88 myLayout->addStretch();
89 myGroups.append(aGroup);
94 * Searches for already created group with given name.
96 XGUI_MenuGroupPanel* XGUI_Workbench::findGroup(const QString& theId)
98 XGUI_MenuGroupPanel* aPanel;
99 foreach(aPanel, myGroups)
101 if (aPanel->objectName() == theId) {
108 void XGUI_Workbench::addSeparator()
110 QFrame* aLine = new QFrame(myChildWidget);
111 aLine->setFrameShape(QFrame::VLine);
112 aLine->setFrameShadow(QFrame::Sunken);
113 myLayout->addWidget(aLine);
116 void XGUI_Workbench::resizeEvent(QResizeEvent* theEvent)
118 QWidget::resizeEvent(theEvent);
119 QSize aSize = theEvent->size();
120 myLeftButton->setMinimumHeight(aSize.height() - 2);
121 myRightButton->setMinimumHeight(aSize.height() - 2);
123 QSize aS = myChildWidget->sizeHint();
124 int aW = myChildWidget->width();
126 myChildWidget->resize(aS.width(), myChildWidget->height());
128 myLeftButton->setVisible(isExceedsLeft());
129 myRightButton->setVisible(isExceedsRight());
132 void XGUI_Workbench::onLeftScroll()
134 if (!isExceedsLeft())
136 myChildWidget->move(myChildWidget->pos().x() + SCROLL_STEP, 0);
137 myLeftButton->setVisible(isExceedsLeft());
138 myRightButton->setVisible(isExceedsRight());
141 void XGUI_Workbench::onRightScroll()
143 if (!isExceedsRight())
145 myChildWidget->move(myChildWidget->pos().x() - SCROLL_STEP, 0);
146 myLeftButton->setVisible(isExceedsLeft());
147 myRightButton->setVisible(isExceedsRight());
150 bool XGUI_Workbench::isExceedsLeft()
152 QPoint aPos = myChildWidget->pos();
153 return (aPos.x() < 0);
156 bool XGUI_Workbench::isExceedsRight()
158 QPoint aPos = myChildWidget->pos();
159 int aVPWidth = myCommandsArea->viewport()->rect().width();
160 int aWgtWidth = myChildWidget->childrenRect().width();
161 return ((aVPWidth - aPos.x()) < aWgtWidth);
164 bool XGUI_Workbench::eventFilter(QObject *theObj, QEvent *theEvent)
166 if (theObj == myCommandsArea->viewport()) {
167 if (theEvent->type() == QEvent::Resize) {
168 myLeftButton->setVisible(isExceedsLeft());
169 myRightButton->setVisible(isExceedsRight());
172 return QWidget::eventFilter(theObj, theEvent);
175 XGUI_Command* XGUI_Workbench::feature(const QString& theId) const
177 QList<XGUI_MenuGroupPanel*>::const_iterator aIt;
178 for (aIt = myGroups.constBegin(); aIt != myGroups.constEnd(); ++aIt) {
179 XGUI_Command* aCmd = (*aIt)->feature(theId);
186 QList<XGUI_Command*> XGUI_Workbench::features() const
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());