insilmaril@432: #include "simplescripteditor.h" insilmaril@432: insilmaril@432: insilmaril@432: #include insilmaril@432: #include insilmaril@432: #include insilmaril@432: insilmaril@432: insilmaril@432: extern QString vymName; insilmaril@432: insilmaril@432: SimpleScriptEditor::SimpleScriptEditor (QWidget *parent):QDialog(parent) insilmaril@432: { insilmaril@432: ui.setupUi (this); insilmaril@432: insilmaril@432: connect ( ui.loadButton, SIGNAL (clicked() ), this, SLOT (loadScriptClicked() )); insilmaril@432: connect ( ui.saveButton, SIGNAL (clicked() ), this, SLOT (saveScriptClicked() )); insilmaril@432: connect ( ui.runButton, SIGNAL (clicked() ), this, SLOT (runScriptClicked() )); insilmaril@432: } insilmaril@432: insilmaril@432: insilmaril@432: void SimpleScriptEditor::saveScript() insilmaril@432: { insilmaril@432: QFile f( filename ); insilmaril@432: if ( !f.open( QIODevice::WriteOnly ) ) insilmaril@432: { insilmaril@432: return; insilmaril@432: } insilmaril@432: insilmaril@432: QTextStream t( &f ); insilmaril@432: t << ui.editor->text(); insilmaril@432: f.close(); insilmaril@432: } insilmaril@432: insilmaril@432: void SimpleScriptEditor::saveScriptClicked() insilmaril@432: { insilmaril@432: QString fn = QFileDialog::getSaveFileName( insilmaril@432: this, insilmaril@432: QString (vymName + " - " +tr("Save script")), insilmaril@432: QString (), insilmaril@432: "VYM script (HTML) (*.vys);;All files (*)", insilmaril@432: 0, insilmaril@432: QFileDialog::DontConfirmOverwrite); insilmaril@432: insilmaril@432: if ( !fn.isEmpty() ) insilmaril@432: { insilmaril@432: QFile file (fn); insilmaril@432: if (file.exists()) insilmaril@432: { insilmaril@432: QMessageBox mb( vymName, insilmaril@432: tr("The file %1\nexists already.\nDo you want to overwrite it?","dialog 'save as'").arg(fn), insilmaril@432: QMessageBox::Warning, insilmaril@432: QMessageBox::Yes | QMessageBox::Default, insilmaril@432: QMessageBox::Cancel | QMessageBox::Escape, insilmaril@432: Qt::NoButton ); insilmaril@432: mb.setButtonText( QMessageBox::Yes, tr("Overwrite") ); insilmaril@432: mb.setButtonText( QMessageBox::No, tr("Cancel")); insilmaril@432: switch( mb.exec() ) insilmaril@432: { insilmaril@432: case QMessageBox::Yes: insilmaril@432: // save insilmaril@432: filename = fn; insilmaril@432: saveScript(); insilmaril@432: return; insilmaril@432: case QMessageBox::Cancel: insilmaril@432: // do nothing insilmaril@432: return; insilmaril@432: } insilmaril@432: } insilmaril@432: filename=fn; insilmaril@432: saveScript(); insilmaril@432: } insilmaril@432: } insilmaril@432: insilmaril@432: void SimpleScriptEditor::loadScriptClicked() insilmaril@432: { insilmaril@432: QFileDialog *fd=new QFileDialog( this); insilmaril@432: QStringList types; insilmaril@432: types<< "VYM scripts (*.vys)" << insilmaril@432: "All (*)" ; insilmaril@432: fd->setFilters (types); insilmaril@432: fd->setDirectory (QDir().current()); insilmaril@432: fd->setCaption (vymName + " - " + tr("Load script")); insilmaril@432: fd->show(); insilmaril@432: QString fn; insilmaril@432: if ( fd->exec() == QDialog::Accepted ) insilmaril@432: fn = fd->selectedFile(); insilmaril@432: insilmaril@432: if ( !fn.isEmpty() ) insilmaril@432: { insilmaril@432: QFile f( fn ); insilmaril@432: if ( !f.open( QIODevice::ReadOnly ) ) insilmaril@432: { insilmaril@432: QMessageBox::warning(0, insilmaril@432: tr("Error"), insilmaril@432: tr("Couldn't open %1.\n").arg(fn)); insilmaril@432: return; insilmaril@432: } insilmaril@432: insilmaril@432: QTextStream ts( &f ); insilmaril@432: ui.editor->setText( ts.read() ); insilmaril@432: f.close(); insilmaril@432: } insilmaril@432: insilmaril@432: } insilmaril@432: insilmaril@432: void SimpleScriptEditor::runScriptClicked() insilmaril@432: { insilmaril@432: emit runScript (ui.editor->text() ); insilmaril@432: }