automatically open include files in new tabs; show in syntax highlighting, whether a file is readable

master
Marc Wäckerlin 8 years ago
parent 1a91f9bf64
commit 54323a1cb6
  1. 26
      src/editor.hxx
  2. 4
      src/makefile.am
  3. 41
      src/testgui.hxx
  4. 6
      src/testgui.ui

@ -9,9 +9,12 @@
#include <QSyntaxHighlighter>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <cassert>
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();
}

@ -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}

@ -10,6 +10,7 @@
#include <webpage.hxx>
#include <editor.hxx>
#include <commands.hxx>
#include <scriptfile.hxx>
#include <QMainWindow>
#include <QSettings>
#include <QWebFrame>
@ -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<QDockWidget*>(w));
w=qobject_cast<QWidget*>(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<QString, ScriptFile*> _testscripts;
};
#endif // TESTGUI_HXX

@ -134,7 +134,7 @@
<widget class="QStatusBar" name="statusbar"/>
<widget class="QDockWidget" name="_domDock">
<property name="windowTitle">
<string>DOM Tree</string>
<string>D&amp;OM Tree</string>
</property>
<attribute name="dockWidgetArea">
<number>2</number>
@ -158,7 +158,7 @@
</widget>
<widget class="QDockWidget" name="_sourceDock">
<property name="windowTitle">
<string>HTML Source</string>
<string>HTML &amp;Source</string>
</property>
<attribute name="dockWidgetArea">
<number>8</number>
@ -596,7 +596,7 @@ this.dispatchEvent(evObj);</string>
</layout>
</item>
<item>
<widget class="QPlainTextEdit" name="_setupscript"/>
<widget class="CodeEditor" name="_setupscript"/>
</item>
</layout>
</widget>

Loading…
Cancel
Save