blob: 0ae39c60e206638126bd5adbcf4456ac9eb5c395 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
#ifndef MO_TEXTEDITOR_H
#define MO_TEXTEDITOR_H
#include <QPlainTextEdit>
class TextEditor;
class TextEditorToolbar
{
public:
TextEditorToolbar(TextEditor& editor);
QWidget* widget();
private:
TextEditor& m_editor;
QWidget* m_widget;
QAction* m_save;
QAction* m_wordWrap;
void onTextModified(bool b);
void onWordWrap(bool b);
};
// mostly from https://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html
//
class TextEditorLineNumbers : public QWidget
{
public:
TextEditorLineNumbers(TextEditor& editor);
QSize sizeHint() const override;
int areaWidth() const;
protected:
void paintEvent(QPaintEvent *event) override;
private:
TextEditor& m_editor;
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:
TextEditor(QWidget* parent=nullptr);
void setupToolbar();
bool load(const QString& filename);
bool save();
const QString& filename() const;
void wordWrap(bool b);
void toggleWordWrap();
bool wordWrap() const;
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);
protected:
void resizeEvent(QResizeEvent* e) override;
private:
TextEditorToolbar m_toolbar;
TextEditorLineNumbers* m_lineNumbers;
TextEditorHighlighter* m_highlighter;
QString m_filename;
QString m_encoding;
bool m_dirty;
void onModified(bool b);
void dirty(bool b);
QWidget* wrapEditWidget();
void paintLineNumbers(QPaintEvent* e);
};
#endif // MO_TEXTEDITOR_H
|