/*
 *  Copyright (C) 2003, Richard J. Moore <rich@kde.org>
 *
 *  This library 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 library 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 library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 */

#include <qmetaobject.h>
#include <qobjectlist.h>
#include <qvariant.h>
#include <qwidget.h>
#include <qwidgetfactory.h>

#include <kaboutdata.h>
#include <kapplication.h>
#include <kcmdlineargs.h>
#include <kdebug.h>
#include <klocale.h>

#include "kuiloader.h"

void KUILoader::dumpProperties( QTextStream &ts, int level, QObject *object, uint /*query*/ )
{
    QCString buf("    ");
    buf.fill( '\t', level/2 );
    if ( level % 2 )
	buf += QCString("    ");

    QMetaObject *meta = object->metaObject();
    int propCount = meta->numProperties( true );
    for ( int i = 0 ; i < propCount ; i++ ) {
	ts << buf << "\t\t\t"
	   << meta->property( i, true )->type()
	   << " " << meta->property( i, true )->name()
	   << endl;
    }

}

QString KUILoader::findValue( QObject *object, uint /*query*/ )
{
    QVariant val;
    QMetaObject *meta = object->metaObject();

    if ( meta->findProperty( "value", true ) != -1 ) {
	val = object->property("value");
    }
    else if ( meta->findProperty( "checked", true ) != -1 ) {
	val = object->property("checked");
    }
    else if ( meta->findProperty( "text", true ) != -1 ) {
	val = object->property("text");
    }

    if ( !val.isValid() )
	return QString::null;

    return val.toString();
}

void KUILoader::dumpSelf( QTextStream &ts, int level, QObject *object, uint /*query*/ )
{
    QCString buf;
    buf.fill( '\t', level/2 );
    if ( level % 2 )
	buf += QCString("    ");
    ts << buf;
    ts << object->className() << "::" << object->name() << endl;
}

void KUILoader::dumpField( QTextStream &ts, QObject *object, uint query )
{
    QString val = KUILoader::findValue( object, query );
    ts << object->name() << "," << val << endl;
}

void KUILoader::dumpTree( QTextStream &ts, int level, QObject *object, uint query )
{
    if ( !object )
	return;

    QWidget *field = object->isWidgetType() ? static_cast<QWidget *>(object) : 0;

    if ( query & MatchFields ) {
	if ( field && field->isFocusEnabled() ) {
	    KUILoader::dumpField( ts, object, query );
	    if ( query & MatchProperties )
		KUILoader::dumpProperties( ts, level, object, query );
	}
    }
    else if ( (query & MatchWidgets) && object->isWidgetType() ) {
	KUILoader::dumpSelf( ts, level, object, query );
	if ( query & MatchProperties )
	    KUILoader::dumpProperties( ts, level, object, query );
    }
    else if ( query & MatchObjects ) {
	KUILoader::dumpSelf( ts, level, object, query );
	if ( query & MatchProperties )
	    KUILoader::dumpProperties( ts, level, object, query );
    }

    if ( object->children() ) {
	QObjectListIt it(*object->children());
	QObject * c;
	while ( (c=it.current()) != 0 ) {
	    ++it;
	    dumpTree( ts, level+1, c, query );
	}
    }
}


// Local Variables:
// c-basic-offset: 4
// End:
