KCompletion

kcompletionbase.cpp
1/*
2 This file is part of the KDE libraries
3
4 SPDX-FileCopyrightText: 2000 Dawit Alemayehu <adawit@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include <kcompletionbase.h>
10
11#include <QKeySequence>
12#include <QPointer>
13
14class KCompletionBasePrivate
15{
16public:
17 KCompletionBasePrivate(KCompletionBase *parent)
18 : q_ptr(parent)
19 {
20 }
21
22 ~KCompletionBasePrivate();
23
24 void init();
25
26 bool autoDeleteCompletionObject;
27 bool handleSignals;
28 bool emitSignals;
29 KCompletion::CompletionMode completionMode;
30 QPointer<KCompletion> completionObject;
32 // we may act as a proxy to another KCompletionBase object
33 KCompletionBase *delegate = nullptr;
34 KCompletionBase *const q_ptr;
35 Q_DECLARE_PUBLIC(KCompletionBase)
36};
37
38KCompletionBasePrivate::~KCompletionBasePrivate()
39{
40 if (autoDeleteCompletionObject && completionObject) {
41 delete completionObject;
42 }
43}
44
45void KCompletionBasePrivate::init()
46{
47 Q_Q(KCompletionBase);
48 completionMode = KCompletion::CompletionPopup;
49 delegate = nullptr;
50 // Initialize all key-bindings to 0 by default so that
51 // the event filter will use the global settings.
53
54 q->setAutoDeleteCompletionObject(false);
55 q->setHandleSignals(true);
56 q->setEmitSignals(false);
57}
58
60 : d_ptr(new KCompletionBasePrivate(this))
61{
63 d->init();
64}
65
69
71{
73 d->delegate = delegate;
74
75 if (delegate) {
76 delegate->setAutoDeleteCompletionObject(d->autoDeleteCompletionObject);
77 delegate->setHandleSignals(d->handleSignals);
78 delegate->setEmitSignals(d->emitSignals);
79 delegate->setCompletionMode(d->completionMode);
80 delegate->setKeyBindingMap(d->keyBindingMap);
81 }
82}
83
85{
86 Q_D(const KCompletionBase);
87 return d->delegate;
88}
89
91{
93 if (d->delegate) {
94 return d->delegate->completionObject(handleSignals);
95 }
96
97 if (!d->completionObject) {
99 d->autoDeleteCompletionObject = true;
100 }
101 return d->completionObject;
102}
103
104void KCompletionBase::setCompletionObject(KCompletion *completionObject, bool handleSignals)
105{
107 if (d->delegate) {
108 d->delegate->setCompletionObject(completionObject, handleSignals);
109 return;
110 }
111
112 if (d->autoDeleteCompletionObject && completionObject != d->completionObject) {
113 delete d->completionObject;
114 }
115
116 d->completionObject = completionObject;
117
120
121 // We emit rotation and completion signals
122 // if completion object is not NULL.
123 setEmitSignals(!d->completionObject.isNull());
124}
125
126// BC: Inline this function and possibly rename it to setHandleEvents??? (DA)
128{
130 if (d->delegate) {
131 d->delegate->setHandleSignals(handle);
132 } else {
133 d->handleSignals = handle;
134 }
135}
136
138{
139 Q_D(const KCompletionBase);
140 return d->delegate ? d->delegate->isCompletionObjectAutoDeleted() : d->autoDeleteCompletionObject;
141}
142
144{
146 if (d->delegate) {
147 d->delegate->setAutoDeleteCompletionObject(autoDelete);
148 } else {
149 d->autoDeleteCompletionObject = autoDelete;
150 }
151}
152
154{
156 if (d->delegate) {
157 d->delegate->setEnableSignals(enable);
158 } else {
159 d->emitSignals = enable;
160 }
161}
162
164{
165 Q_D(const KCompletionBase);
166 return d->delegate ? d->delegate->handleSignals() : d->handleSignals;
167}
168
170{
171 Q_D(const KCompletionBase);
172 return d->delegate ? d->delegate->emitSignals() : d->emitSignals;
173}
174
175void KCompletionBase::setEmitSignals(bool emitRotationSignals)
176{
178 if (d->delegate) {
179 d->delegate->setEmitSignals(emitRotationSignals);
180 } else {
181 d->emitSignals = emitRotationSignals;
182 }
183}
184
186{
188 if (d->delegate) {
189 d->delegate->setCompletionMode(mode);
190 return;
191 }
192
193 d->completionMode = mode;
194 // Always sync up KCompletion mode with ours as long as we
195 // are performing completions.
196 if (d->completionObject && d->completionMode != KCompletion::CompletionNone) {
197 d->completionObject->setCompletionMode(d->completionMode);
198 }
199}
200
202{
203 Q_D(const KCompletionBase);
204 return d->delegate ? d->delegate->completionMode() : d->completionMode;
205}
206
208{
210 if (d->delegate) {
211 return d->delegate->setKeyBinding(item, cut);
212 }
213
214 if (!cut.isEmpty()) {
215 for (KeyBindingMap::Iterator it = d->keyBindingMap.begin(); it != d->keyBindingMap.end(); ++it) {
216 if (it.value() == cut) {
217 return false;
218 }
219 }
220 }
221 d->keyBindingMap.insert(item, cut);
222 return true;
223}
224
226{
227 Q_D(const KCompletionBase);
228 return d->delegate ? d->delegate->keyBinding(item) : d->keyBindingMap[item];
229}
230
232{
234 if (d->delegate) {
235 d->delegate->useGlobalKeyBindings();
236 return;
237 }
238
239 d->keyBindingMap.clear();
240 d->keyBindingMap.insert(TextCompletion, QList<QKeySequence>());
241 d->keyBindingMap.insert(PrevCompletionMatch, QList<QKeySequence>());
242 d->keyBindingMap.insert(NextCompletionMatch, QList<QKeySequence>());
243 d->keyBindingMap.insert(SubstringCompletion, QList<QKeySequence>());
244}
245
247{
248 Q_D(const KCompletionBase);
249 return d->delegate ? d->delegate->compObj() : static_cast<KCompletion *>(d->completionObject);
250}
251
253{
254 Q_D(const KCompletionBase);
255 return d->delegate ? d->delegate->keyBindingMap() : d->keyBindingMap;
256}
257
259{
261 if (d->delegate) {
262 d->delegate->setKeyBindingMap(keyBindingMap);
263 return;
264 }
265
266 d->keyBindingMap = keyBindingMap;
267}
268
270{
271 /*BASE::virtual_hook( id, data );*/
272}
An abstract base class for adding a completion feature into widgets.
KCompletionBase * delegate() const
Returns the delegation object.
QList< QKeySequence > keyBinding(KeyBindingType item) const
Returns the key binding used for the specified item.
bool handleSignals() const
Returns true if the object handles the signals.
void setEmitSignals(bool emitRotationSignals)
Sets whether the object emits rotation signals.
virtual void setCompletionMode(KCompletion::CompletionMode mode)
Sets the type of completion to be used.
void setDelegate(KCompletionBase *delegate)
Sets or removes the delegation object.
KeyBindingMap keyBindingMap() const
Returns a key binding map.
virtual void setHandleSignals(bool handle)
Enables this object to handle completion and rotation events internally.
KCompletion * compObj() const
Returns a pointer to the completion object.
virtual void setCompletionObject(KCompletion *completionObject, bool handleSignals=true)
Sets up the completion object to be used.
virtual void virtual_hook(int id, void *data)
Virtual hook, used to add new "virtual" functions while maintaining binary compatibility.
KCompletion * completionObject(bool handleSignals=true)
Returns a pointer to the current completion object.
bool emitSignals() const
Returns true if the object emits the signals.
bool setKeyBinding(KeyBindingType item, const QList< QKeySequence > &key)
Sets the key binding to be used for manual text completion, text rotation in a history list as well a...
void setAutoDeleteCompletionObject(bool autoDelete)
Sets the completion object when this widget's destructor is called.
KCompletion::CompletionMode completionMode() const
Returns the current completion mode.
virtual ~KCompletionBase()
Destructor.
void setEnableSignals(bool enable)
Sets the widget's ability to emit text completion and rotation signals.
void setKeyBindingMap(KeyBindingMap keyBindingMap)
Sets the keymap.
KCompletionBase()
Default constructor.
bool isCompletionObjectAutoDeleted() const
Returns true if the completion object is deleted upon this widget's destruction.
void useGlobalKeyBindings()
Sets this object to use global values for key bindings.
KeyBindingType
Constants that represent the items whose shortcut key binding is programmable.
@ SubstringCompletion
Substring completion (by default Ctrl-T).
@ NextCompletionMatch
Switch to next completion (by default Ctrl-Down).
@ PrevCompletionMatch
Switch to previous completion (by default Ctrl-Up).
@ TextCompletion
Text completion (by default Ctrl-E).
A generic class for completing QStrings.
CompletionMode
This enum describes the completion mode used for by the KCompletion class.
@ CompletionNone
No completion is used.
@ CompletionPopup
Lists all possible matches in a popup list box to choose from.
typedef Iterator
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:16:24 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.