summaryrefslogtreecommitdiff
path: root/libopie2/qt3/opiecore/ocompletion.h
Unidiff
Diffstat (limited to 'libopie2/qt3/opiecore/ocompletion.h') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/qt3/opiecore/ocompletion.h603
1 files changed, 603 insertions, 0 deletions
diff --git a/libopie2/qt3/opiecore/ocompletion.h b/libopie2/qt3/opiecore/ocompletion.h
new file mode 100644
index 0000000..0317c1b
--- a/dev/null
+++ b/libopie2/qt3/opiecore/ocompletion.h
@@ -0,0 +1,603 @@
1/*
2                 This file is part of the Opie Project
3 Originally part of the KDE Project
4 Copyright (C) 1999,2000 Carsten Pfeiffer <pfeiffer@kde.org>
5 =.
6 .=l.
7           .>+-=
8 _;:,     .>    :=|. This program is free software; you can
9.> <`_,   >  .   <= redistribute it and/or modify it under
10:`=1 )Y*s>-.--   : the terms of the GNU Library General Public
11.="- .-=="i,     .._ License as published by the Free Software
12 - .   .-<_>     .<> Foundation; either version 2 of the License,
13     ._= =}       : or (at your option) any later version.
14    .%`+i>       _;_.
15    .i_,=:_.      -<s. This program is distributed in the hope that
16     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
17    : ..    .:,     . . . without even the implied warranty of
18    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
19  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU
20..}^=.=       =       ; Library General Public License for more
21++=   -.     .`     .: details.
22 :     =  ...= . :.=-
23 -.   .:....=;==+<; You should have received a copy of the GNU
24  -_. . .   )=.  = Library General Public License along with
25    --        :-=` this library; see the file COPYING.LIB.
26 If not, write to the Free Software Foundation,
27 Inc., 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA.
29*/
30
31#ifndef OCOMPLETION_H
32#define OCOMPLETION_H
33
34/* QT */
35
36#include <qmap.h>
37#include <qlist.h>
38#include <qobject.h>
39#include <qstring.h>
40#include <qstringlist.h>
41#include <qguardedptr.h>
42
43/* OPIE */
44
45#include <opie2/oglobalsettings.h>
46#include <opie2/osortablevaluelist.h>
47
48/* FORWARDS */
49
50class OCompTreeNode;
51class OCompletionPrivate;
52class OCompletionBasePrivate;
53class OCompletionMatchesWrapper;
54class OCompletionMatches;
55class QPopupMenu;
56
57// FIXME: Do we need special ShortCut handling in Opie? If so, revise this.
58class OShortcut
59{
60public:
61 bool isNull() const { return true; };
62 bool operator == ( const OShortcut& bla ) const { return false; };
63};
64
65
66/**
67 * This class offers easy use of "auto-completion", "manual-completion" or
68 * "shell completion" on QString objects. A common use is completing filenames
69 * or URLs (see @ref OURLCompletion()).
70 * But it is not limited to URL-completion -- everything should be completable!
71 * The user should be able to complete email-addresses, telephone-numbers,
72 * commands, SQL queries, ...
73 * Every time your program knows what the user can type into an edit-field, you
74 * should offer completion. With OCompletion, this is very easy, and if you are
75 * using a line edit widget (@ref OLineEdit), it is even more easy.
76 * Basically, you tell a OCompletion object what strings should be completable
77 * and whenever completion should be invoked, you call @ref makeCompletion().
78 * OLineEdit and (an editable) OComboBox even do this automatically for you.
79 *
80 * OCompletion offers the completed string via the signal @ref match() and
81 * all matching strings (when the result is ambiguous) via the method
82 * @ref allMatches().
83 *
84 * Notice: auto-completion, shell completion and manual completion work
85 * slightly differently:
86 *
87 * @li auto-completion always returns a complete item as match.
88 * When more than one matching items are available, it will deliver just
89 * the first (depending on sorting order) item. Iterating over all matches
90 * is possible via @ref nextMatch() and @ref previousMatch().
91 *
92 * @li popup-completion works in the same way, the only difference being that
93 * the completed items are not put into the edit-widget, but into a
94 * separate popup-box.
95 *
96 * @li manual completion works the same way as auto-completion, the
97 * subtle difference is, that it isn't invoked automatically while the user
98 * is typing, but only when the user presses a special key. The difference
99 * of manual and auto-completion is therefore only visible in UI classes,
100 * OCompletion needs to know whether to deliver partial matches
101 * (shell completion) or whole matches (auto/manual completion), therefore
102 * @ref OGlobalSettings::CompletionMan and
103 * @ref OGlobalSettings::CompletionAuto have the exact same effect in
104 * OCompletion.
105 *
106 * @li shell completion works like how shells complete filenames:
107 * when multiple matches are available, the longest possible string of all
108 * matches is returned (i.e. only a partial item).
109 * Iterating over all matching items (complete, not partial) is possible
110 * via @ref nextMatch() and @ref previousMatch().
111 *
112 * You don't have to worry much about that though, OCompletion handles
113 * that for you, according to the setting @ref setCompletionMode().
114 * The default setting is globally configured by the user and read
115 * from @ref OGlobalSettings::completionMode().
116 *
117 * A short example:
118 * <pre>
119 * OCompletion completion;
120 * completion.setOrder( OCompletion::Sorted );
121 * completion.addItem( "pfeiffer@kde.org" );
122 * completion.addItem( "coolo@kde.org" );
123 * completion.addItem( "carpdjih@sp.zrz.tu-berlin.de" );
124 * completion.addItem( "carp@cs.tu-berlin.de" );
125 *
126 * cout << completion.makeCompletion( "ca" ).latin1() << endl;
127 * </pre>
128 * In shell-completion-mode, this will be "carp"; in auto-completion-
129 * mode it will be "carp@cs.tu-berlin.de", as that is alphabetically
130 * smaller.
131 * If setOrder was set to Insertion, "carpdjih@sp.zrz.tu-berlin.de"
132 * would be completed in auto-completion-mode, as that was inserted before
133 * "carp@cs.tu-berlin.de".
134 *
135 * You can dynamically update the completable items by removing and adding them
136 * whenever you want.
137 * For advanced usage, you could even use multiple OCompletion objects. E.g.
138 * imagine an editor like kwrite with multiple open files. You could store
139 * items of each file in a different OCompletion object, so that you know (and
140 * tell the user) where a completion comes from.
141 *
142 * Note: OCompletion does not work with strings that contain 0x0 characters
143 * (unicode nul), as this is used internally as a delimiter.
144 *
145 * You may inherit from OCompletion and override @ref makeCompletion() in
146 * special cases (like reading directories/urls and then supplying the
147 * contents to OCompletion, as OURLCompletion does), but generally, this is
148 * not necessary.
149 *
150 *
151 * @short A generic class for completing QStrings
152 * @author Carsten Pfeiffer <pfeiffer@kde.org>
153 * @version $Id$
154 */
155
156class OCompletion : public QObject
157{
158 Q_ENUMS( CompOrder )
159 Q_PROPERTY( CompOrder order READ order WRITE setOrder )
160 Q_PROPERTY( bool ignoreCase READ ignoreCase WRITE setIgnoreCase )
161 Q_PROPERTY( QStringList items READ items WRITE setItems )
162 Q_OBJECT
163
164 public:
165 /**
166 * Constants that represent the order in which OCompletion performs
167 * completion-lookups.
168 */
169 enum CompOrder { Sorted, Insertion, Weighted };
170
171 /**
172 * Constructor, nothing special here :)
173 */
174 OCompletion();
175
176 // FIXME: copy constructor, assignment constructor...
177
178 /**
179 * Destructor, nothing special here, either.
180 */
181 virtual ~OCompletion();
182
183 /**
184 * Attempts to find an item in the list of available completions,
185 * that begins with @p string. Will either return the first matching item
186 * (if there is more than one match) or QString::null, if no match was
187 * found.
188 *
189 * In the latter case, a sound will be issued, depending on
190 * @ref isSoundsEnabled().
191 * If a match was found, it will also be emitted via the signal
192 * @ref match().
193 *
194 * If this is called twice or more often with the same string while no
195 * items were added or removed in the meantime, all available completions
196 * will be emitted via the signal @ref matches().
197 * This happens only in shell-completion-mode.
198 *
199 * @returns the matching item, or QString::null if there is no matching
200 * item.
201 * @see #slotMakeCompletion
202 * @see #substringCompletion
203 */
204 virtual QString makeCompletion( const QString& string );
205
206 /**
207 * @returns a list of items which all contain @p text as a substring,
208 * i.e. not necessarily at the beginning.
209 *
210 * @see #makeCompletion
211 */
212 QStringList substringCompletion( const QString& string ) const;
213
214 /**
215 * @returns the next item from the matching-items-list.
216 * When reaching the beginning, the list is rotated so it will return the
217 * last match and a sound is issued (depending on @ref isSoundsEnabled()).
218 * When there is no match, QString::null is returned and
219 * a sound is be issued.
220 * @see #slotPreviousMatch
221 */
222 QString previousMatch();
223
224 /**
225 * @returns the previous item from the matching-items-list
226 * When reaching the last item, the list is rotated, so it will return
227 * the first match and a sound is issued (depending on
228 * @ref isSoundsEnabled()). When there is no match, QString::null is
229 * returned and a sound is issued.
230 * @see #slotNextMatch
231 */
232 QString nextMatch();
233
234 /**
235 * @returns the last match. Might be useful if you need to check whether
236 * a completion is different from the last one.
237 * QString::null is returned when there is no last match.
238 */
239 virtual const QString& lastMatch() const { return myLastMatch; }
240
241 /**
242 * Returns a list of all items inserted into OCompletion. This is useful
243 * if you need to save the state of a OCompletion object and restore it
244 * later.
245 *
246 * Important note: when @ref order() == Weighted, then every item in the
247 * stringlist has its weight appended, delimited by a colon. E.g. an item
248 * "www.kde.org" might look like "www.kde.org:4", where 4 is the weight.
249 *
250 * This is necessary so that you can save the items along with its
251 * weighting on disk and load them back with @ref setItems(), restoring its
252 * weight as well. If you really don't want the appended weightings, call
253 * @ref setOrder( OCompletion::Insertion )
254 * before calling items().
255 *
256 * @returns a list of all items
257 * @see #setItems
258 */
259 QStringList items() const;
260
261 /**
262 * Sets the completion mode to Auto/Manual, Shell or None.
263 * If you don't set the mode explicitly, the global default value
264 * OGlobalSettings::completionMode() is used.
265 * @ref OGlobalSettings::CompletionNone disables completion.
266 * @see #completionMode
267 * @see #OGlobalSettings::completionMode
268 */
269 virtual void setCompletionMode( OGlobalSettings::Completion mode );
270
271 /**
272 * @returns the current completion mode.
273 * May be different from @ref OGlobalSettings::completionMode(), if you
274 * explicitly called @ref setCompletionMode().
275 * @see #setCompletionMode
276 */
277 OGlobalSettings::Completion completionMode() const { return myCompletionMode; };
278
279 /**
280 * OCompletion offers three different ways in which it offers its items:
281 * @li in the order of insertion
282 * @li sorted alphabetically
283 * @li weighted
284 *
285 * Choosing weighted makes OCompletion perform an implicit weighting based
286 * on how often an item is inserted. Imagine a web browser with a location
287 * bar, where the user enters URLs. The more often a URL is entered, the
288 * higher priority it gets.
289 *
290 * Note: Setting the order to sorted only affects new inserted items,
291 * already existing items will stay in the current order. So you probably
292 * want to call setOrder( Sorted ) before inserting items, when you want
293 * everything sorted.
294 *
295 * Default is insertion order
296 * @see #order
297 */
298 virtual void setOrder( CompOrder order );
299
300 /**
301 * @returns the current completion order.
302 * @see #setOrder
303 */
304 CompOrder order() const { return myOrder; }
305
306 /**
307 * Setting this to true makes OCompletion behave case insensitively.
308 * E.g. makeCompletion( "CA" ); might return "carp@cs.tu-berlin.de".
309 * Default is false (case sensitive).
310 * @see #ignoreCase
311 */
312 virtual void setIgnoreCase( bool ignoreCase );
313
314 /**
315 * @returns whether OCompletion acts case insensitively or not.
316 * Default is false (case sensitive).
317 * @see #setIgnoreCase
318 */
319 bool ignoreCase() const { return myIgnoreCase; };
320
321 /**
322 * @returns a list of all items matching the last completed string.
323 * Might take some time, when you have LOTS of items.
324 *
325 * @see #substringCompletion
326 */
327 QStringList allMatches();
328
329 /**
330 * @returns a list of all items matching @p string.
331 */
332 QStringList allMatches( const QString& string );
333
334 /**
335 * @returns a list of all items matching the last completed string.
336 * Might take some time, when you have LOTS of items.
337 * The matches are returned as OCompletionMatches, which also
338 * keeps the weight of the matches, allowing
339 * you to modify some matches or merge them with matches
340 * from another call to allWeightedMatches(), and sort the matches
341 * after that in order to have the matches ordered correctly
342 *
343 * @see #substringCompletion
344 */
345 OCompletionMatches allWeightedMatches();
346
347 /**
348 * @returns a list of all items matching @p string.
349 */
350 OCompletionMatches allWeightedMatches( const QString& string );
351
352 /**
353 * Enables/disables playing a sound when
354 * @li @ref makeCompletion() can't find a match
355 * @li there is a partial completion (= multiple matches in
356 * Shell-completion mode)
357 * @li @ref nextMatch() or @ref previousMatch() hit the last possible
358 * match -> rotation
359 *
360 * For playing the sounds, @ref ONotifyClient() is used. // FIXME: Revise this for Opie
361 *
362 * @see #isSoundsEnabled
363 */
364 virtual void setEnableSounds( bool enable ) { myBeep = enable; }
365
366 /**
367 * Tells you whether OCompletion will play sounds on certain occasions.
368 * Default is enabled
369 * @see #enableSounds
370 * @see #disableSounds
371 */
372 bool isSoundsEnabled() const { return myBeep; };
373
374 /**
375 * @returns true when more than one match is found
376 * @see #multipleMatches
377 */
378 bool hasMultipleMatches() const { return myHasMultipleMatches; };
379
380 public slots:
381 /**
382 * Attempts to complete "string" and emits the completion via @ref match().
383 * Same as @ref makeCompletion() (just as a slot).
384 * @see #makeCompletion
385 */
386 void slotMakeCompletion( const QString& string ) { (void) makeCompletion( string ); };
387
388 /**
389 * Searches the previous matching item and emits it via @ref match()
390 * Same as @ref previousMatch() (just as a slot).
391 * @see #previousMatch
392 */
393 void slotPreviousMatch() { (void) previousMatch(); };
394
395 /**
396 * Searches the next matching item and emits it via @ref match()
397 * Same as @ref nextMatch() (just as a slot).
398 * @see #nextMatch
399 */
400 void slotNextMatch() { (void) nextMatch(); };
401
402 /**
403 * Inserts @p items into the list of possible completions.
404 * Does the same as @ref setItems(), but does not call @ref clear() before.
405 */
406 void insertItems( const QStringList& items );
407
408 /**
409 * Sets the list of items available for completion. Removes all previous
410 * items.
411 *
412 * Notice: when order() == Weighted, then the weighting is looked up for
413 * every item in the stringlist. Every item should have ":number" appended,
414 * where number is an unsigned integer, specifying the weighting.
415 *
416 * If you don't like this, call
417 * setOrder( OCompletion::Insertion )
418 * before calling setItems().
419 *
420 * @see #items
421 */
422 virtual void setItems( const QStringList& );
423
424 /**
425 * Adds an item to the list of available completions.
426 * Resets the current item-state (@ref previousMatch() and @ref nextMatch()
427 * won't work anymore).
428 */
429 void addItem( const QString& );
430
431 /**
432 * Adds an item to the list of available completions.
433 * Resets the current item-state (@ref previousMatch() and @ref nextMatch()
434 * won't work anymore).
435 *
436 * Sets the weighting of the item to @p weight or adds it to the current
437 * weighting if the item is already available. The weight has to be greater
438 * than 1 to take effect (default weight is 1).
439 */
440 void addItem( const QString&, uint weight );
441
442 /**
443 * Removes an item from the list of available completions.
444 * Resets the current item-state (@ref previousMatch() and @ref nextMatch()
445 * won't work anymore).
446 */
447 void removeItem( const QString& );
448
449 /**
450 * Removes all inserted items.
451 */
452 virtual void clear();
453
454 signals:
455 /**
456 * The matching item. Will be emitted by @ref makeCompletion(),
457 * @ref previousMatch() or @ref nextMatch(). May be QString::null if there
458 * is no matching item.
459 */
460 void match( const QString& );
461
462 /**
463 * All matching items. Will be emitted by @ref makeCompletion() in shell-
464 * completion-mode, when the same string is passed to makeCompletion twice
465 * or more often.
466 */
467 void matches( const QStringList& );
468
469 /**
470 * This signal is emitted, when calling @ref makeCompletion() and more than
471 * one matching item is found.
472 * @see #hasMultipleMatches
473 */
474 void multipleMatches();
475
476 protected:
477 /**
478 * This method is called after a completion is found and before the
479 * matching string is emitted. You can override this method to modify the
480 * string that will be emitted.
481 * This is necessary e.g. in @ref OURLCompletion(), where files with spaces
482 * in their names are shown escaped ("filename\ with\ spaces"), but stored
483 * unescaped inside OCompletion.
484 * Never delete that pointer!
485 *
486 * Default implementation does nothing.
487 * @see #postProcessMatches
488 */
489 virtual void postProcessMatch( QString * /*match*/ ) const {}
490
491 /**
492 * This method is called before a list of all available completions is
493 * emitted via @ref matches. You can override this method to modify the
494 * found items before @ref match() or @ref matches() are emitted.
495 * Never delete that pointer!
496 *
497 * Default implementation does nothing.
498 * @see #postProcessMatch
499 */
500 virtual void postProcessMatches( QStringList * /*matches*/ ) const {}
501
502 /**
503 * This method is called before a list of all available completions is
504 * emitted via @ref matches. You can override this method to modify the
505 * found items before @ref match() or @ref matches() are emitted.
506 * Never delete that pointer!
507 *
508 * Default implementation does nothing.
509 * @see #postProcessMatch
510 */
511 virtual void postProcessMatches( OCompletionMatches * /*matches*/ ) const {}
512
513private:
514 void addWeightedItem( const QString& );
515 QString findCompletion( const QString& string );
516 void findAllCompletions( const QString&, OCompletionMatchesWrapper *matches, bool& hasMultipleMatches ) const;
517
518 void extractStringsFromNode( const OCompTreeNode *,
519 const QString& beginning,
520 OCompletionMatchesWrapper *matches,
521 bool addWeight = false ) const;
522 void extractStringsFromNodeCI( const OCompTreeNode *,
523 const QString& beginning,
524 const QString& restString,
525 OCompletionMatchesWrapper *matches) const;
526
527 enum BeepMode { NoMatch, PartialMatch, Rotation };
528 void doBeep( BeepMode ) const;
529
530 OGlobalSettings::Completion myCompletionMode;
531
532 CompOrder myOrder;
533 QString myLastString;
534 QString myLastMatch;
535 QString myCurrentMatch;
536 OCompTreeNode * myTreeRoot;
537 QStringList myRotations;
538 bool myBeep;
539 bool myIgnoreCase;
540 bool myHasMultipleMatches;
541 uint myRotationIndex;
542
543 private:
544 OCompletionPrivate *d;
545};
546
547// some more helper stuff
548typedef OSortableValueList<QString> OCompletionMatchesList;
549class OCompletionMatchesPrivate;
550
551/**
552 * This structure is returned by @ref OCompletion::allWeightedMatches .
553 * It also keeps the weight of the matches, allowing
554 * you to modify some matches or merge them with matches
555 * from another call to allWeightedMatches(), and sort the matches
556 * after that in order to have the matches ordered correctly
557 *
558 * Example (a simplified example of what Oonqueror's completion does):
559 * <pre>
560 * OCompletionMatches matches = completion->allWeightedMatches( location );
561 * if( !location.startsWith( "www." ))
562 matches += completion->allWeightedmatches( "www." + location" );
563 * matches.removeDuplicates();
564 * QStringList list = matches.list();
565 * </pre>
566 *
567 * @short List for keeping matches returned from OCompletion
568 */
569
570class OCompletionMatches
571 : public OCompletionMatchesList
572{
573 public:
574 OCompletionMatches( bool sort );
575 /**
576 * @internal
577 */
578 OCompletionMatches( const OCompletionMatchesWrapper& matches );
579 ~OCompletionMatches();
580 /**
581 * Removes duplicate matches. Needed only when you merged several matches
582 * results and there's a possibility of duplicates.
583 */
584 void removeDuplicates();
585 /**
586 * Returns the matches as a QStringList.
587 * @param sort if false, the matches won't be sorted before the conversion,
588 * use only if you're sure the sorting is not needed
589 */
590 QStringList list( bool sort = true ) const;
591 /**
592 * If sorting() returns false, the matches aren't sorted by their weight,
593 * even if true is passed to list().
594 */
595 bool sorting() const {
596 return _sorting;
597 }
598private:
599 bool _sorting;
600 OCompletionMatchesPrivate* d;
601};
602
603#endif // OCOMPLETION_H