simplescripteditor.cpp
author insilmaril
Mon, 05 Mar 2007 23:22:51 +0000
changeset 432 f867269ab8a1
child 434 c585be63ec69
permissions -rw-r--r--
1.8.69 Some more scripting functionality (for testing)
insilmaril@432
     1
#include "simplescripteditor.h"
insilmaril@432
     2
insilmaril@432
     3
insilmaril@432
     4
#include <QFileDialog>
insilmaril@432
     5
#include <QMessageBox>
insilmaril@432
     6
#include <QTextStream>
insilmaril@432
     7
insilmaril@432
     8
insilmaril@432
     9
extern QString vymName;
insilmaril@432
    10
insilmaril@432
    11
SimpleScriptEditor::SimpleScriptEditor (QWidget *parent):QDialog(parent)
insilmaril@432
    12
{
insilmaril@432
    13
	ui.setupUi (this);
insilmaril@432
    14
insilmaril@432
    15
	connect ( ui.loadButton, SIGNAL (clicked() ), this, SLOT (loadScriptClicked() ));
insilmaril@432
    16
	connect ( ui.saveButton, SIGNAL (clicked() ), this, SLOT (saveScriptClicked() ));
insilmaril@432
    17
	connect ( ui.runButton,  SIGNAL (clicked() ), this, SLOT (runScriptClicked() ));
insilmaril@432
    18
}
insilmaril@432
    19
insilmaril@432
    20
insilmaril@432
    21
void SimpleScriptEditor::saveScript()
insilmaril@432
    22
{
insilmaril@432
    23
	QFile f( filename );
insilmaril@432
    24
	if ( !f.open( QIODevice::WriteOnly ) ) 
insilmaril@432
    25
	{
insilmaril@432
    26
		return;
insilmaril@432
    27
	}
insilmaril@432
    28
insilmaril@432
    29
	QTextStream t( &f );
insilmaril@432
    30
	t << ui.editor->text();
insilmaril@432
    31
	f.close();
insilmaril@432
    32
}
insilmaril@432
    33
insilmaril@432
    34
void SimpleScriptEditor::saveScriptClicked()
insilmaril@432
    35
{
insilmaril@432
    36
	QString fn = QFileDialog::getSaveFileName( 
insilmaril@432
    37
		this, 
insilmaril@432
    38
		QString (vymName + " - " +tr("Save script")),
insilmaril@432
    39
		QString (),
insilmaril@432
    40
		"VYM script (HTML) (*.vys);;All files (*)",
insilmaril@432
    41
		0,
insilmaril@432
    42
		QFileDialog::DontConfirmOverwrite);
insilmaril@432
    43
		
insilmaril@432
    44
    if ( !fn.isEmpty() ) 
insilmaril@432
    45
	{
insilmaril@432
    46
		QFile file (fn);
insilmaril@432
    47
		if (file.exists())
insilmaril@432
    48
		{
insilmaril@432
    49
			QMessageBox mb( vymName,
insilmaril@432
    50
				tr("The file %1\nexists already.\nDo you want to overwrite it?","dialog 'save as'").arg(fn),
insilmaril@432
    51
			QMessageBox::Warning,
insilmaril@432
    52
			QMessageBox::Yes | QMessageBox::Default,
insilmaril@432
    53
			QMessageBox::Cancel | QMessageBox::Escape,
insilmaril@432
    54
			Qt::NoButton );
insilmaril@432
    55
			mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
insilmaril@432
    56
			mb.setButtonText( QMessageBox::No, tr("Cancel"));
insilmaril@432
    57
			switch( mb.exec() ) 
insilmaril@432
    58
			{
insilmaril@432
    59
				case QMessageBox::Yes:
insilmaril@432
    60
					// save 
insilmaril@432
    61
					filename = fn;
insilmaril@432
    62
					saveScript();
insilmaril@432
    63
					return;
insilmaril@432
    64
				case QMessageBox::Cancel:
insilmaril@432
    65
					// do nothing
insilmaril@432
    66
					return;
insilmaril@432
    67
			}
insilmaril@432
    68
		} 
insilmaril@432
    69
		filename=fn;
insilmaril@432
    70
		saveScript();
insilmaril@432
    71
    }
insilmaril@432
    72
}
insilmaril@432
    73
insilmaril@432
    74
void SimpleScriptEditor::loadScriptClicked()
insilmaril@432
    75
{
insilmaril@432
    76
	QFileDialog *fd=new QFileDialog( this);
insilmaril@432
    77
	QStringList types;
insilmaril@432
    78
	types<< "VYM scripts (*.vys)" <<
insilmaril@432
    79
	        "All         (*)" ;
insilmaril@432
    80
	fd->setFilters (types);
insilmaril@432
    81
	fd->setDirectory (QDir().current());
insilmaril@432
    82
	fd->setCaption (vymName + " - " + tr("Load script"));
insilmaril@432
    83
	fd->show();
insilmaril@432
    84
	QString fn;
insilmaril@432
    85
	if ( fd->exec() == QDialog::Accepted )
insilmaril@432
    86
		fn = fd->selectedFile();
insilmaril@432
    87
insilmaril@432
    88
	if ( !fn.isEmpty() )
insilmaril@432
    89
	{
insilmaril@432
    90
		QFile f( fn );
insilmaril@432
    91
		if ( !f.open( QIODevice::ReadOnly ) )
insilmaril@432
    92
		{
insilmaril@432
    93
			QMessageBox::warning(0, 
insilmaril@432
    94
				tr("Error"),
insilmaril@432
    95
				tr("Couldn't open %1.\n").arg(fn));
insilmaril@432
    96
			return;
insilmaril@432
    97
		}	
insilmaril@432
    98
insilmaril@432
    99
		QTextStream ts( &f );
insilmaril@432
   100
		ui.editor->setText( ts.read() );
insilmaril@432
   101
		f.close();
insilmaril@432
   102
	}
insilmaril@432
   103
insilmaril@432
   104
}
insilmaril@432
   105
insilmaril@432
   106
void SimpleScriptEditor::runScriptClicked()
insilmaril@432
   107
{
insilmaril@432
   108
	emit runScript (ui.editor->text() );
insilmaril@432
   109
}