Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include <ModuleBase_ToolBox.h>
21 #include <ModuleBase_ModelWidget.h>
22
23 #include <QButtonGroup>
24 #include <QStackedWidget>
25 #include <QHBoxLayout>
26 #include <QVBoxLayout>
27 #include <QToolButton>
28
29 #include <ModuleBase_PagedContainer.h>
30
31 ModuleBase_ToolBox::ModuleBase_ToolBox(QWidget* theParent, const bool theUseFrameStyleBox)
32 : QFrame(theParent)
33 {
34   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
35   aMainLayout->setMargin(0);
36   aMainLayout->setSpacing(2);
37
38   if (theUseFrameStyleBox) {
39     setFrameStyle(QFrame::Box | QFrame::Raised);
40     aMainLayout->setMargin(2);
41   }
42
43   myButtonsFrame = new QFrame(this);
44
45   myStack = new QStackedWidget(this);
46
47   aMainLayout->addWidget(myButtonsFrame, 0);
48   aMainLayout->addWidget(myStack, 1);
49
50   myButtonsGroup = new QButtonGroup(this);
51   myButtonsGroup->setExclusive(true);
52   myButtonsLayout = new QHBoxLayout(myButtonsFrame);
53   myButtonsLayout->setMargin(0);
54   myButtonsLayout->setSpacing(5);
55   myButtonsLayout->addStretch(1);
56
57   connect(myStack, SIGNAL(currentChanged(int)), this, SIGNAL(currentChanged(int)));
58   connect(myButtonsGroup, SIGNAL(buttonPressed(int)), this, SLOT(onButton(int)));
59 }
60
61 ModuleBase_ToolBox::~ModuleBase_ToolBox()
62 {
63 }
64
65 void ModuleBase_ToolBox::addItem(QWidget* thePage, const QString& theName, const QPixmap& theIcon)
66 {
67   int anOldCount = myStack->count();
68
69   myStack->addWidget(thePage);
70
71   QToolButton* aButton = new QToolButton(myButtonsFrame);
72   aButton->setFocusPolicy(Qt::StrongFocus);
73   aButton->setCheckable(true);
74   aButton->setIcon(theIcon);
75   aButton->setIconSize(theIcon.size());
76   aButton->setToolTip(theName);
77   aButton->setObjectName(theName);
78   myButtonsGroup->addButton(aButton, anOldCount);
79   myButtonsLayout->insertWidget(anOldCount, aButton);
80 }
81
82 int ModuleBase_ToolBox::count() const
83 {
84   return myStack->count();
85 }
86
87 int ModuleBase_ToolBox::currentIndex() const
88 {
89   return myStack->currentIndex();
90 }
91
92 void ModuleBase_ToolBox::setCurrentIndex(const int theIndex)
93 {
94   myStack->setCurrentIndex(theIndex);
95   myButtonsGroup->button(theIndex)->setChecked(true);
96 }
97
98 void ModuleBase_ToolBox::onButton(int theIndex)
99 {
100   myStack->setCurrentIndex(theIndex);
101 }
102
103 bool ModuleBase_ToolBox::isOffToolBoxParent(ModuleBase_ModelWidget* theWidget)
104 {
105   bool isOffToolBox = false;
106
107   QList<QWidget*> aControls = theWidget->getControls();
108   if (aControls.size() > 0) {
109     QWidget* aFirstControl = aControls.first();
110
111     QWidget* aWidget = aFirstControl;
112     QWidget* aParent = (QWidget*)aFirstControl->parent();
113     while (aParent) {
114       QStackedWidget* aStackedWidget = dynamic_cast<QStackedWidget*>(aParent);
115       if (aStackedWidget) {
116         int anIndex = aStackedWidget->currentIndex();
117         isOffToolBox = aStackedWidget->currentWidget() != aWidget;
118         break;
119       }
120       aWidget = aParent;
121       aParent = (QWidget*)aWidget->parent();
122     }
123   }
124   return isOffToolBox;
125 }