diff --git a/src/editor.hxx b/src/editor.hxx index d6dd619..97ddcf5 100644 --- a/src/editor.hxx +++ b/src/editor.hxx @@ -9,9 +9,12 @@ #include #include #include +#include class Highlighter: public QSyntaxHighlighter { Q_OBJECT; + Q_SIGNALS: + void include(QString); public: Highlighter(QTextDocument *parent): QSyntaxHighlighter(parent) { QString commands="auth|ca-certificate|call|case|check|clear-cookies|click|clicktype|client-certificate|do|download|echo|execute|exists|exit|expect|fail|for|function|if|ignoreto|include|label|load|not|offline-storage-path|open|screenshot|set|setvalue|sleep|testcase|testsuite|timeout|unset|upload"; @@ -20,6 +23,18 @@ class Highlighter: public QSyntaxHighlighter { } protected: void highlightBlock(const QString &text) { + static QRegularExpression inc("^ *include +([^ ].*.\\.wt)"); + QRegularExpressionMatch m(inc.match(text)); + if (m.hasMatch()) { + QTextCharFormat fmt; + if (QFile(m.captured(1)).exists()) { + fmt.setForeground(Qt::darkGreen); + } else { + fmt.setForeground(Qt::darkRed); + } + setFormat(m.capturedStart(1), m.capturedLength(1), fmt); + include(m.captured(1)); + } for (auto e: _expressions) { auto m(e.re.match(text)); for (int i(0); i<=m.lastCapturedIndex(); ++i) { @@ -53,13 +68,16 @@ class LineNumberArea: public QWidget { class CodeEditor: public QPlainTextEdit { Q_OBJECT; + Q_SIGNALS: + void include(QString); public: CodeEditor(QWidget *parent = 0): QPlainTextEdit(parent) { - new Highlighter(document()); + Highlighter *highlighter(new Highlighter(document())); lineNumberArea = new LineNumberArea(this); - connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int))); - connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int))); - connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); + assert(connect(this, SIGNAL(blockCountChanged(int)), SLOT(updateLineNumberAreaWidth(int)))); + assert(connect(this, SIGNAL(updateRequest(QRect,int)), SLOT(updateLineNumberArea(QRect,int)))); + assert(connect(this, SIGNAL(cursorPositionChanged()), SLOT(highlightCurrentLine()))); + assert(connect(highlighter, SIGNAL(include(QString)), SIGNAL(include(QString)))); updateLineNumberAreaWidth(0); highlightCurrentLine(); } diff --git a/src/makefile.am b/src/makefile.am index 19cfa52..adb16bd 100644 --- a/src/makefile.am +++ b/src/makefile.am @@ -8,9 +8,9 @@ bin_PROGRAMS = webtester webrunner -webtester_MOCFILES = moc_testgui.cxx moc_commands.cxx moc_webpage.cxx \ +webtester_MOCFILES = moc_testgui.cxx moc_scriptfile.cxx moc_commands.cxx moc_webpage.cxx \ moc_networkaccessmanager.cxx moc_editor.cxx -webtester_UIFILES = ui_testgui.hxx +webtester_UIFILES = ui_testgui.hxx ui_scriptfile.hxx webtester_SOURCES = version.cxx webtester.cxx exceptions.hxx version.hxx \ ${webtester_MOCFILES} ${webtester_UIFILES} diff --git a/src/testgui.hxx b/src/testgui.hxx index f74c719..22fb565 100644 --- a/src/testgui.hxx +++ b/src/testgui.hxx @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -63,11 +64,12 @@ class TestGUI: public QMainWindow, protected Ui::TestGUI { _web->installEventFilter(this); // track mouse and keyboard page->setForwardUnsupportedContent(true); _commands->setText(Script().commands(Script::HTML)); - connect(page, SIGNAL(uploadFile(QString)), SLOT(uploadFile(QString))); - connect(page, SIGNAL(unsupportedContent(QNetworkReply*)), - SLOT(unsupportedContent(QNetworkReply*))); - connect(page, SIGNAL(downloadRequested(const QNetworkRequest&)), - SLOT(downloadRequested(const QNetworkRequest&))); + assert(connect(page, SIGNAL(uploadFile(QString)), SLOT(uploadFile(QString)))); + assert(connect(page, SIGNAL(unsupportedContent(QNetworkReply*)), + SLOT(unsupportedContent(QNetworkReply*)))); + assert(connect(page, SIGNAL(downloadRequested(const QNetworkRequest&)), + SLOT(downloadRequested(const QNetworkRequest&)))); + assert(connect(_testscript, SIGNAL(include(QString)), SLOT(include(QString)))); if (setupScript.size()) loadSetup(setupScript); if (scriptFile.size()) loadFile(scriptFile); } @@ -338,6 +340,32 @@ class TestGUI: public QMainWindow, protected Ui::TestGUI { if (!vb) return; vb->setValue(vb->maximum()); } + void include(QString name) { + if (_testscripts.contains(name)) return; + _testscripts[name] = new ScriptFile(this); + assert(connect(_testscripts[name], SIGNAL(include(QString)), SLOT(include(QString)))); + assert(connect(_testscripts[name], SIGNAL(close(ScriptFile*)), SLOT(remove(ScriptFile*)))); + QFile file(name); + try { + if (!file.open(QIODevice::ReadOnly|QIODevice::Text)) + throw std::runtime_error("file open failed"); + _testscripts[name]->editor()->setPlainText(QString::fromUtf8(file.readAll())); + if (file.error()!=QFileDevice::NoError) + throw std::runtime_error("file read failed"); + _testscripts[name]->name(name); + tabifyDockWidget(_scriptDock, _testscripts[name]); + QDockWidget* d(0); + for (QWidget* w(QApplication::focusWidget()); w&&!(d=qobject_cast(w)); + w=qobject_cast(w->parent())); + if (d) d->raise(); + } catch(const std::exception& x) { + remove(_testscripts[name]); + } + } + void remove(ScriptFile* scriptfile) { + _testscripts.remove(scriptfile->name()); + delete scriptfile; + } protected: void closeEvent(QCloseEvent* event) { QSettings settings("mrw", "webtester"); @@ -452,7 +480,7 @@ class TestGUI: public QMainWindow, protected Ui::TestGUI { void loadFile(QString name) { QFile file(name); try { - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + if (!file.open(QIODevice::ReadOnly|QIODevice::Text)) throw std::runtime_error("file open failed"); _testscript->setPlainText(QString::fromUtf8(file.readAll())); if (file.error()!=QFileDevice::NoError) @@ -900,6 +928,7 @@ class TestGUI: public QMainWindow, protected Ui::TestGUI { bool _typing; // user is typing bool _inEventFilter; // actually handling event filter Script _setupScript; + QMap _testscripts; }; #endif // TESTGUI_HXX diff --git a/src/testgui.ui b/src/testgui.ui index 6334dbd..b8f764a 100644 --- a/src/testgui.ui +++ b/src/testgui.ui @@ -134,7 +134,7 @@ - DOM Tree + D&OM Tree 2 @@ -158,7 +158,7 @@ - HTML Source + HTML &Source 8 @@ -596,7 +596,7 @@ this.dispatchEvent(evObj); - +