File indexing completed on 2024-05-12 16:39:36

0001 /* This file is part of the KDE project
0002    Copyright (C) 2005-2012 Jarosław Staniek <staniek@kde.org>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this program; see the file COPYING.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "kexidataiteminterface.h"
0021 
0022 #include <QDebug>
0023 #include <QPointer>
0024 
0025 KexiDataItemChangesListener::KexiDataItemChangesListener()
0026 {
0027 }
0028 
0029 KexiDataItemChangesListener::~KexiDataItemChangesListener()
0030 {
0031 }
0032 
0033 //-----------------------------------------------
0034 class Q_DECL_HIDDEN KexiDataItemInterface::Private
0035 {
0036 public:
0037     Private();
0038     ~Private();
0039 
0040     QPointer<QObject> listenerObject;
0041     KexiDataItemChangesListener* listener;
0042     bool listenerIsQObject;
0043     QVariant origValue;
0044 
0045     /*! @see parentDataItemInterface() */
0046     KexiDataItemInterface* parentDataItemInterface;
0047     bool hasFocusableWidget;
0048     bool disable_signalValueChanged;
0049     bool acceptEditorAfterDeleteContents;
0050     bool lengthExceededEmittedAtPreviousChange;
0051 };
0052 
0053 KexiDataItemInterface::Private::Private()
0054     : listener(0), listenerIsQObject(false) ,parentDataItemInterface(0)
0055     ,hasFocusableWidget(true), disable_signalValueChanged(false), acceptEditorAfterDeleteContents(false)
0056     ,lengthExceededEmittedAtPreviousChange(false)
0057 {
0058 
0059 }
0060 
0061 KexiDataItemInterface::Private::~Private()
0062 {
0063 
0064 }
0065 
0066 KexiDataItemInterface::KexiDataItemInterface()
0067     : d(new Private())
0068 {
0069 }
0070 
0071 KexiDataItemInterface::~KexiDataItemInterface()
0072 {
0073     delete d;
0074 }
0075 
0076 KexiDataItemInterface* KexiDataItemInterface::parentDataItemInterface() const
0077 {
0078     return d->parentDataItemInterface;
0079 }
0080 
0081 bool KexiDataItemInterface::acceptEditorAfterDeleteContents() const
0082 {
0083     return d->acceptEditorAfterDeleteContents;
0084 }
0085 
0086 bool KexiDataItemInterface::hasFocusableWidget() const
0087 {
0088     return d->hasFocusableWidget;
0089 }
0090 
0091 void KexiDataItemInterface::setValue(const QVariant& value, const QVariant& add,
0092                                      bool removeOld, const QVariant* visibleValue)
0093 {
0094     d->disable_signalValueChanged = true; //to prevent emmiting valueChanged()
0095     if (dynamic_cast<QObject*>(this)) {
0096         /*qDebug() <<
0097             dynamic_cast<QObject*>(this)->metaObject()->className()
0098             << dynamic_cast<QWidget*>(this)->objectName()
0099             << "value=" << value << "add=" << add;*/
0100     }
0101     d->origValue = value;
0102     setValueInternal(add, removeOld);
0103     if (visibleValue)
0104         setVisibleValueInternal(*visibleValue);
0105     d->disable_signalValueChanged = false;
0106 }
0107 
0108 void KexiDataItemInterface::setVisibleValueInternal(const QVariant& value)
0109 {
0110     Q_UNUSED(value);
0111 }
0112 
0113 void KexiDataItemInterface::signalValueChanged()
0114 {
0115     if (d->disable_signalValueChanged || isReadOnly())
0116         return;
0117     if (d->parentDataItemInterface) {
0118         d->parentDataItemInterface->signalValueChanged();
0119         return;
0120     }
0121     if (d->listener) {
0122         beforeSignalValueChanged();
0123         d->listener->valueChanged(this);
0124     }
0125 }
0126 
0127 void KexiDataItemInterface::signalLengthExceeded(bool lengthExceeded)
0128 {
0129     if (d->listener) {
0130         d->listener->lengthExceeded(this, lengthExceeded);
0131     }
0132 }
0133 
0134 void KexiDataItemInterface::signalUpdateLengthExceededMessage()
0135 {
0136     if (d->listener) {
0137         d->listener->updateLengthExceededMessage(this);
0138     }
0139 }
0140 
0141 void KexiDataItemInterface::emitLengthExceededIfNeeded(bool lengthExceeded)
0142 {
0143     if (lengthExceeded && !d->lengthExceededEmittedAtPreviousChange) {
0144         d->lengthExceededEmittedAtPreviousChange = true;
0145         signalLengthExceeded(true);
0146     }
0147     else if (!lengthExceeded && d->lengthExceededEmittedAtPreviousChange) {
0148         d->lengthExceededEmittedAtPreviousChange = false;
0149         signalLengthExceeded(false);
0150     }
0151     else if (lengthExceeded) {
0152         signalUpdateLengthExceededMessage();
0153     }
0154 }
0155 
0156 bool KexiDataItemInterface::valueChanged()
0157 {
0158     //qDebug() << d->origValue.toString() << " ? " << value().toString();
0159     return d->origValue != value();
0160 }
0161 
0162 KexiDataItemChangesListener* KexiDataItemInterface::listener()
0163 {
0164     if (!d->listener || !d->listenerIsQObject)
0165         return d->listener;
0166     if (!d->listenerObject)
0167         d->listener = 0; //destroyed, update pointer
0168     return d->listener;
0169 }
0170 
0171 void KexiDataItemInterface::installListener(KexiDataItemChangesListener* listener)
0172 {
0173     d->listener = listener;
0174     d->listenerIsQObject = dynamic_cast<QObject*>(listener);
0175     if (d->listenerIsQObject)
0176         d->listenerObject = dynamic_cast<QObject*>(listener);
0177 }
0178 
0179 void KexiDataItemInterface::showFocus(const QRect& r, bool readOnly)
0180 {
0181     Q_UNUSED(r);
0182     Q_UNUSED(readOnly);
0183 }
0184 
0185 void KexiDataItemInterface::hideFocus()
0186 {
0187 }
0188 
0189 void KexiDataItemInterface::clickedOnContents()
0190 {
0191 }
0192 
0193 bool KexiDataItemInterface::valueIsValid()
0194 {
0195     return true;
0196 }
0197 
0198 void KexiDataItemInterface::setParentDataItemInterface(KexiDataItemInterface* parentDataItemInterface)
0199 {
0200     d->parentDataItemInterface = parentDataItemInterface;
0201 }
0202 
0203 bool KexiDataItemInterface::cursorAtNewRecord()
0204 {
0205     return listener() ? listener()->cursorAtNewRecord() : false;
0206 }
0207 
0208 bool KexiDataItemInterface::isComboBox() const
0209 {
0210     return false;
0211 }
0212 
0213 QWidget* KexiDataItemInterface::internalEditor() const
0214 {
0215     return 0;
0216 }
0217 
0218 bool KexiDataItemInterface::fixup()
0219 {
0220     return true;
0221 }
0222 
0223 void KexiDataItemInterface::setFocusableWidget(bool set)
0224 {
0225     d->hasFocusableWidget = set;
0226 }
0227 
0228 void KexiDataItemInterface::setFocus()
0229 {
0230     if (widget())
0231         widget()->setFocus();
0232 }
0233 
0234 void KexiDataItemInterface::showWidget()
0235 {
0236     if (widget())
0237         widget()->show();
0238 }
0239 
0240 void KexiDataItemInterface::hideWidget()
0241 {
0242     if (widget())
0243         widget()->hide();
0244 }
0245 
0246 QVariant KexiDataItemInterface::visibleValue()
0247 {
0248     return QVariant();
0249 }
0250 
0251 bool KexiDataItemInterface::isReadOnly() const
0252 {
0253 
0254     return false;
0255 
0256 }
0257 
0258 void KexiDataItemInterface::handleAction(const QString &actionName)
0259 {
0260     Q_UNUSED(actionName);
0261 }
0262 
0263 QVariant KexiDataItemInterface::originalValue() const
0264 {
0265     return d->origValue;
0266 }
0267 
0268 void KexiDataItemInterface::setHasFocusableWidget(bool set) const
0269 {
0270     d->hasFocusableWidget = set;
0271 }
0272 
0273 void KexiDataItemInterface::setAcceptEditorAfterDeleteContents(bool set) const
0274 {
0275     d->acceptEditorAfterDeleteContents = set;
0276 }
0277 
0278 bool KexiDataItemInterface::lengthExceededEmittedAtPreviousChange() const
0279 {
0280     return d->lengthExceededEmittedAtPreviousChange;
0281 }
0282 
0283 void KexiDataItemInterface::setLengthExceededEmittedAtPreviousChange(bool set)
0284 {
0285     d->lengthExceededEmittedAtPreviousChange = set;
0286 }