File indexing completed on 2024-05-12 05:46:46

0001 /* This file is part of the KDE libraries
0002 
0003    Copyright (c) 2000 Dawit Alemayehu <adawit@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License (LGPL) as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include <kcompletionbase.h>
0022 
0023 #include <kcompletion.h>
0024 #include <QPointer>
0025 #include <QKeySequence>
0026 
0027 class KCompletionBasePrivate
0028 {
0029 public:
0030     KCompletionBasePrivate(KCompletionBase *parent) : q_ptr(parent){}
0031 
0032     ~KCompletionBasePrivate();
0033 
0034     void init();
0035 
0036     bool autoDeleteCompletionObject;
0037     bool handleSignals;
0038     bool emitSignals;
0039     KCompletion::CompletionMode completionMode;
0040     QPointer<KCompletion> completionObject;
0041     KCompletionBase::KeyBindingMap keyBindingMap;
0042     // we may act as a proxy to another KCompletionBase object
0043     KCompletionBase *delegate = nullptr;
0044     KCompletionBase * const q_ptr;
0045     Q_DECLARE_PUBLIC(KCompletionBase)
0046 };
0047 
0048 KCompletionBasePrivate::~KCompletionBasePrivate()
0049 {
0050     if (autoDeleteCompletionObject && completionObject) {
0051         delete completionObject;
0052     }
0053 }
0054 
0055 void KCompletionBasePrivate::init()
0056 {
0057     Q_Q(KCompletionBase);
0058     completionMode = KCompletion::CompletionPopup;
0059     delegate = nullptr;
0060     // Initialize all key-bindings to 0 by default so that
0061     // the event filter will use the global settings.
0062     q->useGlobalKeyBindings();
0063 
0064     q->setAutoDeleteCompletionObject(false);
0065     q->setHandleSignals(true);
0066     q->setEmitSignals(false);
0067 }
0068 
0069 
0070 KCompletionBase::KCompletionBase()
0071     : d_ptr(new KCompletionBasePrivate(this))
0072 {
0073     Q_D(KCompletionBase);
0074     d->init();
0075 }
0076 
0077 KCompletionBase::~KCompletionBase()
0078 {
0079 }
0080 
0081 void KCompletionBase::setDelegate(KCompletionBase *delegate)
0082 {
0083     Q_D(KCompletionBase);
0084     d->delegate = delegate;
0085 
0086     if (delegate) {
0087         delegate->setAutoDeleteCompletionObject(d->autoDeleteCompletionObject);
0088         delegate->setHandleSignals(d->handleSignals);
0089         delegate->setEmitSignals(d->emitSignals);
0090         delegate->setCompletionMode(d->completionMode);
0091         delegate->setKeyBindingMap(d->keyBindingMap);
0092     }
0093 }
0094 
0095 KCompletionBase *KCompletionBase::delegate() const
0096 {
0097     Q_D(const KCompletionBase);
0098     return d->delegate;
0099 }
0100 
0101 KCompletion *KCompletionBase::completionObject(bool handleSignals)
0102 {
0103     Q_D(KCompletionBase);
0104     if (d->delegate) {
0105         return d->delegate->completionObject(handleSignals);
0106     }
0107 
0108     if (!d->completionObject) {
0109         setCompletionObject(new KCompletion(), handleSignals);
0110         d->autoDeleteCompletionObject = true;
0111     }
0112     return d->completionObject;
0113 }
0114 
0115 void KCompletionBase::setCompletionObject(KCompletion *completionObject, bool handleSignals)
0116 {
0117     Q_D(KCompletionBase);
0118     if (d->delegate) {
0119         d->delegate->setCompletionObject(completionObject, handleSignals);
0120         return;
0121     }
0122 
0123     if (d->autoDeleteCompletionObject && completionObject != d->completionObject) {
0124         delete d->completionObject;
0125     }
0126 
0127     d->completionObject = completionObject;
0128 
0129     setAutoDeleteCompletionObject(false);
0130     setHandleSignals(handleSignals);
0131 
0132     // We emit rotation and completion signals
0133     // if completion object is not NULL.
0134     setEmitSignals(!d->completionObject.isNull());
0135 }
0136 
0137 // BC: Inline this function and possibly rename it to setHandleEvents??? (DA)
0138 void KCompletionBase::setHandleSignals(bool handle)
0139 {
0140     Q_D(KCompletionBase);
0141     if (d->delegate) {
0142         d->delegate->setHandleSignals(handle);
0143     } else {
0144         d->handleSignals = handle;
0145     }
0146 }
0147 
0148 bool KCompletionBase::isCompletionObjectAutoDeleted() const
0149 {
0150     Q_D(const KCompletionBase);
0151     return d->delegate ? d->delegate->isCompletionObjectAutoDeleted()
0152            : d->autoDeleteCompletionObject;
0153 }
0154 
0155 void KCompletionBase::setAutoDeleteCompletionObject(bool autoDelete)
0156 {
0157     Q_D(KCompletionBase);
0158     if (d->delegate) {
0159         d->delegate->setAutoDeleteCompletionObject(autoDelete);
0160     } else {
0161         d->autoDeleteCompletionObject = autoDelete;
0162     }
0163 }
0164 
0165 void KCompletionBase::setEnableSignals(bool enable)
0166 {
0167     Q_D(KCompletionBase);
0168     if (d->delegate) {
0169         d->delegate->setEnableSignals(enable);
0170     } else {
0171         d->emitSignals = enable;
0172     }
0173 }
0174 
0175 bool KCompletionBase::handleSignals() const
0176 {
0177     Q_D(const KCompletionBase);
0178     return d->delegate ? d->delegate->handleSignals() : d->handleSignals;
0179 }
0180 
0181 bool KCompletionBase::emitSignals() const
0182 {
0183     Q_D(const KCompletionBase);
0184     return d->delegate ? d->delegate->emitSignals() : d->emitSignals;
0185 }
0186 
0187 void KCompletionBase::setEmitSignals(bool emitRotationSignals)
0188 {
0189     Q_D(KCompletionBase);
0190     if (d->delegate) {
0191         d->delegate->setEmitSignals(emitRotationSignals);
0192     } else {
0193         d->emitSignals = emitRotationSignals;
0194     }
0195 }
0196 
0197 void KCompletionBase::setCompletionMode(KCompletion::CompletionMode mode)
0198 {
0199     Q_D(KCompletionBase);
0200     if (d->delegate) {
0201         d->delegate->setCompletionMode(mode);
0202         return;
0203     }
0204 
0205     d->completionMode = mode;
0206     // Always sync up KCompletion mode with ours as long as we
0207     // are performing completions.
0208     if (d->completionObject && d->completionMode != KCompletion::CompletionNone) {
0209         d->completionObject->setCompletionMode(d->completionMode);
0210     }
0211 }
0212 
0213 KCompletion::CompletionMode KCompletionBase::completionMode() const
0214 {
0215     Q_D(const KCompletionBase);
0216     return d->delegate ? d->delegate->completionMode() : d->completionMode;
0217 }
0218 
0219 bool KCompletionBase::setKeyBinding(KeyBindingType item, const QList<QKeySequence> &cut)
0220 {
0221     Q_D(KCompletionBase);
0222     if (d->delegate) {
0223         return d->delegate->setKeyBinding(item, cut);
0224     }
0225 
0226     if (!cut.isEmpty()) {
0227         for (KeyBindingMap::Iterator it = d->keyBindingMap.begin(); it != d->keyBindingMap.end(); ++it)
0228             if (it.value() == cut) {
0229                 return false;
0230             }
0231     }
0232     d->keyBindingMap.insert(item, cut);
0233     return true;
0234 }
0235 
0236 QList<QKeySequence> KCompletionBase::keyBinding(KeyBindingType item) const
0237 {
0238     Q_D(const KCompletionBase);
0239     return d->delegate ? d->delegate->keyBinding(item) : d->keyBindingMap[ item ];
0240 }
0241 
0242 void KCompletionBase::useGlobalKeyBindings()
0243 {
0244     Q_D(KCompletionBase);
0245     if (d->delegate) {
0246         d->delegate->useGlobalKeyBindings();
0247         return;
0248     }
0249 
0250     d->keyBindingMap.clear();
0251     d->keyBindingMap.insert(TextCompletion, QList<QKeySequence>());
0252     d->keyBindingMap.insert(PrevCompletionMatch, QList<QKeySequence>());
0253     d->keyBindingMap.insert(NextCompletionMatch, QList<QKeySequence>());
0254     d->keyBindingMap.insert(SubstringCompletion, QList<QKeySequence>());
0255 }
0256 
0257 KCompletion *KCompletionBase::compObj() const
0258 {
0259     Q_D(const KCompletionBase);
0260     return d->delegate ? d->delegate->compObj()
0261            : static_cast<KCompletion *>(d->completionObject);
0262 }
0263 
0264 KCompletionBase::KeyBindingMap KCompletionBase::keyBindingMap() const
0265 {
0266     Q_D(const KCompletionBase);
0267     return d->delegate ? d->delegate->keyBindingMap() : d->keyBindingMap;
0268 }
0269 
0270 void KCompletionBase::setKeyBindingMap(KCompletionBase::KeyBindingMap keyBindingMap)
0271 {
0272     Q_D(KCompletionBase);
0273     if (d->delegate) {
0274         d->delegate->setKeyBindingMap(keyBindingMap);
0275         return;
0276     }
0277 
0278     d->keyBindingMap = keyBindingMap;
0279 }
0280 
0281 void KCompletionBase::virtual_hook(int, void *)
0282 {
0283     /*BASE::virtual_hook( id, data );*/
0284 }