OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / basetextdocumentlayout.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #ifndef BASETEXTDOCUMENTLAYOUT_H
34 #define BASETEXTDOCUMENTLAYOUT_H
35
36 #include "texteditor_global.h"
37
38 #include "itexteditor.h"
39
40 #include <QtGui/QTextBlockUserData>
41 #include <QtGui/QPlainTextDocumentLayout>
42
43 namespace TextEditor {
44
45 struct Parenthesis;
46 typedef QVector<Parenthesis> Parentheses;
47
48 struct TEXTEDITOR_EXPORT Parenthesis
49 {
50     enum Type { Opened, Closed };
51
52     inline Parenthesis() : type(Opened), pos(-1) {}
53     inline Parenthesis(Type t, QChar c, int position)
54         : type(t), chr(c), pos(position) {}
55     Type type;
56     QChar chr;
57     int pos;
58 };
59
60 class TEXTEDITOR_EXPORT CodeFormatterData
61 {
62 public:
63     virtual ~CodeFormatterData();
64 };
65
66 class TEXTEDITOR_EXPORT TextBlockUserData : public QTextBlockUserData
67 {
68 public:
69
70     inline TextBlockUserData()
71         : m_folded(false),
72           m_ifdefedOut(false),
73           m_foldingIndent(0),
74           m_lexerState(0),
75           m_foldingStartIncluded(false),
76           m_foldingEndIncluded(false),
77           m_codeFormatterData(0)
78     {}
79     ~TextBlockUserData();
80
81     inline TextMarks marks() const { return m_marks; }
82     void addMark(ITextMark *mark);
83     inline bool removeMark(ITextMark *mark) { return m_marks.removeAll(mark); }
84     inline bool hasMark(ITextMark *mark) const { return m_marks.contains(mark); }
85     inline void clearMarks() { m_marks.clear(); }
86     inline void documentClosing() { Q_FOREACH(ITextMark *tm, m_marks) { tm->documentClosing(); } m_marks.clear();}
87
88     inline void setFolded(bool b) { m_folded = b; }
89     inline bool folded() const { return m_folded; }
90
91     inline void setParentheses(const Parentheses &parentheses) { m_parentheses = parentheses; }
92     inline void clearParentheses() { m_parentheses.clear(); }
93     inline const Parentheses &parentheses() const { return m_parentheses; }
94     inline bool hasParentheses() const { return !m_parentheses.isEmpty(); }
95     int braceDepthDelta() const;
96
97     inline bool setIfdefedOut() { bool result = m_ifdefedOut; m_ifdefedOut = true; return !result; }
98     inline bool clearIfdefedOut() { bool result = m_ifdefedOut; m_ifdefedOut = false; return result;}
99     inline bool ifdefedOut() const { return m_ifdefedOut; }
100
101
102     enum MatchType { NoMatch, Match, Mismatch  };
103     static MatchType checkOpenParenthesis(QTextCursor *cursor, QChar c);
104     static MatchType checkClosedParenthesis(QTextCursor *cursor, QChar c);
105     static MatchType matchCursorBackward(QTextCursor *cursor);
106     static MatchType matchCursorForward(QTextCursor *cursor);
107     static bool findPreviousOpenParenthesis(QTextCursor *cursor, bool select = false);
108     static bool findNextClosingParenthesis(QTextCursor *cursor, bool select = false);
109
110     static bool findPreviousBlockOpenParenthesis(QTextCursor *cursor, bool checkStartPosition = false);
111     static bool findNextBlockClosingParenthesis(QTextCursor *cursor);
112
113     // Get the code folding level
114     inline int foldingIndent() const { return m_foldingIndent; }
115     /* Set the code folding level.
116      *
117      * A code folding marker will appear the line *before* the one where the indention
118      * level increases. The code folding reagion will end in the last line that has the same
119      * indention level (or higher).
120      */
121     inline void setFoldingIndent(int indent) { m_foldingIndent = indent; }
122     // Set whether the first character of the folded region will show when the code is folded.
123     inline void setFoldingStartIncluded(bool included) { m_foldingStartIncluded = included; }
124     inline bool foldingStartIncluded() const { return m_foldingStartIncluded; }
125     // Set whether the last character of the folded region will show when the code is folded.
126     inline void setFoldingEndIncluded(bool included) { m_foldingEndIncluded = included; }
127     inline bool foldingEndIncluded() const { return m_foldingEndIncluded; }
128     inline int lexerState() const { return m_lexerState; }
129     inline void setLexerState(int state) {m_lexerState = state; }
130
131
132     CodeFormatterData *codeFormatterData() const { return m_codeFormatterData; }
133     void setCodeFormatterData(CodeFormatterData *data);
134
135 private:
136     TextMarks m_marks;
137     uint m_folded : 1;
138     uint m_ifdefedOut : 1;
139     uint m_foldingIndent : 16;
140     uint m_lexerState : 4;
141     uint m_foldingStartIncluded : 1;
142     uint m_foldingEndIncluded : 1;
143     Parentheses m_parentheses;
144     CodeFormatterData *m_codeFormatterData;
145 };
146
147
148 class TEXTEDITOR_EXPORT BaseTextDocumentLayout : public QPlainTextDocumentLayout
149 {
150     Q_OBJECT
151
152 public:
153     BaseTextDocumentLayout(QTextDocument *doc);
154     ~BaseTextDocumentLayout();
155
156     static void setParentheses(const QTextBlock &block, const Parentheses &parentheses);
157     static void clearParentheses(const QTextBlock &block) { setParentheses(block, Parentheses());}
158     static Parentheses parentheses(const QTextBlock &block);
159     static bool hasParentheses(const QTextBlock &block);
160     static bool setIfdefedOut(const QTextBlock &block);
161     static bool clearIfdefedOut(const QTextBlock &block);
162     static bool ifdefedOut(const QTextBlock &block);
163     static int braceDepthDelta(const QTextBlock &block);
164     static int braceDepth(const QTextBlock &block);
165     static void setBraceDepth(QTextBlock &block, int depth);
166     static void changeBraceDepth(QTextBlock &block, int delta);
167     static void setFoldingIndent(const QTextBlock &block, int indent);
168     static int foldingIndent(const QTextBlock &block);
169     static void setLexerState(const QTextBlock &block, int state);
170     static int lexerState(const QTextBlock &block);
171     static void changeFoldingIndent(QTextBlock &block, int delta);
172     static bool canFold(const QTextBlock &block);
173     static void doFoldOrUnfold(const QTextBlock& block, bool unfold);
174     static bool isFolded(const QTextBlock &block);
175     static void setFolded(const QTextBlock &block, bool folded);
176
177     static TextBlockUserData *testUserData(const QTextBlock &block) {
178         return static_cast<TextBlockUserData*>(block.userData());
179     }
180     static TextBlockUserData *userData(const QTextBlock &block) {
181         TextBlockUserData *data = static_cast<TextBlockUserData*>(block.userData());
182         if (!data && block.isValid())
183             const_cast<QTextBlock &>(block).setUserData((data = new TextBlockUserData));
184         return data;
185     }
186
187
188     void emitDocumentSizeChanged() { emit documentSizeChanged(documentSize()); }
189     int lastSaveRevision;
190     bool hasMarks;
191
192     int m_requiredWidth;
193     void setRequiredWidth(int width);
194
195     QSizeF documentSize() const;
196
197 };
198
199 } // namespace TextEditor
200
201 #endif // BASETEXTDOCUMENTLAYOUT_H