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 "texteditor_global.h"
29 
30 #include <utils/fileutils.h>
31 #include <utils/id.h>
32 #include <utils/optional.h>
33 #include <utils/theme/theme.h>
34 
35 #include <QCoreApplication>
36 #include <QIcon>
37 #include <QVector>
38 
39 QT_BEGIN_NAMESPACE
40 class QAction;
41 class QGridLayout;
42 class QLayout;
43 class QPainter;
44 class QRect;
45 class QTextBlock;
46 QT_END_NAMESPACE
47 
48 namespace TextEditor {
49 
50 class TextDocument;
51 
52 class TEXTEDITOR_EXPORT TextMark
53 {
54     Q_DECLARE_TR_FUNCTIONS(TextEditor::TextMark)
55 public:
56     TextMark(const Utils::FilePath &fileName,
57              int lineNumber,
58              Utils::Id category,
59              double widthFactor = 1.0);
60     TextMark() = delete;
61     virtual ~TextMark();
62 
63     // determine order on markers on the same line.
64     enum Priority
65     {
66         LowPriority,
67         NormalPriority,
68         HighPriority // shown on top.
69     };
70 
71     Utils::FilePath fileName() const;
72     int lineNumber() const;
73 
74     virtual void paintIcon(QPainter *painter, const QRect &rect) const;
75     virtual void paintAnnotation(QPainter &painter, QRectF *annotationRect,
76                                  const qreal fadeInOffset, const qreal fadeOutOffset,
77                                  const QPointF &contentOffset) const;
78     struct AnnotationRects
79     {
80         QRectF fadeInRect;
81         QRectF annotationRect;
82         QRectF iconRect;
83         QRectF textRect;
84         QRectF fadeOutRect;
85         QString text;
86     };
87     AnnotationRects annotationRects(const QRectF &boundingRect, const QFontMetrics &fm,
88                                     const qreal fadeInOffset, const qreal fadeOutOffset) const;
89     /// called if the filename of the document changed
90     virtual void updateFileName(const Utils::FilePath &fileName);
91     virtual void updateLineNumber(int lineNumber);
92     virtual void updateBlock(const QTextBlock &block);
93     virtual void move(int line);
94     virtual void removedFromEditor();
95     virtual bool isClickable() const;
96     virtual void clicked();
97     virtual bool isDraggable() const;
98     virtual void dragToLine(int lineNumber);
99     void addToToolTipLayout(QGridLayout *target) const;
100     virtual bool addToolTipContent(QLayout *target) const;
101 
102     void setIcon(const QIcon &icon);
103     void setIconProvider(const std::function<QIcon()> &iconProvider);
104     const QIcon icon() const;
105     // call this if the icon has changed.
106     void updateMarker();
priority()107     Priority priority() const { return m_priority;}
108     void setPriority(Priority prioriy);
109     bool isVisible() const;
110     void setVisible(bool isVisible);
category()111     Utils::Id category() const { return m_category; }
112     double widthFactor() const;
113     void setWidthFactor(double factor);
114 
115     Utils::optional<Utils::Theme::Color> color() const;
116     void setColor(const Utils::Theme::Color &color);
117 
defaultToolTip()118     QString defaultToolTip() const { return m_defaultToolTip; }
setDefaultToolTip(const QString & toolTip)119     void setDefaultToolTip(const QString &toolTip) { m_defaultToolTip = toolTip; }
120 
baseTextDocument()121     TextDocument *baseTextDocument() const { return m_baseTextDocument; }
setBaseTextDocument(TextDocument * baseTextDocument)122     void setBaseTextDocument(TextDocument *baseTextDocument) { m_baseTextDocument = baseTextDocument; }
123 
lineAnnotation()124     QString lineAnnotation() const { return m_lineAnnotation; }
setLineAnnotation(const QString & lineAnnotation)125     void setLineAnnotation(const QString &lineAnnotation) { m_lineAnnotation = lineAnnotation; }
126 
127     QString toolTip() const;
128     void setToolTip(const QString &toolTip);
129     void setToolTipProvider(const std::function<QString ()> &toolTipProvider);
130 
131     QVector<QAction *> actions() const;
132     void setActions(const QVector<QAction *> &actions); // Takes ownership
133 
134 protected:
135     void setSettingsPage(Utils::Id settingsPage);
136 
137 private:
138     Q_DISABLE_COPY(TextMark)
139 
140     TextDocument *m_baseTextDocument = nullptr;
141     Utils::FilePath m_fileName;
142     int m_lineNumber = 0;
143     Priority m_priority = LowPriority;
144     QIcon m_icon;
145     std::function<QIcon()> m_iconProvider;
146     Utils::optional<Utils::Theme::Color> m_color;
147     bool m_visible = false;
148     Utils::Id m_category;
149     double m_widthFactor = 1.0;
150     QString m_lineAnnotation;
151     QString m_toolTip;
152     std::function<QString()> m_toolTipProvider;
153     QString m_defaultToolTip;
154     QVector<QAction *> m_actions;
155     QAction *m_settingsAction = nullptr;
156 };
157 
158 } // namespace TextEditor
159