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