Salome HOME
Merge remote-tracking branch 'origin/cbr/export_to_geom_via_xao'
[modules/shaper.git] / src / ModuleBase / ModuleBase_ToolBox.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <ModuleBase_ToolBox.h>
22 #include <ModuleBase_ModelWidget.h>
23
24 #include <QButtonGroup>
25 #include <QStackedWidget>
26 #include <QHBoxLayout>
27 #include <QVBoxLayout>
28 #include <QToolButton>
29
30 #include <ModuleBase_PagedContainer.h>
31
32 ModuleBase_ToolBox::ModuleBase_ToolBox(QWidget* theParent, const bool theUseFrameStyleBox)
33 : QFrame(theParent)
34 {
35   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
36   aMainLayout->setMargin(0);
37   aMainLayout->setSpacing(2);
38
39   if (theUseFrameStyleBox) {
40     setFrameStyle(QFrame::Box | QFrame::Raised);
41     aMainLayout->setMargin(2);
42   }
43
44   myButtonsFrame = new QFrame(this);
45
46   myStack = new QStackedWidget(this);
47
48   aMainLayout->addWidget(myButtonsFrame, 0);
49   aMainLayout->addWidget(myStack, 1);
50
51   myButtonsGroup = new QButtonGroup(this);
52   myButtonsGroup->setExclusive(true);
53   myButtonsLayout = new QHBoxLayout(myButtonsFrame);
54   myButtonsLayout->setMargin(0);
55   myButtonsLayout->setSpacing(5);
56   myButtonsLayout->addStretch(1);
57
58   connect(myStack, SIGNAL(currentChanged(int)), this, SIGNAL(currentChanged(int)));
59   connect(myButtonsGroup, SIGNAL(buttonPressed(int)), this, SLOT(onButton(int)));
60 }
61
62 ModuleBase_ToolBox::~ModuleBase_ToolBox()
63 {
64 }
65
66 void ModuleBase_ToolBox::addItem(QWidget* thePage, const QString& theName, const QPixmap& theIcon)
67 {
68   int anOldCount = myStack->count();
69
70   myStack->addWidget(thePage);
71
72   QToolButton* aButton = new QToolButton(myButtonsFrame);
73   aButton->setFocusPolicy(Qt::StrongFocus);
74   aButton->setCheckable(true);
75   if (theIcon.isNull())
76     aButton->setText(theName);
77   else {
78     aButton->setIcon(theIcon);
79     aButton->setIconSize(theIcon.size());
80   }
81   aButton->setToolTip(theName);
82   aButton->setObjectName(theName);
83   myButtonsGroup->addButton(aButton, anOldCount);
84   myButtonsLayout->insertWidget(anOldCount, aButton);
85 }
86
87 int ModuleBase_ToolBox::count() const
88 {
89   return myStack->count();
90 }
91
92 int ModuleBase_ToolBox::currentIndex() const
93 {
94   return myStack->currentIndex();
95 }
96
97 void ModuleBase_ToolBox::setCurrentIndex(const int theIndex)
98 {
99   myStack->setCurrentIndex(theIndex);
100   myButtonsGroup->button(theIndex)->setChecked(true);
101 }
102
103 void ModuleBase_ToolBox::onButton(int theIndex)
104 {
105   myStack->setCurrentIndex(theIndex);
106 }
107
108 bool ModuleBase_ToolBox::isOffToolBoxParent(ModuleBase_ModelWidget* theWidget)
109 {
110   bool isOffToolBox = false;
111
112   QList<QWidget*> aControls = theWidget->getControls();
113   if (aControls.size() > 0) {
114     QWidget* aFirstControl = aControls.first();
115
116     QWidget* aWidget = aFirstControl;
117     QWidget* aParent = (QWidget*)aFirstControl->parent();
118     while (aParent) {
119       QStackedWidget* aStackedWidget = dynamic_cast<QStackedWidget*>(aParent);
120       if (aStackedWidget) {
121         int anIndex = aStackedWidget->currentIndex();
122         isOffToolBox = aStackedWidget->currentWidget() != aWidget;
123         break;
124       }
125       aWidget = aParent;
126       aParent = (QWidget*)aWidget->parent();
127     }
128   }
129   return isOffToolBox;
130 }