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