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 <QPointer>
12 #include <QKeySequence>
13 
14 class KCompletionBasePrivate
15 {
16 public:
17  KCompletionBasePrivate(KCompletionBase *parent) : q_ptr(parent){}
18 
19  ~KCompletionBasePrivate();
20 
21  void init();
22 
23  bool autoDeleteCompletionObject;
24  bool handleSignals;
25  bool emitSignals;
26  KCompletion::CompletionMode completionMode;
27  QPointer<KCompletion> completionObject;
28  KCompletionBase::KeyBindingMap keyBindingMap;
29  // we may act as a proxy to another KCompletionBase object
30  KCompletionBase *delegate = nullptr;
31  KCompletionBase * const q_ptr;
32  Q_DECLARE_PUBLIC(KCompletionBase)
33 };
34 
35 KCompletionBasePrivate::~KCompletionBasePrivate()
36 {
37  if (autoDeleteCompletionObject && completionObject) {
38  delete completionObject;
39  }
40 }
41 
42 void KCompletionBasePrivate::init()
43 {
44  Q_Q(KCompletionBase);
45  completionMode = KCompletion::CompletionPopup;
46  delegate = nullptr;
47  // Initialize all key-bindings to 0 by default so that
48  // the event filter will use the global settings.
49  q->useGlobalKeyBindings();
50 
51  q->setAutoDeleteCompletionObject(false);
52  q->setHandleSignals(true);
53  q->setEmitSignals(false);
54 }
55 
56 
58  : d_ptr(new KCompletionBasePrivate(this))
59 {
60  Q_D(KCompletionBase);
61  d->init();
62 }
63 
65 {
66 }
67 
69 {
70  Q_D(KCompletionBase);
71  d->delegate = delegate;
72 
73  if (delegate) {
74  delegate->setAutoDeleteCompletionObject(d->autoDeleteCompletionObject);
75  delegate->setHandleSignals(d->handleSignals);
76  delegate->setEmitSignals(d->emitSignals);
77  delegate->setCompletionMode(d->completionMode);
78  delegate->setKeyBindingMap(d->keyBindingMap);
79  }
80 }
81 
83 {
84  Q_D(const KCompletionBase);
85  return d->delegate;
86 }
87 
89 {
90  Q_D(KCompletionBase);
91  if (d->delegate) {
92  return d->delegate->completionObject(handleSignals);
93  }
94 
95  if (!d->completionObject) {
96  setCompletionObject(new KCompletion(), handleSignals);
97  d->autoDeleteCompletionObject = true;
98  }
99  return d->completionObject;
100 }
101 
103 {
104  Q_D(KCompletionBase);
105  if (d->delegate) {
106  d->delegate->setCompletionObject(completionObject, handleSignals);
107  return;
108  }
109 
110  if (d->autoDeleteCompletionObject && completionObject != d->completionObject) {
111  delete d->completionObject;
112  }
113 
114  d->completionObject = completionObject;
115 
117  setHandleSignals(handleSignals);
118 
119  // We emit rotation and completion signals
120  // if completion object is not NULL.
121  setEmitSignals(!d->completionObject.isNull());
122 }
123 
124 // BC: Inline this function and possibly rename it to setHandleEvents??? (DA)
126 {
127  Q_D(KCompletionBase);
128  if (d->delegate) {
129  d->delegate->setHandleSignals(handle);
130  } else {
131  d->handleSignals = handle;
132  }
133 }
134 
136 {
137  Q_D(const KCompletionBase);
138  return d->delegate ? d->delegate->isCompletionObjectAutoDeleted()
139  : d->autoDeleteCompletionObject;
140 }
141 
143 {
144  Q_D(KCompletionBase);
145  if (d->delegate) {
146  d->delegate->setAutoDeleteCompletionObject(autoDelete);
147  } else {
148  d->autoDeleteCompletionObject = autoDelete;
149  }
150 }
151 
153 {
154  Q_D(KCompletionBase);
155  if (d->delegate) {
156  d->delegate->setEnableSignals(enable);
157  } else {
158  d->emitSignals = enable;
159  }
160 }
161 
163 {
164  Q_D(const KCompletionBase);
165  return d->delegate ? d->delegate->handleSignals() : d->handleSignals;
166 }
167 
169 {
170  Q_D(const KCompletionBase);
171  return d->delegate ? d->delegate->emitSignals() : d->emitSignals;
172 }
173 
174 void KCompletionBase::setEmitSignals(bool emitRotationSignals)
175 {
176  Q_D(KCompletionBase);
177  if (d->delegate) {
178  d->delegate->setEmitSignals(emitRotationSignals);
179  } else {
180  d->emitSignals = emitRotationSignals;
181  }
182 }
183 
185 {
186  Q_D(KCompletionBase);
187  if (d->delegate) {
188  d->delegate->setCompletionMode(mode);
189  return;
190  }
191 
192  d->completionMode = mode;
193  // Always sync up KCompletion mode with ours as long as we
194  // are performing completions.
195  if (d->completionObject && d->completionMode != KCompletion::CompletionNone) {
196  d->completionObject->setCompletionMode(d->completionMode);
197  }
198 }
199 
201 {
202  Q_D(const KCompletionBase);
203  return d->delegate ? d->delegate->completionMode() : d->completionMode;
204 }
205 
207 {
208  Q_D(KCompletionBase);
209  if (d->delegate) {
210  return d->delegate->setKeyBinding(item, cut);
211  }
212 
213  if (!cut.isEmpty()) {
214  for (KeyBindingMap::Iterator it = d->keyBindingMap.begin(); it != d->keyBindingMap.end(); ++it)
215  if (it.value() == cut) {
216  return false;
217  }
218  }
219  d->keyBindingMap.insert(item, cut);
220  return true;
221 }
222 
224 {
225  Q_D(const KCompletionBase);
226  return d->delegate ? d->delegate->keyBinding(item) : d->keyBindingMap[ item ];
227 }
228 
230 {
231  Q_D(KCompletionBase);
232  if (d->delegate) {
233  d->delegate->useGlobalKeyBindings();
234  return;
235  }
236 
237  d->keyBindingMap.clear();
238  d->keyBindingMap.insert(TextCompletion, QList<QKeySequence>());
239  d->keyBindingMap.insert(PrevCompletionMatch, QList<QKeySequence>());
240  d->keyBindingMap.insert(NextCompletionMatch, QList<QKeySequence>());
241  d->keyBindingMap.insert(SubstringCompletion, QList<QKeySequence>());
242 }
243 
245 {
246  Q_D(const KCompletionBase);
247  return d->delegate ? d->delegate->compObj()
248  : static_cast<KCompletion *>(d->completionObject);
249 }
250 
252 {
253  Q_D(const KCompletionBase);
254  return d->delegate ? d->delegate->keyBindingMap() : d->keyBindingMap;
255 }
256 
258 {
259  Q_D(KCompletionBase);
260  if (d->delegate) {
261  d->delegate->setKeyBindingMap(keyBindingMap);
262  return;
263  }
264 
265  d->keyBindingMap = keyBindingMap;
266 }
267 
269 {
270  /*BASE::virtual_hook( id, data );*/
271 }
Lists all possible matches in a popup list box to choose from.
Definition: kcompletion.h:150
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:134
CompletionMode
This enum describes the completion mode used for by the KCompletion class.
Definition: kcompletion.h:130
bool isEmpty() const const
A generic class for completing QStrings.
Definition: kcompletion.h:116
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-2021 The KDE developers.
Generated on Sat Jan 23 2021 03:04:08 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.