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