/****************************************************************************
** $Id: quickqtwidgets.cpp  beta1   edited Dec 10 13:07 $
**
** Copyright (C) 2001-2002 Trolltech AS.  All rights reserved.
**
** This file is part of the Qt Script for Applications framework (QSA).
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding a valid QSA Beta Evaluation Version license may use
** this file in accordance with the QSA Beta Evaluation Version License
** Agreement provided with the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
**   information about QSA Commercial License Agreements.
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
*****************************************************************************/

#include "quickqtwidgets.h"

#include <qptrlist.h>
#include <qlineedit.h>
#include <qwidget.h>
#include <qdatetimeedit.h>
#include <qtable.h>
#include <qsocket.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qwidgetfactory.h>
#include <qbuffer.h>
#include <qpopupmenu.h>
#include <qlayout.h>
#include <qwidgetstack.h>
#include <qlistbox.h>
#include <qcombobox.h>
#include <qtextedit.h>
#include <qapplication.h>
#include <qobjectlist.h>
#include <qtimer.h>

QuickWidgetInterface::QuickWidgetInterface( QWidget* w )
    : wid( w )
{
    wid->installEventFilter( this );
    wid->setMouseTracking( TRUE );
    inited = FALSE;
}

void QuickWidgetInterface::setX( int x )
{
    wid->move( x, wid->y() );
}

void QuickWidgetInterface::setY( int y )
{
    wid->move( wid->x(), y );
}

void QuickWidgetInterface::setWidth( int w )
{
    wid->resize( w, wid->height() );
}

void QuickWidgetInterface::setHeight( int h )
{
    wid->resize( wid->width(), h );
}

int QuickWidgetInterface::x() const
{
    return wid->x();
}

int QuickWidgetInterface::y() const
{
    return wid->y();
}

int QuickWidgetInterface::width() const
{
    return wid->width();
}

int QuickWidgetInterface::height() const
{
    return wid->height();
}

bool QuickWidgetInterface::eventFilter( QObject *, QEvent *e )
{
    QMouseEvent *me;
    QKeyEvent *ke;
    QWheelEvent *we;
    QResizeEvent *re;
    switch ( e->type() ) {
    case QEvent::MouseMove:
	me = (QMouseEvent*)e;
	emit mouseMoved( me->x(), me->y(), me->state() );
	break;
    case QEvent::MouseButtonPress:
	me = (QMouseEvent*)e;
	emit mousePressed( me->x(), me->y(), me->button() );
	break;
    case QEvent::MouseButtonRelease:
	me = (QMouseEvent*)e;
	emit mouseReleased( me->x(), me->y(), me->button() );
	break;
    case QEvent::MouseButtonDblClick:
	me = (QMouseEvent*)e;
	emit mouseDoubleClicked( me->x(), me->y(), me->button() );
	break;
    case QEvent::KeyPress:
	ke = (QKeyEvent*)e;
	emit keyPressed( ke->key(), ke->ascii(), ke->state(), ke->text() );
	break;
    case QEvent::Wheel:
	we = (QWheelEvent*)e;
	emit wheelRotated( we->pos().x(), we->pos().y(), we->delta(), we->state() );
	break;
    case QEvent::Resize:
	re = (QResizeEvent*)e;
	emit resized( re->oldSize().width(), re->oldSize().height() );
	break;
    case QEvent::Show:
	if ( !inited )
	    QTimer::singleShot( 0, this, SIGNAL( init() ) );
	inited = TRUE;
	QTimer::singleShot( 0, this, SIGNAL( shown() ) );
	break;
    case QEvent::Hide:
	emit hidden();
	break;
    case QEvent::Close:
	emit closed();
	break;
    default:
	break;
    };
    return FALSE;
}

QObjectList *QuickWidgetInterface::children( bool recursive ) const
{
    return wid->queryList( "QWidget", 0, recursive );
}

QObject *QuickWidgetInterface::child( const QString &name ) const
{
    return wid->child( name );
}


QObjectListPtr::QObjectListPtr( QObjectList *l )
    : QuickPtrDispatchObject( "QObjectList", l )
{
}

int QObjectListPtr::count() const
{
    return list()->count();
}

QObject *QObjectListPtr::at( int i ) const
{
    return list()->at( i );
}

QObjectList *QObjectListPtr::list() const
{
    return (QObjectList*)pointer();
}





QuickPopupMenuInterface::QuickPopupMenuInterface( QPopupMenu *m )
    : popup( m )
{
}

int QuickPopupMenuInterface::exec()
{
    return popup->exec( QCursor::pos() );
}

int QuickPopupMenuInterface::insertItem( const QString &text )
{
    return popup->insertItem( text );
}

int QuickPopupMenuInterface::insertItem( const QString &text, int id )
{
    return popup->insertItem( text, 0, 0, 0, id );
}

int QuickPopupMenuInterface::insertItem( const QString &text, int id, int index )
{
    return popup->insertItem( text, 0, 0, 0, id, index );
}

int QuickPopupMenuInterface::insertSeparator()
{
    return popup->insertSeparator();
}



QuickDateEditInterface::QuickDateEditInterface( QDateEdit *de )
    : dateEdit( de )
{
}

void QuickDateEditInterface::setDay( int d )
{
    QDate dt( dateEdit->date() );
    dt.setYMD( dt.year(), dt.month(), d );
    dateEdit->setDate( dt );
}

void QuickDateEditInterface::setMonth( int m )
{
    QDate dt( dateEdit->date() );
    dt.setYMD( dt.year(), m, dt.day() );
    dateEdit->setDate( dt );
}

void QuickDateEditInterface::setYear( int y )
{
    QDate dt( dateEdit->date() );
    dt.setYMD( y, dt.month(), dt.day() );
    dateEdit->setDate( dt );
}

int QuickDateEditInterface::day() const
{
    return dateEdit->date().day();
}

int QuickDateEditInterface::month() const
{
    return dateEdit->date().month();
}

int QuickDateEditInterface::year() const
{
    return dateEdit->date().year();
}

//////////

QuickTableInterface::QuickTableInterface( QTable* t )
    : table( t )
{
}

int QuickTableInterface::currentRow() const
{
    return table->currentRow();
}

int QuickTableInterface::currentColumn() const
{
    return table->currentColumn();
}

QString QuickTableInterface::text( int row, int col ) const
{
    return table->text( row, col );
}

void QuickTableInterface::setText( int row, int col, const QString &text )
{
    table->setText( row, col, text );
}

void QuickTableInterface::setHorizontalLabel( int column, const QString &text )
{
    table->horizontalHeader()->setLabel( column, text );
}

void QuickTableInterface::setVerticalLabel( int column, const QString &text )
{
    table->verticalHeader()->setLabel( column, text );
}

////////////

QuickListViewInterface::QuickListViewInterface( QListView* lv )
    : listview( lv )
{
}

QListViewItem *QuickListViewInterface::findItem( const QString &text, int column ) const
{
    qApp->processEvents();
    return listview->findItem( text, column );
}

void QuickListViewInterface::ensureItemVisible( QListViewItem *item )
{
    listview->ensureItemVisible( item );
}

QListViewItem *QuickListViewInterface::currentItem() const
{
    return listview->currentItem();
}

void QuickListViewInterface::setSelected( QListViewItem *item, bool selected )
{
    listview->setSelected( item, selected );
}

void QuickListViewInterface::clearSelection()
{
    listview->clearSelection();
}

void QuickListViewInterface::setCurrentItem( QListViewItem *item )
{
    listview->setCurrentItem( item );
}

void QuickListViewInterface::setColumnWidth( int column, int w )
{
    listview->setColumnWidth( column, w );
}

void QuickListViewInterface::setSorting( int column )
{
    listview->setSorting( column );
}

void QuickListViewInterface::sort()
{
    listview->sort();
}

QListViewItem *QuickListViewInterface::firstChild() const
{
    return listview->firstChild();
}

QListViewItem *QuickListViewInterface::lastItem() const
{
    return listview->lastItem();
}

////////////



QuickToolTip::QuickToolTip()
{
}

void QuickToolTip::add( QWidget *widget, const QString &text )
{
    QToolTip::add( widget, text );
}

void QuickToolTip::remove( QWidget *widget )
{
    QToolTip::remove( widget );
}



QuickWhatsThis::QuickWhatsThis()
{
}

void QuickWhatsThis::add( QWidget *widget, const QString &text )
{
    QWhatsThis::add( widget, text );
}

void QuickWhatsThis::remove( QWidget *widget )
{
    QWhatsThis::remove( widget );
}




QListViewItemPtr::QListViewItemPtr( QListViewItem *i )
    : QuickPtrDispatchObject( "QListViewItem", i ) { }


QString QListViewItemPtr::text( int column ) const
{
    return item()->text( column );
}


QListViewItem *QListViewItemPtr::item() const
{
    return (QListViewItem*)pointer();
}


void QListViewItemPtr::setText( int column, const QString &text )
{
    item()->setText( column, text );
}


QListViewItem *QListViewItemPtr::parent() const
{
    if ( item() )
	return (QListViewItem*)item()->parent();
    return 0;
}


void QListViewItemPtr::setOpen( bool open )
{
    item()->setOpen( open );
}

void QListViewItemPtr::setSelected( bool selected )
{
    item()->setSelected( selected );
}

bool QListViewItemPtr::isOpen() const
{
    return item()->isOpen();
}

bool QListViewItemPtr::isSelected() const
{
    return item()->isSelected();
}

void QListViewItemPtr::remove()
{
    delete item();
    setPointer( 0 );
}

QListViewItem *QListViewItemPtr::itemAbove()
{
    return (QListViewItem*)item()->itemAbove();
}


QListViewItem *QListViewItemPtr::itemBelow()
{
    return (QListViewItem*)item()->itemBelow();
}


QListBoxItemPtr::QListBoxItemPtr( QListBoxItem *i )
    : QuickPtrDispatchObject( "QListBoxItem", i ) { }

QListBoxItem *QListBoxItemPtr::item() const
{
    return (QListBoxItem*)pointer();
}

QString QListBoxItemPtr::text() const
{
    return item()->text( );
}

QuickWidgetStackInterface::QuickWidgetStackInterface( QWidgetStack *m )
    : widgetstack( m )
{
}

void QuickWidgetStackInterface::addWidget( QWidget *widget, int id )
{
    widgetstack->addWidget( widget, id );
}

void QuickWidgetStackInterface::removeWidget( QWidget *widget )
{
    widgetstack->removeWidget( widget );
}

QWidget *QuickWidgetStackInterface::widget( int id ) const
{
    return widgetstack->widget( id );
}

int QuickWidgetStackInterface::id( QWidget *widget ) const
{
    return widgetstack->id( widget );
}

QWidget *QuickWidgetStackInterface::visibleWidget() const
{
    return widgetstack->visibleWidget();
}


QuickWidgetFactory::QuickWidgetFactory()
{
    factory = new QWidgetFactory;
}

QuickWidgetFactory::~QuickWidgetFactory()
{
    delete factory;
}

QWidget *QuickWidgetFactory::createWidget( const QString &className, QWidget *parent, const char *name )
{
    QWidget *w = factory->createWidget( className, parent, name );
    return w;
}

QWidget *QuickWidgetFactory::createForm( const QString &xmlContents )
{
    QBuffer buf( xmlContents.utf8() );
    return QWidgetFactory::create( &buf );
}

QWidget *QuickWidgetFactory::createFormFromFile( const QString &xmlFile )
{
    return QWidgetFactory::create( xmlFile );
}

QLayout *QuickWidgetFactory::createBoxLayout( const QString &className, QWidget *parent, const char *name )
{
    if ( className == "QHBoxLayout" )
	return new QHBoxLayout( parent, 0, -1, name );
    else if ( className == "QVBoxLayout" )
	return new QVBoxLayout( parent, 0, -1, name );
    return 0;
}

QLayout *QuickWidgetFactory::createBoxLayout( const QString &className, QLayout *parent, const char *name )
{
    if ( className == "QHBoxLayout" )
	return new QHBoxLayout( parent, -1, name );
    else if ( className == "QVBoxLayout" )
	return new QVBoxLayout( parent, -1, name );
    return 0;
}

QuickHBoxLayoutInterface::QuickHBoxLayoutInterface( QHBoxLayout *l )
    : lay( l )
{
}

void QuickHBoxLayoutInterface::addSpacing( int size )
{
    lay->addSpacing( size );
}

void QuickHBoxLayoutInterface::addStretch( int stretch )
{
    lay->addStretch( stretch );
}

void QuickHBoxLayoutInterface::addWidget( QWidget *widget, int stretch )
{
    lay->addWidget( widget, stretch );
}

void QuickHBoxLayoutInterface::setAutoAdd( bool b )
{
    lay->setAutoAdd( b );
}

bool QuickHBoxLayoutInterface::autoAdd() const
{
    return lay->autoAdd();
}


QuickVBoxLayoutInterface::QuickVBoxLayoutInterface( QVBoxLayout *l )
    : lay( l )
{
}

void QuickVBoxLayoutInterface::addSpacing( int size )
{
    lay->addSpacing( size );
}

void QuickVBoxLayoutInterface::addStretch( int stretch )
{
    lay->addStretch( stretch );
}

void QuickVBoxLayoutInterface::addWidget( QWidget *widget, int stretch )
{
    lay->addWidget( widget, stretch );
}

void QuickVBoxLayoutInterface::setAutoAdd( bool b )
{
    lay->setAutoAdd( b );
}

bool QuickVBoxLayoutInterface::autoAdd() const
{
    return lay->autoAdd();
}




QuickListBoxInterface::QuickListBoxInterface( QListBox* lb )
    : listbox( lb )
{
}

void QuickListBoxInterface::insertItem( const QString &text, int index )
{
    listbox->insertItem( text, index );
}

void QuickListBoxInterface::removeItem( int index )
{
    listbox->removeItem( index );
}

void QuickListBoxInterface::changeItem( const QString &text, int index )
{
    listbox->changeItem( text, index );
}

void QuickListBoxInterface::setSelected( int item, bool select )
{
    listbox->setSelected( item, select );
}

bool QuickListBoxInterface::isSelected( int item ) const
{
    return listbox->isSelected( item );
}

void QuickListBoxInterface::sort( bool ascending )
{
    listbox->sort( ascending );
}

QString QuickListBoxInterface::text( int index ) const
{
    return listbox->text( index );
}



QuickComboBoxInterface::QuickComboBoxInterface( QComboBox* cb )
    : combobox( cb )
{
}

void QuickComboBoxInterface::insertItem( const QString &text, int index )
{
    combobox->insertItem( text, index );
}

void QuickComboBoxInterface::removeItem( int index )
{
    combobox->removeItem( index );
}

void QuickComboBoxInterface::changeItem( const QString &text, int index )
{
    combobox->changeItem( text, index );
}

void QuickComboBoxInterface::setCurrentItem( int index )
{
    combobox->setCurrentItem( index );
}

QString QuickComboBoxInterface::text( int index ) const
{
    return combobox->text( index );
}



QuickTextEditInterface::QuickTextEditInterface( QTextEdit *te )
    : textedit( te )
{
}

bool QuickTextEditInterface::replace( const QString &find, const QString &replace, bool cs, bool wo,
				      bool forward, bool startAtCursor, bool replaceAll )
{
    bool ok = FALSE;
    if ( startAtCursor ) {
	ok = textedit->find( find, cs, wo, forward );
    } else {
	int dummy = 0;
	ok =  textedit->find( find, cs, wo, forward, &dummy, &dummy );
    }

    if ( ok ) {
	textedit->removeSelectedText();
	textedit->insert( replace, FALSE, FALSE );
    }

    if ( !replaceAll || !ok )
	return ok;

    bool ok2 = TRUE;
    while ( ok2 ) {
	ok2 = textedit->find( find, cs, wo, forward );
	if ( ok2 ) {
	    textedit->removeSelectedText();
	    textedit->insert( replace, FALSE, FALSE );
	}
    }

    return TRUE;
}

bool QuickTextEditInterface::find( const QString &expr, bool cs, bool wo, bool forward )
{
    return textedit->find( expr, cs, wo, forward );
}
