00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "kcombobox.h"
00024
00025 #include <QtGui/QClipboard>
00026 #include <QtGui/QLineEdit>
00027 #include <QtGui/QMenu>
00028 #include <QtGui/QApplication>
00029 #include <QtGui/QActionEvent>
00030
00031 #include <kselectaction.h>
00032 #include <kcompletionbox.h>
00033 #include <kcursor.h>
00034 #include <kiconloader.h>
00035 #include <kicontheme.h>
00036 #include <klineedit.h>
00037 #include <klocale.h>
00038 #include <kurl.h>
00039 #include <kicon.h>
00040
00041 #include <kdebug.h>
00042
00043 class KComboBox::KComboBoxPrivate
00044 {
00045 public:
00046 KComboBoxPrivate() : klineEdit(0L), trapReturnKey(false)
00047 {
00048 }
00049 ~KComboBoxPrivate()
00050 {
00051 }
00052
00053 KLineEdit *klineEdit;
00054 bool trapReturnKey;
00055 };
00056
00057 KComboBox::KComboBox( QWidget *parent )
00058 : QComboBox( parent ), d(new KComboBoxPrivate)
00059 {
00060 init();
00061 }
00062
00063 KComboBox::KComboBox( bool rw, QWidget *parent )
00064 : QComboBox( parent ), d(new KComboBoxPrivate)
00065 {
00066 init();
00067 setEditable( rw );
00068 }
00069
00070 KComboBox::~KComboBox()
00071 {
00072 delete d;
00073 }
00074
00075 void KComboBox::init()
00076 {
00077
00078 QComboBox::setAutoCompletion( false );
00079
00080
00081
00082 setContextMenuEnabled( true );
00083 }
00084
00085
00086 bool KComboBox::contains( const QString& _text ) const
00087 {
00088 if ( _text.isEmpty() )
00089 return false;
00090
00091 const int itemCount = count();
00092 for (int i = 0; i < itemCount; ++i )
00093 {
00094 if ( itemText(i) == _text )
00095 return true;
00096 }
00097 return false;
00098 }
00099
00100 int KComboBox::cursorPosition() const
00101 {
00102 return ( lineEdit() ) ? lineEdit()->cursorPosition() : -1;
00103 }
00104
00105 void KComboBox::setAutoCompletion( bool autocomplete )
00106 {
00107 if ( d->klineEdit )
00108 {
00109 if ( autocomplete )
00110 {
00111 d->klineEdit->setCompletionMode( KGlobalSettings::CompletionAuto );
00112 setCompletionMode( KGlobalSettings::CompletionAuto );
00113 }
00114 else
00115 {
00116 d->klineEdit->setCompletionMode( KGlobalSettings::completionMode() );
00117 setCompletionMode( KGlobalSettings::completionMode() );
00118 }
00119 }
00120 }
00121
00122 bool KComboBox::autoCompletion() const
00123 {
00124 return completionMode() == KGlobalSettings::CompletionAuto;
00125 }
00126
00127 void KComboBox::setContextMenuEnabled( bool showMenu )
00128 {
00129 if( d->klineEdit )
00130 d->klineEdit->setContextMenuEnabled( showMenu );
00131 }
00132
00133
00134 void KComboBox::setUrlDropsEnabled( bool enable )
00135 {
00136 if ( d->klineEdit )
00137 d->klineEdit->setUrlDropsEnabled( enable );
00138 }
00139
00140 bool KComboBox::urlDropsEnabled() const
00141 {
00142 return d->klineEdit && d->klineEdit->urlDropsEnabled();
00143 }
00144
00145
00146 void KComboBox::setCompletedText( const QString& text, bool marked )
00147 {
00148 if ( d->klineEdit )
00149 d->klineEdit->setCompletedText( text, marked );
00150 }
00151
00152 void KComboBox::setCompletedText( const QString& text )
00153 {
00154 if ( d->klineEdit )
00155 d->klineEdit->setCompletedText( text );
00156 }
00157
00158 void KComboBox::makeCompletion( const QString& text )
00159 {
00160 if( d->klineEdit )
00161 d->klineEdit->makeCompletion( text );
00162
00163 else
00164 {
00165 if( text.isNull() || !view() )
00166 return;
00167
00168 view()->keyboardSearch(text);
00169 }
00170 }
00171
00172 void KComboBox::rotateText( KCompletionBase::KeyBindingType type )
00173 {
00174 if ( d->klineEdit )
00175 d->klineEdit->rotateText( type );
00176 }
00177
00178 bool KComboBox::eventFilter( QObject* o, QEvent* ev )
00179 {
00180
00181
00182 if( ev->type() == QEvent::ActionChanged )
00183 {
00184 KSelectAction* selectAction = qobject_cast<KSelectAction*>( o );
00185 if ( selectAction )
00186 setEnabled( selectAction->isEnabled() );
00187 }
00188
00189 return QComboBox::eventFilter( o, ev );
00190 }
00191
00192 void KComboBox::setTrapReturnKey( bool grab )
00193 {
00194 d->trapReturnKey = grab;
00195
00196 if ( d->klineEdit )
00197 d->klineEdit->setTrapReturnKey( grab );
00198 else
00199 qWarning("KComboBox::setTrapReturnKey not supported with a non-KLineEdit.");
00200 }
00201
00202 bool KComboBox::trapReturnKey() const
00203 {
00204 return d->trapReturnKey;
00205 }
00206
00207
00208 void KComboBox::setEditUrl( const KUrl& url )
00209 {
00210 QComboBox::setEditText( url.prettyUrl() );
00211 }
00212
00213 void KComboBox::addUrl( const KUrl& url )
00214 {
00215 QComboBox::addItem( url.prettyUrl() );
00216 }
00217
00218 void KComboBox::addUrl( const QIcon& icon, const KUrl& url )
00219 {
00220 QComboBox::addItem( icon, url.prettyUrl() );
00221 }
00222
00223 void KComboBox::insertUrl( int index, const KUrl& url )
00224 {
00225 QComboBox::insertItem( index, url.prettyUrl() );
00226 }
00227
00228 void KComboBox::insertUrl( int index, const QIcon& icon, const KUrl& url )
00229 {
00230 QComboBox::insertItem( index, icon, url.prettyUrl() );
00231 }
00232
00233 void KComboBox::changeUrl( int index, const KUrl& url )
00234 {
00235 QComboBox::setItemText( index, url.prettyUrl() );
00236 }
00237
00238 void KComboBox::changeUrl( int index, const QIcon& icon, const KUrl& url )
00239 {
00240 QComboBox::setItemIcon( index, icon );
00241 QComboBox::setItemText( index, url.prettyUrl() );
00242 }
00243
00244 void KComboBox::setCompletedItems( const QStringList& items, bool autosubject )
00245 {
00246 if ( d->klineEdit )
00247 d->klineEdit->setCompletedItems( items, autosubject );
00248 }
00249
00250 KCompletionBox * KComboBox::completionBox( bool create )
00251 {
00252 if ( d->klineEdit )
00253 return d->klineEdit->completionBox( create );
00254 return 0;
00255 }
00256
00257
00258 void KComboBox::create( WId id, bool initializeWindow, bool destroyOldWindow )
00259 {
00260 QComboBox::create( id, initializeWindow, destroyOldWindow );
00261 KCursor::setAutoHideCursor( lineEdit(), true, true );
00262 }
00263
00264 void KComboBox::wheelEvent( QWheelEvent *ev )
00265 {
00266
00267 QComboBox::wheelEvent( ev );
00268 }
00269
00270 QSize KComboBox::minimumSizeHint() const
00271 {
00272 QSize size = QComboBox::minimumSizeHint();
00273 if (isEditable() && d->klineEdit) {
00274
00275
00276
00277 QSize bs = d->klineEdit->clearButtonUsedSize();
00278 if (bs.isValid()) {
00279 size.rwidth() += bs.width();
00280 size.rheight() = qMax(size.height(), bs.height());
00281 }
00282 }
00283 return size;
00284 }
00285
00286 void KComboBox::setLineEdit( QLineEdit *edit )
00287 {
00288 if ( !isEditable() && edit &&
00289 !qstrcmp( edit->metaObject()->className(), "QLineEdit" ) )
00290 {
00291
00292
00293
00294
00295
00296 delete edit;
00297 KLineEdit* kedit = new KLineEdit( this );
00298
00299 if ( isEditable() ) {
00300 kedit->setClearButtonShown( true );
00301 }
00302
00303 edit = kedit;
00304 }
00305
00306 QComboBox::setLineEdit( edit );
00307 d->klineEdit = qobject_cast<KLineEdit*>( edit );
00308 setDelegate( d->klineEdit );
00309
00310
00311 if (edit)
00312 connect( edit, SIGNAL( returnPressed() ), SIGNAL( returnPressed() ));
00313
00314 if ( d->klineEdit )
00315 {
00316
00317
00318
00319
00320 connect( edit, SIGNAL( destroyed() ), SLOT( lineEditDeleted() ));
00321
00322 connect( d->klineEdit, SIGNAL( returnPressed( const QString& )),
00323 SIGNAL( returnPressed( const QString& ) ));
00324
00325 connect( d->klineEdit, SIGNAL( completion( const QString& )),
00326 SIGNAL( completion( const QString& )) );
00327
00328 connect( d->klineEdit, SIGNAL( substringCompletion( const QString& )),
00329 SIGNAL( substringCompletion( const QString& )) );
00330
00331 connect( d->klineEdit,
00332 SIGNAL( textRotation( KCompletionBase::KeyBindingType )),
00333 SIGNAL( textRotation( KCompletionBase::KeyBindingType )) );
00334
00335 connect( d->klineEdit,
00336 SIGNAL( completionModeChanged( KGlobalSettings::Completion )),
00337 SIGNAL( completionModeChanged( KGlobalSettings::Completion)));
00338
00339 connect( d->klineEdit,
00340 SIGNAL( aboutToShowContextMenu( QMenu * )),
00341 SIGNAL( aboutToShowContextMenu( QMenu * )) );
00342
00343 connect( d->klineEdit,
00344 SIGNAL( completionBoxActivated( const QString& )),
00345 SIGNAL( activated( const QString& )) );
00346
00347 d->klineEdit->setTrapReturnKey( d->trapReturnKey );
00348 }
00349 }
00350
00351 void KComboBox::setCurrentItem( const QString& item, bool insert, int index )
00352 {
00353 int sel = -1;
00354
00355 const int itemCount = count();
00356 for (int i = 0; i < itemCount; ++i)
00357 {
00358 if (itemText(i) == item)
00359 {
00360 sel = i;
00361 break;
00362 }
00363 }
00364
00365 if (sel == -1 && insert)
00366 {
00367 if (index >= 0) {
00368 insertItem(index, item);
00369 sel = index;
00370 } else {
00371 addItem(item);
00372 sel = count() - 1;
00373 }
00374 }
00375 setCurrentIndex(sel);
00376 }
00377
00378 void KComboBox::lineEditDeleted()
00379 {
00380
00381
00382
00383 const KCompletionBase *base = static_cast<const KCompletionBase*>( static_cast<const KLineEdit*>( sender() ));
00384
00385
00386 if ( base == delegate() )
00387 setDelegate( 0L );
00388 }
00389
00390 void KComboBox::setEditable(bool editable)
00391 {
00392 if (editable) {
00393
00394
00395
00396 KLineEdit *edit = new KLineEdit( this );
00397 edit->setClearButtonShown( true );
00398 setLineEdit( edit );
00399 } else {
00400 QComboBox::setEditable(editable);
00401 }
00402 }
00403
00404 #include "kcombobox.moc"