/****************************************************************************
** 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 <qfiledialog.h>
#include <qpixmap.h>
#include <qsinterpreter.h>
#include <qsproject.h>
#include <qmessagebox.h>

void AddScriptDialog::init()
{
    // List all global functions of the project
    comboFunction->insertStringList( QSInterpreter::defaultInterpreter()->
				     globalFunctions() );
}

void AddScriptDialog::choosePixmap()
{
    QString f = QFileDialog::getOpenFileName();
    if ( f.isEmpty() )
	return;
    QPixmap pix( f );
    labelPixmap->setPixmap( pix );
}

void AddScriptDialog::addScript()
{
    QSInterpreter *script = QSInterpreter::defaultInterpreter();
    QString func = comboFunction->currentText();
    if ( script->globalFunctions().findIndex( func ) == -1 ) {
	QString msg = tr( "The function <b>%1</b> doesn't exist. "
			  "Do you want to add it?" ).arg( func );
	if ( QMessageBox::information( 0, tr( "Add Function" ), msg,
				       tr( "&Yes" ), tr( "&No" ),
				       "", 0, 1 ) == 0 ) {
	    // if the function doesn't exist yet, add it to the
	    // project and open the IDE with it
	    script->currentProject()->addFunction( func );
	}
    }


    emit newScript( func, editName->text(), *labelPixmap->pixmap() );
    accept();
}
