simplescripteditor.cpp
author insilmaril
Wed Mar 21 11:51:38 2007 +0000 (2007-03-21)
changeset 435 bd71dfb2292c
parent 432 f867269ab8a1
child 438 eae9abe70d7c
permissions -rw-r--r--
Slightly improved scripting abilities
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@434
    18
insilmaril@434
    19
insilmaril@434
    20
	// Initialize Editor
insilmaril@434
    21
	QFont font;
insilmaril@434
    22
	font.setFamily("Courier");
insilmaril@434
    23
	font.setFixedPitch(true);
insilmaril@434
    24
	font.setPointSize(10);
insilmaril@434
    25
	ui.editor->setFont(font);
insilmaril@434
    26
insilmaril@434
    27
	highlighter = new Highlighter(ui.editor->document());
insilmaril@432
    28
}
insilmaril@432
    29
insilmaril@432
    30
void SimpleScriptEditor::saveScript()
insilmaril@432
    31
{
insilmaril@432
    32
	QFile f( filename );
insilmaril@432
    33
	if ( !f.open( QIODevice::WriteOnly ) ) 
insilmaril@432
    34
	{
insilmaril@432
    35
		return;
insilmaril@432
    36
	}
insilmaril@432
    37
insilmaril@432
    38
	QTextStream t( &f );
insilmaril@432
    39
	t << ui.editor->text();
insilmaril@432
    40
	f.close();
insilmaril@432
    41
}
insilmaril@432
    42
insilmaril@434
    43
void SimpleScriptEditor::setScript(const QString &s)
insilmaril@434
    44
{
insilmaril@434
    45
	ui.editor->setText(s);
insilmaril@434
    46
}
insilmaril@434
    47
insilmaril@432
    48
void SimpleScriptEditor::saveScriptClicked()
insilmaril@432
    49
{
insilmaril@432
    50
	QString fn = QFileDialog::getSaveFileName( 
insilmaril@432
    51
		this, 
insilmaril@432
    52
		QString (vymName + " - " +tr("Save script")),
insilmaril@432
    53
		QString (),
insilmaril@432
    54
		"VYM script (HTML) (*.vys);;All files (*)",
insilmaril@432
    55
		0,
insilmaril@432
    56
		QFileDialog::DontConfirmOverwrite);
insilmaril@432
    57
		
insilmaril@432
    58
    if ( !fn.isEmpty() ) 
insilmaril@432
    59
	{
insilmaril@432
    60
		QFile file (fn);
insilmaril@432
    61
		if (file.exists())
insilmaril@432
    62
		{
insilmaril@432
    63
			QMessageBox mb( vymName,
insilmaril@432
    64
				tr("The file %1\nexists already.\nDo you want to overwrite it?","dialog 'save as'").arg(fn),
insilmaril@432
    65
			QMessageBox::Warning,
insilmaril@432
    66
			QMessageBox::Yes | QMessageBox::Default,
insilmaril@432
    67
			QMessageBox::Cancel | QMessageBox::Escape,
insilmaril@432
    68
			Qt::NoButton );
insilmaril@432
    69
			mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
insilmaril@432
    70
			mb.setButtonText( QMessageBox::No, tr("Cancel"));
insilmaril@432
    71
			switch( mb.exec() ) 
insilmaril@432
    72
			{
insilmaril@432
    73
				case QMessageBox::Yes:
insilmaril@432
    74
					// save 
insilmaril@432
    75
					filename = fn;
insilmaril@432
    76
					saveScript();
insilmaril@432
    77
					return;
insilmaril@432
    78
				case QMessageBox::Cancel:
insilmaril@432
    79
					// do nothing
insilmaril@432
    80
					return;
insilmaril@432
    81
			}
insilmaril@432
    82
		} 
insilmaril@432
    83
		filename=fn;
insilmaril@432
    84
		saveScript();
insilmaril@432
    85
    }
insilmaril@432
    86
}
insilmaril@432
    87
insilmaril@432
    88
void SimpleScriptEditor::loadScriptClicked()
insilmaril@432
    89
{
insilmaril@432
    90
	QFileDialog *fd=new QFileDialog( this);
insilmaril@432
    91
	QStringList types;
insilmaril@432
    92
	types<< "VYM scripts (*.vys)" <<
insilmaril@432
    93
	        "All         (*)" ;
insilmaril@432
    94
	fd->setFilters (types);
insilmaril@432
    95
	fd->setDirectory (QDir().current());
insilmaril@432
    96
	fd->setCaption (vymName + " - " + tr("Load script"));
insilmaril@432
    97
	fd->show();
insilmaril@432
    98
	QString fn;
insilmaril@432
    99
	if ( fd->exec() == QDialog::Accepted )
insilmaril@432
   100
		fn = fd->selectedFile();
insilmaril@432
   101
insilmaril@432
   102
	if ( !fn.isEmpty() )
insilmaril@432
   103
	{
insilmaril@432
   104
		QFile f( fn );
insilmaril@432
   105
		if ( !f.open( QIODevice::ReadOnly ) )
insilmaril@432
   106
		{
insilmaril@432
   107
			QMessageBox::warning(0, 
insilmaril@432
   108
				tr("Error"),
insilmaril@432
   109
				tr("Couldn't open %1.\n").arg(fn));
insilmaril@432
   110
			return;
insilmaril@432
   111
		}	
insilmaril@432
   112
insilmaril@432
   113
		QTextStream ts( &f );
insilmaril@432
   114
		ui.editor->setText( ts.read() );
insilmaril@432
   115
		f.close();
insilmaril@432
   116
	}
insilmaril@432
   117
}
insilmaril@432
   118
insilmaril@432
   119
void SimpleScriptEditor::runScriptClicked()
insilmaril@432
   120
{
insilmaril@432
   121
	emit runScript (ui.editor->text() );
insilmaril@432
   122
}