/*
 *  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"

static KCmdLineOptions options[] =
{
    { "f", 0, 0 },
    { "fields", I18N_NOOP("Prints the widgets that can take focus to stdout."), 0 },
    { "w", 0, 0 },
    { "widgets", I18N_NOOP("Prints the widget tree to stdout."), 0 },
    { "a", 0, 0 },
    { "all", I18N_NOOP("Prints the entire object tree to stdout."), 0 },

    { "s", 0, 0 },
    { "self", I18N_NOOP("Prints the matching items to stdout."), 0 },
    { "p", 0, 0 },
    { "print", I18N_NOOP("Prints the values of widgets to stdout."), 0 },
    { "l", 0, 0 },
    { "properties", I18N_NOOP("Prints the properties of each object to stdout."), 0 },

    { "+uifile", I18N_NOOP("UI file to display."), 0 },
    { 0, 0, 0 }
};


int main( int argc, char **argv )
{
    // Cmd Line
    KAboutData about( "kuiloader", I18N_NOOP("KUILoader"), "0.1",
		      I18N_NOOP("Displayus Designer UI files from the command line."),
		      KAboutData::License_LGPL, I18N_NOOP("(c) 2003 Richard Moore") );
    about.addAuthor( "Richard Moore", 0, "rich@kde.org" );

    KCmdLineArgs::init( argc, argv, &about );
    KCmdLineArgs::addCmdLineOptions( options ); // Add our own options.
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    KApplication app;

    //
    // Load UI dialog
    //
    if ( !args->count() ) {
	KCmdLineArgs::usage();
	app.exit( 1 );
    }
    
    QWidget *w = QWidgetFactory::create( QCString(args->arg(0)), 0, 0, 0 );

    //
    // Tree Dumping Options
    //
    uint query = KUILoader::MatchNone;

    if ( args->isSet("fields") || args->isSet("f") ) {
	query |= KUILoader::MatchFields;
    }
    if ( args->isSet("widgets") || args->isSet("w") ) {
	query |= KUILoader::MatchWidgets;
    }
    else if ( args->isSet("all") || args->isSet("a") ) {
	query |= KUILoader::MatchObjects;
    }

    if ( args->isSet("print") || args->isSet("p") ) {
	query |= KUILoader::MatchValue;
    }

    if ( args->isSet("properties") || args->isSet("l") ) {
	query |= KUILoader::MatchProperties;
    }

    if ( query != KUILoader::MatchNone ) {
	QTextStream ts( stdout, IO_WriteOnly );
	KUILoader::dumpTree( ts, 0, w, query );
    }

    //
    // Display window.
    //
    app.setMainWidget(w);
    w->show();

    return app.exec();
}

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