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