Salome HOME
Issue #273: Add copyright string
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSwitch.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_WidgetSwitch.cpp
5  *
6  *  Created on: Apr 16, 2014
7  *      Author: sbh
8  */
9
10 #include <ModuleBase_WidgetSwitch.h>
11
12 #include <QComboBox>
13 #include <QVBoxLayout>
14 #include <QSpacerItem>
15
16 ModuleBase_WidgetSwitch::ModuleBase_WidgetSwitch(QWidget* parent)
17     : QFrame(parent)
18 {
19   myMainLay = new QVBoxLayout(this);
20   myMainLay->setContentsMargins(2, 4, 2, 2);
21   myCombo = new QComboBox(this);
22   myCombo->hide();
23   myMainLay->addWidget(myCombo);
24   this->setFrameShape(QFrame::StyledPanel);
25   connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(setCurrentIndex(int)));
26   connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SIGNAL(currentPageChanged(int)));
27
28 }
29
30 ModuleBase_WidgetSwitch::~ModuleBase_WidgetSwitch()
31 {
32 }
33
34 int ModuleBase_WidgetSwitch::addPage(QWidget* theWidget, const QString& theName)
35 {
36   return insertPage(count(), theWidget, theName);
37 }
38
39 int ModuleBase_WidgetSwitch::count() const
40 {
41   return myCombo->count();
42 }
43
44 int ModuleBase_WidgetSwitch::currentIndex() const
45 {
46   return myCombo->currentIndex();
47 }
48
49 QWidget* ModuleBase_WidgetSwitch::currentWidget() const
50 {
51   int idx = currentIndex();
52   return myCases[idx];
53 }
54
55 int ModuleBase_WidgetSwitch::indexOf(QWidget* theWidget) const
56 {
57   return myCases.indexOf(theWidget);
58 }
59
60 int ModuleBase_WidgetSwitch::insertPage(int theIndex, QWidget* theWidget, const QString& theName)
61 {
62   int index = theIndex < count() ? theIndex : count();
63   if (count() == 0)
64     myCombo->show();
65   myCombo->insertItem(index, theName);
66   myCases.insert(index, theWidget);
67   myMainLay->addWidget(theWidget);
68   setCurrentIndex(theIndex);
69   return index;
70 }
71
72 bool ModuleBase_WidgetSwitch::isPageEnabled(int index) const
73 {
74   return myCases[index]->isEnabled();
75 }
76
77 QString ModuleBase_WidgetSwitch::pageText(int index) const
78 {
79   return myCombo->itemText(index);
80 }
81
82 QString ModuleBase_WidgetSwitch::pageToolTip(int index) const
83 {
84   return myCases[index]->toolTip();
85 }
86
87 void ModuleBase_WidgetSwitch::removePage(int index)
88 {
89   myCombo->removeItem(index);
90   myCases.removeAt(index);
91   if (count() == 0) {
92     myCombo->hide();
93   }
94 }
95
96 void ModuleBase_WidgetSwitch::setPageEnabled(int index, bool enabled)
97 {
98   myCases[index]->setEnabled(enabled);
99 }
100
101 void ModuleBase_WidgetSwitch::setPageName(int index, const QString& theName)
102 {
103   myCombo->setItemText(index, theName);
104 }
105
106 void ModuleBase_WidgetSwitch::setPageToolTip(int index, const QString& toolTip)
107 {
108   myCases[index]->setToolTip(toolTip);
109 }
110
111 void ModuleBase_WidgetSwitch::setCurrentIndex(int index)
112 {
113   myCombo->setCurrentIndex(index);
114   refresh();
115 }
116
117 void ModuleBase_WidgetSwitch::refresh()
118 {
119   foreach(QWidget* eachWidget, myCases)
120   {
121     eachWidget->setVisible(false);
122   }
123   if (currentIndex() >= myCases.count())
124     return;
125   myCases[currentIndex()]->setVisible(true);
126 }