Salome HOME
f8aa4abc72b559a058b48052174ba57e327bbccf
[modules/shaper.git] / src / ModuleBase / ModuleBase_ToolBox.cpp
1 // Copyright (C) 2014-2023  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
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 const QString AStyle = "QToolButton:checked {border: 1px solid black; background-color:#C0DCF3}";
32
33
34 ModuleBase_ToolBox::ModuleBase_ToolBox(QWidget* theParent, const bool theUseFrameStyleBox)
35 : QFrame(theParent)
36 {
37   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
38   aMainLayout->setMargin(0);
39   aMainLayout->setSpacing(2);
40
41   if (theUseFrameStyleBox) {
42     setFrameStyle(QFrame::Box | QFrame::Raised);
43     aMainLayout->setMargin(2);
44   }
45
46   myButtonsFrame = new QFrame(this);
47
48   myStack = new QStackedWidget(this);
49
50   aMainLayout->addWidget(myButtonsFrame, 0);
51   aMainLayout->addWidget(myStack, 1);
52
53   myButtonsGroup = new QButtonGroup(this);
54   myButtonsGroup->setExclusive(true);
55   myButtonsLayout = new QHBoxLayout(myButtonsFrame);
56   myButtonsLayout->setMargin(0);
57   myButtonsLayout->setSpacing(5);
58   myButtonsLayout->addStretch(1);
59
60   connect(myStack, SIGNAL(currentChanged(int)), this, SIGNAL(currentChanged(int)));
61   connect(myButtonsGroup, SIGNAL(buttonPressed(int)), this, SLOT(onButton(int)));
62 }
63
64 ModuleBase_ToolBox::~ModuleBase_ToolBox()
65 {
66 }
67
68 void ModuleBase_ToolBox::addItem(QWidget* thePage, const QString& theName, const QPixmap& theIcon)
69 {
70   int anOldCount = myStack->count();
71
72   myStack->addWidget(thePage);
73
74   QToolButton* aButton = new QToolButton(myButtonsFrame);
75   aButton->setFocusPolicy(Qt::StrongFocus);
76   aButton->setCheckable(true);
77   aButton->setStyleSheet(AStyle);
78   if (theIcon.isNull())
79     aButton->setText(theName);
80   else {
81     aButton->setIcon(theIcon);
82     aButton->setIconSize(theIcon.size());
83   }
84   aButton->setToolTip(theName);
85   aButton->setObjectName(theName);
86   myButtonsGroup->addButton(aButton, anOldCount);
87   myButtonsLayout->insertWidget(anOldCount, aButton);
88 }
89
90 int ModuleBase_ToolBox::count() const
91 {
92   return myStack->count();
93 }
94
95 int ModuleBase_ToolBox::currentIndex() const
96 {
97   return myStack->currentIndex();
98 }
99
100 void ModuleBase_ToolBox::setCurrentIndex(const int theIndex)
101 {
102   myStack->setCurrentIndex(theIndex);
103   myButtonsGroup->button(theIndex)->setChecked(true);
104 }
105
106 void ModuleBase_ToolBox::onButton(int theIndex)
107 {
108   myStack->setCurrentIndex(theIndex);
109 }
110
111 bool ModuleBase_ToolBox::isOffToolBoxParent(ModuleBase_ModelWidget* theWidget)
112 {
113   bool isOffToolBox = false;
114
115   QList<QWidget*> aControls = theWidget->getControls();
116   if (aControls.size() > 0) {
117     QWidget* aFirstControl = aControls.first();
118
119     QWidget* aWidget = aFirstControl;
120     QWidget* aParent = (QWidget*)aFirstControl->parent();
121     while (aParent) {
122       QStackedWidget* aStackedWidget = dynamic_cast<QStackedWidget*>(aParent);
123       if (aStackedWidget) {
124         isOffToolBox = aStackedWidget->currentWidget() != aWidget;
125         break;
126       }
127       aWidget = aParent;
128       aParent = (QWidget*)aWidget->parent();
129     }
130   }
131   return isOffToolBox;
132 }