Salome HOME
Issue #1659 New widget for supporting optional inputs : rename CheckGroupBox to Optio...
[modules/shaper.git] / src / ModuleBase / ModuleBase_ToolBox.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_ToolBox.cpp
4 // Created:     10 August 2015
5 // Author:      Alexandre SOLOVYOV
6
7 #include <ModuleBase_ToolBox.h>
8 #include <ModuleBase_ModelWidget.h>
9
10 #include <QButtonGroup>
11 #include <QStackedWidget>
12 #include <QHBoxLayout>
13 #include <QVBoxLayout>
14 #include <QToolButton>
15
16 #include <ModuleBase_PagedContainer.h>
17
18 ModuleBase_ToolBox::ModuleBase_ToolBox(QWidget* theParent, const bool theUseFrameStyleBox)
19 : QFrame(theParent)
20 {
21   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
22   aMainLayout->setMargin(0);
23   aMainLayout->setSpacing(2);
24
25   if (theUseFrameStyleBox) {
26     setFrameStyle(QFrame::Box | QFrame::Raised);
27     aMainLayout->setMargin(2);
28   }
29
30   myButtonsFrame = new QFrame(this);
31
32   myStack = new QStackedWidget(this);
33
34   aMainLayout->addWidget(myButtonsFrame, 0);
35   aMainLayout->addWidget(myStack, 1);
36
37   myButtonsGroup = new QButtonGroup(this);
38   myButtonsGroup->setExclusive(true);
39   myButtonsLayout = new QHBoxLayout(myButtonsFrame);
40   myButtonsLayout->setMargin(0);
41   myButtonsLayout->setSpacing(5);
42   myButtonsLayout->addStretch(1);
43
44   connect(myStack, SIGNAL(currentChanged(int)), this, SIGNAL(currentChanged(int)));
45   connect(myButtonsGroup, SIGNAL(buttonPressed(int)), this, SLOT(onButton(int)));
46 }
47
48 ModuleBase_ToolBox::~ModuleBase_ToolBox()
49 {
50 }
51
52 void ModuleBase_ToolBox::addItem(QWidget* thePage, const QString& theName, const QPixmap& theIcon)
53 {
54   int anOldCount = myStack->count();
55
56   myStack->addWidget(thePage);
57
58   QToolButton* aButton = new QToolButton(myButtonsFrame);
59   aButton->setFocusPolicy(Qt::StrongFocus);
60   aButton->setCheckable(true);
61   aButton->setIcon(theIcon);
62   aButton->setIconSize(theIcon.size());
63   aButton->setToolTip(theName);
64   aButton->setObjectName(theName);
65   myButtonsGroup->addButton(aButton, anOldCount);
66   myButtonsLayout->insertWidget(anOldCount, aButton);
67 }
68
69 int ModuleBase_ToolBox::count() const
70 {
71   return myStack->count();
72 }
73
74 int ModuleBase_ToolBox::currentIndex() const
75 {
76   return myStack->currentIndex();
77 }
78
79 void ModuleBase_ToolBox::setCurrentIndex(const int theIndex)
80 {
81   myStack->setCurrentIndex(theIndex);
82   myButtonsGroup->button(theIndex)->setChecked(true);
83 }
84
85 void ModuleBase_ToolBox::onButton(int theIndex)
86 {
87   myStack->setCurrentIndex(theIndex);
88 }
89
90 bool ModuleBase_ToolBox::isOffToolBoxParent(ModuleBase_ModelWidget* theWidget)
91 {
92   bool isOffToolBox = false;
93
94   QList<QWidget*> aControls = theWidget->getControls();
95   if (aControls.size() > 0) {
96     QWidget* aFirstControl = aControls.first();
97
98     QWidget* aWidget = aFirstControl;
99     QWidget* aParent = (QWidget*)aFirstControl->parent();
100     while (aParent) {
101       QStackedWidget* aStackedWidget = dynamic_cast<QStackedWidget*>(aParent);
102       if (aStackedWidget) {
103         int anIndex = aStackedWidget->currentIndex();
104         isOffToolBox = aStackedWidget->currentWidget() != aWidget;
105         break;
106       }
107       aWidget = aParent;
108       aParent = (QWidget*)aWidget->parent();
109     }
110   }
111   return isOffToolBox;
112 }