HAL
code_editor.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
30 
31 #include <QPlainTextEdit>
32 
33 class QPaintEvent;
34 class QPropertyAnimation;
35 class QResizeEvent;
36 
37 namespace hal
38 {
39 
40  class CodeEditorMinimap;
41  class CodeEditorScrollbar;
42  class LineNumberArea;
43 
51  class CodeEditor : public QPlainTextEdit
52  {
53  Q_OBJECT
60 
61  public:
67  explicit CodeEditor(QWidget* parent = nullptr);
68 
77  virtual bool eventFilter(QObject* object, QEvent* event) override;
78 
86 
93 
99  int lineNumberAreaWidth();
100 
106  int minimapWidth();
107 
113  int first_visible_block();
114 
120  void centerOnLine(const int number);
121 
129 
136 
139  QFont lineNumberFont() const;
140  QColor lineNumberColor() const;
145 
147 
149  void setLineNumberFont(const QFont& font);
150  void setLineNumberColor(QColor& color);
151  void setLineNumberBackground(QColor& color);
152  void setLineNumberHighlightColor(QColor& color);
154  void setCurrentLineBackground(QColor& color);
156 
157 
163  void setLineNumberEnabled(bool enabled);
164 
171 
177  void setLineWrapEnabled(bool enabled);
178 
184  void setMinimapEnabled(bool enabled);
185 
186  public Q_SLOTS:
193  void search(const QString& string, SearchOptions searchOpts = 8);
194 
198  void toggleLineNumbers();
199 
203  void toggleMinimap();
204 
210  void setFontSize(int pt);
211 
212  protected:
218  virtual void resizeEvent(QResizeEvent* event) override;
219 
220  private Q_SLOTS:
224  void highlightCurrentLine();
225 
231  void handleBlockCountChanged(int new_block_count);
232 
239  void updateLineNumberArea(const QRect& rect, int dy);
240 
247  void updateMinimap(const QRect& rect, int dy);
248 
249  private:
250  void updateLayout();
251  void clearLineHighlight();
252 
253  CodeEditorScrollbar* mScrollbar;
254 
255  LineNumberArea* mLineNumberArea;
256  CodeEditorMinimap* mMinimap;
257 
258  QPropertyAnimation* mAnimation;
259 
260  bool mLineNumbersEnabled;
261  bool mLineHighlightEnabled;
262  bool mMinimapEnabled;
263  bool mLineWrapEnabled;
264 
265  QFont mLineNumberFont;
266  QColor mLineNumberColor;
267  QColor mLineNumberBackground;
268  QColor mLineNumberHighlightColor;
269  QColor mLineNumberHighlightBackground;
270  QColor mCurrentLineBackground;
271  };
272 }
A plain text edit widget that is intended for editing code.
Definition: code_editor.h:52
virtual void resizeEvent(QResizeEvent *event) override
void setHighlightCurrentLineEnabled(bool enabled)
int lineNumberAreaWidth()
void setLineNumberColor(QColor &color)
void setFontSize(int pt)
QColor lineNumberHighlightColor
Definition: code_editor.h:57
void setLineWrapEnabled(bool enabled)
QColor lineNumberColor
Definition: code_editor.h:55
CodeEditor(QWidget *parent=nullptr)
Definition: code_editor.cpp:25
virtual bool eventFilter(QObject *object, QEvent *event) override
Definition: code_editor.cpp:64
void centerOnLine(const int number)
int first_visible_block()
void setLineNumberBackground(QColor &color)
QColor lineNumberHighlightBackground
Definition: code_editor.h:58
void setMinimapEnabled(bool enabled)
void lineNumberAreaPaintEvent(QPaintEvent *event)
Definition: code_editor.cpp:85
void handleWheelEvent(QWheelEvent *event)
void setLineNumberHighlightColor(QColor &color)
QColor lineNumberBackground
Definition: code_editor.h:56
void setLineNumberFont(const QFont &font)
CodeEditorMinimap * minimap()
void setLineNumberHighlightBackground(QColor &color)
void toggleLineNumbers()
QFont lineNumberFont
Definition: code_editor.h:54
void search(const QString &string, SearchOptions searchOpts=8)
void minimapPaintEvent(QPaintEvent *event)
QColor currentLineBackground
Definition: code_editor.h:59
void setLineNumberEnabled(bool enabled)
void setCurrentLineBackground(QColor &color)
A minimap that supports an easier navigation in larger files.
Represents the scrollbar of the CodeEditor.
Shows line numbers next to a CodeEditor.
virtual bool event(QEvent *event) override
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SLOTSQ_SLOTS
QObject * parent() const const