Salome HOME
Copyrights update
[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/
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 QtxIntSpinBox::QtxIntSpinBox( QWidget* parent, const char* name )
28 : QSpinBox( parent, name ),
29 myCleared( false ),
30 myBlocked( false )
31 {
32   connect( editor(), SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
33 }
34
35 QtxIntSpinBox::QtxIntSpinBox( int min, int max, int step, QWidget* parent, const char* name )
36 : QSpinBox( min, max, step, parent, name ),
37 myCleared( false ),
38 myBlocked( false )
39 {
40   connect( editor(), SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
41 }
42
43 QtxIntSpinBox::~QtxIntSpinBox()
44 {
45 }
46
47 bool QtxIntSpinBox::isCleared() const
48 {
49   return myCleared;
50 }
51
52 void QtxIntSpinBox::setCleared( const bool on )
53 {
54   if ( myCleared == on )
55     return;
56     
57   myCleared = on;
58   updateDisplay();
59 }
60
61 void QtxIntSpinBox::setValue( int value )
62 {
63   myCleared = false;
64
65   QSpinBox::setValue( value );
66 }
67
68 bool QtxIntSpinBox::eventFilter( QObject* o, QEvent* e )
69 {
70   if ( !myCleared || o != editor() || !editor()->text().stripWhiteSpace().isEmpty() )
71     return QSpinBox::eventFilter( o, e );
72
73   if ( e->type() == QEvent::FocusOut || e->type() == QEvent::Leave || e->type() == QEvent::Hide )
74     return false;
75
76   if ( e->type() == QEvent::KeyPress &&
77              ( ((QKeyEvent*)e)->key() == Key_Tab || ((QKeyEvent*)e)->key() == Key_BackTab ) )
78   {
79     QApplication::sendEvent( this, e );
80     return true;
81   }
82
83   return QSpinBox::eventFilter( o, e );
84 }
85
86 void QtxIntSpinBox::interpretText()
87 {
88   myCleared = false;
89
90   QSpinBox::interpretText();
91 }
92
93 void QtxIntSpinBox::updateDisplay()
94 {
95   if ( myBlocked )
96     return;
97
98   bool block = myBlocked;
99   myBlocked = true;
100
101   QSpinBox::updateDisplay();
102
103   if ( myCleared )
104     editor()->clear();
105   else if ( editor()->hasFocus() )
106   {
107     if ( editor()->text() == specialValueText() )
108       editor()->selectAll();
109     else
110       editor()->setSelection( prefix().length(), editor()->text().length() - prefix().length() - suffix().length() );
111   }
112
113   myBlocked = block;
114 }
115
116 void QtxIntSpinBox::leaveEvent( QEvent* e )
117 {
118   if ( !myCleared )
119     QSpinBox::leaveEvent( e );
120 }
121
122 void QtxIntSpinBox::wheelEvent( QWheelEvent* e )
123 {
124   if ( !isEnabled() )
125     return;
126
127   QSpinBox::wheelEvent( e );
128   updateDisplay();
129 }
130
131 void QtxIntSpinBox::onTextChanged( const QString& )
132 {
133   if ( !myBlocked )
134     myCleared = false;
135 }