Salome HOME
Initial version
[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::insertStringList( const QIntList& ids, const QStringList& list, int index )
92 {
93 }
94 */
95 void QtxComboBox::insertItem( const int id, const QString& t, int index )
96 {
97 }
98
99 void QtxComboBox::insertItem( const int id, const QPixmap& pixmap, int index )
100 {
101 }
102
103 void QtxComboBox::insertItem( const int id, const QPixmap& pixmap, const QString& text, int index )
104 {
105 }
106
107 void QtxComboBox::paintEvent( QPaintEvent* e )
108 {
109     if ( !count() || !myCleared || editable() )
110         QComboBox::paintEvent( e );
111     else
112         paintClear( e );
113 }
114
115 void QtxComboBox::onActivated( int idx )
116 {
117     resetClear();
118     
119     if ( myIndexId.contains( idx ) )
120         emit activatedId( myIndexId[idx] );
121 }
122
123 void QtxComboBox::onActivated( const QString& )
124 {
125     resetClear();
126 }
127
128 void QtxComboBox::resetClear()
129 {
130     if ( !myCleared )
131         return;
132     
133     myCleared = false;
134     update();
135 }
136
137 void QtxComboBox::paintClear( QPaintEvent* e )
138 {
139     int curIndex = currentItem();
140     QString curText = text( curIndex );
141     
142     QPixmap curPix;
143     if ( pixmap( curIndex ) )
144         curPix = *pixmap( curIndex );
145     
146     bool upd = isUpdatesEnabled();
147     setUpdatesEnabled( false );
148     
149     changeItem( "", curIndex );
150     QComboBox::paintEvent( e );
151     
152     if ( curPix.isNull() )
153         changeItem( curText, curIndex );
154     else
155         changeItem( curPix, curText, curIndex );
156     
157     setUpdatesEnabled( upd );
158 }
159
160 int QtxComboBox::id( const int idx ) const
161 {
162     int id = -1;
163     if ( myIndexId.contains( idx ) )
164         id = myIndexId[idx];
165     return id;
166 }
167
168 int QtxComboBox::index( const int id ) const
169 {
170     int idx = -1;
171     for ( IndexIdMap::ConstIterator it = myIndexId.begin();
172     it != myIndexId.end() && idx == -1; ++it )
173         if ( it.data() == id )
174             idx = it.key();
175         return idx;
176 }