Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / QDS / QDS_CheckBox.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "QDS_CheckBox.h"
23
24 #include <QCheckBox>
25
26 /*
27   \class QDS_CheckBox
28   \brief Datum with control corresponding to check box.
29
30   This control can have only two states:
31   - 1 (on/true)
32   - 0 (off/false).
33
34   QDS_CheckBox don't take into account standard parameter properties
35   (minimum, maximum, filter, etc).
36
37   QDS_CheckBox can set and get following values for access methods (setStringValue(),
38   setIntegerValue(), setDoubleValue(), stringValue(), integerValue(), doubleValue()):
39   - "1"  - check box state is switched on.
40   - "0"  - check box state is switched off.
41   - "-1" - check box state is "PartiallyChecked" (undefined).
42
43   User can set and test "PartiallyChecked" check using methods clear() 
44   and isEmpty() correspondingly.
45 */
46
47 /*!
48   \brief Constructor. 
49
50   Create check box datum object with datum identifier \a id 
51   and parent widget \a parent.
52
53   Parameter \a flags defines behaviour of datum and set of created
54   subwidgets. Default value of this parameter is QDS::All. 
55   
56   Parameter \a comp specifies the component name which will be used
57   when searching the dictionary item.
58
59   \param id datum identifier
60   \param parent parent widget
61   \param flags datum flags
62   \param comp component
63 */
64 QDS_CheckBox::QDS_CheckBox( const QString& id, QWidget* parent, const int flags, const QString& comp )
65 : QDS_Datum( id, parent, flags, comp )
66 {
67 }
68
69 /*!
70   \brief Destructor.
71 */
72 QDS_CheckBox::~QDS_CheckBox()
73 {
74 }
75
76 /*!
77   \brief Set the state "PartiallyChecked" (undefined) for checkbox.
78 */
79 void QDS_CheckBox::clear()
80 {
81   setStringValue( "-1" );
82 }
83
84 /*!
85   \brief Get string value from the widget.
86   \return "1" if check box is checked on and "0" otherwise
87 */
88 QString QDS_CheckBox::getString() const
89 {
90   QString val;
91   if ( checkBox() && checkBox()->checkState() != Qt::PartiallyChecked )
92     val = checkBox()->isChecked() ? "1" : "0";
93   return val;
94 }
95
96 /*!
97   \brief Set the string value into the widget.
98
99   If string \a txt contains "1", then check box state is switched on.
100   If string \a txt contains "0", then check box state is switched on.
101   If string \a txt contains "-1", then check box is reset to 
102   "PartiallyChecked" (undefined) state.
103
104   \param txt string value
105 */
106 void QDS_CheckBox::setString( const QString& txt )
107 {
108   if ( !checkBox() )
109     return;
110
111   bool isOk;
112   int val = (int)txt.toDouble( &isOk );
113   if ( isOk && val < 0 )
114   {
115     checkBox()->setTristate();
116     checkBox()->setCheckState(Qt::PartiallyChecked);
117   }
118   else
119     checkBox()->setChecked( isOk && val != 0 );
120 }
121
122 /*!
123   \brief Get internal check box.
124   \return pointer to QCheckBox widget
125 */
126 QCheckBox* QDS_CheckBox::checkBox() const
127 {
128   return ::qobject_cast<QCheckBox*>( controlWidget() );
129 }
130
131 /*!
132   \brief Create internal check box as control widget.
133   \param parent parent widget
134   \return created check box widget
135 */
136 QWidget* QDS_CheckBox::createControl( QWidget* parent )
137 {
138   QCheckBox* cb = new QCheckBox( parent );
139   connect( cb, SIGNAL( stateChanged( int ) ), SLOT( onParamChanged() ) );
140   connect( cb, SIGNAL( toggled( bool ) ), SIGNAL( toggled( bool ) ) );
141   connect( cb, SIGNAL( stateChanged( int ) ), this, SLOT( onStateChanged( int ) ) );
142   return cb;
143 }
144
145 /*!
146   \brief Called when check box is switched.
147
148   Emits signal paramChanged() to notify about changing of the control state.
149 */
150 void QDS_CheckBox::onParamChanged()
151 {
152   emit paramChanged();
153 }
154
155 /*!
156   \brief Called when check box is switched.
157
158   Switch off check box property "tristate" when state is changed by the user.
159
160   \param state new check box state
161 */
162 void QDS_CheckBox::onStateChanged( int state )
163 {
164   if ( state != Qt::PartiallyChecked && checkBox() )
165     checkBox()->setTristate( false );
166 }
167
168 /*!
169   \brief Set the check box state to \a theState.
170   \param theState new check box state
171 */
172 void QDS_CheckBox::setChecked( const bool theState )
173 {
174   if ( checkBox() )
175     checkBox()->setChecked( theState );
176 }
177
178 /*!
179   \brief Get current check box state.
180   \return check box state
181 */
182 bool QDS_CheckBox::isChecked() const
183 {
184   return checkBox() ? checkBox()->isChecked() : false;
185 }
186
187 /*!
188   \fn void QDS_CheckBox::toggled( bool on );
189   \brief Emitted when the check box state is toggled.
190   \param on new check box state
191 */