void Test_KCompletion::sortedOrder() { KCompletion completion; completion.setSoundsEnabled(false); QSignalSpy spy1(&completion, SIGNAL(match(QString))); QSignalSpy spy3(&completion, SIGNAL(multipleMatches())); completion.setOrder(KCompletion::Sorted); QVERIFY(completion.order() == KCompletion::Sorted); completion.setItems(strings); QVERIFY(completion.items().count() == 4); completion.setCompletionMode(KGlobalSettings::CompletionShell); QCOMPARE(completion.makeCompletion("ca"), QString("carp")); QVERIFY(spy1.count() == 1); QCOMPARE(spy1.takeFirst().at(0).toString(), QString("carp")); QVERIFY(spy3.count() == 1); spy3.takeFirst(); QSignalSpy spy2(&completion, SIGNAL(matches(QStringList))); completion.makeCompletion("ca"); QCOMPARE(spy2.count(), 1); QVERIFY(spy3.count() == 0); // shouldn't be signaled on 2nd call QStringList matches = spy2.takeFirst().at(0).toStringList(); QVERIFY(matches.count() == 2); QCOMPARE(matches[0], carp); QCOMPARE(matches[1], carpet); completion.setCompletionMode(KGlobalSettings::CompletionAuto); QCOMPARE(completion.makeCompletion("ca"), carp); QVERIFY(spy1.count() == 1); QCOMPARE(spy1.takeFirst().at(0).toString(), carp); }
void CKeyReferenceLineEdit::makeCompletion(const QString &text) { KCompletion *comp = compObj(); KGlobalSettings::Completion mode = completionMode(); if ( !comp || mode == KGlobalSettings::CompletionNone ) return; // No completion object... QString match = comp->makeCompletion( text ); if ( mode == KGlobalSettings::CompletionPopup || mode == KGlobalSettings::CompletionPopupAuto ) { if ( match.isNull() ) { KCompletionBox *compbox = completionBox(); compbox->hide(); compbox->clear(); } else { QStringList t; t.append(match); setCompletedItems(t); } } else { KLineEdit::makeCompletion(text); } }
void JourneySearchParser::doCorrections( KLineEdit *lineEdit, QString *searchLine, int cursorPos, const QStringList &words, int removedWordsFromLeft ) { int selStart = -1; int selLength = 0; int pos = searchLine->lastIndexOf( ' ', cursorPos - 1 ); int posEnd = searchLine->indexOf( ' ', cursorPos ); if ( posEnd == -1 ) { posEnd = searchLine->length(); } QString lastWordBeforeCursor; if ( posEnd == cursorPos && pos != -1 && !( lastWordBeforeCursor = searchLine->mid( pos, posEnd - pos ).trimmed() ).isEmpty() ) { if ( timeKeywordsAt().contains( lastWordBeforeCursor, Qt::CaseInsensitive ) ) { // Automatically add the current time after 'at' QString formattedTime = KGlobal::locale()->formatTime( QTime::currentTime() ); searchLine->insert( posEnd, ' ' + formattedTime ); selStart = posEnd + 1; // +1 for the added space selLength = formattedTime.length(); } else if ( timeKeywordsIn().contains( lastWordBeforeCursor, Qt::CaseInsensitive ) ) { // Automatically add '5 minutes' after 'in' searchLine->insert( posEnd, ' ' + relativeTimeString(5) ); selStart = posEnd + 1; // +1 for the added space selLength = 1; // only select the number (5) } else { QStringList completionItems; completionItems << timeKeywordsAt() << timeKeywordsIn() << timeKeywordsTomorrow() << departureKeywords() << arrivalKeywords(); KCompletion *comp = lineEdit->completionObject( false ); comp->setItems( completionItems ); comp->setIgnoreCase( true ); QString completion = comp->makeCompletion( lastWordBeforeCursor ); setJourneySearchWordCompletion( lineEdit, completion ); } } // Select an appropriate substring after inserting something if ( selStart != -1 ) { QStringList removedWords = ( QStringList )words.mid( 0, removedWordsFromLeft ); QString removedPart = removedWords.join( " " ).trimmed(); QString correctedSearch; if ( removedPart.isEmpty() ) { correctedSearch = *searchLine; } else { correctedSearch = removedPart + ' ' + *searchLine; selStart += removedPart.length() + 1; } lineEdit->setText( correctedSearch ); lineEdit->setSelection( selStart, selLength ); } }
void Test_KCompletion::weightedOrder() { KCompletion completion; completion.setSoundsEnabled(false); QSignalSpy spy1(&completion, SIGNAL(match(QString))); QSignalSpy spy3(&completion, SIGNAL(multipleMatches())); completion.setOrder(KCompletion::Weighted); QVERIFY(completion.order() == KCompletion::Weighted); completion.setItems(wstrings); QVERIFY(completion.items().count() == 4); completion.setCompletionMode(KCompletion::CompletionShell); QCOMPARE(completion.makeCompletion(QStringLiteral("ca")), QStringLiteral("carp")); spy1.takeFirst(); // empty the list QVERIFY(spy3.count() == 1); spy3.takeFirst(); QSignalSpy spy2(&completion, SIGNAL(matches(QStringList))); completion.makeCompletion(QStringLiteral("ca")); QCOMPARE(spy2.count(), 1); QVERIFY(spy3.count() == 0); // shouldn't be signaled on 2nd call QStringList matches = spy2.takeFirst().at(0).toStringList(); QVERIFY(matches.count() == 2); QCOMPARE(matches[0], carpet); QCOMPARE(matches[1], carp); completion.setCompletionMode(KCompletion::CompletionAuto); QCOMPARE(completion.makeCompletion(QStringLiteral("ca")), carpet); matches = completion.substringCompletion(QStringLiteral("ca")); QVERIFY(matches.count() == 3); QCOMPARE(matches[0], carpet); QCOMPARE(matches[1], coolcat); QCOMPARE(matches[2], carp); }
void Test_KCompletion::cycleMatches_Weighted() { KCompletion completion; completion.setSoundsEnabled(false); completion.setOrder(KCompletion::Weighted); completion.setItems(wstrings); completion.setCompletionMode(KGlobalSettings::CompletionAuto); completion.makeCompletion("ca"); QCOMPARE(completion.nextMatch(), carpet); QCOMPARE(completion.nextMatch(), carp); QCOMPARE(completion.previousMatch(), carpet); QCOMPARE(completion.previousMatch(), carp); }
void Test_KCompletion::cycleMatches_Sorted() { KCompletion completion; completion.setSoundsEnabled(false); completion.setOrder(KCompletion::Sorted); completion.setItems(strings); completion.setCompletionMode(KCompletion::CompletionAuto); completion.makeCompletion(QStringLiteral("ca")); QCOMPARE(completion.nextMatch(), carp); QCOMPARE(completion.nextMatch(), carpet); QCOMPARE(completion.previousMatch(), carp); QCOMPARE(completion.previousMatch(), carpet); }
void KLineEdit::makeCompletion( const QString& text ) { KCompletion *comp = compObj(); KGlobalSettings::Completion mode = completionMode(); if ( !comp || mode == KGlobalSettings::CompletionNone ) return; // No completion object... QString match = comp->makeCompletion( text ); if ( mode == KGlobalSettings::CompletionPopup || mode == KGlobalSettings::CompletionPopupAuto ) { if ( match.isNull() ) { if ( d->completionBox ) { d->completionBox->hide(); d->completionBox->clear(); } } else setCompletedItems( comp->allMatches() ); } else // Auto, ShortAuto (Man) and Shell { // all other completion modes // If no match or the same match, simply return without completing. if ( match.isNull() || match == text ) return; if ( mode != KGlobalSettings::CompletionShell ) setUserSelection(false); if ( d->autoSuggest ) setCompletedText( match ); } }