• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

kcombobox.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002 
00003    Copyright (c) 2000,2001 Dawit Alemayehu <adawit@kde.org>
00004    Copyright (c) 2000,2001 Carsten Pfeiffer <pfeiffer@kde.org>
00005    Copyright (c) 2000 Stefan Schimanski <1Stein@gmx.de>
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License (LGPL) as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public License
00018    along with this library; see the file COPYING.LIB.  If not, write to
00019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020    Boston, MA 02110-1301, USA.
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     // Permanently set some parameters in the parent object.
00078     QComboBox::setAutoCompletion( false );
00079 
00080     // Enable context menu by default if widget
00081     // is editable.
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 // read-only combo completion
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     // hack for the fact that QWidgetAction does not sync all its created widgets
00181     // to its enabled state, just QToolButtons (Qt 4.4.3)
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 // QWidget::create() turns off mouse-Tracking which would break auto-hiding
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     // Not necessary anymore
00267     QComboBox::wheelEvent( ev );
00268 }
00269 
00270 QSize KComboBox::minimumSizeHint() const
00271 {
00272     QSize size = QComboBox::minimumSizeHint();
00273     if (isEditable() && d->klineEdit) {
00274         // if it's a KLineEdit and it's editable add the clear button size
00275         // to the minimum size hint, otherwise looks ugly because the
00276         // clear button will cover the last 2/3 letters of the biggest entry
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         // uic generates code that creates a read-only KComboBox and then
00292         // calls combo->setEditable( true ), which causes QComboBox to set up
00293         // a dumb QLineEdit instead of our nice KLineEdit.
00294         // As some KComboBox features rely on the KLineEdit, we reject
00295         // this order here.
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     // Connect the returnPressed signal for both Q[K]LineEdits'
00311     if (edit)
00312         connect( edit, SIGNAL( returnPressed() ), SIGNAL( returnPressed() ));
00313 
00314     if ( d->klineEdit )
00315     {
00316         // someone calling KComboBox::setEditable( false ) destroys our
00317         // lineedit without us noticing. And KCompletionBase::delegate would
00318         // be a dangling pointer then, so prevent that. Note: only do this
00319         // when it is a KLineEdit!
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     // yes, we need those ugly casts due to the multiple inheritance
00381     // sender() is guaranteed to be a KLineEdit (see the connect() to the
00382     // destroyed() signal
00383     const KCompletionBase *base = static_cast<const KCompletionBase*>( static_cast<const KLineEdit*>( sender() ));
00384 
00385     // is it our delegate, that is destroyed?
00386     if ( base == delegate() )
00387         setDelegate( 0L );
00388 }
00389 
00390 void KComboBox::setEditable(bool editable)
00391 {
00392     if (editable) {
00393         // Create a KLineEdit instead of a QLineEdit
00394         // Compared to QComboBox::setEditable, we might be missing the SH_ComboBox_Popup code though...
00395         // If a style needs this, then we'll need to call QComboBox::setEditable and then setLineEdit again
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"

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal