]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQComboBox.cc
restart qt4 porting
[duncan/yast2-qt4.git] / src / YQComboBox.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQComboBox.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #define SEND_SELECTION_CHANGED_EVENT 0
21
22 #include <qstring.h>
23 #include <qlabel.h>
24 #include <qcombobox.h>
25 #include <qlineedit.h>
26 #include <qpixmap.h>
27 #define y2log_component "qt-ui"
28 #include <ycp/y2log.h>
29
30 #include "utf8.h"
31 #include "YQUI.h"
32 #include "YEvent.h"
33 #include "QY2CharValidator.h"
34 #include "YQComboBox.h"
35 #include "YQSignalBlocker.h"
36 #include "YQWidgetCaption.h"
37
38
39
40 YQComboBox::YQComboBox( YWidget *       parent,
41                         const string &  label,
42                         bool            editable )
43     : QVBox( (QWidget *) parent->widgetRep() )
44     , YComboBox( parent, label, editable )
45     , _validator(0)
46 {
47     setWidgetRep( this );
48     setSpacing( YQWidgetSpacing );
49     setMargin ( YQWidgetMargin  );
50
51     _caption = new YQWidgetCaption( this, label );
52     YUI_CHECK_NEW( _caption );
53     
54     _qt_comboBox = new QComboBox( editable, this );
55     YUI_CHECK_NEW( _caption );
56     
57     _caption->setBuddy( _qt_comboBox );
58
59 #if SEND_SELECTION_CHANGED_EVENT
60     connect( _qt_comboBox,      SIGNAL( highlighted (int) ),
61              this,              SLOT  ( slotSelected(int) ) );
62 #endif
63
64     connect( _qt_comboBox,      SIGNAL( activated  ( const QString & ) ),
65              this,              SLOT  ( textChanged( const QString & ) ) );
66
67     connect( _qt_comboBox,      SIGNAL( textChanged( const QString & ) ),
68              this,              SLOT  ( textChanged( const QString & ) ) );
69 }
70
71
72 YQComboBox::~YQComboBox()
73 {
74     // NOP
75 }
76
77
78 string YQComboBox::text()
79 {
80     return toUTF8( _qt_comboBox->currentText() );
81 }
82
83
84 void YQComboBox::setText( const string & newValue )
85 {
86     QString text = fromUTF8( newValue );
87
88     if ( isValidText( text ) )
89     {
90         YQSignalBlocker sigBlocker( _qt_comboBox );
91         _qt_comboBox->setCurrentText( text );
92     }
93     else
94     {
95         y2error( "%s \"%s\": Rejecting invalid value \"%s\"",
96                  widgetClass(), debugLabel().c_str(), newValue.c_str() );
97     }
98 }
99
100
101 void YQComboBox::addItem( YItem * item )
102 {
103     YComboBox::addItem( item );
104     QPixmap icon;
105     
106     if ( item->hasIconName() )
107     {
108         string iconName = iconFullPath( item );
109         icon = QPixmap( iconName.c_str() );
110
111         if ( icon.isNull() )
112             y2warning( "Can't load icon %s", iconName.c_str() );
113     }
114
115     if ( icon.isNull() )
116         _qt_comboBox->insertItem( fromUTF8( item->label() ) );
117     else
118         _qt_comboBox->insertItem( icon, fromUTF8( item->label() ) );
119
120     if ( item->selected() )
121     {
122         YQSignalBlocker sigBlocker( _qt_comboBox );
123         setText( item->label() );
124     }
125 }
126
127
128 void YQComboBox::deleteAllItems()
129 {
130     YQSignalBlocker sigBlocker( _qt_comboBox );
131     
132     _qt_comboBox->clear();
133     YComboBox::deleteAllItems();
134 }
135
136
137 void YQComboBox::setLabel( const string & label )
138 {
139     _caption->setText( label );
140     YComboBox::setLabel( label );
141 }
142
143
144 void YQComboBox::setValidChars( const string & newValidChars )
145 {
146     if ( ! _qt_comboBox->editable() )
147     {
148         y2warning( "Setting ValidChars is useless on a combo box that isn't editable! (%s)",
149                    debugLabel().c_str() );
150         return;
151     }
152
153     if ( _validator )
154     {
155         _validator->setValidChars( fromUTF8( newValidChars ) );
156     }
157     else
158     {
159         _validator = new QY2CharValidator( fromUTF8( newValidChars ), this );
160         _qt_comboBox->setValidator( _validator );
161
162         // No need to delete the validator in the destructor - Qt will take
163         // care of that since it's a QObject with a parent!
164     }
165
166     if ( ! isValidText( _qt_comboBox->currentText() ) )
167     {
168         y2error( "Old value \"%s\" of %s \"%s\" invalid according to ValidChars \"%s\" - deleting",
169                  (const char *) _qt_comboBox->currentText(),
170                  widgetClass(), debugLabel().c_str(),
171                  newValidChars.c_str() );
172         _qt_comboBox->setCurrentText( "" );
173     }
174
175     YComboBox::setValidChars( newValidChars );
176 }
177
178
179 bool YQComboBox::isValidText( const QString & txt ) const
180 {
181     if ( ! _validator )
182         return true;
183
184     int pos = 0;
185     QString text( txt );        // need a non-const QString &
186
187     return _validator->validate( text, pos ) == QValidator::Acceptable;
188 }
189
190
191 void YQComboBox::slotSelected( int i )
192 {
193     if ( notify() )
194     {
195         if ( ! YQUI::ui()->eventPendingFor( this ) )
196         {
197             // Avoid overwriting a (more important) ValueChanged event with a SelectionChanged event
198
199             YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::SelectionChanged ) );
200         }
201     }
202 }
203
204
205 void YQComboBox::textChanged( const QString & new_text )
206 {
207     if ( notify() )
208         YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
209 }
210
211
212 void YQComboBox::setInputMaxLength( int len )
213 {
214     _qt_comboBox->lineEdit()->setMaxLength( len );
215     YComboBox::setInputMaxLength( len );
216 }
217
218
219 int YQComboBox::preferredWidth()
220 {
221     return sizeHint().width();
222 }
223
224
225 int YQComboBox::preferredHeight()
226 {
227     return sizeHint().height();
228 }
229
230
231 void YQComboBox::setSize( int newWidth, int newHeight )
232 {
233     resize( newWidth, newHeight );
234 }
235
236
237 void YQComboBox::setEnabled( bool enabled )
238 {
239     _caption->setEnabled( enabled );
240     _qt_comboBox->setEnabled( enabled );
241     YWidget::setEnabled( enabled );
242 }
243
244
245 bool YQComboBox::setKeyboardFocus()
246 {
247     _qt_comboBox->setFocus();
248
249     return true;
250 }
251
252
253 #include "YQComboBox.moc"