Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / QDS / QDS_SpinBox.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_SpinBox.h"
23
24 #include <QtxIntSpinBox.h>
25
26 /*
27   \class QDS_SpinBox
28   \brief Datum with control corresponding to spin box.
29
30   This control used for integer 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_SpinBox::QDS_SpinBox( 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_SpinBox::~QDS_SpinBox()
59 {
60 }
61
62 /*!
63   \brief Get string from the spin box.
64   \return string value
65 */
66 QString QDS_SpinBox::getString() const
67 {
68   QString res;
69   QtxIntSpinBox* aSpinBox = spinBox();
70   if ( aSpinBox && !aSpinBox->isCleared() )
71   {
72     res = aSpinBox->text();
73     if ( !aSpinBox->suffix().isEmpty() )
74       res.remove( res.indexOf( aSpinBox->suffix() ), aSpinBox->suffix().length() );
75     if ( !aSpinBox->prefix().isEmpty() )
76       res.remove( res.indexOf( aSpinBox->prefix() ), aSpinBox->prefix().length() );
77   }
78   return res;
79 }
80
81 /*!
82   \brief Set the string value to the spin box widget.
83   \param txt string value
84 */
85 void QDS_SpinBox::setString( const QString& txt )
86 {
87   if ( !spinBox() )
88     return;
89
90   spinBox()->setCleared( txt.isEmpty() );
91   if ( !txt.isEmpty() )
92     spinBox()->setValue( txt.toInt() );
93 }
94
95 /*!
96   \brief Get spin box widget.
97   \return internal spin box widget.
98 */
99 QtxIntSpinBox* QDS_SpinBox::spinBox() const
100 {
101   return ::qobject_cast<QtxIntSpinBox*>( controlWidget() );
102 }
103
104 /*!
105   \brief Create internal spin box as control widget.
106   \param parent parent widget
107   \return created spin box widget
108 */
109 QWidget* QDS_SpinBox::createControl( QWidget* parent )
110 {
111   QtxIntSpinBox* aSpinBox = new QtxIntSpinBox( parent );
112   aSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
113   connect( aSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( onValueChanged( int ) ) );
114   return aSpinBox;
115 }
116
117 /*!
118   \brief Called when value in the spin box is changed.
119
120   Emit signals to notify  about value changing in the spin box.
121
122   \param val current spin box value
123 */
124 void QDS_SpinBox::onValueChanged( int val )
125 {
126   onParamChanged();
127   QString str = QString::number( val );
128   emit paramChanged();
129   emit paramChanged( str );
130 }
131
132 /*!
133   \brief Set the spin box increment value.
134   \param step new increment value
135 */
136 void QDS_SpinBox::setStep( const int step )
137 {
138   if ( spinBox() )
139     spinBox()->setSingleStep( step );
140 }
141
142 /*!
143   \brief Get the spin box increment value.
144   \return current increment value
145 */
146 int QDS_SpinBox::step() const
147 {
148   int s = 0;
149   if ( spinBox() )
150     s = spinBox()->singleStep();
151   return s;
152 }
153
154 /*!
155   \brief Process notification about active units system changing.
156
157   Update spin box contents according to the data dictionary item properties: suffix, prefix, minimum, maximum
158   
159   \param system new active units system
160 */
161 void QDS_SpinBox::unitSystemChanged( const QString& system )
162 {
163   QDS_Datum::unitSystemChanged( system );
164
165   QSpinBox* sb = spinBox();
166   if ( sb )
167   {
168     // not porting this code to qt4, only commented, since from the task context
169     // the new setted validator accepts all integers
170     //delete sb->validator();
171     //QValidator* valid = validator();
172     //sb->setValidator( valid );
173
174     sb->setSuffix( suffix() );
175     sb->setPrefix( prefix() );
176     sb->setMinimum( minValue().isEmpty() ? -INT_MAX : minValue().toInt() );
177     sb->setMaximum( maxValue().isEmpty() ? INT_MAX : maxValue().toInt() );
178
179   }
180 }