KCompletion

kcompletionbase.cpp
1 /*
2  This file is part of the KDE libraries
3 
4  SPDX-FileCopyrightText: 2000 Dawit Alemayehu <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include <kcompletionbase.h>
10 
11 #include <kcompletion.h>
12 #include <QPointer>
13 #include <QKeySequence>
14 
15 class KCompletionBasePrivate
16 {
17 public:
18  KCompletionBasePrivate(KCompletionBase *parent) : q_ptr(parent){}
19 
20  ~KCompletionBasePrivate();
21 
22  void init();
23 
24  bool autoDeleteCompletionObject;
25  bool handleSignals;
26  bool emitSignals;
27  KCompletion::CompletionMode completionMode;
28  QPointer<KCompletion> completionObject;
29  KCompletionBase::KeyBindingMap keyBindingMap;
30  // we may act as a proxy to another KCompletionBase object
31  KCompletionBase *delegate = nullptr;
32  KCompletionBase * const q_ptr;
33  Q_DECLARE_PUBLIC(KCompletionBase)
34 };
35 
36 KCompletionBasePrivate::~KCompletionBasePrivate()
37 {
38  if (autoDeleteCompletionObject && completionObject) {
39  delete completionObject;
40  }
41 }
42 
43 void KCompletionBasePrivate::init()
44 {
45  Q_Q(KCompletionBase);
46  completionMode = KCompletion::CompletionPopup;
47  delegate = nullptr;
48  // Initialize all key-bindings to 0 by default so that
49  // the event filter will use the global settings.
50  q->useGlobalKeyBindings();
51 
52  q->setAutoDeleteCompletionObject(false);
53  q->setHandleSignals(true);
54  q->setEmitSignals(false);
55 }
56 
57 
59  : d_ptr(new KCompletionBasePrivate(this))
60 {
61  Q_D(KCompletionBase);
62  d->init();
63 }
64 
66 {
67 }
68 
70 {
71  Q_D(KCompletionBase);
72  d->delegate = delegate;
73 
74  if (delegate) {
75  delegate->setAutoDeleteCompletionObject(d->autoDeleteCompletionObject);
76  delegate->setHandleSignals(d->handleSignals);
77  delegate->setEmitSignals(d->emitSignals);
78  delegate->setCompletionMode(d->completionMode);
79  delegate->setKeyBindingMap(d->keyBindingMap);
80  }
81 }
82 
84 {
85  Q_D(const KCompletionBase);
86  return d->delegate;
87 }
88 
90 {
91  Q_D(KCompletionBase);
92  if (d->delegate) {
93  return d->delegate->completionObject(handleSignals);
94  }
95 
96  if (!d->completionObject) {
97  setCompletionObject(new KCompletion(), handleSignals);
98  d->autoDeleteCompletionObject = true;
99  }
100  return d->completionObject;
101 }
102 
104 {
105  Q_D(KCompletionBase);
106  if (d->delegate) {
107  d->delegate->setCompletionObject(completionObject, handleSignals);
108  return;
109  }
110 
111  if (d->autoDeleteCompletionObject && completionObject != d->completionObject) {
112  delete d->completionObject;
113  }
114 
115  d->completionObject = completionObject;
116 
118  setHandleSignals(handleSignals);
119 
120  // We emit rotation and completion signals
121  // if completion object is not NULL.
122  setEmitSignals(!d->completionObject.isNull());
123 }
124 
125 // BC: Inline this function and possibly rename it to setHandleEvents??? (DA)
127 {
128  Q_D(KCompletionBase);
129  if (d->delegate) {
130  d->delegate->setHandleSignals(handle);
131  } else {
132  d->handleSignals = handle;
133  }
134 }
135 
137 {
138  Q_D(const KCompletionBase);
139  return d->delegate ? d->delegate->isCompletionObjectAutoDeleted()
140  : d->autoDeleteCompletionObject;
141 }
142 
144 {
145  Q_D(KCompletionBase);
146  if (d->delegate) {
147  d->delegate->setAutoDeleteCompletionObject(autoDelete);
148  } else {
149  d->autoDeleteCompletionObject = autoDelete;
150  }
151 }
152 
154 {
155  Q_D(KCompletionBase);
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 
175 void 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 
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 
202 {
203  Q_D(const KCompletionBase);
204  return d->delegate ? d->delegate->completionMode() : d->completionMode;
205 }
206 
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  d->keyBindingMap.insert(item, cut);
221  return true;
222 }
223 
225 {
226  Q_D(const KCompletionBase);
227  return d->delegate ? d->delegate->keyBinding(item) : d->keyBindingMap[ item ];
228 }
229 
231 {
232  Q_D(KCompletionBase);
233  if (d->delegate) {
234  d->delegate->useGlobalKeyBindings();
235  return;
236  }
237 
238  d->keyBindingMap.clear();
239  d->keyBindingMap.insert(TextCompletion, QList<QKeySequence>());
240  d->keyBindingMap.insert(PrevCompletionMatch, QList<QKeySequence>());
241  d->keyBindingMap.insert(NextCompletionMatch, QList<QKeySequence>());
242  d->keyBindingMap.insert(SubstringCompletion, QList<QKeySequence>());
243 }
244 
246 {
247  Q_D(const KCompletionBase);
248  return d->delegate ? d->delegate->compObj()
249  : 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 {
260  Q_D(KCompletionBase);
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 }
Lists all possible matches in a popup list box to choose from.
Definition: kcompletion.h:149
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 setEnableSignals(bool enable)
Sets the widget&#39;s ability to emit text completion and rotation signals.
KCompletionBase * delegate() const
Returns the delegation object.
QList< QKeySequence > keyBinding(KeyBindingType item) const
Returns the key binding used for the specified item.
Switch to next completion (by default Ctrl-Down).
KCompletion * compObj() const
Returns a pointer to the completion object.
Text completion (by default Ctrl-E).
void useGlobalKeyBindings()
Sets this object to use global values for key bindings.
KCompletion::CompletionMode completionMode() const
Returns the current completion mode.
void setKeyBindingMap(KeyBindingMap keyBindingMap)
Sets the keymap.
KeyBindingMap keyBindingMap() const
Returns a key binding map.
virtual void virtual_hook(int id, void *data)
Virtual hook, used to add new "virtual" functions while maintaining binary compatibility.
virtual ~KCompletionBase()
Destructor.
Substring completion (by default Ctrl-T).
Switch to previous completion (by default Ctrl-Up).
virtual void setHandleSignals(bool handle)
Enables this object to handle completion and rotation events internally.
No completion is used.
Definition: kcompletion.h:133
CompletionMode
This enum describes the completion mode used for by the KCompletion class.
Definition: kcompletion.h:129
bool isEmpty() const const
A generic class for completing QStrings.
Definition: kcompletion.h:115
bool handleSignals() const
Returns true if the object handles the signals.
void setAutoDeleteCompletionObject(bool autoDelete)
Sets the completion object when this widget&#39;s destructor is called.
KCompletion * completionObject(bool handleSignals=true)
Returns a pointer to the current completion object.
bool isCompletionObjectAutoDeleted() const
Returns true if the completion object is deleted upon this widget&#39;s destruction.
QCA_EXPORT void init()
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.
KCompletionBase()
Default constructor.
bool emitSignals() const
Returns true if the object emits the signals.
KeyBindingType
Constants that represent the items whose shortcut key binding is programmable.
void setEmitSignals(bool emitRotationSignals)
Sets whether the object emits rotation signals.
typedef Iterator
virtual void setCompletionObject(KCompletion *completionObject, bool handleSignals=true)
Sets up the completion object to be used.
void setDelegate(KCompletionBase *delegate)
Sets or removes the delegation object.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 2 2020 22:51:00 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.