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;
31 KCompletionBase::KeyBindingMap keyBindingMap;
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.
52 q->useGlobalKeyBindings();
53
54 q->setAutoDeleteCompletionObject(false);
55 q->setHandleSignals(true);
56 q->setEmitSignals(false);
57}
58
59KCompletionBase::KCompletionBase()
60 : d_ptr(new KCompletionBasePrivate(this))
61{
62 Q_D(KCompletionBase);
63 d->init();
64}
65
66KCompletionBase::~KCompletionBase()
67{
68}
69
70void KCompletionBase::setDelegate(KCompletionBase *delegate)
71{
72 Q_D(KCompletionBase);
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
84KCompletionBase *KCompletionBase::delegate() const
85{
86 Q_D(const KCompletionBase);
87 return d->delegate;
88}
89
90KCompletion *KCompletionBase::completionObject(bool handleSignals)
91{
92 Q_D(KCompletionBase);
93 if (d->delegate) {
94 return d->delegate->completionObject(handleSignals);
95 }
96
97 if (!d->completionObject) {
98 setCompletionObject(completionObject: new KCompletion(), handleSignals);
99 d->autoDeleteCompletionObject = true;
100 }
101 return d->completionObject;
102}
103
104void KCompletionBase::setCompletionObject(KCompletion *completionObject, bool handleSignals)
105{
106 Q_D(KCompletionBase);
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
118 setAutoDeleteCompletionObject(false);
119 setHandleSignals(handleSignals);
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)
127void KCompletionBase::setHandleSignals(bool handle)
128{
129 Q_D(KCompletionBase);
130 if (d->delegate) {
131 d->delegate->setHandleSignals(handle);
132 } else {
133 d->handleSignals = handle;
134 }
135}
136
137bool KCompletionBase::isCompletionObjectAutoDeleted() const
138{
139 Q_D(const KCompletionBase);
140 return d->delegate ? d->delegate->isCompletionObjectAutoDeleted() : d->autoDeleteCompletionObject;
141}
142
143void KCompletionBase::setAutoDeleteCompletionObject(bool autoDelete)
144{
145 Q_D(KCompletionBase);
146 if (d->delegate) {
147 d->delegate->setAutoDeleteCompletionObject(autoDelete);
148 } else {
149 d->autoDeleteCompletionObject = autoDelete;
150 }
151}
152
153void KCompletionBase::setEnableSignals(bool enable)
154{
155 Q_D(KCompletionBase);
156 if (d->delegate) {
157 d->delegate->setEnableSignals(enable);
158 } else {
159 d->emitSignals = enable;
160 }
161}
162
163bool KCompletionBase::handleSignals() const
164{
165 Q_D(const KCompletionBase);
166 return d->delegate ? d->delegate->handleSignals() : d->handleSignals;
167}
168
169bool KCompletionBase::emitSignals() const
170{
171 Q_D(const KCompletionBase);
172 return d->delegate ? d->delegate->emitSignals() : d->emitSignals;
173}
174
175void KCompletionBase::setEmitSignals(bool emitRotationSignals)
176{
177 Q_D(KCompletionBase);
178 if (d->delegate) {
179 d->delegate->setEmitSignals(emitRotationSignals);
180 } else {
181 d->emitSignals = emitRotationSignals;
182 }
183}
184
185void KCompletionBase::setCompletionMode(KCompletion::CompletionMode mode)
186{
187 Q_D(KCompletionBase);
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
201KCompletion::CompletionMode KCompletionBase::completionMode() const
202{
203 Q_D(const KCompletionBase);
204 return d->delegate ? d->delegate->completionMode() : d->completionMode;
205}
206
207bool KCompletionBase::setKeyBinding(KeyBindingType item, const QList<QKeySequence> &cut)
208{
209 Q_D(KCompletionBase);
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(key: item, value: cut);
222 return true;
223}
224
225QList<QKeySequence> KCompletionBase::keyBinding(KeyBindingType item) const
226{
227 Q_D(const KCompletionBase);
228 return d->delegate ? d->delegate->keyBinding(item) : d->keyBindingMap[item];
229}
230
231void KCompletionBase::useGlobalKeyBindings()
232{
233 Q_D(KCompletionBase);
234 if (d->delegate) {
235 d->delegate->useGlobalKeyBindings();
236 return;
237 }
238
239 d->keyBindingMap.clear();
240 d->keyBindingMap.insert(key: TextCompletion, value: QList<QKeySequence>());
241 d->keyBindingMap.insert(key: PrevCompletionMatch, value: QList<QKeySequence>());
242 d->keyBindingMap.insert(key: NextCompletionMatch, value: QList<QKeySequence>());
243 d->keyBindingMap.insert(key: SubstringCompletion, value: QList<QKeySequence>());
244}
245
246KCompletion *KCompletionBase::compObj() const
247{
248 Q_D(const KCompletionBase);
249 return d->delegate ? d->delegate->compObj() : static_cast<KCompletion *>(d->completionObject);
250}
251
252KCompletionBase::KeyBindingMap KCompletionBase::keyBindingMap() const
253{
254 Q_D(const KCompletionBase);
255 return d->delegate ? d->delegate->keyBindingMap() : d->keyBindingMap;
256}
257
258void KCompletionBase::setKeyBindingMap(KCompletionBase::KeyBindingMap keyBindingMap)
259{
260 Q_D(KCompletionBase);
261 if (d->delegate) {
262 d->delegate->setKeyBindingMap(keyBindingMap);
263 return;
264 }
265
266 d->keyBindingMap = keyBindingMap;
267}
268
269void KCompletionBase::virtual_hook(int, void *)
270{
271 /*BASE::virtual_hook( id, data );*/
272}
273

source code of kcompletion/src/kcompletionbase.cpp