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