• Skip to content
  • Skip to link menu
KDE 4.1 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 <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     // Permanently set some parameters in the parent object.
00076     QComboBox::setAutoCompletion( false );
00077 
00078     // Enable context menu by default if widget
00079     // is editable.
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 // read-only combo completion
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 // not needed anymore
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 // QWidget::create() turns off mouse-Tracking which would break auto-hiding
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     // Not necessary anymore
00255     QComboBox::wheelEvent( ev );
00256 }
00257 
00258 QSize KComboBox::minimumSizeHint() const
00259 {
00260     QSize size = QComboBox::minimumSizeHint();
00261     if (isEditable() && d->klineEdit) {
00262         // if it's a KLineEdit and it's editable add the clear button size
00263         // to the minimum size hint, otherwise looks ugly because the
00264         // clear button will cover the last 2/3 letters of the biggest entry
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         // uic generates code that creates a read-only KComboBox and then
00280         // calls combo->setEditable( true ), which causes QComboBox to set up
00281         // a dumb QLineEdit instead of our nice KLineEdit.
00282         // As some KComboBox features rely on the KLineEdit, we reject
00283         // this order here.
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     // Connect the returnPressed signal for both Q[K]LineEdits'
00299     if (edit)
00300         connect( edit, SIGNAL( returnPressed() ), SIGNAL( returnPressed() ));
00301 
00302     if ( d->klineEdit )
00303     {
00304         // someone calling KComboBox::setEditable( false ) destroys our
00305         // lineedit without us noticing. And KCompletionBase::delegate would
00306         // be a dangling pointer then, so prevent that. Note: only do this
00307         // when it is a KLineEdit!
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     // yes, we need those ugly casts due to the multiple inheritance
00367     // sender() is guaranteed to be a KLineEdit (see the connect() to the
00368     // destroyed() signal
00369     const KCompletionBase *base = static_cast<const KCompletionBase*>( static_cast<const KLineEdit*>( sender() ));
00370 
00371     // is it our delegate, that is destroyed?
00372     if ( base == delegate() )
00373         setDelegate( 0L );
00374 }
00375 
00376 void KComboBox::setEditable(bool editable)
00377 {
00378     if (editable) {
00379         // Create a KLineEdit instead of a QLineEdit
00380         // Compared to QComboBox::setEditable, we might be missing the SH_ComboBox_Popup code though...
00381         // If a style needs this, then we'll need to call QComboBox::setEditable and then setLineEdit again
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"

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
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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