summaryrefslogtreecommitdiff
path: root/src/texteditor.h
blob: e66945181471d4a1630095d30a26a38d16719a36 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef MO_TEXTEDITOR_H
#define MO_TEXTEDITOR_H

#include <QPlainTextEdit>

class TextEditor;

class TextEditorToolbar : public QFrame
{
  Q_OBJECT;

public:
  TextEditorToolbar(TextEditor& editor);

private:
  TextEditor& m_editor;
  QAction* m_save;
  QAction* m_wordWrap;
  QAction* m_explore;
  QLineEdit* m_path;

  void onTextModified(bool b);
  void onWordWrap(bool b);
  void onLoaded(const QString& s);
};

// mostly from https://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html
//
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);
};

class TextEditorHighlighter : public QSyntaxHighlighter
{
  Q_OBJECT;

public:
  TextEditorHighlighter(QTextDocument* doc);

  QColor backgroundColor() const;
  void setBackgroundColor(const QColor& c);

  QColor textColor() const;
  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(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;

public:
  TextEditor(QWidget* parent = nullptr);

  void setupToolbar();

  void clear();
  bool load(const QString& filename);
  bool save();

  const QString& filename() const;

  void wordWrap(bool b);
  void toggleWordWrap();
  bool wordWrap() const;

  bool dirty() const;

  QColor backgroundColor() const;
  void setBackgroundColor(const QColor& c);

  QColor textColor() const;
  void setTextColor(const QColor& c);

  QColor highlightBackgroundColor() const;
  void setHighlightBackgroundColor(const QColor& c);

  void explore();

signals:
  void loaded(QString filename);
  void modified(bool b);
  void wordWrapChanged(bool b);

protected:
  void resizeEvent(QResizeEvent* e) override;

private:
  TextEditorToolbar* m_toolbar;
  TextEditorLineNumbers* m_lineNumbers;
  TextEditorHighlighter* m_highlighter;
  QColor m_highlightBackground;
  QString m_filename;
  QString m_encoding;
  bool m_needsBOM;
  bool m_dirty;
  bool m_loading;

  void setDefaultStyle();
  void onModified(bool b);
  void dirty(bool b);

  QWidget* wrapEditWidget();

  void highlightCurrentLine();
  void paintLineNumbers(QPaintEvent* e, const QColor& textColor);
};

class HTMLEditor : public QTextEdit
{
  Q_OBJECT;

public:
  using QTextEdit::QTextEdit;

signals:
  void editingFinished();

protected:
  void focusOutEvent(QFocusEvent* e);

private:
};

#endif  // MO_TEXTEDITOR_H