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);
87 if (theId != "Default") {
90 myLayout->addStretch();
91 myGroups.append(aGroup);
96 * Searches for already created group with given name.
98 XGUI_MenuGroupPanel* XGUI_Workbench::findGroup(const QString& theId)
100 XGUI_MenuGroupPanel* aPanel;
101 foreach(aPanel, myGroups)
103 if (aPanel->objectName() == theId) {
110 void XGUI_Workbench::addSeparator()
112 QFrame* aLine = new QFrame(myChildWidget);
113 aLine->setFrameShape(QFrame::VLine);
114 aLine->setFrameShadow(QFrame::Sunken);
115 myLayout->addWidget(aLine);
118 void XGUI_Workbench::resizeEvent(QResizeEvent* theEvent)
120 QWidget::resizeEvent(theEvent);
121 QSize aSize = theEvent->size();
122 myLeftButton->setMinimumHeight(aSize.height() - 2);
123 myRightButton->setMinimumHeight(aSize.height() - 2);
125 QSize aS = myChildWidget->sizeHint();
126 int aW = myChildWidget->width();
128 myChildWidget->resize(aS.width(), myChildWidget->height());
130 myLeftButton->setVisible(isExceedsLeft());
131 myRightButton->setVisible(isExceedsRight());
134 void XGUI_Workbench::onLeftScroll()
136 if (!isExceedsLeft())
138 myChildWidget->move(myChildWidget->pos().x() + SCROLL_STEP, 0);
139 myLeftButton->setVisible(isExceedsLeft());
140 myRightButton->setVisible(isExceedsRight());
143 void XGUI_Workbench::onRightScroll()
145 if (!isExceedsRight())
147 myChildWidget->move(myChildWidget->pos().x() - SCROLL_STEP, 0);
148 myLeftButton->setVisible(isExceedsLeft());
149 myRightButton->setVisible(isExceedsRight());
152 bool XGUI_Workbench::isExceedsLeft()
154 QPoint aPos = myChildWidget->pos();
155 return (aPos.x() < 0);
158 bool XGUI_Workbench::isExceedsRight()
160 QPoint aPos = myChildWidget->pos();
161 int aVPWidth = myCommandsArea->viewport()->rect().width();
162 int aWgtWidth = myChildWidget->childrenRect().width();
163 return ((aVPWidth - aPos.x()) < aWgtWidth);
166 bool XGUI_Workbench::eventFilter(QObject *theObj, QEvent *theEvent)
168 if (theObj == myCommandsArea->viewport()) {
169 if (theEvent->type() == QEvent::Resize) {
170 myLeftButton->setVisible(isExceedsLeft());
171 myRightButton->setVisible(isExceedsRight());
174 return QWidget::eventFilter(theObj, theEvent);
177 XGUI_Command* XGUI_Workbench::feature(const QString& theId) const
179 QList<XGUI_MenuGroupPanel*>::const_iterator aIt;
180 for (aIt = myGroups.constBegin(); aIt != myGroups.constEnd(); ++aIt) {
181 XGUI_Command* aCmd = (*aIt)->feature(theId);
188 QList<XGUI_Command*> XGUI_Workbench::features() const
190 QList<XGUI_Command*> aList;
191 foreach (XGUI_MenuGroupPanel* aGroup, myGroups)
193 aList.append(aGroup->features());