From 4bdc945f94bf0d2c8787c6397df68ac4221e114d Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 29 Apr 2014 20:12:16 +0200 Subject: [PATCH 1/9] Changed font sizes in script-editor to pixels, it should he readable in both Windows and OSX now --- interface/ui/scriptEditorWidget.ui | 52 ++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/interface/ui/scriptEditorWidget.ui b/interface/ui/scriptEditorWidget.ui index 88761c91c5..363f99b635 100644 --- a/interface/ui/scriptEditorWidget.ui +++ b/interface/ui/scriptEditorWidget.ui @@ -18,8 +18,8 @@ - 541 - 238 + 690 + 328 @@ -35,20 +35,11 @@ Edit Script - + 0 - - 0 - - - 0 - - - 0 - - + 0 @@ -56,14 +47,17 @@ Courier - 9 + -1 50 false false - font: 9pt "Courier"; + font: 16px "Courier"; + + + false @@ -86,6 +80,9 @@ 0 + + font: 13px "Helvetica","Arial","sans-serif"; + Debug Log: @@ -93,8 +90,20 @@ + + + Helvetica,Arial,sans-serif + -1 + 50 + false + false + + + + font: 13px "Helvetica","Arial","sans-serif"; + - Run on the fly (Careful: Any valid change made to the code will run immediately) + Run on the fly (Careful: Any valid change made to the code will run immediately) @@ -115,12 +124,21 @@ + + + 0 + 0 + + - font: 8pt "Courier"; + font: 15px "Courier"; true + + + From df14e1e6e4c7402cb9f4ae5009b863673b0796fe Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 29 Apr 2014 21:51:20 +0200 Subject: [PATCH 2/9] Use same load/save dialog behavior as in main application for ScriptEditor, this will keep track of the last directory that was used to load a script. --- interface/src/Application.cpp | 23 ++++++++++++++++------- interface/src/Application.h | 3 +++ interface/src/ui/ScriptEditorWidget.cpp | 11 +++++++++-- interface/src/ui/ScriptEditorWindow.cpp | 5 ++++- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 558ba31d80..228e40fdce 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3438,9 +3438,9 @@ ScriptEngine* Application::loadScript(const QString& scriptName, bool loadScript return scriptEngine; } -void Application::loadDialog() { +QString Application::getPreviousScriptLocation() { QString suggestedName; - + if (_previousScriptLocation.isEmpty()) { QString desktopLocation = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); // Temporary fix to Qt bug: http://stackoverflow.com/questions/16194475 @@ -3450,14 +3450,23 @@ void Application::loadDialog() { } else { suggestedName = _previousScriptLocation; } + + return suggestedName; +} - QString fileNameString = QFileDialog::getOpenFileName(_glWidget, tr("Open Script"), suggestedName, +void Application::setPreviousScriptLocation(QString previousScriptLocation) { + _previousScriptLocation = previousScriptLocation; + QMutexLocker locker(&_settingsMutex); + _settings->setValue("LastScriptLocation", _previousScriptLocation); +} + +void Application::loadDialog() { + + QString fileNameString = QFileDialog::getOpenFileName(_glWidget, tr("Open Script"), + getPreviousScriptLocation(), tr("JavaScript Files (*.js)")); if (!fileNameString.isEmpty()) { - _previousScriptLocation = fileNameString; - QMutexLocker locker(&_settingsMutex); - _settings->setValue("LastScriptLocation", _previousScriptLocation); - + setPreviousScriptLocation(fileNameString); loadScript(fileNameString); } } diff --git a/interface/src/Application.h b/interface/src/Application.h index 325770a8df..8929c06134 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -124,6 +124,9 @@ public: void restoreSizeAndPosition(); ScriptEngine* loadScript(const QString& fileNameString, bool loadScriptFromEditor = false); void loadScripts(); + QString getPreviousScriptLocation(); + void setPreviousScriptLocation(QString previousScriptLocation); + void storeSizeAndPosition(); void clearScriptsBeforeRunning(); void saveScripts(); diff --git a/interface/src/ui/ScriptEditorWidget.cpp b/interface/src/ui/ScriptEditorWidget.cpp index 3f9b0137ef..95d60e3b4f 100644 --- a/interface/src/ui/ScriptEditorWidget.cpp +++ b/interface/src/ui/ScriptEditorWidget.cpp @@ -128,8 +128,15 @@ bool ScriptEditorWidget::save() { } bool ScriptEditorWidget::saveAs() { - QString fileName = QFileDialog::getSaveFileName(this, tr("Save script"), QString(), tr("Javascript (*.js)")); - return !fileName.isEmpty() ? saveFile(fileName) : false; + QString fileName = QFileDialog::getSaveFileName(this, tr("Save script"), + Application::getInstance()->getPreviousScriptLocation(), + tr("JavaScript Files (*.js)")); + if (!fileName.isEmpty()) { + Application::getInstance()->setPreviousScriptLocation(fileName); + return saveFile(fileName); + } else { + return false; + } } void ScriptEditorWidget::setScriptFile(const QString& scriptPath) { diff --git a/interface/src/ui/ScriptEditorWindow.cpp b/interface/src/ui/ScriptEditorWindow.cpp index 0c34959353..d118f30d0b 100644 --- a/interface/src/ui/ScriptEditorWindow.cpp +++ b/interface/src/ui/ScriptEditorWindow.cpp @@ -73,8 +73,11 @@ void ScriptEditorWindow::loadScriptMenu(const QString& scriptName) { } void ScriptEditorWindow::loadScriptClicked() { - QString scriptName = QFileDialog::getOpenFileName(this, tr("Interface"), QString(), tr("Javascript (*.js)")); + QString scriptName = QFileDialog::getOpenFileName(this, tr("Interface"), + Application::getInstance()->getPreviousScriptLocation(), + tr("JavaScript Files (*.js)")); if (!scriptName.isEmpty()) { + Application::getInstance()->setPreviousScriptLocation(scriptName); addScriptEditorWidget("loading...")->loadFile(scriptName); updateButtons(); } From fc2ed9f9fb5a67da4b1b55d0c0d35a76c9d07f8f Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 29 Apr 2014 21:51:20 +0200 Subject: [PATCH 3/9] Use same load/save dialog behavior as in main application for ScriptEditor, this will keep track of the last directory that was used to load a script. --- interface/src/Application.cpp | 21 ++++++++++++++------- interface/src/Application.h | 3 +++ interface/src/ui/ScriptEditorWidget.cpp | 11 +++++++++-- interface/src/ui/ScriptEditorWindow.cpp | 5 ++++- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 558ba31d80..72cf2c5e51 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3438,9 +3438,8 @@ ScriptEngine* Application::loadScript(const QString& scriptName, bool loadScript return scriptEngine; } -void Application::loadDialog() { +QString Application::getPreviousScriptLocation() { QString suggestedName; - if (_previousScriptLocation.isEmpty()) { QString desktopLocation = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); // Temporary fix to Qt bug: http://stackoverflow.com/questions/16194475 @@ -3450,14 +3449,22 @@ void Application::loadDialog() { } else { suggestedName = _previousScriptLocation; } + return suggestedName; +} - QString fileNameString = QFileDialog::getOpenFileName(_glWidget, tr("Open Script"), suggestedName, +void Application::setPreviousScriptLocation(QString previousScriptLocation) { + _previousScriptLocation = previousScriptLocation; + QMutexLocker locker(&_settingsMutex); + _settings->setValue("LastScriptLocation", _previousScriptLocation); +} + +void Application::loadDialog() { + + QString fileNameString = QFileDialog::getOpenFileName(_glWidget, tr("Open Script"), + getPreviousScriptLocation(), tr("JavaScript Files (*.js)")); if (!fileNameString.isEmpty()) { - _previousScriptLocation = fileNameString; - QMutexLocker locker(&_settingsMutex); - _settings->setValue("LastScriptLocation", _previousScriptLocation); - + setPreviousScriptLocation(fileNameString); loadScript(fileNameString); } } diff --git a/interface/src/Application.h b/interface/src/Application.h index 325770a8df..9b460f846a 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -124,6 +124,9 @@ public: void restoreSizeAndPosition(); ScriptEngine* loadScript(const QString& fileNameString, bool loadScriptFromEditor = false); void loadScripts(); + QString getPreviousScriptLocation(); + void setPreviousScriptLocation(QString previousScriptLocation); + void storeSizeAndPosition(); void clearScriptsBeforeRunning(); void saveScripts(); diff --git a/interface/src/ui/ScriptEditorWidget.cpp b/interface/src/ui/ScriptEditorWidget.cpp index 3f9b0137ef..95d60e3b4f 100644 --- a/interface/src/ui/ScriptEditorWidget.cpp +++ b/interface/src/ui/ScriptEditorWidget.cpp @@ -128,8 +128,15 @@ bool ScriptEditorWidget::save() { } bool ScriptEditorWidget::saveAs() { - QString fileName = QFileDialog::getSaveFileName(this, tr("Save script"), QString(), tr("Javascript (*.js)")); - return !fileName.isEmpty() ? saveFile(fileName) : false; + QString fileName = QFileDialog::getSaveFileName(this, tr("Save script"), + Application::getInstance()->getPreviousScriptLocation(), + tr("JavaScript Files (*.js)")); + if (!fileName.isEmpty()) { + Application::getInstance()->setPreviousScriptLocation(fileName); + return saveFile(fileName); + } else { + return false; + } } void ScriptEditorWidget::setScriptFile(const QString& scriptPath) { diff --git a/interface/src/ui/ScriptEditorWindow.cpp b/interface/src/ui/ScriptEditorWindow.cpp index 0c34959353..d118f30d0b 100644 --- a/interface/src/ui/ScriptEditorWindow.cpp +++ b/interface/src/ui/ScriptEditorWindow.cpp @@ -73,8 +73,11 @@ void ScriptEditorWindow::loadScriptMenu(const QString& scriptName) { } void ScriptEditorWindow::loadScriptClicked() { - QString scriptName = QFileDialog::getOpenFileName(this, tr("Interface"), QString(), tr("Javascript (*.js)")); + QString scriptName = QFileDialog::getOpenFileName(this, tr("Interface"), + Application::getInstance()->getPreviousScriptLocation(), + tr("JavaScript Files (*.js)")); if (!scriptName.isEmpty()) { + Application::getInstance()->setPreviousScriptLocation(scriptName); addScriptEditorWidget("loading...")->loadFile(scriptName); updateButtons(); } From d93f91a628b03911513f379d52bc6f19487b7e8b Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 29 Apr 2014 22:17:45 +0200 Subject: [PATCH 4/9] Resized ScriptEditorWindow --- interface/ui/scriptEditorWindow.ui | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interface/ui/scriptEditorWindow.ui b/interface/ui/scriptEditorWindow.ui index 9e1b08de3e..f5b0d84235 100644 --- a/interface/ui/scriptEditorWindow.ui +++ b/interface/ui/scriptEditorWindow.ui @@ -9,8 +9,8 @@ 0 0 - 706 - 682 + 780 + 717 From 5ea512f65aaa13ae3175d08d30aecaa4db65b578 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 29 Apr 2014 23:53:46 +0200 Subject: [PATCH 5/9] ScriptEditorWindow not modal, but yet always on foreground --- interface/src/ui/ScriptEditorWindow.cpp | 1 + interface/ui/scriptEditorWindow.ui | 13 ++----------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/interface/src/ui/ScriptEditorWindow.cpp b/interface/src/ui/ScriptEditorWindow.cpp index d118f30d0b..2d52165612 100644 --- a/interface/src/ui/ScriptEditorWindow.cpp +++ b/interface/src/ui/ScriptEditorWindow.cpp @@ -34,6 +34,7 @@ ScriptEditorWindow::ScriptEditorWindow() : _saveMenu(new QMenu) { _ScriptEditorWindowUI->setupUi(this); + this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint); show(); addScriptEditorWidget("New script"); connect(_loadMenu, SIGNAL(aboutToShow()), this, SLOT(loadMenuAboutToShow())); diff --git a/interface/ui/scriptEditorWindow.ui b/interface/ui/scriptEditorWindow.ui index f5b0d84235..9103fc1f57 100644 --- a/interface/ui/scriptEditorWindow.ui +++ b/interface/ui/scriptEditorWindow.ui @@ -3,7 +3,7 @@ ScriptEditorWindow - Qt::WindowModal + Qt::NonModal @@ -29,16 +29,7 @@ 0 - - 0 - - - 0 - - - 0 - - + 0 From 87260a4c5813ada502fc95d57d7178ff9e45e659 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Wed, 30 Apr 2014 00:58:22 +0200 Subject: [PATCH 6/9] ability to load a script thats running from an external link into the ScriptEditor , followed by a save dialog --- interface/src/ui/ScriptEditorWidget.cpp | 41 +++++++++++++++++-------- interface/src/ui/ScriptEditorWindow.cpp | 6 ++++ interface/src/ui/ScriptEditorWindow.h | 3 ++ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/interface/src/ui/ScriptEditorWidget.cpp b/interface/src/ui/ScriptEditorWidget.cpp index 95d60e3b4f..08a68b1d8c 100644 --- a/interface/src/ui/ScriptEditorWidget.cpp +++ b/interface/src/ui/ScriptEditorWidget.cpp @@ -101,21 +101,36 @@ bool ScriptEditorWidget::saveFile(const QString &scriptPath) { } void ScriptEditorWidget::loadFile(const QString& scriptPath) { - QFile file(scriptPath); - if (!file.open(QFile::ReadOnly | QFile::Text)) { - QMessageBox::warning(this, tr("Interface"), tr("Cannot read script %1:\n%2.").arg(scriptPath).arg(file.errorString())); - return; + QUrl url(scriptPath); + + // if the scheme length is one or lower, maybe they typed in a file, let's try + const int WINDOWS_DRIVE_LETTER_SIZE = 1; + if (url.scheme().size() <= WINDOWS_DRIVE_LETTER_SIZE) { + QFile file(scriptPath); + if (!file.open(QFile::ReadOnly | QFile::Text)) { + QMessageBox::warning(this, tr("Interface"), tr("Cannot read script %1:\n%2.").arg(scriptPath).arg(file.errorString())); + return; + } + QTextStream in(&file); + _scriptEditorWidgetUI->scriptEdit->setPlainText(in.readAll()); + setScriptFile(scriptPath); + + disconnect(this, SLOT(onScriptError(const QString&))); + disconnect(this, SLOT(onScriptPrint(const QString&))); + } else { + QNetworkAccessManager* networkManager = new QNetworkAccessManager(this); + QNetworkReply* reply = networkManager->get(QNetworkRequest(url)); + qDebug() << "Downloading included script at" << scriptPath; + QEventLoop loop; + QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); + loop.exec(); + _scriptEditorWidgetUI->scriptEdit->setPlainText(reply->readAll()); + if (!saveAs()) { + emit static_cast(this->parent()->parent()->parent())->terminateCurrentTab(); + } } - QTextStream in(&file); - _scriptEditorWidgetUI->scriptEdit->setPlainText(in.readAll()); - - setScriptFile(scriptPath); - - disconnect(this, SLOT(onScriptError(const QString&))); - disconnect(this, SLOT(onScriptPrint(const QString&))); - - _scriptEngine = Application::getInstance()->getScriptEngine(scriptPath); + _scriptEngine = Application::getInstance()->getScriptEngine(_currentScript); if (_scriptEngine != NULL) { connect(_scriptEngine, SIGNAL(runningStateChanged()), this, SIGNAL(runningStateChanged())); connect(_scriptEngine, SIGNAL(errorMessage(const QString&)), this, SLOT(onScriptError(const QString&))); diff --git a/interface/src/ui/ScriptEditorWindow.cpp b/interface/src/ui/ScriptEditorWindow.cpp index 2d52165612..9f90606f5b 100644 --- a/interface/src/ui/ScriptEditorWindow.cpp +++ b/interface/src/ui/ScriptEditorWindow.cpp @@ -202,3 +202,9 @@ void ScriptEditorWindow::updateScriptNameOrStatus() { } } } + +void ScriptEditorWindow::terminateCurrentTab() { + if (_ScriptEditorWindowUI->tabWidget->currentIndex() != -1) { + _ScriptEditorWindowUI->tabWidget->removeTab(_ScriptEditorWindowUI->tabWidget->currentIndex()); + } +} diff --git a/interface/src/ui/ScriptEditorWindow.h b/interface/src/ui/ScriptEditorWindow.h index 0bf5015ccf..c934e44073 100644 --- a/interface/src/ui/ScriptEditorWindow.h +++ b/interface/src/ui/ScriptEditorWindow.h @@ -37,6 +37,9 @@ private: void setRunningState(bool run); void setScriptName(const QString& scriptName); +public slots: + void terminateCurrentTab(); + private slots: void loadScriptMenu(const QString& scriptName); void loadScriptClicked(); From c20381a0eafb8ead7797a1e828774afffce2643e Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Wed, 30 Apr 2014 08:24:04 +0200 Subject: [PATCH 7/9] EditorWindow on-top behavior fixed --- interface/src/ui/ScriptEditorWidget.cpp | 2 +- interface/src/ui/ScriptEditorWindow.cpp | 3 ++- interface/src/ui/ScriptEditorWindow.h | 5 ++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/interface/src/ui/ScriptEditorWidget.cpp b/interface/src/ui/ScriptEditorWidget.cpp index 08a68b1d8c..33e014ac07 100644 --- a/interface/src/ui/ScriptEditorWidget.cpp +++ b/interface/src/ui/ScriptEditorWidget.cpp @@ -126,7 +126,7 @@ void ScriptEditorWidget::loadFile(const QString& scriptPath) { loop.exec(); _scriptEditorWidgetUI->scriptEdit->setPlainText(reply->readAll()); if (!saveAs()) { - emit static_cast(this->parent()->parent()->parent())->terminateCurrentTab(); + static_cast(this->parent()->parent()->parent())->terminateCurrentTab(); } } diff --git a/interface/src/ui/ScriptEditorWindow.cpp b/interface/src/ui/ScriptEditorWindow.cpp index 9f90606f5b..ec5070ac20 100644 --- a/interface/src/ui/ScriptEditorWindow.cpp +++ b/interface/src/ui/ScriptEditorWindow.cpp @@ -34,7 +34,7 @@ ScriptEditorWindow::ScriptEditorWindow() : _saveMenu(new QMenu) { _ScriptEditorWindowUI->setupUi(this); - this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint); + this->setWindowFlags(Qt::Tool); show(); addScriptEditorWidget("New script"); connect(_loadMenu, SIGNAL(aboutToShow()), this, SLOT(loadMenuAboutToShow())); @@ -206,5 +206,6 @@ void ScriptEditorWindow::updateScriptNameOrStatus() { void ScriptEditorWindow::terminateCurrentTab() { if (_ScriptEditorWindowUI->tabWidget->currentIndex() != -1) { _ScriptEditorWindowUI->tabWidget->removeTab(_ScriptEditorWindowUI->tabWidget->currentIndex()); + this->raise(); } } diff --git a/interface/src/ui/ScriptEditorWindow.h b/interface/src/ui/ScriptEditorWindow.h index c934e44073..360e902cc2 100644 --- a/interface/src/ui/ScriptEditorWindow.h +++ b/interface/src/ui/ScriptEditorWindow.h @@ -25,6 +25,8 @@ public: ScriptEditorWindow(); ~ScriptEditorWindow(); + void terminateCurrentTab(); + protected: void closeEvent(QCloseEvent* event); @@ -37,9 +39,6 @@ private: void setRunningState(bool run); void setScriptName(const QString& scriptName); -public slots: - void terminateCurrentTab(); - private slots: void loadScriptMenu(const QString& scriptName); void loadScriptClicked(); From cf1baaf6eabaaf314011deea17f24db8ed1ae88c Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Wed, 30 Apr 2014 23:32:41 +0200 Subject: [PATCH 8/9] - Highlighting Fixes ( Single line comments in quoted text don't occur anymore. / Better number recognition) - Line numbers in ScriptEditor --- interface/src/ScriptHighlighting.cpp | 19 +++- interface/src/ScriptHighlighting.h | 1 + interface/src/ui/ScriptEditBox.cpp | 107 ++++++++++++++++++++++ interface/src/ui/ScriptEditBox.h | 38 ++++++++ interface/src/ui/ScriptEditorWidget.h | 1 - interface/src/ui/ScriptLineNumberArea.cpp | 28 ++++++ interface/src/ui/ScriptLineNumberArea.h | 31 +++++++ interface/ui/scriptEditorWidget.ui | 23 ++++- 8 files changed, 240 insertions(+), 8 deletions(-) create mode 100644 interface/src/ui/ScriptEditBox.cpp create mode 100644 interface/src/ui/ScriptEditBox.h create mode 100644 interface/src/ui/ScriptLineNumberArea.cpp create mode 100644 interface/src/ui/ScriptLineNumberArea.h diff --git a/interface/src/ScriptHighlighting.cpp b/interface/src/ScriptHighlighting.cpp index 3ab1394097..3d63ba55a8 100644 --- a/interface/src/ScriptHighlighting.cpp +++ b/interface/src/ScriptHighlighting.cpp @@ -22,6 +22,7 @@ ScriptHighlighting::ScriptHighlighting(QTextDocument* parent) : _numberRegex = QRegExp("[0-9]+(\\.[0-9]+){0,1}"); _singleLineComment = QRegExp("//[^\n]*"); _truefalseRegex = QRegExp("\\b(true|false)\\b"); + _alphacharRegex = QRegExp("[A-Za-z]"); } void ScriptHighlighting::highlightBlock(const QString& text) { @@ -60,7 +61,19 @@ void ScriptHighlighting::formatComments(const QString& text) { int index = _singleLineComment.indexIn(text); while (index >= 0) { int length = _singleLineComment.matchedLength(); - setFormat(index, length, Qt::lightGray); + int quoted_index = _qoutedTextRegex.indexIn(text); + bool valid = true; + while (quoted_index >= 0 && valid) { + int quoted_length = _qoutedTextRegex.matchedLength(); + if (quoted_index <= index && index <= (quoted_index + quoted_length)) { + valid = false; + } + quoted_index = _qoutedTextRegex.indexIn(text, quoted_index + quoted_length); + } + + if (valid) { + setFormat(index, length, Qt::lightGray); + } index = _singleLineComment.indexIn(text, index + length); } } @@ -78,7 +91,9 @@ void ScriptHighlighting::formatNumbers(const QString& text){ int index = _numberRegex.indexIn(text); while (index >= 0) { int length = _numberRegex.matchedLength(); - setFormat(index, length, Qt::green); + if (index == 0 || _alphacharRegex.indexIn(text, index - 1) != (index - 1)) { + setFormat(index, length, Qt::green); + } index = _numberRegex.indexIn(text, index + length); } } diff --git a/interface/src/ScriptHighlighting.h b/interface/src/ScriptHighlighting.h index d86d6f4d77..232d594308 100644 --- a/interface/src/ScriptHighlighting.h +++ b/interface/src/ScriptHighlighting.h @@ -34,6 +34,7 @@ protected: void formatTrueFalse(const QString& text); private: + QRegExp _alphacharRegex; QRegExp _keywordRegex; QRegExp _qoutedTextRegex; QRegExp _multiLineCommentBegin; diff --git a/interface/src/ui/ScriptEditBox.cpp b/interface/src/ui/ScriptEditBox.cpp new file mode 100644 index 0000000000..4c2b6564ba --- /dev/null +++ b/interface/src/ui/ScriptEditBox.cpp @@ -0,0 +1,107 @@ +// +// ScriptEditBox.cpp +// interface/src/ui +// +// Created by Thijs Wenker on 4/30/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "ScriptEditBox.h" +#include "ScriptLineNumberArea.h" +#include "Application.h" + +ScriptEditBox::ScriptEditBox(QWidget* parent) : + QPlainTextEdit(parent) +{ + _scriptLineNumberArea = new ScriptLineNumberArea(this); + + connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int))); + connect(this, SIGNAL(updateRequest(QRect, int)), this, SLOT(updateLineNumberArea(QRect, int))); + connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); + + updateLineNumberAreaWidth(0); + highlightCurrentLine(); +} + +int ScriptEditBox::lineNumberAreaWidth() { + int digits = 1; + const int SPACER_PIXELS = 3; + const int BASE_TEN = 10; + int max = qMax(1, blockCount()); + while (max >= BASE_TEN) { + max /= BASE_TEN; + ++digits; + } + return SPACER_PIXELS + fontMetrics().width(QLatin1Char('H')) * digits; +} + +void ScriptEditBox::updateLineNumberAreaWidth(int) { + setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); +} + +void ScriptEditBox::updateLineNumberArea(const QRect& rect, int dy) { + if (dy) { + _scriptLineNumberArea->scroll(0, dy); + } else { + _scriptLineNumberArea->update(0, rect.y(), _scriptLineNumberArea->width(), rect.height()); + } + + if (rect.contains(viewport()->rect())) { + updateLineNumberAreaWidth(0); + } +} + +void ScriptEditBox::resizeEvent(QResizeEvent* e) { + QPlainTextEdit::resizeEvent(e); + + QRect cr = contentsRect(); + _scriptLineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); +} + +void ScriptEditBox::highlightCurrentLine() { + QList extraSelections; + + if (!isReadOnly()) { + QTextEdit::ExtraSelection selection; + + QColor lineColor = QColor(Qt::gray).lighter(); + + selection.format.setBackground(lineColor); + selection.format.setProperty(QTextFormat::FullWidthSelection, true); + selection.cursor = textCursor(); + selection.cursor.clearSelection(); + extraSelections.append(selection); + } + + setExtraSelections(extraSelections); +} + +void ScriptEditBox::lineNumberAreaPaintEvent(QPaintEvent* event) +{ + QPainter painter(_scriptLineNumberArea); + painter.fillRect(event->rect(), Qt::lightGray); + QTextBlock block = firstVisibleBlock(); + int blockNumber = block.blockNumber(); + int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top(); + int bottom = top + (int) blockBoundingRect(block).height(); + + while (block.isValid() && top <= event->rect().bottom()) { + if (block.isVisible() && bottom >= event->rect().top()) { + QFont font = painter.font(); + font.setBold(this->textCursor().blockNumber() == block.blockNumber()); + painter.setFont(font); + QString number = QString::number(blockNumber + 1); + painter.setPen(Qt::black); + painter.drawText(0, top, _scriptLineNumberArea->width(), fontMetrics().height(), + Qt::AlignRight, number); + } + + block = block.next(); + top = bottom; + bottom = top + (int) blockBoundingRect(block).height(); + ++blockNumber; + } +} diff --git a/interface/src/ui/ScriptEditBox.h b/interface/src/ui/ScriptEditBox.h new file mode 100644 index 0000000000..ea00ec02b6 --- /dev/null +++ b/interface/src/ui/ScriptEditBox.h @@ -0,0 +1,38 @@ +// +// ScriptEditBox.h +// interface/src/ui +// +// Created by Thijs Wenker on 4/30/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_ScriptEditBox_h +#define hifi_ScriptEditBox_h + +#include + +class ScriptEditBox : public QPlainTextEdit { + Q_OBJECT + +public: + ScriptEditBox(QWidget* parent = NULL); + + void lineNumberAreaPaintEvent(QPaintEvent* event); + int lineNumberAreaWidth(); + +protected: + void resizeEvent(QResizeEvent* event); + +private slots: + void updateLineNumberAreaWidth(int); + void highlightCurrentLine(); + void updateLineNumberArea(const QRect&, int); + +private: + QWidget* _scriptLineNumberArea; +}; + +#endif // hifi_ScriptEditBox_h diff --git a/interface/src/ui/ScriptEditorWidget.h b/interface/src/ui/ScriptEditorWidget.h index 3e50280a62..1a96661cf7 100644 --- a/interface/src/ui/ScriptEditorWidget.h +++ b/interface/src/ui/ScriptEditorWidget.h @@ -13,7 +13,6 @@ #define hifi_ScriptEditorWidget_h #include -#include "ScriptEditorWidget.h" #include "ScriptEngine.h" namespace Ui { diff --git a/interface/src/ui/ScriptLineNumberArea.cpp b/interface/src/ui/ScriptLineNumberArea.cpp new file mode 100644 index 0000000000..9173c72375 --- /dev/null +++ b/interface/src/ui/ScriptLineNumberArea.cpp @@ -0,0 +1,28 @@ +// +// ScriptLineNumberArea.cpp +// interface/src/ui +// +// Created by Thijs Wenker on 4/30/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "ScriptLineNumberArea.h" + +#include "Application.h" + +ScriptLineNumberArea::ScriptLineNumberArea(ScriptEditBox* scriptEditBox) : + QWidget(scriptEditBox) +{ + _scriptEditBox = scriptEditBox; +} + +QSize ScriptLineNumberArea::sizeHint() { + return QSize(_scriptEditBox->lineNumberAreaWidth(), 0); +} + +void ScriptLineNumberArea::paintEvent(QPaintEvent* event) { + _scriptEditBox->lineNumberAreaPaintEvent(event); +} diff --git a/interface/src/ui/ScriptLineNumberArea.h b/interface/src/ui/ScriptLineNumberArea.h new file mode 100644 index 0000000000..75be2048f0 --- /dev/null +++ b/interface/src/ui/ScriptLineNumberArea.h @@ -0,0 +1,31 @@ +// +// ScriptLineNumberArea.h +// interface/src/ui +// +// Created by Thijs Wenker on 4/30/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_ScriptLineNumberArea_h +#define hifi_ScriptLineNumberArea_h + +#include +#include "ScriptEditBox.h" + +class ScriptLineNumberArea : public QWidget { + +public: + ScriptLineNumberArea(ScriptEditBox* scriptEditBox); + QSize sizeHint(); + +protected: + void paintEvent(QPaintEvent* event); + +private: + ScriptEditBox* _scriptEditBox; +}; + +#endif // hifi_ScriptLineNumberArea_h diff --git a/interface/ui/scriptEditorWidget.ui b/interface/ui/scriptEditorWidget.ui index 363f99b635..8aeeff363f 100644 --- a/interface/ui/scriptEditorWidget.ui +++ b/interface/ui/scriptEditorWidget.ui @@ -39,11 +39,20 @@ 0 - + + 0 + + + 0 + + + 0 + + 0 - + Courier @@ -56,9 +65,6 @@ font: 16px "Courier"; - - false - @@ -144,6 +150,13 @@ + + + ScriptEditBox + QTextEdit +
ui/ScriptEditBox.h
+
+
From 966774815643251d941f7f179eb7eecba19a5a5f Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Thu, 1 May 2014 21:07:46 +0200 Subject: [PATCH 9/9] - Style fixes - ScriptHightlighting qouting RegEx fix ( "hello" + " " + "world" // the text between two quote entities isn't red anymore ) --- interface/src/Application.cpp | 2 +- interface/src/Application.h | 2 +- interface/src/ScriptHighlighting.cpp | 2 +- interface/src/ui/ScriptEditBox.cpp | 27 +++++++++--------- interface/src/ui/ScriptEditBox.h | 4 +-- interface/src/ui/ScriptEditorWidget.cpp | 37 +++++++++++++++---------- interface/src/ui/ScriptEditorWindow.cpp | 16 +++++------ 7 files changed, 50 insertions(+), 40 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index aa28d2768c..058408af66 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3461,7 +3461,7 @@ QString Application::getPreviousScriptLocation() { return suggestedName; } -void Application::setPreviousScriptLocation(QString previousScriptLocation) { +void Application::setPreviousScriptLocation(const QString& previousScriptLocation) { _previousScriptLocation = previousScriptLocation; QMutexLocker locker(&_settingsMutex); _settings->setValue("LastScriptLocation", _previousScriptLocation); diff --git a/interface/src/Application.h b/interface/src/Application.h index fb15019c19..9b613f7ed7 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -126,7 +126,7 @@ public: ScriptEngine* loadScript(const QString& fileNameString, bool loadScriptFromEditor = false); void loadScripts(); QString getPreviousScriptLocation(); - void setPreviousScriptLocation(QString previousScriptLocation); + void setPreviousScriptLocation(const QString& previousScriptLocation); void storeSizeAndPosition(); void clearScriptsBeforeRunning(); void saveScripts(); diff --git a/interface/src/ScriptHighlighting.cpp b/interface/src/ScriptHighlighting.cpp index 3d63ba55a8..125ab1e967 100644 --- a/interface/src/ScriptHighlighting.cpp +++ b/interface/src/ScriptHighlighting.cpp @@ -16,7 +16,7 @@ ScriptHighlighting::ScriptHighlighting(QTextDocument* parent) : QSyntaxHighlighter(parent) { _keywordRegex = QRegExp("\\b(break|case|catch|continue|debugger|default|delete|do|else|finally|for|function|if|in|instanceof|new|return|switch|this|throw|try|typeof|var|void|while|with)\\b"); - _qoutedTextRegex = QRegExp("\".*\""); + _qoutedTextRegex = QRegExp("\"[^\"]*(\"){0,1}"); _multiLineCommentBegin = QRegExp("/\\*"); _multiLineCommentEnd = QRegExp("\\*/"); _numberRegex = QRegExp("[0-9]+(\\.[0-9]+){0,1}"); diff --git a/interface/src/ui/ScriptEditBox.cpp b/interface/src/ui/ScriptEditBox.cpp index 4c2b6564ba..acabaa3c8f 100644 --- a/interface/src/ui/ScriptEditBox.cpp +++ b/interface/src/ui/ScriptEditBox.cpp @@ -18,9 +18,9 @@ ScriptEditBox::ScriptEditBox(QWidget* parent) : { _scriptLineNumberArea = new ScriptLineNumberArea(this); - connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int))); - connect(this, SIGNAL(updateRequest(QRect, int)), this, SLOT(updateLineNumberArea(QRect, int))); - connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); + connect(this, &ScriptEditBox::blockCountChanged, this, &ScriptEditBox::updateLineNumberAreaWidth); + connect(this, &ScriptEditBox::updateRequest, this, &ScriptEditBox::updateLineNumberArea); + connect(this, &ScriptEditBox::cursorPositionChanged, this, &ScriptEditBox::highlightCurrentLine); updateLineNumberAreaWidth(0); highlightCurrentLine(); @@ -33,18 +33,18 @@ int ScriptEditBox::lineNumberAreaWidth() { int max = qMax(1, blockCount()); while (max >= BASE_TEN) { max /= BASE_TEN; - ++digits; + digits++; } return SPACER_PIXELS + fontMetrics().width(QLatin1Char('H')) * digits; } -void ScriptEditBox::updateLineNumberAreaWidth(int) { +void ScriptEditBox::updateLineNumberAreaWidth(int blockCount) { setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); } -void ScriptEditBox::updateLineNumberArea(const QRect& rect, int dy) { - if (dy) { - _scriptLineNumberArea->scroll(0, dy); +void ScriptEditBox::updateLineNumberArea(const QRect& rect, int deltaY) { + if (deltaY) { + _scriptLineNumberArea->scroll(0, deltaY); } else { _scriptLineNumberArea->update(0, rect.y(), _scriptLineNumberArea->width(), rect.height()); } @@ -54,11 +54,12 @@ void ScriptEditBox::updateLineNumberArea(const QRect& rect, int dy) { } } -void ScriptEditBox::resizeEvent(QResizeEvent* e) { - QPlainTextEdit::resizeEvent(e); +void ScriptEditBox::resizeEvent(QResizeEvent* event) { + QPlainTextEdit::resizeEvent(event); - QRect cr = contentsRect(); - _scriptLineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); + QRect localContentsRect = contentsRect(); + _scriptLineNumberArea->setGeometry(QRect(localContentsRect.left(), localContentsRect.top(), lineNumberAreaWidth(), + localContentsRect.height())); } void ScriptEditBox::highlightCurrentLine() { @@ -102,6 +103,6 @@ void ScriptEditBox::lineNumberAreaPaintEvent(QPaintEvent* event) block = block.next(); top = bottom; bottom = top + (int) blockBoundingRect(block).height(); - ++blockNumber; + blockNumber++; } } diff --git a/interface/src/ui/ScriptEditBox.h b/interface/src/ui/ScriptEditBox.h index ea00ec02b6..41d881b904 100644 --- a/interface/src/ui/ScriptEditBox.h +++ b/interface/src/ui/ScriptEditBox.h @@ -27,9 +27,9 @@ protected: void resizeEvent(QResizeEvent* event); private slots: - void updateLineNumberAreaWidth(int); + void updateLineNumberAreaWidth(int blockCount); void highlightCurrentLine(); - void updateLineNumberArea(const QRect&, int); + void updateLineNumberArea(const QRect& rect, int deltaY); private: QWidget* _scriptLineNumberArea; diff --git a/interface/src/ui/ScriptEditorWidget.cpp b/interface/src/ui/ScriptEditorWidget.cpp index 33e014ac07..07c6e72226 100644 --- a/interface/src/ui/ScriptEditorWidget.cpp +++ b/interface/src/ui/ScriptEditorWidget.cpp @@ -32,8 +32,10 @@ ScriptEditorWidget::ScriptEditorWidget() : { _scriptEditorWidgetUI->setupUi(this); - connect(_scriptEditorWidgetUI->scriptEdit->document(), SIGNAL(modificationChanged(bool)), this, SIGNAL(scriptModified())); - connect(_scriptEditorWidgetUI->scriptEdit->document(), SIGNAL(contentsChanged()), this, SLOT(onScriptModified())); + connect(_scriptEditorWidgetUI->scriptEdit->document(), &QTextDocument::modificationChanged, this, + &ScriptEditorWidget::scriptModified); + connect(_scriptEditorWidgetUI->scriptEdit->document(), &QTextDocument::contentsChanged, this, + &ScriptEditorWidget::onScriptModified); // remove the title bar (see the Qt docs on setTitleBarWidget) setTitleBarWidget(new QWidget()); @@ -68,16 +70,19 @@ bool ScriptEditorWidget::setRunning(bool run) { return false; } // Clean-up old connections. - disconnect(this, SLOT(onScriptError(const QString&))); - disconnect(this, SLOT(onScriptPrint(const QString&))); + if (_scriptEngine != NULL) { + disconnect(_scriptEngine, &ScriptEngine::runningStateChanged, this, &ScriptEditorWidget::runningStateChanged); + disconnect(_scriptEngine, &ScriptEngine::errorMessage, this, &ScriptEditorWidget::onScriptError); + disconnect(_scriptEngine, &ScriptEngine::printedMessage, this, &ScriptEditorWidget::onScriptPrint); + } if (run) { _scriptEngine = Application::getInstance()->loadScript(_currentScript, true); - connect(_scriptEngine, SIGNAL(runningStateChanged()), this, SIGNAL(runningStateChanged())); + connect(_scriptEngine, &ScriptEngine::runningStateChanged, this, &ScriptEditorWidget::runningStateChanged); // Make new connections. - connect(_scriptEngine, SIGNAL(errorMessage(const QString&)), this, SLOT(onScriptError(const QString&))); - connect(_scriptEngine, SIGNAL(printedMessage(const QString&)), this, SLOT(onScriptPrint(const QString&))); + connect(_scriptEngine, &ScriptEngine::errorMessage, this, &ScriptEditorWidget::onScriptError); + connect(_scriptEngine, &ScriptEngine::printedMessage, this, &ScriptEditorWidget::onScriptPrint); } else { Application::getInstance()->stopScript(_currentScript); _scriptEngine = NULL; @@ -108,21 +113,25 @@ void ScriptEditorWidget::loadFile(const QString& scriptPath) { if (url.scheme().size() <= WINDOWS_DRIVE_LETTER_SIZE) { QFile file(scriptPath); if (!file.open(QFile::ReadOnly | QFile::Text)) { - QMessageBox::warning(this, tr("Interface"), tr("Cannot read script %1:\n%2.").arg(scriptPath).arg(file.errorString())); + QMessageBox::warning(this, tr("Interface"), tr("Cannot read script %1:\n%2.").arg(scriptPath) + .arg(file.errorString())); return; } QTextStream in(&file); _scriptEditorWidgetUI->scriptEdit->setPlainText(in.readAll()); setScriptFile(scriptPath); - disconnect(this, SLOT(onScriptError(const QString&))); - disconnect(this, SLOT(onScriptPrint(const QString&))); + if (_scriptEngine != NULL) { + disconnect(_scriptEngine, &ScriptEngine::runningStateChanged, this, &ScriptEditorWidget::runningStateChanged); + disconnect(_scriptEngine, &ScriptEngine::errorMessage, this, &ScriptEditorWidget::onScriptError); + disconnect(_scriptEngine, &ScriptEngine::printedMessage, this, &ScriptEditorWidget::onScriptPrint); + } } else { QNetworkAccessManager* networkManager = new QNetworkAccessManager(this); QNetworkReply* reply = networkManager->get(QNetworkRequest(url)); qDebug() << "Downloading included script at" << scriptPath; QEventLoop loop; - QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); + QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit); loop.exec(); _scriptEditorWidgetUI->scriptEdit->setPlainText(reply->readAll()); if (!saveAs()) { @@ -132,9 +141,9 @@ void ScriptEditorWidget::loadFile(const QString& scriptPath) { _scriptEngine = Application::getInstance()->getScriptEngine(_currentScript); if (_scriptEngine != NULL) { - connect(_scriptEngine, SIGNAL(runningStateChanged()), this, SIGNAL(runningStateChanged())); - connect(_scriptEngine, SIGNAL(errorMessage(const QString&)), this, SLOT(onScriptError(const QString&))); - connect(_scriptEngine, SIGNAL(printedMessage(const QString&)), this, SLOT(onScriptPrint(const QString&))); + connect(_scriptEngine, &ScriptEngine::runningStateChanged, this, &ScriptEditorWidget::runningStateChanged); + connect(_scriptEngine, &ScriptEngine::errorMessage, this, &ScriptEditorWidget::onScriptError); + connect(_scriptEngine, &ScriptEngine::printedMessage, this, &ScriptEditorWidget::onScriptPrint); } } diff --git a/interface/src/ui/ScriptEditorWindow.cpp b/interface/src/ui/ScriptEditorWindow.cpp index ec5070ac20..41732d18c6 100644 --- a/interface/src/ui/ScriptEditorWindow.cpp +++ b/interface/src/ui/ScriptEditorWindow.cpp @@ -37,17 +37,17 @@ ScriptEditorWindow::ScriptEditorWindow() : this->setWindowFlags(Qt::Tool); show(); addScriptEditorWidget("New script"); - connect(_loadMenu, SIGNAL(aboutToShow()), this, SLOT(loadMenuAboutToShow())); + connect(_loadMenu, &QMenu::aboutToShow, this, &ScriptEditorWindow::loadMenuAboutToShow); _ScriptEditorWindowUI->loadButton->setMenu(_loadMenu); _saveMenu->addAction("Save as..", this, SLOT(saveScriptAsClicked()), Qt::CTRL | Qt::SHIFT | Qt::Key_S); _ScriptEditorWindowUI->saveButton->setMenu(_saveMenu); - connect(new QShortcut(QKeySequence("Ctrl+N"), this), SIGNAL(activated()), this, SLOT(newScriptClicked())); - connect(new QShortcut(QKeySequence("Ctrl+S"), this), SIGNAL(activated()), this, SLOT(saveScriptClicked())); - connect(new QShortcut(QKeySequence("Ctrl+O"), this), SIGNAL(activated()), this, SLOT(loadScriptClicked())); - connect(new QShortcut(QKeySequence("F5"), this), SIGNAL(activated()), this, SLOT(toggleRunScriptClicked())); + connect(new QShortcut(QKeySequence("Ctrl+N"), this), &QShortcut::activated, this, &ScriptEditorWindow::newScriptClicked); + connect(new QShortcut(QKeySequence("Ctrl+S"), this), &QShortcut::activated, this,&ScriptEditorWindow::saveScriptClicked); + connect(new QShortcut(QKeySequence("Ctrl+O"), this), &QShortcut::activated, this, &ScriptEditorWindow::loadScriptClicked); + connect(new QShortcut(QKeySequence("F5"), this), &QShortcut::activated, this, &ScriptEditorWindow::toggleRunScriptClicked); } ScriptEditorWindow::~ScriptEditorWindow() { @@ -130,9 +130,9 @@ void ScriptEditorWindow::saveScriptAsClicked() { ScriptEditorWidget* ScriptEditorWindow::addScriptEditorWidget(QString title) { ScriptEditorWidget* newScriptEditorWidget = new ScriptEditorWidget(); - connect(newScriptEditorWidget, SIGNAL(scriptnameChanged()), this, SLOT(updateScriptNameOrStatus())); - connect(newScriptEditorWidget, SIGNAL(scriptModified()), this, SLOT(updateScriptNameOrStatus())); - connect(newScriptEditorWidget, SIGNAL(runningStateChanged()), this, SLOT(updateButtons())); + connect(newScriptEditorWidget, &ScriptEditorWidget::scriptnameChanged, this, &ScriptEditorWindow::updateScriptNameOrStatus); + connect(newScriptEditorWidget, &ScriptEditorWidget::scriptModified, this, &ScriptEditorWindow::updateScriptNameOrStatus); + connect(newScriptEditorWidget, &ScriptEditorWidget::runningStateChanged, this, &ScriptEditorWindow::updateButtons); _ScriptEditorWindowUI->tabWidget->addTab(newScriptEditorWidget, title); _ScriptEditorWindowUI->tabWidget->setCurrentWidget(newScriptEditorWidget); newScriptEditorWidget->setUpdatesEnabled(true);