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