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