itom  4.1.0
textDecoration.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet Stuttgart, Germany
6 
7  This file is part of itom.
8 
9  itom is free software; you can redistribute it and/or modify it
10  under the terms of the GNU Library General Public Licence as published by
11  the Free Software Foundation; either version 2 of the Licence, or (at
12  your option) any later version.
13 
14  itom is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17  General Public Licence for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with itom. If not, see <http://www.gnu.org/licenses/>.
21 
22  Further hints:
23  ------------------------
24 
25  This file belongs to the code editor of itom. The code editor is
26  in major parts a fork / rewritten version of the python-based source
27  code editor PyQode from Colin Duquesnoy and others
28  (see https://github.com/pyQode). PyQode itself is licensed under
29  the MIT License (MIT).
30 
31  Some parts of the code editor of itom are also inspired by the
32  source code editor of the Spyder IDE (https://github.com/spyder-ide),
33  also licensed under the MIT License and developed by the Spyder Project
34  Contributors.
35 
36 *********************************************************************** */
37 
38 #ifndef TEXTDECORATIONS_H
39 #define TEXTDECORATIONS_H
40 
41 /*
42 This module contains the text decoration API.
43 */
44 
45 #include <qwidget.h>
46 #include <qtextedit.h>
47 #include <qtextcursor.h>
48 #include <qsharedpointer.h>
49 #include <qmap.h>
50 #include <qvariant.h>
51 #include <QTextBlock>
52 
53 class QTextDecoration;
54 
55 namespace ito {
56 
57 class TextDecorationsSignals;
58 class CodeEditor;
59 
60 /*
61 Helper class to quickly create a text decoration. The text decoration is an
62 utility class that adds a few utility methods to QTextEdit.ExtraSelection.
63 
64 In addition to the helper methods, a tooltip can be added to a decoration.
65 (useful for errors markers and so on...)
66 
67 Text decoration expose a **clicked** signal stored in a separate QObject:
68  :attr:`pyqode.core.api.TextDecoration.Signals`
69 
70 .. code-block:: python
71  deco = TextDecoration()
72  deco.signals.clicked.connect(a_slot)
73  def a_slot(decoration):
74  print(decoration)
75 */
76 struct TextDecoration : public QTextEdit::ExtraSelection
77 {
78 
79 public:
80  typedef QSharedPointer<TextDecoration> Ptr;
81 
83  TextDecoration(const QTextCursor &cursor, int startPos = -1, int endPos = -1, \
84  int startLine = -1, int endLine =-1, int drawOrder = 0, const QString &tooltip = "", \
85  bool fullWidth = false);
86  TextDecoration(QTextDocument *document, int startPos = -1, int endPos = -1, \
87  int startLine = -1, int endLine =-1, int drawOrder = 0, const QString &tooltip = "", \
88  bool fullWidth = false);
89  virtual ~TextDecoration();
90 
91  bool operator==(const TextDecoration &other) const;
92 
93  int drawOrder() const { return m_drawOrder; }
94  void setDrawOrder(int order) { m_drawOrder = order; }
95 
96  QVariantMap &properties() { return m_properties; }
97 
98  void setAsBold();
99  void setForeground(const QColor &color);
100  void setBackground(const QBrush &brush);
101  void setOutline(const QColor &color);
102  void selectLine();
103  void setFullWidth(bool flag = true, bool clear = true);
104 
105  void setAsUnderlined(const QColor &color = QColor("blue"));
106  void setAsSpellCheck(const QColor &color = QColor("blue"));
107  void setAsError(const QColor &color = QColor("red"));
108  void setAsWarning(const QColor &color = QColor("orange"));
109 
110  bool containsCursor(const QTextCursor &cursor) const;
111 
112  void emitClicked(TextDecoration::Ptr selection) const;
113 
114  QString tooltip() const { return m_tooltip; }
115  void setTooltip(const QString &tooltip)
116  {
117  m_tooltip = tooltip;
118  }
119 
120  bool isValid() const { return m_drawOrder >= 0; }
121 
122  QTextBlock block() const { return m_block; }
123  void setBlock(const QTextBlock &block) { m_block = block; }
124 
125  QMetaObject::Connection connect(const char* signal, QObject *receiver, const char *slot);
126 
127 protected:
128  QSharedPointer<TextDecorationsSignals> m_signals;
129  int m_drawOrder;
130  QString m_tooltip;
131  QVariantMap m_properties;
132  QTextBlock m_block;
133 
134 private:
135 
136 };
137 
138 } //end namespace ito
139 
140 Q_DECLARE_METATYPE(ito::TextDecoration)
141 Q_DECLARE_METATYPE(ito::TextDecoration::Ptr)
142 
143 namespace ito {
144 
145 
146 /*
147 Holds the signals for a TextDecoration (since we cannot make it a
148 QObject, we need to store its signals in an external QObject).
149 */
150 class TextDecorationsSignals : public QObject
151 {
152  Q_OBJECT
153 public:
154  TextDecorationsSignals(QObject *parent = NULL) : QObject(parent) {}
155  virtual ~TextDecorationsSignals() {}
156 
157 signals:
158  //Signal emitted when a TextDecoration has been clicked.
159  void clicked(TextDecoration::Ptr selection);
160 };
161 
162 } //end namespace ito
163 
164 
165 #endif
Definition: apiFunctionsGraph.cpp:39
Definition: textDecoration.h:150
Definition: textDecoration.h:76