/* This file is part of the KDE project
   Copyright (C) 2006 Jarosław Staniek <staniek@kde.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "widgetwithsubpropertiesinterface.h"

#include <QMetaObject>
#include <QPointer>
#include <QDebug>

#include <kexiutils/utils.h>

using namespace KFormDesigner;

class WidgetWithSubpropertiesInterface::Private
{
public:
    Private()
    {
    }

    ~Private()
    {
    }

    QPointer<QWidget> subwidget;
    QSet<QByteArray> subproperties;
};

WidgetWithSubpropertiesInterface::WidgetWithSubpropertiesInterface() : d(new Private())
{
}

WidgetWithSubpropertiesInterface::~WidgetWithSubpropertiesInterface()
{
    delete d;
}

void WidgetWithSubpropertiesInterface::setSubwidget(QWidget *widget)
{
    d->subwidget = widget;
    d->subproperties.clear();
    QSet<QByteArray> addedSubproperties;
    const QObject *thisObject = dynamic_cast<const QObject*>(this);
    if (thisObject && d->subwidget) {
        //remember properties in the subwidget that are not present in the parent
        for (const QMetaObject *metaObject = d->subwidget->metaObject(); metaObject;
                metaObject = metaObject->superClass()) {
            QList<QMetaProperty> properties(
                KexiUtils::propertiesForMetaObjectWithInherited(metaObject));
            foreach(const QMetaProperty &property, properties) {
                if (-1 != thisObject->metaObject()->indexOfProperty(property.name())
                    && !addedSubproperties.contains(property.name()))
                {
                    d->subproperties.insert(property.name());
                    addedSubproperties.insert(property.name());
                    qDebug() << "added subwidget's property that is not present in the parent: "
                        << property.name();
                }
            }
        }
    }
}

QWidget* WidgetWithSubpropertiesInterface::subwidget() const
{
    return d->subwidget;
}

QSet<QByteArray> WidgetWithSubpropertiesInterface::subproperties() const
{
    return d->subproperties;
}

QMetaProperty WidgetWithSubpropertiesInterface::findMetaSubproperty(const char * name) const
{
    if (!d->subwidget || d->subproperties.contains(name))
        return QMetaProperty();
    return KexiUtils::findPropertyWithSuperclasses(d->subwidget, name);
}

QVariant WidgetWithSubpropertiesInterface::subproperty(const char * name, bool *ok) const
{
    Q_ASSERT(ok);
    if (!d->subwidget || d->subproperties.contains(name)) {
        *ok = false;
        return QVariant();
    }
    *ok = true;
    return d->subwidget->property(name);
}

bool WidgetWithSubpropertiesInterface::setSubproperty(const char * name, const QVariant & value)
{
    if (!d->subwidget || d->subproperties.contains(name))
        return false;
    return d->subwidget->setProperty(name, value);
}
