-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtextcursor_p.h
214 lines (195 loc) · 7.51 KB
/
textcursor_p.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2010 Anders Bakken
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef TEXTCURSOR_P_H
#define TEXTCURSOR_P_H
#include <QSize>
#include <QList>
#include <QObject>
#include <QHash>
#include <QAtomicInt>
#include <QCoreApplication>
#include <QTextCursor>
#include "textlayout_p.h"
#include "textedit.h"
#include "textedit_p.h"
static inline bool match(const TextCursor &cursor, const TextLayout *layout, int lines)
{
Q_ASSERT(cursor.document() == layout->document);
if (cursor.viewportWidth() != -1 && cursor.viewportWidth() != layout->viewport) {
return false;
}
if (layout->viewportPosition > cursor.position()
|| layout->layoutEnd < cursor.position()) {
return false;
}
int index = -1;
if (!layout->layoutForPosition(cursor.position(), 0, &index)) {
// ### need an interface for this if I am going to have a mode
// ### that doesn't break lines
return false;
}
if (index < lines && layout->viewportPosition > 0) {
return false; // need more margin before the cursor
} else if (layout->textLayouts.size() - index - 1 < lines && layout->layoutEnd < layout->document->documentSize()) {
return false; // need more margin after cursor
}
return true;
}
class TextLayoutCacheManager : public QObject
{
Q_OBJECT
public:
static TextLayoutCacheManager *instance()
{
static TextLayoutCacheManager *inst = new TextLayoutCacheManager(QCoreApplication::instance());
return inst;
}
// ### this class doesn't react to TextSections added or removed. I
// ### don't think it needs to since it's only being used for
// ### cursor movement which shouldn't be impacted by these things
static TextLayout *requestLayout(const TextCursor &cursor, int margin)
{
Q_ASSERT(cursor.document());
if (cursor.textEdit && match(cursor, cursor.textEdit->d, margin)) {
return cursor.textEdit->d;
}
TextDocument *doc = cursor.document();
Q_ASSERT(doc);
QList<TextLayout*> &layouts = instance()->cache[doc];
Q_ASSERT(cursor.document());
foreach(TextLayout *l, layouts) {
if (match(cursor, l, margin)) {
return l;
}
}
if (layouts.size() < instance()->maxLayouts) {
if (layouts.isEmpty()) {
connect(cursor.document(), SIGNAL(charactersAdded(int, int)),
instance(), SLOT(onCharactersAddedOrRemoved(int)));
connect(cursor.document(), SIGNAL(charactersRemoved(int, int)),
instance(), SLOT(onCharactersAddedOrRemoved(int)));
connect(cursor.document(), SIGNAL(destroyed(QObject*)),
instance(), SLOT(onDocumentDestroyed(QObject*)));
}
layouts.append(new TextLayout(doc));
}
TextLayout *l = layouts.last();
l->viewport = cursor.viewportWidth();
if (l->viewport == -1)
l->viewport = INT_MAX - 1024; // prevent overflow in comparisons.
if (cursor.textEdit) {
l->textEdit = cursor.textEdit;
l->suppressTextEditUpdates = true;
l->lineBreaking = cursor.textEdit->lineBreaking();
l->sections = cursor.textEdit->d->sections;
l->font = cursor.textEdit->font();
l->syntaxHighlighters = cursor.textEdit->syntaxHighlighters();
// l->extraSelections = cursor.textEdit->extraSelections();
// ### can the extra selections impact layout? If so they
// ### need to be in the actual textLayout shouldn't need
// ### to care about the actual selection
}
int startPos = (cursor.position() == 0
? 0
: qMax(0, doc->find(QLatin1Char('\n'), cursor.position() - 1, TextDocument::FindBackward).anchor()));
// We start at the beginning of the current line
int linesAbove = margin;
if (startPos > 0) {
while (linesAbove > 0) {
const TextCursor c = doc->find(QLatin1Char('\n'), startPos - 1, TextDocument::FindBackward);
if (c.isNull()) {
startPos = 0;
break;
}
startPos = c.anchor();
ASSUME(c.anchor() == 0 || c.cursorCharacter() == QLatin1Char('\n'));
ASSUME(c.anchor() == 0 || doc->readCharacter(c.anchor()) == QLatin1Char('\n'));
ASSUME(c.cursorCharacter() == doc->readCharacter(c.anchor()));
--linesAbove;
}
}
int linesBelow = margin;
int endPos = cursor.position();
if (endPos < doc->documentSize()) {
while (linesBelow > 0) {
const TextCursor c = doc->find(QLatin1Char('\n'), endPos + 1);
if (c.isNull()) {
endPos = doc->documentSize();
break;
}
endPos = c.anchor();
--linesBelow;
}
}
if (startPos > 0)
++startPos; // in this case startPos points to the newline before it
l->viewportPosition = startPos;
l->layoutDirty = true;
ASSUME(l->viewportPosition == 0 || doc->readCharacter(l->viewportPosition - 1) == QLatin1Char('\n'));
l->relayoutByPosition(endPos - startPos + 100); // ### fudged a couple of lines likely
ASSUME(l->viewportPosition < l->layoutEnd
|| (l->viewportPosition == l->layoutEnd && l->viewportPosition == doc->documentSize()));
ASSUME(l->textLayouts.size() > margin * 2 || l->viewportPosition == 0 || l->layoutEnd == doc->documentSize());
return l;
}
private slots:
void onDocumentDestroyed(QObject *o)
{
qDeleteAll(cache.take(static_cast<TextDocument*>(o)));
}
void onCharactersAddedOrRemoved(int pos)
{
QList<TextLayout*> &layouts = cache[qobject_cast<TextDocument*>(sender())];
ASSUME(!layouts.isEmpty());
for (int i=layouts.size() - 1; i>=0; --i) {
TextLayout *l = layouts.at(i);
if (pos <= l->layoutEnd) {
delete l;
layouts.removeAt(i);
}
}
if (layouts.isEmpty()) {
disconnect(sender(), 0, this, 0);
cache.remove(qobject_cast<TextDocument*>(sender()));
}
}
private:
TextLayoutCacheManager(QObject *parent)
: QObject(parent)
{
maxLayouts = qMax(1, qgetenv("TEXTCURSOR_MAX_CACHED_TEXTLAYOUTS").toInt());
}
int maxLayouts;
QHash<TextDocument*, QList<TextLayout*> > cache;
};
class TextLayout;
struct TextCursorSharedPrivate
{
public:
TextCursorSharedPrivate() : ref(1), position(-1), anchor(-1),
overrideColumn(-1), viewportWidth(-1),
document(0)
{}
~TextCursorSharedPrivate()
{
ASSUME(ref == 0);
}
void invalidate()
{
}
mutable QAtomicInt ref;
int position, anchor, overrideColumn, viewportWidth;
TextDocument *document;
};
#endif