/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you wish to add, delete or rename slots use Qt Designer which will
** update this file, preserving your code. Create an init() slot in place of
** a constructor, and a destroy() slot in place of a destructor.
*****************************************************************************/

#include <qsinterpreter.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qinputdialog.h>
#include <qmessagebox.h>

void Form1::init()
{
    QSInterpreter *ip = QSInterpreter::defaultInterpreter();
    ip->setErrorMode( QSInterpreter::Notify );
    ip->addObject( PlayGround );
    QFile f( "script.qs" );
    if ( f.open( IO_ReadOnly ) ) {
	QTextStream ts( &f );
	editCode->setText( ts.read(), PlayGround );
	editCode->activate();
    }
    f.close();
}

void Form1::keyPressEvent( QKeyEvent *e )
{
    if ( e->key() == Key_Escape ) { // let the editor handle the ESC key
	e->ignore();
	return;
    }
    QDialog::keyPressEvent( e );
}

void Form1::buttonCallFunction_clicked()
{
    QSInterpreter *ip = QSInterpreter::defaultInterpreter();
    ip->evaluate( editCode->text(), PlayGround );
    QString func = QInputDialog::getItem( "Choose a function", "Choose a function",
					  ip->functionsOf( "PlayGround" ),
					  0, FALSE );
    if ( func.isEmpty() )
	return;
    ip->call( func, QValueList<QVariant>(), PlayGround );
}
