KDECore
kcompletion.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef KCOMPLETION_H
00021 #define KCOMPLETION_H
00022
00023 #include <qmap.h>
00024 #include <qptrlist.h>
00025 #include <qobject.h>
00026 #include <qstring.h>
00027 #include <qstringlist.h>
00028 #include <qguardedptr.h>
00029
00030 #include "kdelibs_export.h"
00031 #include <kglobalsettings.h>
00032 #include <ksortablevaluelist.h>
00033 #include <kshortcut.h>
00034
00035 class KCompTreeNode;
00036 class KCompletionPrivate;
00037 class KCompletionBasePrivate;
00038 class KCompletionMatchesWrapper;
00039 class KCompletionMatches;
00040 class QPopupMenu;
00041
00132 class KDECORE_EXPORT KCompletion : public QObject
00133 {
00134 Q_ENUMS( CompOrder )
00135 Q_PROPERTY( CompOrder order READ order WRITE setOrder )
00136 Q_PROPERTY( bool ignoreCase READ ignoreCase WRITE setIgnoreCase )
00137 Q_PROPERTY( QStringList items READ items WRITE setItems )
00138 Q_OBJECT
00139
00140 public:
00145 enum CompOrder { Sorted,
00146 Insertion,
00147 Weighted
00148 };
00149
00153 KCompletion();
00154
00155
00156
00160 virtual ~KCompletion();
00161
00184 virtual QString makeCompletion( const QString& string );
00185
00194 QStringList substringCompletion( const QString& string ) const;
00195
00205 QString previousMatch();
00206
00216 QString nextMatch();
00217
00224 virtual const QString& lastMatch() const { return myLastMatch; }
00225
00244 QStringList items() const;
00245
00249 bool isEmpty() const;
00250
00260 virtual void setCompletionMode( KGlobalSettings::Completion mode );
00261
00269 KGlobalSettings::Completion completionMode() const {
00270 return myCompletionMode;
00271 }
00272
00293 virtual void setOrder( CompOrder order );
00294
00300 CompOrder order() const { return myOrder; }
00301
00309 virtual void setIgnoreCase( bool ignoreCase );
00310
00317 bool ignoreCase() const { return myIgnoreCase; }
00318
00325 QStringList allMatches();
00326
00332 QStringList allMatches( const QString& string );
00333
00346 KCompletionMatches allWeightedMatches();
00347
00353 KCompletionMatches allWeightedMatches( const QString& string );
00354
00368 virtual void setEnableSounds( bool enable ) { myBeep = enable; }
00369
00377 bool isSoundsEnabled() const { return myBeep; }
00378
00384 bool hasMultipleMatches() const { return myHasMultipleMatches; }
00385
00386 #ifndef KDE_NO_COMPAT
00387
00391 void enableSounds() { myBeep = true; }
00392
00397 void disableSounds() { myBeep = false; }
00398 #endif
00399
00400 public slots:
00407 void slotMakeCompletion( const QString& string ) {
00408 (void) makeCompletion( string );
00409 }
00410
00416 void slotPreviousMatch() {
00417 (void) previousMatch();
00418 }
00419
00425 void slotNextMatch() {
00426 (void) nextMatch();
00427 }
00428
00429
00430
00436 void insertItems( const QStringList& items );
00437
00453 virtual void setItems( const QStringList& list);
00454
00461 void addItem( const QString& item);
00462
00474 void addItem( const QString& item, uint weight );
00475
00482 void removeItem( const QString& item);
00483
00487 virtual void clear();
00488
00489
00490 signals:
00497 void match( const QString& item);
00498
00505 void matches( const QStringList& matchlist);
00506
00512 void multipleMatches();
00513
00514 protected:
00528 virtual void postProcessMatch( QString *match ) const { Q_UNUSED(match) }
00529
00540 virtual void postProcessMatches( QStringList * matches ) const { Q_UNUSED(matches)}
00541
00552 virtual void postProcessMatches( KCompletionMatches * matches ) const {Q_UNUSED(matches)}
00553
00554 private:
00555 void addWeightedItem( const QString& );
00556 QString findCompletion( const QString& string );
00557 void findAllCompletions( const QString&,
00558 KCompletionMatchesWrapper *matches,
00559 bool& hasMultipleMatches ) const;
00560
00561 void extractStringsFromNode( const KCompTreeNode *,
00562 const QString& beginning,
00563 KCompletionMatchesWrapper *matches,
00564 bool addWeight = false ) const;
00565 void extractStringsFromNodeCI( const KCompTreeNode *,
00566 const QString& beginning,
00567 const QString& restString,
00568 KCompletionMatchesWrapper *matches) const;
00569
00570 enum BeepMode { NoMatch, PartialMatch, Rotation };
00571 void doBeep( BeepMode ) const;
00572
00573 KGlobalSettings::Completion myCompletionMode;
00574
00575 CompOrder myOrder;
00576 QString myLastString;
00577 QString myLastMatch;
00578 QString myCurrentMatch;
00579 KCompTreeNode * myTreeRoot;
00580 QStringList myRotations;
00581 bool myBeep;
00582 bool myIgnoreCase;
00583 bool myHasMultipleMatches;
00584 uint myRotationIndex;
00585
00586
00587 protected:
00588 virtual void virtual_hook( int id, void* data );
00589 private:
00590 KCompletionPrivate *d;
00591 };
00592
00593
00594 typedef KSortableValueList<QString> KCompletionMatchesList;
00595 class KCompletionMatchesPrivate;
00596
00615 class KDECORE_EXPORT KCompletionMatches : public KCompletionMatchesList
00616 {
00617 public:
00618 KCompletionMatches( bool sort );
00622 KCompletionMatches( const KCompletionMatchesWrapper& matches );
00623 ~KCompletionMatches();
00628 void removeDuplicates();
00635 QStringList list( bool sort = true ) const;
00641 bool sorting() const {
00642 return _sorting;
00643 }
00644 private:
00645 bool _sorting;
00646 KCompletionMatchesPrivate* d;
00647 };
00648
00663 class KDECORE_EXPORT KCompletionBase
00664 {
00665 public:
00671 enum KeyBindingType {
00675 TextCompletion,
00679 PrevCompletionMatch,
00683 NextCompletionMatch,
00687 SubstringCompletion
00688 };
00689
00690
00691
00692 typedef QMap<KeyBindingType, KShortcut> KeyBindingMap;
00693
00697 KCompletionBase();
00698
00702 virtual ~KCompletionBase();
00703
00719 KCompletion* completionObject( bool hsig = true );
00720
00737 virtual void setCompletionObject( KCompletion* compObj, bool hsig = true );
00738
00751 virtual void setHandleSignals( bool handle );
00752
00763 bool isCompletionObjectAutoDeleted() const {
00764 return m_delegate ? m_delegate->isCompletionObjectAutoDeleted() : m_bAutoDelCompObj;
00765 }
00766
00776 void setAutoDeleteCompletionObject( bool autoDelete ) {
00777 if ( m_delegate )
00778 m_delegate->setAutoDeleteCompletionObject( autoDelete );
00779 else
00780 m_bAutoDelCompObj = autoDelete;
00781 }
00782
00803 void setEnableSignals( bool enable ) {
00804 if ( m_delegate )
00805 m_delegate->setEnableSignals( enable );
00806 else
00807 m_bEmitSignals = enable;
00808 }
00809
00815 bool handleSignals() const { return m_delegate ? m_delegate->handleSignals() : m_bHandleSignals; }
00816
00822 bool emitSignals() const { return m_delegate ? m_delegate->emitSignals() : m_bEmitSignals; }
00823
00844 virtual void setCompletionMode( KGlobalSettings::Completion mode );
00845
00854 KGlobalSettings::Completion completionMode() const {
00855 return m_delegate ? m_delegate->completionMode() : m_iCompletionMode;
00856 }
00857
00888 bool setKeyBinding( KeyBindingType item , const KShortcut& key );
00889
00902 const KShortcut& getKeyBinding( KeyBindingType item ) const {
00903 return m_delegate ? m_delegate->getKeyBinding( item ) : m_keyMap[ item ];
00904 }
00905
00917 void useGlobalKeyBindings();
00918
00933 virtual void setCompletedText( const QString& text ) = 0;
00934
00940 virtual void setCompletedItems( const QStringList& items ) = 0;
00941
00953 KCompletion* compObj() const { return m_delegate ? m_delegate->compObj() : (KCompletion*) m_pCompObj; }
00954
00955 protected:
00964 KeyBindingMap getKeyBindings() const { return m_delegate ? m_delegate->getKeyBindings() : m_keyMap; }
00965
00971 void setDelegate( KCompletionBase *delegate );
00972
00978 KCompletionBase *delegate() const { return m_delegate; }
00979
00980 private:
00981
00982
00983
00984 void setup( bool, bool, bool );
00985
00986
00987
00988 bool m_bAutoDelCompObj;
00989
00990
00991 bool m_bHandleSignals;
00992
00993 bool m_bEmitSignals;
00994
00995 KGlobalSettings::Completion m_iCompletionMode;
00996
00997 QGuardedPtr<KCompletion> m_pCompObj;
00998
00999 KeyBindingMap m_keyMap;
01000
01001 KCompletionBase *m_delegate;
01002
01003
01004 protected:
01005 virtual void virtual_hook( int id, void* data );
01006 private:
01007 KCompletionBasePrivate *d;
01008 };
01009
01010 #endif // KCOMPLETION_H