File indexing completed on 2024-04-28 03:53:08
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 1999, 2000 Carsten Pfeiffer <pfeiffer@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #ifndef KCOMPLETIONBASE_H 0009 #define KCOMPLETIONBASE_H 0010 0011 #include <kcompletion.h> 0012 #include <kcompletion_export.h> 0013 0014 #include <QMap> 0015 #include <memory> 0016 0017 class KCompletionBasePrivate; 0018 0019 /** 0020 * @class KCompletionBase kcompletionbase.h KCompletionBase 0021 * 0022 * An abstract base class for adding a completion feature 0023 * into widgets. 0024 * 0025 * This is a convenience class that provides the basic functions 0026 * needed to add text completion support into widgets. All that 0027 * is required is an implementation for the pure virtual function 0028 * setCompletedText(). Refer to KLineEdit or KComboBox 0029 * to see how easily such support can be added using this as a base 0030 * class. 0031 * 0032 * @short An abstract class for adding text completion support to widgets. 0033 * @author Dawit Alemayehu <adawit@kde.org> 0034 */ 0035 0036 class KCOMPLETION_EXPORT KCompletionBase 0037 { 0038 public: 0039 Q_DECLARE_PRIVATE(KCompletionBase) 0040 /** 0041 * Constants that represent the items whose shortcut 0042 * key binding is programmable. The default key bindings 0043 * for these items are defined in KStandardShortcut. 0044 */ 0045 enum KeyBindingType { 0046 /** 0047 * Text completion (by default Ctrl-E). 0048 */ 0049 TextCompletion, 0050 /** 0051 * Switch to previous completion (by default Ctrl-Up). 0052 */ 0053 PrevCompletionMatch, 0054 /** 0055 * Switch to next completion (by default Ctrl-Down). 0056 */ 0057 NextCompletionMatch, 0058 /** 0059 * Substring completion (by default Ctrl-T). 0060 */ 0061 SubstringCompletion, 0062 }; 0063 0064 // Map for the key binding types mentioned above. 0065 typedef QMap<KeyBindingType, QList<QKeySequence>> KeyBindingMap; 0066 0067 /** 0068 * Default constructor. 0069 */ 0070 KCompletionBase(); 0071 0072 /** 0073 * Destructor. 0074 */ 0075 virtual ~KCompletionBase(); 0076 0077 /** 0078 * Returns a pointer to the current completion object. 0079 * 0080 * If the completion object does not exist, it is automatically created and 0081 * by default handles all the completion signals internally unless @c handleSignals 0082 * is set to false. It is also automatically destroyed when the destructor 0083 * is called. You can change this default behavior using the 0084 * @ref setAutoDeleteCompletionObject and @ref setHandleSignals member 0085 * functions. 0086 * 0087 * See also @ref compObj. 0088 * 0089 * @param handleSignals if true, handles completion signals internally. 0090 * @return a pointer to the completion object. 0091 */ 0092 KCompletion *completionObject(bool handleSignals = true); 0093 0094 /** 0095 * Sets up the completion object to be used. 0096 * 0097 * This method assigns the completion object and sets it up to automatically 0098 * handle the completion and rotation signals internally. You should use 0099 * this function if you want to share one completion object among your 0100 * widgets or need to use a customized completion object. 0101 * 0102 * The object assigned through this method is not deleted when this object's 0103 * destructor is invoked unless you explicitly call @ref setAutoDeleteCompletionObject 0104 * after calling this method. Be sure to set the bool argument to false, if 0105 * you want to handle the completion signals yourself. 0106 * 0107 * @param completionObject a KCompletion or a derived child object. 0108 * @param handleCompletionSignals if true, handles completion signals internally. 0109 */ 0110 virtual void setCompletionObject(KCompletion *completionObject, bool handleSignals = true); 0111 0112 /** 0113 * Enables this object to handle completion and rotation 0114 * events internally. 0115 * 0116 * This function simply assigns a boolean value that 0117 * indicates whether it should handle rotation and 0118 * completion events or not. Note that this does not 0119 * stop the object from emitting signals when these 0120 * events occur. 0121 * 0122 * @param handle if true, it handles completion and rotation internally. 0123 */ 0124 virtual void setHandleSignals(bool handle); 0125 0126 /** 0127 * Returns true if the completion object is deleted 0128 * upon this widget's destruction. 0129 * 0130 * See setCompletionObject() and enableCompletion() 0131 * for details. 0132 * 0133 * @return true if the completion object will be deleted 0134 * automatically 0135 */ 0136 bool isCompletionObjectAutoDeleted() const; 0137 0138 /** 0139 * Sets the completion object when this widget's destructor 0140 * is called. 0141 * 0142 * If the argument is set to true, the completion object 0143 * is deleted when this widget's destructor is called. 0144 * 0145 * @param autoDelete if true, delete completion object on destruction. 0146 */ 0147 void setAutoDeleteCompletionObject(bool autoDelete); 0148 0149 /** 0150 * Sets the widget's ability to emit text completion and 0151 * rotation signals. 0152 * 0153 * Invoking this function with @p enable set to @c false will 0154 * cause the completion and rotation signals not to be emitted. 0155 * However, unlike setting the completion object to @c nullptr 0156 * using setCompletionObject, disabling the emission of 0157 * the signals through this method does not affect the current 0158 * completion object. 0159 * 0160 * There is no need to invoke this function by default. When a 0161 * completion object is created through completionObject or 0162 * setCompletionObject, these signals are set to emit 0163 * automatically. Also note that disabling this signals will not 0164 * necessarily interfere with the objects' ability to handle these 0165 * events internally. See setHandleSignals. 0166 * 0167 * @param enable if false, disables the emission of completion and rotation signals. 0168 */ 0169 void setEnableSignals(bool enable); 0170 0171 /** 0172 * Returns true if the object handles the signals. 0173 * 0174 * @return true if this signals are handled internally. 0175 */ 0176 bool handleSignals() const; 0177 0178 /** 0179 * Returns true if the object emits the signals. 0180 * 0181 * @return true if signals are emitted 0182 */ 0183 bool emitSignals() const; 0184 0185 /** 0186 * Sets whether the object emits rotation signals. 0187 * 0188 * @param emitRotationSignals if false, disables the emission of rotation signals. 0189 */ 0190 void setEmitSignals(bool emitRotationSignals); 0191 0192 /** 0193 * Sets the type of completion to be used. 0194 * 0195 * @param mode Completion type 0196 * @see CompletionMode 0197 */ 0198 virtual void setCompletionMode(KCompletion::CompletionMode mode); 0199 0200 /** 0201 * Returns the current completion mode. 0202 * 0203 * @return the completion mode. 0204 */ 0205 KCompletion::CompletionMode completionMode() const; 0206 0207 /** 0208 * Sets the key binding to be used for manual text 0209 * completion, text rotation in a history list as 0210 * well as a completion list. 0211 * 0212 * 0213 * When the keys set by this function are pressed, a 0214 * signal defined by the inheriting widget will be activated. 0215 * If the default value or 0 is specified by the second 0216 * parameter, then the key binding as defined in the global 0217 * setting should be used. This method returns false 0218 * when @p key is negative or the supplied key binding conflicts 0219 * with another one set for another feature. 0220 * 0221 * NOTE: To use a modifier key (Shift, Ctrl, Alt) as part of 0222 * the key binding simply @p sum up the values of the 0223 * modifier and the actual key. For example, to use CTRL+E, supply 0224 * @c "Qt::CtrlButton | Qt::Key_E" as the second argument to this 0225 * function. 0226 * 0227 * @param item the feature whose key binding needs to be set: 0228 * @li TextCompletion the manual completion key binding. 0229 * @li PrevCompletionMatch the previous match key for multiple completion. 0230 * @li NextCompletionMatch the next match key for for multiple completion. 0231 * @li SubstringCompletion the key for substring completion 0232 * @param key key binding used to rotate down in a list. 0233 * @return true if key binding is successfully set. 0234 * @see keyBinding 0235 */ 0236 bool setKeyBinding(KeyBindingType item, const QList<QKeySequence> &key); 0237 0238 /** 0239 * Returns the key binding used for the specified item. 0240 * 0241 * This method returns the key binding used to activate 0242 * the feature given by @p item. If the binding 0243 * contains modifier key(s), the sum of the modifier key 0244 * and the actual key code is returned. 0245 * 0246 * @param item the item to check 0247 * @return the key binding used for the feature given by @p item. 0248 * @see setKeyBinding 0249 * @since 5.0 0250 */ 0251 QList<QKeySequence> keyBinding(KeyBindingType item) const; 0252 0253 /** 0254 * Sets this object to use global values for key bindings. 0255 * 0256 * This method changes the values of the key bindings for 0257 * rotation and completion features to the default values 0258 * provided in KGlobalSettings. 0259 * 0260 * NOTE: By default, inheriting widgets should use the 0261 * global key bindings so that there is no need to 0262 * call this method. 0263 */ 0264 void useGlobalKeyBindings(); 0265 0266 /** 0267 * A pure virtual function that must be implemented by 0268 * all inheriting classes. 0269 * 0270 * This function is intended to allow external completion 0271 * implementations to set completed text appropriately. It 0272 * is mostly relevant when the completion mode is set to 0273 * CompletionAuto and CompletionManual modes. See 0274 * KCompletionBase::setCompletedText. 0275 * Does nothing in CompletionPopup mode, as all available 0276 * matches will be shown in the popup. 0277 * 0278 * @param text the completed text to be set in the widget. 0279 */ 0280 virtual void setCompletedText(const QString &text) = 0; 0281 0282 /** 0283 * A pure virtual function that must be implemented by 0284 * all inheriting classes. 0285 * @param items the list of completed items 0286 * @param autoSuggest if @c true, the first element of @p items 0287 * is automatically completed (i.e. preselected). 0288 */ 0289 virtual void setCompletedItems(const QStringList &items, bool autoSuggest = true) = 0; 0290 0291 /** 0292 * Returns a pointer to the completion object. 0293 * 0294 * This method is only different from completionObject() 0295 * in that it does not create a new KCompletion object even if 0296 * the internal pointer is @c NULL. Use this method to get the 0297 * pointer to a completion object when inheriting so that you 0298 * will not inadvertently create it. 0299 * 0300 * @return the completion object or @c NULL if one does not exist. 0301 */ 0302 KCompletion *compObj() const; 0303 0304 protected: 0305 /** 0306 * Returns a key binding map. 0307 * 0308 * This method is the same as getKeyBinding(), except that it 0309 * returns the whole keymap containing the key bindings. 0310 * 0311 * @return the key binding used for the feature given by @p item. 0312 * @since 5.0 0313 */ 0314 KeyBindingMap keyBindingMap() const; 0315 0316 /** 0317 * Sets the keymap. 0318 * 0319 * @param keyBindingMap 0320 */ 0321 void setKeyBindingMap(KeyBindingMap keyBindingMap); 0322 0323 /** 0324 * Sets or removes the delegation object. If a delegation object is 0325 * set, all function calls will be forwarded to the delegation object. 0326 * @param delegate the delegation object, or @c nullptr to remove it 0327 */ 0328 void setDelegate(KCompletionBase *delegate); 0329 0330 /** 0331 * Returns the delegation object. 0332 * @return the delegation object, or @c nullptr if there is none 0333 * @see setDelegate() 0334 */ 0335 KCompletionBase *delegate() const; 0336 0337 /** Virtual hook, used to add new "virtual" functions while maintaining 0338 binary compatibility. Unused in this class. 0339 */ 0340 virtual void virtual_hook(int id, void *data); 0341 0342 private: 0343 Q_DISABLE_COPY(KCompletionBase) 0344 std::unique_ptr<KCompletionBasePrivate> const d_ptr; 0345 }; 0346 0347 #endif // KCOMPLETIONBASE_H