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