Salome HOME
cafca0444d8d41cc209efa1a11b678fffec59b76
[modules/gui.git] / src / QDS / QDS_CheckBox.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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
23 #include "QDS_CheckBox.h"
24
25 #include <QCheckBox>
26
27 /*
28   \class QDS_CheckBox
29   \brief Datum with control corresponding to check box.
30
31   This control can have only two states:
32   - 1 (on/true)
33   - 0 (off/false).
34
35   QDS_CheckBox don't take into account standard parameter properties
36   (minimum, maximum, filter, etc).
37
38   QDS_CheckBox can set and get following values for access methods (setStringValue(),
39   setIntegerValue(), setDoubleValue(), stringValue(), integerValue(), doubleValue()):
40   - "1"  - check box state is switched on.
41   - "0"  - check box state is switched off.
42   - "-1" - check box state is "PartiallyChecked" (undefined).
43
44   User can set and test "PartiallyChecked" check using methods clear() 
45   and isEmpty() correspondingly.
46 */
47
48 /*!
49   \brief Constructor. 
50
51   Create check box datum object with datum identifier \a id 
52   and parent widget \a parent.
53
54   Parameter \a flags defines behaviour of datum and set of created
55   subwidgets. Default value of this parameter is QDS::All. 
56   
57   Parameter \a comp specifies the component name which will be used
58   when searching the dictionary item.
59
60   \param id datum identifier
61   \param parent parent widget
62   \param flags datum flags
63   \param comp component
64 */
65 QDS_CheckBox::QDS_CheckBox( const QString& id, QWidget* parent, const int flags, const QString& comp )
66 : QDS_Datum( id, parent, flags, comp )
67 {
68 }
69
70 /*!
71   \brief Destructor.
72 */
73 QDS_CheckBox::~QDS_CheckBox()
74 {
75 }
76
77 /*!
78   \brief Set the state "PartiallyChecked" (undefined) for checkbox.
79 */
80 void QDS_CheckBox::clear()
81 {
82   setStringValue( "-1" );
83 }
84
85 /*!
86   \brief Get string value from the widget.
87   \return "1" if check box is checked on and "0" otherwise
88 */
89 QString QDS_CheckBox::getString() const
90 {
91   QString val;
92   if ( checkBox() && checkBox()->checkState() != Qt::PartiallyChecked )
93     val = checkBox()->isChecked() ? "1" : "0";
94   return val;
95 }
96
97 /*!
98   \brief Set the string value into the widget.
99
100   If string \a txt contains "1", then check box state is switched on.
101   If string \a txt contains "0", then check box state is switched on.
102   If string \a txt contains "-1", then check box is reset to 
103   "PartiallyChecked" (undefined) state.
104
105   \param txt string value
106 */
107 void QDS_CheckBox::setString( const QString& txt )
108 {
109   if ( !checkBox() )
110     return;
111
112   bool isOk;
113   int val = (int)txt.toDouble( &isOk );
114   if ( isOk && val < 0 )
115   {
116     checkBox()->setTristate();
117     checkBox()->setCheckState(Qt::PartiallyChecked);
118   }
119   else
120     checkBox()->setChecked( isOk && val != 0 );
121 }
122
123 /*!
124   \brief Get internal check box.
125   \return pointer to QCheckBox widget
126 */
127 QCheckBox* QDS_CheckBox::checkBox() const
128 {
129   return ::qobject_cast<QCheckBox*>( controlWidget() );
130 }
131
132 /*!
133   \brief Create internal check box as control widget.
134   \param parent parent widget
135   \return created check box widget
136 */
137 QWidget* QDS_CheckBox::createControl( QWidget* parent )
138 {
139   QCheckBox* cb = new QCheckBox( parent );
140   connect( cb, SIGNAL( stateChanged( int ) ), SLOT( onParamChanged() ) );
141   connect( cb, SIGNAL( toggled( bool ) ), SIGNAL( toggled( bool ) ) );
142   connect( cb, SIGNAL( stateChanged( int ) ), this, SLOT( onStateChanged( int ) ) );
143   return cb;
144 }
145
146 /*!
147   \brief Called when check box is switched.
148
149   Emits signal paramChanged() to notify about changing of the control state.
150 */
151 void QDS_CheckBox::onParamChanged()
152 {
153   emit paramChanged();
154 }
155
156 /*!
157   \brief Called when check box is switched.
158
159   Switch off check box property "tristate" when state is changed by the user.
160
161   \param state new check box state
162 */
163 void QDS_CheckBox::onStateChanged( int state )
164 {
165   if ( state != Qt::PartiallyChecked && checkBox() )
166     checkBox()->setTristate( false );
167 }
168
169 /*!
170   \brief Set the check box state to \a theState.
171   \param theState new check box state
172 */
173 void QDS_CheckBox::setChecked( const bool theState )
174 {
175   if ( checkBox() )
176     checkBox()->setChecked( theState );
177 }
178
179 /*!
180   \brief Get current check box state.
181   \return check box state
182 */
183 bool QDS_CheckBox::isChecked() const
184 {
185   return checkBox() ? checkBox()->isChecked() : false;
186 }
187
188 /*!
189   \fn void QDS_CheckBox::toggled( bool on );
190   \brief Emitted when the check box state is toggled.
191   \param on new check box state
192 */