Salome HOME
7428db7d0045dfd7ede4c0c28178ab79800eeee3
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSwitch.cpp
1 // Copyright (C) 2014-2022  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_WidgetSwitch.h>
21 #include <ModuleBase_ModelWidget.h>
22 #include <ModuleBase_PageBase.h>
23 #include <ModuleBase_Tools.h>
24
25 #include <QComboBox>
26 #include <QFrame>
27 #include <QSpacerItem>
28 #include <QStackedLayout>
29 #include <QVBoxLayout>
30
31 ModuleBase_WidgetSwitch::ModuleBase_WidgetSwitch(QWidget* theParent,
32   const Config_WidgetAPI* theData)
33 : ModuleBase_PagedContainer(theParent, theData)
34 {
35   myRemeberChoice = false;
36   QVBoxLayout*  aMainLay = new QVBoxLayout(this);
37   //aMainLay->setContentsMargins(2, 4, 2, 2);
38   ModuleBase_Tools::adjustMargins(aMainLay);
39   myCombo = new QComboBox(this);
40   myCombo->hide();
41   myPagesLayout = new QStackedLayout(this);
42   aMainLay->addWidget(myCombo);
43   aMainLay->addLayout(myPagesLayout, 1);
44   setLayout(aMainLay);
45
46   connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onPageChanged()));
47   connect(myCombo, SIGNAL(activated(int)), myPagesLayout, SLOT(setCurrentIndex(int)));
48 }
49
50 ModuleBase_WidgetSwitch::~ModuleBase_WidgetSwitch()
51 {
52 }
53
54
55 int ModuleBase_WidgetSwitch::addPage(ModuleBase_PageBase* thePage, const QString& theName,
56                                                                    const QString& theCaseId,
57                                                                    const QPixmap& theIcon,
58                                                                    const QString& theTooltip)
59 {
60   ModuleBase_PagedContainer::addPage(thePage, theName, theCaseId, theIcon, theTooltip);
61   myCombo->addItem(translate(theName.toStdString()));
62   int aResultCount = myCombo->count();
63   if (aResultCount == 2)
64     myCombo->show();
65   QFrame* aFrame = dynamic_cast<QFrame*>(thePage);
66   aFrame->setFrameShape(QFrame::Box);
67   aFrame->setFrameStyle(QFrame::Sunken);
68   myPagesLayout->addWidget(aFrame);
69   return aResultCount;
70 }
71
72 int ModuleBase_WidgetSwitch::currentPageIndex() const
73 {
74   int aComboIndex = myCombo->currentIndex();
75   return aComboIndex;
76 }
77
78
79 void ModuleBase_WidgetSwitch::setCurrentPageIndex(int theIndex)
80 {
81   bool isComboSignalsBlocked = myCombo->blockSignals(true);
82   bool isPagesLayoutSignalsBlocked = myPagesLayout->blockSignals(true);
83   myCombo->setCurrentIndex(theIndex);
84   myPagesLayout->setCurrentIndex(theIndex);
85   myCombo->blockSignals(isComboSignalsBlocked);
86   myPagesLayout->blockSignals(isPagesLayoutSignalsBlocked);
87 }