Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / QDS / QDS_CheckBox.cxx
1 // Copyright (C) 2005  CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D
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.
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 #include "QDS_CheckBox.h"
20
21 #include <qcheckbox.h>
22
23 /*
24   \class QDS_CheckBox
25   
26   Datum with control corresponding to check box. This control can have only two states:
27   1 (on/true) or 0 (off/false). QDS_CheckBox don't take into account standard parameter
28   properties (minimum, maximum, filter, etc).
29
30   QDS_CheckBox can set and get following values for access methods (setStringValue(),
31   setIntegerValue(), setDoubleValue(), stringValue(), integerValue(), doubleValue()):
32     \li "1"  - check box state is setted as on.
33     \li "0"  - check box state is setted as off.
34     \li "-1" - check box state is setted as "NoChage" (undefined).
35
36   User can set and check a state "NoChange" using methods clear() and isEmpty() accordingly.
37 */
38
39 /*!
40   Constructor. Create check box datum object with datum identifier \aid under widget \aparent.
41   Parameter \aflags define behaviour of datum and set of created subwidgets. Default value of
42   this parameter is QDS::All. Parameter \acomp specify the component name which will be used
43   during search of dictionary item.
44 */
45 QDS_CheckBox::QDS_CheckBox( const QString& id, QWidget* parent, const int flags, const QString& comp )
46 : QDS_Datum( id, parent, flags, comp )
47 {
48 }
49
50 /*!
51   Destructor.
52 */
53 QDS_CheckBox::~QDS_CheckBox()
54 {
55 }
56
57 /*!
58   Sets the state "NoChange" for checkbox.
59 */
60 void QDS_CheckBox::clear()
61 {
62   setStringValue( "-1" );
63 }
64
65 /*!
66   Returns string from QCheckBox widget. If the check box state is on then 1 returned otherwise 0.
67 */
68 QString QDS_CheckBox::getString() const
69 {
70   QString val;
71   if ( checkBox() && checkBox()->state() != QButton::NoChange )
72     val = checkBox()->isChecked() ? "1" : "0";
73   return val;
74 }
75
76 /*!
77   Sets the string into QCheckBox widget. If argument \atxt is string with number "1" then check box
78   state is setted as on. If argument \atxt is string with number "0" then state is setted as off.
79   If argument \atxt is string with number "-1" then state is setted as "NoChage" (undefined).
80 */
81 void QDS_CheckBox::setString( const QString& txt )
82 {
83   if ( !checkBox() )
84     return;
85
86   bool isOk;
87   int val = (int)txt.toDouble( &isOk );
88   if ( isOk && val < 0 )
89   {
90     checkBox()->setTristate();
91     checkBox()->setNoChange();
92   }
93   else
94     checkBox()->setChecked( isOk && val != 0 );
95 }
96
97 /*!
98   Returns pointer to QCheckBox widget.
99 */
100 QCheckBox* QDS_CheckBox::checkBox() const
101 {
102   return ::qt_cast<QCheckBox*>( controlWidget() );
103 }
104
105 /*!
106   Create QCheckBox widget as control subwidget.
107 */
108 QWidget* QDS_CheckBox::createControl( QWidget* parent )
109 {
110   QCheckBox* cb = new QCheckBox( parent );
111   connect( cb, SIGNAL( stateChanged( int ) ), SLOT( onParamChanged() ) );
112   connect( cb, SIGNAL( toggled( bool ) ), SIGNAL( toggled( bool ) ) );
113   connect( cb, SIGNAL( stateChanged( int ) ), this, SLOT( onStateChanged( int ) ) );
114   return cb;
115 }
116
117 /*!
118   Notify about ñhanging of control state
119 */
120 void QDS_CheckBox::onParamChanged()
121 {
122   emit paramChanged();
123 }
124
125 /*!
126   Notify about ñhanging of control state. Switch off check box property "tristate" when
127   state changed by user.
128 */
129 void QDS_CheckBox::onStateChanged( int state )
130 {
131   if ( state != QButton::NoChange && checkBox() )
132     checkBox()->setTristate( false );
133 }
134
135 /*!
136   Sets the check box state \atheState.
137 */
138 void QDS_CheckBox::setChecked( const bool theState )
139 {
140   if ( checkBox() )
141     checkBox()->setChecked( theState );
142 }
143
144 /*!
145   Returns current check box state.
146 */
147 bool QDS_CheckBox::isChecked() const
148 {
149   return checkBox() ? checkBox()->isChecked() : false;
150 }