Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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   \class QDS_SpinBoxDbl
29   
30   Datum with control corresponding to spin box. This control used for double numbers.
31   User can input data directly in spin box or can modify current value with given
32   increment.
33 */
34
35 /*!
36   Constructor. Create spin box datum object with datum identifier \aid under widget \aparent. Parameter \aflags
37   define behaviour of datum and set of created subwidgets. Default value of this parameter is QDS::All.
38   Parameter \acomp specify the component name which will be used during search of dictionary item.
39 */
40 QDS_SpinBoxDbl::QDS_SpinBoxDbl( const QString& id, QWidget* parent, const int flags, const QString& comp )
41 : QDS_Datum( id, parent, flags, comp )
42 {
43 }
44
45 /*!
46   Destructor.
47 */
48 QDS_SpinBoxDbl::~QDS_SpinBoxDbl()
49 {
50 }
51
52 /*!
53   Returns string from QSpinBox widget. Reimplemented.
54 */
55 QString QDS_SpinBoxDbl::getString() const
56 {
57   QString res;
58   QtxDblSpinBox* sb = spinBox();
59   if ( sb && !sb->isCleared() )
60   {
61     bool hasFocus = sb->hasFocus();
62     if ( hasFocus )
63       sb->clearFocus();
64     
65     res = sb->text();
66     if ( !sb->suffix().isEmpty() )
67       res.remove( res.find( sb->suffix() ), sb->suffix().length() );
68     if ( !sb->prefix().isEmpty() )
69       res.remove( res.find( sb->prefix() ), sb->prefix().length() );
70     
71     if ( hasFocus )
72       sb->setFocus();
73   }
74
75   return res;
76 }
77
78 /*!
79   Sets the string into QSpinBox widget. Reimplemented.
80 */
81 void QDS_SpinBoxDbl::setString( const QString& txt )
82 {
83   if ( !spinBox() )
84     return;
85
86   spinBox()->setCleared( txt.isEmpty() );
87   if ( !txt.isEmpty() )
88     spinBox()->setValue( txt.toDouble() );
89 }
90
91 /*!
92   Returns pointer to QtxDblSpinBox widget.
93 */
94 QtxDblSpinBox* QDS_SpinBoxDbl::spinBox() const
95 {
96   return ::qt_cast<QtxDblSpinBox*>( controlWidget() );
97 }
98
99 /*!
100   Create QSpinBox widget as control subwidget. Reimplemented.
101 */
102 QWidget* QDS_SpinBoxDbl::createControl( QWidget* parent )
103 {
104   QtxDblSpinBox* aSpinBox = new QtxDblSpinBox( parent );
105   aSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
106   connect( aSpinBox, SIGNAL( valueChanged( double ) ), this, SLOT( onValueChanged( double ) ) );
107   return aSpinBox;
108 }
109
110 /*!
111   Notify about value changing in spin box.
112 */
113 void QDS_SpinBoxDbl::onValueChanged( double )
114 {
115   onParamChanged();
116   QString str = getString();
117
118   emit paramChanged();
119   emit paramChanged( str );
120 }
121
122 /*!
123   Returns the increment step.
124 */
125 double QDS_SpinBoxDbl::step() const
126 {
127   double s = 0;
128   if ( spinBox() )
129     s = spinBox()->lineStep();
130   return s;
131 }
132
133 /*!
134   Sets the increment step.
135 */
136 void QDS_SpinBoxDbl::setStep( const double step )
137 {
138   if ( spinBox() )
139     spinBox()->setLineStep( step );
140 }
141
142 /*!
143   Notification about active unit system changing. Reimplemented from QDS_Datum.
144   Update validator and spin box parameters: suffix, prefix, minimum, maximum.
145 */
146 void QDS_SpinBoxDbl::unitSystemChanged( const QString& system )
147 {
148   QDS_Datum::unitSystemChanged( system );
149
150   QtxDblSpinBox* sb = spinBox();
151   if ( !sb )
152     return;
153
154   delete sb->validator();
155   QValidator* valid = validator();
156   sb->setValidator( valid );
157
158   sb->setSuffix( suffix() );
159   sb->setPrefix( prefix() );
160
161   Standard_Integer aPreci = 1;
162   Handle(DDS_DicItem) aDicItem = dicItem();
163   if ( !aDicItem.IsNull() )
164     aPreci = aDicItem->GetPrecision();
165
166   sb->setPrecision( aPreci );
167
168   sb->setLineStep( .1 );
169   sb->setMinValue( minValue().isEmpty() ? -DBL_MAX : minValue().toDouble() );
170   sb->setMaxValue( maxValue().isEmpty() ? DBL_MAX : maxValue().toDouble() );
171 }