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

0001 /* This file is part of the KDE project
0002    Copyright (C) 2006 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 "widgetwithsubpropertiesinterface.h"
0021 
0022 #include <QMetaObject>
0023 #include <QPointer>
0024 #include <QDebug>
0025 
0026 #include <kexiutils/utils.h>
0027 
0028 using namespace KFormDesigner;
0029 
0030 class Q_DECL_HIDDEN WidgetWithSubpropertiesInterface::Private
0031 {
0032 public:
0033     Private()
0034     {
0035     }
0036 
0037     ~Private()
0038     {
0039     }
0040 
0041     QPointer<QWidget> subwidget;
0042     QSet<QByteArray> subproperties;
0043 };
0044 
0045 WidgetWithSubpropertiesInterface::WidgetWithSubpropertiesInterface() : d(new Private())
0046 {
0047 }
0048 
0049 WidgetWithSubpropertiesInterface::~WidgetWithSubpropertiesInterface()
0050 {
0051     delete d;
0052 }
0053 
0054 void WidgetWithSubpropertiesInterface::setSubwidget(QWidget *widget)
0055 {
0056     d->subwidget = widget;
0057     d->subproperties.clear();
0058     QSet<QByteArray> addedSubproperties;
0059     const QObject *thisObject = dynamic_cast<const QObject*>(this);
0060     if (thisObject && d->subwidget) {
0061         //remember properties in the subwidget that are not present in the parent
0062         for (const QMetaObject *metaObject = d->subwidget->metaObject(); metaObject;
0063                 metaObject = metaObject->superClass()) {
0064             QList<QMetaProperty> properties(
0065                 KexiUtils::propertiesForMetaObjectWithInherited(metaObject));
0066             foreach(const QMetaProperty &property, properties) {
0067                 if (-1 != thisObject->metaObject()->indexOfProperty(property.name())
0068                     && !addedSubproperties.contains(property.name()))
0069                 {
0070                     d->subproperties.insert(property.name());
0071                     addedSubproperties.insert(property.name());
0072                     qDebug() << "added subwidget's property that is not present in the parent: "
0073                         << property.name();
0074                 }
0075             }
0076         }
0077     }
0078 }
0079 
0080 QWidget* WidgetWithSubpropertiesInterface::subwidget() const
0081 {
0082     return d->subwidget;
0083 }
0084 
0085 QSet<QByteArray> WidgetWithSubpropertiesInterface::subproperties() const
0086 {
0087     return d->subproperties;
0088 }
0089 
0090 QMetaProperty WidgetWithSubpropertiesInterface::findMetaSubproperty(const char * name) const
0091 {
0092     if (!d->subwidget || d->subproperties.contains(name))
0093         return QMetaProperty();
0094     return KexiUtils::findPropertyWithSuperclasses(d->subwidget, name);
0095 }
0096 
0097 QVariant WidgetWithSubpropertiesInterface::subproperty(const char * name, bool *ok) const
0098 {
0099     Q_ASSERT(ok);
0100     if (!d->subwidget || d->subproperties.contains(name)) {
0101         *ok = false;
0102         return QVariant();
0103     }
0104     *ok = true;
0105     return d->subwidget->property(name);
0106 }
0107 
0108 bool WidgetWithSubpropertiesInterface::setSubproperty(const char * name, const QVariant & value)
0109 {
0110     if (!d->subwidget || d->subproperties.contains(name))
0111         return false;
0112     return d->subwidget->setProperty(name, value);
0113 }