Showing the changes to interface/src/ui/ScriptEditorWidget.cpp (1 of 15 files). Show all
Unified · Split
| @@ -13,6 +13,7 @@ · 12 unmodified lines | ||
| 13 | 13 | #include <QFrame> |
| 14 | 14 | #include <QLayoutItem> |
| 15 | 15 | #include <QMainWindow> |
| 16 | +#include <QMessageBox> | |
| 16 | 17 | #include <QPalette> |
| 17 | 18 | #include <QScrollBar> |
| 18 | 19 | #include <QSizePolicy> |
| @@ -30,13 +31,126 @@ ScriptEditorWidget::ScriptEditorWidget() : · 11 unmodified lines | ||
| 30 | 31 | { |
| 31 | 32 | ui->setupUi(this); |
| 32 | 33 | |
| 34 | + scriptEngine = NULL; | |
| 35 | + | |
| 36 | + connect(ui->scriptEdit->document(), SIGNAL(modificationChanged(bool)), this, SIGNAL(scriptModified())); | |
| 37 | + connect(ui->scriptEdit->document(), SIGNAL(contentsChanged()), this, SLOT(onScriptModified())); | |
| 38 | + | |
| 33 | 39 | // remove the title bar (see the Qt docs on setTitleBarWidget) |
| 34 | 40 | setTitleBarWidget(new QWidget()); |
| 35 | 41 | QFontMetrics fm(this->ui->scriptEdit->font()); |
| 36 | 42 | this->ui->scriptEdit->setTabStopWidth(fm.width('0') * 4); |
| 37 | 43 | ScriptHighlighting* highlighting = new ScriptHighlighting(this->ui->scriptEdit->document()); |
| 44 | + QTimer::singleShot(0, this->ui->scriptEdit, SLOT(setFocus())); | |
| 38 | 45 | } |
| 39 | 46 | |
| 40 | 47 | ScriptEditorWidget::~ScriptEditorWidget() { |
| 41 | 48 | delete ui; |
| 49 | +} | |
| 50 | + | |
| 51 | +void ScriptEditorWidget::onScriptModified() { | |
| 52 | + if(ui->onTheFlyCheckBox->isChecked() && isRunning()) { | |
| 53 | + setRunning(false); | |
| 54 | + setRunning(true); | |
| 55 | + } | |
| 56 | +} | |
| 57 | + | |
| 58 | +bool ScriptEditorWidget::isModified() { | |
| 59 | + return ui->scriptEdit->document()->isModified(); | |
| 60 | +} | |
| 61 | + | |
| 62 | +bool ScriptEditorWidget::isRunning() { | |
| 63 | + return (scriptEngine != NULL) ? scriptEngine->isRunning() : false; | |
| 64 | +} | |
| 65 | + | |
| 66 | +bool ScriptEditorWidget::setRunning(bool run) { | |
| 67 | + if (run && !save()) { | |
| 68 | + return false; | |
| 69 | + } | |
| 70 | + // Clean-up old connections. | |
| 71 | + disconnect(this, SLOT(onScriptError(const QString&))); | |
| 72 | + disconnect(this, SLOT(onScriptPrint(const QString&))); | |
| 73 | + | |
| 74 | + if (run) { | |
| 75 | + scriptEngine = Application::getInstance()->loadScript(this->currentScript, false); | |
| 76 | + connect(scriptEngine, SIGNAL(runningStateChanged()), this, SIGNAL(runningStateChanged())); | |
| 77 | + | |
| 78 | + // Make new connections. | |
| 79 | + connect(scriptEngine, SIGNAL(errorMessage(const QString&)), this, SLOT(onScriptError(const QString&))); | |
| 80 | + connect(scriptEngine, SIGNAL(printedMessage(const QString&)), this, SLOT(onScriptPrint(const QString&))); | |
| 81 | + } else { | |
| 82 | + Application::getInstance()->stopScript(this->currentScript); | |
| 83 | + scriptEngine = NULL; | |
| 84 | + } | |
| 85 | + return true; | |
| 86 | +} | |
| 87 | + | |
| 88 | +bool ScriptEditorWidget::saveFile(const QString &scriptPath) { | |
| 89 | + QFile file(scriptPath); | |
| 90 | + if (!file.open(QFile::WriteOnly | QFile::Text)) { | |
| 91 | + QMessageBox::warning(this, tr("Interface"), tr("Cannot write script %1:\n%2.").arg(scriptPath).arg(file.errorString())); | |
| 92 | + return false; | |
| 93 | + } | |
| 94 | + | |
| 95 | + QTextStream out(&file); | |
| 96 | + out << ui->scriptEdit->toPlainText(); | |
| 97 | + | |
| 98 | + setScriptFile(scriptPath); | |
| 99 | + return true; | |
| 100 | +} | |
| 101 | + | |
| 102 | +void ScriptEditorWidget::loadFile(const QString &scriptPath) { | |
| 103 | + QFile file(scriptPath); | |
| 104 | + if (!file.open(QFile::ReadOnly | QFile::Text)) { | |
| 105 | + QMessageBox::warning(this, tr("Interface"), tr("Cannot read script %1:\n%2.").arg(scriptPath).arg(file.errorString())); | |
| 106 | + return; | |
| 107 | + } | |
| 108 | + | |
| 109 | + QTextStream in(&file); | |
| 110 | + ui->scriptEdit->setPlainText(in.readAll()); | |
| 111 | + | |
| 112 | + setScriptFile(scriptPath); | |
| 113 | + | |
| 114 | + disconnect(this, SLOT(onScriptError(const QString&))); | |
| 115 | + disconnect(this, SLOT(onScriptPrint(const QString&))); | |
| 116 | + | |
| 117 | + scriptEngine = Application::getInstance()->getScriptEngine(scriptPath); | |
| 118 | + if (scriptEngine != NULL) { | |
| 119 | + connect(scriptEngine, SIGNAL(runningStateChanged()), this, SIGNAL(runningStateChanged())); | |
| 120 | + connect(scriptEngine, SIGNAL(errorMessage(const QString&)), this, SLOT(onScriptError(const QString&))); | |
| 121 | + connect(scriptEngine, SIGNAL(printedMessage(const QString&)), this, SLOT(onScriptPrint(const QString&))); | |
| 122 | + } | |
| 123 | +} | |
| 124 | + | |
| 125 | +bool ScriptEditorWidget::save() { | |
| 126 | + return currentScript.isEmpty() ? saveAs() : saveFile(currentScript); | |
| 127 | +} | |
| 128 | + | |
| 129 | +bool ScriptEditorWidget::saveAs() { | |
| 130 | + QString fileName = QFileDialog::getSaveFileName(this, tr("Save script"), QString(), tr("Javascript (*.js)")); | |
| 131 | + return !fileName.isEmpty() ? saveFile(fileName) : false; | |
| 132 | +} | |
| 133 | + | |
| 134 | +void ScriptEditorWidget::setScriptFile(const QString& scriptPath) { | |
| 135 | + currentScript = scriptPath; | |
| 136 | + ui->scriptEdit->document()->setModified(false); | |
| 137 | + setWindowModified(false); | |
| 138 | + | |
| 139 | + emit scriptnameChanged(); | |
| 140 | +} | |
| 141 | + | |
| 142 | +bool ScriptEditorWidget::questionSave() { | |
| 143 | + if (ui->scriptEdit->document()->isModified()) { | |
| 144 | + QMessageBox::StandardButton button = QMessageBox::warning(this, tr("Interface"), tr("The script has been modified.\nDo you want to save your changes?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save); | |
| 145 | + return button == QMessageBox::Save ? save() : (button == QMessageBox::Cancel ? false : true); | |
| 146 | + } | |
| 147 | + return true; | |
| 148 | +} | |
| 149 | + | |
| 150 | +void ScriptEditorWidget::onScriptError(const QString& message) { | |
| 151 | + ui->debugText->appendPlainText("ERROR: "+ message); | |
| 152 | +} | |
| 153 | + | |
| 154 | +void ScriptEditorWidget::onScriptPrint(const QString& message) { | |
| 155 | + ui->debugText->appendPlainText("> "+message); | |
| 42 | 156 | } |