Salome HOME
Some minor improovements for spin boxes (update values before getting)
[modules/gui.git] / src / Qtx / QtxIntSpinBox.cxx
1 // File:      QtxIntSpinBox.cxx
2 // Author:    Sergey TELKOV
3
4 #include "QtxIntSpinBox.h"
5
6 #include <qlineedit.h>
7 #include <qapplication.h>
8
9 QtxIntSpinBox::QtxIntSpinBox( QWidget* parent, const char* name )
10 : QSpinBox( parent, name ),
11 myCleared( false ),
12 myBlocked( false )
13 {
14   connect( editor(), SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
15 }
16
17 QtxIntSpinBox::QtxIntSpinBox( int min, int max, int step, QWidget* parent, const char* name )
18 : QSpinBox( min, max, step, parent, name ),
19 myCleared( false ),
20 myBlocked( false )
21 {
22   connect( editor(), SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
23 }
24
25 QtxIntSpinBox::~QtxIntSpinBox()
26 {
27 }
28
29 bool QtxIntSpinBox::isCleared() const
30 {
31   return myCleared;
32 }
33
34 void QtxIntSpinBox::setCleared( const bool on )
35 {
36   if ( myCleared == on )
37     return;
38     
39   myCleared = on;
40   updateDisplay();
41 }
42
43 void QtxIntSpinBox::setValue( int value )
44 {
45   myCleared = false;
46
47   QSpinBox::setValue( value );
48 }
49
50 bool QtxIntSpinBox::eventFilter( QObject* o, QEvent* e )
51 {
52   if ( !myCleared || o != editor() || !editor()->text().stripWhiteSpace().isEmpty() )
53     return QSpinBox::eventFilter( o, e );
54
55   if ( e->type() == QEvent::FocusOut || e->type() == QEvent::Leave || e->type() == QEvent::Hide )
56     return false;
57
58   if ( e->type() == QEvent::KeyPress &&
59              ( ((QKeyEvent*)e)->key() == Key_Tab || ((QKeyEvent*)e)->key() == Key_BackTab ) )
60   {
61     QApplication::sendEvent( this, e );
62     return true;
63   }
64
65   return QSpinBox::eventFilter( o, e );
66 }
67
68 void QtxIntSpinBox::interpretText()
69 {
70   myCleared = false;
71
72   QSpinBox::interpretText();
73 }
74
75 void QtxIntSpinBox::updateDisplay()
76 {
77   if ( myBlocked )
78     return;
79
80   bool block = myBlocked;
81   myBlocked = true;
82
83   QSpinBox::updateDisplay();
84
85   if ( myCleared )
86     editor()->clear();
87   else
88   {
89     if ( editor()->text() == specialValueText() )
90       editor()->selectAll();
91     else
92       editor()->setSelection( prefix().length(), editor()->text().length() - prefix().length() - suffix().length() );
93   }
94
95   myBlocked = block;
96 }
97
98 void QtxIntSpinBox::leaveEvent( QEvent* e )
99 {
100   if ( !myCleared )
101     QSpinBox::leaveEvent( e );
102 }
103
104 void QtxIntSpinBox::wheelEvent( QWheelEvent* e )
105 {
106   if ( !isEnabled() )
107     return;
108
109   QSpinBox::wheelEvent( e );
110   updateDisplay();
111 }
112
113 void QtxIntSpinBox::onTextChanged( const QString& )
114 {
115   if ( !myBlocked )
116     myCleared = false;
117 }