Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/gui.git] / src / QDS / QDS_SpinBoxDbl.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_SpinBoxDbl.h"
20
21 #include <DDS_Dictionary.h>
22
23 #include <qvalidator.h>
24
25 #include <QtxDblSpinBox.h>
26
27 /*!
28   Constructor.
29 */
30 QDS_SpinBoxDbl::QDS_SpinBoxDbl( const QString& id, QWidget* parent, const int flags, const QString& comp )
31 : QDS_Datum( id, parent, flags, comp )
32 {
33 }
34
35 /*!
36   Destructor.
37 */
38 QDS_SpinBoxDbl::~QDS_SpinBoxDbl()
39 {
40 }
41
42 /*!
43   Returns string from QSpinBox widget.
44 */
45 QString QDS_SpinBoxDbl::getString() const
46 {
47   QString res;
48   QtxDblSpinBox* sb = spinBox();
49   if ( sb && !sb->isCleared() )
50   {
51     bool hasFocus = sb->hasFocus();
52     if ( hasFocus )
53       sb->clearFocus();
54     
55     res = sb->text();
56     if ( !sb->suffix().isEmpty() )
57       res.remove( res.find( sb->suffix() ), sb->suffix().length() );
58     if ( !sb->prefix().isEmpty() )
59       res.remove( res.find( sb->prefix() ), sb->prefix().length() );
60     
61     if ( hasFocus )
62       sb->setFocus();
63   }
64
65   return res;
66 }
67
68 /*!
69   Sets the string into QSpinBox widget.
70 */
71 void QDS_SpinBoxDbl::setString( const QString& txt )
72 {
73   if ( !spinBox() )
74     return;
75
76   spinBox()->setCleared( txt.isEmpty() );
77   if ( !txt.isEmpty() )
78     spinBox()->setValue( txt.toDouble() );
79 }
80
81 /*!
82   Returns pointer to QtxDblSpinBox widget.
83 */
84 QtxDblSpinBox* QDS_SpinBoxDbl::spinBox() const
85 {
86   return ::qt_cast<QtxDblSpinBox*>( controlWidget() );
87 }
88
89 /*!
90   Create QSpinBox widget as control subwidget.
91 */
92 QWidget* QDS_SpinBoxDbl::createControl( QWidget* parent )
93 {
94   QtxDblSpinBox* aSpinBox = new QtxDblSpinBox( parent );
95   aSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
96   connect( aSpinBox, SIGNAL( valueChanged( double ) ), this, SLOT( onValueChanged( double ) ) );
97   return aSpinBox;
98 }
99
100 /*!
101   Notify about text changing in spin box.
102 */
103 void QDS_SpinBoxDbl::onValueChanged( double )
104 {
105   onParamChanged();
106   QString str = getString();
107
108   emit paramChanged();
109   emit paramChanged( str );
110 }
111
112 /*!
113   Returns the increment step.
114 */
115 double QDS_SpinBoxDbl::step() const
116 {
117   double s = 0;
118   if ( spinBox() )
119     s = spinBox()->lineStep();
120   return s;
121 }
122
123 /*!
124   Sets the increment step.
125 */
126 void QDS_SpinBoxDbl::setStep( const double step )
127 {
128   if ( spinBox() )
129     spinBox()->setLineStep( step );
130 }
131
132 void QDS_SpinBoxDbl::unitSystemChanged( const QString& system )
133 {
134   QDS_Datum::unitSystemChanged( system );
135
136   QtxDblSpinBox* sb = spinBox();
137   if ( !sb )
138     return;
139
140   delete sb->validator();
141   QValidator* valid = validator();
142   sb->setValidator( valid );
143
144   sb->setSuffix( suffix() );
145   sb->setPrefix( prefix() );
146
147   Standard_Integer aPreci = 1;
148   Handle(DDS_DicItem) aDicItem = dicItem();
149   if ( !aDicItem.IsNull() )
150     aPreci = aDicItem->GetPrecision();
151
152   sb->setPrecision( aPreci );
153
154   sb->setLineStep( .1 );
155   sb->setMinValue( minValue().isEmpty() ? -DBL_MAX : minValue().toDouble() );
156   sb->setMaxValue( maxValue().isEmpty() ? DBL_MAX : maxValue().toDouble() );
157 }