From af47510b051f4c73e2d56ce91b4a66d8bc5e6b9e Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 15 Apr 2014 00:30:01 +0200 Subject: [PATCH 01/13] Initial script editor widget / icons made by MaximillianMerlin ty --- interface/resources/icons/load-script.svg | 125 ++++ interface/resources/icons/new-script.svg | 129 +++++ interface/resources/icons/save-script.svg | 674 ++++++++++++++++++++++ interface/src/Application.h | 1 + interface/src/Menu.cpp | 8 + interface/src/Menu.h | 4 + interface/src/ScriptHighlighting.cpp | 18 + interface/src/ScriptHighlighting.h | 25 + interface/src/ui/ScriptEditorWidget.cpp | 39 ++ interface/src/ui/ScriptEditorWidget.h | 34 ++ interface/src/ui/ScriptEditorWindow.cpp | 62 ++ interface/src/ui/ScriptEditorWindow.h | 39 ++ interface/ui/ScriptEditorWidget.ui | 89 +++ interface/ui/ScriptEditorWindow.ui | 290 ++++++++++ 14 files changed, 1537 insertions(+) create mode 100644 interface/resources/icons/load-script.svg create mode 100644 interface/resources/icons/new-script.svg create mode 100644 interface/resources/icons/save-script.svg create mode 100644 interface/src/ScriptHighlighting.cpp create mode 100644 interface/src/ScriptHighlighting.h create mode 100644 interface/src/ui/ScriptEditorWidget.cpp create mode 100644 interface/src/ui/ScriptEditorWidget.h create mode 100644 interface/src/ui/ScriptEditorWindow.cpp create mode 100644 interface/src/ui/ScriptEditorWindow.h create mode 100644 interface/ui/ScriptEditorWidget.ui create mode 100644 interface/ui/ScriptEditorWindow.ui diff --git a/interface/resources/icons/load-script.svg b/interface/resources/icons/load-script.svg new file mode 100644 index 0000000000..21be61c321 --- /dev/null +++ b/interface/resources/icons/load-script.svg @@ -0,0 +1,125 @@ + + + + + + + + + + image/svg+xml + + + + + T.Hofmeister + + + + + + + + + + + + + + + + + + + + + + diff --git a/interface/resources/icons/new-script.svg b/interface/resources/icons/new-script.svg new file mode 100644 index 0000000000..f68fcfa967 --- /dev/null +++ b/interface/resources/icons/new-script.svg @@ -0,0 +1,129 @@ + + + + + + + + + + image/svg+xml + + + + + T.Hofmeister + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/interface/resources/icons/save-script.svg b/interface/resources/icons/save-script.svg new file mode 100644 index 0000000000..04d41b8302 --- /dev/null +++ b/interface/resources/icons/save-script.svg @@ -0,0 +1,674 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + T.Hofmeister + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/interface/src/Application.h b/interface/src/Application.h index 6a14788caa..761b24bb31 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -244,6 +244,7 @@ public: void skipVersion(QString latestVersion); QStringList getRunningScripts() { return _scriptEnginesHash.keys(); } + ScriptEngine* getScriptEngine(QString scriptHash) { return _scriptEnginesHash.contains(scriptHash) ? _scriptEnginesHash[scriptHash] : NULL; } signals: diff --git a/interface/src/Menu.cpp b/interface/src/Menu.cpp index 8ad55dec5b..87ab75cf3f 100644 --- a/interface/src/Menu.cpp +++ b/interface/src/Menu.cpp @@ -192,6 +192,7 @@ Menu::Menu() : QMenu* toolsMenu = addMenu("Tools"); addActionToQMenuAndActionHash(toolsMenu, MenuOption::MetavoxelEditor, 0, this, SLOT(showMetavoxelEditor())); + addActionToQMenuAndActionHash(toolsMenu, MenuOption::ScriptEditor, 0, this, SLOT(showScriptEditor())); #ifdef HAVE_QXMPP _chatAction = addActionToQMenuAndActionHash(toolsMenu, @@ -1058,6 +1059,13 @@ void Menu::showMetavoxelEditor() { _MetavoxelEditor->raise(); } +void Menu::showScriptEditor() { + if(!_ScriptEditor) { + _ScriptEditor = new ScriptEditorWindow(); + } + _ScriptEditor->raise(); +} + void Menu::showChat() { QMainWindow* mainWindow = Application::getInstance()->getWindow(); if (!_chatWindow) { diff --git a/interface/src/Menu.h b/interface/src/Menu.h index e827e43014..14ebbf356d 100644 --- a/interface/src/Menu.h +++ b/interface/src/Menu.h @@ -24,6 +24,7 @@ #include "location/LocationManager.h" #include "ui/PreferencesDialog.h" #include "ui/ChatWindow.h" +#include "ui/ScriptEditorWindow.h" const float ADJUST_LOD_DOWN_FPS = 40.0; const float ADJUST_LOD_UP_FPS = 55.0; @@ -161,6 +162,7 @@ private slots: void cycleFrustumRenderMode(); void runTests(); void showMetavoxelEditor(); + void showScriptEditor(); void showChat(); void toggleChat(); void audioMuteToggled(); @@ -212,6 +214,7 @@ private: FrustumDrawMode _frustumDrawMode; ViewFrustumOffset _viewFrustumOffset; QPointer _MetavoxelEditor; + QPointer _ScriptEditor; QPointer _chatWindow; OctreeStatsDialog* _octreeStatsDialog; LodToolsDialog* _lodToolsDialog; @@ -308,6 +311,7 @@ namespace MenuOption { const QString ResetAvatarSize = "Reset Avatar Size"; const QString RunningScripts = "Running Scripts"; const QString RunTimingTests = "Run Timing Tests"; + const QString ScriptEditor = "Script Editor..."; const QString SettingsExport = "Export Settings"; const QString SettingsImport = "Import Settings"; const QString Shadows = "Shadows"; diff --git a/interface/src/ScriptHighlighting.cpp b/interface/src/ScriptHighlighting.cpp new file mode 100644 index 0000000000..15505ac49a --- /dev/null +++ b/interface/src/ScriptHighlighting.cpp @@ -0,0 +1,18 @@ +// +// ScriptHighlighting.cpp +// interface/src +// +// Created by Thijs Wenker on 4/15/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 "ScriptHighlighting.h" + +ScriptHighlighting::ScriptHighlighting(QObject* parent) : + QSyntaxHighlighter(parent) +{ + +} \ No newline at end of file diff --git a/interface/src/ScriptHighlighting.h b/interface/src/ScriptHighlighting.h new file mode 100644 index 0000000000..f2bd97930e --- /dev/null +++ b/interface/src/ScriptHighlighting.h @@ -0,0 +1,25 @@ +// +// ScriptHighlighting.h +// interface/src +// +// Created by Thijs Wenker on 4/15/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_ScriptHighlighting_h +#define hifi_ScriptHighlighting_h + +#include + +class ScriptHighlighting : public QSyntaxHighlighter { + Q_OBJECT + +public: + ScriptHighlighting(QObject* parent = 0); + +}; + +#endif // hifi_ScriptHighlighting_h diff --git a/interface/src/ui/ScriptEditorWidget.cpp b/interface/src/ui/ScriptEditorWidget.cpp new file mode 100644 index 0000000000..4adf01a28a --- /dev/null +++ b/interface/src/ui/ScriptEditorWidget.cpp @@ -0,0 +1,39 @@ +// +// ScriptEditorWidget.cpp +// interface/src/ui +// +// Created by Thijs Wenker on 4/14/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 +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Application.h" +#include "ui_scriptEditorWidget.h" + +#include "ScriptEditorWidget.h" + +ScriptEditorWidget::ScriptEditorWidget() : + ui(new Ui::ScriptEditorWidget) +{ + ui->setupUi(this); + + // remove the title bar (see the Qt docs on setTitleBarWidget) + setTitleBarWidget(new QWidget()); + //QSyntaxHighlighter* highlighter = new QSyntaxHighlighter(); +} + +ScriptEditorWidget::~ScriptEditorWidget() { + delete ui; +} \ No newline at end of file diff --git a/interface/src/ui/ScriptEditorWidget.h b/interface/src/ui/ScriptEditorWidget.h new file mode 100644 index 0000000000..931ec105c9 --- /dev/null +++ b/interface/src/ui/ScriptEditorWidget.h @@ -0,0 +1,34 @@ +// +// ScriptEditorWidget.h +// interface/src/ui +// +// Created by Thijs Wenker on 4/14/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_ScriptEditorWidget_h +#define hifi_ScriptEditorWidget_h + +#include + +#include + +namespace Ui { +class ScriptEditorWidget; +} + +class ScriptEditorWidget : public QDockWidget { + Q_OBJECT + +public: + ScriptEditorWidget(); + ~ScriptEditorWidget(); + +private: + Ui::ScriptEditorWidget* ui; +}; + +#endif // hifi_ScriptEditorWidget_h diff --git a/interface/src/ui/ScriptEditorWindow.cpp b/interface/src/ui/ScriptEditorWindow.cpp new file mode 100644 index 0000000000..38fa26622a --- /dev/null +++ b/interface/src/ui/ScriptEditorWindow.cpp @@ -0,0 +1,62 @@ +// +// ScriptEditorWindow.cpp +// interface/src/ui +// +// Created by Thijs Wenker on 4/14/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 +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Application.h" +#include "FlowLayout.h" +#include "ui_scriptEditorWindow.h" +#include "ScriptEditorWidget.h" + +#include "ScriptEditorWindow.h" + +ScriptEditorWindow::ScriptEditorWindow() : + ui(new Ui::ScriptEditorWindow) +{ + ui->setupUi(this); + show(); +} + +ScriptEditorWindow::~ScriptEditorWindow() { + delete ui; +} + +void ScriptEditorWindow::loadScriptClicked(){ + +} + +void ScriptEditorWindow::newScriptClicked(){ + addScriptEditorWidget(QString("new Script")); +} + +void ScriptEditorWindow::toggleRunScriptClicked(){ + +} + +void ScriptEditorWindow::saveScriptClicked(){ + +} + +void ScriptEditorWindow::addScriptEditorWidget(QString title){ + ScriptEditorWidget* newScriptEditorWidget = new ScriptEditorWidget();//ScriptEditorWidget(); + ui->tabWidget->addTab(newScriptEditorWidget, title); + ui->tabWidget->setCurrentWidget(newScriptEditorWidget); + newScriptEditorWidget->setUpdatesEnabled(true); + newScriptEditorWidget->adjustSize(); +} \ No newline at end of file diff --git a/interface/src/ui/ScriptEditorWindow.h b/interface/src/ui/ScriptEditorWindow.h new file mode 100644 index 0000000000..718826cf9d --- /dev/null +++ b/interface/src/ui/ScriptEditorWindow.h @@ -0,0 +1,39 @@ +// +// ScriptEditorWindow.h +// interface/src/ui +// +// Created by Thijs Wenker on 4/14/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_ScriptEditorWindow_h +#define hifi_ScriptEditorWindow_h + +#include + +namespace Ui { +class ScriptEditorWindow; +} + +class ScriptEditorWindow : public QWidget { + Q_OBJECT + +public: + ScriptEditorWindow(); + ~ScriptEditorWindow(); + +private: + Ui::ScriptEditorWindow* ui; + void addScriptEditorWidget(QString title); + +private slots: + void loadScriptClicked(); + void newScriptClicked(); + void toggleRunScriptClicked(); + void saveScriptClicked(); +}; + +#endif // hifi_ScriptEditorWindow_h diff --git a/interface/ui/ScriptEditorWidget.ui b/interface/ui/ScriptEditorWidget.ui new file mode 100644 index 0000000000..82398d587c --- /dev/null +++ b/interface/ui/ScriptEditorWidget.ui @@ -0,0 +1,89 @@ + + + ScriptEditorWidget + + + + 0 + 0 + 702 + 543 + + + + + 0 + 0 + + + + + 400 + 238 + + + + font-family: Helvetica, Arial, sans-serif; + + + QDockWidget::DockWidgetFloatable|QDockWidget::DockWidgetMovable + + + Qt::NoDockWidgetArea + + + Edit Script + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + Courier + 9 + 50 + false + false + + + + font: 9pt "Courier"; + + + + + + + Debug Log: + + + + + + + font: 8pt "Courier"; + + + + + + + + + diff --git a/interface/ui/ScriptEditorWindow.ui b/interface/ui/ScriptEditorWindow.ui new file mode 100644 index 0000000000..a612b2b1c9 --- /dev/null +++ b/interface/ui/ScriptEditorWindow.ui @@ -0,0 +1,290 @@ + + + ScriptEditorWindow + + + Qt::WindowModal + + + + 0 + 0 + 474 + 638 + + + + + 400 + 250 + + + + Script editor [] + + + font-family: Helvetica, Arial, sans-serif; + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + 3 + + + QLayout::SetNoConstraint + + + 0 + + + 0 + + + + + New Script + + + New + + + + ../resources/icons/new-script.svg + ../resources/images/pinned.svg../resources/icons/new-script.svg + + + + 32 + 32 + + + + + + + + + 30 + 0 + + + + + 25 + 0 + + + + Load Script + + + Load + + + + ../resources/icons/load-script.svg../resources/icons/load-script.svg + + + + 32 + 32 + + + + false + + + QToolButton::MenuButtonPopup + + + Qt::ToolButtonIconOnly + + + + + + + + 0 + 2 + + + + Qt::NoFocus + + + Qt::NoContextMenu + + + Save Script + + + Save + + + + ../resources/icons/save-script.svg../resources/icons/save-script.svg + + + + 32 + 32 + + + + 316 + + + + + + + Toggle Run Script + + + Run/Stop + + + + ../resources/images/plus.svg../resources/images/plus.svg + + + + 32 + 32 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + true + + + + 250 + 80 + + + + QTabWidget::West + + + QTabWidget::Triangular + + + -1 + + + Qt::ElideNone + + + true + + + true + + + + + + + + + saveButton + clicked() + ScriptEditorWindow + saveScriptClicked() + + + 236 + 10 + + + 199 + 264 + + + + + toggleRunButton + clicked() + ScriptEditorWindow + toggleRunScriptClicked() + + + 330 + 10 + + + 199 + 264 + + + + + newButton + clicked() + ScriptEditorWindow + newScriptClicked() + + + 58 + 10 + + + 199 + 264 + + + + + loadButton + clicked() + ScriptEditorWindow + loadScriptClicked() + + + 85 + 10 + + + 199 + 264 + + + + + From e66822c6ee56cc12854628e6b1448bdc635fdb82 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Wed, 16 Apr 2014 00:55:06 +0200 Subject: [PATCH 02/13] ScriptEditor: Syntax highlighting update --- interface/src/ScriptHighlighting.cpp | 57 ++++++++++++++++++++- interface/src/ScriptHighlighting.h | 20 +++++++- interface/src/ui/ScriptEditorWidget.cpp | 5 +- interface/ui/ScriptEditorWidget.ui | 66 ++++++++++++++++++++++--- 4 files changed, 137 insertions(+), 11 deletions(-) diff --git a/interface/src/ScriptHighlighting.cpp b/interface/src/ScriptHighlighting.cpp index 15505ac49a..119926742c 100644 --- a/interface/src/ScriptHighlighting.cpp +++ b/interface/src/ScriptHighlighting.cpp @@ -11,8 +11,61 @@ #include "ScriptHighlighting.h" -ScriptHighlighting::ScriptHighlighting(QObject* parent) : +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("\".*\""); + multiLineCommentBegin = QRegExp("/\\*"); + multiLineCommentEnd = QRegExp("\\*/"); + numberRegex = QRegExp("[0-9]+(\\.[0-9]+){0,1}"); +} -} \ No newline at end of file +void ScriptHighlighting::highlightBlock(const QString &text) { + this->highlightKeywords(text); + this->formatComments(text); + this->formatQoutedText(text); + this->formatNumbers(text); +} + +void ScriptHighlighting::highlightKeywords(const QString &text) { + int index = keywordRegex.indexIn(text); + while (index >= 0) { + int length = keywordRegex.matchedLength(); + setFormat(index, length, Qt::blue); + index = keywordRegex.indexIn(text, index + length); + } +} + +void ScriptHighlighting::formatComments(const QString &text) { + + setCurrentBlockState(BlockStateClean); + + int start = (previousBlockState() != BlockStateInMultiComment) ? text.indexOf(multiLineCommentBegin) : 0; + + while (start > -1) { + int end = text.indexOf(multiLineCommentEnd, start); + int length = (end == -1 ? text.length() : (end + multiLineCommentEnd.matchedLength())) - start; + setFormat(start, length, Qt::lightGray); + start = text.indexOf(multiLineCommentBegin, start + length); + if (end == -1) setCurrentBlockState(BlockStateInMultiComment); + } +} + +void ScriptHighlighting::formatQoutedText(const QString &text){ + int index = qoutedTextRegex.indexIn(text); + while (index >= 0) { + int length = qoutedTextRegex.matchedLength(); + setFormat(index, length, Qt::red); + index = qoutedTextRegex.indexIn(text, index + length); + } +} + +void ScriptHighlighting::formatNumbers(const QString &text){ + int index = numberRegex.indexIn(text); + while (index >= 0) { + int length = numberRegex.matchedLength(); + setFormat(index, length, Qt::green); + index = numberRegex.indexIn(text, index + length); + } +} diff --git a/interface/src/ScriptHighlighting.h b/interface/src/ScriptHighlighting.h index f2bd97930e..b9567cb06a 100644 --- a/interface/src/ScriptHighlighting.h +++ b/interface/src/ScriptHighlighting.h @@ -18,8 +18,26 @@ class ScriptHighlighting : public QSyntaxHighlighter { Q_OBJECT public: - ScriptHighlighting(QObject* parent = 0); + ScriptHighlighting(QTextDocument* parent = 0); + enum BlockState { + BlockStateClean, + BlockStateInMultiComment + }; + +protected: + void highlightBlock(const QString &text); + void highlightKeywords(const QString &text); + void formatComments(const QString &text); + void formatQoutedText(const QString &text); + void formatNumbers(const QString &text); + +private: + QRegExp keywordRegex; + QRegExp qoutedTextRegex; + QRegExp multiLineCommentBegin; + QRegExp multiLineCommentEnd; + QRegExp numberRegex; }; #endif // hifi_ScriptHighlighting_h diff --git a/interface/src/ui/ScriptEditorWidget.cpp b/interface/src/ui/ScriptEditorWidget.cpp index 4adf01a28a..618e405448 100644 --- a/interface/src/ui/ScriptEditorWidget.cpp +++ b/interface/src/ui/ScriptEditorWidget.cpp @@ -20,6 +20,7 @@ #include #include "Application.h" +#include "ScriptHighlighting.h" #include "ui_scriptEditorWidget.h" #include "ScriptEditorWidget.h" @@ -31,7 +32,9 @@ ScriptEditorWidget::ScriptEditorWidget() : // remove the title bar (see the Qt docs on setTitleBarWidget) setTitleBarWidget(new QWidget()); - //QSyntaxHighlighter* highlighter = new QSyntaxHighlighter(); + QFontMetrics fm(this->ui->scriptEdit->font()); + this->ui->scriptEdit->setTabStopWidth(fm.width('0') * 4); + ScriptHighlighting* highlighting = new ScriptHighlighting(this->ui->scriptEdit->document()); } ScriptEditorWidget::~ScriptEditorWidget() { diff --git a/interface/ui/ScriptEditorWidget.ui b/interface/ui/ScriptEditorWidget.ui index 82398d587c..5878f26c69 100644 --- a/interface/ui/ScriptEditorWidget.ui +++ b/interface/ui/ScriptEditorWidget.ui @@ -52,7 +52,7 @@ 0 - + Courier @@ -68,22 +68,74 @@ - - - Debug Log: + + + 0 - + + 0 + + + 0 + + + + + + 0 + 0 + + + + Debug Log: + + + + + + + Clear + + + + 16 + 16 + + + + + - + font: 8pt "Courier"; + + true + - + + + clearButton + clicked() + debugText + clear() + + + 663 + 447 + + + 350 + 501 + + + + From 0b644b5738b8ed3324f61fbb7957529865b79b63 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Wed, 16 Apr 2014 22:58:44 +0200 Subject: [PATCH 03/13] ctrl+shift+s to open the script editor --- interface/src/Menu.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/src/Menu.cpp b/interface/src/Menu.cpp index cfeba0705f..303e155da6 100644 --- a/interface/src/Menu.cpp +++ b/interface/src/Menu.cpp @@ -192,7 +192,7 @@ Menu::Menu() : QMenu* toolsMenu = addMenu("Tools"); addActionToQMenuAndActionHash(toolsMenu, MenuOption::MetavoxelEditor, 0, this, SLOT(showMetavoxelEditor())); - addActionToQMenuAndActionHash(toolsMenu, MenuOption::ScriptEditor, 0, this, SLOT(showScriptEditor())); + addActionToQMenuAndActionHash(toolsMenu, MenuOption::ScriptEditor, Qt::CTRL | Qt::SHIFT | Qt::Key_S, this, SLOT(showScriptEditor())); #ifdef HAVE_QXMPP _chatAction = addActionToQMenuAndActionHash(toolsMenu, From c016d6557ef5f473b866a0e850718aa2401bde67 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Mon, 21 Apr 2014 05:25:28 +0200 Subject: [PATCH 04/13] - few Adjustments in Application class for Script editor - Shortkey for script editor (Alt+S) - highlighter: bold true/false , single line comments - Run scripts from the editor works - More icons from Maximillian, Thanks - Run on the fly checkbox, works if the script is running. - Load/save features --- interface/resources/icons/start-script.svg | 557 +++++++++++++++++++ interface/resources/icons/stop-script.svg | 163 ++++++ interface/src/Application.cpp | 35 +- interface/src/Application.h | 2 +- interface/src/Menu.cpp | 4 +- interface/src/ScriptHighlighting.cpp | 26 +- interface/src/ScriptHighlighting.h | 13 +- interface/src/ui/ScriptEditorWidget.cpp | 114 ++++ interface/src/ui/ScriptEditorWidget.h | 22 + interface/src/ui/ScriptEditorWindow.cpp | 148 ++++- interface/src/ui/ScriptEditorWindow.h | 17 +- interface/ui/ScriptEditorWidget.ui | 13 +- interface/ui/ScriptEditorWindow.ui | 69 ++- libraries/script-engine/src/ScriptEngine.cpp | 24 +- libraries/script-engine/src/ScriptEngine.h | 7 + 15 files changed, 1163 insertions(+), 51 deletions(-) create mode 100644 interface/resources/icons/start-script.svg create mode 100644 interface/resources/icons/stop-script.svg diff --git a/interface/resources/icons/start-script.svg b/interface/resources/icons/start-script.svg new file mode 100644 index 0000000000..86354a555d --- /dev/null +++ b/interface/resources/icons/start-script.svg @@ -0,0 +1,557 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + Maximillian Merlin + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/interface/resources/icons/stop-script.svg b/interface/resources/icons/stop-script.svg new file mode 100644 index 0000000000..31cdcee749 --- /dev/null +++ b/interface/resources/icons/stop-script.svg @@ -0,0 +1,163 @@ + + + + + + + + + + image/svg+xml + + + + + Maximillian Merlin + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 438722df17..769791d09f 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -797,6 +797,7 @@ void Application::keyPressEvent(QKeyEvent* event) { if (activeWindow() == _window) { bool isShifted = event->modifiers().testFlag(Qt::ShiftModifier); bool isMeta = event->modifiers().testFlag(Qt::ControlModifier); + bool isOption = event->modifiers().testFlag(Qt::AltModifier); switch (event->key()) { break; case Qt::Key_BracketLeft: @@ -839,9 +840,11 @@ void Application::keyPressEvent(QKeyEvent* event) { break; case Qt::Key_S: - if (isShifted && isMeta) { + if (isShifted && isMeta && !isOption) { Menu::getInstance()->triggerOption(MenuOption::SuppressShortTimings); - } else if (!isShifted && isMeta) { + } else if (isOption && !isShifted && !isMeta) { + Menu::getInstance()->triggerOption(MenuOption::ScriptEditor); + } else if (!isOption && !isShifted && isMeta) { takeSnapshot(); } else { _myAvatar->setDriveKeys(BACK, 1.f); @@ -3299,13 +3302,14 @@ void Application::stopAllScripts() { bumpSettings(); } -void Application::stopScript(const QString &scriptName) -{ - _scriptEnginesHash.value(scriptName)->stop(); - qDebug() << "stopping script..." << scriptName; - _scriptEnginesHash.remove(scriptName); - _runningScriptsWidget->setRunningScripts(getRunningScripts()); - bumpSettings(); +void Application::stopScript(const QString &scriptName) { + if (_scriptEnginesHash.contains(scriptName)) { + _scriptEnginesHash.value(scriptName)->stop(); + qDebug() << "stopping script..." << scriptName; + _scriptEnginesHash.remove(scriptName); + _runningScriptsWidget->setRunningScripts(getRunningScripts()); + bumpSettings(); + } } void Application::reloadAllScripts() { @@ -3366,7 +3370,10 @@ void Application::uploadSkeleton() { uploadFST(false); } -void Application::loadScript(const QString& scriptName) { +ScriptEngine* Application::loadScript(const QString& scriptName, bool focusMainWindow) { + if(_scriptEnginesHash.contains(scriptName) && !_scriptEnginesHash[scriptName]->isFinished()){ + return _scriptEnginesHash[scriptName]; + } // start the script on a new thread... ScriptEngine* scriptEngine = new ScriptEngine(QUrl(scriptName), &_controllerScriptingInterface); @@ -3374,7 +3381,7 @@ void Application::loadScript(const QString& scriptName) { if (!scriptEngine->hasScript()) { qDebug() << "Application::loadScript(), script failed to load..."; - return; + return NULL; } _runningScriptsWidget->setRunningScripts(getRunningScripts()); @@ -3422,8 +3429,12 @@ void Application::loadScript(const QString& scriptName) { workerThread->start(); // restore the main window's active state - _window->activateWindow(); + if (focusMainWindow) { + _window->activateWindow(); + } bumpSettings(); + + return scriptEngine; } void Application::loadDialog() { diff --git a/interface/src/Application.h b/interface/src/Application.h index 00b71c4ce7..3254c874b6 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -121,7 +121,7 @@ public: ~Application(); void restoreSizeAndPosition(); - void loadScript(const QString& fileNameString); + ScriptEngine* loadScript(const QString& fileNameString, bool focusMainWindow = true); void loadScripts(); void storeSizeAndPosition(); void clearScriptsBeforeRunning(); diff --git a/interface/src/Menu.cpp b/interface/src/Menu.cpp index 6f70c5616c..4e6dd2eec2 100644 --- a/interface/src/Menu.cpp +++ b/interface/src/Menu.cpp @@ -195,7 +195,7 @@ Menu::Menu() : QMenu* toolsMenu = addMenu("Tools"); addActionToQMenuAndActionHash(toolsMenu, MenuOption::MetavoxelEditor, 0, this, SLOT(showMetavoxelEditor())); - addActionToQMenuAndActionHash(toolsMenu, MenuOption::ScriptEditor, Qt::CTRL | Qt::SHIFT | Qt::Key_S, this, SLOT(showScriptEditor())); + addActionToQMenuAndActionHash(toolsMenu, MenuOption::ScriptEditor, Qt::ALT | Qt::Key_S, this, SLOT(showScriptEditor())); #ifdef HAVE_QXMPP _chatAction = addActionToQMenuAndActionHash(toolsMenu, @@ -1125,7 +1125,7 @@ void Menu::showMetavoxelEditor() { } void Menu::showScriptEditor() { - if(!_ScriptEditor) { + if(!_ScriptEditor || !_ScriptEditor->isVisible()) { _ScriptEditor = new ScriptEditorWindow(); } _ScriptEditor->raise(); diff --git a/interface/src/ScriptHighlighting.cpp b/interface/src/ScriptHighlighting.cpp index 119926742c..b2c5ca2ec6 100644 --- a/interface/src/ScriptHighlighting.cpp +++ b/interface/src/ScriptHighlighting.cpp @@ -10,6 +10,7 @@ // #include "ScriptHighlighting.h" +#include ScriptHighlighting::ScriptHighlighting(QTextDocument* parent) : QSyntaxHighlighter(parent) @@ -19,13 +20,16 @@ ScriptHighlighting::ScriptHighlighting(QTextDocument* parent) : multiLineCommentBegin = QRegExp("/\\*"); multiLineCommentEnd = QRegExp("\\*/"); numberRegex = QRegExp("[0-9]+(\\.[0-9]+){0,1}"); + singleLineComment = QRegExp("//[^\n]*"); + truefalseRegex = QRegExp("\\b(true|false)\\b"); } void ScriptHighlighting::highlightBlock(const QString &text) { this->highlightKeywords(text); - this->formatComments(text); - this->formatQoutedText(text); this->formatNumbers(text); + this->formatTrueFalse(text); + this->formatQoutedText(text); + this->formatComments(text); } void ScriptHighlighting::highlightKeywords(const QString &text) { @@ -50,6 +54,13 @@ void ScriptHighlighting::formatComments(const QString &text) { start = text.indexOf(multiLineCommentBegin, start + length); if (end == -1) setCurrentBlockState(BlockStateInMultiComment); } + + int index = singleLineComment.indexIn(text); + while (index >= 0) { + int length = singleLineComment.matchedLength(); + setFormat(index, length, Qt::lightGray); + index = singleLineComment.indexIn(text, index + length); + } } void ScriptHighlighting::formatQoutedText(const QString &text){ @@ -69,3 +80,14 @@ void ScriptHighlighting::formatNumbers(const QString &text){ index = numberRegex.indexIn(text, index + length); } } + +void ScriptHighlighting::formatTrueFalse(const QString text){ + int index = truefalseRegex.indexIn(text); + while (index >= 0) { + int length = truefalseRegex.matchedLength(); + QFont* font = new QFont(this->document()->defaultFont()); + font->setBold(true); + setFormat(index, length, *font); + index = truefalseRegex.indexIn(text, index + length); + } +} \ No newline at end of file diff --git a/interface/src/ScriptHighlighting.h b/interface/src/ScriptHighlighting.h index b9567cb06a..9cbbf277cc 100644 --- a/interface/src/ScriptHighlighting.h +++ b/interface/src/ScriptHighlighting.h @@ -26,11 +26,12 @@ public: }; protected: - void highlightBlock(const QString &text); - void highlightKeywords(const QString &text); - void formatComments(const QString &text); - void formatQoutedText(const QString &text); - void formatNumbers(const QString &text); + void highlightBlock(const QString& text); + void highlightKeywords(const QString& text); + void formatComments(const QString& text); + void formatQoutedText(const QString& text); + void formatNumbers(const QString& text); + void formatTrueFalse(const QString text); private: QRegExp keywordRegex; @@ -38,6 +39,8 @@ private: QRegExp multiLineCommentBegin; QRegExp multiLineCommentEnd; QRegExp numberRegex; + QRegExp singleLineComment; + QRegExp truefalseRegex; }; #endif // hifi_ScriptHighlighting_h diff --git a/interface/src/ui/ScriptEditorWidget.cpp b/interface/src/ui/ScriptEditorWidget.cpp index 618e405448..98b6f2fe96 100644 --- a/interface/src/ui/ScriptEditorWidget.cpp +++ b/interface/src/ui/ScriptEditorWidget.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -30,13 +31,126 @@ ScriptEditorWidget::ScriptEditorWidget() : { ui->setupUi(this); + scriptEngine = NULL; + + connect(ui->scriptEdit->document(), SIGNAL(modificationChanged(bool)), this, SIGNAL(scriptModified())); + connect(ui->scriptEdit->document(), SIGNAL(contentsChanged()), this, SLOT(onScriptModified())); + // remove the title bar (see the Qt docs on setTitleBarWidget) setTitleBarWidget(new QWidget()); QFontMetrics fm(this->ui->scriptEdit->font()); this->ui->scriptEdit->setTabStopWidth(fm.width('0') * 4); ScriptHighlighting* highlighting = new ScriptHighlighting(this->ui->scriptEdit->document()); + QTimer::singleShot(0, this->ui->scriptEdit, SLOT(setFocus())); } ScriptEditorWidget::~ScriptEditorWidget() { delete ui; +} + +void ScriptEditorWidget::onScriptModified() { + if(ui->onTheFlyCheckBox->isChecked() && isRunning()) { + setRunning(false); + setRunning(true); + } +} + +bool ScriptEditorWidget::isModified() { + return ui->scriptEdit->document()->isModified(); +} + +bool ScriptEditorWidget::isRunning() { + return (scriptEngine != NULL) ? scriptEngine->isRunning() : false; +} + +bool ScriptEditorWidget::setRunning(bool run) { + if (run && !save()) { + return false; + } + // Clean-up old connections. + disconnect(this, SLOT(onScriptError(const QString&))); + disconnect(this, SLOT(onScriptPrint(const QString&))); + + if (run) { + scriptEngine = Application::getInstance()->loadScript(this->currentScript, false); + connect(scriptEngine, SIGNAL(runningStateChanged()), this, SIGNAL(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&))); + } else { + Application::getInstance()->stopScript(this->currentScript); + scriptEngine = NULL; + } + return true; +} + +bool ScriptEditorWidget::saveFile(const QString &scriptPath) { + QFile file(scriptPath); + if (!file.open(QFile::WriteOnly | QFile::Text)) { + QMessageBox::warning(this, tr("Interface"), tr("Cannot write script %1:\n%2.").arg(scriptPath).arg(file.errorString())); + return false; + } + + QTextStream out(&file); + out << ui->scriptEdit->toPlainText(); + + setScriptFile(scriptPath); + return true; +} + +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; + } + + QTextStream in(&file); + ui->scriptEdit->setPlainText(in.readAll()); + + setScriptFile(scriptPath); + + disconnect(this, SLOT(onScriptError(const QString&))); + disconnect(this, SLOT(onScriptPrint(const QString&))); + + scriptEngine = Application::getInstance()->getScriptEngine(scriptPath); + 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&))); + } +} + +bool ScriptEditorWidget::save() { + return currentScript.isEmpty() ? saveAs() : saveFile(currentScript); +} + +bool ScriptEditorWidget::saveAs() { + QString fileName = QFileDialog::getSaveFileName(this, tr("Save script"), QString(), tr("Javascript (*.js)")); + return !fileName.isEmpty() ? saveFile(fileName) : false; +} + +void ScriptEditorWidget::setScriptFile(const QString& scriptPath) { + currentScript = scriptPath; + ui->scriptEdit->document()->setModified(false); + setWindowModified(false); + + emit scriptnameChanged(); +} + +bool ScriptEditorWidget::questionSave() { + if (ui->scriptEdit->document()->isModified()) { + 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); + return button == QMessageBox::Save ? save() : (button == QMessageBox::Cancel ? false : true); + } + return true; +} + +void ScriptEditorWidget::onScriptError(const QString& message) { + ui->debugText->appendPlainText("ERROR: "+ message); +} + +void ScriptEditorWidget::onScriptPrint(const QString& message) { + ui->debugText->appendPlainText("> "+message); } \ No newline at end of file diff --git a/interface/src/ui/ScriptEditorWidget.h b/interface/src/ui/ScriptEditorWidget.h index 931ec105c9..674304acb6 100644 --- a/interface/src/ui/ScriptEditorWidget.h +++ b/interface/src/ui/ScriptEditorWidget.h @@ -27,8 +27,30 @@ public: ScriptEditorWidget(); ~ScriptEditorWidget(); + bool isModified(); + bool isRunning(); + bool setRunning(bool run); + bool saveFile(const QString& scriptPath); + void loadFile(const QString& scriptPath); + void setScriptFile(const QString& scriptPath); + bool save(); + bool saveAs(); + bool questionSave(); + const QString getScriptName() const { return currentScript; }; +signals: + void runningStateChanged(); + void scriptnameChanged(); + void scriptModified(); + +private slots: + void onScriptError(const QString& message); + void onScriptPrint(const QString& message); + void onScriptModified(); + private: Ui::ScriptEditorWidget* ui; + ScriptEngine* scriptEngine; + QString currentScript; }; #endif // hifi_ScriptEditorWidget_h diff --git a/interface/src/ui/ScriptEditorWindow.cpp b/interface/src/ui/ScriptEditorWindow.cpp index 38fa26622a..687b992c5f 100644 --- a/interface/src/ui/ScriptEditorWindow.cpp +++ b/interface/src/ui/ScriptEditorWindow.cpp @@ -13,8 +13,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -31,32 +33,158 @@ ScriptEditorWindow::ScriptEditorWindow() : { ui->setupUi(this); show(); + addScriptEditorWidget("New script"); + loadMenu = new QMenu(); + connect(loadMenu, SIGNAL(aboutToShow()), this, SLOT(loadMenuAboutToShow())); + ui->loadButton->setMenu(loadMenu); + + saveMenu = new QMenu(); + saveMenu->addAction("Save as..", this, SLOT(saveScriptAsClicked()), Qt::CTRL|Qt::SHIFT|Qt::Key_S); + + ui->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())); } ScriptEditorWindow::~ScriptEditorWindow() { delete ui; } -void ScriptEditorWindow::loadScriptClicked(){ - +void ScriptEditorWindow::setRunningState(bool run) { + if (ui->tabWidget->currentIndex() != -1) { + ((ScriptEditorWidget*)ui->tabWidget->currentWidget())->setRunning(run); + } + this->updateButtons(); } -void ScriptEditorWindow::newScriptClicked(){ - addScriptEditorWidget(QString("new Script")); +void ScriptEditorWindow::updateButtons() { + ui->toggleRunButton->setEnabled(ui->tabWidget->currentIndex() != -1); + ui->toggleRunButton->setIcon(ui->tabWidget->currentIndex() != -1 && ((ScriptEditorWidget*)ui->tabWidget->currentWidget())->isRunning() ? QIcon("../resources/icons/stop-script.svg"):QIcon("../resources/icons/start-script.svg")); } -void ScriptEditorWindow::toggleRunScriptClicked(){ - +void ScriptEditorWindow::loadScriptMenu(const QString& scriptName) { + addScriptEditorWidget("loading...")->loadFile(scriptName); + updateButtons(); } -void ScriptEditorWindow::saveScriptClicked(){ - +void ScriptEditorWindow::loadScriptClicked() { + QString scriptName = QFileDialog::getOpenFileName(this, tr("Interface"), QString(), tr("Javascript (*.js)")); + if (!scriptName.isEmpty()) { + addScriptEditorWidget("loading...")->loadFile(scriptName); + updateButtons(); + } } -void ScriptEditorWindow::addScriptEditorWidget(QString title){ - ScriptEditorWidget* newScriptEditorWidget = new ScriptEditorWidget();//ScriptEditorWidget(); +void ScriptEditorWindow::loadMenuAboutToShow() { + loadMenu->clear(); + QStringList runningScripts = Application::getInstance()->getRunningScripts(); + if (runningScripts.count() > 0) { + QSignalMapper* signalMapper = new QSignalMapper(this); + foreach (const QString& runningScript, runningScripts) { + QAction* runningScriptAction = new QAction(runningScript, loadMenu); + connect(runningScriptAction, SIGNAL(triggered()), signalMapper, SLOT(map())); + signalMapper->setMapping(runningScriptAction, runningScript); + loadMenu->addAction(runningScriptAction); + } + connect(signalMapper, SIGNAL(mapped(const QString &)), this, SLOT(loadScriptMenu(const QString &))); + } else { + QAction* naAction = new QAction("(no running scripts)",loadMenu); + naAction->setDisabled(true); + loadMenu->addAction(naAction); + } +} + +void ScriptEditorWindow::newScriptClicked() { + addScriptEditorWidget(QString("New script")); +} + +void ScriptEditorWindow::toggleRunScriptClicked() { + this->setRunningState(!(ui->tabWidget->currentIndex() !=-1 && ((ScriptEditorWidget*)ui->tabWidget->currentWidget())->isRunning())); +} + +void ScriptEditorWindow::saveScriptClicked() { + if (ui->tabWidget->currentIndex() != -1) { + ScriptEditorWidget* currentScriptWidget = (ScriptEditorWidget*)ui->tabWidget->currentWidget(); + currentScriptWidget->save(); + } +} + +void ScriptEditorWindow::saveScriptAsClicked() { + if (ui->tabWidget->currentIndex() != -1) { + ScriptEditorWidget* currentScriptWidget = (ScriptEditorWidget*)ui->tabWidget->currentWidget(); + currentScriptWidget->saveAs(); + } +} + +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())); ui->tabWidget->addTab(newScriptEditorWidget, title); ui->tabWidget->setCurrentWidget(newScriptEditorWidget); newScriptEditorWidget->setUpdatesEnabled(true); newScriptEditorWidget->adjustSize(); + return newScriptEditorWidget; +} + +void ScriptEditorWindow::tabSwitched(int tabIndex) { + this->updateButtons(); + if (ui->tabWidget->currentIndex() != -1) { + ScriptEditorWidget* currentScriptWidget = (ScriptEditorWidget*)ui->tabWidget->currentWidget(); + QString modifiedStar = (currentScriptWidget->isModified()?"*":""); + if (currentScriptWidget->getScriptName().length() > 0) { + this->setWindowTitle("Script Editor ["+currentScriptWidget->getScriptName()+modifiedStar+"]"); + } else { + this->setWindowTitle("Script Editor [New script"+modifiedStar+"]"); + } + } else { + this->setWindowTitle("Script Editor"); + } +} + +void ScriptEditorWindow::tabCloseRequested(int tabIndex) { + ScriptEditorWidget* closingScriptWidget = (ScriptEditorWidget*)ui->tabWidget->widget(tabIndex); + if(closingScriptWidget->questionSave()) { + ui->tabWidget->removeTab(tabIndex); + } +} + +void ScriptEditorWindow::closeEvent(QCloseEvent *event) { + bool unsaved_docs_warning = false; + for (int i = 0; i < ui->tabWidget->count(); i++ && !unsaved_docs_warning){ + if(((ScriptEditorWidget*)ui->tabWidget->widget(i))->isModified()){ + unsaved_docs_warning = true; + } + } + + if (!unsaved_docs_warning || QMessageBox::warning(this, tr("Interface"), tr("There are some unsaved scripts, are you sure you want to close the editor? Changes will be lost!"), QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel) == QMessageBox::Discard) { + event->accept(); + } else { + event->ignore(); + } +} + +void ScriptEditorWindow::updateScriptNameOrStatus() { + ScriptEditorWidget* source = (ScriptEditorWidget*)QObject::sender(); + QString modifiedStar = (source->isModified()?"*":""); + if (source->getScriptName().length() > 0) { + for (int i = 0; i < ui->tabWidget->count(); i++){ + if (ui->tabWidget->widget(i) == source) { + ui->tabWidget->setTabText(i,modifiedStar+QFileInfo(source->getScriptName()).fileName()); + ui->tabWidget->setTabToolTip(i, source->getScriptName()); + } + } + } + + if (ui->tabWidget->currentWidget() == source) { + if (source->getScriptName().length() > 0) { + this->setWindowTitle("Script Editor ["+source->getScriptName()+modifiedStar+"]"); + } else { + this->setWindowTitle("Script Editor [New script"+modifiedStar+"]"); + } + } } \ No newline at end of file diff --git a/interface/src/ui/ScriptEditorWindow.h b/interface/src/ui/ScriptEditorWindow.h index 718826cf9d..290a9d6051 100644 --- a/interface/src/ui/ScriptEditorWindow.h +++ b/interface/src/ui/ScriptEditorWindow.h @@ -13,6 +13,7 @@ #define hifi_ScriptEditorWindow_h #include +#include "ScriptEditorWidget.h" namespace Ui { class ScriptEditorWindow; @@ -25,15 +26,29 @@ public: ScriptEditorWindow(); ~ScriptEditorWindow(); +protected: + void closeEvent(QCloseEvent *event); + private: Ui::ScriptEditorWindow* ui; - void addScriptEditorWidget(QString title); + QMenu* loadMenu; + QMenu* saveMenu; + ScriptEditorWidget* addScriptEditorWidget(QString title); + void setRunningState(bool run); + void setScriptName(const QString& scriptName); private slots: + void loadScriptMenu(const QString& scriptName); void loadScriptClicked(); void newScriptClicked(); void toggleRunScriptClicked(); void saveScriptClicked(); + void saveScriptAsClicked(); + void loadMenuAboutToShow(); + void tabSwitched(int tabIndex); + void tabCloseRequested(int tabIndex); + void updateScriptNameOrStatus(); + void updateButtons(); }; #endif // hifi_ScriptEditorWindow_h diff --git a/interface/ui/ScriptEditorWidget.ui b/interface/ui/ScriptEditorWidget.ui index 5878f26c69..88761c91c5 100644 --- a/interface/ui/ScriptEditorWidget.ui +++ b/interface/ui/ScriptEditorWidget.ui @@ -6,8 +6,8 @@ 0 0 - 702 - 543 + 691 + 549 @@ -18,7 +18,7 @@ - 400 + 541 238 @@ -91,6 +91,13 @@ + + + + Run on the fly (Careful: Any valid change made to the code will run immediately) + + + diff --git a/interface/ui/ScriptEditorWindow.ui b/interface/ui/ScriptEditorWindow.ui index a612b2b1c9..9e1b08de3e 100644 --- a/interface/ui/ScriptEditorWindow.ui +++ b/interface/ui/ScriptEditorWindow.ui @@ -9,8 +9,8 @@ 0 0 - 474 - 638 + 706 + 682 @@ -20,7 +20,7 @@ - Script editor [] + Script Editor font-family: Helvetica, Arial, sans-serif; @@ -58,7 +58,7 @@ - New Script + New Script (Ctrl+N) New @@ -66,7 +66,7 @@ ../resources/icons/new-script.svg - ../resources/images/pinned.svg../resources/icons/new-script.svg + ../resources/icons/new-script.svg../resources/icons/new-script.svg @@ -91,7 +91,7 @@ - Load Script + Load Script (Ctrl+O) Load @@ -119,11 +119,17 @@ - - - 0 - 2 - + + + 30 + 0 + + + + + 32 + 0 + Qt::NoFocus @@ -132,7 +138,7 @@ Qt::NoContextMenu - Save Script + Save Script (Ctrl+S) Save @@ -150,19 +156,22 @@ 316 + + QToolButton::MenuButtonPopup + - Toggle Run Script + Toggle Run Script (F5) Run/Stop - ../resources/images/plus.svg../resources/images/plus.svg + ../resources/icons/start-script.svg../resources/icons/start-script.svg @@ -286,5 +295,37 @@ + + tabWidget + currentChanged(int) + ScriptEditorWindow + tabSwitched(int) + + + 352 + 360 + + + 352 + 340 + + + + + tabWidget + tabCloseRequested(int) + ScriptEditorWindow + tabCloseRequested(int) + + + 352 + 360 + + + 352 + 340 + + + diff --git a/libraries/script-engine/src/ScriptEngine.cpp b/libraries/script-engine/src/ScriptEngine.cpp index 684c55fbb0..eeb1cebe09 100644 --- a/libraries/script-engine/src/ScriptEngine.cpp +++ b/libraries/script-engine/src/ScriptEngine.cpp @@ -43,6 +43,11 @@ static QScriptValue soundConstructor(QScriptContext* context, QScriptEngine* eng return soundScriptValue; } +static QScriptValue debugPrint(QScriptContext* context, QScriptEngine* engine){ + qDebug() << "script:print()<<" << context->argument(0).toString(); + engine->evaluate("Script.print('"+context->argument(0).toString()+"')"); + return QScriptValue(); +} ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNameString, AbstractControllerScriptingInterface* controllerScriptingInterface) : @@ -115,6 +120,7 @@ ScriptEngine::ScriptEngine(const QUrl& scriptURL, _scriptContents = in.readAll(); } else { qDebug() << "ERROR Loading file:" << fileName; + emit errorMessage("ERROR Loading file:" + fileName); } } else { QNetworkAccessManager* networkManager = new QNetworkAccessManager(this); @@ -200,6 +206,9 @@ void ScriptEngine::init() { qScriptRegisterSequenceMetaType >(&_engine); qScriptRegisterSequenceMetaType >(&_engine); + QScriptValue printConstructorValue = _engine.newFunction(debugPrint); + _engine.globalObject().setProperty("print", printConstructorValue); + QScriptValue soundConstructorValue = _engine.newFunction(soundConstructor); QScriptValue soundMetaObject = _engine.newQMetaObject(&Sound::staticMetaObject, soundConstructorValue); _engine.globalObject().setProperty("Sound", soundMetaObject); @@ -246,6 +255,7 @@ void ScriptEngine::evaluate() { if (_engine.hasUncaughtException()) { int line = _engine.uncaughtExceptionLineNumber(); qDebug() << "Uncaught exception at line" << line << ":" << result.toString(); + emit errorMessage("Uncaught exception at line" + QString::number(line) + ":" + result.toString()); } } @@ -266,11 +276,14 @@ void ScriptEngine::run() { init(); } _isRunning = true; + emit runningStateChanged(); QScriptValue result = _engine.evaluate(_scriptContents); if (_engine.hasUncaughtException()) { int line = _engine.uncaughtExceptionLineNumber(); + qDebug() << "Uncaught exception at line" << line << ":" << result.toString(); + emit errorMessage("Uncaught exception at line" + QString::number(line) + ":" + result.toString()); } timeval startTime; @@ -401,6 +414,7 @@ void ScriptEngine::run() { if (_engine.hasUncaughtException()) { int line = _engine.uncaughtExceptionLineNumber(); qDebug() << "Uncaught exception at line" << line << ":" << _engine.uncaughtException().toString(); + emit errorMessage("Uncaught exception at line" + QString::number(line) + ":" + _engine.uncaughtException().toString()); } } emit scriptEnding(); @@ -436,10 +450,12 @@ void ScriptEngine::run() { emit finished(_fileNameString); _isRunning = false; + emit runningStateChanged(); } void ScriptEngine::stop() { _isFinished = true; + emit runningStateChanged(); } void ScriptEngine::timerFired() { @@ -510,6 +526,10 @@ QUrl ScriptEngine::resolveInclude(const QString& include) const { return url; } +void ScriptEngine::print(const QString& message) { + emit printedMessage(message); +} + void ScriptEngine::include(const QString& includeFile) { QUrl url = resolveInclude(includeFile); QString includeContents; @@ -523,6 +543,7 @@ void ScriptEngine::include(const QString& includeFile) { includeContents = in.readAll(); } else { qDebug() << "ERROR Loading file:" << fileName; + emit errorMessage("ERROR Loading file:" + fileName); } } else { QNetworkAccessManager* networkManager = new QNetworkAccessManager(this); @@ -538,5 +559,6 @@ void ScriptEngine::include(const QString& includeFile) { if (_engine.hasUncaughtException()) { int line = _engine.uncaughtExceptionLineNumber(); qDebug() << "Uncaught exception at (" << includeFile << ") line" << line << ":" << result.toString(); + emit errorMessage("Uncaught exception at (" + includeFile + ") line" + QString::number(line) + ":" + result.toString()); } -} +} \ No newline at end of file diff --git a/libraries/script-engine/src/ScriptEngine.h b/libraries/script-engine/src/ScriptEngine.h index 941c6bbe27..9ea99276d3 100644 --- a/libraries/script-engine/src/ScriptEngine.h +++ b/libraries/script-engine/src/ScriptEngine.h @@ -80,6 +80,9 @@ public: bool hasScript() const { return !_scriptContents.isEmpty(); } + bool isFinished() const { return _isFinished; } + bool isRunning() const { return _isRunning; } + public slots: void stop(); @@ -88,12 +91,16 @@ public slots: void clearInterval(QObject* timer) { stopTimer(reinterpret_cast(timer)); } void clearTimeout(QObject* timer) { stopTimer(reinterpret_cast(timer)); } void include(const QString& includeFile); + void print(const QString& message); signals: void update(float deltaTime); void scriptEnding(); void finished(const QString& fileNameString); void cleanupMenuItem(const QString& menuItemString); + void printedMessage(const QString& message); + void errorMessage(const QString& message); + void runningStateChanged(); protected: QString _scriptContents; From 5bffb17643f013e267deeeea6efe615449386232 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Mon, 21 Apr 2014 05:51:34 +0200 Subject: [PATCH 05/13] #include -> #include --- interface/src/ui/ScriptEditorWindow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/src/ui/ScriptEditorWindow.cpp b/interface/src/ui/ScriptEditorWindow.cpp index 687b992c5f..0774ed02f0 100644 --- a/interface/src/ui/ScriptEditorWindow.cpp +++ b/interface/src/ui/ScriptEditorWindow.cpp @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include #include From 77994ae0cbaecdb0175051de3ae04d4db80ca123 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Mon, 21 Apr 2014 06:20:18 +0200 Subject: [PATCH 06/13] header cleanup (hopefully the ubuntu test-build will like this) --- interface/src/ui/ScriptEditorWidget.cpp | 6 +++--- interface/src/ui/ScriptEditorWidget.h | 4 ++-- interface/src/ui/ScriptEditorWindow.cpp | 8 ++++---- interface/src/ui/ScriptEditorWindow.h | 1 - 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/interface/src/ui/ScriptEditorWidget.cpp b/interface/src/ui/ScriptEditorWidget.cpp index 98b6f2fe96..91e3efe1df 100644 --- a/interface/src/ui/ScriptEditorWidget.cpp +++ b/interface/src/ui/ScriptEditorWidget.cpp @@ -9,6 +9,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +#include "ui_scriptEditorWidget.h" +#include "ScriptEditorWidget.h" + #include #include #include @@ -22,9 +25,6 @@ #include "Application.h" #include "ScriptHighlighting.h" -#include "ui_scriptEditorWidget.h" - -#include "ScriptEditorWidget.h" ScriptEditorWidget::ScriptEditorWidget() : ui(new Ui::ScriptEditorWidget) diff --git a/interface/src/ui/ScriptEditorWidget.h b/interface/src/ui/ScriptEditorWidget.h index 674304acb6..5fed373658 100644 --- a/interface/src/ui/ScriptEditorWidget.h +++ b/interface/src/ui/ScriptEditorWidget.h @@ -13,8 +13,8 @@ #define hifi_ScriptEditorWidget_h #include - -#include +#include "ScriptEditorWidget.h" +#include "ScriptEngine.h" namespace Ui { class ScriptEditorWidget; diff --git a/interface/src/ui/ScriptEditorWindow.cpp b/interface/src/ui/ScriptEditorWindow.cpp index 0774ed02f0..69283feccf 100644 --- a/interface/src/ui/ScriptEditorWindow.cpp +++ b/interface/src/ui/ScriptEditorWindow.cpp @@ -9,6 +9,10 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +#include "ui_scriptEditorWindow.h" +#include "ScriptEditorWindow.h" +#include "ScriptEditorWidget.h" + #include #include #include @@ -23,10 +27,6 @@ #include "Application.h" #include "FlowLayout.h" -#include "ui_scriptEditorWindow.h" -#include "ScriptEditorWidget.h" - -#include "ScriptEditorWindow.h" ScriptEditorWindow::ScriptEditorWindow() : ui(new Ui::ScriptEditorWindow) diff --git a/interface/src/ui/ScriptEditorWindow.h b/interface/src/ui/ScriptEditorWindow.h index 290a9d6051..a1b72423ec 100644 --- a/interface/src/ui/ScriptEditorWindow.h +++ b/interface/src/ui/ScriptEditorWindow.h @@ -12,7 +12,6 @@ #ifndef hifi_ScriptEditorWindow_h #define hifi_ScriptEditorWindow_h -#include #include "ScriptEditorWidget.h" namespace Ui { From 0e28ff2fcda7ee5e1bd13decb5cb6b20eed2506c Mon Sep 17 00:00:00 2001 From: "U-Uthoypia\\Thijs" Date: Mon, 21 Apr 2014 06:33:20 +0200 Subject: [PATCH 07/13] This should fix the ubuntu build --- interface/ui/{ScriptEditorWidget.ui => scriptEditorWidget.ui} | 0 interface/ui/{ScriptEditorWindow.ui => scriptEditorWindow.ui} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename interface/ui/{ScriptEditorWidget.ui => scriptEditorWidget.ui} (100%) rename interface/ui/{ScriptEditorWindow.ui => scriptEditorWindow.ui} (100%) diff --git a/interface/ui/ScriptEditorWidget.ui b/interface/ui/scriptEditorWidget.ui similarity index 100% rename from interface/ui/ScriptEditorWidget.ui rename to interface/ui/scriptEditorWidget.ui diff --git a/interface/ui/ScriptEditorWindow.ui b/interface/ui/scriptEditorWindow.ui similarity index 100% rename from interface/ui/ScriptEditorWindow.ui rename to interface/ui/scriptEditorWindow.ui From c1ed38f4deddbd7e9873b522e6bb3d3f3d559cc0 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Mon, 21 Apr 2014 21:23:39 +0200 Subject: [PATCH 08/13] Style --- interface/src/ScriptHighlighting.cpp | 68 +++++++++--------- interface/src/ScriptHighlighting.h | 18 ++--- interface/src/ui/ScriptEditorWidget.cpp | 74 +++++++++---------- interface/src/ui/ScriptEditorWidget.h | 11 +-- interface/src/ui/ScriptEditorWindow.cpp | 96 ++++++++++++++----------- interface/src/ui/ScriptEditorWindow.h | 11 +-- 6 files changed, 147 insertions(+), 131 deletions(-) diff --git a/interface/src/ScriptHighlighting.cpp b/interface/src/ScriptHighlighting.cpp index b2c5ca2ec6..9b58298b62 100644 --- a/interface/src/ScriptHighlighting.cpp +++ b/interface/src/ScriptHighlighting.cpp @@ -15,16 +15,16 @@ 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("\".*\""); - multiLineCommentBegin = QRegExp("/\\*"); - multiLineCommentEnd = QRegExp("\\*/"); - numberRegex = QRegExp("[0-9]+(\\.[0-9]+){0,1}"); - singleLineComment = QRegExp("//[^\n]*"); - truefalseRegex = QRegExp("\\b(true|false)\\b"); + _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("\".*\""); + _multiLineCommentBegin = QRegExp("/\\*"); + _multiLineCommentEnd = QRegExp("\\*/"); + _numberRegex = QRegExp("[0-9]+(\\.[0-9]+){0,1}"); + _singleLineComment = QRegExp("//[^\n]*"); + _truefalseRegex = QRegExp("\\b(true|false)\\b"); } -void ScriptHighlighting::highlightBlock(const QString &text) { +void ScriptHighlighting::highlightBlock(const QString& text) { this->highlightKeywords(text); this->formatNumbers(text); this->formatTrueFalse(text); @@ -32,62 +32,64 @@ void ScriptHighlighting::highlightBlock(const QString &text) { this->formatComments(text); } -void ScriptHighlighting::highlightKeywords(const QString &text) { - int index = keywordRegex.indexIn(text); +void ScriptHighlighting::highlightKeywords(const QString& text) { + int index = _keywordRegex.indexIn(text); while (index >= 0) { - int length = keywordRegex.matchedLength(); + int length = _keywordRegex.matchedLength(); setFormat(index, length, Qt::blue); - index = keywordRegex.indexIn(text, index + length); + index = _keywordRegex.indexIn(text, index + length); } } -void ScriptHighlighting::formatComments(const QString &text) { +void ScriptHighlighting::formatComments(const QString& text) { setCurrentBlockState(BlockStateClean); - int start = (previousBlockState() != BlockStateInMultiComment) ? text.indexOf(multiLineCommentBegin) : 0; + int start = (previousBlockState() != BlockStateInMultiComment) ? text.indexOf(_multiLineCommentBegin) : 0; while (start > -1) { - int end = text.indexOf(multiLineCommentEnd, start); - int length = (end == -1 ? text.length() : (end + multiLineCommentEnd.matchedLength())) - start; + int end = text.indexOf(_multiLineCommentEnd, start); + int length = (end == -1 ? text.length() : (end + _multiLineCommentEnd.matchedLength())) - start; setFormat(start, length, Qt::lightGray); - start = text.indexOf(multiLineCommentBegin, start + length); - if (end == -1) setCurrentBlockState(BlockStateInMultiComment); + start = text.indexOf(_multiLineCommentBegin, start + length); + if (end == -1) { + setCurrentBlockState(BlockStateInMultiComment); + } } - int index = singleLineComment.indexIn(text); + int index = _singleLineComment.indexIn(text); while (index >= 0) { - int length = singleLineComment.matchedLength(); + int length = _singleLineComment.matchedLength(); setFormat(index, length, Qt::lightGray); - index = singleLineComment.indexIn(text, index + length); + index = _singleLineComment.indexIn(text, index + length); } } -void ScriptHighlighting::formatQoutedText(const QString &text){ - int index = qoutedTextRegex.indexIn(text); +void ScriptHighlighting::formatQoutedText(const QString& text){ + int index = _qoutedTextRegex.indexIn(text); while (index >= 0) { - int length = qoutedTextRegex.matchedLength(); + int length = _qoutedTextRegex.matchedLength(); setFormat(index, length, Qt::red); - index = qoutedTextRegex.indexIn(text, index + length); + index = _qoutedTextRegex.indexIn(text, index + length); } } -void ScriptHighlighting::formatNumbers(const QString &text){ - int index = numberRegex.indexIn(text); +void ScriptHighlighting::formatNumbers(const QString& text){ + int index = _numberRegex.indexIn(text); while (index >= 0) { - int length = numberRegex.matchedLength(); + int length = _numberRegex.matchedLength(); setFormat(index, length, Qt::green); - index = numberRegex.indexIn(text, index + length); + index = _numberRegex.indexIn(text, index + length); } } -void ScriptHighlighting::formatTrueFalse(const QString text){ - int index = truefalseRegex.indexIn(text); +void ScriptHighlighting::formatTrueFalse(const QString& text){ + int index = _truefalseRegex.indexIn(text); while (index >= 0) { - int length = truefalseRegex.matchedLength(); + int length = _truefalseRegex.matchedLength(); QFont* font = new QFont(this->document()->defaultFont()); font->setBold(true); setFormat(index, length, *font); - index = truefalseRegex.indexIn(text, index + length); + index = _truefalseRegex.indexIn(text, index + length); } } \ No newline at end of file diff --git a/interface/src/ScriptHighlighting.h b/interface/src/ScriptHighlighting.h index 9cbbf277cc..d86d6f4d77 100644 --- a/interface/src/ScriptHighlighting.h +++ b/interface/src/ScriptHighlighting.h @@ -18,7 +18,7 @@ class ScriptHighlighting : public QSyntaxHighlighter { Q_OBJECT public: - ScriptHighlighting(QTextDocument* parent = 0); + ScriptHighlighting(QTextDocument* parent = NULL); enum BlockState { BlockStateClean, @@ -31,16 +31,16 @@ protected: void formatComments(const QString& text); void formatQoutedText(const QString& text); void formatNumbers(const QString& text); - void formatTrueFalse(const QString text); + void formatTrueFalse(const QString& text); private: - QRegExp keywordRegex; - QRegExp qoutedTextRegex; - QRegExp multiLineCommentBegin; - QRegExp multiLineCommentEnd; - QRegExp numberRegex; - QRegExp singleLineComment; - QRegExp truefalseRegex; + QRegExp _keywordRegex; + QRegExp _qoutedTextRegex; + QRegExp _multiLineCommentBegin; + QRegExp _multiLineCommentEnd; + QRegExp _numberRegex; + QRegExp _singleLineComment; + QRegExp _truefalseRegex; }; #endif // hifi_ScriptHighlighting_h diff --git a/interface/src/ui/ScriptEditorWidget.cpp b/interface/src/ui/ScriptEditorWidget.cpp index 91e3efe1df..d24c68a613 100644 --- a/interface/src/ui/ScriptEditorWidget.cpp +++ b/interface/src/ui/ScriptEditorWidget.cpp @@ -27,40 +27,39 @@ #include "ScriptHighlighting.h" ScriptEditorWidget::ScriptEditorWidget() : - ui(new Ui::ScriptEditorWidget) + _scriptEditorWidgetUI(new Ui::ScriptEditorWidget), + _scriptEngine(NULL) { - ui->setupUi(this); + _scriptEditorWidgetUI->setupUi(this); - scriptEngine = NULL; - - connect(ui->scriptEdit->document(), SIGNAL(modificationChanged(bool)), this, SIGNAL(scriptModified())); - connect(ui->scriptEdit->document(), SIGNAL(contentsChanged()), this, SLOT(onScriptModified())); + connect(_scriptEditorWidgetUI->scriptEdit->document(), SIGNAL(modificationChanged(bool)), this, SIGNAL(scriptModified())); + connect(_scriptEditorWidgetUI->scriptEdit->document(), SIGNAL(contentsChanged()), this, SLOT(onScriptModified())); // remove the title bar (see the Qt docs on setTitleBarWidget) setTitleBarWidget(new QWidget()); - QFontMetrics fm(this->ui->scriptEdit->font()); - this->ui->scriptEdit->setTabStopWidth(fm.width('0') * 4); - ScriptHighlighting* highlighting = new ScriptHighlighting(this->ui->scriptEdit->document()); - QTimer::singleShot(0, this->ui->scriptEdit, SLOT(setFocus())); + QFontMetrics fm(_scriptEditorWidgetUI->scriptEdit->font()); + _scriptEditorWidgetUI->scriptEdit->setTabStopWidth(fm.width('0') * 4); + ScriptHighlighting* highlighting = new ScriptHighlighting(_scriptEditorWidgetUI->scriptEdit->document()); + QTimer::singleShot(0, _scriptEditorWidgetUI->scriptEdit, SLOT(setFocus())); } ScriptEditorWidget::~ScriptEditorWidget() { - delete ui; + delete _scriptEditorWidgetUI; } void ScriptEditorWidget::onScriptModified() { - if(ui->onTheFlyCheckBox->isChecked() && isRunning()) { + if(_scriptEditorWidgetUI->onTheFlyCheckBox->isChecked() && isRunning()) { setRunning(false); setRunning(true); } } bool ScriptEditorWidget::isModified() { - return ui->scriptEdit->document()->isModified(); + return _scriptEditorWidgetUI->scriptEdit->document()->isModified(); } bool ScriptEditorWidget::isRunning() { - return (scriptEngine != NULL) ? scriptEngine->isRunning() : false; + return (_scriptEngine != NULL) ? _scriptEngine->isRunning() : false; } bool ScriptEditorWidget::setRunning(bool run) { @@ -72,15 +71,15 @@ bool ScriptEditorWidget::setRunning(bool run) { disconnect(this, SLOT(onScriptPrint(const QString&))); if (run) { - scriptEngine = Application::getInstance()->loadScript(this->currentScript, false); - connect(scriptEngine, SIGNAL(runningStateChanged()), this, SIGNAL(runningStateChanged())); + _scriptEngine = Application::getInstance()->loadScript(_currentScript, false); + connect(_scriptEngine, SIGNAL(runningStateChanged()), this, SIGNAL(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, SIGNAL(errorMessage(const QString&)), this, SLOT(onScriptError(const QString&))); + connect(_scriptEngine, SIGNAL(printedMessage(const QString&)), this, SLOT(onScriptPrint(const QString&))); } else { - Application::getInstance()->stopScript(this->currentScript); - scriptEngine = NULL; + Application::getInstance()->stopScript(_currentScript); + _scriptEngine = NULL; } return true; } @@ -88,18 +87,19 @@ bool ScriptEditorWidget::setRunning(bool run) { bool ScriptEditorWidget::saveFile(const QString &scriptPath) { QFile file(scriptPath); if (!file.open(QFile::WriteOnly | QFile::Text)) { - QMessageBox::warning(this, tr("Interface"), tr("Cannot write script %1:\n%2.").arg(scriptPath).arg(file.errorString())); + QMessageBox::warning(this, tr("Interface"), tr("Cannot write script %1:\n%2.").arg(scriptPath) + .arg(file.errorString())); return false; } QTextStream out(&file); - out << ui->scriptEdit->toPlainText(); + out << _scriptEditorWidgetUI->scriptEdit->toPlainText(); setScriptFile(scriptPath); return true; } -void ScriptEditorWidget::loadFile(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())); @@ -107,23 +107,23 @@ void ScriptEditorWidget::loadFile(const QString &scriptPath) { } QTextStream in(&file); - ui->scriptEdit->setPlainText(in.readAll()); + _scriptEditorWidgetUI->scriptEdit->setPlainText(in.readAll()); setScriptFile(scriptPath); disconnect(this, SLOT(onScriptError(const QString&))); disconnect(this, SLOT(onScriptPrint(const QString&))); - scriptEngine = Application::getInstance()->getScriptEngine(scriptPath); - 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&))); + _scriptEngine = Application::getInstance()->getScriptEngine(scriptPath); + 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&))); } } bool ScriptEditorWidget::save() { - return currentScript.isEmpty() ? saveAs() : saveFile(currentScript); + return _currentScript.isEmpty() ? saveAs() : saveFile(_currentScript); } bool ScriptEditorWidget::saveAs() { @@ -132,25 +132,27 @@ bool ScriptEditorWidget::saveAs() { } void ScriptEditorWidget::setScriptFile(const QString& scriptPath) { - currentScript = scriptPath; - ui->scriptEdit->document()->setModified(false); + _currentScript = scriptPath; + _scriptEditorWidgetUI->scriptEdit->document()->setModified(false); setWindowModified(false); emit scriptnameChanged(); } bool ScriptEditorWidget::questionSave() { - if (ui->scriptEdit->document()->isModified()) { - 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); + if (_scriptEditorWidgetUI->scriptEdit->document()->isModified()) { + 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); return button == QMessageBox::Save ? save() : (button == QMessageBox::Cancel ? false : true); } return true; } void ScriptEditorWidget::onScriptError(const QString& message) { - ui->debugText->appendPlainText("ERROR: "+ message); + _scriptEditorWidgetUI->debugText->appendPlainText("ERROR: " + message); } void ScriptEditorWidget::onScriptPrint(const QString& message) { - ui->debugText->appendPlainText("> "+message); + _scriptEditorWidgetUI->debugText->appendPlainText("> " + message); } \ No newline at end of file diff --git a/interface/src/ui/ScriptEditorWidget.h b/interface/src/ui/ScriptEditorWidget.h index 5fed373658..3e50280a62 100644 --- a/interface/src/ui/ScriptEditorWidget.h +++ b/interface/src/ui/ScriptEditorWidget.h @@ -17,7 +17,7 @@ #include "ScriptEngine.h" namespace Ui { -class ScriptEditorWidget; + class ScriptEditorWidget; } class ScriptEditorWidget : public QDockWidget { @@ -36,7 +36,8 @@ public: bool save(); bool saveAs(); bool questionSave(); - const QString getScriptName() const { return currentScript; }; + const QString getScriptName() const { return _currentScript; }; + signals: void runningStateChanged(); void scriptnameChanged(); @@ -48,9 +49,9 @@ private slots: void onScriptModified(); private: - Ui::ScriptEditorWidget* ui; - ScriptEngine* scriptEngine; - QString currentScript; + Ui::ScriptEditorWidget* _scriptEditorWidgetUI; + ScriptEngine* _scriptEngine; + QString _currentScript; }; #endif // hifi_ScriptEditorWidget_h diff --git a/interface/src/ui/ScriptEditorWindow.cpp b/interface/src/ui/ScriptEditorWindow.cpp index 69283feccf..00a3ef9f0c 100644 --- a/interface/src/ui/ScriptEditorWindow.cpp +++ b/interface/src/ui/ScriptEditorWindow.cpp @@ -29,19 +29,19 @@ #include "FlowLayout.h" ScriptEditorWindow::ScriptEditorWindow() : - ui(new Ui::ScriptEditorWindow) + _ScriptEditorWindowUI(new Ui::ScriptEditorWindow), + _loadMenu(new QMenu), + _saveMenu(new QMenu) { - ui->setupUi(this); + _ScriptEditorWindowUI->setupUi(this); show(); addScriptEditorWidget("New script"); - loadMenu = new QMenu(); - connect(loadMenu, SIGNAL(aboutToShow()), this, SLOT(loadMenuAboutToShow())); - ui->loadButton->setMenu(loadMenu); + connect(_loadMenu, SIGNAL(aboutToShow()), this, SLOT(loadMenuAboutToShow())); + _ScriptEditorWindowUI->loadButton->setMenu(_loadMenu); - saveMenu = new QMenu(); - saveMenu->addAction("Save as..", this, SLOT(saveScriptAsClicked()), Qt::CTRL|Qt::SHIFT|Qt::Key_S); + _saveMenu->addAction("Save as..", this, SLOT(saveScriptAsClicked()), Qt::CTRL | Qt::SHIFT | Qt::Key_S); - ui->saveButton->setMenu(saveMenu); + _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())); @@ -50,19 +50,21 @@ ScriptEditorWindow::ScriptEditorWindow() : } ScriptEditorWindow::~ScriptEditorWindow() { - delete ui; + delete _ScriptEditorWindowUI; } void ScriptEditorWindow::setRunningState(bool run) { - if (ui->tabWidget->currentIndex() != -1) { - ((ScriptEditorWidget*)ui->tabWidget->currentWidget())->setRunning(run); + if (_ScriptEditorWindowUI->tabWidget->currentIndex() != -1) { + static_cast(_ScriptEditorWindowUI->tabWidget->currentWidget())->setRunning(run); } this->updateButtons(); } void ScriptEditorWindow::updateButtons() { - ui->toggleRunButton->setEnabled(ui->tabWidget->currentIndex() != -1); - ui->toggleRunButton->setIcon(ui->tabWidget->currentIndex() != -1 && ((ScriptEditorWidget*)ui->tabWidget->currentWidget())->isRunning() ? QIcon("../resources/icons/stop-script.svg"):QIcon("../resources/icons/start-script.svg")); + _ScriptEditorWindowUI->toggleRunButton->setEnabled(_ScriptEditorWindowUI->tabWidget->currentIndex() != -1); + _ScriptEditorWindowUI->toggleRunButton->setIcon(_ScriptEditorWindowUI->tabWidget->currentIndex() != -1 && + static_cast(_ScriptEditorWindowUI->tabWidget->currentWidget())->isRunning() ? + QIcon("../resources/icons/stop-script.svg") : QIcon("../resources/icons/start-script.svg")); } void ScriptEditorWindow::loadScriptMenu(const QString& scriptName) { @@ -79,21 +81,21 @@ void ScriptEditorWindow::loadScriptClicked() { } void ScriptEditorWindow::loadMenuAboutToShow() { - loadMenu->clear(); + _loadMenu->clear(); QStringList runningScripts = Application::getInstance()->getRunningScripts(); if (runningScripts.count() > 0) { QSignalMapper* signalMapper = new QSignalMapper(this); foreach (const QString& runningScript, runningScripts) { - QAction* runningScriptAction = new QAction(runningScript, loadMenu); + QAction* runningScriptAction = new QAction(runningScript, _loadMenu); connect(runningScriptAction, SIGNAL(triggered()), signalMapper, SLOT(map())); signalMapper->setMapping(runningScriptAction, runningScript); - loadMenu->addAction(runningScriptAction); + _loadMenu->addAction(runningScriptAction); } - connect(signalMapper, SIGNAL(mapped(const QString &)), this, SLOT(loadScriptMenu(const QString &))); + connect(signalMapper, SIGNAL(mapped(const QString &)), this, SLOT(loadScriptMenu(const QString&))); } else { - QAction* naAction = new QAction("(no running scripts)",loadMenu); + QAction* naAction = new QAction("(no running scripts)", _loadMenu); naAction->setDisabled(true); - loadMenu->addAction(naAction); + _loadMenu->addAction(naAction); } } @@ -102,19 +104,22 @@ void ScriptEditorWindow::newScriptClicked() { } void ScriptEditorWindow::toggleRunScriptClicked() { - this->setRunningState(!(ui->tabWidget->currentIndex() !=-1 && ((ScriptEditorWidget*)ui->tabWidget->currentWidget())->isRunning())); + this->setRunningState(!(_ScriptEditorWindowUI->tabWidget->currentIndex() !=-1 + && static_cast(_ScriptEditorWindowUI->tabWidget->currentWidget())->isRunning())); } void ScriptEditorWindow::saveScriptClicked() { - if (ui->tabWidget->currentIndex() != -1) { - ScriptEditorWidget* currentScriptWidget = (ScriptEditorWidget*)ui->tabWidget->currentWidget(); + if (_ScriptEditorWindowUI->tabWidget->currentIndex() != -1) { + ScriptEditorWidget* currentScriptWidget = static_cast(_ScriptEditorWindowUI->tabWidget + ->currentWidget()); currentScriptWidget->save(); } } void ScriptEditorWindow::saveScriptAsClicked() { - if (ui->tabWidget->currentIndex() != -1) { - ScriptEditorWidget* currentScriptWidget = (ScriptEditorWidget*)ui->tabWidget->currentWidget(); + if (_ScriptEditorWindowUI->tabWidget->currentIndex() != -1) { + ScriptEditorWidget* currentScriptWidget = static_cast(_ScriptEditorWindowUI->tabWidget + ->currentWidget()); currentScriptWidget->saveAs(); } } @@ -124,8 +129,8 @@ ScriptEditorWidget* ScriptEditorWindow::addScriptEditorWidget(QString title) { connect(newScriptEditorWidget, SIGNAL(scriptnameChanged()), this, SLOT(updateScriptNameOrStatus())); connect(newScriptEditorWidget, SIGNAL(scriptModified()), this, SLOT(updateScriptNameOrStatus())); connect(newScriptEditorWidget, SIGNAL(runningStateChanged()), this, SLOT(updateButtons())); - ui->tabWidget->addTab(newScriptEditorWidget, title); - ui->tabWidget->setCurrentWidget(newScriptEditorWidget); + _ScriptEditorWindowUI->tabWidget->addTab(newScriptEditorWidget, title); + _ScriptEditorWindowUI->tabWidget->setCurrentWidget(newScriptEditorWidget); newScriptEditorWidget->setUpdatesEnabled(true); newScriptEditorWidget->adjustSize(); return newScriptEditorWidget; @@ -133,13 +138,14 @@ ScriptEditorWidget* ScriptEditorWindow::addScriptEditorWidget(QString title) { void ScriptEditorWindow::tabSwitched(int tabIndex) { this->updateButtons(); - if (ui->tabWidget->currentIndex() != -1) { - ScriptEditorWidget* currentScriptWidget = (ScriptEditorWidget*)ui->tabWidget->currentWidget(); - QString modifiedStar = (currentScriptWidget->isModified()?"*":""); + if (_ScriptEditorWindowUI->tabWidget->currentIndex() != -1) { + ScriptEditorWidget* currentScriptWidget = static_cast(_ScriptEditorWindowUI->tabWidget + ->currentWidget()); + QString modifiedStar = (currentScriptWidget->isModified() ? "*" : ""); if (currentScriptWidget->getScriptName().length() > 0) { - this->setWindowTitle("Script Editor ["+currentScriptWidget->getScriptName()+modifiedStar+"]"); + this->setWindowTitle("Script Editor [" + currentScriptWidget->getScriptName() + modifiedStar + "]"); } else { - this->setWindowTitle("Script Editor [New script"+modifiedStar+"]"); + this->setWindowTitle("Script Editor [New script" + modifiedStar + "]"); } } else { this->setWindowTitle("Script Editor"); @@ -147,21 +153,25 @@ void ScriptEditorWindow::tabSwitched(int tabIndex) { } void ScriptEditorWindow::tabCloseRequested(int tabIndex) { - ScriptEditorWidget* closingScriptWidget = (ScriptEditorWidget*)ui->tabWidget->widget(tabIndex); + ScriptEditorWidget* closingScriptWidget = static_cast(_ScriptEditorWindowUI->tabWidget + ->widget(tabIndex)); if(closingScriptWidget->questionSave()) { - ui->tabWidget->removeTab(tabIndex); + _ScriptEditorWindowUI->tabWidget->removeTab(tabIndex); } } void ScriptEditorWindow::closeEvent(QCloseEvent *event) { bool unsaved_docs_warning = false; - for (int i = 0; i < ui->tabWidget->count(); i++ && !unsaved_docs_warning){ - if(((ScriptEditorWidget*)ui->tabWidget->widget(i))->isModified()){ + for (int i = 0; i < _ScriptEditorWindowUI->tabWidget->count(); i++){ + if(static_cast(_ScriptEditorWindowUI->tabWidget->widget(i))->isModified()){ unsaved_docs_warning = true; + break; } } - if (!unsaved_docs_warning || QMessageBox::warning(this, tr("Interface"), tr("There are some unsaved scripts, are you sure you want to close the editor? Changes will be lost!"), QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel) == QMessageBox::Discard) { + if (!unsaved_docs_warning || QMessageBox::warning(this, tr("Interface"), + tr("There are some unsaved scripts, are you sure you want to close the editor? Changes will be lost!"), + QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel) == QMessageBox::Discard) { event->accept(); } else { event->ignore(); @@ -172,19 +182,19 @@ void ScriptEditorWindow::updateScriptNameOrStatus() { ScriptEditorWidget* source = (ScriptEditorWidget*)QObject::sender(); QString modifiedStar = (source->isModified()?"*":""); if (source->getScriptName().length() > 0) { - for (int i = 0; i < ui->tabWidget->count(); i++){ - if (ui->tabWidget->widget(i) == source) { - ui->tabWidget->setTabText(i,modifiedStar+QFileInfo(source->getScriptName()).fileName()); - ui->tabWidget->setTabToolTip(i, source->getScriptName()); + for (int i = 0; i < _ScriptEditorWindowUI->tabWidget->count(); i++){ + if (_ScriptEditorWindowUI->tabWidget->widget(i) == source) { + _ScriptEditorWindowUI->tabWidget->setTabText(i,modifiedStar + QFileInfo(source->getScriptName()).fileName()); + _ScriptEditorWindowUI->tabWidget->setTabToolTip(i, source->getScriptName()); } } } - if (ui->tabWidget->currentWidget() == source) { + if (_ScriptEditorWindowUI->tabWidget->currentWidget() == source) { if (source->getScriptName().length() > 0) { - this->setWindowTitle("Script Editor ["+source->getScriptName()+modifiedStar+"]"); + this->setWindowTitle("Script Editor [" + source->getScriptName() + modifiedStar + "]"); } else { - this->setWindowTitle("Script Editor [New script"+modifiedStar+"]"); + this->setWindowTitle("Script Editor [New script" + modifiedStar + "]"); } } } \ No newline at end of file diff --git a/interface/src/ui/ScriptEditorWindow.h b/interface/src/ui/ScriptEditorWindow.h index a1b72423ec..0bf5015ccf 100644 --- a/interface/src/ui/ScriptEditorWindow.h +++ b/interface/src/ui/ScriptEditorWindow.h @@ -15,7 +15,7 @@ #include "ScriptEditorWidget.h" namespace Ui { -class ScriptEditorWindow; + class ScriptEditorWindow; } class ScriptEditorWindow : public QWidget { @@ -26,12 +26,13 @@ public: ~ScriptEditorWindow(); protected: - void closeEvent(QCloseEvent *event); + void closeEvent(QCloseEvent* event); private: - Ui::ScriptEditorWindow* ui; - QMenu* loadMenu; - QMenu* saveMenu; + Ui::ScriptEditorWindow* _ScriptEditorWindowUI; + QMenu* _loadMenu; + QMenu* _saveMenu; + ScriptEditorWidget* addScriptEditorWidget(QString title); void setRunningState(bool run); void setScriptName(const QString& scriptName); From 9faee0d82d9a42938778075f5bc43d38bf16e21d Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Mon, 21 Apr 2014 21:37:49 +0200 Subject: [PATCH 09/13] - Style - Script Editor can run script file once at time, but you can still start multiple scripts through the original ways. --- interface/src/Application.cpp | 6 +++--- interface/src/Application.h | 2 +- interface/src/ui/ScriptEditorWidget.cpp | 2 +- interface/src/ui/ScriptEditorWindow.cpp | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 79d3e7597e..4ba465bbe1 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3377,8 +3377,8 @@ void Application::uploadSkeleton() { uploadFST(false); } -ScriptEngine* Application::loadScript(const QString& scriptName, bool focusMainWindow) { - if(_scriptEnginesHash.contains(scriptName) && !_scriptEnginesHash[scriptName]->isFinished()){ +ScriptEngine* Application::loadScript(const QString& scriptName, bool loadScriptFromEditor) { + if(loadScriptFromEditor && _scriptEnginesHash.contains(scriptName) && !_scriptEnginesHash[scriptName]->isFinished()){ return _scriptEnginesHash[scriptName]; } @@ -3436,7 +3436,7 @@ ScriptEngine* Application::loadScript(const QString& scriptName, bool focusMainW workerThread->start(); // restore the main window's active state - if (focusMainWindow) { + if (!loadScriptFromEditor) { _window->activateWindow(); } bumpSettings(); diff --git a/interface/src/Application.h b/interface/src/Application.h index e66e7e2975..fb48acb721 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -121,7 +121,7 @@ public: ~Application(); void restoreSizeAndPosition(); - ScriptEngine* loadScript(const QString& fileNameString, bool focusMainWindow = true); + ScriptEngine* loadScript(const QString& fileNameString, bool loadScriptFromEditor = false); void loadScripts(); void storeSizeAndPosition(); void clearScriptsBeforeRunning(); diff --git a/interface/src/ui/ScriptEditorWidget.cpp b/interface/src/ui/ScriptEditorWidget.cpp index d24c68a613..74b74e1ad6 100644 --- a/interface/src/ui/ScriptEditorWidget.cpp +++ b/interface/src/ui/ScriptEditorWidget.cpp @@ -71,7 +71,7 @@ bool ScriptEditorWidget::setRunning(bool run) { disconnect(this, SLOT(onScriptPrint(const QString&))); if (run) { - _scriptEngine = Application::getInstance()->loadScript(_currentScript, false); + _scriptEngine = Application::getInstance()->loadScript(_currentScript, true); connect(_scriptEngine, SIGNAL(runningStateChanged()), this, SIGNAL(runningStateChanged())); // Make new connections. diff --git a/interface/src/ui/ScriptEditorWindow.cpp b/interface/src/ui/ScriptEditorWindow.cpp index 00a3ef9f0c..26488f0223 100644 --- a/interface/src/ui/ScriptEditorWindow.cpp +++ b/interface/src/ui/ScriptEditorWindow.cpp @@ -180,11 +180,11 @@ void ScriptEditorWindow::closeEvent(QCloseEvent *event) { void ScriptEditorWindow::updateScriptNameOrStatus() { ScriptEditorWidget* source = (ScriptEditorWidget*)QObject::sender(); - QString modifiedStar = (source->isModified()?"*":""); + QString modifiedStar = (source->isModified()? "*" : ""); if (source->getScriptName().length() > 0) { for (int i = 0; i < _ScriptEditorWindowUI->tabWidget->count(); i++){ if (_ScriptEditorWindowUI->tabWidget->widget(i) == source) { - _ScriptEditorWindowUI->tabWidget->setTabText(i,modifiedStar + QFileInfo(source->getScriptName()).fileName()); + _ScriptEditorWindowUI->tabWidget->setTabText(i, modifiedStar + QFileInfo(source->getScriptName()).fileName()); _ScriptEditorWindowUI->tabWidget->setTabToolTip(i, source->getScriptName()); } } From 70ac93c20d8153ca32fbc7a6f49633462c6c17f8 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Mon, 21 Apr 2014 21:44:50 +0200 Subject: [PATCH 10/13] Style --- interface/src/ui/ScriptEditorWindow.cpp | 2 +- libraries/script-engine/src/ScriptEngine.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/interface/src/ui/ScriptEditorWindow.cpp b/interface/src/ui/ScriptEditorWindow.cpp index 26488f0223..ec63e0341d 100644 --- a/interface/src/ui/ScriptEditorWindow.cpp +++ b/interface/src/ui/ScriptEditorWindow.cpp @@ -179,7 +179,7 @@ void ScriptEditorWindow::closeEvent(QCloseEvent *event) { } void ScriptEditorWindow::updateScriptNameOrStatus() { - ScriptEditorWidget* source = (ScriptEditorWidget*)QObject::sender(); + ScriptEditorWidget* source = static_cast(QObject::sender()); QString modifiedStar = (source->isModified()? "*" : ""); if (source->getScriptName().length() > 0) { for (int i = 0; i < _ScriptEditorWindowUI->tabWidget->count(); i++){ diff --git a/libraries/script-engine/src/ScriptEngine.cpp b/libraries/script-engine/src/ScriptEngine.cpp index eeb1cebe09..2e92567fe7 100644 --- a/libraries/script-engine/src/ScriptEngine.cpp +++ b/libraries/script-engine/src/ScriptEngine.cpp @@ -45,7 +45,7 @@ static QScriptValue soundConstructor(QScriptContext* context, QScriptEngine* eng static QScriptValue debugPrint(QScriptContext* context, QScriptEngine* engine){ qDebug() << "script:print()<<" << context->argument(0).toString(); - engine->evaluate("Script.print('"+context->argument(0).toString()+"')"); + engine->evaluate("Script.print('" + context->argument(0).toString() + "')"); return QScriptValue(); } From 681aab5bf036a75383918a6822f1f01f9277e3b3 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 21 Apr 2014 16:17:45 -0700 Subject: [PATCH 11/13] Remove printf and replace them by qDebug() --- animation-server/src/AnimationServer.cpp | 36 +++++++++---------- .../octree/OctreeInboundPacketProcessor.cpp | 10 +++--- assignment-client/src/octree/OctreeServer.cpp | 18 +++++----- interface/src/Application.cpp | 6 ++-- interface/src/DatagramProcessor.cpp | 2 +- interface/src/Util.cpp | 2 +- interface/src/avatar/MyAvatar.cpp | 2 +- interface/src/devices/SixenseManager.cpp | 2 +- interface/src/voxels/VoxelSystem.cpp | 2 +- libraries/networking/src/Logging.cpp | 2 +- libraries/octree/src/Octree.cpp | 10 +++--- libraries/octree/src/Octree.h | 20 +++++------ .../octree/src/OctreeEditPacketSender.cpp | 4 +-- libraries/octree/src/OctreeElement.cpp | 4 +-- libraries/octree/src/OctreePacketData.cpp | 14 ++++---- .../octree/src/OctreeProjectedPolygon.cpp | 8 ++--- libraries/octree/src/Plane.cpp | 4 ++- libraries/particles/src/Particle.cpp | 28 +++++++-------- .../particles/src/ParticleTreeElement.cpp | 6 ++-- voxel-edit/src/SceneUtils.cpp | 8 ++--- 20 files changed, 95 insertions(+), 93 deletions(-) diff --git a/animation-server/src/AnimationServer.cpp b/animation-server/src/AnimationServer.cpp index ffb98b4ae0..32a95b48ea 100644 --- a/animation-server/src/AnimationServer.cpp +++ b/animation-server/src/AnimationServer.cpp @@ -171,7 +171,7 @@ static void renderMovingBug() { } } else { - //printf("bugPathCenter=(%f,%f,%f)\n", bugPathCenter.x, bugPathCenter.y, bugPathCenter.z); + //qDebug("bugPathCenter=(%f,%f,%f)", bugPathCenter.x, bugPathCenter.y, bugPathCenter.z); bugPathTheta += bugAngleDelta; // move slightly bugRotation -= bugAngleDelta; // rotate slightly @@ -187,12 +187,12 @@ static void renderMovingBug() { float y = bugPathCenter.y; bugPosition = glm::vec3(x, y, z); - //printf("bugPathTheta=%f\n", bugPathTheta); - //printf("bugRotation=%f\n", bugRotation); + //qDebug("bugPathTheta=%f", bugPathTheta); + //qDebug("bugRotation=%f", bugRotation); } - //printf("bugPosition=(%f,%f,%f)\n", bugPosition.x, bugPosition.y, bugPosition.z); - //printf("bugDirection=(%f,%f,%f)\n", bugDirection.x, bugDirection.y, bugDirection.z); + //qDebug("bugPosition=(%f,%f,%f)", bugPosition.x, bugPosition.y, bugPosition.z); + //qDebug("bugDirection=(%f,%f,%f)", bugDirection.x, bugDirection.y, bugDirection.z); // would be nice to add some randomness here... // Generate voxels for where bug is going to @@ -690,32 +690,32 @@ AnimationServer::AnimationServer(int &argc, char **argv) : // Handle Local Domain testing with the --local command line const char* NON_THREADED_PACKETSENDER = "--NonThreadedPacketSender"; ::nonThreadedPacketSender = cmdOptionExists(argc, (const char**) argv, NON_THREADED_PACKETSENDER); - printf("nonThreadedPacketSender=%s\n", debug::valueOf(::nonThreadedPacketSender)); + qDebug("nonThreadedPacketSender=%s", debug::valueOf(::nonThreadedPacketSender)); // Handle Local Domain testing with the --local command line const char* NO_BILLBOARD = "--NoBillboard"; ::includeBillboard = !cmdOptionExists(argc, (const char**) argv, NO_BILLBOARD); - printf("includeBillboard=%s\n", debug::valueOf(::includeBillboard)); + qDebug("includeBillboard=%s", debug::valueOf(::includeBillboard)); const char* NO_BORDER_TRACER = "--NoBorderTracer"; ::includeBorderTracer = !cmdOptionExists(argc, (const char**) argv, NO_BORDER_TRACER); - printf("includeBorderTracer=%s\n", debug::valueOf(::includeBorderTracer)); + qDebug("includeBorderTracer=%s", debug::valueOf(::includeBorderTracer)); const char* NO_MOVING_BUG = "--NoMovingBug"; ::includeMovingBug = !cmdOptionExists(argc, (const char**) argv, NO_MOVING_BUG); - printf("includeMovingBug=%s\n", debug::valueOf(::includeMovingBug)); + qDebug("includeMovingBug=%s", debug::valueOf(::includeMovingBug)); const char* INCLUDE_BLINKING_VOXEL = "--includeBlinkingVoxel"; ::includeBlinkingVoxel = cmdOptionExists(argc, (const char**) argv, INCLUDE_BLINKING_VOXEL); - printf("includeBlinkingVoxel=%s\n", debug::valueOf(::includeBlinkingVoxel)); + qDebug("includeBlinkingVoxel=%s", debug::valueOf(::includeBlinkingVoxel)); const char* NO_DANCE_FLOOR = "--NoDanceFloor"; ::includeDanceFloor = !cmdOptionExists(argc, (const char**) argv, NO_DANCE_FLOOR); - printf("includeDanceFloor=%s\n", debug::valueOf(::includeDanceFloor)); + qDebug("includeDanceFloor=%s", debug::valueOf(::includeDanceFloor)); const char* BUILD_STREET = "--BuildStreet"; ::buildStreet = cmdOptionExists(argc, (const char**) argv, BUILD_STREET); - printf("buildStreet=%s\n", debug::valueOf(::buildStreet)); + qDebug("buildStreet=%s", debug::valueOf(::buildStreet)); // Handle Local Domain testing with the --local command line const char* showPPS = "--showPPS"; @@ -725,7 +725,7 @@ AnimationServer::AnimationServer(int &argc, char **argv) : const char* local = "--local"; ::wantLocalDomain = cmdOptionExists(argc, (const char**) argv,local); if (::wantLocalDomain) { - printf("Local Domain MODE!\n"); + qDebug("Local Domain MODE!"); nodeList->getDomainHandler().setIPToLocalhost(); } @@ -738,7 +738,7 @@ AnimationServer::AnimationServer(int &argc, char **argv) : if (packetsPerSecondCommand) { ::packetsPerSecond = atoi(packetsPerSecondCommand); } - printf("packetsPerSecond=%d\n",packetsPerSecond); + qDebug("packetsPerSecond=%d",packetsPerSecond); const char* animateFPSCommand = getCmdOption(argc, (const char**) argv, "--AnimateFPS"); const char* animateIntervalCommand = getCmdOption(argc, (const char**) argv, "--AnimateInterval"); @@ -753,8 +753,8 @@ AnimationServer::AnimationServer(int &argc, char **argv) : ::ANIMATE_VOXELS_INTERVAL_USECS = (ANIMATE_FPS_IN_MILLISECONDS * 1000.0); // converts from milliseconds to usecs } } - printf("ANIMATE_FPS=%d\n",ANIMATE_FPS); - printf("ANIMATE_VOXELS_INTERVAL_USECS=%llu\n",ANIMATE_VOXELS_INTERVAL_USECS); + qDebug("ANIMATE_FPS=%d",ANIMATE_FPS); + qDebug("ANIMATE_VOXELS_INTERVAL_USECS=%llu",ANIMATE_VOXELS_INTERVAL_USECS); const char* processingFPSCommand = getCmdOption(argc, (const char**) argv, "--ProcessingFPS"); const char* processingIntervalCommand = getCmdOption(argc, (const char**) argv, "--ProcessingInterval"); @@ -769,8 +769,8 @@ AnimationServer::AnimationServer(int &argc, char **argv) : ::PROCESSING_INTERVAL_USECS = (PROCESSING_FPS_IN_MILLISECONDS * 1000.0) - FUDGE_USECS; // converts from milliseconds to usecs } } - printf("PROCESSING_FPS=%d\n",PROCESSING_FPS); - printf("PROCESSING_INTERVAL_USECS=%llu\n",PROCESSING_INTERVAL_USECS); + qDebug("PROCESSING_FPS=%d",PROCESSING_FPS); + qDebug("PROCESSING_INTERVAL_USECS=%llu",PROCESSING_INTERVAL_USECS); nodeList->linkedDataCreateCallback = NULL; // do we need a callback? diff --git a/assignment-client/src/octree/OctreeInboundPacketProcessor.cpp b/assignment-client/src/octree/OctreeInboundPacketProcessor.cpp index 41158bf339..545c502036 100644 --- a/assignment-client/src/octree/OctreeInboundPacketProcessor.cpp +++ b/assignment-client/src/octree/OctreeInboundPacketProcessor.cpp @@ -45,7 +45,7 @@ void OctreeInboundPacketProcessor::processPacket(const SharedNodePointer& sendin bool debugProcessPacket = _myServer->wantsVerboseDebug(); if (debugProcessPacket) { - printf("OctreeInboundPacketProcessor::processPacket() packetData=%p packetLength=%d\n", &packet, packet.size()); + qDebug("OctreeInboundPacketProcessor::processPacket() packetData=%p packetLength=%d", &packet, packet.size()); } int numBytesPacketHeader = numBytesForPacketHeader(packet); @@ -78,8 +78,8 @@ void OctreeInboundPacketProcessor::processPacket(const SharedNodePointer& sendin int maxSize = packet.size() - atByte; if (debugProcessPacket) { - printf("OctreeInboundPacketProcessor::processPacket() %c " - "packetData=%p packetLength=%d voxelData=%p atByte=%d maxSize=%d\n", + qDebug("OctreeInboundPacketProcessor::processPacket() %c " + "packetData=%p packetLength=%d voxelData=%p atByte=%d maxSize=%d", packetType, packetData, packet.size(), editData, atByte, maxSize); } @@ -105,8 +105,8 @@ void OctreeInboundPacketProcessor::processPacket(const SharedNodePointer& sendin } if (debugProcessPacket) { - printf("OctreeInboundPacketProcessor::processPacket() DONE LOOPING FOR %c " - "packetData=%p packetLength=%d voxelData=%p atByte=%d\n", + qDebug("OctreeInboundPacketProcessor::processPacket() DONE LOOPING FOR %c " + "packetData=%p packetLength=%d voxelData=%p atByte=%d", packetType, packetData, packet.size(), editData, atByte); } diff --git a/assignment-client/src/octree/OctreeServer.cpp b/assignment-client/src/octree/OctreeServer.cpp index 2e8a354c6a..21762f1534 100644 --- a/assignment-client/src/octree/OctreeServer.cpp +++ b/assignment-client/src/octree/OctreeServer.cpp @@ -486,7 +486,7 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url // encode float averageEncodeTime = getAverageEncodeTime(); - statsString += QString().sprintf(" Average encode time: %9.2f usecs\r\n", averageEncodeTime); + statsString += QString().sqDebug(" Average encode time: %9.2f usecs\r", averageEncodeTime); int allEncodeTimes = _noEncode + _shortEncode + _longEncode + _extraLongEncode; @@ -554,9 +554,9 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url noVsTotalSend * AS_PERCENT, _noSend); float averageNodeWaitTime = getAverageNodeWaitTime(); - statsString += QString().sprintf(" Average node lock wait time: %9.2f usecs\r\n", averageNodeWaitTime); + statsString += QString().sqDebug(" Average node lock wait time: %9.2f usecs\r", averageNodeWaitTime); - statsString += QString().sprintf("--------------------------------------------------------------\r\n"); + statsString += QString().sqDebug("--------------------------------------------------------------\r"); float encodeToInsidePercent = averageInsideTime == 0.0f ? 0.0f : (averageEncodeTime / averageInsideTime) * AS_PERCENT; statsString += QString().sprintf(" encode ratio: %5.2f%%\r\n", @@ -564,7 +564,7 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url float waitToInsidePercent = averageInsideTime == 0.0f ? 0.0f : ((averageTreeWaitTime + averageNodeWaitTime) / averageInsideTime) * AS_PERCENT; - statsString += QString().sprintf(" waiting ratio: %5.2f%%\r\n", waitToInsidePercent); + statsString += QString().sqDebug(" waiting ratio: %5.2f%%\r", waitToInsidePercent); float compressAndWriteToInsidePercent = averageInsideTime == 0.0f ? 0.0f : (averageCompressAndWriteTime / averageInsideTime) * AS_PERCENT; @@ -573,7 +573,7 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url float sendingToInsidePercent = averageInsideTime == 0.0f ? 0.0f : (averagePacketSendingTime / averageInsideTime) * AS_PERCENT; - statsString += QString().sprintf(" sending ratio: %5.2f%%\r\n", sendingToInsidePercent); + statsString += QString().sqDebug(" sending ratio: %5.2f%%\r", sendingToInsidePercent); @@ -615,7 +615,7 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url .arg(locale.toString((uint)totalPacketsProcessed).rightJustified(COLUMN_WIDTH, ' ')); statsString += QString(" Total Inbound Elements: %1 elements\r\n") .arg(locale.toString((uint)totalElementsProcessed).rightJustified(COLUMN_WIDTH, ' ')); - statsString += QString().sprintf(" Average Inbound Elements/Packet: %f elements/packet\r\n", averageElementsPerPacket); + statsString += QString().sqDebug(" Average Inbound Elements/Packet: %f elements/packet\r", averageElementsPerPacket); statsString += QString(" Average Transit Time/Packet: %1 usecs\r\n") .arg(locale.toString((uint)averageTransitTimePerPacket).rightJustified(COLUMN_WIDTH, ' ')); statsString += QString(" Average Process Time/Packet: %1 usecs\r\n") @@ -671,7 +671,7 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url // display memory usage stats statsString += "Current Memory Usage Statistics\r\n"; - statsString += QString().sprintf("\r\nOctreeElement size... %ld bytes\r\n", sizeof(OctreeElement)); + statsString += QString().sqDebug("\r\nOctreeElement size... %ld bytes\r", sizeof(OctreeElement)); statsString += "\r\n"; const char* memoryScaleLabel; @@ -738,8 +738,8 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url OctreeElement::getExternalChildrenCount(); statsString += " ----------------\r\n"; - statsString += QString().sprintf(" Total: %10.llu nodes\r\n", checkSum); - statsString += QString().sprintf(" Expected: %10.lu nodes\r\n", nodeCount); + statsString += QString().sqDebug(" Total: %10.llu nodes\r", checkSum); + statsString += QString().sqDebug(" Expected: %10.lu nodes\r", nodeCount); statsString += "\r\n"; statsString += "In other news....\r\n"; diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index cd07f21300..a885c80968 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3122,7 +3122,7 @@ void Application::nodeKilled(SharedNodePointer node) { VoxelPositionSize rootDetails; voxelDetailsForCode(rootCode, rootDetails); - printf("voxel server going away...... v[%f, %f, %f, %f]\n", + qDebug("voxel server going away...... v[%f, %f, %f, %f]", rootDetails.x, rootDetails.y, rootDetails.z, rootDetails.s); // Add the jurisditionDetails object to the list of "fade outs" @@ -3153,7 +3153,7 @@ void Application::nodeKilled(SharedNodePointer node) { VoxelPositionSize rootDetails; voxelDetailsForCode(rootCode, rootDetails); - printf("particle server going away...... v[%f, %f, %f, %f]\n", + qDebug("particle server going away...... v[%f, %f, %f, %f]", rootDetails.x, rootDetails.y, rootDetails.z, rootDetails.s); // Add the jurisditionDetails object to the list of "fade outs" @@ -3234,7 +3234,7 @@ int Application::parseOctreeStats(const QByteArray& packet, const SharedNodePoin if (jurisdiction->find(nodeUUID) == jurisdiction->end()) { - printf("stats from new server... v[%f, %f, %f, %f]\n", + qDebug("stats from new server... v[%f, %f, %f, %f]", rootDetails.x, rootDetails.y, rootDetails.z, rootDetails.s); // Add the jurisditionDetails object to the list of "fade outs" diff --git a/interface/src/DatagramProcessor.cpp b/interface/src/DatagramProcessor.cpp index 92260ad9dc..6525e79cf2 100644 --- a/interface/src/DatagramProcessor.cpp +++ b/interface/src/DatagramProcessor.cpp @@ -78,7 +78,7 @@ void DatagramProcessor::processDatagrams() { OCTREE_PACKET_SENT_TIME arrivedAt = usecTimestampNow(); int flightTime = arrivedAt - sentAt; - printf("got PacketType_VOXEL_DATA, sequence:%d flightTime:%d\n", sequence, flightTime); + qDebug("got PacketType_VOXEL_DATA, sequence:%d flightTime:%d", sequence, flightTime); } SharedNodePointer matchedNode = NodeList::getInstance()->sendingNodeForPacket(incomingPacket); diff --git a/interface/src/Util.cpp b/interface/src/Util.cpp index 1dae3a4fd6..febcb2f36e 100644 --- a/interface/src/Util.cpp +++ b/interface/src/Util.cpp @@ -63,7 +63,7 @@ void eulerToOrthonormals(glm::vec3 * angles, glm::vec3 * front, glm::vec3 * righ } void printVector(glm::vec3 vec) { - printf("%4.2f, %4.2f, %4.2f\n", vec.x, vec.y, vec.z); + qDebug("%4.2f, %4.2f, %4.2f", vec.x, vec.y, vec.z); } // Return the azimuth angle (in radians) between two points. diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 8229611646..317f408aa7 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -432,7 +432,7 @@ void MyAvatar::renderDebugBodyPoints() { glm::vec3 headPosition(getHead()->getEyePosition()); float torsoToHead = glm::length(headPosition - torsoPosition); glm::vec3 position; - printf("head-above-torso %.2f, scale = %0.2f\n", torsoToHead, getScale()); + qDebug("head-above-torso %.2f, scale = %0.2f", torsoToHead, getScale()); // Torso Sphere position = torsoPosition; diff --git a/interface/src/devices/SixenseManager.cpp b/interface/src/devices/SixenseManager.cpp index b950e6097c..f7c00411c1 100644 --- a/interface/src/devices/SixenseManager.cpp +++ b/interface/src/devices/SixenseManager.cpp @@ -96,7 +96,7 @@ void SixenseManager::update(float deltaTime) { hand->getPalms().push_back(newPalm); palm = &(hand->getPalms()[hand->getNumPalms() - 1]); palm->setSixenseID(data->controller_index); - printf("Found new Sixense controller, ID %i\n", data->controller_index); + qDebug("Found new Sixense controller, ID %i", data->controller_index); } palm->setActive(true); diff --git a/interface/src/voxels/VoxelSystem.cpp b/interface/src/voxels/VoxelSystem.cpp index 692f299bdc..8d59f33674 100644 --- a/interface/src/voxels/VoxelSystem.cpp +++ b/interface/src/voxels/VoxelSystem.cpp @@ -117,7 +117,7 @@ void VoxelSystem::elementDeleted(OctreeElement* element) { forceRemoveNodeFromArrays(voxel); } else { if (Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings)) { - printf("VoxelSystem::elementDeleted() while _voxelsInWriteArrays==0, is that expected? \n"); + qDebug("VoxelSystem::elementDeleted() while _voxelsInWriteArrays==0, is that expected? "); } } } diff --git a/libraries/networking/src/Logging.cpp b/libraries/networking/src/Logging.cpp index 17a4706de0..f42f1bda58 100644 --- a/libraries/networking/src/Logging.cpp +++ b/libraries/networking/src/Logging.cpp @@ -46,7 +46,7 @@ const HifiSockAddr& Logging::socket() { // use the first IP address _logstashSocket.setAddress(hostInfo.addresses().first()); } else { - printf("Failed to lookup logstash IP - will try again on next log attempt.\n"); + qDebug("Failed to lookup logstash IP - will try again on next log attempt."); } } diff --git a/libraries/octree/src/Octree.cpp b/libraries/octree/src/Octree.cpp index 696a65b4b5..d7fc7713cf 100644 --- a/libraries/octree/src/Octree.cpp +++ b/libraries/octree/src/Octree.cpp @@ -471,8 +471,8 @@ void Octree::processRemoveOctreeElementsBitstream(const unsigned char* bitstream int codeLength = numberOfThreeBitSectionsInCode(voxelCode, maxSize); if (codeLength == OVERFLOWED_OCTCODE_BUFFER) { - printf("WARNING! Got remove voxel bitstream that would overflow buffer in numberOfThreeBitSectionsInCode(), "); - printf("bailing processing of packet!\n"); + qDebug("WARNING! Got remove voxel bitstream that would overflow buffer in numberOfThreeBitSectionsInCode()," + " bailing processing of packet!"); break; } int voxelDataSize = bytesRequiredForCodeLength(codeLength) + SIZE_OF_COLOR_DATA; @@ -482,7 +482,7 @@ void Octree::processRemoveOctreeElementsBitstream(const unsigned char* bitstream voxelCode += voxelDataSize; atByte += voxelDataSize; } else { - printf("WARNING! Got remove voxel bitstream that would overflow buffer, bailing processing!\n"); + qDebug("WARNING! Got remove voxel bitstream that would overflow buffer, bailing processing!"); break; } } @@ -1404,7 +1404,7 @@ int Octree::encodeTreeBitstreamRecursion(OctreeElement* node, outputBits(childrenExistInPacketBits, false, true); printf(" childrenColored:"); outputBits(childrenColoredBits, false, true); - printf("\n"); + qDebug(""); **/ // if we were unable to fit this level in our packet, then rewind and add it to the node bag for @@ -1620,7 +1620,7 @@ void Octree::copyFromTreeIntoSubTree(Octree* sourceTree, OctreeElement* destinat } void dumpSetContents(const char* name, std::set set) { - printf("set %s has %ld elements\n", name, set.size()); + qDebug("set %s has %ld elements", name, set.size()); /* for (std::set::iterator i = set.begin(); i != set.end(); ++i) { printOctalCode(*i); diff --git a/libraries/octree/src/Octree.h b/libraries/octree/src/Octree.h index 839e61d1c2..3c868ab172 100644 --- a/libraries/octree/src/Octree.h +++ b/libraries/octree/src/Octree.h @@ -129,17 +129,17 @@ public: printf("StopReason: "); switch (stopReason) { default: - case UNKNOWN: printf("UNKNOWN\n"); break; + case UNKNOWN: qDebug("UNKNOWN"); break; - case DIDNT_FIT: printf("DIDNT_FIT\n"); break; - case NULL_NODE: printf("NULL_NODE\n"); break; - case TOO_DEEP: printf("TOO_DEEP\n"); break; - case OUT_OF_JURISDICTION: printf("OUT_OF_JURISDICTION\n"); break; - case LOD_SKIP: printf("LOD_SKIP\n"); break; - case OUT_OF_VIEW: printf("OUT_OF_VIEW\n"); break; - case WAS_IN_VIEW: printf("WAS_IN_VIEW\n"); break; - case NO_CHANGE: printf("NO_CHANGE\n"); break; - case OCCLUDED: printf("OCCLUDED\n"); break; + case DIDNT_FIT: qDebug("DIDNT_FIT"); break; + case NULL_NODE: qDebug("NULL_NODE"); break; + case TOO_DEEP: qDebug("TOO_DEEP"); break; + case OUT_OF_JURISDICTION: qDebug("OUT_OF_JURISDICTION"); break; + case LOD_SKIP: qDebug("LOD_SKIP"); break; + case OUT_OF_VIEW: qDebug("OUT_OF_VIEW"); break; + case WAS_IN_VIEW: qDebug("WAS_IN_VIEW"); break; + case NO_CHANGE: qDebug("NO_CHANGE"); break; + case OCCLUDED: qDebug("OCCLUDED"); break; } } }; diff --git a/libraries/octree/src/OctreeEditPacketSender.cpp b/libraries/octree/src/OctreeEditPacketSender.cpp index b606dffe44..7e59b3ae7f 100644 --- a/libraries/octree/src/OctreeEditPacketSender.cpp +++ b/libraries/octree/src/OctreeEditPacketSender.cpp @@ -37,7 +37,7 @@ OctreeEditPacketSender::OctreeEditPacketSender() : _serverJurisdictions(NULL), _sequenceNumber(0), _maxPacketSize(MAX_PACKET_SIZE) { - //printf("OctreeEditPacketSender::OctreeEditPacketSender() [%p] created... \n", this); + //qDebug("OctreeEditPacketSender::OctreeEditPacketSender() [%p] created... ", this); } OctreeEditPacketSender::~OctreeEditPacketSender() { @@ -53,7 +53,7 @@ OctreeEditPacketSender::~OctreeEditPacketSender() { _preServerPackets.erase(_preServerPackets.begin()); } _pendingPacketsLock.unlock(); - //printf("OctreeEditPacketSender::~OctreeEditPacketSender() [%p] destroyed... \n", this); + //qDebug("OctreeEditPacketSender::~OctreeEditPacketSender() [%p] destroyed... ", this); } diff --git a/libraries/octree/src/OctreeElement.cpp b/libraries/octree/src/OctreeElement.cpp index e21148df5e..96311ceabd 100644 --- a/libraries/octree/src/OctreeElement.cpp +++ b/libraries/octree/src/OctreeElement.cpp @@ -204,7 +204,7 @@ void OctreeElement::calculateAABox() { void OctreeElement::deleteChildAtIndex(int childIndex) { OctreeElement* childAt = getChildAtIndex(childIndex); if (childAt) { - //printf("deleteChildAtIndex()... about to call delete childAt=%p\n",childAt); + //qDebug("deleteChildAtIndex()... about to call delete childAt=%p",childAt); delete childAt; setChildAtIndex(childIndex, NULL); _isDirty = true; @@ -1315,7 +1315,7 @@ OctreeElement* OctreeElement::getOrCreateChildElementAt(float x, float y, float float halfOurScale = ourScale / 2.0f; if(s > ourScale) { - printf("UNEXPECTED -- OctreeElement::getOrCreateChildElementAt() s=[%f] > ourScale=[%f] \n", s, ourScale); + qDebug("UNEXPECTED -- OctreeElement::getOrCreateChildElementAt() s=[%f] > ourScale=[%f] ", s, ourScale); } if (s > halfOurScale) { diff --git a/libraries/octree/src/OctreePacketData.cpp b/libraries/octree/src/OctreePacketData.cpp index 2edf560de6..b54a87bef8 100644 --- a/libraries/octree/src/OctreePacketData.cpp +++ b/libraries/octree/src/OctreePacketData.cpp @@ -125,7 +125,7 @@ const unsigned char* OctreePacketData::getFinalizedData() { if (_dirty) { if (_debug) { - printf("getFinalizedData() _compressedBytes=%d _bytesInUse=%d\n",_compressedBytes, _bytesInUse); + qDebug("getFinalizedData() _compressedBytes=%d _bytesInUse=%d",_compressedBytes, _bytesInUse); } compressContent(); } @@ -139,7 +139,7 @@ int OctreePacketData::getFinalizedSize() { if (_dirty) { if (_debug) { - printf("getFinalizedSize() _compressedBytes=%d _bytesInUse=%d\n",_compressedBytes, _bytesInUse); + qDebug("getFinalizedSize() _compressedBytes=%d _bytesInUse=%d",_compressedBytes, _bytesInUse); } compressContent(); } @@ -187,7 +187,7 @@ void OctreePacketData::discardLevel(LevelDetails key) { _totalBytesOfColor -= reduceBytesOfColor; if (_debug) { - printf("discardLevel() BEFORE _dirty=%s bytesInLevel=%d _compressedBytes=%d _bytesInUse=%d\n", + qDebug("discardLevel() BEFORE _dirty=%s bytesInLevel=%d _compressedBytes=%d _bytesInUse=%d", debug::valueOf(_dirty), bytesInLevel, _compressedBytes, _bytesInUse); } @@ -196,7 +196,7 @@ void OctreePacketData::discardLevel(LevelDetails key) { _dirty = true; if (_debug) { - printf("discardLevel() AFTER _dirty=%s bytesInLevel=%d _compressedBytes=%d _bytesInUse=%d\n", + qDebug("discardLevel() AFTER _dirty=%s bytesInLevel=%d _compressedBytes=%d _bytesInUse=%d", debug::valueOf(_dirty), bytesInLevel, _compressedBytes, _bytesInUse); } } @@ -413,13 +413,13 @@ void OctreePacketData::loadFinalizedContent(const unsigned char* data, int lengt } } else { if (_debug) { - printf("OctreePacketData::loadCompressedContent()... length = 0, nothing to do...\n"); + qDebug("OctreePacketData::loadCompressedContent()... length = 0, nothing to do..."); } } } void OctreePacketData::debugContent() { - printf("OctreePacketData::debugContent()... COMPRESSED DATA.... size=%d\n",_compressedBytes); + qDebug("OctreePacketData::debugContent()... COMPRESSED DATA.... size=%d",_compressedBytes); int perline=0; for (int i = 0; i < _compressedBytes; i++) { printf("%.2x ",_compressed[i]); @@ -431,7 +431,7 @@ void OctreePacketData::debugContent() { } printf("\n"); - printf("OctreePacketData::debugContent()... UNCOMPRESSED DATA.... size=%d\n",_bytesInUse); + qDebug("OctreePacketData::debugContent()... UNCOMPRESSED DATA.... size=%d",_bytesInUse); perline=0; for (int i = 0; i < _bytesInUse; i++) { printf("%.2x ",_uncompressed[i]); diff --git a/libraries/octree/src/OctreeProjectedPolygon.cpp b/libraries/octree/src/OctreeProjectedPolygon.cpp index f86d598a87..92a5b2ac18 100644 --- a/libraries/octree/src/OctreeProjectedPolygon.cpp +++ b/libraries/octree/src/OctreeProjectedPolygon.cpp @@ -263,12 +263,12 @@ bool OctreeProjectedPolygon::pointInside(const glm::vec2& point, bool* matchesVe } void OctreeProjectedPolygon::printDebugDetails() const { - printf("OctreeProjectedPolygon..."); - printf(" minX=%f maxX=%f minY=%f maxY=%f\n", getMinX(), getMaxX(), getMinY(), getMaxY()); - printf(" vertex count=%d distance=%f\n", getVertexCount(), getDistance()); + qDebug("OctreeProjectedPolygon..." + " minX=%f maxX=%f minY=%f maxY=%f", getMinX(), getMaxX(), getMinY(), getMaxY()); + qDebug(" vertex count=%d distance=%f", getVertexCount(), getDistance()); for (int i = 0; i < getVertexCount(); i++) { glm::vec2 point = getVertex(i); - printf(" vertex[%d] = %f, %f \n", i, point.x, point.y); + qDebug(" vertex[%d] = %f, %f ", i, point.x, point.y); } } diff --git a/libraries/octree/src/Plane.cpp b/libraries/octree/src/Plane.cpp index d7cb2c54ff..d9e5633233 100755 --- a/libraries/octree/src/Plane.cpp +++ b/libraries/octree/src/Plane.cpp @@ -14,6 +14,8 @@ #include "Plane.h" +#include + #include void Plane::set3Points(const glm::vec3 &v1, const glm::vec3 &v2, const glm::vec3 &v3) { @@ -61,6 +63,6 @@ float Plane::distance(const glm::vec3 &point) const { } void Plane::print() const { - printf("Plane - point (x=%f y=%f z=%f) normal (x=%f y=%f z=%f) d=%f\n", + qDebug("Plane - point (x=%f y=%f z=%f) normal (x=%f y=%f z=%f) d=%f", _point.x, _point.y, _point.z, _normal.x, _normal.y, _normal.z, _dCoefficient); } diff --git a/libraries/particles/src/Particle.cpp b/libraries/particles/src/Particle.cpp index a36998aad4..59265c00dc 100644 --- a/libraries/particles/src/Particle.cpp +++ b/libraries/particles/src/Particle.cpp @@ -146,7 +146,7 @@ bool Particle::appendParticleData(OctreePacketData* packetData) const { bool success = packetData->appendValue(getID()); - //printf("Particle::appendParticleData()... getID()=%d\n", getID()); + //qDebug("Particle::appendParticleData()... getID()=%d", getID()); if (success) { success = packetData->appendValue(getAge()); @@ -541,15 +541,15 @@ Particle Particle::fromEditPacket(const unsigned char* data, int length, int& pr } void Particle::debugDump() const { - printf("Particle id :%u\n", _id); - printf(" age:%f\n", getAge()); - printf(" edited ago:%f\n", getEditedAgo()); - printf(" should die:%s\n", debug::valueOf(getShouldDie())); - printf(" position:%f,%f,%f\n", _position.x, _position.y, _position.z); - printf(" radius:%f\n", getRadius()); - printf(" velocity:%f,%f,%f\n", _velocity.x, _velocity.y, _velocity.z); - printf(" gravity:%f,%f,%f\n", _gravity.x, _gravity.y, _gravity.z); - printf(" color:%d,%d,%d\n", _color[0], _color[1], _color[2]); + qDebug("Particle id :%u", _id); + qDebug(" age:%f", getAge()); + qDebug(" edited ago:%f", getEditedAgo()); + qDebug(" should die:%s", debug::valueOf(getShouldDie())); + qDebug(" position:%f,%f,%f", _position.x, _position.y, _position.z); + qDebug(" radius:%f", getRadius()); + qDebug(" velocity:%f,%f,%f", _velocity.x, _velocity.y, _velocity.z); + qDebug(" gravity:%f,%f,%f", _gravity.x, _gravity.y, _gravity.z); + qDebug(" color:%d,%d,%d", _color[0], _color[1], _color[2]); } bool Particle::encodeParticleEditMessageDetails(PacketType command, ParticleID id, const ParticleProperties& properties, @@ -732,9 +732,9 @@ bool Particle::encodeParticleEditMessageDetails(PacketType command, ParticleID i bool wantDebugging = false; if (wantDebugging) { - printf("encodeParticleEditMessageDetails()....\n"); - printf("Particle id :%u\n", id.id); - printf(" nextID:%u\n", _nextID); + qDebug("encodeParticleEditMessageDetails()...."); + qDebug("Particle id :%u", id.id); + qDebug(" nextID:%u", _nextID); } // cleanup @@ -844,7 +844,7 @@ void Particle::update(const quint64& now) { // handle damping glm::vec3 dampingResistance = _velocity * _damping; _velocity -= dampingResistance * timeElapsed; - //printf("applying damping to Particle timeElapsed=%f\n",timeElapsed); + //qDebug("applying damping to Particle timeElapsed=%f",timeElapsed); } } diff --git a/libraries/particles/src/ParticleTreeElement.cpp b/libraries/particles/src/ParticleTreeElement.cpp index 19353c427a..d28ccf2f5e 100644 --- a/libraries/particles/src/ParticleTreeElement.cpp +++ b/libraries/particles/src/ParticleTreeElement.cpp @@ -142,7 +142,7 @@ bool ParticleTreeElement::updateParticle(const Particle& particle) { bool localOlder = thisParticle.getLastUpdated() < particle.getLastUpdated(); if (changedOnServer || localOlder) { if (wantDebug) { - printf("local particle [id:%d] %s and %s than server particle by %d, particle.isNewlyCreated()=%s\n", + qDebug("local particle [id:%d] %s and %s than server particle by %d, particle.isNewlyCreated()=%s", particle.getID(), (changedOnServer ? "CHANGED" : "same"), (localOlder ? "OLDER" : "NEWER"), difference, debug::valueOf(particle.isNewlyCreated()) ); @@ -150,8 +150,8 @@ bool ParticleTreeElement::updateParticle(const Particle& particle) { thisParticle.copyChangedProperties(particle); } else { if (wantDebug) { - printf(">>> IGNORING SERVER!!! Would've caused jutter! <<< " - "local particle [id:%d] %s and %s than server particle by %d, particle.isNewlyCreated()=%s\n", + qDebug(">>> IGNORING SERVER!!! Would've caused jutter! <<< " + "local particle [id:%d] %s and %s than server particle by %d, particle.isNewlyCreated()=%s", particle.getID(), (changedOnServer ? "CHANGED" : "same"), (localOlder ? "OLDER" : "NEWER"), difference, debug::valueOf(particle.isNewlyCreated()) ); diff --git a/voxel-edit/src/SceneUtils.cpp b/voxel-edit/src/SceneUtils.cpp index 19e227c71a..937b03cc39 100644 --- a/voxel-edit/src/SceneUtils.cpp +++ b/voxel-edit/src/SceneUtils.cpp @@ -20,7 +20,7 @@ void addCornersAndAxisLines(VoxelTree* tree) { float voxelSize = 0.5f / TREE_SCALE; // Now some more examples... a little more complex - printf("creating corner points...\n"); + qDebug("creating corner points..."); tree->createVoxel(0 , 0 , 0 , voxelSize, 255, 255 ,255); tree->createVoxel(1.0 - voxelSize, 0 , 0 , voxelSize, 255, 0 ,0 ); tree->createVoxel(0 , 1.0 - voxelSize, 0 , voxelSize, 0 , 255 ,0 ); @@ -29,11 +29,11 @@ void addCornersAndAxisLines(VoxelTree* tree) { tree->createVoxel(0 , 1.0 - voxelSize, 1.0 - voxelSize, voxelSize, 0 , 255 ,255); tree->createVoxel(1.0 - voxelSize, 1.0 - voxelSize, 0 , voxelSize, 255, 255 ,0 ); tree->createVoxel(1.0 - voxelSize, 1.0 - voxelSize, 1.0 - voxelSize, voxelSize, 255, 255 ,255); - printf("DONE creating corner points...\n"); + qDebug("DONE creating corner points..."); } void addSurfaceScene(VoxelTree * tree) { - printf("adding surface scene...\n"); + qDebug("adding surface scene..."); float voxelSize = 1.f / (8 * TREE_SCALE); // color 1= blue, color 2=green @@ -57,5 +57,5 @@ void addSurfaceScene(VoxelTree * tree) { } } } - printf("DONE adding surface scene...\n"); + qDebug("DONE adding surface scene..."); } From cfc778d10c3c3fddffb6acdaa89b3eb9b6f0185a Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 21 Apr 2014 16:32:17 -0700 Subject: [PATCH 12/13] Fix regexp fail --- assignment-client/src/octree/OctreeServer.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/assignment-client/src/octree/OctreeServer.cpp b/assignment-client/src/octree/OctreeServer.cpp index 21762f1534..2e8a354c6a 100644 --- a/assignment-client/src/octree/OctreeServer.cpp +++ b/assignment-client/src/octree/OctreeServer.cpp @@ -486,7 +486,7 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url // encode float averageEncodeTime = getAverageEncodeTime(); - statsString += QString().sqDebug(" Average encode time: %9.2f usecs\r", averageEncodeTime); + statsString += QString().sprintf(" Average encode time: %9.2f usecs\r\n", averageEncodeTime); int allEncodeTimes = _noEncode + _shortEncode + _longEncode + _extraLongEncode; @@ -554,9 +554,9 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url noVsTotalSend * AS_PERCENT, _noSend); float averageNodeWaitTime = getAverageNodeWaitTime(); - statsString += QString().sqDebug(" Average node lock wait time: %9.2f usecs\r", averageNodeWaitTime); + statsString += QString().sprintf(" Average node lock wait time: %9.2f usecs\r\n", averageNodeWaitTime); - statsString += QString().sqDebug("--------------------------------------------------------------\r"); + statsString += QString().sprintf("--------------------------------------------------------------\r\n"); float encodeToInsidePercent = averageInsideTime == 0.0f ? 0.0f : (averageEncodeTime / averageInsideTime) * AS_PERCENT; statsString += QString().sprintf(" encode ratio: %5.2f%%\r\n", @@ -564,7 +564,7 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url float waitToInsidePercent = averageInsideTime == 0.0f ? 0.0f : ((averageTreeWaitTime + averageNodeWaitTime) / averageInsideTime) * AS_PERCENT; - statsString += QString().sqDebug(" waiting ratio: %5.2f%%\r", waitToInsidePercent); + statsString += QString().sprintf(" waiting ratio: %5.2f%%\r\n", waitToInsidePercent); float compressAndWriteToInsidePercent = averageInsideTime == 0.0f ? 0.0f : (averageCompressAndWriteTime / averageInsideTime) * AS_PERCENT; @@ -573,7 +573,7 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url float sendingToInsidePercent = averageInsideTime == 0.0f ? 0.0f : (averagePacketSendingTime / averageInsideTime) * AS_PERCENT; - statsString += QString().sqDebug(" sending ratio: %5.2f%%\r", sendingToInsidePercent); + statsString += QString().sprintf(" sending ratio: %5.2f%%\r\n", sendingToInsidePercent); @@ -615,7 +615,7 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url .arg(locale.toString((uint)totalPacketsProcessed).rightJustified(COLUMN_WIDTH, ' ')); statsString += QString(" Total Inbound Elements: %1 elements\r\n") .arg(locale.toString((uint)totalElementsProcessed).rightJustified(COLUMN_WIDTH, ' ')); - statsString += QString().sqDebug(" Average Inbound Elements/Packet: %f elements/packet\r", averageElementsPerPacket); + statsString += QString().sprintf(" Average Inbound Elements/Packet: %f elements/packet\r\n", averageElementsPerPacket); statsString += QString(" Average Transit Time/Packet: %1 usecs\r\n") .arg(locale.toString((uint)averageTransitTimePerPacket).rightJustified(COLUMN_WIDTH, ' ')); statsString += QString(" Average Process Time/Packet: %1 usecs\r\n") @@ -671,7 +671,7 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url // display memory usage stats statsString += "Current Memory Usage Statistics\r\n"; - statsString += QString().sqDebug("\r\nOctreeElement size... %ld bytes\r", sizeof(OctreeElement)); + statsString += QString().sprintf("\r\nOctreeElement size... %ld bytes\r\n", sizeof(OctreeElement)); statsString += "\r\n"; const char* memoryScaleLabel; @@ -738,8 +738,8 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url OctreeElement::getExternalChildrenCount(); statsString += " ----------------\r\n"; - statsString += QString().sqDebug(" Total: %10.llu nodes\r", checkSum); - statsString += QString().sqDebug(" Expected: %10.lu nodes\r", nodeCount); + statsString += QString().sprintf(" Total: %10.llu nodes\r\n", checkSum); + statsString += QString().sprintf(" Expected: %10.lu nodes\r\n", nodeCount); statsString += "\r\n"; statsString += "In other news....\r\n"; From 6c89521f1d136486fde387c256e85c7c31650701 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 22 Apr 2014 13:12:21 +0200 Subject: [PATCH 13/13] Style --- interface/src/ScriptHighlighting.cpp | 6 +++--- interface/src/ui/ScriptEditorWidget.cpp | 2 +- interface/src/ui/ScriptEditorWindow.cpp | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/interface/src/ScriptHighlighting.cpp b/interface/src/ScriptHighlighting.cpp index 9b58298b62..3ab1394097 100644 --- a/interface/src/ScriptHighlighting.cpp +++ b/interface/src/ScriptHighlighting.cpp @@ -29,7 +29,7 @@ void ScriptHighlighting::highlightBlock(const QString& text) { this->formatNumbers(text); this->formatTrueFalse(text); this->formatQoutedText(text); - this->formatComments(text); + this->formatComments(text); } void ScriptHighlighting::highlightKeywords(const QString& text) { @@ -54,7 +54,7 @@ void ScriptHighlighting::formatComments(const QString& text) { start = text.indexOf(_multiLineCommentBegin, start + length); if (end == -1) { setCurrentBlockState(BlockStateInMultiComment); - } + } } int index = _singleLineComment.indexIn(text); @@ -92,4 +92,4 @@ void ScriptHighlighting::formatTrueFalse(const QString& text){ setFormat(index, length, *font); index = _truefalseRegex.indexIn(text, index + length); } -} \ No newline at end of file +} diff --git a/interface/src/ui/ScriptEditorWidget.cpp b/interface/src/ui/ScriptEditorWidget.cpp index 74b74e1ad6..1765a5ea1a 100644 --- a/interface/src/ui/ScriptEditorWidget.cpp +++ b/interface/src/ui/ScriptEditorWidget.cpp @@ -155,4 +155,4 @@ void ScriptEditorWidget::onScriptError(const QString& message) { void ScriptEditorWidget::onScriptPrint(const QString& message) { _scriptEditorWidgetUI->debugText->appendPlainText("> " + message); -} \ No newline at end of file +} diff --git a/interface/src/ui/ScriptEditorWindow.cpp b/interface/src/ui/ScriptEditorWindow.cpp index ec63e0341d..0c34959353 100644 --- a/interface/src/ui/ScriptEditorWindow.cpp +++ b/interface/src/ui/ScriptEditorWindow.cpp @@ -197,4 +197,4 @@ void ScriptEditorWindow::updateScriptNameOrStatus() { this->setWindowTitle("Script Editor [New script" + modifiedStar + "]"); } } -} \ No newline at end of file +}