1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25
26 #pragma once
27
28 #include <QObject>
29 #include <QList>
30 #include <QVector>
31 #include <QTextCursor>
32 #include <QColor>
33
34 QT_FORWARD_DECLARE_CLASS(QWidget)
QT_FORWARD_DECLARE_CLASS(QPainterPath)35 QT_FORWARD_DECLARE_CLASS(QPainterPath)
36
37 namespace TextEditor {
38 class TextEditorWidget;
39
40 namespace Internal {
41
42 struct OverlaySelection
43 {
44 OverlaySelection() = default;
45
46 QTextCursor m_cursor_begin;
47 QTextCursor m_cursor_end;
48 QColor m_fg;
49 QColor m_bg;
50 int m_fixedLength = -1;
51 bool m_dropShadow = false;
52 };
53
54 class TextEditorOverlay : public QObject
55 {
56 Q_OBJECT
57 public:
58 TextEditorOverlay(TextEditorWidget *editor);
59
60 QRect rect() const;
61 void paint(QPainter *painter, const QRect &clip);
62 void fill(QPainter *painter, const QColor &color, const QRect &clip);
63
64 bool isVisible() const { return m_visible; }
65 void setVisible(bool b);
66
67 inline void hide() { setVisible(false); }
68 inline void show() { setVisible(true); }
69
70 void setBorderWidth(int bw) {m_borderWidth = bw; }
71
72 void update();
73
74 void setAlpha(bool enabled) { m_alpha = enabled; }
75
76 virtual void clear();
77
78 enum OverlaySelectionFlags {
79 LockSize = 1,
80 DropShadow = 2,
81 ExpandBegin = 4
82 };
83
84 void addOverlaySelection(const QTextCursor &cursor, const QColor &fg, const QColor &bg,
85 uint overlaySelectionFlags = 0);
86 void addOverlaySelection(int begin, int end, const QColor &fg, const QColor &bg,
87 uint overlaySelectionFlags = 0);
88
89 const QList<OverlaySelection> &selections() const { return m_selections; }
90
91 inline bool isEmpty() const { return m_selections.isEmpty(); }
92
93 inline int dropShadowWidth() const { return m_dropShadowWidth; }
94
95 bool hasFirstSelectionBeginMoved() const;
96
97 protected:
98 QTextCursor cursorForSelection(const OverlaySelection &selection) const;
99 QTextCursor cursorForIndex(int selectionIndex) const;
100
101 private:
102 QPainterPath createSelectionPath(const QTextCursor &begin, const QTextCursor &end, const QRect& clip);
103 void paintSelection(QPainter *painter, const OverlaySelection &selection);
104 void fillSelection(QPainter *painter, const OverlaySelection &selection, const QColor &color);
105
106 bool m_visible;
107 bool m_alpha;
108 int m_borderWidth;
109 int m_dropShadowWidth;
110 int m_firstSelectionOriginalBegin;
111 TextEditorWidget *m_editor;
112 QWidget *m_viewport;
113 QList<OverlaySelection> m_selections;
114 };
115
116 } // namespace Internal
117 } // namespace TextEditor
118