|
|
This class let's you easily use "auto-completion", "manual-completion" or "shell completion" on QString objects. A common use is completing filenames or URLs (see KURLCompletion). But URL-completion is not all, everything should be completable. The user should be able to complete email-addresses, telephone-numbers, commands, SQL queries, ... Everytime 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 LineEdit-widget (KLineEdit), it is even more easy. Basically, you tell a KCompletion-object what strings should be completable and then, whenever the user types something, 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 via the method allMatches().
Notice: auto-completion, shell completion and manual completion work slightly differently:
You don't have to worry much about that though, KCompletion handles that for you, according to the setting setCompletionMode(). The default setting is globally configured by the user and read from KGlobalSettings::completionMode().
A short example:
KCompletion completion; completion.setSorted( true ); 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" ); debug( completion.makeCompletion( "ca" ).latin1() );
In shell-completion-mode, this will be "carp"; in auto-completion- mode it will return "carp@cs.tu-berlin.de", as that is alphabetically smaller. If setSorted was set to false (default), "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 every 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 nul), as this is used internally as a delimiter.
You may inherit from KCompletion and override makeCompletion() in special cases (like reading directories/urls and then supplying the contents to KCompletion, as KURLCompletion does), but generally, this is not necessary.
|
Constructor, nothing special here :)
~ |
[virtual]
Destructor, nothing special here, either.
QString |
[virtual]
Attempts to find an item in the list of available completions, that begins with string. Will either return the first (if more than one match) matching item or QString::null, if no match was found. In the latter case, a beep will be issued, depending on isSoundsEnabled(). If a match was found, it will also be emitted via the signal match().
If this is called twice or more often 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.
Returns: the matching item, or QString::null if there is no matching item.
QString |
Returns: the next item from the matching-items-list When reaching the beginning, the list is rotated, so it will return the last match. When there is no match, QString::null is returned and a beep will be issued, depending on isSoundsEnabled().
QString |
Returns: the previous item from the matching-items-list When reaching the last item, the list is rotated, so it will return the first match. When there is no match, QString::null is returned and a beep will be issued, depending on isSoundsEnabled().
const QString& |
[const virtual]
Returns: the last match. Might be useful if you need to check whether a completion is different from the last one. QString::null is returned when there is no last match.
QStringList |
[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.
void |
Sets the completion mode to Auto/Manual (see KCompletion documentation), Shell or None. If you don't set the mode explicitly, the global default value KGlobalSettings::completionMode() is used. KGlobalSettings::CompletionNone disables completion.
See also: completionMode, completionMode
KGlobalSettings::Completion |
[const]
Returns: the current completion mode. May be different from KGlobalSettings::completionMode(), if you explicitly called setCompletionMode().
void |
Setting this to true makes us go into sorted mode (doh). Completion will then always return the alphabetically first match. If set to false, the order is the same as the items were inserted. Note: this only affects new inserted items, already existing items will stay in the current order. So you probably want to call setSorted( true ) before inserting items, when you want everything sorted. Default is false, not sorted.
bool |
[const]
Returns: true if the completion-items are alphabetically sorted and false if the order of insertion is used.
QStringList |
Returns: a list of all matching items. Might take some time, when you have LOTS of items.
void |
Enables playing a sound when
Sounds are only played in shell-completion mode. Default is enabled
See also: disableSounds, isSoundEnabled
void |
Disables playing a sound when
Sounds are only played in shell-completion mode. Default is enabled
See also: enableSounds, isSoundEnabled
bool |
[const]
Tells you whether KCompletion will issue beeps (KApplication::beep()) Beeps only in manual-completion mode Default is enabled
See also: enableSounds, disableSounds
void |
[slot]
Attempts to complete "string" and emits the completion via match(). Same as makeCompletion() (just as a slot).
void |
[slot]
Searches the previous matching item and emits it via match() Same as previousMatch() (just as a slot).
void |
[slot]
Searches the next matching item and emits it via match() Same as nextMatch() (just as a slot).
bool |
[const slot]
Returns: true when more than one match is found
void |
[slot]
Sets the list of items available for completion. Removes all previous items.
void |
[slot]
Adds an item to the list of available completions. Resets the current item-state (previousMatch() and nextMatch() won't work anymore).
void |
[slot]
Removes an item from the list of available completions. Resets the current item-state (previousMatch() and nextMatch() won't work anymore).
void |
[slot]
Clears the list of inserted items.
void |
[signal]
The matching item. Will be emitted by makeCompletion(), previousMatch() or nextMatch(). May be QString::null if there is no matching item.
void |
[signal]
All matching items. Will be emitted by makeCompletion() in shell- completion-mode, when the same string is passed to makeCompletion twice or more often.
void |
[signal]
This signal is emitted, when calling makeCompletion() and more than one matching item is found.
void |
[protected virtual]
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.
void |
[protected virtual]
This method is called before a list of all available completions is emitted via matches. You can override this method to modify the list which that will be emitted. Never delete that pointer!
Default implementation does nothing.