OSDN Git Service

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