Salome HOME
Issue #3038: Add tooltip
[modules/shaper.git] / src / ModuleBase / ModuleBase_ToolBox.cpp
1 // Copyright (C) 2014-2019  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 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   if (theIcon.isNull())
75     aButton->setText(theName);
76   else {
77     aButton->setIcon(theIcon);
78     aButton->setIconSize(theIcon.size());
79   }
80   aButton->setToolTip(theName);
81   aButton->setObjectName(theName);
82   myButtonsGroup->addButton(aButton, anOldCount);
83   myButtonsLayout->insertWidget(anOldCount, aButton);
84 }
85
86 int ModuleBase_ToolBox::count() const
87 {
88   return myStack->count();
89 }
90
91 int ModuleBase_ToolBox::currentIndex() const
92 {
93   return myStack->currentIndex();
94 }
95
96 void ModuleBase_ToolBox::setCurrentIndex(const int theIndex)
97 {
98   myStack->setCurrentIndex(theIndex);
99   myButtonsGroup->button(theIndex)->setChecked(true);
100 }
101
102 void ModuleBase_ToolBox::onButton(int theIndex)
103 {
104   myStack->setCurrentIndex(theIndex);
105 }
106
107 bool ModuleBase_ToolBox::isOffToolBoxParent(ModuleBase_ModelWidget* theWidget)
108 {
109   bool isOffToolBox = false;
110
111   QList<QWidget*> aControls = theWidget->getControls();
112   if (aControls.size() > 0) {
113     QWidget* aFirstControl = aControls.first();
114
115     QWidget* aWidget = aFirstControl;
116     QWidget* aParent = (QWidget*)aFirstControl->parent();
117     while (aParent) {
118       QStackedWidget* aStackedWidget = dynamic_cast<QStackedWidget*>(aParent);
119       if (aStackedWidget) {
120         int anIndex = aStackedWidget->currentIndex();
121         isOffToolBox = aStackedWidget->currentWidget() != aWidget;
122         break;
123       }
124       aWidget = aParent;
125       aParent = (QWidget*)aWidget->parent();
126     }
127   }
128   return isOffToolBox;
129 }