Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/gui.git] / src / QDS / QDS_SpinBox.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/
18 //
19 #include "QDS_SpinBox.h"
20
21 #include <QtxIntSpinBox.h>
22
23 #include <qvalidator.h>
24
25 /*!
26   Constructor.
27 */
28 QDS_SpinBox::QDS_SpinBox( const QString& id, QWidget* parent, const int flags, const QString& comp )
29 : QDS_Datum( id, parent, flags, comp )
30 {
31 }
32
33 /*!
34   Destructor.
35 */
36 QDS_SpinBox::~QDS_SpinBox()
37 {
38 }
39
40 /*!
41   Returns string from QSpinBox widget.
42 */
43 QString QDS_SpinBox::getString() const
44 {
45   QString res;
46   QtxIntSpinBox* aSpinBox = spinBox();
47   if ( aSpinBox && !aSpinBox->isCleared() )
48   {
49     res = aSpinBox->text();
50     if ( !aSpinBox->suffix().isEmpty() )
51       res.remove( res.find( aSpinBox->suffix() ), aSpinBox->suffix().length() );
52     if ( !aSpinBox->prefix().isEmpty() )
53       res.remove( res.find( aSpinBox->prefix() ), aSpinBox->prefix().length() );
54   }
55   return res;
56 }
57
58 /*!
59   Sets the string into QSpinBox widget.
60 */
61 void QDS_SpinBox::setString( const QString& txt )
62 {
63   if ( !spinBox() )
64     return;
65
66   spinBox()->setCleared( txt.isEmpty() );
67   if ( !txt.isEmpty() )
68     spinBox()->setValue( txt.toInt() );
69 }
70
71 /*!
72   Returns pointer to QSpinBox widget.
73 */
74 QtxIntSpinBox* QDS_SpinBox::spinBox() const
75 {
76   return ::qt_cast<QtxIntSpinBox*>( controlWidget() );
77 }
78
79 /*!
80   Create QSpinBox widget as control subwidget.
81 */
82 QWidget* QDS_SpinBox::createControl( QWidget* parent )
83 {
84   QtxIntSpinBox* aSpinBox = new QtxIntSpinBox( parent );
85   aSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
86   connect( aSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( onValueChanged( int ) ) );
87   return aSpinBox;
88 }
89
90 /*!
91   Notify about text changing in spin box.
92 */
93 void QDS_SpinBox::onValueChanged( int val )
94 {
95   onParamChanged();
96   QString str = QString::number( val );
97   emit paramChanged();
98   emit paramChanged( str );
99 }
100
101 /*!
102   Sets the increment step.
103 */
104 void QDS_SpinBox::setStep( const int step )
105 {
106   if ( spinBox() )
107     spinBox()->setLineStep( step );
108 }
109
110 /*!
111   Returns the increment step.
112 */
113 int QDS_SpinBox::step() const
114 {
115   int s = 0;
116   if ( spinBox() )
117     s = spinBox()->lineStep();
118   return s;
119 }
120
121 /*!
122   This method is redefined from ancestor class to perform own initialization ( suffix, prefix, etc ).
123 */
124 void QDS_SpinBox::unitSystemChanged( const QString& system )
125 {
126   QDS_Datum::unitSystemChanged( system );
127
128   QSpinBox* sb = spinBox();
129   if ( sb )
130   {
131     delete sb->validator();
132     QValidator* valid = validator();
133     sb->setValidator( valid );
134
135     sb->setSuffix( suffix() );
136     sb->setPrefix( prefix() );
137     sb->setMinValue( minValue().toInt() );
138     sb->setMaxValue( maxValue().toInt() );
139   }
140 }