Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/gui.git] / src / Qtx / QtxIntSpinBox.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, 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 // File:      QtxIntSpinBox.cxx
20 // Author:    Sergey TELKOV
21
22 #include "QtxIntSpinBox.h"
23
24 #include <qlineedit.h>
25 #include <qapplication.h>
26
27 /*!
28   Constructor
29 */
30 QtxIntSpinBox::QtxIntSpinBox( QWidget* parent, const char* name )
31 : QSpinBox( parent, name ),
32 myCleared( false ),
33 myBlocked( false )
34 {
35   connect( editor(), SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
36 }
37
38 /*!
39   Constructor
40 */
41 QtxIntSpinBox::QtxIntSpinBox( int min, int max, int step, QWidget* parent, const char* name )
42 : QSpinBox( min, max, step, parent, name ),
43 myCleared( false ),
44 myBlocked( false )
45 {
46   connect( editor(), SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
47 }
48
49 /*!
50   Destructor
51 */
52 QtxIntSpinBox::~QtxIntSpinBox()
53 {
54 }
55
56 /*!
57   \return true if spin box is cleared
58 */
59 bool QtxIntSpinBox::isCleared() const
60 {
61   return myCleared;
62 }
63
64 /*!
65   Changes cleared status of spin box
66   \param on - new status
67 */
68 void QtxIntSpinBox::setCleared( const bool on )
69 {
70   if ( myCleared == on )
71     return;
72     
73   myCleared = on;
74   updateDisplay();
75 }
76
77 /*!
78   Changes value of spin box
79   \param val - new value of spin box
80 */
81 void QtxIntSpinBox::setValue( int value )
82 {
83   myCleared = false;
84
85   QSpinBox::setValue( value );
86 }
87
88 /*!
89   Custom event filter
90 */
91 bool QtxIntSpinBox::eventFilter( QObject* o, QEvent* e )
92 {
93   if ( !myCleared || o != editor() || !editor()->text().stripWhiteSpace().isEmpty() )
94     return QSpinBox::eventFilter( o, e );
95
96   if ( e->type() == QEvent::FocusOut || e->type() == QEvent::Leave || e->type() == QEvent::Hide )
97     return false;
98
99   if ( e->type() == QEvent::KeyPress &&
100              ( ((QKeyEvent*)e)->key() == Key_Tab || ((QKeyEvent*)e)->key() == Key_BackTab ) )
101   {
102     QApplication::sendEvent( this, e );
103     return true;
104   }
105
106   return QSpinBox::eventFilter( o, e );
107 }
108
109 /*!
110   Sets integer value by text in editor
111 */
112 void QtxIntSpinBox::interpretText()
113 {
114   myCleared = false;
115
116   QSpinBox::interpretText();
117 }
118
119 /*!
120   Updates text of editor
121 */
122 void QtxIntSpinBox::updateDisplay()
123 {
124   if ( myBlocked )
125     return;
126
127   bool block = myBlocked;
128   myBlocked = true;
129
130   QSpinBox::updateDisplay();
131
132   if ( myCleared )
133     editor()->clear();
134   else if ( editor()->hasFocus() )
135   {
136     if ( editor()->text() == specialValueText() )
137       editor()->selectAll();
138     else
139       editor()->setSelection( prefix().length(), editor()->text().length() - prefix().length() - suffix().length() );
140   }
141
142   myBlocked = block;
143 }
144
145 /*!
146   Custom handler for leave event
147 */
148 void QtxIntSpinBox::leaveEvent( QEvent* e )
149 {
150   if ( !myCleared )
151     QSpinBox::leaveEvent( e );
152 }
153
154 /*!
155   Custom handler for wheel event
156 */
157 void QtxIntSpinBox::wheelEvent( QWheelEvent* e )
158 {
159   if ( !isEnabled() )
160     return;
161
162   QSpinBox::wheelEvent( e );
163   updateDisplay();
164 }
165
166 /*!
167   SLOT: called if text is changed
168 */
169 void QtxIntSpinBox::onTextChanged( const QString& )
170 {
171   if ( !myBlocked )
172     myCleared = false;
173 }