Back to index

salome-gui  6.5.0
QDS_CheckBox.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "QDS_CheckBox.h"
00024 
00025 #include <QCheckBox>
00026 
00027 /*
00028   \class QDS_CheckBox
00029   \brief Datum with control corresponding to check box.
00030 
00031   This control can have only two states:
00032   - 1 (on/true)
00033   - 0 (off/false).
00034 
00035   QDS_CheckBox don't take into account standard parameter properties
00036   (minimum, maximum, filter, etc).
00037 
00038   QDS_CheckBox can set and get following values for access methods (setStringValue(),
00039   setIntegerValue(), setDoubleValue(), stringValue(), integerValue(), doubleValue()):
00040   - "1"  - check box state is switched on.
00041   - "0"  - check box state is switched off.
00042   - "-1" - check box state is "PartiallyChecked" (undefined).
00043 
00044   User can set and test "PartiallyChecked" check using methods clear() 
00045   and isEmpty() correspondingly.
00046 */
00047 
00065 QDS_CheckBox::QDS_CheckBox( const QString& id, QWidget* parent, const int flags, const QString& comp )
00066 : QDS_Datum( id, parent, flags, comp )
00067 {
00068 }
00069 
00073 QDS_CheckBox::~QDS_CheckBox()
00074 {
00075 }
00076 
00080 void QDS_CheckBox::clear()
00081 {
00082   setStringValue( "-1" );
00083 }
00084 
00089 QString QDS_CheckBox::getString() const
00090 {
00091   QString val;
00092   if ( checkBox() && checkBox()->checkState() != Qt::PartiallyChecked )
00093     val = checkBox()->isChecked() ? "1" : "0";
00094   return val;
00095 }
00096 
00107 void QDS_CheckBox::setString( const QString& txt )
00108 {
00109   if ( !checkBox() )
00110     return;
00111 
00112   bool isOk;
00113   int val = (int)txt.toDouble( &isOk );
00114   if ( isOk && val < 0 )
00115   {
00116     checkBox()->setTristate();
00117     checkBox()->setCheckState(Qt::PartiallyChecked);
00118   }
00119   else
00120     checkBox()->setChecked( isOk && val != 0 );
00121 }
00122 
00127 QCheckBox* QDS_CheckBox::checkBox() const
00128 {
00129   return ::qobject_cast<QCheckBox*>( controlWidget() );
00130 }
00131 
00137 QWidget* QDS_CheckBox::createControl( QWidget* parent )
00138 {
00139   QCheckBox* cb = new QCheckBox( parent );
00140   connect( cb, SIGNAL( stateChanged( int ) ), SLOT( onParamChanged() ) );
00141   connect( cb, SIGNAL( toggled( bool ) ), SIGNAL( toggled( bool ) ) );
00142   connect( cb, SIGNAL( stateChanged( int ) ), this, SLOT( onStateChanged( int ) ) );
00143   return cb;
00144 }
00145 
00151 void QDS_CheckBox::onParamChanged()
00152 {
00153   emit paramChanged();
00154 }
00155 
00163 void QDS_CheckBox::onStateChanged( int state )
00164 {
00165   if ( state != Qt::PartiallyChecked && checkBox() )
00166     checkBox()->setTristate( false );
00167 }
00168 
00173 void QDS_CheckBox::setChecked( const bool theState )
00174 {
00175   if ( checkBox() )
00176     checkBox()->setChecked( theState );
00177 }
00178 
00183 bool QDS_CheckBox::isChecked() const
00184 {
00185   return checkBox() ? checkBox()->isChecked() : false;
00186 }
00187