Debian Codegraph @

include/internal/QCodeEditor.hpp

raw symbols SCIP
1 #pragma once
2
3 // Qt
4 #include <QTextEdit> // Required for inheritance
5
6 class QCompleter;
7 class QLineNumberArea;
8 class QSyntaxStyle;
9 class QStyleSyntaxHighlighter;
10 class QFramedTextAttribute;
11
12 /**
13 * @brief Class, that describes code editor.
14 */
15 class QCodeEditor : public QTextEdit
16 {
17 Q_OBJECT
18
19 public:
20 /**
21 * @brief Constructor.
22 * @param widget Pointer to parent widget.
23 */
24 explicit QCodeEditor(QWidget* widget=nullptr);
25
26 // Disable copying
27 QCodeEditor(const QCodeEditor&) = delete;
28 QCodeEditor& operator=(const QCodeEditor&) = delete;
29
30 /**
31 * @brief Method for getting first visible block
32 * index.
33 * @return Index.
34 */
35 int getFirstVisibleBlock();
36
37 /**
38 * @brief Method for setting highlighter.
39 * @param highlighter Pointer to syntax highlighter.
40 */
41 void setHighlighter(QStyleSyntaxHighlighter* highlighter);
42
43 /**
44 * @brief Method for setting syntax sty.e.
45 * @param style Pointer to syntax style.
46 */
47 void setSyntaxStyle(QSyntaxStyle* style);
48
49 /**
50 * @brief Method setting auto parentheses enabled.
51 */
52 void setAutoParentheses(bool enabled);
53
54 /**
55 * @brief Method for getting is auto parentheses enabled.
56 * Default value: true
57 */
58 bool autoParentheses() const;
59
60 /**
61 * @brief Method for setting tab replacing
62 * enabled.
63 */
64 void setTabReplace(bool enabled);
65
66 /**
67 * @brief Method for getting is tab replacing enabled.
68 * Default value: true
69 */
70 bool tabReplace() const;
71
72 /**
73 * @brief Method for setting amount of spaces, that will
74 * replace tab.
75 * @param val Number of spaces.
76 */
77 void setTabReplaceSize(int val);
78
79 /**
80 * @brief Method for getting number of spaces, that will
81 * replace tab if `tabReplace` is true.
82 * Default: 4
83 */
84 int tabReplaceSize() const;
85
86 /**
87 * @brief Method for setting auto indentation enabled.
88 */
89 void setAutoIndentation(bool enabled);
90
91 /**
92 * @brief Method for getting is auto indentation enabled.
93 * Default: true
94 */
95 bool autoIndentation() const;
96
97 /**
98 * @brief Method for setting completer.
99 * @param completer Pointer to completer object.
100 */
101 void setCompleter(QCompleter* completer);
102
103 /**
104 * @brief Method for getting completer.
105 * @return Pointer to completer.
106 */
107 QCompleter* completer() const;
108
109 public Q_SLOTS:
110
111 /**
112 * @brief Slot, that performs insertion of
113 * completion info into code.
114 * @param s Data.
115 */
116 void insertCompletion(QString s);
117
118 /**
119 * @brief Slot, that performs update of
120 * internal editor viewport based on line
121 * number area width.
122 */
123 void updateLineNumberAreaWidth(int);
124
125 /**
126 * @brief Slot, that performs update of some
127 * part of line number area.
128 * @param rect Area that has to be updated.
129 */
130 void updateLineNumberArea(const QRect& rect);
131
132 /**
133 * @brief Slot, that will proceed extra selection
134 * for current cursor position.
135 */
136 void updateExtraSelection();
137
138 /**
139 * @brief Slot, that will update editor style.
140 */
141 void updateStyle();
142
143 /**
144 * @brief Slot, that will be called on selection
145 * change.
146 */
147 void onSelectionChanged();
148
149 protected:
150 /**
151 * @brief Method, that's called on any text insertion of
152 * mimedata into editor. If it's text - it inserts text
153 * as plain text.
154 */
155 void insertFromMimeData(const QMimeData* source) override;
156
157 /**
158 * @brief Method, that's called on editor painting. This
159 * method if overloaded for line number area redraw.
160 */
161 void paintEvent(QPaintEvent* e) override;
162
163 /**
164 * @brief Method, that's called on any widget resize.
165 * This method if overloaded for line number area
166 * resizing.
167 */
168 void resizeEvent(QResizeEvent* e) override;
169
170 /**
171 * @brief Method, that's called on any key press, posted
172 * into code editor widget. This method is overloaded for:
173 *
174 * 1. Completion
175 * 2. Tab to spaces
176 * 3. Low indentation
177 * 4. Auto parenthesis
178 */
179 void keyPressEvent(QKeyEvent* e) override;
180
181 /**
182 * @brief Method, that's called on focus into widget.
183 * It's required for setting this widget to set
184 * completer.
185 */
186 void focusInEvent(QFocusEvent *e) override;
187
188 private:
189
190 /**
191 * @brief Method for initializing document
192 * layout handlers.
193 */
194 void initDocumentLayoutHandlers();
195
196 /**
197 * @brief Method for initializing default
198 * monospace font.
199 */
200 void initFont();
201
202 /**
203 * @brief Method for performing connection
204 * of objects.
205 */
206 void performConnections();
207
208 /**
209 * @brief Method, that performs selection
210 * frame selection.
211 */
212 void handleSelectionQuery(QTextCursor cursor);
213
214 /**
215 * @brief Method for updating geometry of line number area.
216 */
217 void updateLineGeometry();
218
219 /**
220 * @brief Method, that performs completer processing.
221 * Returns true if event has to be dropped.
222 * @param e Pointer to key event.
223 * @return Shall event be dropped.
224 */
225 bool proceedCompleterBegin(QKeyEvent *e);
226 void proceedCompleterEnd(QKeyEvent* e);
227
228 /**
229 * @brief Method for getting character under
230 * cursor.
231 * @param offset Offset to cursor.
232 */
233 QChar charUnderCursor(int offset = 0) const;
234
235 /**
236 * @brief Method for getting word under
237 * cursor.
238 * @return Word under cursor.
239 */
240 QString wordUnderCursor() const;
241
242 /**
243 * @brief Method, that adds highlighting of
244 * currently selected line to extra selection list.
245 */
246 void highlightCurrentLine(QList<QTextEdit::ExtraSelection>& extraSelection);
247
248 /**
249 * @brief Method, that adds highlighting of
250 * parenthesis if available.
251 */
252 void highlightParenthesis(QList<QTextEdit::ExtraSelection>& extraSelection);
253
254 /**
255 * @brief Method for getting number of indentation
256 * spaces in current line. Tabs will be treated
257 * as `tabWidth / spaceWidth`
258 */
259 int getIndentationSpaces();
260
261 QStyleSyntaxHighlighter* m_highlighter;
262 QSyntaxStyle* m_syntaxStyle;
263 QLineNumberArea* m_lineNumberArea;
264 QCompleter* m_completer;
265
266 QFramedTextAttribute* m_framedAttribute;
267
268 bool m_autoIndentation;
269 bool m_autoParentheses;
270 bool m_replaceTab;
271 QString m_tabReplace;
272 };
273