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