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