kcompletion.h
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1999,2000 Carsten Pfeiffer <pfeiffer@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #ifndef KCOMPLETION_H 00021 #define KCOMPLETION_H 00022 00023 #include <tqmap.h> 00024 #include <tqptrlist.h> 00025 #include <tqobject.h> 00026 #include <tqstring.h> 00027 #include <tqstringlist.h> 00028 #include <tqguardedptr.h> 00029 00030 #include "tdelibs_export.h" 00031 #include <tdeglobalsettings.h> 00032 #include <ksortablevaluelist.h> 00033 #include <tdeshortcut.h> 00034 00035 class TDECompTreeNode; 00036 class TDECompletionPrivate; 00037 class TDECompletionBasePrivate; 00038 class TDECompletionMatchesWrapper; 00039 class TDECompletionMatches; 00040 class TQPopupMenu; 00041 00132 class TDECORE_EXPORT TDECompletion : public TQObject 00133 { 00134 TQ_ENUMS( CompOrder ) 00135 TQ_PROPERTY( CompOrder order READ order WRITE setOrder ) 00136 TQ_PROPERTY( bool ignoreCase READ ignoreCase WRITE setIgnoreCase ) 00137 TQ_PROPERTY( TQStringList items READ items WRITE setItems ) 00138 Q_OBJECT 00139 00140 public: 00145 enum CompOrder { Sorted, 00146 Insertion, 00147 Weighted 00148 }; 00149 00153 TDECompletion(); 00154 00155 // FIXME: copy constructor, assignment operator... 00156 00160 virtual ~TDECompletion(); 00161 00184 virtual TQString makeCompletion( const TQString& string ); 00185 00194 TQStringList substringCompletion( const TQString& string ) const; 00195 00205 TQString previousMatch(); 00206 00216 TQString nextMatch(); 00217 00224 virtual const TQString& lastMatch() const { return myLastMatch; } 00225 00244 TQStringList items() const; 00245 00249 bool isEmpty() const; 00250 00260 virtual void setCompletionMode( TDEGlobalSettings::Completion mode ); 00261 00269 TDEGlobalSettings::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 TQStringList allMatches(); 00326 00332 TQStringList allMatches( const TQString& string ); 00333 00346 TDECompletionMatches allWeightedMatches(); 00347 00353 TDECompletionMatches allWeightedMatches( const TQString& 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 TQString& string ) { 00408 (void) makeCompletion( string ); 00409 } 00410 00416 void slotPreviousMatch() { 00417 (void) previousMatch(); 00418 } 00419 00425 void slotNextMatch() { 00426 (void) nextMatch(); 00427 } 00428 00429 // FIXME ###: KDE4: unify the nomenclature. We have insertItems, addItem, 00430 // setItems... 00436 void insertItems( const TQStringList& items ); 00437 00453 virtual void setItems( const TQStringList& list); 00454 00461 void addItem( const TQString& item); 00462 00474 void addItem( const TQString& item, uint weight ); 00475 00482 void removeItem( const TQString& item); 00483 00487 virtual void clear(); 00488 00489 00490 signals: 00497 void match( const TQString& item); 00498 00505 void matches( const TQStringList& matchlist); 00506 00512 void multipleMatches(); 00513 00514 protected: 00528 virtual void postProcessMatch( TQString *match ) const { Q_UNUSED(match) } 00529 00540 virtual void postProcessMatches( TQStringList * matches ) const { Q_UNUSED(matches)} 00541 00552 virtual void postProcessMatches( TDECompletionMatches * matches ) const {Q_UNUSED(matches)} 00553 00554 private: 00555 void addWeightedItem( const TQString& ); 00556 TQString findCompletion( const TQString& string ); 00557 void findAllCompletions( const TQString&, 00558 TDECompletionMatchesWrapper *matches, 00559 bool& hasMultipleMatches ) const; 00560 00561 void extractStringsFromNode( const TDECompTreeNode *, 00562 const TQString& beginning, 00563 TDECompletionMatchesWrapper *matches, 00564 bool addWeight = false ) const; 00565 void extractStringsFromNodeCI( const TDECompTreeNode *, 00566 const TQString& beginning, 00567 const TQString& restString, 00568 TDECompletionMatchesWrapper *matches) const; 00569 00570 enum BeepMode { NoMatch, PartialMatch, Rotation }; 00571 void doBeep( BeepMode ) const; 00572 00573 TDEGlobalSettings::Completion myCompletionMode; 00574 00575 CompOrder myOrder; 00576 TQString myLastString; 00577 TQString myLastMatch; 00578 TQString myCurrentMatch; 00579 TDECompTreeNode * myTreeRoot; 00580 TQStringList 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 TDECompletionPrivate *d; 00591 }; 00592 00593 // some more helper stuff 00594 typedef KSortableValueList<TQString> TDECompletionMatchesList; 00595 class TDECompletionMatchesPrivate; 00596 00615 class TDECORE_EXPORT TDECompletionMatches : public TDECompletionMatchesList 00616 { 00617 public: 00618 TDECompletionMatches( bool sort ); 00622 TDECompletionMatches( const TDECompletionMatchesWrapper& matches ); 00623 ~TDECompletionMatches(); 00628 void removeDuplicates(); 00635 TQStringList list( bool sort = true ) const; 00641 bool sorting() const { 00642 return _sorting; 00643 } 00644 private: 00645 bool _sorting; 00646 TDECompletionMatchesPrivate* d; 00647 }; 00648 00663 class TDECORE_EXPORT TDECompletionBase 00664 { 00665 public: 00671 enum KeyBindingType { 00675 TextCompletion, 00679 PrevCompletionMatch, 00683 NextCompletionMatch, 00687 SubstringCompletion 00688 }; 00689 00690 00691 // Map for the key binding types mentioned above. 00692 typedef TQMap<KeyBindingType, TDEShortcut> KeyBindingMap; 00693 00697 TDECompletionBase(); 00698 00702 virtual ~TDECompletionBase(); 00703 00719 TDECompletion* completionObject( bool hsig = true ); 00720 00737 virtual void setCompletionObject( TDECompletion* 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( TDEGlobalSettings::Completion mode ); 00845 00854 TDEGlobalSettings::Completion completionMode() const { 00855 return m_delegate ? m_delegate->completionMode() : m_iCompletionMode; 00856 } 00857 00888 bool setKeyBinding( KeyBindingType item , const TDEShortcut& key ); 00889 00902 const TDEShortcut& 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 TQString& text ) = 0; 00934 00940 virtual void setCompletedItems( const TQStringList& items ) = 0; 00941 00953 TDECompletion* compObj() const { return m_delegate ? m_delegate->compObj() : (TDECompletion*) m_pCompObj; } 00954 00955 protected: 00964 KeyBindingMap getKeyBindings() const { return m_delegate ? m_delegate->getKeyBindings() : m_keyMap; } 00965 00971 void setDelegate( TDECompletionBase *delegate ); 00972 00978 TDECompletionBase *delegate() const { return m_delegate; } 00979 00980 private: 00981 // This method simply sets the autodelete boolean for 00982 // the completion object, the emit signals and handle 00983 // signals internally flags to the provided values. 00984 void setup( bool, bool, bool ); 00985 00986 // Flag that determined whether the completion object 00987 // should be deleted when this object is destroyed. 00988 bool m_bAutoDelCompObj; 00989 // Determines whether this widget handles completion signals 00990 // internally or not 00991 bool m_bHandleSignals; 00992 // Determines whether this widget fires rotation signals 00993 bool m_bEmitSignals; 00994 // Stores the completion mode locally. 00995 TDEGlobalSettings::Completion m_iCompletionMode; 00996 // Pointer to Completion object. 00997 TQGuardedPtr<TDECompletion> m_pCompObj; 00998 // Keybindings 00999 KeyBindingMap m_keyMap; 01000 // we may act as a proxy to another TDECompletionBase object 01001 TDECompletionBase *m_delegate; 01002 01003 // BCI 01004 protected: 01005 virtual void virtual_hook( int id, void* data ); 01006 private: 01007 TDECompletionBasePrivate *d; 01008 }; 01009 01010 #endif // KCOMPLETION_H