Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / QDS / QDS_SpinBoxDbl.cxx
1 //  Copyright (C) 2007-2008  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.
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 #include "QDS_SpinBoxDbl.h"
23
24 #include <QtxDoubleSpinBox.h>
25
26 /*
27   \class QDS_SpinBoxDbl
28   \brief Datum with control corresponding to spin box.
29   
30   This control is suitable for double numbers. User can input data directly in the spin box
31   or can modify current value by clicking arrow (+/-) buttons.
32 */
33
34 /*!
35   \brief Constructor. 
36
37   Create spin box datum object with datum identifier \a id and parent widget \a parent.
38
39   Parameter \a flags defines behaviour of datum and set of created
40   subwidgets. Default value of this parameter is QDS::All.
41
42   Parameter \a comp specifies the component name which will be used
43   when searching the dictionary item.
44
45   \param id datum identifier
46   \param parent parent widget
47   \param flags datum flags
48   \param comp component
49 */
50 QDS_SpinBoxDbl::QDS_SpinBoxDbl( const QString& id, QWidget* parent, const int flags, const QString& comp )
51 : QDS_Datum( id, parent, flags, comp )
52 {
53 }
54
55 /*!
56   \brief Destructor.
57 */
58 QDS_SpinBoxDbl::~QDS_SpinBoxDbl()
59 {
60 }
61
62 /*!
63   \brief Get string from the spin box.
64   \return string value
65 */
66 QString QDS_SpinBoxDbl::getString() const
67 {
68   QString res;
69   QtxDoubleSpinBox* sb = spinBox();
70   if ( sb && !sb->isCleared() )
71   {
72     bool hasFocus = sb->hasFocus();
73     if ( hasFocus )
74       sb->clearFocus();
75     
76     res = sb->text();
77     if ( !sb->suffix().isEmpty() )
78       res.remove( res.indexOf( sb->suffix() ), sb->suffix().length() );
79     if ( !sb->prefix().isEmpty() )
80       res.remove( res.indexOf( sb->prefix() ), sb->prefix().length() );
81     
82     if ( hasFocus )
83       sb->setFocus();
84   }
85
86   return res;
87 }
88
89 /*!
90   \brief Set the string value to the spin box widget.
91   \param txt string value
92 */
93 void QDS_SpinBoxDbl::setString( const QString& txt )
94 {
95   if ( !spinBox() )
96     return;
97
98   spinBox()->setCleared( txt.isEmpty() );
99   if ( !txt.isEmpty() )
100     spinBox()->setValue( txt.toDouble() );
101 }
102
103 /*!
104   \brief Get spin box widget.
105   \return internal spin box widget.
106 */
107 QtxDoubleSpinBox* QDS_SpinBoxDbl::spinBox() const
108 {
109   return ::qobject_cast<QtxDoubleSpinBox*>( controlWidget() );
110 }
111
112 /*!
113   \brief Create internal spin box as control widget.
114   \param parent parent widget
115   \return created spin box widget
116 */
117 QWidget* QDS_SpinBoxDbl::createControl( QWidget* parent )
118 {
119   QtxDoubleSpinBox* aSpinBox = new QtxDoubleSpinBox( parent );
120   aSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
121   connect( aSpinBox, SIGNAL( valueChanged( double ) ), this, SLOT( onValueChanged( double ) ) );
122   return aSpinBox;
123 }
124
125 /*!
126   \brief Called when value in the spin box is changed.
127
128   Emit signals to notify  about value changing in the spin box.
129
130   \param val current spin box value
131 */
132 void QDS_SpinBoxDbl::onValueChanged( double )
133 {
134   onParamChanged();
135   QString str = getString();
136
137   emit paramChanged();
138   emit paramChanged( str );
139 }
140
141 /*!
142   \brief Get the spin box increment value.
143   \return current increment value
144 */
145 double QDS_SpinBoxDbl::step() const
146 {
147   double s = 0;
148   if ( spinBox() )
149     s = spinBox()->singleStep();
150   return s;
151 }
152
153 /*!
154   \brief Set the spin box increment value.
155   \param step new increment value
156 */
157 void QDS_SpinBoxDbl::setStep( const double step )
158 {
159   if ( spinBox() )
160     spinBox()->setSingleStep( step );
161 }
162
163 /*!
164   \brief Process notification about active units system changing.
165
166   Update spin box contents according to the data dictionary item properties: suffix, prefix, minimum, maximum
167   
168   \param system new active units system
169 */
170 void QDS_SpinBoxDbl::unitSystemChanged( const QString& system )
171 {
172   QDS_Datum::unitSystemChanged( system );
173
174   QtxDoubleSpinBox* sb = spinBox();
175   if ( !sb )
176     return;
177
178   // not porting this code to qt4, only commented, since from the task context
179   // the new setted validator accepts any double
180   //delete sb->validator();
181   //QValidator* valid = validator();
182   //sb->setValidator( valid );
183
184   sb->setSuffix( suffix() );
185   sb->setPrefix( prefix() );
186
187   Standard_Integer aPreci = 1;
188   Handle(DDS_DicItem) aDicItem = dicItem();
189   if ( !aDicItem.IsNull() )
190     aPreci = aDicItem->GetPrecision();
191
192   sb->setDecimals( aPreci );
193
194   sb->setSingleStep( .1 );
195   sb->setMinimum( minValue().isEmpty() ? -DBL_MAX : minValue().toDouble() );
196   sb->setMaximum( maxValue().isEmpty() ? DBL_MAX : maxValue().toDouble() );
197 }