00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <qapplication.h>
00036 #include <qclipboard.h>
00037 #include <qtimer.h>
00038 #include <qpopupmenu.h>
00039
00040
00041
00042 #include <opie2/ocompletionbox.h>
00043 #include <opie2/olineedit.h>
00044 #include <opie2/oglobalsettings.h>
00045
00046 typedef QString KURL;
00047
00048
00049
00050
00051
00052 class OLineEdit::OLineEditPrivate
00053 {
00054 public:
00055 OLineEditPrivate()
00056 {
00057 grabReturnKeyEvents = false;
00058 handleURLDrops = true;
00059 completionBox = 0L;
00060 }
00061 ~OLineEditPrivate()
00062 {
00063 delete completionBox;
00064 }
00065
00066 bool grabReturnKeyEvents;
00067 bool handleURLDrops;
00068 OCompletionBox *completionBox;
00069 };
00070
00071
00072
00073
00074
00075
00076 OLineEdit::OLineEdit( const QString &string, QWidget *parent, const char *name )
00077 : QLineEdit( string, parent, name )
00078 {
00079 init();
00080 }
00081
00082 OLineEdit::OLineEdit( QWidget *parent, const char *name )
00083 : QLineEdit( parent, name )
00084 {
00085 init();
00086 }
00087
00088 OLineEdit::~OLineEdit ()
00089 {
00090 delete d;
00091 }
00092
00093 void OLineEdit::init()
00094 {
00095 d = new OLineEditPrivate;
00096 possibleTripleClick = false;
00097
00098 setContextMenuEnabled( true );
00099
00100 installEventFilter( this );
00101 }
00102
00103 void OLineEdit::setCompletionMode( OGlobalSettings::Completion mode )
00104 {
00105 OGlobalSettings::Completion oldMode = completionMode();
00106 if ( oldMode != mode && oldMode == OGlobalSettings::CompletionPopup &&
00107 d->completionBox && d->completionBox->isVisible() )
00108 d->completionBox->hide();
00109
00110
00111
00112 if ( echoMode() != QLineEdit::Normal )
00113 mode = OGlobalSettings::CompletionNone;
00114
00115 OCompletionBase::setCompletionMode( mode );
00116 }
00117
00118 void OLineEdit::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 OLineEdit::setCompletedText( const QString& text )
00129 {
00130 OGlobalSettings::Completion mode = completionMode();
00131 bool marked = ( mode == OGlobalSettings::CompletionAuto ||
00132 mode == OGlobalSettings::CompletionMan ||
00133 mode == OGlobalSettings::CompletionPopup );
00134 setCompletedText( text, marked );
00135 }
00136
00137 void OLineEdit::rotateText( OCompletionBase::KeyBindingType type )
00138 {
00139 OCompletion* comp = compObj();
00140 if ( comp &&
00141 (type == OCompletionBase::PrevCompletionMatch ||
00142 type == OCompletionBase::NextCompletionMatch ) )
00143 {
00144 QString input = (type == OCompletionBase::PrevCompletionMatch) ? comp->previousMatch() : comp->nextMatch();
00145
00146 if ( input.isNull() || input == displayText() )
00147 return;
00148 #if QT_VERSION >= 0x030000
00149 setCompletedText( input, hasSelectedText() );
00150 #else
00151 setCompletedText( input, hasMarkedText() );
00152 #endif
00153 }
00154 }
00155
00156 void OLineEdit::makeCompletion( const QString& text )
00157 {
00158 OCompletion *comp = compObj();
00159 if ( !comp )
00160 return;
00161
00162 QString match = comp->makeCompletion( text );
00163 OGlobalSettings::Completion mode = completionMode();
00164 if ( mode == OGlobalSettings::CompletionPopup )
00165 {
00166 if ( match.isNull() )
00167 {
00168 if ( d->completionBox ) {
00169 d->completionBox->hide();
00170 d->completionBox->clear();
00171 }
00172 }
00173 else
00174 setCompletedItems( comp->allMatches() );
00175 }
00176 else
00177 {
00178
00179
00180 if ( match.isNull() || match == text )
00181 return;
00182
00183 setCompletedText( match );
00184 }
00185 }
00186
00187 void OLineEdit::setReadOnly(bool readOnly)
00188 {
00189 QPalette p = palette();
00190 if (readOnly)
00191 {
00192 QColor color = p.color(QPalette::Disabled, QColorGroup::Background);
00193 p.setColor(QColorGroup::Base, color);
00194 p.setColor(QColorGroup::Background, color);
00195 }
00196 else
00197 {
00198 QColor color = p.color(QPalette::Normal, QColorGroup::Base);
00199 p.setColor(QColorGroup::Base, color);
00200 p.setColor(QColorGroup::Background, color);
00201 }
00202 setPalette(p);
00203
00204 QLineEdit::setReadOnly (readOnly);
00205 }
00206
00207 void OLineEdit::keyPressEvent( QKeyEvent *e )
00208 {
00209 qDebug( "OLineEdit::keyPressEvent()" );
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258 if ( echoMode() == QLineEdit::Normal &&
00259 completionMode() != OGlobalSettings::CompletionNone )
00260 {
00261 KeyBindingMap keys = getKeyBindings();
00262 OGlobalSettings::Completion mode = completionMode();
00263 bool noModifier = (e->state() == NoButton || e->state()== ShiftButton);
00264
00265 if ( (mode == OGlobalSettings::CompletionAuto ||
00266 mode == OGlobalSettings::CompletionMan) && noModifier )
00267 {
00268 QString keycode = e->text();
00269 if ( !keycode.isNull() && keycode.unicode()->isPrint() )
00270 {
00271 QLineEdit::keyPressEvent ( e );
00272 QString txt = text();
00273 int len = txt.length();
00274 #if QT_VERSION >= 0x030000
00275 if ( !hasSelectedText() && len && cursorPosition() == len )
00276 #else
00277 if ( !hasMarkedText() && len && cursorPosition() == len )
00278 #endif
00279 {
00280 if ( emitSignals() )
00281 emit completion( txt );
00282 if ( handleSignals() )
00283 makeCompletion( txt );
00284 e->accept();
00285 }
00286 return;
00287 }
00288 }
00289
00290 else if ( mode == OGlobalSettings::CompletionPopup && noModifier )
00291 {
00292 qDebug( "OLineEdit::keyPressEvent() - global settings = CompletionPopup & noModifier" );
00293
00294 QString old_txt = text();
00295 QLineEdit::keyPressEvent ( e );
00296 QString txt = text();
00297 int len = txt.length();
00298 QString keycode = e->text();
00299
00300
00301 if ( txt != old_txt && len && cursorPosition() == len &&
00302 ( (!keycode.isNull() && keycode.unicode()->isPrint()) ||
00303 e->key() == Key_Backspace ) )
00304 {
00305 if ( emitSignals() )
00306 emit completion( txt );
00307 if ( handleSignals() )
00308 makeCompletion( txt );
00309 e->accept();
00310 }
00311 else if (!len && d->completionBox && d->completionBox->isVisible())
00312 d->completionBox->hide();
00313
00314 return;
00315 }
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 }
00402
00403
00404 QLineEdit::keyPressEvent ( e );
00405 }
00406
00407 void OLineEdit::mouseDoubleClickEvent( QMouseEvent* e )
00408 {
00409 if ( e->button() == Qt::LeftButton )
00410 {
00411 possibleTripleClick=true;
00412 QTimer::singleShot( QApplication::doubleClickInterval(),this,
00413 SLOT(tripleClickTimeout()) );
00414 }
00415 QLineEdit::mouseDoubleClickEvent( e );
00416 }
00417
00418 void OLineEdit::mousePressEvent( QMouseEvent* e )
00419 {
00420 if ( possibleTripleClick && e->button() == Qt::LeftButton )
00421 {
00422 selectAll();
00423 return;
00424 }
00425 QLineEdit::mousePressEvent( e );
00426 }
00427
00428 void OLineEdit::tripleClickTimeout()
00429 {
00430 possibleTripleClick=false;
00431 }
00432
00433 QPopupMenu *OLineEdit::createPopupMenu()
00434 {
00435
00436 if ( !m_bEnableMenu )
00437 return 0;
00438
00439 #if QT_VERSION >= 0x030000
00440 QPopupMenu *popup = QLineEdit::createPopupMenu();
00441 #else
00442 QPopupMenu *popup = new QPopupMenu();
00443 #warning OLineEdit is not fully functional on Qt2
00444 #endif
00445
00446
00447 if ( compObj() )
00448 {
00449 QPopupMenu *subMenu = new QPopupMenu( popup );
00450 connect( subMenu, SIGNAL( activated(int) ),
00451 this, SLOT( completionMenuActivated(int) ) );
00452
00453 popup->insertSeparator();
00454
00455
00456
00457 popup->insertItem( tr("Text Completion"), subMenu );
00458
00459 subMenu->insertItem( tr("None"), NoCompletion );
00460 subMenu->insertItem( tr("Manual"), ShellCompletion );
00461 subMenu->insertItem( tr("Automatic"), AutoCompletion );
00462 subMenu->insertItem( tr("Dropdown List"), PopupCompletion );
00463 subMenu->insertItem( tr("Short Automatic"), SemiAutoCompletion );
00464
00465
00466 subMenu->setAccel( Key_Tab, ShellCompletion );
00467
00468 OGlobalSettings::Completion mode = completionMode();
00469 subMenu->setItemChecked( NoCompletion,
00470 mode == OGlobalSettings::CompletionNone );
00471 subMenu->setItemChecked( ShellCompletion,
00472 mode == OGlobalSettings::CompletionShell );
00473 subMenu->setItemChecked( PopupCompletion,
00474 mode == OGlobalSettings::CompletionPopup );
00475 subMenu->setItemChecked( AutoCompletion,
00476 mode == OGlobalSettings::CompletionAuto );
00477 subMenu->setItemChecked( SemiAutoCompletion,
00478 mode == OGlobalSettings::CompletionMan );
00479 if ( mode != OGlobalSettings::completionMode() )
00480 {
00481 subMenu->insertSeparator();
00482 subMenu->insertItem( tr("Default"), Default );
00483 }
00484 }
00485
00486
00487
00488 emit aboutToShowContextMenu( popup );
00489
00490 return popup;
00491 }
00492
00493 void OLineEdit::completionMenuActivated( int id )
00494 {
00495 OGlobalSettings::Completion oldMode = completionMode();
00496
00497 switch ( id )
00498 {
00499 case Default:
00500 setCompletionMode( OGlobalSettings::completionMode() ); break;
00501 case NoCompletion:
00502 setCompletionMode( OGlobalSettings::CompletionNone ); break;
00503 case AutoCompletion:
00504 setCompletionMode( OGlobalSettings::CompletionAuto ); break;
00505 case SemiAutoCompletion:
00506 setCompletionMode( OGlobalSettings::CompletionMan ); break;
00507 case ShellCompletion:
00508 setCompletionMode( OGlobalSettings::CompletionShell ); break;
00509 case PopupCompletion:
00510 setCompletionMode( OGlobalSettings::CompletionPopup ); break;
00511 default: return;
00512 }
00513
00514 if ( oldMode != completionMode() )
00515 {
00516 if ( oldMode == OGlobalSettings::CompletionPopup &&
00517 d->completionBox && d->completionBox->isVisible() )
00518 d->completionBox->hide();
00519 emit completionModeChanged( completionMode() );
00520 }
00521 }
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546 bool OLineEdit::eventFilter( QObject* o, QEvent* ev )
00547 {
00548 if( o == this )
00549 {
00550
00551 if ( ev->type() == QEvent::AccelOverride )
00552 {
00553 QKeyEvent *e = static_cast<QKeyEvent *>( ev );
00554
00555
00556
00557
00558
00559 }
00560 else if( ev->type() == QEvent::KeyPress )
00561 {
00562 QKeyEvent *e = static_cast<QKeyEvent *>( ev );
00563
00564 if( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter )
00565 {
00566 bool trap = d->completionBox && d->completionBox->isVisible();
00567
00568
00569 if ( d->grabReturnKeyEvents || trap )
00570 emit QLineEdit::returnPressed();
00571
00572 emit returnPressed( displayText() );
00573
00574 if ( trap )
00575 d->completionBox->hide();
00576
00577
00578 return d->grabReturnKeyEvents || trap;
00579 }
00580 }
00581 }
00582 return QLineEdit::eventFilter( o, ev );
00583 }
00584
00585
00586 void OLineEdit::setURLDropsEnabled(bool enable)
00587 {
00588 d->handleURLDrops=enable;
00589 }
00590
00591 bool OLineEdit::isURLDropsEnabled() const
00592 {
00593 return d->handleURLDrops;
00594 }
00595
00596 void OLineEdit::setTrapReturnKey( bool grab )
00597 {
00598 d->grabReturnKeyEvents = grab;
00599 }
00600
00601 bool OLineEdit::trapReturnKey() const
00602 {
00603 return d->grabReturnKeyEvents;
00604 }
00605
00606
00607
00608
00609
00610
00611 void OLineEdit::makeCompletionBox()
00612 {
00613 if ( d->completionBox )
00614 return;
00615
00616 d->completionBox = new OCompletionBox( this, "completion box" );
00617 if ( handleSignals() )
00618 {
00619 connect( d->completionBox, SIGNAL(highlighted(const QString&)),
00620 SLOT(setText(const QString&)) );
00621 connect( d->completionBox, SIGNAL(userCancelled(const QString&)),
00622 SLOT(setText(const QString&)) );
00623
00624
00625
00626
00627 if ( parentWidget() && parentWidget()->inherits("KComboBox") )
00628 connect( d->completionBox, SIGNAL( activated(const QString&)),
00629 parentWidget(), SIGNAL( activated(const QString&)));
00630 }
00631 }
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676 void OLineEdit::setCompletedItems( const QStringList& items )
00677 {
00678 QString txt = text();
00679 if ( !items.isEmpty() &&
00680 !(items.count() == 1 && txt == items.first()) )
00681 {
00682 if ( !d->completionBox )
00683 makeCompletionBox();
00684
00685 if ( !txt.isEmpty() )
00686 d->completionBox->setCancelledText( txt );
00687 d->completionBox->setItems( items );
00688 d->completionBox->popup();
00689 }
00690 else
00691 {
00692 if ( d->completionBox && d->completionBox->isVisible() )
00693 d->completionBox->hide();
00694 }
00695 }
00696
00697 OCompletionBox * OLineEdit::completionBox( bool create )
00698 {
00699 if ( create )
00700 makeCompletionBox();
00701
00702 return d->completionBox;
00703 }
00704
00705 void OLineEdit::setCompletionObject( OCompletion* comp, bool hsig )
00706 {
00707 OCompletion *oldComp = compObj();
00708 if ( oldComp && handleSignals() )
00709 disconnect( oldComp, SIGNAL( matches(const QStringList&)),
00710 this, SLOT( setCompletedItems(const QStringList&)));
00711
00712 if ( comp && hsig )
00713 connect( comp, SIGNAL( matches(const QStringList&)),
00714 this, SLOT( setCompletedItems(const QStringList&)));
00715
00716 OCompletionBase::setCompletionObject( comp, hsig );
00717 }
00718
00719
00720 void OLineEdit::create( WId id, bool initializeWindow, bool destroyOldWindow )
00721 {
00722 QLineEdit::create( id, initializeWindow, destroyOldWindow );
00723
00724 }
00725
00726 void OLineEdit::clear()
00727 {
00728 setText( QString::null );
00729 }