KCompletion

klineedit.h
1/*
2 This file is part of the KDE libraries
3
4 This class was originally inspired by Torben Weis'
5 fileentry.cpp for KFM II.
6
7 SPDX-FileCopyrightText: 1997 Sven Radej <sven.radej@iname.com>
8 SPDX-FileCopyrightText: 1999 Patrick Ward <PAT_WARD@HP-USA-om5.om.hp.com>
9 SPDX-FileCopyrightText: 1999 Preston Brown <pbrown@kde.org>
10
11 Completely re-designed:
12 SPDX-FileCopyrightText: 2000, 2001 Dawit Alemayehu <adawit@kde.org>
13
14 SPDX-License-Identifier: LGPL-2.0-or-later
15*/
16
17#ifndef KLINEEDIT_H
18#define KLINEEDIT_H
19
20#include <kcompletion.h>
21#include <kcompletion_export.h>
22#include <kcompletionbase.h>
23
24#include <QLineEdit>
25#include <memory>
26
27class QAction;
28class QMenu;
29class KCompletionBox;
30class QUrl;
31class KLineEditPrivate;
32
33/**
34 * @class KLineEdit klineedit.h KLineEdit
35 *
36 * An enhanced QLineEdit widget for inputting text.
37 *
38 * \b Detail \n
39 *
40 * This widget inherits from QLineEdit and implements the following
41 * additional functionalities:
42 * @li a completion object that provides both automatic and manual text
43 * completion as well as multiple match iteration features
44 * @li configurable key-bindings to activate these features
45 * @li a popup-menu item that can be used to allow the user to set text
46 * completion modes on the fly based on their preference
47 *
48 * To support these features KLineEdit also emits a few more additional
49 * signals:
50 * @li @c completion(const QString &): this signal can be connected to
51 * a slot that will assist the user in filling out the remaining text
52 * @li @c textRotation(KeyBindingType): this signal is intended to be
53 * used to iterate through the list of all possible matches whenever
54 * there is more than one match for the entered text
55 * @li @c returnKeyPressed(const QString &): this signal provides the
56 * current text in the widget as its argument whenever appropriate (this
57 * is in addition to the @c QLineEdit::returnPressed() signal which KLineEdit
58 * inherits from QLineEdit).
59 *
60 * This widget by default creates a completion object when you invoke
61 * the @c completionObject(bool) member function for the first time or
62 * use @c setCompletionObject(KCompletion *, bool) to assign your own
63 * completion object. Additionally, to make this widget more functional,
64 * KLineEdit will by default handle the text rotation and completion
65 * events internally when a completion object is created through either one
66 * of the methods mentioned above. If you do not need this functionality,
67 * simply use @c KCompletionBase::setHandleSignals(bool) or set the boolean
68 * parameter in the above functions to false.
69 *
70 * The default key-bindings for completion and rotation is determined
71 * from the global settings in KStandardShortcut. These values, however,
72 * can be overridden locally by invoking @c KCompletionBase::setKeyBinding().
73 * The values can easily be reverted back to the default setting, by simply
74 * calling @c useGlobalSettings(). An alternate method would be to default
75 * individual key-bindings by using setKeyBinding() with the default
76 * second argument.
77 *
78 * If @c EchoMode for this widget is set to something other than @c QLineEdit::Normal,
79 * the completion mode will always be defaulted to CompletionNone.
80 * This is done purposefully to guard against protected entries, such as
81 * passwords, being cached in KCompletion's list. Hence, if the @c EchoMode
82 * is not @c QLineEdit::Normal, the completion mode is automatically disabled.
83 *
84 * A read-only KLineEdit will have the same background color as a disabled
85 * KLineEdit, but its foreground color will be the one used for the read-write
86 * mode. This differs from QLineEdit's implementation and is done to give visual
87 * distinction between the three different modes: disabled, read-only, and read-write.
88 *
89 * @b Usage
90 *
91 * To enable the basic completion feature:
92 *
93 * @code
94 * KLineEdit *edit = new KLineEdit(this);
95 * KCompletion *comp = edit->completionObject();
96 * // Connect to the Return pressed signal - optional
97 * connect(edit, &KLineEdit::returnKeyPressed, comp, [this](const QString &text) { addItem(text); });
98 * @endcode
99 *
100 * To use a customized completion object or your own completion object:
101 *
102 * @code
103 * KLineEdit *edit = new KLineEdit(this);
104 * KUrlCompletion *comp = new KUrlCompletion();
105 * edit->setCompletionObject(comp);
106 * // Connect to the return pressed signal - optional
107 * connect(edit, &KLineEdit::returnKeyPressed, comp, [this](const QString &text) { addItem(text); });
108 * @endcode
109 *
110 * Note if you specify your own completion object you have to either delete
111 * it when you don't need it anymore, or you can tell KLineEdit to delete it
112 * for you:
113 * @code
114 * edit->setAutoDeleteCompletionObject(true);
115 * @endcode
116 *
117 * <b>Miscellaneous function calls :</b>\n
118 *
119 * @code
120 * // Tell the widget to not handle completion and iteration automatically
121 * edit->setHandleSignals(false);
122 *
123 * // Set your own key-bindings for a text completion mode
124 * edit->setKeyBinding(KCompletionBase::TextCompletion, Qt::End);
125 *
126 * // Hide the context (popup) menu
127 * edit->setContextMenuPolicy(Qt::NoContextMenu);
128 *
129 * // Default the key-bindings back to the default system settings
130 * edit->useGlobalKeyBindings();
131 * @endcode
132 *
133 * @image html klineedit.png "KLineEdit widgets with clear-button"
134 *
135 * @author Dawit Alemayehu <adawit@kde.org>
136 */
137
138class KCOMPLETION_EXPORT KLineEdit : public QLineEdit, public KCompletionBase // krazy:exclude=qclasses
139{
140 friend class KComboBox;
141 friend class KLineEditStyle;
142
143 Q_OBJECT
144 Q_DECLARE_PRIVATE(KLineEdit)
145 Q_PROPERTY(bool trapEnterKeyEvent READ trapReturnKey WRITE setTrapReturnKey)
146 Q_PROPERTY(bool squeezedTextEnabled READ isSqueezedTextEnabled WRITE setSqueezedTextEnabled)
147
148public:
149 /**
150 * Constructs a KLineEdit object with a default text, a parent,
151 * and a name.
152 *
153 * @param string Text to be shown in the edit widget.
154 * @param parent The parent widget of the line edit.
155 */
156 explicit KLineEdit(const QString &string, QWidget *parent = nullptr);
157
158 /**
159 * Constructs a line edit
160 * @param parent The parent widget of the line edit.
161 */
162 explicit KLineEdit(QWidget *parent = nullptr);
163
164 /**
165 * Destructor.
166 */
167 ~KLineEdit() override;
168
169 /**
170 * Sets @p url into the lineedit. It uses QUrl::toDisplayString() so
171 * that the url is properly decoded for displaying.
172 */
173 void setUrl(const QUrl &url);
174
175 /**
176 * Reimplemented from KCompletionBase for internal reasons.
177 *
178 * This function is re-implemented in order to make sure that
179 * the EchoMode is acceptable before we set the completion mode.
180 *
181 * See KCompletionBase::setCompletionMode
182 */
184
185 /**
186 * Disables completion modes by making them non-checkable.
187 *
188 * The context menu allows to change the completion mode.
189 * This method allows to disable some modes.
190 */
191 void setCompletionModeDisabled(KCompletion::CompletionMode mode, bool disable = true);
192
193 /**
194 * Returns @c true when decoded URL drops are enabled
195 */
196 bool urlDropsEnabled() const;
197
198 /**
199 * By default, KLineEdit recognizes @c Key_Return and @c Key_Enter and emits
200 * the returnPressed() signals, but it also lets the event pass,
201 * for example causing a dialog's default-button to be called.
202 *
203 * Call this method with @p trap = @c true to make @c KLineEdit stop these
204 * events. The signals will still be emitted of course.
205 *
206 * @see trapReturnKey()
207 */
208 void setTrapReturnKey(bool trap);
209
210 /**
211 * @returns @c true if keyevents of @c Key_Return or
212 * @c Key_Enter will be stopped or if they will be propagated.
213 *
214 * @see setTrapReturnKey ()
215 */
216 bool trapReturnKey() const;
217
218 /**
219 * This method will create a completion-box if none is there, yet.
220 *
221 * @param create Set this to false if you don't want the box to be created
222 * i.e. to test if it is available.
223 * @returns the completion-box, that is used in completion mode
224 * CompletionPopup.
225 */
226 virtual KCompletionBox *completionBox(bool create = true);
227
228 /**
229 * Reimplemented for internal reasons, the API is not affected.
230 */
231 void setCompletionObject(KCompletion *, bool handle = true) override;
232
233 /**
234 * Reimplemented for internal reasons, the API is not affected.
235 */
236 virtual void copy() const;
237
238 /**
239 * Enable text squeezing whenever the supplied text is too long.
240 * Only works for "read-only" mode.
241 *
242 * Note that once text squeezing is enabled, QLineEdit::text()
243 * and QLineEdit::displayText() return the squeezed text. If
244 * you want the original text, use @ref originalText.
245 *
246 * @see QLineEdit
247 */
248 void setSqueezedTextEnabled(bool enable);
249
250 /**
251 * Returns true if text squeezing is enabled.
252 * This is only valid when the widget is in read-only mode.
253 */
254 bool isSqueezedTextEnabled() const;
255
256 /**
257 * Returns the original text if text squeezing is enabled.
258 * If the widget is not in "read-only" mode, this function
259 * returns the same thing as QLineEdit::text().
260 *
261 * @see QLineEdit
262 */
263 QString originalText() const;
264
265 /**
266 * Returns the text as given by the user (i.e. not autocompleted)
267 * if the widget has autocompletion disabled, this function
268 * returns the same as QLineEdit::text().
269 * @since 4.2.2
270 */
271 QString userText() const;
272
273 /**
274 * Set the completion-box to be used in completion mode
275 * CompletionPopup.
276 * This will do nothing if a completion-box already exists.
277 *
278 * @param box The KCompletionBox to set
279 */
280 void setCompletionBox(KCompletionBox *box);
281
282 /**
283 * @return the size used by the clear button
284 * @since 4.1
285 */
286 QSize clearButtonUsedSize() const;
287
288 /**
289 * Do completion now. This is called automatically when typing a key for instance.
290 * Emits completion() and/or calls makeCompletion(), depending on
291 * emitSignals and handleSignals.
292 *
293 * @since 4.2.1
294 */
295 void doCompletion(const QString &text);
296
298 /**
299 * Emitted whenever the completion box is activated.
300 */
302
303 /**
304 * Emitted when the user presses the Return or Enter key.
305 *
306 * The argument is the current text. Note that this signal is @em not emitted
307 * if the widget's @c EchoMode is set to QLineEdit::EchoMode.
308 *
309 * @since 5.81
310 */
311 void returnKeyPressed(const QString &text);
312
313 /**
314 * Emitted when the completion key is pressed.
315 *
316 * Please note that this signal is @em not emitted if the
317 * completion mode is set to @c CompletionNone or @c EchoMode is
318 * @em normal.
319 */
320 void completion(const QString &);
321
322 /**
323 * Emitted when the shortcut for substring completion is pressed.
324 */
326
327 /**
328 * Emitted when the text rotation key-bindings are pressed.
329 *
330 * The argument indicates which key-binding was pressed.
331 * In KLineEdit's case this can be either one of two values:
332 * PrevCompletionMatch or NextCompletionMatch. See
333 * KCompletionBase::setKeyBinding for details.
334 *
335 * Note that this signal is @em not emitted if the completion
336 * mode is set to @c CompletionNone or @c echoMode() is @em not normal.
337 */
339
340 /**
341 * Emitted when the user changed the completion mode by using the
342 * popupmenu.
343 */
345
346 /**
347 * Emitted before the context menu is displayed.
348 *
349 * The signal allows you to add your own entries into the
350 * the context menu that is created on demand.
351 *
352 * NOTE: Do not store the pointer to the QMenu
353 * provided through since it is created and deleted
354 * on demand.
355 *
356 * @param contextMenu the context menu about to be displayed
357 */
358 void aboutToShowContextMenu(QMenu *contextMenu);
359
360 /**
361 * Emitted when the user clicked on the clear button
362 */
364
365public Q_SLOTS:
366
367 /**
368 * Sets the lineedit to read-only. Similar to QLineEdit::setReadOnly
369 * but also takes care of the background color, and the clear button.
370 */
371 virtual void setReadOnly(bool);
372
373 /**
374 * Iterates through all possible matches of the completed text or
375 * the history list.
376 *
377 * This function simply iterates over all possible matches in case
378 * multiple matches are found as a result of a text completion request.
379 * It will have no effect if only a single match is found.
380 *
381 * @param type The key-binding invoked.
382 */
383 void rotateText(KCompletionBase::KeyBindingType type);
384
385 /**
386 * See KCompletionBase::setCompletedText.
387 */
388 void setCompletedText(const QString &) override;
389
390 /**
391 * Same as the above function except it allows you to temporarily
392 * turn off text completion in CompletionPopupAuto mode.
393 *
394 *
395 * @param items list of completion matches to be shown in the completion box.
396 * @param autoSuggest true if you want automatic text completion (suggestion) enabled.
397 */
398 void setCompletedItems(const QStringList &items, bool autoSuggest = true) override;
399
400 /**
401 * Squeezes @p text into the line edit.
402 * This can only be used with read-only line-edits.
403 */
404 void setSqueezedText(const QString &text);
405
406 /**
407 * Reimplemented to enable text squeezing. API is not affected.
408 */
409 virtual void setText(const QString &);
410
411protected Q_SLOTS:
412
413 /**
414 * Completes the remaining text with a matching one from
415 * a given list.
416 */
417 virtual void makeCompletion(const QString &);
418
419 /**
420 * Resets the current displayed text.
421 * Call this function to revert a text completion if the user
422 * cancels the request. Mostly applies to popup completions.
423 */
424 void userCancelled(const QString &cancelText);
425
426protected:
427 /**
428 * Reimplemented for internal reasons. API not affected.
429 */
430 bool event(QEvent *) override;
431
432 /**
433 * Reimplemented for internal reasons. API not affected.
434 *
435 * See QLineEdit::resizeEvent().
436 */
437 void resizeEvent(QResizeEvent *) override;
438
439 /**
440 * Reimplemented for internal reasons. API not affected.
441 *
442 * See QLineEdit::keyPressEvent().
443 */
444 void keyPressEvent(QKeyEvent *) override;
445
446 /**
447 * Reimplemented for internal reasons. API not affected.
448 *
449 * See QLineEdit::mousePressEvent().
450 */
451 void mousePressEvent(QMouseEvent *) override;
452
453 /**
454 * Reimplemented for internal reasons. API not affected.
455 *
456 * See QLineEdit::mouseReleaseEvent().
457 */
458 void mouseReleaseEvent(QMouseEvent *) override;
459
460 /**
461 * Reimplemented for internal reasons. API not affected.
462 *
463 * See QWidget::mouseDoubleClickEvent().
464 */
465 void mouseDoubleClickEvent(QMouseEvent *) override;
466
467 /**
468 * Reimplemented for internal reasons. API not affected.
469 *
470 * See QLineEdit::contextMenuEvent().
471 */
472 void contextMenuEvent(QContextMenuEvent *) override;
473
474 /**
475 * Reimplemented for internal reasons. API not affected.
476 *
477 * See QLineEdit::createStandardContextMenu().
478 */
480
481 /**
482 * This function simply sets the lineedit text and
483 * highlights the text appropriately if the boolean
484 * value is set to true.
485 *
486 * @param text
487 * @param marked
488 */
489 virtual void setCompletedText(const QString & /*text*/, bool /*marked*/);
490
491 /**
492 * Sets the widget in userSelection mode or in automatic completion
493 * selection mode. This changes the colors of selections.
494 */
495 void setUserSelection(bool userSelection);
496
497 /**
498 * Whether in current state text should be auto-suggested
499 */
500 bool autoSuggest() const;
501
502 void paintEvent(QPaintEvent *ev) override;
503
504private:
505 std::unique_ptr<KLineEditPrivate> const d_ptr;
506};
507
508#endif
A combo box with completion support.
Definition kcombobox.h:136
An abstract base class for adding a completion feature into widgets.
virtual void setCompletionMode(KCompletion::CompletionMode mode)
Sets the type of completion to be used.
virtual void setCompletedItems(const QStringList &items, bool autoSuggest=true)=0
A pure virtual function that must be implemented by all inheriting classes.
virtual void setCompletedText(const QString &text)=0
A pure virtual function that must be implemented by all inheriting classes.
virtual void setCompletionObject(KCompletion *completionObject, bool handleSignals=true)
Sets up the completion object to be used.
KeyBindingType
Constants that represent the items whose shortcut key binding is programmable.
A helper widget for "completion-widgets" (KLineEdit, KComboBox))
A generic class for completing QStrings.
CompletionMode
This enum describes the completion mode used for by the KCompletion class.
An enhanced QLineEdit widget for inputting text.
Definition klineedit.h:139
void returnKeyPressed(const QString &text)
Emitted when the user presses the Return or Enter key.
void aboutToShowContextMenu(QMenu *contextMenu)
Emitted before the context menu is displayed.
void textRotation(KCompletionBase::KeyBindingType)
Emitted when the text rotation key-bindings are pressed.
void completionBoxActivated(const QString &)
Emitted whenever the completion box is activated.
void completionModeChanged(KCompletion::CompletionMode)
Emitted when the user changed the completion mode by using the popupmenu.
void clearButtonClicked()
Emitted when the user clicked on the clear button.
void completion(const QString &)
Emitted when the completion key is pressed.
void substringCompletion(const QString &)
Emitted when the shortcut for substring completion is pressed.
virtual void contextMenuEvent(QContextMenuEvent *event) override
void copy() const const
QMenu * createStandardContextMenu()
virtual bool event(QEvent *e) override
virtual void keyPressEvent(QKeyEvent *event) override
virtual void mouseDoubleClickEvent(QMouseEvent *e) override
virtual void mousePressEvent(QMouseEvent *e) override
virtual void mouseReleaseEvent(QMouseEvent *e) override
virtual void paintEvent(QPaintEvent *) override
void setReadOnly(bool)
void setText(const QString &)
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
virtual void resizeEvent(QResizeEvent *event)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.