Salome HOME
no message
[modules/gui.git] / src / Qtx / QtxComboBox.cxx
1 // File:      QtxComboBox.cxx
2 // Author:    Sergey TELKOV
3
4 #include "QtxComboBox.h"
5
6 #include <qpixmap.h>
7 #include <qlineedit.h>
8 #include <qvaluelist.h>
9
10 QtxComboBox::QtxComboBox( QWidget* parent, const char* name )
11 : QComboBox( parent, name ),
12 myCleared( false )
13 {
14     connect( this, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
15     connect( this, SIGNAL( activated( const QString& ) ), this, SLOT( onActivated( const QString& ) ) );
16 }
17
18 QtxComboBox::QtxComboBox( bool rw, QWidget* parent, const char* name )
19 : QComboBox( rw, parent, name ),
20 myCleared( false )
21 {
22     connect( this, SIGNAL( activated( int ) ), this, SLOT( onActivated( int ) ) );
23     connect( this, SIGNAL( activated( const QString& ) ), this, SLOT( onActivated( const QString& ) ) );
24 }
25
26 QtxComboBox::~QtxComboBox()
27 {
28 }
29
30 bool QtxComboBox::isCleared() const
31 {
32     return myCleared;
33 }
34
35 void QtxComboBox::setCleared( const bool isClear )
36 {
37     if ( myCleared == isClear )
38         return;
39     
40     myCleared = isClear;
41     
42     if ( editable() )
43     {
44         if ( myCleared )
45             lineEdit()->setText( "" );
46         else
47             lineEdit()->setText( text( currentItem() ) );
48     }
49     
50     update();
51 }
52
53 void QtxComboBox::setCurrentItem( int idx )
54 {
55     if ( idx < 0 || idx >= count() )
56         return;
57     
58     myCleared = false;
59     QComboBox::setCurrentItem( idx );
60 }
61
62 void QtxComboBox::setCurrentText( const QString& txt )
63 {
64     myCleared = false;
65 #if QT_VER < 3
66     int i = -1;
67     for ( int j = 0; j < count() && i == -1; j++ )
68         if ( text( j ) == txt )
69             i = j;
70     if ( i >= 0 && i < count() )
71         setCurrentItem( i );
72     else if ( editable() )
73         lineEdit()->setText( txt );
74     else
75         changeItem( txt, currentItem() );
76 #else
77     QComboBox::setCurrentText( txt );
78 #endif
79 }
80
81 int QtxComboBox::currentId() const
82 {
83     return id( currentItem() );
84 }
85
86 void QtxComboBox::setCurrentId( int num )
87 {
88     setCurrentItem( index( num ) );
89 }
90
91 void QtxComboBox::paintEvent( QPaintEvent* e )
92 {
93     if ( !count() || !myCleared || editable() )
94         QComboBox::paintEvent( e );
95     else
96         paintClear( e );
97 }
98
99 void QtxComboBox::onActivated( int idx )
100 {
101     resetClear();
102     
103     if ( myIndexId.contains( idx ) )
104         emit activatedId( myIndexId[idx] );
105 }
106
107 void QtxComboBox::onActivated( const QString& )
108 {
109     resetClear();
110 }
111
112 void QtxComboBox::resetClear()
113 {
114     if ( !myCleared )
115         return;
116     
117     myCleared = false;
118     update();
119 }
120
121 void QtxComboBox::paintClear( QPaintEvent* e )
122 {
123     int curIndex = currentItem();
124     QString curText = text( curIndex );
125     
126     QPixmap curPix;
127     if ( pixmap( curIndex ) )
128         curPix = *pixmap( curIndex );
129     
130     bool upd = isUpdatesEnabled();
131     setUpdatesEnabled( false );
132     
133     changeItem( "", curIndex );
134     QComboBox::paintEvent( e );
135     
136     if ( curPix.isNull() )
137         changeItem( curText, curIndex );
138     else
139         changeItem( curPix, curText, curIndex );
140     
141     setUpdatesEnabled( upd );
142 }
143
144 int QtxComboBox::id( const int idx ) const
145 {
146     int id = -1;
147     if ( myIndexId.contains( idx ) )
148         id = myIndexId[idx];
149     return id;
150 }
151
152 int QtxComboBox::index( const int id ) const
153 {
154     int idx = -1;
155     for ( IndexIdMap::ConstIterator it = myIndexId.begin();
156     it != myIndexId.end() && idx == -1; ++it )
157         if ( it.data() == id )
158             idx = it.key();
159         return idx;
160 }