KCompletion Class

A generic class for completing QStrings. More...

Header: #include <KCompletion>
CMake: find_package(KF6 REQUIRED COMPONENTS Completion)
target_link_libraries(mytarget PRIVATE KF6::Completion)
Inherits: QObject

Public Types

enum CompOrder { Sorted, Insertion, Weighted }
(since 5.0) enum CompletionMode { CompletionNone, CompletionAuto, CompletionMan, Comple, CompletionPopup, CompletionPopupAuto }

Properties

Public Functions

KCompletion()
QStringList allMatches()
QStringList allMatches(const QString &string)
KCompletionMatches allWeightedMatches()
KCompletionMatches allWeightedMatches(const QString &string)
KCompletion::CompletionMode completionMode() const
bool hasMultipleMatches() const
bool ignoreCase() const
bool isEmpty() const
QStringList items() const
virtual const QString &lastMatch() const
KCompletion::CompOrder order() const
virtual void setCompletionMode(KCompletion::CompletionMode mode)
virtual void setIgnoreCase(bool ignoreCase)
virtual void setOrder(KCompletion::CompOrder order)
virtual void setSoundsEnabled(bool enable)
(since 5.87) bool shouldAutoSuggest() const
bool soundsEnabled() const
QStringList substringCompletion(const QString &string) const

Public Slots

void addItem(const QString &item)
void addItem(const QString &item, uint weight)
virtual void clear()
void insertItems(const QStringList &items)
virtual QString makeCompletion(const QString &string)
QString nextMatch()
QString previousMatch()
void removeItem(const QString &item)
virtual void setItems(const QStringList &itemList)

Signals

void match(const QString &item)
void matches(const QStringList &matchlist)
void multipleMatches()

Protected Functions

virtual void postProcessMatch(QString *match) const
virtual void postProcessMatches(KCompletionMatches *matches) const
virtual void postProcessMatches(QStringList *matchList) const
(since 5.87) void setShouldAutoSuggest(bool shouldAutosuggest)
(since 5.88) void setSorterFunction(KCompletion::SorterFunction sortFunc)

Detailed Description

This class offers easy use of "auto completion", "manual completion" or "shell completion" on QString objects. A common use is completing filenames or URLs (see KUrlCompletion()). But it is not limited to URL-completion – everything should be completable! The user should be able to complete email addresses, telephone numbers, commands, SQL queries... Every time your program knows what the user can type into an edit field, you should offer completion. With KCompletion, this is very easy, and if you are using a line edit widget (KLineEdit), it is even easier. Basically, you tell a KCompletion object what strings should be completable and, whenever completion should be invoked, you call makeCompletion(). KLineEdit and (an editable) KComboBox even do this automatically for you.

KCompletion offers the completed string via the signal match() and all matching strings (when the result is ambiguous) via the method allMatches().

Notice: auto completion, shell completion and manual completion work slightly differently:

  • auto completion always returns a complete item as match. When more than one matching item is available, it will deliver just the first one (depending on sorting order). Iterating over all matches is possible via nextMatch() and previousMatch().
  • popup completion works in the same way, the only difference being that the completed items are not put into the edit widget, but into a separate popup box.
  • manual completion works the same way as auto completion, except that it is not invoked automatically while the user is typing, but only when the user presses a special key. The difference of manual and auto completion is therefore only visible in UI classes. KCompletion needs to know whether to deliver partial matches (shell completion) or whole matches (auto/manual completion), therefore KCompletion::CompletionMan and KCompletion::CompletionAuto have the exact same effect in KCompletion.
  • shell completion works like "tab completion" in a shell: when multiple matches are available, the longest possible string of all matches is returned (i.e. only a partial item). Iterating over all matching items (complete, not partial) is possible via nextMatch() and previousMatch().

As an application programmer, you do not normally have to worry about the different completion modes; KCompletion handles that for you, according to the setting setCompletionMode(). The default setting is globally configured by the user and read from completionMode().

A short example:

KCompletion completion;
completion.setOrder(KCompletion::Sorted);
completion.addItem("pfeiffer@kde.org");
completion.addItem("coolo@kde.org");
completion.addItem("carpdjih@sp.zrz.tu-berlin.de");
completion.addItem("carp\cs.tu-berlin.de");

cout << completion.makeCompletion("ca").latin1() << endl;

In shell-completion mode, this will be "carp"; in auto-completion mode it will be "carp\cs.tu-berlin.de", as that is alphabetically smaller. If setOrder was set to Insertion, "carpdjih@sp.zrz.tu-berlin.de" would be completed in auto-completion mode, as that was inserted before "carp\cs.tu-berlin.de".

You can dynamically update the completable items by removing and adding them whenever you want. For advanced usage, you could even use multiple KCompletion objects. E.g. imagine an editor like kwrite with multiple open files. You could store items of each file in a different KCompletion object, so that you know (and tell the user) where a completion comes from.

Note: KCompletion does not work with strings that contain 0x0 characters (unicode null), as this is used internally as a delimiter.

You may inherit from KCompletion and override makeCompletion() in special cases (like reading directories or urls and then supplying the contents to KCompletion, as KUrlCompletion does), but this is usually not necessary.

Member Type Documentation

enum KCompletion::CompOrder

Constants that represent the order in which KCompletion performs completion lookups.

ConstantValueDescription
KCompletion::Sorted0Use alphabetically sorted order or custom sorter logic.
KCompletion::Insertion1Use order of insertion.
KCompletion::Weighted2Use weighted order

[since 5.0] enum KCompletion::CompletionMode

This enum describes the completion mode used for by the KCompletion class.

ConstantValueDescription
KCompletion::CompletionNone1No completion is used.
KCompletion::CompletionAuto2Text is automatically filled in whenever possible.
KCompletion::CompletionMan3Same as automatic, but shortest match is used for completion.
KCompletion::Comple?Completes text much in the same way as a typical *nix shell would.
KCompletion::CompletionPopup5Lists all possible matches in a popup list box to choose from.
KCompletion::CompletionPopupAuto6Lists all possible matches in a popup list box to choose from, and automatically fills the result whenever possible.

This enum was introduced in 5.0.

Property Documentation

ignoreCase : bool

Access functions:

bool ignoreCase() const
virtual void setIgnoreCase(bool ignoreCase)

items : QStringList

Access functions:

QStringList items() const
virtual void setItems(const QStringList &itemList)

order : CompOrder

Access functions:

KCompletion::CompOrder order() const
virtual void setOrder(KCompletion::CompOrder order)

Member Function Documentation

KCompletion::KCompletion()

Constructor, nothing special here :)

[slot] void KCompletion::addItem(const QString &item)

Adds an item to the list of available completions. Resets the current item state (previousMatch() and nextMatch() won't work the next time they are called).

item the item to add

[slot] void KCompletion::addItem(const QString &item, uint weight)

Adds an item to the list of available completions. Resets the current item state (previousMatch() and nextMatch() won't work the next time they are called).

Sets the weight of the item to weight or adds it to the current weight if the item is already available. The weight has to be greater than 1 to take effect (default weight is 1).

item the item to add

weight the weight of the item, default is 1

QStringList KCompletion::allMatches()

Returns a list of all items matching the last completed string. It might take some time if you have a lot of items.

See also substringCompletion.

QStringList KCompletion::allMatches(const QString &string)

Returns a list of all items matching string.

KCompletionMatches KCompletion::allWeightedMatches()

Returns a list of all items matching the last completed string. It might take some time if you have a lot of items. The matches are returned as KCompletionMatches, which also keeps the weight of the matches, allowing you to modify some matches or merge them with matches from another call to allWeightedMatches(), and sort the matches after that in order to have the matches ordered correctly.

See also substringCompletion.

KCompletionMatches KCompletion::allWeightedMatches(const QString &string)

Returns a list of all items matching string.

[virtual slot] void KCompletion::clear()

Removes all inserted items.

KCompletion::CompletionMode KCompletion::completionMode() const

Returns the current completion mode, default is CompletionPopup

See also setCompletionMode and CompletionMode.

bool KCompletion::hasMultipleMatches() const

Returns true when more than one match is found.

See also multipleMatches.

bool KCompletion::ignoreCase() const

Returns whether KCompletion acts case insensitively or not.

Default is false (case sensitive).

Note: Getter function for property ignoreCase.

See also setIgnoreCase.

[slot] void KCompletion::insertItems(const QStringList &items)

Inserts items into the list of possible completions.

It does the same as setItems(), but without calling clear() before.

items the items to insert

bool KCompletion::isEmpty() const

Returns true if the completion object contains no entries.

QStringList KCompletion::items() const

Returns a list of all items inserted into KCompletion. This is useful if you need to save the state of a KCompletion object and restore it later.

Note: When order() == Weighted, then every item in the stringlist has its weight appended, delimited by a colon. E.g. an item "www.kde.org" might look like "www.kde.org:4", where 4 is the weight. This is necessary so that you can save the items along with its weighting on disk and load them back with setItems(), restoring its weight as well. If you really don't want the appended weightings, call setOrder( KCompletion::Insertion ) before calling items().

Note: Getter function for property items.

See also setItems.

[virtual] const QString &KCompletion::lastMatch() const

Returns the last match. Might be useful if you need to check whether a completion is different from the last one.

QString() is returned when there is no last match.

[virtual slot] QString KCompletion::makeCompletion(const QString &string)

Attempts to find an item in the list of available completions that begins with string. Will either return the first matching item (if there is more than one match) or QString(), if no match is found.

In the latter case, a sound will be emitted, depending on soundsEnabled(). If a match is found, it will be emitted via the signal match().

If this is called twice or more with the same string while no items were added or removed in the meantime, all available completions will be emitted via the signal matches(). This happens only in shell-completion mode.

string the string to complete

Returns the matching item, or QString() if there is no matching item.

See also substringCompletion.

[signal] void KCompletion::match(const QString &item)

This signal is emitted when a match is found.

In particular, makeCompletion(), previousMatch() and nextMatch() all emit this signal; makeCompletion() will only emit it when a match is found, but the other methods will always emit it (and so may emit it with an empty string).

item the matching item, or QString() if there were no more matching items.

[signal] void KCompletion::matches(const QStringList &matchlist)

This signal is emitted by makeCompletion() in shell-completion mode when the same string is passed to makeCompletion() multiple times in a row.

matchlist the list of all matching items

[signal] void KCompletion::multipleMatches()

This signal is emitted when calling makeCompletion() and more than one matching item is found.

See also hasMultipleMatches.

[slot] QString KCompletion::nextMatch()

Returns the next item from the list of matching items.

When reaching the last item, the list is rotated, so it will return the first match and a sound is emitted (depending on soundsEnabled()).

Returns the next item from the list of matching items. When there is no match, QString() is returned and a sound is emitted.

KCompletion::CompOrder KCompletion::order() const

Returns the completion order.

Note: Getter function for property order.

See also setOrder.

[virtual protected] void KCompletion::postProcessMatch(QString *match) const

This method is called after a completion is found and before the matching string is emitted. You can override this method to modify the string that will be emitted. This is necessary e.g. in KUrlCompletion(), where files with spaces in their names are shown escaped ("filename with spaces"), but stored unescaped inside KCompletion. Never delete that pointer!

Default implementation does nothing.

match the match to process

See also postProcessMatches.

[virtual protected] void KCompletion::postProcessMatches(KCompletionMatches *matches) const

This method is called before a list of all available completions is emitted via #matches(). You can override this method to modify the found items before #match() or #matches() are emitted. Never delete that pointer!

Default implementation does nothing.

matches the matches to process

See also postProcessMatch.

[virtual protected] void KCompletion::postProcessMatches(QStringList *matchList) const

This method is called before a list of all available completions is emitted via matches(). You can override this method to modify the found items before match() or matches() are emitted. Never delete that pointer!

Default implementation does nothing.

matchList the matches to process

See also postProcessMatch.

[slot] QString KCompletion::previousMatch()

Returns the next item from the list of matching items.

When reaching the beginning, the list is rotated so it will return the last match and a sound is emitted (depending on soundsEnabled()).

Returns the next item from the list of matching items. When there is no match, QString() is returned and a sound is emitted.

[slot] void KCompletion::removeItem(const QString &item)

Removes an item from the list of available completions. Resets the current item state (previousMatch() and nextMatch() won't work the next time they are called).

item the item to remove

[virtual] void KCompletion::setCompletionMode(KCompletion::CompletionMode mode)

Sets the completion mode.

mode the completion mode

See also completionMode() and CompletionMode.

[virtual] void KCompletion::setIgnoreCase(bool ignoreCase)

Setting this to true makes KCompletion behave case insensitively.

E.g. makeCompletion("CA"); might return "carp\cs.tu-berlin.de".

Default is false (case sensitive).

ignoreCase true to ignore the case

Note: Setter function for property ignoreCase.

See also ignoreCase.

[virtual slot] void KCompletion::setItems(const QStringList &itemList)

Sets the list of items available for completion. Removes all previous items.

Note: When order() == Weighted, then the weighting is looked up for every item in the stringlist. Every item should have ":number" appended, where number is an unsigned integer, specifying the weighting. If you don't like this, call setOrder(KCompletion::Insertion) before calling setItems().

itemList the list of items that are available for completion

Note: Setter function for property items.

See also items.

[virtual] void KCompletion::setOrder(KCompletion::CompOrder order)

KCompletion offers three different ways in which it offers its items:

  • in the order of insertion
  • sorted alphabetically
  • weighted

Choosing weighted makes KCompletion perform an implicit weighting based on how often an item is inserted. Imagine a web browser with a location bar, where the user enters URLs. The more often a URL is entered, the higher priority it gets.

Note: Setting the order to sorted only affects new inserted items, already existing items will stay in the current order. So you probably want to call setOrder(Sorted) before inserting items if you want everything sorted.

Default is insertion order.

order the new order

Note: Setter function for property order.

See also order.

[protected, since 5.87] void KCompletion::setShouldAutoSuggest(bool shouldAutosuggest)

Deriving classes may set this property and control whether the auto-suggestion should be displayed for the last completion operation performed.

Applies for CompletionPopupAuto and CompletionAuto modes.

This function was introduced in 5.87.

See also shouldAutoSuggest().

[protected, since 5.88] void KCompletion::setSorterFunction(KCompletion::SorterFunction sortFunc)

Sets a custom function to be used to sort the matches. Can be set to nullptr to use the default sorting logic.

Applies for CompOrder::Sorted mode.

This function was introduced in 5.88.

[virtual] void KCompletion::setSoundsEnabled(bool enable)

Enables/disables emitting a sound when

  • makeCompletion() can't find a match
  • there is a partial completion (= multiple matches in Shell-completion mode)
  • nextMatch() or previousMatch() hit the last possible match and the list is rotated

KNotifyClient() is used to emit the sounds.

enable true to enable sounds

See also soundsEnabled.

[since 5.87] bool KCompletion::shouldAutoSuggest() const

Informs the caller if they should display the auto-suggestion for the last completion operation performed.

Applies for CompletionPopupAuto and CompletionAuto modes.

Defaults to true, but deriving classes may set it to false in special cases via "setShouldAutoSuggest".

Returns true if auto-suggestion should be displayed for the last completion operation performed.

This function was introduced in 5.87.

See also setShouldAutoSuggest().

bool KCompletion::soundsEnabled() const

Tells you whether KCompletion will emit sounds on certain occasions.

Default is enabled.

Returns true if sounds are enabled

See also setSoundsEnabled.

QStringList KCompletion::substringCompletion(const QString &string) const

Returns a list of all completion items that contain the given string. string the string to complete

Returns a list of items which contain text as a substring, i.e. not necessarily at the beginning.

See also makeCompletion.