Salome HOME
Copyright update 2020
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetRadiobox.cpp
1 // Copyright (C) 2014-2020  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_WidgetRadiobox.h>
21 #include <ModuleBase_PageBase.h>
22
23 #include <QFormLayout>
24 #include <QRadioButton>
25 #include <QFrame>
26 #include <QButtonGroup>
27
28
29 ModuleBase_WidgetRadiobox::ModuleBase_WidgetRadiobox(QWidget* theParent,
30                                                      const Config_WidgetAPI* theData)
31   : ModuleBase_PagedContainer(theParent, theData)
32 {
33   myLayout = new QFormLayout(this);
34   ModuleBase_Tools::adjustMargins(myLayout);
35   myGroup = new QButtonGroup(this);
36   myGroup->setExclusive(true);
37
38   connect(myGroup, SIGNAL(buttonToggled(int, bool)), SLOT(onPageChanged()));
39 }
40
41 ModuleBase_WidgetRadiobox::~ModuleBase_WidgetRadiobox()
42 {
43 }
44
45 int ModuleBase_WidgetRadiobox::addPage(ModuleBase_PageBase* thePage,
46                                         const QString& theName,
47                                         const QString& theCaseId,
48                                         const QPixmap& theIcon,
49                                         const QString& theTooltip)
50 {
51   ModuleBase_PagedContainer::addPage(thePage, theName, theCaseId, theIcon, theTooltip);
52   QWidget* aWgt = new QWidget(this);
53   QVBoxLayout* aLay = new QVBoxLayout(aWgt);
54   aLay->setContentsMargins(0, 0, 0, 0);
55
56   QRadioButton* aButton;
57   if (theIcon.isNull()) {
58     aButton = new QRadioButton(theName, aWgt);
59     aButton->setToolTip(translate(theTooltip.toStdString()));
60   }
61   else {
62     aButton = new QRadioButton(aWgt);
63     aButton->setToolTip(translate(theName.toStdString()));
64   }
65   aLay->addStretch();
66   aLay->addWidget(aButton);
67   aLay->addStretch();
68
69   //QFrame* aFrame = dynamic_cast<QFrame*>(thePage);
70   QWidget* aPage = thePage->pageWidget();
71   myLayout->addRow(aWgt, aPage);
72   myGroup->addButton(aButton, myGroup->buttons().count());
73
74   bool isDefault = theCaseId.toStdString() == getDefaultValue();
75   aButton->setChecked(isDefault);
76   aPage->setEnabled(isDefault);
77   connect(aButton, SIGNAL(toggled(bool)), aPage, SLOT(setEnabled(bool)));
78
79   return myGroup->buttons().count();
80 }
81
82 int ModuleBase_WidgetRadiobox::currentPageIndex() const
83 {
84   return myGroup->checkedId();
85 }
86
87 void ModuleBase_WidgetRadiobox::setCurrentPageIndex(int theIndex)
88 {
89   bool isSignalsBlocked = myGroup->blockSignals(true);
90   myGroup->button(theIndex)->setChecked(true);
91   myGroup->blockSignals(isSignalsBlocked);
92 }