kdeui Library API Documentation

klineedit.cpp

00001 /* This file is part of the KDE libraries
00002 
00003    Copyright (C) 1997 Sven Radej (sven.radej@iname.com)
00004    Copyright (c) 1999 Patrick Ward <PAT_WARD@HP-USA-om5.om.hp.com>
00005    Copyright (c) 1999 Preston Brown <pbrown@kde.org>
00006 
00007    Re-designed for KDE 2.x by
00008    Copyright (c) 2000, 2001 Dawit Alemayehu <adawit@kde.org>
00009    Copyright (c) 2000, 2001 Carsten Pfeiffer <pfeiffer@kde.org>
00010 
00011    This library is free software; you can redistribute it and/or
00012    modify it under the terms of the GNU Lesser General Public
00013    License (LGPL) as published by the Free Software Foundation;
00014    either version 2 of the License, or (at your option) any later
00015    version.
00016 
00017    This library is distributed in the hope that it will be useful,
00018    but WITHOUT ANY WARRANTY; without even the implied warranty of
00019    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020    Lesser General Public License for more details.
00021 
00022    You should have received a copy of the GNU Lesser General Public License
00023    along with this library; see the file COPYING.LIB.  If not, write to
00024    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00025    Boston, MA 02111-1307, USA.
00026 */
00027 
00028 #include <qclipboard.h>
00029 #include <qtimer.h>
00030 #include <qtooltip.h>
00031 #include <kcursor.h>
00032 #include <klocale.h>
00033 #include <kstdaccel.h>
00034 #include <kpopupmenu.h>
00035 #include <kdebug.h>
00036 #include <kcompletionbox.h>
00037 #include <kurl.h>
00038 #include <kurldrag.h>
00039 #include <kiconloader.h>
00040 #include <kapplication.h>
00041 
00042 #include "klineedit.h"
00043 #include "klineedit.moc"
00044 
00045 
00046 class KLineEdit::KLineEditPrivate
00047 {
00048 public:
00049     KLineEditPrivate()
00050     {
00051         completionBox = 0L;
00052         handleURLDrops = true;
00053         grabReturnKeyEvents = false;
00054     }
00055     ~KLineEditPrivate()
00056     {
00057         delete completionBox;
00058     }
00059 
00060     int squeezedEnd;
00061     int squeezedStart;
00062     bool handleURLDrops;
00063     bool grabReturnKeyEvents;
00064     BackgroundMode bgMode;
00065     QString squeezedText;
00066     KCompletionBox *completionBox;
00067 };
00068 
00069 
00070 KLineEdit::KLineEdit( const QString &string, QWidget *parent, const char *name )
00071           :QLineEdit( string, parent, name )
00072 {
00073     init();
00074 }
00075 
00076 KLineEdit::KLineEdit( QWidget *parent, const char *name )
00077           :QLineEdit( parent, name )
00078 {
00079     init();
00080 }
00081 
00082 KLineEdit::~KLineEdit ()
00083 {
00084     delete d;
00085 }
00086 
00087 void KLineEdit::init()
00088 {
00089     d = new KLineEditPrivate;
00090     possibleTripleClick = false;
00091     d->bgMode = backgroundMode ();
00092 
00093     // Enable the context menu by default.
00094     setContextMenuEnabled( true );
00095     KCursor::setAutoHideCursor( this, true, true );
00096     installEventFilter( this );
00097 }
00098 
00099 void KLineEdit::setCompletionMode( KGlobalSettings::Completion mode )
00100 {
00101     KGlobalSettings::Completion oldMode = completionMode();
00102     if ( oldMode != mode && oldMode == KGlobalSettings::CompletionPopup &&
00103          d->completionBox && d->completionBox->isVisible() )
00104         d->completionBox->hide();
00105 
00106     // If the widgets echo mode is not Normal, no completion
00107     // feature will be enabled even if one is requested.
00108     if ( echoMode() != QLineEdit::Normal )
00109         mode = KGlobalSettings::CompletionNone; // Override the request.
00110 
00111     if ( kapp && !kapp->authorize("lineedit_text_completion") )
00112         mode = KGlobalSettings::CompletionNone;
00113     
00114     KCompletionBase::setCompletionMode( mode );
00115 }
00116 
00117 void KLineEdit::setCompletedText( const QString& t, bool marked )
00118 {
00119     QString txt = text();
00120     if ( t != txt )
00121     {
00122         int curpos = marked ? txt.length() : t.length();
00123         validateAndSet( t, curpos, curpos, t.length() );
00124     }
00125 }
00126 
00127 void KLineEdit::setCompletedText( const QString& text )
00128 {
00129     KGlobalSettings::Completion mode = completionMode();
00130     bool marked = ( mode == KGlobalSettings::CompletionAuto ||
00131                     mode == KGlobalSettings::CompletionMan ||
00132                     mode == KGlobalSettings::CompletionPopup );
00133     setCompletedText( text, marked );
00134 }
00135 
00136 void KLineEdit::rotateText( KCompletionBase::KeyBindingType type )
00137 {
00138     KCompletion* comp = compObj();
00139     if ( comp &&
00140        (type == KCompletionBase::PrevCompletionMatch ||
00141         type == KCompletionBase::NextCompletionMatch ) )
00142     {
00143        QString input = (type == KCompletionBase::PrevCompletionMatch) ? comp->previousMatch() : comp->nextMatch();
00144        // Skip rotation if previous/next match is null or the same text
00145        if ( input.isNull() || input == displayText() )
00146             return;
00147        setCompletedText( input, hasSelectedText() );
00148     }
00149 }
00150 
00151 void KLineEdit::makeCompletion( const QString& text )
00152 {
00153     KCompletion *comp = compObj();
00154     if ( !comp )
00155         return;  // No completion object...
00156 
00157     QString match = comp->makeCompletion( text );
00158     KGlobalSettings::Completion mode = completionMode();
00159     if ( mode == KGlobalSettings::CompletionPopup )
00160     {
00161         if ( match.isNull() )
00162         {
00163             if ( d->completionBox ) {
00164                 d->completionBox->hide();
00165                 d->completionBox->clear();
00166             }
00167         }
00168         else
00169             setCompletedItems( comp->allMatches() );
00170     }
00171     else
00172     {
00173         // all other completion modes
00174         // If no match or the same match, simply return without completing.
00175         if ( match.isNull() || match == text )
00176             return;
00177 
00178         setCompletedText( match );
00179     }
00180 }
00181 
00182 void KLineEdit::setReadOnly(bool readOnly)
00183 {
00184     // Do not do anything if nothing changed...
00185     if (readOnly == isReadOnly ())
00186       return;
00187 
00188     QLineEdit::setReadOnly (readOnly);
00189 
00190     if (readOnly)
00191     {
00192         d->bgMode = backgroundMode ();
00193         setBackgroundMode (Qt::PaletteBackground);
00194     }
00195     else
00196     {
00197         if (!d->squeezedText.isEmpty())
00198         {
00199            setText(d->squeezedText);
00200            d->squeezedText = QString::null;
00201         }
00202         setBackgroundMode (d->bgMode);
00203     }
00204 }
00205 
00206 void KLineEdit::setSqueezedText( const QString &text)
00207 {
00208     if (isReadOnly())
00209     {
00210         d->squeezedText = text;
00211         d->squeezedStart = 0;
00212         d->squeezedEnd = 0;
00213         if (isVisible())
00214         {
00215             QResizeEvent ev(size(), size());
00216             resizeEvent(&ev);
00217         }
00218     }
00219     else
00220     {
00221         setText(text);
00222     }
00223 }
00224 
00225 void KLineEdit::copy() const
00226 {
00227    if (!d->squeezedText.isEmpty() && d->squeezedStart)
00228    {
00229       int start, end;
00230       KLineEdit *that = const_cast<KLineEdit *>(this);
00231       if (!that->getSelection(&start, &end))
00232          return;
00233       if (start >= d->squeezedStart+3)
00234          start = start - 3 - d->squeezedStart + d->squeezedEnd;
00235       else if (start > d->squeezedStart)
00236          start = d->squeezedStart;
00237       if (end >= d->squeezedStart+3)
00238          end = end - 3 - d->squeezedStart + d->squeezedEnd;
00239       else if (end > d->squeezedStart)
00240          end = d->squeezedEnd;
00241       if (start == end)
00242          return;
00243       QString t = d->squeezedText;
00244       t = t.mid(start, end - start);
00245       disconnect( QApplication::clipboard(), SIGNAL(selectionChanged()), this, 0);
00246       QApplication::clipboard()->setText( t );
00247       connect( QApplication::clipboard(), SIGNAL(selectionChanged()),
00248      this, SLOT(clipboardChanged()) );
00249    }
00250    else
00251    {
00252       QLineEdit::copy();
00253    }
00254 }
00255 
00256 void KLineEdit::resizeEvent( QResizeEvent * ev )
00257 {
00258     if (!d->squeezedText.isEmpty())
00259     {
00260        d->squeezedStart = 0;
00261        d->squeezedEnd = 0;
00262        QString fullText = d->squeezedText;
00263        QFontMetrics fm(fontMetrics());
00264        int labelWidth = size().width() - 2*frameWidth() - 2;
00265        int textWidth = fm.width(fullText);
00266        if (textWidth > labelWidth) {
00267           // start with the dots only
00268           QString squeezedText = "...";
00269           int squeezedWidth = fm.width(squeezedText);
00270 
00271           // estimate how many letters we can add to the dots on both sides
00272           int letters = fullText.length() * (labelWidth - squeezedWidth) / textWidth / 2;
00273           squeezedText = fullText.left(letters) + "..." + fullText.right(letters);
00274           squeezedWidth = fm.width(squeezedText);
00275 
00276           if (squeezedWidth < labelWidth) {
00277              // we estimated too short
00278              // add letters while text < label
00279              do {
00280                 letters++;
00281                 squeezedText = fullText.left(letters) + "..." + fullText.right(letters);
00282                 squeezedWidth = fm.width(squeezedText);
00283              } while (squeezedWidth < labelWidth);
00284              letters--;
00285              squeezedText = fullText.left(letters) + "..." + fullText.right(letters);
00286           } else if (squeezedWidth > labelWidth) {
00287              // we estimated too long
00288              // remove letters while text > label
00289              do {
00290                letters--;
00291                 squeezedText = fullText.left(letters) + "..." + fullText.right(letters);
00292                 squeezedWidth = fm.width(squeezedText);
00293              } while (squeezedWidth > labelWidth);
00294           }
00295 
00296           if (letters < 5) {
00297              // too few letters added -> we give up squeezing
00298              setText(fullText);
00299           } else {
00300              setText(squeezedText);
00301              d->squeezedStart = letters;
00302              d->squeezedEnd = fullText.length() - letters;
00303           }
00304 
00305           QToolTip::remove( this );
00306           QToolTip::add( this, fullText );
00307 
00308        } else {
00309           setText(fullText);
00310 
00311           QToolTip::remove( this );
00312           QToolTip::hide();
00313        }
00314        setCursorPosition(0);
00315     }
00316     QLineEdit::resizeEvent(ev);
00317 }
00318 
00319 void KLineEdit::keyPressEvent( QKeyEvent *e )
00320 {
00321     // Filter key-events if EchoMode is normal &
00322     // completion mode is not set to CompletionNone
00323     KKey key( e );
00324 
00325     if ( echoMode() == QLineEdit::Normal &&
00326          completionMode() != KGlobalSettings::CompletionNone )
00327     {
00328         KeyBindingMap keys = getKeyBindings();
00329         KGlobalSettings::Completion mode = completionMode();
00330         bool noModifier = (e->state() == NoButton || e->state()== ShiftButton);
00331 
00332         if ( (mode == KGlobalSettings::CompletionAuto ||
00333               mode == KGlobalSettings::CompletionMan) && noModifier )
00334         {
00335             QString keycode = e->text();
00336             if ( !keycode.isEmpty() && keycode.unicode()->isPrint() )
00337             {
00338                 QLineEdit::keyPressEvent ( e );
00339                 QString txt = text();
00340                 int len = txt.length();
00341                 if ( !hasSelectedText() && len && cursorPosition() == len )
00342                 {
00343                     if ( emitSignals() )
00344                         emit completion( txt );
00345                     if ( handleSignals() )
00346                         makeCompletion( txt );
00347                     e->accept();
00348                 }
00349                 return;
00350             }
00351         }
00352 
00353         else if ( mode == KGlobalSettings::CompletionPopup && noModifier &&
00354             !e->text().isEmpty() )
00355         {
00356             QString old_txt = text();
00357             QLineEdit::keyPressEvent ( e );
00358             QString txt = text();
00359             int len = txt.length();
00360             QString keycode = e->text();
00361 
00362 
00363             if ( txt != old_txt && len && cursorPosition() == len &&
00364                  ( (!keycode.isEmpty() && keycode.unicode()->isPrint()) ||
00365                    e->key() == Key_Backspace ) )
00366             {
00367                 if ( emitSignals() )
00368                     emit completion( txt ); // emit when requested...
00369                 if ( handleSignals() )
00370                     makeCompletion( txt );  // handle when requested...
00371                 e->accept();
00372             }
00373             else if (!len && d->completionBox && d->completionBox->isVisible())
00374                 d->completionBox->hide();
00375 
00376             return;
00377         }
00378 
00379         else if ( mode == KGlobalSettings::CompletionShell )
00380         {
00381             // Handles completion.
00382             KShortcut cut;
00383             if ( keys[TextCompletion].isNull() )
00384                 cut = KStdAccel::shortcut(KStdAccel::TextCompletion);
00385             else
00386                 cut = keys[TextCompletion];
00387 
00388             if ( cut.contains( key ) )
00389             {
00390                 // Emit completion if the completion mode is CompletionShell
00391                 // and the cursor is at the end of the string.
00392                 QString txt = text();
00393                 int len = txt.length();
00394                 if ( cursorPosition() == len && len != 0 )
00395                 {
00396                     if ( emitSignals() )
00397                         emit completion( txt );
00398                     if ( handleSignals() )
00399                         makeCompletion( txt );
00400                     return;
00401                 }
00402             }
00403             else if ( d->completionBox )
00404                 d->completionBox->hide();
00405         }
00406 
00407         // handle rotation
00408         if ( mode != KGlobalSettings::CompletionNone )
00409         {
00410             // Handles previous match
00411             KShortcut cut;
00412             if ( keys[PrevCompletionMatch].isNull() )
00413                 cut = KStdAccel::shortcut(KStdAccel::PrevCompletion);
00414             else
00415                 cut = keys[PrevCompletionMatch];
00416 
00417             if ( cut.contains( key ) )
00418             {
00419                 if ( emitSignals() )
00420                     emit textRotation( KCompletionBase::PrevCompletionMatch );
00421                 if ( handleSignals() )
00422                     rotateText( KCompletionBase::PrevCompletionMatch );
00423                 return;
00424             }
00425 
00426             // Handles next match
00427             if ( keys[NextCompletionMatch].isNull() )
00428                 cut = KStdAccel::key(KStdAccel::NextCompletion);
00429             else
00430                 cut = keys[NextCompletionMatch];
00431 
00432             if ( cut.contains( key ) )
00433             {
00434                 if ( emitSignals() )
00435                     emit textRotation( KCompletionBase::NextCompletionMatch );
00436                 if ( handleSignals() )
00437                     rotateText( KCompletionBase::NextCompletionMatch );
00438                 return;
00439             }
00440         }
00441 
00442         // substring completion
00443         if ( compObj() )
00444         {
00445             KShortcut cut;
00446             if ( keys[SubstringCompletion].isNull() )
00447                 cut = KStdAccel::shortcut(KStdAccel::SubstringCompletion);
00448             else
00449                 cut = keys[SubstringCompletion];
00450 
00451             if ( cut.contains( key ) )
00452             {
00453                 if ( emitSignals() )
00454                     emit substringCompletion( text() );
00455                 if ( handleSignals() )
00456                 {
00457                     setCompletedItems( compObj()->substringCompletion(text()));
00458                     e->accept();
00459                 }
00460                 return;
00461             }
00462         }
00463     }
00464 
00465     if ( KStdAccel::copy().contains( key ) )
00466     {
00467         copy();
00468         return;
00469     }
00470     else if ( KStdAccel::paste().contains( key ) )
00471     {
00472         paste();
00473         return;
00474     }
00475 
00476     // support for pasting Selection with Shift-Ctrl-Insert
00477     else if ( e->key() == Key_Insert &&
00478               (e->state() == (ShiftButton | ControlButton)) )
00479     {
00480 #if QT_VERSION >= 0x030100
00481         QString text = QApplication::clipboard()->text( QClipboard::Selection);
00482 #else
00483         QClipboard *clip = QApplication::clipboard();
00484         bool oldMode = clip->selectionModeEnabled();
00485         clip->setSelectionMode( true );
00486         QString text = QApplication::clipboard()->text();
00487         clip->setSelectionMode( oldMode );
00488 #endif
00489 
00490         insert( text );
00491         deselect();
00492         return;
00493     }
00494 
00495     else if ( KStdAccel::cut().contains( key ) )
00496     {
00497         cut();
00498         return;
00499     }
00500     else if ( KStdAccel::undo().contains( key ) )
00501     {
00502         undo();
00503         return;
00504     }
00505     else if ( KStdAccel::redo().contains( key ) )
00506     {
00507         redo();
00508         return;
00509     }
00510     else if ( KStdAccel::deleteWordBack().contains( key ) )
00511     {
00512         cursorWordBackward(TRUE);
00513         if ( hasSelectedText() )
00514             del();
00515 
00516         e->accept();
00517         return;
00518     }
00519     else if ( KStdAccel::deleteWordForward().contains( key ) )
00520     {
00521         // Workaround for QT bug where
00522         cursorWordForward(TRUE);
00523         if ( hasSelectedText() )
00524             del();
00525 
00526         e->accept();
00527         return;
00528     }
00529 
00530     // Let QLineEdit handle any other keys events.
00531     QLineEdit::keyPressEvent ( e );
00532 }
00533 
00534 void KLineEdit::mouseDoubleClickEvent( QMouseEvent* e )
00535 {
00536     if ( e->button() == Qt::LeftButton  )
00537     {
00538         possibleTripleClick=true;
00539         QTimer::singleShot( QApplication::doubleClickInterval(),this,
00540                             SLOT(tripleClickTimeout()) );
00541     }
00542     QLineEdit::mouseDoubleClickEvent( e );
00543 }
00544 
00545 void KLineEdit::mousePressEvent( QMouseEvent* e )
00546 {
00547     if ( possibleTripleClick && e->button() == Qt::LeftButton )
00548     {
00549         selectAll();
00550         return;
00551     }
00552     QLineEdit::mousePressEvent( e );
00553 }
00554 
00555 void KLineEdit::tripleClickTimeout()
00556 {
00557     possibleTripleClick=false;
00558 }
00559 
00560 QPopupMenu *KLineEdit::createPopupMenu()
00561 {
00562     // Return if popup menu is not enabled !!
00563     if ( !m_bEnableMenu )
00564         return 0;
00565 
00566     QPopupMenu *popup = QLineEdit::createPopupMenu();
00567 
00568     // If a completion object is present and the input
00569     // widget is not read-only, show the Text Completion
00570     // menu item.
00571     if ( compObj() && !isReadOnly() && kapp->authorize("lineedit_text_completion"))
00572     {
00573         QPopupMenu *subMenu = new QPopupMenu( popup );
00574         connect( subMenu, SIGNAL( activated( int ) ),
00575                  this, SLOT( completionMenuActivated( int ) ) );
00576 
00577         popup->insertSeparator();
00578         popup->insertItem( SmallIconSet("completion"), i18n("Text Completion"),
00579                            subMenu );
00580 
00581         subMenu->insertItem( i18n("None"), NoCompletion );
00582         subMenu->insertItem( i18n("Manual"), ShellCompletion );
00583         subMenu->insertItem( i18n("Automatic"), AutoCompletion );
00584         subMenu->insertItem( i18n("Dropdown List"), PopupCompletion );
00585         subMenu->insertItem( i18n("Short Automatic"), SemiAutoCompletion );
00586 
00587         subMenu->setAccel( KStdAccel::completion(), ShellCompletion );
00588 
00589         KGlobalSettings::Completion mode = completionMode();
00590         subMenu->setItemChecked( NoCompletion,
00591                                  mode == KGlobalSettings::CompletionNone );
00592         subMenu->setItemChecked( ShellCompletion,
00593                                  mode == KGlobalSettings::CompletionShell );
00594         subMenu->setItemChecked( PopupCompletion,
00595                                  mode == KGlobalSettings::CompletionPopup );
00596         subMenu->setItemChecked( AutoCompletion,
00597                                  mode == KGlobalSettings::CompletionAuto );
00598         subMenu->setItemChecked( SemiAutoCompletion,
00599                                  mode == KGlobalSettings::CompletionMan );
00600         if ( mode != KGlobalSettings::completionMode() )
00601         {
00602             subMenu->insertSeparator();
00603             subMenu->insertItem( i18n("Default"), Default );
00604         }
00605     }
00606 
00607     // ### do we really need this?  Yes, Please do not remove!  This
00608     // allows applications to extend the popup menu without having to
00609     // inherit from this class! (DA)
00610     emit aboutToShowContextMenu( popup );
00611 
00612     return popup;
00613 }
00614 
00615 void KLineEdit::completionMenuActivated( int id )
00616 {
00617     KGlobalSettings::Completion oldMode = completionMode();
00618 
00619     switch ( id )
00620     {
00621         case Default:
00622            setCompletionMode( KGlobalSettings::completionMode() );
00623            break;
00624         case NoCompletion:
00625            setCompletionMode( KGlobalSettings::CompletionNone );
00626            break;
00627         case AutoCompletion:
00628             setCompletionMode( KGlobalSettings::CompletionAuto );
00629             break;
00630         case SemiAutoCompletion:
00631             setCompletionMode( KGlobalSettings::CompletionMan );
00632             break;
00633         case ShellCompletion:
00634             setCompletionMode( KGlobalSettings::CompletionShell );
00635             break;
00636         case PopupCompletion:
00637             setCompletionMode( KGlobalSettings::CompletionPopup );
00638             break;
00639         default:
00640             return;
00641     }
00642 
00643     if ( oldMode != completionMode() )
00644     {
00645         if ( oldMode == KGlobalSettings::CompletionPopup &&
00646              d->completionBox && d->completionBox->isVisible() )
00647             d->completionBox->hide();
00648         emit completionModeChanged( completionMode() );
00649     }
00650 }
00651 
00652 void KLineEdit::dropEvent(QDropEvent *e)
00653 {
00654     KURL::List urlList;
00655     if( d->handleURLDrops && KURLDrag::decode( e, urlList ) )
00656     {
00657         QString dropText = text();
00658         KURL::List::ConstIterator it;
00659         for( it = urlList.begin() ; it != urlList.end() ; ++it )
00660         {
00661             if(!dropText.isEmpty())
00662                 dropText+=' ';
00663 
00664             dropText += (*it).prettyURL();
00665         }
00666 
00667         validateAndSet( dropText, dropText.length(), 0, 0);
00668 
00669         e->accept();
00670     }
00671     else
00672         QLineEdit::dropEvent(e);
00673 }
00674 
00675 bool KLineEdit::eventFilter( QObject* o, QEvent* ev )
00676 {
00677     if( o == this )
00678     {
00679         KCursor::autoHideEventFilter( this, ev );
00680         if ( ev->type() == QEvent::AccelOverride )
00681         {
00682             QKeyEvent *e = static_cast<QKeyEvent *>( ev );
00683             if (overrideAccel (e))
00684             {
00685                 e->accept();
00686                 return true;
00687             }
00688         }
00689         else if( ev->type() == QEvent::KeyPress )
00690         {
00691             QKeyEvent *e = static_cast<QKeyEvent *>( ev );
00692 
00693             if( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter )
00694             {
00695                 bool trap = d->completionBox && d->completionBox->isVisible();
00696                 bool stopEvent = trap || (d->grabReturnKeyEvents &&
00697                                           (e->state() == NoButton));
00698 
00699                 // Qt will emit returnPressed() itself if we return false
00700                 if ( stopEvent )
00701                     emit QLineEdit::returnPressed();
00702 
00703                 emit returnPressed( displayText() );
00704 
00705                 if ( trap )
00706                     d->completionBox->hide();
00707 
00708                 // Eat the event if the user asked for it, or if a completionbox was visible
00709                 return stopEvent;
00710             }
00711         }
00712     }
00713     return QLineEdit::eventFilter( o, ev );
00714 }
00715 
00716 
00717 void KLineEdit::setURLDropsEnabled(bool enable)
00718 {
00719     d->handleURLDrops=enable;
00720 }
00721 
00722 bool KLineEdit::isURLDropsEnabled() const
00723 {
00724     return d->handleURLDrops;
00725 }
00726 
00727 void KLineEdit::setTrapReturnKey( bool grab )
00728 {
00729     d->grabReturnKeyEvents = grab;
00730 }
00731 
00732 bool KLineEdit::trapReturnKey() const
00733 {
00734     return d->grabReturnKeyEvents;
00735 }
00736 
00737 void KLineEdit::setURL( const KURL& url )
00738 {
00739     QLineEdit::setText( url.prettyURL() );
00740 }
00741 
00742 void KLineEdit::makeCompletionBox()
00743 {
00744     if ( d->completionBox )
00745         return;
00746 
00747     d->completionBox = new KCompletionBox( this, "completion box" );
00748     if ( handleSignals() )
00749     {
00750         connect( d->completionBox, SIGNAL(highlighted( const QString& )),
00751                  SLOT(setTextWorkaround( const QString& )) );
00752         connect( d->completionBox, SIGNAL(userCancelled( const QString& )),
00753                  SLOT(setText( const QString& )) );
00754         connect( d->completionBox, SIGNAL( activated( const QString& )),
00755                  SIGNAL(completionBoxActivated( const QString& )) );
00756     }
00757 }
00758 
00759 bool KLineEdit::overrideAccel (const QKeyEvent* e)
00760 {
00761     KShortcut scKey;
00762 
00763     KKey key( e );
00764     KeyBindingMap keys = getKeyBindings();
00765 
00766     if (keys[TextCompletion].isNull())
00767         scKey = KStdAccel::shortcut(KStdAccel::TextCompletion);
00768     else
00769         scKey = keys[TextCompletion];
00770 
00771     if (scKey.contains( key ))
00772         return true;
00773 
00774     if (keys[NextCompletionMatch].isNull())
00775         scKey = KStdAccel::shortcut(KStdAccel::NextCompletion);
00776     else
00777         scKey = keys[NextCompletionMatch];
00778 
00779     if (scKey.contains( key ))
00780         return true;
00781 
00782     if (keys[PrevCompletionMatch].isNull())
00783         scKey = KStdAccel::shortcut(KStdAccel::PrevCompletion);
00784     else
00785         scKey = keys[PrevCompletionMatch];
00786 
00787     if (scKey.contains( key ))
00788         return true;
00789 
00790     // Override all the text manupilation accelerators...
00791     if ( KStdAccel::copy().contains( key ) )
00792         return true;
00793     else if ( KStdAccel::paste().contains( key ) )
00794         return true;
00795     else if ( KStdAccel::cut().contains( key ) )
00796         return true;
00797     else if ( KStdAccel::undo().contains( key ) )
00798         return true;
00799     else if ( KStdAccel::redo().contains( key ) )
00800         return true;
00801     else if (KStdAccel::deleteWordBack().contains( key ))
00802         return true;
00803     else if (KStdAccel::deleteWordForward().contains( key ))
00804         return true;
00805 
00806     if (d->completionBox && d->completionBox->isVisible ())
00807       if (e->key () == Key_Backtab)
00808         return true;
00809 
00810     return false;
00811 }
00812 
00813 void KLineEdit::setCompletedItems( const QStringList& items )
00814 {
00815     QString txt = text();
00816     if ( !items.isEmpty() &&
00817          !(items.count() == 1 && txt == items.first()) )
00818     {
00819         if ( !d->completionBox )
00820             makeCompletionBox();
00821 
00822         if ( !txt.isEmpty() )
00823             d->completionBox->setCancelledText( txt );
00824         d->completionBox->setItems( items );
00825         d->completionBox->popup();
00826     }
00827     else
00828     {
00829         if ( d->completionBox && d->completionBox->isVisible() )
00830             d->completionBox->hide();
00831     }
00832 }
00833 
00834 KCompletionBox * KLineEdit::completionBox( bool create )
00835 {
00836     if ( create )
00837         makeCompletionBox();
00838 
00839     return d->completionBox;
00840 }
00841 
00842 void KLineEdit::setCompletionObject( KCompletion* comp, bool hsig )
00843 {
00844     KCompletion *oldComp = compObj();
00845     if ( oldComp && handleSignals() )
00846         disconnect( oldComp, SIGNAL( matches( const QStringList& )),
00847                     this, SLOT( setCompletedItems( const QStringList& )));
00848 
00849     if ( comp && hsig )
00850       connect( comp, SIGNAL( matches( const QStringList& )),
00851                this, SLOT( setCompletedItems( const QStringList& )));
00852 
00853     KCompletionBase::setCompletionObject( comp, hsig );
00854 }
00855 
00856 // QWidget::create() turns off mouse-Tracking which would break auto-hiding
00857 void KLineEdit::create( WId id, bool initializeWindow, bool destroyOldWindow )
00858 {
00859     QLineEdit::create( id, initializeWindow, destroyOldWindow );
00860     KCursor::setAutoHideCursor( this, true, true );
00861 }
00862 
00863 void KLineEdit::clear()
00864 {
00865     setText( QString::null );
00866 }
00867 
00868 void KLineEdit::setTextWorkaround( const QString& text )
00869 {
00870     setText( text );
00871     end( false ); // force cursor at end
00872 }
00873 
00874 void KLineEdit::virtual_hook( int id, void* data )
00875 { KCompletionBase::virtual_hook( id, data ); }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:15:04 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001