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