1 /****************************************************************************
2 **
3 ** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
4 **
5 ** This file is part of the Edyuk project <http://edyuk.org>
6 **
7 ** This file may be used under the terms of the GNU General Public License
8 ** version 3 as published by the Free Software Foundation and appearing in the
9 ** file GPL.txt included in the packaging of this file.
10 **
11 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 **
14 ****************************************************************************/
15 
16 #ifndef Header_QDocument_Line
17 #define Header_QDocument_Line
18 
19 #include "qce-config.h"
20 
21 /*!
22 	\file qdocumentline.h
23 	\brief Definition of the QDocumentLine class
24 */
25 
26 #include "qformat.h"
27 
28 class QPoint;
29 class QString;
30 
31 class QDocument;
32 class QDocumentLineHandle;
33 
34 struct QNFAMatchContext;
35 
36 struct QParenthesis
37 {
38 	enum Role
39 	{
40 		Open		= 1,
41 		Close		= 2,
42 		Indent		= 4,
43 		Fold		= 8,
44 		Match		= 16
45 	};
46 
QParenthesisQParenthesis47 	inline QParenthesis()
48 	 : id(0), role(0), offset(0), length(0)
49 	{}
50 
QParenthesisQParenthesis51 	inline QParenthesis(int i, quint8 r, int pos, int len)
52 	 : id(i), role(r), offset(pos), length(len)
53 	{}
54 
55 	int id;
56 	int role;
57 	int offset;
58 	int length;
59 };
60 
61 Q_DECLARE_TYPEINFO(QParenthesis, Q_MOVABLE_TYPE);
62 
63 class QCE_EXPORT QDocumentLine
64 {
65 	friend class QDocumentLineHandle;
66 	friend class QDocumentCursorHandle;
67 
68 	public:
69 		enum LineCookies {
70 			STACK_ENVIRONMENT_COOKIE = 1,
71 			DIFF_LIST_COOCKIE = 2,
72 			UNCLOSED_ENVIRONMENT_COOKIE = 3,
73             LEXER_COOKIE = 4,
74             LEXER_REMAINDER_COOKIE = 5,
75             LEXER_RAW_COOKIE = 6,
76             LEXER_COMMANDSTACK_COOKIE = 7,
77             LEXER_COMMENTSTART_COOKIE = 8,
78 			PICTURE_COOKIE = 42,
79 			PICTURE_COOKIE_DRAWING_POS = 43,
80 			GRAMMAR_ERROR_COOKIE = 44
81 		};
82 
83 		enum State
84 		{
85 			None				= 0,
86 			Hidden				= 1,
87 			CollapsedBlockStart	= 2,
88 			CollapsedBlockEnd	= 4,
89 
90 			LayoutDirty			= 16,
91 			FormatsApplied		= 32,
92 			LayoutedByQTextLayout	= 64
93 		};
94 
95         Q_DECLARE_FLAGS(States, State)
96 
97 		explicit QDocumentLine(QDocument *doc);
98 		QDocumentLine(const QDocumentLine& line);
99         QDocumentLine(QDocumentLineHandle *h = nullptr);
100 
101 		~QDocumentLine();
102 
103 		bool isNull() const;
104 		bool isValid() const;
105 
106 		inline bool operator == (const QDocumentLineHandle* h) const
107 		{
108 			return m_handle == h;
109 		}
110 
111 		inline bool operator != (const QDocumentLineHandle* h) const
112 		{
113 			return m_handle != h;
114 		}
115 
116 		bool operator == (const QDocumentLine& l) const;
117 		bool operator != (const QDocumentLine& l) const;
118 
119         /*bool operator < (const QDocumentLine& l) const;
120 		bool operator >= (const QDocumentLine& l) const;
121 
122 		bool operator > (const QDocumentLine& l) const;
123         bool operator <= (const QDocumentLine& l) const;*/
124 
125 		QDocumentLine& operator = (const QDocumentLine& l);
126 
127 		QString text() const;
128 
129 		int length() const;
130 		int lineSpan() const;
131 
132 		int firstChar() const;
133 		int lastChar() const;
134 		bool startsWith(const QString &txt);
135 
136 		int indent() const;
137 
138 		int nextNonSpaceChar(int pos) const;
139 		int previousNonSpaceChar(int pos) const;
140 
indentation()141 		inline QString indentation() const
142 		{ int idx = firstChar(); return idx != -1 ? text().left(idx) : text(); }
143 
isHidden()144 		inline bool isHidden() const
145 		{ return hasFlag(Hidden); }
146 
147 		bool hasFlag(State s) const;
148 		bool hasAnyFlag(int s) const;
149 		void setFlag(State s, bool y = true);
150 
151 		QDocument* document() const;
152 
153 		int xToCursor(int x) const;
154 		int cursorToX(int cpos) const;
155 
156 		int wrappedLineForCursor(int cpos) const;
157 
158         int documentOffsetToCursor(qreal x, qreal y, bool disallowPositionBeyondLine=false) const;
159 		void cursorToDocumentOffset(int cpos, qreal &x, qreal &y) const;
160 
161 		QPointF cursorToDocumentOffset(int cpos) const;
162 
163 		void addMark(int id);
164 		void removeMark(int id);
165 		void toggleMark(int id);
166 
167 		QList<int> marks() const;
168 		bool hasMark(int id) const;
169 
170 		bool hasOverlay(int fid) const;
171 		QList<QFormatRange> overlays() const;
172 
173 		void clearOverlays();
174 		void clearOverlays(int formatToClear);
175 		void clearOverlays(QList<int> formatsToClear);
176 		void addOverlay(const QFormatRange& over);
177 		void removeOverlay(const QFormatRange& over);
178 		bool hasOverlay(int id);
179 		QList<QFormatRange> getOverlays(int preferredFormat = -1) const;
180 		QFormatRange getOverlayAt(int index, int preferredFormat = -1) const;
181 		QFormatRange getFirstOverlay(int start = 0, int end = -1, int preferredFormat = -1) const;
182 		QFormatRange getLastOverlay(int start = 0, int end = -1, int preferredFormat = -1) const;
183 
184 		void setFormats(const QVector<int>& formats);
185 		QVector<int> compose();
186 		QVector<int> getFormats();
187         int getFormatAt(int pos) const;
188         int getCachedFormatAt(int pos) const;
189 
190 		const QVector<QParenthesis>& parentheses() const;
191 		void setParentheses(const QVector<QParenthesis>& parentheses);
192 
handle()193 		inline QDocumentLineHandle* handle() const
194 		{ return m_handle; }
195 
196 		QNFAMatchContext* matchContext();
197 
198 		QVariant getCookie(int type);
199 		void setCookie(int type,QVariant data);
200 		void removeCookie(int type);
201 
202 		bool isRTLByLayout() const;
203 		bool isRTLByText() const;
204 		QTextLayout* getLayout() const;
205 		int leftCursorPosition(int oldPos) const;
206 		int rightCursorPosition(int oldPos) const;
207 private:
208 		QDocumentLineHandle *m_handle;
209 };
210 
211 Q_DECLARE_OPERATORS_FOR_FLAGS(QDocumentLine::States)
212 
213 #endif
214