basetexteditor_p.h 9.56 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32

con's avatar
con committed
33
34
35
36
#ifndef BASETEXTEDITOR_P_H
#define BASETEXTEDITOR_P_H

#include "basetexteditor.h"
37
#include "behaviorsettings.h"
38
39
#include "displaysettings.h"
#include "fontsettings.h"
40
#include "refactoroverlay.h"
41

42
#include <utils/changeset.h>
con's avatar
con committed
43

44
45
46
47
#include <QBasicTimer>
#include <QSharedData>
#include <QPointer>
#include <QScopedPointer>
con's avatar
con committed
48
49
50
51

namespace TextEditor {

class BaseTextDocument;
52
class TextEditorActionHandler;
Leandro Melo's avatar
Leandro Melo committed
53
class CodeAssistant;
con's avatar
con committed
54
55

namespace Internal {
56
class TextEditorOverlay;
con's avatar
con committed
57

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class TEXTEDITOR_EXPORT BaseTextBlockSelection
{
public:

    bool isValid() const{ return !firstBlock.isNull() && !lastBlock.isNull(); }
    void clear() { firstBlock = lastBlock = QTextCursor(); }

    QTextCursor firstBlock; // defines the first block
    QTextCursor lastBlock; // defines the last block
    int firstVisualColumn; // defines the first visual column of the selection
    int lastVisualColumn; // defines the last visual column of the selection
    enum Anchor {TopLeft = 0, TopRight, BottomLeft, BottomRight} anchor;
    BaseTextBlockSelection():firstVisualColumn(0), lastVisualColumn(0), anchor(BottomRight){}
    void moveAnchor(int blockNumber, int visualColumn);
    inline int anchorColumnNumber() const { return (anchor % 2) ? lastVisualColumn : firstVisualColumn; }
    inline int anchorBlockNumber() const {
        return (anchor <= TopRight ? firstBlock.blockNumber() : lastBlock.blockNumber()); }
    QTextCursor selection(const TabSettings &ts) const;
    void fromSelection(const TabSettings &ts, const QTextCursor &selection);
};

con's avatar
con committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//========== Pointers with reference count ==========

template <class T> class QRefCountData : public QSharedData
{
public:
    QRefCountData(T *data) { m_data = data; }

    ~QRefCountData() { delete m_data; }

    T *m_data;
};

/* MOSTLY COPIED FROM QSHAREDDATA(-POINTER) */
template <class T> class QRefCountPointer
{
public:
    inline T &operator*() { return d ? *(d->m_data) : 0; }
    inline const T &operator*() const { return d ? *(d->m_data) : 0; }
    inline T *operator->() { return d ? d->m_data : 0; }
    inline const T *operator->() const { return d ? d->m_data : 0; }
    inline operator T *() { return d ? d->m_data : 0; }
    inline operator const T *() const { return d ? d->m_data : 0; }

    inline bool operator==(const QRefCountPointer<T> &other) const { return d == other.d; }
    inline bool operator!=(const QRefCountPointer<T> &other) const { return d != other.d; }

    inline QRefCountPointer() { d = 0; }
    inline ~QRefCountPointer() { if (d && !d->ref.deref()) delete d; }

    explicit QRefCountPointer(T *data) {
        if (data) {
            d = new QRefCountData<T>(data);
            d->ref.ref();
        }
        else {
            d = 0;
        }
    }
    inline QRefCountPointer(const QRefCountPointer<T> &o) : d(o.d) { if (d) d->ref.ref(); }
    inline QRefCountPointer<T> & operator=(const QRefCountPointer<T> &o) {
        if (o.d != d) {
            if (d && !d->ref.deref())
                delete d;
            //todo: atomic assign of pointers
            d = o.d;
            if (d)
                d->ref.ref();
        }
        return *this;
    }
    inline QRefCountPointer &operator=(T *o) {
        if (d == 0 || d->m_data != o) {
            if (d && !d->ref.deref())
                delete d;
            d = new QRefCountData<T>(o);
            if (d)
                d->ref.ref();
        }
        return *this;
    }

    inline bool operator!() const { return !d; }

private:
    QRefCountData<T> *d;
};

//================BaseTextEditorPrivate==============

148
149
150
151
152
struct BaseTextEditorPrivateHighlightBlocks
{
    QList<int> open;
    QList<int> close;
    QList<int> visualIndent;
mae's avatar
mae committed
153
    inline int count() const { return visualIndent.size(); }
154
    inline bool isEmpty() const { return open.isEmpty() || close.isEmpty() || visualIndent.isEmpty(); }
155
156
157
158
159
160
    inline bool operator==(const BaseTextEditorPrivateHighlightBlocks &o) const {
        return (open == o.open && close == o.close && visualIndent == o.visualIndent);
    }
    inline bool operator!=(const BaseTextEditorPrivateHighlightBlocks &o) const { return !(*this == o); }
};

161

162
class BaseTextEditorWidgetPrivate
con's avatar
con committed
163
{
164
165
    BaseTextEditorWidgetPrivate(const BaseTextEditorWidgetPrivate &);
    BaseTextEditorWidgetPrivate &operator=(const BaseTextEditorWidgetPrivate &);
con's avatar
con committed
166
167

public:
168
169
    BaseTextEditorWidgetPrivate();
    ~BaseTextEditorWidgetPrivate();
con's avatar
con committed
170
171
172
173
174
175
176
177
178
179
180

    void setupBasicEditActions(TextEditorActionHandler *actionHandler);
    void setupDocumentSignals(BaseTextDocument *document);
    void updateLineSelectionColor();

    void print(QPrinter *printer);

    QTextBlock m_firstVisible;
    int m_lastScrollPos;
    int m_lineNumber;

181
    BaseTextEditorWidget *q;
con's avatar
con committed
182
    bool m_contentsChanged;
183
    bool m_lastCursorChangeWasInteresting;
con's avatar
con committed
184
185
186
187
188
189

    QList<QTextEdit::ExtraSelection> m_syntaxHighlighterSelections;
    QTextEdit::ExtraSelection m_lineSelection;

    QRefCountPointer<BaseTextDocument> m_document;
    QByteArray m_tempState;
190
    QByteArray m_tempNavigationState;
con's avatar
con committed
191
192
193
194
195

    QString m_displayName;
    bool m_parenthesesMatchingEnabled;
    QTimer *m_updateTimer;

196
197
    Utils::ChangeSet m_changeSet;

con's avatar
con committed
198
199
200
201
202
203
204
205
206
    // parentheses matcher
    bool m_formatRange;
    QTextCharFormat m_matchFormat;
    QTextCharFormat m_mismatchFormat;
    QTextCharFormat m_rangeFormat;
    QTimer *m_parenthesesMatchingTimer;
    // end parentheses matcher

    QWidget *m_extraArea;
207

208
    QString m_tabSettingsId;
Jarek Kobus's avatar
Jarek Kobus committed
209
    ICodeStylePreferences *m_codeStylePreferences;
con's avatar
con committed
210
    DisplaySettings m_displaySettings;
211
212
    FontSettings m_fontSettings;
    BehaviorSettings m_behaviorSettings;
con's avatar
con committed
213
214
215

    int extraAreaSelectionAnchorBlockNumber;
    int extraAreaToggleMarkBlockNumber;
mae's avatar
mae committed
216
    int extraAreaHighlightFoldedBlockNumber;
con's avatar
con committed
217

218
    TextEditorOverlay *m_overlay;
219
    TextEditorOverlay *m_snippetOverlay;
220
    TextEditorOverlay *m_searchResultOverlay;
221
    bool snippetCheckCursor(const QTextCursor &cursor);
222
    void snippetTabOrBacktab(bool forward);
mae's avatar
mae committed
223
224
    QTextCharFormat m_occurrencesFormat;
    QTextCharFormat m_occurrenceRenameFormat;
225

226
227
    RefactorOverlay *m_refactorOverlay;

mae's avatar
mae committed
228
229
230
231
232
233
    QBasicTimer foldedBlockTimer;
    int visibleFoldedBlockNumber;
    int suggestedVisibleFoldedBlockNumber;
    void clearVisibleFoldedBlock();
    bool m_mouseOnFoldedMarker;
    void foldLicenseHeader();
con's avatar
con committed
234
235
236
237

    QBasicTimer autoScrollTimer;
    uint m_marksVisible : 1;
    uint m_codeFoldingVisible : 1;
238
    uint m_codeFoldingSupported : 1;
con's avatar
con committed
239
240
241
242
243
    uint m_revisionsVisible : 1;
    uint m_lineNumbersVisible : 1;
    uint m_highlightCurrentLine : 1;
    uint m_requestMarkEnabled : 1;
    uint m_lineSeparatorsAllowed : 1;
244
245
    uint autoParenthesisOverwriteBackup : 1;
    uint surroundWithEnabledOverwriteBackup : 1;
246
    uint m_maybeFakeTooltipEvent : 1;
247
    uint m_isCirculatingClipboard: 1;
con's avatar
con committed
248
249
    int m_visibleWrapColumn;

250
    QTextCharFormat m_linkFormat;
251
    BaseTextEditorWidget::Link m_currentLink;
252
    bool m_linkPressed;
253

con's avatar
con committed
254
255
256
    QTextCharFormat m_ifdefedOutFormat;

    QRegExp m_searchExpr;
257
    Find::FindFlags m_findFlags;
con's avatar
con committed
258
259
260
    QTextCharFormat m_searchResultFormat;
    QTextCharFormat m_searchScopeFormat;
    QTextCharFormat m_currentLineFormat;
261
    QTextCharFormat m_currentLineNumberFormat;
mae's avatar
mae committed
262
    void highlightSearchResults(const QTextBlock &block, TextEditorOverlay *overlay);
263
    QTimer *m_delayedUpdateTimer;
con's avatar
con committed
264

265
    BaseTextEditor *m_editor;
con's avatar
con committed
266
267
268

    QObject *m_actionHack;

269
    QList<QTextEdit::ExtraSelection> m_extraSelections[BaseTextEditorWidget::NExtraSelectionKinds];
con's avatar
con committed
270
271
272
273
274
275

    // block selection mode
    bool m_inBlockSelectionMode;
    void clearBlockSelection();
    QString copyBlockSelection();
    void removeBlockSelection(const QString &text = QString());
276
    bool m_moveLineUndoHack;
277

mae's avatar
mae committed
278
279
    QTextCursor m_findScopeStart;
    QTextCursor m_findScopeEnd;
mae's avatar
mae committed
280
281
282
    int m_findScopeVerticalBlockSelectionFirstColumn;
    int m_findScopeVerticalBlockSelectionLastColumn;

283
    QTextCursor m_selectBlockAnchor;
con's avatar
con committed
284

mae's avatar
mae committed
285
286
    Internal::BaseTextBlockSelection m_blockSelection;

287
    void moveCursorVisible(bool ensureVisible = true);
288
289
290
291
292

    int visualIndent(const QTextBlock &block) const;
    BaseTextEditorPrivateHighlightBlocks m_highlightBlocksInfo;
    QTimer *m_highlightBlocksTimer;

Leandro Melo's avatar
Leandro Melo committed
293
294
    QScopedPointer<CodeAssistant> m_codeAssistant;
    bool m_assistRelevantContentAdded;
295

296
    QPointer<BaseTextEditorAnimator> m_animator;
297
    int m_cursorBlockNumber;
298

299
    QScopedPointer<AutoCompleter> m_autoCompleter;
300
    QScopedPointer<Indenter> m_indenter;
con's avatar
con committed
301
302
303
304
305
306
};

} // namespace Internal
} // namespace TextEditor

#endif // BASETEXTEDITOR_P_H