summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorisanae <14251494+isanae@users.noreply.github.com>2019-06-17 02:53:52 -0400
committerisanae <14251494+isanae@users.noreply.github.com>2019-07-02 10:10:17 -0400
commit0084ca6c4c1fad76dafe700aad3db4fdb5ef2750 (patch)
tree3977404e4ab39388bf2fff02fa3b5eed11ab09e2
parenta2b68c3a2e17198e834684fe1719b19464559657 (diff)
text editor default style
changed colour properties to use an actual QColor stylable highlight background color padding for line numbers
-rw-r--r--src/texteditor.cpp187
-rw-r--r--src/texteditor.h48
2 files changed, 159 insertions, 76 deletions
diff --git a/src/texteditor.cpp b/src/texteditor.cpp
index 7a83e461..ccafb37a 100644
--- a/src/texteditor.cpp
+++ b/src/texteditor.cpp
@@ -7,17 +7,53 @@ TextEditor::TextEditor(QWidget* parent) :
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());
+ setDefaultStyle();
+ wordWrap(true);
+
emit modified(false);
- QObject::connect(
+ connect(
document(), &QTextDocument::modificationChanged,
[&](bool b){ onModified(b); });
+
+ connect(
+ this, &QPlainTextEdit::cursorPositionChanged,
+ [&]{ highlightCurrentLine(); });
+}
+
+void TextEditor::setDefaultStyle()
+{
+ const auto font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
+
+ setFont(font);
+ m_lineNumbers->setFont(font);
+
+ QColor textColor(Qt::black);
+ QColor altTextColor(Qt::darkGray);
+ QColor backgroundColor(Qt::white);
+
+ {
+ auto w = std::make_unique<QWidget>();
+
+ if (auto* s=style()) {
+ s->polish(w.get());
+ }
+
+ textColor = w->palette().color(QPalette::WindowText);
+ altTextColor = w->palette().color(QPalette::Disabled, QPalette::WindowText);
+ backgroundColor = w->palette().color(QPalette::Window);
+ }
+
+ setTextColor(textColor);
+ m_lineNumbers->setTextColor(altTextColor);
+
+ setBackgroundColor(backgroundColor);
+ m_lineNumbers->setBackgroundColor(backgroundColor);
+
+ setHighlightBackgroundColor(backgroundColor);
}
bool TextEditor::load(const QString& filename)
@@ -84,35 +120,13 @@ bool TextEditor::dirty() const
return m_dirty;
}
-QString TextEditor::textColor() const
-{
- return m_highlighter->textColor().name(QColor::HexArgb);
-}
-
-void TextEditor::setTextColor(const QString& s)
+QColor TextEditor::backgroundColor() const
{
- const QColor c(s);
- if (!c.isValid()) {
- qWarning() << "TextEditor: invalid text color '" << s << "'";
- return;
- }
-
- m_highlighter->setTextColor(c);
+ return m_highlighter->backgroundColor();
}
-QString TextEditor::backgroundColor() const
+void TextEditor::setBackgroundColor(const QColor& c)
{
- 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;
}
@@ -126,6 +140,27 @@ void TextEditor::setBackgroundColor(const QString& s)
.arg(c.alphaF()));
}
+QColor TextEditor::textColor() const
+{
+ return m_highlighter->textColor();
+}
+
+void TextEditor::setTextColor(const QColor& c)
+{
+ m_highlighter->setTextColor(c);
+}
+
+QColor TextEditor::highlightBackgroundColor() const
+{
+ return m_highlightBackground;
+}
+
+void TextEditor::setHighlightBackgroundColor(const QColor& c)
+{
+ m_highlightBackground = c;
+ update();
+}
+
void TextEditor::onModified(bool b)
{
dirty(b);
@@ -201,10 +236,12 @@ void TextEditor::resizeEvent(QResizeEvent* e)
m_lineNumbers->setGeometry(QRect(cr.left(), cr.top(), m_lineNumbers->areaWidth(), cr.height()));
}
-void TextEditor::paintLineNumbers(QPaintEvent* e)
+void TextEditor::paintLineNumbers(QPaintEvent* e, const QColor& textColor)
{
+ QStyleOption opt;
+ opt.init(m_lineNumbers);
+
QPainter painter(m_lineNumbers);
- painter.fillRect(e->rect(), Qt::lightGray);
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
@@ -214,10 +251,10 @@ void TextEditor::paintLineNumbers(QPaintEvent* e)
while (block.isValid() && top <= e->rect().bottom()) {
if (block.isVisible() && bottom >= e->rect().top()) {
QString number = QString::number(blockNumber + 1);
- painter.setPen(Qt::black);
+ painter.setPen(textColor);
painter.drawText(
- 0, top, m_lineNumbers->width(), fontMetrics().height(),
+ 0, top, m_lineNumbers->width() - 3, fontMetrics().height(),
Qt::AlignRight, number);
}
@@ -228,6 +265,25 @@ void TextEditor::paintLineNumbers(QPaintEvent* e)
}
}
+void TextEditor::highlightCurrentLine()
+{
+ QList<QTextEdit::ExtraSelection> extraSelections;
+
+ if (!isReadOnly()) {
+ QTextEdit::ExtraSelection selection;
+
+ QColor lineColor = QColor(Qt::yellow).lighter(160);
+
+ selection.format.setBackground(m_highlightBackground);
+ selection.format.setProperty(QTextFormat::FullWidthSelection, true);
+ selection.cursor = textCursor();
+ selection.cursor.clearSelection();
+ extraSelections.append(selection);
+ }
+
+ setExtraSelections(extraSelections);
+}
+
TextEditorHighlighter::TextEditorHighlighter(QTextDocument* doc) :
QSyntaxHighlighter(doc),
@@ -236,7 +292,7 @@ TextEditorHighlighter::TextEditorHighlighter(QTextDocument* doc) :
{
}
-QColor TextEditorHighlighter::backgroundColor()
+QColor TextEditorHighlighter::backgroundColor() const
{
return m_background;
}
@@ -247,7 +303,7 @@ void TextEditorHighlighter::setBackgroundColor(const QColor& c)
changed();
}
-QColor TextEditorHighlighter::textColor()
+QColor TextEditorHighlighter::textColor() const
{
return m_text;
}
@@ -274,16 +330,14 @@ void TextEditorHighlighter::changed()
TextEditorLineNumbers::TextEditorLineNumbers(TextEditor& editor)
- : QWidget(&editor), m_editor(editor)
+ : QFrame(&editor), m_editor(editor)
{
setFont(editor.font());
connect(&m_editor, &QPlainTextEdit::blockCountChanged, [&]{ updateAreaWidth(); });
connect(&m_editor, &QPlainTextEdit::updateRequest, [&](auto&& rect, int dy){ updateArea(rect, dy); });
- connect(&m_editor, &QPlainTextEdit::cursorPositionChanged, [&]{ highlightCurrentLine(); });
updateAreaWidth();
- highlightCurrentLine();
}
QSize TextEditorLineNumbers::sizeHint() const
@@ -291,11 +345,6 @@ QSize TextEditorLineNumbers::sizeHint() const
return QSize(areaWidth(), 0);
}
-void TextEditorLineNumbers::paintEvent(QPaintEvent* e)
-{
- m_editor.paintLineNumbers(e);
-}
-
int TextEditorLineNumbers::areaWidth() const
{
int digits = 1;
@@ -308,11 +357,42 @@ int TextEditorLineNumbers::areaWidth() const
digits = std::max(3, digits);
- int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits;
+ int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits + 3;
return space;
}
+QColor TextEditorLineNumbers::textColor() const
+{
+ return m_text;
+}
+
+void TextEditorLineNumbers::setTextColor(const QColor& c)
+{
+ m_text = c;
+ m_editor.update();
+}
+
+QColor TextEditorLineNumbers::backgroundColor() const
+{
+ return m_background;
+}
+
+void TextEditorLineNumbers::setBackgroundColor(const QColor& c)
+{
+ m_background = c;
+ m_editor.update();
+}
+
+void TextEditorLineNumbers::paintEvent(QPaintEvent* e)
+{
+ QPainter painter(this);
+ painter.fillRect(e->rect(), m_background);
+
+ QFrame::paintEvent(e);
+ m_editor.paintLineNumbers(e, m_text);
+}
+
void TextEditorLineNumbers::updateAreaWidth()
{
m_editor.setViewportMargins(areaWidth(), 0, 0, 0);
@@ -331,25 +411,6 @@ 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 0ae39c60..0f7447ff 100644
--- a/src/texteditor.h
+++ b/src/texteditor.h
@@ -5,8 +5,10 @@
class TextEditor;
-class TextEditorToolbar
+class TextEditorToolbar : public QObject
{
+ Q_OBJECT;
+
public:
TextEditorToolbar(TextEditor& editor);
@@ -25,34 +27,47 @@ private:
// mostly from https://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html
//
-class TextEditorLineNumbers : public QWidget
+class TextEditorLineNumbers : public QFrame
{
+ Q_OBJECT;
+ Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor);
+ Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor);
+
public:
TextEditorLineNumbers(TextEditor& editor);
+
QSize sizeHint() const override;
int areaWidth() const;
+ QColor textColor() const;
+ void setTextColor(const QColor& c);
+
+ QColor backgroundColor() const;
+ void setBackgroundColor(const QColor& c);
+
protected:
void paintEvent(QPaintEvent *event) override;
private:
TextEditor& m_editor;
+ QColor m_background, m_text;
void updateAreaWidth();
void updateArea(const QRect &rect, int dy);
- void highlightCurrentLine();
};
class TextEditorHighlighter : public QSyntaxHighlighter
{
+ Q_OBJECT;
+
public:
TextEditorHighlighter(QTextDocument* doc);
- QColor backgroundColor();
+ QColor backgroundColor() const;
void setBackgroundColor(const QColor& c);
- QColor textColor();
+ QColor textColor() const;
void setTextColor(const QColor& c);
protected:
@@ -67,9 +82,10 @@ private:
class TextEditor : public QPlainTextEdit
{
- Q_OBJECT
- Q_PROPERTY(QString textColor READ textColor WRITE setTextColor)
- Q_PROPERTY(QString backgroundColor READ backgroundColor WRITE setBackgroundColor)
+ Q_OBJECT;
+ Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor);
+ Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor);
+ Q_PROPERTY(QColor highlightBackgroundColor READ highlightBackgroundColor WRITE setHighlightBackgroundColor);
friend class TextEditorLineNumbers;
@@ -89,11 +105,14 @@ public:
bool dirty() const;
- QString textColor() const;
- void setTextColor(const QString& s);
+ QColor backgroundColor() const;
+ void setBackgroundColor(const QColor& c);
- QString backgroundColor() const;
- void setBackgroundColor(const QString& s);
+ QColor textColor() const;
+ void setTextColor(const QColor& c);
+
+ QColor highlightBackgroundColor() const;
+ void setHighlightBackgroundColor(const QColor& c);
signals:
void modified(bool b);
@@ -106,16 +125,19 @@ private:
TextEditorToolbar m_toolbar;
TextEditorLineNumbers* m_lineNumbers;
TextEditorHighlighter* m_highlighter;
+ QColor m_highlightBackground;
QString m_filename;
QString m_encoding;
bool m_dirty;
+ void setDefaultStyle();
void onModified(bool b);
void dirty(bool b);
QWidget* wrapEditWidget();
- void paintLineNumbers(QPaintEvent* e);
+ void highlightCurrentLine();
+ void paintLineNumbers(QPaintEvent* e, const QColor& textColor);
};
#endif // MO_TEXTEDITOR_H