diff options
| author | isanae <14251494+isanae@users.noreply.github.com> | 2019-06-17 01:11:13 -0400 |
|---|---|---|
| committer | isanae <14251494+isanae@users.noreply.github.com> | 2019-07-02 10:10:17 -0400 |
| commit | a2b68c3a2e17198e834684fe1719b19464559657 (patch) | |
| tree | bba0ce9607f41c2c1c85db0f75d4ec98640034ae /src | |
| parent | c79cf72d2250631110e8605ced6f34dda0378dc0 (diff) | |
text and background color, simple syntax highlighter that doesn't do anything
highlight current line
Diffstat (limited to 'src')
| -rw-r--r-- | src/texteditor.cpp | 115 | ||||
| -rw-r--r-- | src/texteditor.h | 34 |
2 files changed, 145 insertions, 4 deletions
diff --git a/src/texteditor.cpp b/src/texteditor.cpp index ecb46547..7a83e461 100644 --- a/src/texteditor.cpp +++ b/src/texteditor.cpp @@ -4,12 +4,14 @@ TextEditor::TextEditor(QWidget* parent) : QPlainTextEdit(parent), - m_toolbar(*this), m_lineNumbers(nullptr), m_dirty(false) + m_toolbar(*this), m_lineNumbers(nullptr), m_highlighter(nullptr), + m_dirty(false) { setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); wordWrap(true); m_lineNumbers = new TextEditorLineNumbers(*this); + m_highlighter = new TextEditorHighlighter(document()); emit modified(false); @@ -82,6 +84,48 @@ bool TextEditor::dirty() const return m_dirty; } +QString TextEditor::textColor() const +{ + return m_highlighter->textColor().name(QColor::HexArgb); +} + +void TextEditor::setTextColor(const QString& s) +{ + const QColor c(s); + if (!c.isValid()) { + qWarning() << "TextEditor: invalid text color '" << s << "'"; + return; + } + + m_highlighter->setTextColor(c); +} + +QString TextEditor::backgroundColor() const +{ + return m_highlighter->backgroundColor().name(QColor::HexArgb); +} + +void TextEditor::setBackgroundColor(const QString& s) +{ + const QColor c(s); + if (!c.isValid()) { + qWarning() << "TextEditor: invalid backgorund color '" << s << "'"; + return; + } + + if (m_highlighter->backgroundColor() == c) { + return; + } + + m_highlighter->setBackgroundColor(c); + + setStyleSheet(QString("QPlainTextEdit{ background-color: rgba(%1, %2, %3, %4); }") + .arg(c.redF() * 255) + .arg(c.greenF() * 255) + .arg(c.blueF() * 255) + .arg(c.alphaF())); +} + void TextEditor::onModified(bool b) { dirty(b); @@ -185,6 +229,50 @@ void TextEditor::paintLineNumbers(QPaintEvent* e) } +TextEditorHighlighter::TextEditorHighlighter(QTextDocument* doc) : + QSyntaxHighlighter(doc), + m_background(QColor("transparent")), + m_text(QColor("black")) +{ +} + +QColor TextEditorHighlighter::backgroundColor() +{ + return m_background; +} + +void TextEditorHighlighter::setBackgroundColor(const QColor& c) +{ + m_background = c; + changed(); +} + +QColor TextEditorHighlighter::textColor() +{ + return m_text; +} + +void TextEditorHighlighter::setTextColor(const QColor& c) +{ + m_text = c; + changed(); +} + +void TextEditorHighlighter::highlightBlock(const QString& s) +{ + QTextCharFormat f; + f.setBackground(m_background); + f.setForeground(m_text); + + setFormat(0, s.size(), f); +} + +void TextEditorHighlighter::changed() +{ + rehighlight(); +} + + TextEditorLineNumbers::TextEditorLineNumbers(TextEditor& editor) : QWidget(&editor), m_editor(editor) { @@ -192,10 +280,10 @@ TextEditorLineNumbers::TextEditorLineNumbers(TextEditor& editor) connect(&m_editor, &QPlainTextEdit::blockCountChanged, [&]{ updateAreaWidth(); }); connect(&m_editor, &QPlainTextEdit::updateRequest, [&](auto&& rect, int dy){ updateArea(rect, dy); }); - //connect(e, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); + connect(&m_editor, &QPlainTextEdit::cursorPositionChanged, [&]{ highlightCurrentLine(); }); updateAreaWidth(); - //highlightCurrentLine(); + highlightCurrentLine(); } QSize TextEditorLineNumbers::sizeHint() const @@ -211,7 +299,7 @@ void TextEditorLineNumbers::paintEvent(QPaintEvent* e) int TextEditorLineNumbers::areaWidth() const { int digits = 1; - int max = qMax(1, m_editor.blockCount()); + int max = std::max(1, m_editor.blockCount()); while (max >= 10) { max /= 10; @@ -243,6 +331,25 @@ void TextEditorLineNumbers::updateArea(const QRect &rect, int dy) } } +void TextEditorLineNumbers::highlightCurrentLine() +{ + QList<QTextEdit::ExtraSelection> extraSelections; + + if (!m_editor.isReadOnly()) { + QTextEdit::ExtraSelection selection; + + QColor lineColor = QColor(Qt::yellow).lighter(160); + + selection.format.setBackground(lineColor); + selection.format.setProperty(QTextFormat::FullWidthSelection, true); + selection.cursor = m_editor.textCursor(); + selection.cursor.clearSelection(); + extraSelections.append(selection); + } + + m_editor.setExtraSelections(extraSelections); +} + TextEditorToolbar::TextEditorToolbar(TextEditor& editor) : m_editor(editor), diff --git a/src/texteditor.h b/src/texteditor.h index af542341..0ae39c60 100644 --- a/src/texteditor.h +++ b/src/texteditor.h @@ -23,6 +23,8 @@ private: }; +// mostly from https://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html +// class TextEditorLineNumbers : public QWidget { public: @@ -38,12 +40,37 @@ private: void updateAreaWidth(); void updateArea(const QRect &rect, int dy); + void highlightCurrentLine(); +}; + + +class TextEditorHighlighter : public QSyntaxHighlighter +{ +public: + TextEditorHighlighter(QTextDocument* doc); + + QColor backgroundColor(); + void setBackgroundColor(const QColor& c); + + QColor textColor(); + void setTextColor(const QColor& c); + +protected: + void highlightBlock(const QString& text) override; + +private: + QColor m_background, m_text; + + void changed(); }; class TextEditor : public QPlainTextEdit { Q_OBJECT + Q_PROPERTY(QString textColor READ textColor WRITE setTextColor) + Q_PROPERTY(QString backgroundColor READ backgroundColor WRITE setBackgroundColor) + friend class TextEditorLineNumbers; public: @@ -62,6 +89,12 @@ public: bool dirty() const; + QString textColor() const; + void setTextColor(const QString& s); + + QString backgroundColor() const; + void setBackgroundColor(const QString& s); + signals: void modified(bool b); void wordWrapChanged(bool b); @@ -72,6 +105,7 @@ protected: private: TextEditorToolbar m_toolbar; TextEditorLineNumbers* m_lineNumbers; + TextEditorHighlighter* m_highlighter; QString m_filename; QString m_encoding; bool m_dirty; |
