added CodeEditor with line numbering from Qt sample code

master
Marc Wäckerlin 7 years ago
parent 32f090c2f0
commit cb040f47a1
  1. 2
      COPYING
  2. 35
      ChangeLog
  3. 2
      INSTALL
  4. 107
      src/editor.hxx
  5. 2
      src/makefile.am
  6. 1
      src/testgui.hxx
  7. 47
      src/testgui.ui

@ -1 +1 @@
/usr/share/automake-1.14/COPYING
/usr/share/automake-1.15/COPYING

@ -1,3 +1,38 @@
2017-01-31 17:32
* [r97] src/commands.hxx, src/webrunner.cxx:
better progress indicator
2017-01-30 15:09
* [r96] src/testgui.hxx, src/testgui.ui, src/webtester.cxx:
fixed window title and file modification dialog
2017-01-27 15:57
* [r95] src/testgui.hxx, src/testgui.ui:
url as combobox with completion
2017-01-24 14:17
* [r94] src/commands.hxx, src/testgui.hxx, src/testgui.ui:
show test result as check mark or cross
2017-01-20 10:58
* [r93] doc/footer.html.in[ADD], doc/header.html.in[ADD],
doc/plantuml.jar[ADD], doc/style.css[ADD],
makefile_test.inc.am[ADD]:
packager fixed - fix
2017-01-20 10:57
* [r92] ChangeLog, bootstrap.sh, build-in-docker.conf,
debian/changelog.in, doc/footer.html.in[DEL],
doc/header.html.in[DEL], doc/plantuml.jar[DEL],
doc/style.css[DEL], makefile_test.inc.am[DEL]:
packager fixed
2017-01-20 08:30
* [r91] COPYING, INSTALL, README, configure.ac, src/commands.hxx,

@ -1 +1 @@
/usr/share/automake-1.14/INSTALL
/usr/share/automake-1.15/INSTALL

@ -0,0 +1,107 @@
#ifndef EDITOR_HXX
#define EDITOR_HXX
/// from qt http://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html
#include <QPlainTextEdit>
#include <QPainter>
#include <QTextBlock>
#include <QResizeEvent>
class CodeEditor;
class LineNumberArea: public QWidget {
public:
LineNumberArea(CodeEditor *editor);
QSize sizeHint() const override;
protected:
void paintEvent(QPaintEvent *event) override;
private:
CodeEditor *codeEditor;
};
class CodeEditor: public QPlainTextEdit {
Q_OBJECT;
public:
CodeEditor(QWidget *parent = 0): QPlainTextEdit(parent) {
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()));
updateLineNumberAreaWidth(0);
highlightCurrentLine();
}
void lineNumberAreaPaintEvent(QPaintEvent *event) {
QPainter painter(lineNumberArea);
painter.fillRect(event->rect(), Qt::lightGray);
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
int bottom = top + (int) blockBoundingRect(block).height();
while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1);
painter.setPen(Qt::black);
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
Qt::AlignRight, number);
}
block = block.next();
top = bottom;
bottom = top + (int) blockBoundingRect(block).height();
++blockNumber;
}
}
int lineNumberAreaWidth() {
int digits(1);
int max = qMax(1, blockCount());
while (max >= 10) {
max /= 10;
++digits;
}
int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
return space;
}
protected:
void resizeEvent(QResizeEvent *e) override {
QPlainTextEdit::resizeEvent(e);
QRect cr = contentsRect();
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}
private Q_SLOTS:
void updateLineNumberAreaWidth(int newBlockCount) {
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}
void highlightCurrentLine() {
QList<QTextEdit::ExtraSelection> extraSelections;
if (!isReadOnly()) {
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
}
setExtraSelections(extraSelections);
}
void updateLineNumberArea(const QRect &rect, int dy) {
if (dy)
lineNumberArea->scroll(0, dy);
else
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
if (rect.contains(viewport()->rect()))
updateLineNumberAreaWidth(0);
}
private:
QWidget *lineNumberArea;
};
inline LineNumberArea::LineNumberArea(CodeEditor *editor): QWidget(editor) {
codeEditor = editor;
}
inline QSize LineNumberArea::sizeHint() const {
return QSize(codeEditor->lineNumberAreaWidth(), 0);
}
inline void LineNumberArea::paintEvent(QPaintEvent *event) {
codeEditor->lineNumberAreaPaintEvent(event);
}
#endif

@ -9,7 +9,7 @@
bin_PROGRAMS = webtester webrunner
webtester_MOCFILES = moc_testgui.cxx moc_commands.cxx moc_webpage.cxx \
moc_networkaccessmanager.cxx
moc_networkaccessmanager.cxx moc_editor.cxx
webtester_UIFILES = ui_testgui.hxx
webtester_SOURCES = version.cxx webtester.cxx exceptions.hxx version.hxx \
${webtester_MOCFILES} ${webtester_UIFILES}

@ -8,6 +8,7 @@
#define TESTGUI_HXX
#include <webpage.hxx>
#include <editor.hxx>
#include <commands.hxx>
#include <QMainWindow>
#include <QSettings>

@ -81,8 +81,8 @@
</widget>
</item>
<item>
<widget class="QWebView" name="_web" native="true">
<property name="url" stdset="0">
<widget class="QWebView" name="_web">
<property name="url">
<url>
<string>about:blank</string>
</url>
@ -97,12 +97,12 @@
<x>0</x>
<y>0</y>
<width>888</width>
<height>20</height>
<height>26</height>
</rect>
</property>
<widget class="QMenu" name="menuViews">
<property name="title">
<string>Views</string>
<string>&amp;Views</string>
</property>
</widget>
<widget class="QMenu" name="menuFile">
@ -173,7 +173,7 @@
</widget>
<widget class="QDockWidget" name="_linksDock">
<property name="windowTitle">
<string>Links</string>
<string>&amp;Links</string>
</property>
<attribute name="dockWidgetArea">
<number>2</number>
@ -238,7 +238,7 @@
</widget>
<widget class="QDockWidget" name="_executeDock">
<property name="windowTitle">
<string>Execute JavaScript on First Selected Item</string>
<string>Execute &amp;JavaScript on First Selected Item</string>
</property>
<attribute name="dockWidgetArea">
<number>8</number>
@ -391,7 +391,7 @@ this.dispatchEvent(evObj);</string>
</widget>
<widget class="QDockWidget" name="_scriptDock">
<property name="windowTitle">
<string>Test Script</string>
<string>&amp;Test Script</string>
</property>
<attribute name="dockWidgetArea">
<number>4</number>
@ -401,7 +401,7 @@ this.dispatchEvent(evObj);</string>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QPlainTextEdit" name="_testscript"/>
<widget class="CodeEditor" name="_testscript"/>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_8">
@ -529,7 +529,7 @@ this.dispatchEvent(evObj);</string>
</widget>
<widget class="QDockWidget" name="_logDock">
<property name="windowTitle">
<string>Script Run Log</string>
<string>Scri&amp;pt Run Log</string>
</property>
<attribute name="dockWidgetArea">
<number>8</number>
@ -544,7 +544,7 @@ this.dispatchEvent(evObj);</string>
</widget>
<widget class="QDockWidget" name="_setupScriptDock">
<property name="windowTitle">
<string>Setup Script</string>
<string>Set&amp;up Script</string>
</property>
<attribute name="dockWidgetArea">
<number>4</number>
@ -603,7 +603,7 @@ this.dispatchEvent(evObj);</string>
</widget>
<widget class="QDockWidget" name="_scriptCommandsDock">
<property name="windowTitle">
<string>Script Commands</string>
<string>Script &amp;Commands</string>
</property>
<attribute name="dockWidgetArea">
<number>4</number>
@ -688,7 +688,7 @@ this.dispatchEvent(evObj);</string>
</action>
<action name="_actionOpen">
<property name="text">
<string>Open ...</string>
<string>&amp;Open ...</string>
</property>
<property name="shortcut">
<string>Ctrl+O</string>
@ -696,7 +696,7 @@ this.dispatchEvent(evObj);</string>
</action>
<action name="_actionSaveAs">
<property name="text">
<string>Save As ...</string>
<string>Save &amp;As ...</string>
</property>
<property name="shortcut">
<string>Ctrl+Shift+S</string>
@ -704,7 +704,7 @@ this.dispatchEvent(evObj);</string>
</action>
<action name="_actionQuit">
<property name="text">
<string>Quit</string>
<string>&amp;Quit</string>
</property>
<property name="shortcut">
<string>Ctrl+Q</string>
@ -712,7 +712,7 @@ this.dispatchEvent(evObj);</string>
</action>
<action name="_actionRun">
<property name="text">
<string>Run</string>
<string>&amp;Run</string>
</property>
<property name="shortcut">
<string>Ctrl+R</string>
@ -720,12 +720,12 @@ this.dispatchEvent(evObj);</string>
</action>
<action name="_actionRunLine">
<property name="text">
<string>Run Line</string>
<string>Run &amp;Line</string>
</property>
</action>
<action name="_actionClear">
<property name="text">
<string>Clear</string>
<string>&amp;Clear</string>
</property>
</action>
<action name="_actionLog">
@ -744,7 +744,7 @@ this.dispatchEvent(evObj);</string>
<bool>false</bool>
</property>
<property name="text">
<string>Save</string>
<string>&amp;Save</string>
</property>
<property name="shortcut">
<string>Ctrl+S</string>
@ -755,7 +755,7 @@ this.dispatchEvent(evObj);</string>
<bool>false</bool>
</property>
<property name="text">
<string>Revert to saved</string>
<string>R&amp;evert to saved</string>
</property>
<property name="shortcut">
<string>Ctrl+Shift+R</string>
@ -763,12 +763,12 @@ this.dispatchEvent(evObj);</string>
</action>
<action name="_actionOpenSetupScript">
<property name="text">
<string>Open Setup Script ...</string>
<string>O&amp;pen Setup Script ...</string>
</property>
</action>
<action name="_actionCommands">
<property name="text">
<string>Commands ...</string>
<string>&amp;Commands ...</string>
</property>
</action>
</widget>
@ -778,6 +778,11 @@ this.dispatchEvent(evObj);</string>
<extends>QWidget</extends>
<header>QtWebKitWidgets/QWebView</header>
</customwidget>
<customwidget>
<class>CodeEditor</class>
<extends>QPlainTextEdit</extends>
<header>editor.hxx</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>_load</tabstop>

Loading…
Cancel
Save