Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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/ or email : webmaster.salome@opencascade.com
18 //
19 #include "QDS_SpinBox.h"
20
21 #include <QtxIntSpinBox.h>
22
23 #include <qvalidator.h>
24
25 /*
26   \class QDS_SpinBox
27   
28   Datum with control corresponding to spin box. This control used for integer numbers.
29   User can input data directly in spin box or can modify current value with given
30   increment.
31 */
32
33 /*!
34   Constructor. Create spin box datum object with datum identifier \aid under widget \aparent. Parameter \aflags
35   define behaviour of datum and set of created subwidgets. Default value of this parameter is QDS::All.
36   Parameter \acomp specify the component name which will be used during search of dictionary item.
37 */
38 QDS_SpinBox::QDS_SpinBox( const QString& id, QWidget* parent, const int flags, const QString& comp )
39 : QDS_Datum( id, parent, flags, comp )
40 {
41 }
42
43 /*!
44   Destructor.
45 */
46 QDS_SpinBox::~QDS_SpinBox()
47 {
48 }
49
50 /*!
51   Returns string from QSpinBox widget. Reimplemented.
52 */
53 QString QDS_SpinBox::getString() const
54 {
55   QString res;
56   QtxIntSpinBox* aSpinBox = spinBox();
57   if ( aSpinBox && !aSpinBox->isCleared() )
58   {
59     res = aSpinBox->text();
60     if ( !aSpinBox->suffix().isEmpty() )
61       res.remove( res.find( aSpinBox->suffix() ), aSpinBox->suffix().length() );
62     if ( !aSpinBox->prefix().isEmpty() )
63       res.remove( res.find( aSpinBox->prefix() ), aSpinBox->prefix().length() );
64   }
65   return res;
66 }
67
68 /*!
69   Sets the string into QSpinBox widget. Reimplemented.
70 */
71 void QDS_SpinBox::setString( const QString& txt )
72 {
73   if ( !spinBox() )
74     return;
75
76   spinBox()->setCleared( txt.isEmpty() );
77   if ( !txt.isEmpty() )
78     spinBox()->setValue( txt.toInt() );
79 }
80
81 /*!
82   Returns pointer to QSpinBox widget.
83 */
84 QtxIntSpinBox* QDS_SpinBox::spinBox() const
85 {
86   return ::qt_cast<QtxIntSpinBox*>( controlWidget() );
87 }
88
89 /*!
90   Create QSpinBox widget as control subwidget. Reimplemented.
91 */
92 QWidget* QDS_SpinBox::createControl( QWidget* parent )
93 {
94   QtxIntSpinBox* aSpinBox = new QtxIntSpinBox( parent );
95   aSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
96   connect( aSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( onValueChanged( int ) ) );
97   return aSpinBox;
98 }
99
100 /*!
101   Notify about value changing in spin box.
102 */
103 void QDS_SpinBox::onValueChanged( int val )
104 {
105   onParamChanged();
106   QString str = QString::number( val );
107   emit paramChanged();
108   emit paramChanged( str );
109 }
110
111 /*!
112   Sets the increment step.
113 */
114 void QDS_SpinBox::setStep( const int step )
115 {
116   if ( spinBox() )
117     spinBox()->setLineStep( step );
118 }
119
120 /*!
121   Returns the increment step.
122 */
123 int QDS_SpinBox::step() const
124 {
125   int s = 0;
126   if ( spinBox() )
127     s = spinBox()->lineStep();
128   return s;
129 }
130
131 /*!
132   Notification about active unit system changing. Reimplemented from QDS_Datum.
133   Update validator and spin box parameters: suffix, prefix, minimum, maximum.
134 */
135 void QDS_SpinBox::unitSystemChanged( const QString& system )
136 {
137   QDS_Datum::unitSystemChanged( system );
138
139   QSpinBox* sb = spinBox();
140   if ( sb )
141   {
142     delete sb->validator();
143     QValidator* valid = validator();
144     sb->setValidator( valid );
145
146     sb->setSuffix( suffix() );
147     sb->setPrefix( prefix() );
148     sb->setMinValue( minValue().toInt() );
149     sb->setMaxValue( maxValue().toInt() );
150   }
151 }