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