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