|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
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 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
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 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 |
|
43 #include <QtTest/QtTest> |
|
44 |
|
45 |
|
46 #include <qtextdocument.h> |
|
47 #include <qtexttable.h> |
|
48 #include <qvariant.h> |
|
49 #include <qtextdocumentfragment.h> |
|
50 #include <qabstracttextdocumentlayout.h> |
|
51 #include <qtextlayout.h> |
|
52 #include <qtextcursor.h> |
|
53 #include <qdebug.h> |
|
54 |
|
55 //TESTED_FILES=gui/text/qtextcursor.cpp gui/text/qtextcursor_p.h |
|
56 |
|
57 QT_FORWARD_DECLARE_CLASS(QTextDocument) |
|
58 |
|
59 class tst_QTextCursor : public QObject |
|
60 { |
|
61 Q_OBJECT |
|
62 |
|
63 public: |
|
64 tst_QTextCursor(); |
|
65 |
|
66 |
|
67 public slots: |
|
68 void init(); |
|
69 void cleanup(); |
|
70 private slots: |
|
71 void navigation1(); |
|
72 void navigation2_data(); |
|
73 void navigation2(); |
|
74 void navigation3(); |
|
75 void navigation4(); |
|
76 void navigation5(); |
|
77 void navigation6(); |
|
78 void navigation7(); |
|
79 void navigation8(); |
|
80 void navigation9(); |
|
81 void navigation10(); |
|
82 void movePositionEndOfLine(); |
|
83 void insertBlock(); |
|
84 void insertWithBlockSeparator1(); |
|
85 void insertWithBlockSeparator2(); |
|
86 void insertWithBlockSeparator3(); |
|
87 void insertWithBlockSeparator4(); |
|
88 void clearObjectType1(); |
|
89 void clearObjectType2(); |
|
90 void clearObjectType3(); |
|
91 void comparisonOperators1(); |
|
92 void comparisonOperators2(); |
|
93 void selection1(); |
|
94 void dontCopyTableAttributes(); |
|
95 |
|
96 void checkFrame1(); |
|
97 void checkFrame2(); |
|
98 |
|
99 void tableMovement(); |
|
100 void selectionsInTable(); |
|
101 |
|
102 void insertBlockToUseCharFormat(); |
|
103 |
|
104 void selectedText(); |
|
105 |
|
106 void insertBlockShouldRemoveSelection(); |
|
107 void insertBlockShouldRemoveSelection2(); |
|
108 void mergeCellShouldUpdateSelection(); |
|
109 |
|
110 void joinPreviousEditBlock(); |
|
111 |
|
112 void setBlockFormatInTable(); |
|
113 |
|
114 void blockCharFormat(); |
|
115 void blockCharFormat2(); |
|
116 void blockCharFormat3(); |
|
117 void blockCharFormatOnSelection(); |
|
118 |
|
119 void anchorInitialized1(); |
|
120 void anchorInitialized2(); |
|
121 void anchorInitialized3(); |
|
122 |
|
123 void selectWord(); |
|
124 void selectWordWithSeparators_data(); |
|
125 void selectWordWithSeparators(); |
|
126 void startOfWord(); |
|
127 void selectBlock(); |
|
128 void selectVisually(); |
|
129 |
|
130 void insertText(); |
|
131 |
|
132 void insertFragmentShouldUseCurrentCharFormat(); |
|
133 |
|
134 void endOfLine(); |
|
135 |
|
136 void editBlocksDuringRemove(); |
|
137 |
|
138 void update_data(); |
|
139 void update(); |
|
140 |
|
141 void disallowSettingObjectIndicesOnCharFormats(); |
|
142 |
|
143 void blockAndColumnNumber(); |
|
144 |
|
145 void clearCells(); |
|
146 |
|
147 void task244408_wordUnderCursor_data(); |
|
148 void task244408_wordUnderCursor(); |
|
149 |
|
150 void adjustCursorsOnInsert(); |
|
151 |
|
152 private: |
|
153 int blockCount(); |
|
154 |
|
155 QTextDocument *doc; |
|
156 QTextCursor cursor; |
|
157 }; |
|
158 |
|
159 Q_DECLARE_METATYPE(QList<QVariant>) |
|
160 |
|
161 tst_QTextCursor::tst_QTextCursor() |
|
162 {} |
|
163 |
|
164 void tst_QTextCursor::init() |
|
165 { |
|
166 doc = new QTextDocument; |
|
167 cursor = QTextCursor(doc); |
|
168 } |
|
169 |
|
170 void tst_QTextCursor::cleanup() |
|
171 { |
|
172 cursor = QTextCursor(); |
|
173 delete doc; |
|
174 doc = 0; |
|
175 } |
|
176 |
|
177 void tst_QTextCursor::navigation1() |
|
178 { |
|
179 |
|
180 cursor.insertText("Hello World"); |
|
181 QVERIFY(doc->toPlainText() == "Hello World"); |
|
182 |
|
183 cursor.movePosition(QTextCursor::End); |
|
184 QVERIFY(cursor.position() == 11); |
|
185 cursor.deletePreviousChar(); |
|
186 QVERIFY(cursor.position() == 10); |
|
187 cursor.deletePreviousChar(); |
|
188 cursor.deletePreviousChar(); |
|
189 cursor.deletePreviousChar(); |
|
190 cursor.deletePreviousChar(); |
|
191 cursor.deletePreviousChar(); |
|
192 QVERIFY(doc->toPlainText() == "Hello"); |
|
193 |
|
194 QTextCursor otherCursor(doc); |
|
195 otherCursor.movePosition(QTextCursor::Start); |
|
196 otherCursor.movePosition(QTextCursor::Right); |
|
197 cursor = otherCursor; |
|
198 cursor.movePosition(QTextCursor::Right); |
|
199 QVERIFY(cursor != otherCursor); |
|
200 otherCursor.insertText("Hey"); |
|
201 QVERIFY(cursor.position() == 5); |
|
202 |
|
203 doc->undo(); |
|
204 QVERIFY(cursor.position() == 2); |
|
205 doc->redo(); |
|
206 QVERIFY(cursor.position() == 5); |
|
207 |
|
208 doc->undo(); |
|
209 |
|
210 doc->undo(); |
|
211 QVERIFY(doc->toPlainText() == "Hello World"); |
|
212 |
|
213 cursor.movePosition(QTextCursor::Start); |
|
214 cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 6); |
|
215 QVERIFY(cursor.position() == 6); |
|
216 otherCursor = cursor; |
|
217 otherCursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 2); |
|
218 otherCursor.deletePreviousChar(); |
|
219 otherCursor.deletePreviousChar(); |
|
220 otherCursor.deletePreviousChar(); |
|
221 QVERIFY(cursor.position() == 5); |
|
222 |
|
223 cursor.movePosition(QTextCursor::End); |
|
224 cursor.insertBlock(); |
|
225 { |
|
226 int oldPos = cursor.position(); |
|
227 cursor.movePosition(QTextCursor::End); |
|
228 QVERIFY(cursor.position() == oldPos); |
|
229 } |
|
230 QVERIFY(cursor.atBlockStart()); |
|
231 QVERIFY(cursor.position() == 9); |
|
232 |
|
233 QTextCharFormat fmt; |
|
234 fmt.setForeground(Qt::blue); |
|
235 cursor.insertText("Test", fmt); |
|
236 QVERIFY(fmt == cursor.charFormat()); |
|
237 QVERIFY(cursor.position() == 13); |
|
238 } |
|
239 |
|
240 void tst_QTextCursor::navigation2_data() |
|
241 { |
|
242 QTest::addColumn<QStringList>("sl"); |
|
243 QTest::addColumn<QList<QVariant> >("movement"); |
|
244 QTest::addColumn<int>("finalPos"); |
|
245 |
|
246 QTest::newRow("startBlock1") << QStringList("Happy happy happy joy joy joy") |
|
247 << (QList<QVariant>() << QVariant(QTextCursor::StartOfBlock)) << 0; |
|
248 QTest::newRow("endBlock1") << QStringList("Happy happy happy joy joy joy") |
|
249 << (QList<QVariant>() << QVariant(QTextCursor::StartOfBlock) |
|
250 << QVariant(QTextCursor::EndOfBlock)) << 29; |
|
251 QTest::newRow("startBlock2") << QStringList("Happy happy happy joy joy joy") |
|
252 << (QList<QVariant>() << QVariant(QTextCursor::StartOfBlock) |
|
253 << QVariant(QTextCursor::EndOfBlock) |
|
254 << QVariant(QTextCursor::StartOfBlock)) << 0; |
|
255 QTest::newRow("endBlock2") << QStringList("Happy happy happy joy joy joy") |
|
256 << (QList<QVariant>() << QVariant(QTextCursor::StartOfBlock) |
|
257 << QVariant(QTextCursor::EndOfBlock) |
|
258 << QVariant(QTextCursor::StartOfBlock) |
|
259 << QVariant(QTextCursor::EndOfBlock) |
|
260 ) << 29; |
|
261 QTest::newRow("multiBlock1") << (QStringList() << QString("Happy happy happy") |
|
262 << QString("Joy Joy Joy")) |
|
263 << (QList<QVariant>() << QVariant(QTextCursor::StartOfBlock)) |
|
264 << 18; |
|
265 QTest::newRow("multiBlock2") << (QStringList() << QString("Happy happy happy") |
|
266 << QString("Joy Joy Joy")) |
|
267 << (QList<QVariant>() << QVariant(QTextCursor::StartOfBlock) |
|
268 << QVariant(QTextCursor::EndOfBlock)) |
|
269 << 29; |
|
270 QTest::newRow("multiBlock3") << (QStringList() << QString("Happy happy happy") |
|
271 << QString("Joy Joy Joy")) |
|
272 << (QList<QVariant>() << QVariant(QTextCursor::StartOfBlock) |
|
273 << QVariant(QTextCursor::StartOfBlock)) |
|
274 << 18; |
|
275 QTest::newRow("multiBlock4") << (QStringList() << QString("Happy happy happy") |
|
276 << QString("Joy Joy Joy")) |
|
277 << (QList<QVariant>() << QVariant(QTextCursor::Start) |
|
278 << QVariant(QTextCursor::EndOfBlock)) |
|
279 << 17; |
|
280 QTest::newRow("multiBlock5") << (QStringList() << QString("Happy happy happy") |
|
281 << QString("Joy Joy Joy")) |
|
282 << (QList<QVariant>() << QVariant(QTextCursor::Start) |
|
283 << QVariant(QTextCursor::EndOfBlock) |
|
284 << QVariant(QTextCursor::EndOfBlock)) |
|
285 << 17; |
|
286 QTest::newRow("multiBlock6") << (QStringList() << QString("Happy happy happy") |
|
287 << QString("Joy Joy Joy")) |
|
288 << (QList<QVariant>() << QVariant(QTextCursor::End) |
|
289 << QVariant(QTextCursor::StartOfBlock)) |
|
290 << 18; |
|
291 QTest::newRow("multiBlock7") << (QStringList() << QString("Happy happy happy") |
|
292 << QString("Joy Joy Joy")) |
|
293 << (QList<QVariant>() << QVariant(QTextCursor::PreviousBlock)) |
|
294 << 0; |
|
295 QTest::newRow("multiBlock8") << (QStringList() << QString("Happy happy happy") |
|
296 << QString("Joy Joy Joy")) |
|
297 << (QList<QVariant>() << QVariant(QTextCursor::PreviousBlock) |
|
298 << QVariant(QTextCursor::EndOfBlock)) |
|
299 << 17; |
|
300 QTest::newRow("multiBlock9") << (QStringList() << QString("Happy happy happy") |
|
301 << QString("Joy Joy Joy")) |
|
302 << (QList<QVariant>() << QVariant(QTextCursor::PreviousBlock) |
|
303 << QVariant(QTextCursor::NextBlock)) |
|
304 << 18; |
|
305 QTest::newRow("multiBlock10") << (QStringList() << QString("Happy happy happy") |
|
306 << QString("Joy Joy Joy")) |
|
307 << (QList<QVariant>() << QVariant(QTextCursor::PreviousBlock) |
|
308 << QVariant(QTextCursor::NextBlock) |
|
309 << QVariant(QTextCursor::NextBlock)) |
|
310 << 18; |
|
311 QTest::newRow("multiBlock11") << (QStringList() << QString("Happy happy happy") |
|
312 << QString("Joy Joy Joy")) |
|
313 << (QList<QVariant>() << QVariant(QTextCursor::PreviousBlock) |
|
314 << QVariant(QTextCursor::NextBlock) |
|
315 << QVariant(QTextCursor::EndOfBlock)) |
|
316 << 29; |
|
317 QTest::newRow("PreviousWord1") << (QStringList() << QString("Happy happy happy Joy Joy Joy")) |
|
318 << (QList<QVariant>() << QVariant(QTextCursor::PreviousWord)) |
|
319 << 26; |
|
320 QTest::newRow("PreviousWord2") << (QStringList() << QString("Happy happy happy Joy Joy Joy")) |
|
321 << (QList<QVariant>() << QVariant(QTextCursor::PreviousWord) |
|
322 << QVariant(QTextCursor::PreviousWord)) |
|
323 << 22; |
|
324 QTest::newRow("EndWord1") << (QStringList() << QString("Happy happy happy Joy Joy Joy")) |
|
325 << (QList<QVariant>() << QVariant(QTextCursor::PreviousWord) |
|
326 << QVariant(QTextCursor::PreviousWord) |
|
327 << QVariant(QTextCursor::EndOfWord)) |
|
328 << 25; |
|
329 QTest::newRow("NextWord1") << (QStringList() << QString("Happy happy happy Joy Joy Joy")) |
|
330 << (QList<QVariant>() << QVariant(QTextCursor::PreviousWord) |
|
331 << QVariant(QTextCursor::PreviousWord) |
|
332 << QVariant(QTextCursor::NextWord)) |
|
333 << 26; |
|
334 QTest::newRow("NextWord2") << (QStringList() << QString("Happy happy happy Joy Joy Joy")) |
|
335 << (QList<QVariant>() << QVariant(QTextCursor::Start) |
|
336 << QVariant(QTextCursor::NextWord) |
|
337 << QVariant(QTextCursor::EndOfWord)) |
|
338 << 11; |
|
339 QTest::newRow("StartWord1") << (QStringList() << QString("Happy happy happy Joy Joy Joy")) |
|
340 << (QList<QVariant>() << QVariant(QTextCursor::PreviousWord) |
|
341 << QVariant(QTextCursor::PreviousWord) |
|
342 << QVariant(QTextCursor::StartOfWord)) |
|
343 << 22; |
|
344 QTest::newRow("StartWord3") << (QStringList() << QString("Happy happy happy Joy Joy Joy")) |
|
345 << (QList<QVariant>() << QVariant(QTextCursor::Start) |
|
346 << QVariant(QTextCursor::NextWord) |
|
347 << QVariant(QTextCursor::EndOfWord) |
|
348 << QVariant(QTextCursor::StartOfWord)) |
|
349 << 6; |
|
350 |
|
351 QTest::newRow("PreviousCharacter") << (QStringList() << QString("Happy happy Joy Joy")) |
|
352 << (QList<QVariant>() << QVariant(QTextCursor::PreviousCharacter) |
|
353 << QVariant(QTextCursor::PreviousCharacter)) |
|
354 << 17; |
|
355 } |
|
356 |
|
357 void tst_QTextCursor::navigation2() |
|
358 { |
|
359 QFETCH(QStringList, sl); |
|
360 QFETCH(QList<QVariant>, movement); |
|
361 int i; |
|
362 for (i = 0; i < sl.size(); ++i) { |
|
363 cursor.insertText(sl.at(i)); |
|
364 if (i < sl.size() - 1) |
|
365 cursor.insertBlock(); |
|
366 } |
|
367 |
|
368 for (i = 0; i < movement.size(); ++i) |
|
369 cursor.movePosition(QTextCursor::MoveOperation(movement.at(i).toInt())); |
|
370 QTEST(cursor.position(), "finalPos"); |
|
371 } |
|
372 |
|
373 void tst_QTextCursor::navigation3() |
|
374 { |
|
375 cursor.insertText("a"); |
|
376 cursor.deletePreviousChar(); |
|
377 QCOMPARE(cursor.position(), 0); |
|
378 QVERIFY(doc->toPlainText().isEmpty()); |
|
379 } |
|
380 |
|
381 void tst_QTextCursor::navigation4() |
|
382 { |
|
383 cursor.insertText(" Test "); |
|
384 |
|
385 cursor.setPosition(4); |
|
386 cursor.movePosition(QTextCursor::EndOfWord); |
|
387 QCOMPARE(cursor.position(), 6); |
|
388 } |
|
389 |
|
390 void tst_QTextCursor::navigation5() |
|
391 { |
|
392 cursor.insertText("Test"); |
|
393 cursor.insertBlock(); |
|
394 cursor.insertText("Test"); |
|
395 |
|
396 cursor.setPosition(0); |
|
397 cursor.movePosition(QTextCursor::EndOfBlock); |
|
398 QCOMPARE(cursor.position(), 4); |
|
399 } |
|
400 |
|
401 void tst_QTextCursor::navigation6() |
|
402 { |
|
403 // triger creation of document layout, so that QTextLines are there |
|
404 doc->documentLayout(); |
|
405 doc->setTextWidth(1000); |
|
406 |
|
407 cursor.insertText("Test "); |
|
408 |
|
409 cursor.movePosition(QTextCursor::Start); |
|
410 cursor.movePosition(QTextCursor::EndOfLine); |
|
411 QCOMPARE(cursor.position(), 8); |
|
412 } |
|
413 |
|
414 void tst_QTextCursor::navigation7() |
|
415 { |
|
416 QVERIFY(doc->isEmpty()); |
|
417 for (int i = QTextCursor::Start; i <= QTextCursor::WordRight; ++i) |
|
418 QVERIFY(!cursor.movePosition(QTextCursor::MoveOperation(i))); |
|
419 |
|
420 doc->setPlainText("Hello World"); |
|
421 cursor.movePosition(QTextCursor::Start); |
|
422 do { |
|
423 } while (cursor.movePosition(QTextCursor::NextCharacter)); |
|
424 QVERIFY(true /*reached*/); |
|
425 } |
|
426 |
|
427 void tst_QTextCursor::navigation8() |
|
428 { |
|
429 cursor.insertList(QTextListFormat::ListDecimal); |
|
430 QCOMPARE(cursor.position(), 1); |
|
431 cursor.insertText("foo"); |
|
432 QCOMPARE(cursor.position(), 4); |
|
433 |
|
434 cursor.insertList(QTextListFormat::ListCircle); |
|
435 QCOMPARE(cursor.position(), 5); |
|
436 cursor.insertText("something"); |
|
437 QCOMPARE(cursor.position(), 14); |
|
438 |
|
439 cursor.movePosition(QTextCursor::PreviousCharacter); |
|
440 QCOMPARE(cursor.position(), 13); |
|
441 |
|
442 cursor.setPosition(2); |
|
443 cursor.movePosition(QTextCursor::NextCharacter); |
|
444 QCOMPARE(cursor.position(), 3); |
|
445 } |
|
446 |
|
447 void tst_QTextCursor::navigation9() |
|
448 { |
|
449 cursor.insertText("Hello &-=+\t World"); |
|
450 cursor.movePosition(QTextCursor::PreviousWord); |
|
451 QCOMPARE(cursor.position(), 15); |
|
452 cursor.movePosition(QTextCursor::PreviousWord); |
|
453 QCOMPARE(cursor.position(), 7); |
|
454 cursor.movePosition(QTextCursor::PreviousWord); |
|
455 QCOMPARE(cursor.position(), 0); |
|
456 cursor.movePosition(QTextCursor::NextWord); |
|
457 QCOMPARE(cursor.position(), 7); |
|
458 cursor.movePosition(QTextCursor::NextWord); |
|
459 QCOMPARE(cursor.position(), 15); |
|
460 } |
|
461 |
|
462 void tst_QTextCursor::navigation10() |
|
463 { |
|
464 doc->setHtml("<html><p>just a simple paragraph.</p>" |
|
465 "<table>" |
|
466 "<tr><td>Cell number 1</td><td>another cell</td><td></td><td>previous</br>is</br>empty</td></tr>" |
|
467 "<tr><td>row 2</td><td colspan=\"2\">foo bar</td><td>last cell</td></tr>" |
|
468 "<tr><td colspan=\"3\">row 3</td><td>a</td></tr>" |
|
469 "</table></html"); |
|
470 QCOMPARE(cursor.position(), 101); // end of document |
|
471 cursor.setPosition(0); |
|
472 QCOMPARE(cursor.position(), 0); |
|
473 bool ok = cursor.movePosition(QTextCursor::EndOfLine); |
|
474 QVERIFY(ok); |
|
475 QCOMPARE(cursor.position(), 24); |
|
476 ok = cursor.movePosition(QTextCursor::NextBlock); |
|
477 QCOMPARE(cursor.position(), 25); // cell 1 |
|
478 ok = cursor.movePosition(QTextCursor::NextCell); |
|
479 QVERIFY(ok); |
|
480 QCOMPARE(cursor.position(), 39); // another.. |
|
481 ok = cursor.movePosition(QTextCursor::NextCell); |
|
482 QVERIFY(ok); |
|
483 QCOMPARE(cursor.position(), 52); // empty |
|
484 ok = cursor.movePosition(QTextCursor::NextCell); |
|
485 QVERIFY(ok); |
|
486 QCOMPARE(cursor.position(), 53); // last on row 1 |
|
487 ok = cursor.movePosition(QTextCursor::NextCell); |
|
488 QVERIFY(ok); |
|
489 QCOMPARE(cursor.position(), 69); // row 2 |
|
490 ok = cursor.movePosition(QTextCursor::NextCell); |
|
491 QVERIFY(ok); |
|
492 QCOMPARE(cursor.position(), 75); |
|
493 ok = cursor.movePosition(QTextCursor::NextCell); |
|
494 QVERIFY(ok); |
|
495 QCOMPARE(cursor.position(), 83); |
|
496 ok = cursor.movePosition(QTextCursor::NextCell); |
|
497 QVERIFY(ok); |
|
498 QCOMPARE(cursor.position(), 93); // row 3 |
|
499 ok = cursor.movePosition(QTextCursor::NextCell); |
|
500 QVERIFY(ok); |
|
501 QCOMPARE(cursor.position(), 99); |
|
502 ok = cursor.movePosition(QTextCursor::NextCell); |
|
503 QVERIFY(ok == false); |
|
504 QCOMPARE(cursor.position(), 99); // didn't move. |
|
505 QVERIFY(cursor.currentTable()); |
|
506 |
|
507 // same thing in reverse... |
|
508 ok = cursor.movePosition(QTextCursor::PreviousCell); |
|
509 QVERIFY(ok); |
|
510 QCOMPARE(cursor.position(), 93); |
|
511 ok = cursor.movePosition(QTextCursor::PreviousCell); |
|
512 QVERIFY(ok); |
|
513 QCOMPARE(cursor.position(), 83); |
|
514 ok = cursor.movePosition(QTextCursor::PreviousCell); |
|
515 QVERIFY(ok); |
|
516 QCOMPARE(cursor.position(), 75); |
|
517 ok = cursor.movePosition(QTextCursor::PreviousCell); |
|
518 QVERIFY(ok); |
|
519 QCOMPARE(cursor.position(), 69); |
|
520 ok = cursor.movePosition(QTextCursor::PreviousCell); |
|
521 QVERIFY(ok); |
|
522 QCOMPARE(cursor.position(), 53); |
|
523 ok = cursor.movePosition(QTextCursor::PreviousCell); |
|
524 QVERIFY(ok); |
|
525 QCOMPARE(cursor.position(), 52); |
|
526 ok = cursor.movePosition(QTextCursor::PreviousCell); |
|
527 QVERIFY(ok); |
|
528 QCOMPARE(cursor.position(), 39); |
|
529 ok = cursor.movePosition(QTextCursor::PreviousCell); |
|
530 QVERIFY(ok); |
|
531 QCOMPARE(cursor.position(), 25); |
|
532 ok = cursor.movePosition(QTextCursor::PreviousCell); |
|
533 QVERIFY(!ok); |
|
534 QCOMPARE(cursor.position(), 25); // can't leave the table |
|
535 |
|
536 ok = cursor.movePosition(QTextCursor::NextRow); |
|
537 QVERIFY(ok); |
|
538 QCOMPARE(cursor.position(), 69); |
|
539 ok = cursor.movePosition(QTextCursor::NextRow); |
|
540 QVERIFY(ok); |
|
541 QCOMPARE(cursor.position(), 93); |
|
542 ok = cursor.movePosition(QTextCursor::NextRow); |
|
543 QVERIFY(!ok); |
|
544 QCOMPARE(cursor.position(), 93); // didn't move |
|
545 |
|
546 ok = cursor.movePosition(QTextCursor::PreviousRow); |
|
547 QVERIFY(ok); |
|
548 QCOMPARE(cursor.position(), 83); // last col in row 2 |
|
549 ok = cursor.movePosition(QTextCursor::PreviousRow); |
|
550 QVERIFY(ok); |
|
551 QCOMPARE(cursor.position(), 53); // last col in row 1 |
|
552 ok = cursor.movePosition(QTextCursor::PreviousRow); |
|
553 QVERIFY(!ok); |
|
554 QCOMPARE(cursor.position(), 53); |
|
555 |
|
556 // test usecase of jumping over a cell |
|
557 doc->clear(); |
|
558 doc->setHtml("<html><table>tr><td rowspan=\"2\">a</td><td>b</td></tr><tr><td>c</td></tr></table></html>"); |
|
559 cursor.setPosition(1); // a |
|
560 ok = cursor.movePosition(QTextCursor::NextCell); |
|
561 QVERIFY(ok); |
|
562 QCOMPARE(cursor.position(), 3); // b |
|
563 ok = cursor.movePosition(QTextCursor::NextCell); |
|
564 QVERIFY(ok); |
|
565 QCOMPARE(cursor.position(), 5); // c |
|
566 ok = cursor.movePosition(QTextCursor::PreviousCell); |
|
567 QVERIFY(ok); |
|
568 QCOMPARE(cursor.position(), 3); // b |
|
569 ok = cursor.movePosition(QTextCursor::PreviousCell); |
|
570 QVERIFY(ok); |
|
571 QCOMPARE(cursor.position(), 1); // a |
|
572 } |
|
573 |
|
574 void tst_QTextCursor::insertBlock() |
|
575 { |
|
576 QTextBlockFormat fmt; |
|
577 fmt.setTopMargin(100); |
|
578 cursor.insertBlock(fmt); |
|
579 QVERIFY(cursor.position() == 1); |
|
580 QVERIFY(cursor.blockFormat() == fmt); |
|
581 } |
|
582 |
|
583 void tst_QTextCursor::insertWithBlockSeparator1() |
|
584 { |
|
585 QString text = "Hello" + QString(QChar::ParagraphSeparator) + "World"; |
|
586 |
|
587 cursor.insertText(text); |
|
588 |
|
589 cursor.movePosition(QTextCursor::PreviousBlock); |
|
590 QVERIFY(cursor.position() == 0); |
|
591 |
|
592 cursor.movePosition(QTextCursor::NextBlock); |
|
593 QVERIFY(cursor.position() == 6); |
|
594 } |
|
595 |
|
596 void tst_QTextCursor::insertWithBlockSeparator2() |
|
597 { |
|
598 cursor.insertText(QString(QChar::ParagraphSeparator)); |
|
599 QVERIFY(cursor.position() == 1); |
|
600 } |
|
601 |
|
602 void tst_QTextCursor::insertWithBlockSeparator3() |
|
603 { |
|
604 cursor.insertText(QString(QChar::ParagraphSeparator) + "Hi" + QString(QChar::ParagraphSeparator)); |
|
605 QVERIFY(cursor.position() == 4); |
|
606 } |
|
607 |
|
608 void tst_QTextCursor::insertWithBlockSeparator4() |
|
609 { |
|
610 cursor.insertText(QString(QChar::ParagraphSeparator) + QString(QChar::ParagraphSeparator)); |
|
611 QVERIFY(cursor.position() == 2); |
|
612 } |
|
613 |
|
614 void tst_QTextCursor::clearObjectType1() |
|
615 { |
|
616 cursor.insertImage("test.png"); |
|
617 QVERIFY(cursor.charFormat().isValid()); |
|
618 QVERIFY(cursor.charFormat().isImageFormat()); |
|
619 cursor.insertText("Hey"); |
|
620 QVERIFY(cursor.charFormat().isValid()); |
|
621 QVERIFY(!cursor.charFormat().isImageFormat()); |
|
622 } |
|
623 |
|
624 void tst_QTextCursor::clearObjectType2() |
|
625 { |
|
626 cursor.insertImage("test.png"); |
|
627 QVERIFY(cursor.charFormat().isValid()); |
|
628 QVERIFY(cursor.charFormat().isImageFormat()); |
|
629 cursor.insertBlock(); |
|
630 QVERIFY(cursor.charFormat().isValid()); |
|
631 QVERIFY(!cursor.charFormat().isImageFormat()); |
|
632 } |
|
633 |
|
634 void tst_QTextCursor::clearObjectType3() |
|
635 { |
|
636 // like clearObjectType2 but tests different insertBlock overload |
|
637 cursor.insertImage("test.png"); |
|
638 QVERIFY(cursor.charFormat().isValid()); |
|
639 QVERIFY(cursor.charFormat().isImageFormat()); |
|
640 QTextBlockFormat bfmt; |
|
641 bfmt.setAlignment(Qt::AlignRight); |
|
642 cursor.insertBlock(bfmt); |
|
643 QVERIFY(cursor.charFormat().isValid()); |
|
644 QVERIFY(!cursor.charFormat().isImageFormat()); |
|
645 } |
|
646 |
|
647 void tst_QTextCursor::comparisonOperators1() |
|
648 { |
|
649 cursor.insertText("Hello World"); |
|
650 |
|
651 cursor.movePosition(QTextCursor::PreviousWord); |
|
652 |
|
653 QTextCursor startCursor = cursor; |
|
654 startCursor.movePosition(QTextCursor::Start); |
|
655 |
|
656 QVERIFY(startCursor < cursor); |
|
657 |
|
658 QTextCursor midCursor = startCursor; |
|
659 midCursor.movePosition(QTextCursor::NextWord); |
|
660 |
|
661 QVERIFY(midCursor <= cursor); |
|
662 QVERIFY(midCursor == cursor); |
|
663 QVERIFY(midCursor >= cursor); |
|
664 |
|
665 QVERIFY(midCursor > startCursor); |
|
666 |
|
667 QVERIFY(midCursor != startCursor); |
|
668 QVERIFY(!(midCursor == startCursor)); |
|
669 |
|
670 QTextCursor nullCursor; |
|
671 |
|
672 QVERIFY(!(startCursor < nullCursor)); |
|
673 QVERIFY(!(nullCursor < nullCursor)); |
|
674 QVERIFY(nullCursor < startCursor); |
|
675 |
|
676 QVERIFY(nullCursor <= startCursor); |
|
677 QVERIFY(!(startCursor <= nullCursor)); |
|
678 |
|
679 QVERIFY(!(nullCursor >= startCursor)); |
|
680 QVERIFY(startCursor >= nullCursor); |
|
681 |
|
682 QVERIFY(!(nullCursor > startCursor)); |
|
683 QVERIFY(!(nullCursor > nullCursor)); |
|
684 QVERIFY(startCursor > nullCursor); |
|
685 } |
|
686 |
|
687 void tst_QTextCursor::comparisonOperators2() |
|
688 { |
|
689 QTextDocument doc1; |
|
690 QTextDocument doc2; |
|
691 |
|
692 QTextCursor cursor1(&doc1); |
|
693 QTextCursor cursor2(&doc2); |
|
694 |
|
695 QVERIFY(cursor1 != cursor2); |
|
696 QVERIFY(cursor1 == QTextCursor(&doc1)); |
|
697 } |
|
698 |
|
699 void tst_QTextCursor::selection1() |
|
700 { |
|
701 cursor.insertText("Hello World"); |
|
702 |
|
703 cursor.setPosition(0); |
|
704 cursor.clearSelection(); |
|
705 cursor.setPosition(4, QTextCursor::KeepAnchor); |
|
706 |
|
707 QCOMPARE(cursor.selectionStart(), 0); |
|
708 QCOMPARE(cursor.selectionEnd(), 4); |
|
709 } |
|
710 |
|
711 void tst_QTextCursor::dontCopyTableAttributes() |
|
712 { |
|
713 /* when pressing 'enter' inside a cell it shouldn't |
|
714 * enlarge the table by adding another cell but just |
|
715 * extend the cell */ |
|
716 QTextTable *table = cursor.insertTable(2, 2); |
|
717 QVERIFY(cursor == table->cellAt(0, 0).firstCursorPosition()); |
|
718 cursor.insertBlock(); |
|
719 QCOMPARE(table->columns(), 2); |
|
720 } |
|
721 |
|
722 void tst_QTextCursor::checkFrame1() |
|
723 { |
|
724 QVERIFY(cursor.position() == 0); |
|
725 QPointer<QTextFrame> frame = cursor.insertFrame(QTextFrameFormat()); |
|
726 QVERIFY(frame != 0); |
|
727 |
|
728 QTextFrame *root = frame->parentFrame(); |
|
729 QVERIFY(root != 0); |
|
730 |
|
731 QVERIFY(frame->firstPosition() == 1); |
|
732 QVERIFY(frame->lastPosition() == 1); |
|
733 QVERIFY(frame->parentFrame() != 0); |
|
734 QVERIFY(root->childFrames().size() == 1); |
|
735 |
|
736 QVERIFY(cursor.position() == 1); |
|
737 QVERIFY(cursor.selectionStart() == 1); |
|
738 QVERIFY(cursor.selectionEnd() == 1); |
|
739 |
|
740 doc->undo(); |
|
741 |
|
742 QVERIFY(!frame); |
|
743 QVERIFY(root->childFrames().size() == 0); |
|
744 |
|
745 QVERIFY(cursor.position() == 0); |
|
746 QVERIFY(cursor.selectionStart() == 0); |
|
747 QVERIFY(cursor.selectionEnd() == 0); |
|
748 |
|
749 doc->redo(); |
|
750 |
|
751 frame = doc->frameAt(1); |
|
752 |
|
753 QVERIFY(frame); |
|
754 QVERIFY(frame->firstPosition() == 1); |
|
755 QVERIFY(frame->lastPosition() == 1); |
|
756 QVERIFY(frame->parentFrame() != 0); |
|
757 QVERIFY(root->childFrames().size() == 1); |
|
758 |
|
759 QVERIFY(cursor.position() == 1); |
|
760 QVERIFY(cursor.selectionStart() == 1); |
|
761 QVERIFY(cursor.selectionEnd() == 1); |
|
762 |
|
763 // cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); |
|
764 // QVERIFY(cursor.position() == 2); |
|
765 // QVERIFY(cursor.selectionStart() == 0); |
|
766 // QVERIFY(cursor.selectionEnd() == 2); |
|
767 } |
|
768 |
|
769 void tst_QTextCursor::checkFrame2() |
|
770 { |
|
771 QVERIFY(cursor.position() == 0); |
|
772 cursor.insertText("A"); |
|
773 QVERIFY(cursor.position() == 1); |
|
774 cursor.movePosition(QTextCursor::Start, QTextCursor::KeepAnchor); |
|
775 |
|
776 QPointer<QTextFrame> frame = cursor.insertFrame(QTextFrameFormat()); |
|
777 QTextFrame *root = frame->parentFrame(); |
|
778 |
|
779 QVERIFY(frame->firstPosition() == 1); |
|
780 QVERIFY(frame->lastPosition() == 2); |
|
781 QVERIFY(frame->parentFrame() != 0); |
|
782 QVERIFY(root->childFrames().size() == 1); |
|
783 |
|
784 QVERIFY(cursor.position() == 1); |
|
785 QVERIFY(cursor.selectionStart() == 1); |
|
786 QVERIFY(cursor.selectionEnd() == 2); |
|
787 |
|
788 doc->undo(); |
|
789 |
|
790 QVERIFY(!frame); |
|
791 QVERIFY(root->childFrames().size() == 0); |
|
792 |
|
793 QVERIFY(cursor.position() == 0); |
|
794 QVERIFY(cursor.selectionStart() == 0); |
|
795 QVERIFY(cursor.selectionEnd() == 1); |
|
796 |
|
797 doc->redo(); |
|
798 |
|
799 frame = doc->frameAt(1); |
|
800 |
|
801 QVERIFY(frame); |
|
802 QVERIFY(frame->firstPosition() == 1); |
|
803 QVERIFY(frame->lastPosition() == 2); |
|
804 QVERIFY(frame->parentFrame() != 0); |
|
805 QVERIFY(root->childFrames().size() == 1); |
|
806 |
|
807 QVERIFY(cursor.position() == 1); |
|
808 QVERIFY(cursor.selectionStart() == 1); |
|
809 QVERIFY(cursor.selectionEnd() == 2); |
|
810 |
|
811 cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor); |
|
812 QVERIFY(cursor.position() == 0); |
|
813 QVERIFY(cursor.selectionStart() == 0); |
|
814 QVERIFY(cursor.selectionEnd() == 3); |
|
815 } |
|
816 |
|
817 void tst_QTextCursor::insertBlockToUseCharFormat() |
|
818 { |
|
819 QTextCharFormat fmt; |
|
820 fmt.setForeground(Qt::blue); |
|
821 cursor.insertText("Hello", fmt); |
|
822 QCOMPARE(cursor.charFormat().foreground().color(), QColor(Qt::blue)); |
|
823 |
|
824 cursor.insertBlock(); |
|
825 QCOMPARE(cursor.charFormat().foreground().color(), QColor(Qt::blue)); |
|
826 |
|
827 fmt.setForeground(Qt::red); |
|
828 cursor.insertText("Hello\nWorld", fmt); |
|
829 cursor.insertText("Blah"); |
|
830 QCOMPARE(cursor.charFormat().foreground().color(), QColor(Qt::red)); |
|
831 |
|
832 // ### we might want a testcase for createTable, too, as it calls insertBlock, too, |
|
833 // and we might want to have the char format copied (the one that gets inserted |
|
834 // as table separators, that are undeletable) |
|
835 } |
|
836 |
|
837 void tst_QTextCursor::tableMovement() |
|
838 { |
|
839 QVERIFY(cursor.position() == 0); |
|
840 cursor.insertText("AA"); |
|
841 QVERIFY(cursor.position() == 2); |
|
842 cursor.movePosition(QTextCursor::Left); |
|
843 |
|
844 cursor.insertTable(3, 3); |
|
845 QCOMPARE(cursor.position(), 2); |
|
846 |
|
847 cursor.movePosition(QTextCursor::Down); |
|
848 QCOMPARE(cursor.position(), 5); |
|
849 |
|
850 cursor.movePosition(QTextCursor::Right); |
|
851 QCOMPARE(cursor.position(), 6); |
|
852 |
|
853 cursor.movePosition(QTextCursor::Up); |
|
854 QCOMPARE(cursor.position(), 3); |
|
855 |
|
856 cursor.movePosition(QTextCursor::Right); |
|
857 QCOMPARE(cursor.position(), 4); |
|
858 |
|
859 cursor.movePosition(QTextCursor::Right); |
|
860 QCOMPARE(cursor.position(), 5); |
|
861 |
|
862 cursor.movePosition(QTextCursor::Up); |
|
863 QCOMPARE(cursor.position(), 2); |
|
864 |
|
865 cursor.movePosition(QTextCursor::Up); |
|
866 QCOMPARE(cursor.position(), 0); |
|
867 |
|
868 } |
|
869 |
|
870 void tst_QTextCursor::selectionsInTable() |
|
871 { |
|
872 QTextTable *table = cursor.insertTable(2, 2); |
|
873 table->cellAt(0, 0).firstCursorPosition().insertText("First"); |
|
874 table->cellAt(0, 1).firstCursorPosition().insertText("Second"); |
|
875 table->cellAt(1, 0).firstCursorPosition().insertText("Third"); |
|
876 table->cellAt(1, 1).firstCursorPosition().insertText("Fourth"); |
|
877 |
|
878 cursor = table->cellAt(0, 0).lastCursorPosition(); |
|
879 QVERIFY(cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::KeepAnchor)); |
|
880 QVERIFY(cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::KeepAnchor) == false); |
|
881 |
|
882 cursor = table->cellAt(1, 0).lastCursorPosition(); |
|
883 QVERIFY(cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::KeepAnchor)); |
|
884 QVERIFY(cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::KeepAnchor) == false); |
|
885 |
|
886 cursor = table->cellAt(0, 1).firstCursorPosition(); |
|
887 QVERIFY(cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor)); |
|
888 QVERIFY(cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor) == false); |
|
889 |
|
890 cursor = table->cellAt(1, 1).firstCursorPosition(); |
|
891 QVERIFY(cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor)); |
|
892 QVERIFY(cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor) == false); |
|
893 } |
|
894 |
|
895 void tst_QTextCursor::selectedText() |
|
896 { |
|
897 cursor.insertText("Hello World"); |
|
898 cursor.movePosition(QTextCursor::Start); |
|
899 cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor); |
|
900 |
|
901 QCOMPARE(cursor.selectedText(), QString("Hello World")); |
|
902 } |
|
903 |
|
904 void tst_QTextCursor::insertBlockShouldRemoveSelection() |
|
905 { |
|
906 cursor.insertText("Hello World"); |
|
907 cursor.movePosition(QTextCursor::Start); |
|
908 cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); |
|
909 |
|
910 QVERIFY(cursor.hasSelection()); |
|
911 QCOMPARE(cursor.selectedText(), QString("Hello")); |
|
912 |
|
913 cursor.insertBlock(); |
|
914 |
|
915 QVERIFY(!cursor.hasSelection()); |
|
916 QVERIFY(doc->toPlainText().indexOf("Hello") == -1); |
|
917 } |
|
918 |
|
919 void tst_QTextCursor::insertBlockShouldRemoveSelection2() |
|
920 { |
|
921 cursor.insertText("Hello World"); |
|
922 cursor.movePosition(QTextCursor::Start); |
|
923 cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); |
|
924 |
|
925 QVERIFY(cursor.hasSelection()); |
|
926 QCOMPARE(cursor.selectedText(), QString("Hello")); |
|
927 |
|
928 QTextBlockFormat fmt = cursor.blockFormat(); |
|
929 cursor.insertBlock(fmt); |
|
930 |
|
931 QVERIFY(!cursor.hasSelection()); |
|
932 QVERIFY(doc->toPlainText().indexOf("Hello") == -1); |
|
933 } |
|
934 |
|
935 void tst_QTextCursor::mergeCellShouldUpdateSelection() |
|
936 { |
|
937 QTextTable *table = cursor.insertTable(4, 4); |
|
938 cursor.setPosition(table->cellAt(0, 0).firstPosition()); |
|
939 cursor.setPosition(table->cellAt(3, 0).firstPosition(), QTextCursor::KeepAnchor); // aka bottom left |
|
940 int firstRow, numRows, firstColumn, numColumns; |
|
941 cursor.selectedTableCells(&firstRow, &numRows, &firstColumn, &numColumns); |
|
942 QCOMPARE(firstRow, 0); |
|
943 QCOMPARE(numRows, 4); |
|
944 QCOMPARE(firstColumn, 0); |
|
945 QCOMPARE(numColumns, 1); |
|
946 |
|
947 table->removeColumns(firstColumn, numColumns); |
|
948 |
|
949 QCOMPARE(cursor.anchor(), table->cellAt(0, 0).firstPosition()); |
|
950 QCOMPARE(cursor.position(), table->cellAt(0, 0).firstPosition()); |
|
951 QCOMPARE(cursor.position(), cursor.anchor()); // empty. I don't really care where it ends up. |
|
952 |
|
953 // prepare for another test with multiple cursors. |
|
954 // note we have a 4 rows, 3 cols table now. |
|
955 cursor.setPosition(table->cellAt(0, 0).firstPosition()); |
|
956 cursor.setPosition(table->cellAt(0, 2).firstPosition(), QTextCursor::KeepAnchor); |
|
957 |
|
958 // now create a selection of a whole row. |
|
959 QTextCursor c2 = table->cellAt(2, 0).firstCursorPosition(); |
|
960 c2.setPosition(table->cellAt(2, 2).firstPosition(), QTextCursor::KeepAnchor); |
|
961 |
|
962 // just for good measure, another one for a block of cells. |
|
963 QTextCursor c3 = table->cellAt(2, 1).firstCursorPosition(); |
|
964 c3.setPosition(table->cellAt(3, 2).firstPosition(), QTextCursor::KeepAnchor); |
|
965 |
|
966 table->removeRows(2, 1); |
|
967 |
|
968 QCOMPARE(cursor.anchor(), table->cellAt(0, 0).firstPosition()); |
|
969 QCOMPARE(cursor.position(), table->cellAt(0, 2).firstPosition()); |
|
970 |
|
971 QCOMPARE(c2.position(), c2.anchor()); // empty. I don't really care where it ends up. |
|
972 |
|
973 QCOMPARE(c3.anchor(), table->cellAt(2, 1).firstPosition()); |
|
974 QCOMPARE(c3.position(), table->cellAt(2, 2).firstPosition()); |
|
975 |
|
976 |
|
977 // prepare for another test where we remove a column |
|
978 // note we have a 3 rows, 3 cols table now. |
|
979 cursor.setPosition(table->cellAt(0, 0).firstPosition()); |
|
980 cursor.setPosition(table->cellAt(2, 1).firstPosition(), QTextCursor::KeepAnchor); |
|
981 |
|
982 c2.setPosition(table->cellAt(0, 1).firstPosition()); |
|
983 c2.setPosition(table->cellAt(2, 2).firstPosition(), QTextCursor::KeepAnchor); |
|
984 |
|
985 table->removeColumns(1, 1); |
|
986 |
|
987 QCOMPARE(cursor.anchor(), table->cellAt(0, 0).firstPosition()); |
|
988 QCOMPARE(cursor.position(), table->cellAt(2, 0).firstPosition()); |
|
989 |
|
990 QCOMPARE(c2.anchor(), table->cellAt(0, 1).firstPosition()); |
|
991 QCOMPARE(c2.position(), table->cellAt(2, 1).firstPosition()); |
|
992 |
|
993 // test for illegal cursor positions. |
|
994 // note we have a 3 rows, 2 cols table now. |
|
995 cursor.setPosition(table->cellAt(2, 0).firstPosition()); |
|
996 cursor.setPosition(table->cellAt(2, 1).firstPosition(), QTextCursor::KeepAnchor); |
|
997 |
|
998 c2.setPosition(table->cellAt(0, 0).firstPosition()); |
|
999 c2.setPosition(table->cellAt(2, 1).firstPosition(), QTextCursor::KeepAnchor); |
|
1000 |
|
1001 c3.setPosition(table->cellAt(2, 1).firstPosition()); |
|
1002 |
|
1003 table->removeRows(2, 1); |
|
1004 |
|
1005 QCOMPARE(cursor.anchor(), table->cellAt(1, 1).lastPosition()+1); |
|
1006 QCOMPARE(cursor.position(), cursor.anchor()); |
|
1007 |
|
1008 QCOMPARE(c2.anchor(), table->cellAt(0, 0).firstPosition()); |
|
1009 QCOMPARE(c2.position(), table->cellAt(1, 1).firstPosition()); |
|
1010 |
|
1011 QCOMPARE(c3.anchor(), table->cellAt(1, 1).firstPosition()); |
|
1012 QCOMPARE(c3.position(), table->cellAt(1, 1).firstPosition()); |
|
1013 } |
|
1014 |
|
1015 void tst_QTextCursor::joinPreviousEditBlock() |
|
1016 { |
|
1017 cursor.beginEditBlock(); |
|
1018 cursor.insertText("Hello"); |
|
1019 cursor.insertText("World"); |
|
1020 cursor.endEditBlock(); |
|
1021 QVERIFY(doc->toPlainText().startsWith("HelloWorld")); |
|
1022 |
|
1023 cursor.joinPreviousEditBlock(); |
|
1024 cursor.insertText("Hey"); |
|
1025 cursor.endEditBlock(); |
|
1026 QVERIFY(doc->toPlainText().startsWith("HelloWorldHey")); |
|
1027 |
|
1028 doc->undo(); |
|
1029 QVERIFY(!doc->toPlainText().contains("HelloWorldHey")); |
|
1030 } |
|
1031 |
|
1032 void tst_QTextCursor::setBlockFormatInTable() |
|
1033 { |
|
1034 // someone reported this on qt4-preview-feedback |
|
1035 QTextBlockFormat fmt; |
|
1036 fmt.setBackground(Qt::blue); |
|
1037 cursor.setBlockFormat(fmt); |
|
1038 |
|
1039 QTextTable *table = cursor.insertTable(2, 2); |
|
1040 cursor = table->cellAt(0, 0).firstCursorPosition(); |
|
1041 fmt.setBackground(Qt::red); |
|
1042 cursor.setBlockFormat(fmt); |
|
1043 |
|
1044 cursor.movePosition(QTextCursor::Start); |
|
1045 QVERIFY(cursor.blockFormat().background().color() == Qt::blue); |
|
1046 } |
|
1047 |
|
1048 void tst_QTextCursor::blockCharFormat2() |
|
1049 { |
|
1050 QTextCharFormat fmt; |
|
1051 fmt.setForeground(Qt::green); |
|
1052 cursor.mergeBlockCharFormat(fmt); |
|
1053 |
|
1054 fmt.setForeground(Qt::red); |
|
1055 |
|
1056 cursor.insertText("Test", fmt); |
|
1057 cursor.movePosition(QTextCursor::Start); |
|
1058 cursor.insertText("Red"); |
|
1059 cursor.movePosition(QTextCursor::PreviousCharacter); |
|
1060 QVERIFY(cursor.charFormat().foreground().color() == Qt::red); |
|
1061 } |
|
1062 |
|
1063 void tst_QTextCursor::blockCharFormat3() |
|
1064 { |
|
1065 QVERIFY(cursor.atBlockStart()); |
|
1066 QVERIFY(cursor.atBlockEnd()); |
|
1067 QVERIFY(cursor.atStart()); |
|
1068 |
|
1069 QTextCharFormat fmt; |
|
1070 fmt.setForeground(Qt::green); |
|
1071 cursor.setBlockCharFormat(fmt); |
|
1072 cursor.insertText("Test"); |
|
1073 cursor.movePosition(QTextCursor::Start); |
|
1074 cursor.movePosition(QTextCursor::NextCharacter); |
|
1075 QVERIFY(cursor.charFormat().foreground().color() == Qt::green); |
|
1076 |
|
1077 cursor.movePosition(QTextCursor::Start); |
|
1078 QVERIFY(cursor.charFormat().foreground().color() == Qt::green); |
|
1079 |
|
1080 fmt.setForeground(Qt::red); |
|
1081 cursor.setBlockCharFormat(fmt); |
|
1082 QVERIFY(cursor.blockCharFormat().foreground().color() == Qt::red); |
|
1083 |
|
1084 cursor.movePosition(QTextCursor::End); |
|
1085 cursor.movePosition(QTextCursor::Start); |
|
1086 QVERIFY(cursor.charFormat().foreground().color() == Qt::green); |
|
1087 |
|
1088 cursor.insertText("Test"); |
|
1089 QVERIFY(cursor.charFormat().foreground().color() == Qt::green); |
|
1090 |
|
1091 cursor.select(QTextCursor::Document); |
|
1092 cursor.removeSelectedText(); |
|
1093 QVERIFY(cursor.atBlockStart()); |
|
1094 QVERIFY(cursor.atBlockEnd()); |
|
1095 QVERIFY(cursor.atStart()); |
|
1096 |
|
1097 cursor.insertText("Test"); |
|
1098 QVERIFY(cursor.charFormat().foreground().color() == Qt::red); |
|
1099 } |
|
1100 |
|
1101 void tst_QTextCursor::blockCharFormat() |
|
1102 { |
|
1103 QTextCharFormat fmt; |
|
1104 fmt.setForeground(Qt::blue); |
|
1105 cursor.insertBlock(QTextBlockFormat(), fmt); |
|
1106 cursor.insertText("Hm"); |
|
1107 |
|
1108 QVERIFY(cursor.blockCharFormat().foreground().color() == Qt::blue); |
|
1109 |
|
1110 fmt.setForeground(Qt::red); |
|
1111 |
|
1112 cursor.setBlockCharFormat(fmt); |
|
1113 QVERIFY(cursor.blockCharFormat().foreground().color() == Qt::red); |
|
1114 } |
|
1115 |
|
1116 void tst_QTextCursor::blockCharFormatOnSelection() |
|
1117 { |
|
1118 QTextCharFormat fmt; |
|
1119 fmt.setForeground(Qt::blue); |
|
1120 cursor.insertBlock(QTextBlockFormat(), fmt); |
|
1121 |
|
1122 fmt.setForeground(Qt::green); |
|
1123 cursor.insertText("Hm", fmt); |
|
1124 |
|
1125 fmt.setForeground(Qt::red); |
|
1126 cursor.insertBlock(QTextBlockFormat(), fmt); |
|
1127 cursor.insertText("Ah"); |
|
1128 |
|
1129 fmt.setForeground(Qt::white); |
|
1130 cursor.insertBlock(QTextBlockFormat(), fmt); |
|
1131 cursor.insertText("bleh"); |
|
1132 |
|
1133 cursor.movePosition(QTextCursor::Start); |
|
1134 cursor.movePosition(QTextCursor::NextBlock); |
|
1135 QVERIFY(cursor.blockCharFormat().foreground().color() == Qt::blue); |
|
1136 cursor.movePosition(QTextCursor::NextBlock); |
|
1137 QVERIFY(cursor.blockCharFormat().foreground().color() == Qt::red); |
|
1138 cursor.movePosition(QTextCursor::NextBlock); |
|
1139 QVERIFY(cursor.blockCharFormat().foreground().color() == Qt::white); |
|
1140 |
|
1141 cursor.movePosition(QTextCursor::Start); |
|
1142 cursor.movePosition(QTextCursor::NextBlock); |
|
1143 cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor); |
|
1144 |
|
1145 fmt.setForeground(Qt::cyan); |
|
1146 cursor.setBlockCharFormat(fmt); |
|
1147 |
|
1148 cursor.movePosition(QTextCursor::Start); |
|
1149 cursor.movePosition(QTextCursor::NextBlock); |
|
1150 QVERIFY(cursor.blockCharFormat().foreground().color() == Qt::cyan); |
|
1151 |
|
1152 cursor.movePosition(QTextCursor::Right); |
|
1153 cursor.movePosition(QTextCursor::Right); |
|
1154 QVERIFY(cursor.charFormat().foreground().color() == Qt::green); |
|
1155 |
|
1156 cursor.movePosition(QTextCursor::NextBlock); |
|
1157 QVERIFY(cursor.blockCharFormat().foreground().color() == Qt::cyan); |
|
1158 |
|
1159 cursor.movePosition(QTextCursor::NextBlock); |
|
1160 QVERIFY(cursor.blockCharFormat().foreground().color() == Qt::white); |
|
1161 } |
|
1162 |
|
1163 void tst_QTextCursor::anchorInitialized1() |
|
1164 { |
|
1165 cursor.insertBlock(); |
|
1166 cursor = QTextCursor(cursor.block()); |
|
1167 QCOMPARE(cursor.position(), 1); |
|
1168 QCOMPARE(cursor.anchor(), 1); |
|
1169 QCOMPARE(cursor.selectionStart(), 1); |
|
1170 QCOMPARE(cursor.selectionEnd(), 1); |
|
1171 } |
|
1172 |
|
1173 void tst_QTextCursor::anchorInitialized2() |
|
1174 { |
|
1175 cursor.insertBlock(); |
|
1176 cursor = QTextCursor(cursor.block().docHandle(), 1); |
|
1177 QCOMPARE(cursor.position(), 1); |
|
1178 QCOMPARE(cursor.anchor(), 1); |
|
1179 QCOMPARE(cursor.selectionStart(), 1); |
|
1180 QCOMPARE(cursor.selectionEnd(), 1); |
|
1181 } |
|
1182 |
|
1183 void tst_QTextCursor::anchorInitialized3() |
|
1184 { |
|
1185 QTextFrame *frame = cursor.insertFrame(QTextFrameFormat()); |
|
1186 cursor = QTextCursor(frame); |
|
1187 QCOMPARE(cursor.position(), 1); |
|
1188 QCOMPARE(cursor.anchor(), 1); |
|
1189 QCOMPARE(cursor.selectionStart(), 1); |
|
1190 QCOMPARE(cursor.selectionEnd(), 1); |
|
1191 } |
|
1192 |
|
1193 void tst_QTextCursor::selectWord() |
|
1194 { |
|
1195 cursor.insertText("first second third"); |
|
1196 cursor.insertBlock(); |
|
1197 cursor.insertText("words in second paragraph"); |
|
1198 |
|
1199 cursor.setPosition(9); |
|
1200 cursor.select(QTextCursor::WordUnderCursor); |
|
1201 QVERIFY(cursor.hasSelection()); |
|
1202 QCOMPARE(cursor.selectionStart(), 6); |
|
1203 QCOMPARE(cursor.selectionEnd(), 12); |
|
1204 |
|
1205 cursor.setPosition(5); |
|
1206 cursor.select(QTextCursor::WordUnderCursor); |
|
1207 QVERIFY(cursor.hasSelection()); |
|
1208 QCOMPARE(cursor.selectionStart(), 0); |
|
1209 QCOMPARE(cursor.selectionEnd(), 5); |
|
1210 |
|
1211 cursor.setPosition(6); |
|
1212 cursor.select(QTextCursor::WordUnderCursor); |
|
1213 QVERIFY(cursor.hasSelection()); |
|
1214 QCOMPARE(cursor.selectionStart(), 6); |
|
1215 QCOMPARE(cursor.selectionEnd(), 12); |
|
1216 |
|
1217 cursor.setPosition(14); |
|
1218 cursor.select(QTextCursor::WordUnderCursor); |
|
1219 QVERIFY(cursor.hasSelection()); |
|
1220 QCOMPARE(cursor.selectionStart(), 6); |
|
1221 QCOMPARE(cursor.selectionEnd(), 12); |
|
1222 |
|
1223 cursor.movePosition(QTextCursor::Start); |
|
1224 cursor.select(QTextCursor::WordUnderCursor); |
|
1225 QVERIFY(cursor.hasSelection()); |
|
1226 QCOMPARE(cursor.selectionStart(), 0); |
|
1227 QCOMPARE(cursor.selectionEnd(), 5); |
|
1228 |
|
1229 cursor.movePosition(QTextCursor::EndOfBlock); |
|
1230 cursor.select(QTextCursor::WordUnderCursor); |
|
1231 QVERIFY(cursor.hasSelection()); |
|
1232 QCOMPARE(cursor.selectionStart(), 17); |
|
1233 QCOMPARE(cursor.selectionEnd(), 22); |
|
1234 } |
|
1235 |
|
1236 void tst_QTextCursor::selectWordWithSeparators_data() |
|
1237 { |
|
1238 QTest::addColumn<QString>("text"); |
|
1239 QTest::addColumn<int>("initialPosition"); |
|
1240 QTest::addColumn<QString>("expectedSelectedText"); |
|
1241 |
|
1242 QTest::newRow("dereference") << QString::fromLatin1("foo->bar()") << 1 << QString::fromLatin1("foo"); |
|
1243 QTest::newRow("funcsignature") << QString::fromLatin1("bar(int x);") << 1 << QString::fromLatin1("bar"); |
|
1244 QTest::newRow("def") << QString::fromLatin1("foo *f;") << 1 << QString::fromLatin1("foo"); |
|
1245 } |
|
1246 |
|
1247 void tst_QTextCursor::selectWordWithSeparators() |
|
1248 { |
|
1249 QFETCH(QString, text); |
|
1250 QFETCH(int, initialPosition); |
|
1251 QFETCH(QString, expectedSelectedText); |
|
1252 |
|
1253 cursor.insertText(text); |
|
1254 cursor.setPosition(initialPosition); |
|
1255 cursor.select(QTextCursor::WordUnderCursor); |
|
1256 |
|
1257 QCOMPARE(cursor.selectedText(), expectedSelectedText); |
|
1258 } |
|
1259 |
|
1260 void tst_QTextCursor::startOfWord() |
|
1261 { |
|
1262 cursor.insertText("first second"); |
|
1263 cursor.setPosition(7); |
|
1264 cursor.movePosition(QTextCursor::StartOfWord); |
|
1265 QCOMPARE(cursor.position(), 0); |
|
1266 } |
|
1267 |
|
1268 void tst_QTextCursor::selectBlock() |
|
1269 { |
|
1270 cursor.insertText("foobar"); |
|
1271 QTextBlockFormat blockFmt; |
|
1272 blockFmt.setAlignment(Qt::AlignHCenter); |
|
1273 cursor.insertBlock(blockFmt); |
|
1274 cursor.insertText("blah"); |
|
1275 cursor.insertBlock(QTextBlockFormat()); |
|
1276 |
|
1277 cursor.movePosition(QTextCursor::PreviousBlock); |
|
1278 QCOMPARE(cursor.block().text(), QString("blah")); |
|
1279 |
|
1280 cursor.select(QTextCursor::BlockUnderCursor); |
|
1281 QVERIFY(cursor.hasSelection()); |
|
1282 |
|
1283 QTextDocumentFragment fragment(cursor); |
|
1284 doc->clear(); |
|
1285 cursor.insertFragment(fragment); |
|
1286 QCOMPARE(blockCount(), 2); |
|
1287 |
|
1288 cursor.movePosition(QTextCursor::Start); |
|
1289 cursor.movePosition(QTextCursor::NextBlock); |
|
1290 QVERIFY(cursor.blockFormat().alignment() == Qt::AlignHCenter); |
|
1291 QCOMPARE(cursor.block().text(), QString("blah")); |
|
1292 } |
|
1293 |
|
1294 void tst_QTextCursor::selectVisually() |
|
1295 { |
|
1296 cursor.insertText("Foo\nlong line which is probably going to be cut in two when shown in a widget\nparagraph 3\n"); |
|
1297 |
|
1298 cursor.setPosition(6); // somewhere in the long paragraph. |
|
1299 cursor.select(QTextCursor::LineUnderCursor); |
|
1300 // since we are not yet layed-out, we expect the whole paragraph to be selected. |
|
1301 QCOMPARE(cursor.position(), 77); |
|
1302 QCOMPARE(cursor.anchor(), 4); |
|
1303 } |
|
1304 |
|
1305 void tst_QTextCursor::insertText() |
|
1306 { |
|
1307 QString txt = "Foo\nBar\r\nMeep"; |
|
1308 txt += QChar::LineSeparator; |
|
1309 txt += "Baz"; |
|
1310 txt += QChar::ParagraphSeparator; |
|
1311 txt += "yoyodyne"; |
|
1312 cursor.insertText(txt); |
|
1313 QCOMPARE(blockCount(), 4); |
|
1314 cursor.movePosition(QTextCursor::Start); |
|
1315 QCOMPARE(cursor.block().text(), QString("Foo")); |
|
1316 cursor.movePosition(QTextCursor::NextBlock); |
|
1317 QCOMPARE(cursor.block().text(), QString("Bar")); |
|
1318 cursor.movePosition(QTextCursor::NextBlock); |
|
1319 QCOMPARE(cursor.block().text(), QString(QString("Meep") + QChar(QChar::LineSeparator) + QString("Baz"))); |
|
1320 cursor.movePosition(QTextCursor::NextBlock); |
|
1321 QCOMPARE(cursor.block().text(), QString("yoyodyne")); |
|
1322 } |
|
1323 |
|
1324 void tst_QTextCursor::insertFragmentShouldUseCurrentCharFormat() |
|
1325 { |
|
1326 QTextDocumentFragment fragment = QTextDocumentFragment::fromPlainText("Hello World"); |
|
1327 QTextCharFormat fmt; |
|
1328 fmt.setFontUnderline(true); |
|
1329 |
|
1330 cursor.clearSelection(); |
|
1331 cursor.setCharFormat(fmt); |
|
1332 cursor.insertFragment(fragment); |
|
1333 cursor.movePosition(QTextCursor::Start); |
|
1334 cursor.movePosition(QTextCursor::NextCharacter); |
|
1335 QVERIFY(cursor.charFormat() == fmt); |
|
1336 } |
|
1337 |
|
1338 int tst_QTextCursor::blockCount() |
|
1339 { |
|
1340 int cnt = 0; |
|
1341 for (QTextBlock blk = doc->begin(); blk.isValid(); blk = blk.next()) |
|
1342 ++cnt; |
|
1343 return cnt; |
|
1344 } |
|
1345 |
|
1346 void tst_QTextCursor::endOfLine() |
|
1347 { |
|
1348 doc->setPageSize(QSizeF(100000, INT_MAX)); |
|
1349 |
|
1350 QString text("First Line \nSecond Line "); |
|
1351 text.replace(QLatin1Char('\n'), QChar(QChar::LineSeparator)); |
|
1352 cursor.insertText(text); |
|
1353 |
|
1354 // ensure layouted |
|
1355 doc->documentLayout()->documentSize(); |
|
1356 |
|
1357 cursor.movePosition(QTextCursor::Start); |
|
1358 |
|
1359 QCOMPARE(cursor.block().layout()->lineCount(), 2); |
|
1360 |
|
1361 cursor.movePosition(QTextCursor::EndOfLine); |
|
1362 QCOMPARE(cursor.position(), 14); |
|
1363 cursor.movePosition(QTextCursor::NextCharacter); |
|
1364 QCOMPARE(cursor.position(), 15); |
|
1365 cursor.movePosition(QTextCursor::EndOfLine); |
|
1366 QCOMPARE(cursor.position(), 28); |
|
1367 } |
|
1368 |
|
1369 class CursorListener : public QObject |
|
1370 { |
|
1371 Q_OBJECT |
|
1372 public: |
|
1373 CursorListener(QTextCursor *_cursor) : lastRecordedPosition(-1), lastRecordedAnchor(-1), recordingCount(0), cursor(_cursor) {} |
|
1374 |
|
1375 int lastRecordedPosition; |
|
1376 int lastRecordedAnchor; |
|
1377 int recordingCount; |
|
1378 |
|
1379 public slots: |
|
1380 void recordCursorPosition() |
|
1381 { |
|
1382 lastRecordedPosition = cursor->position(); |
|
1383 lastRecordedAnchor = cursor->anchor(); |
|
1384 ++recordingCount; |
|
1385 } |
|
1386 |
|
1387 private: |
|
1388 QTextCursor *cursor; |
|
1389 }; |
|
1390 |
|
1391 void tst_QTextCursor::editBlocksDuringRemove() |
|
1392 { |
|
1393 CursorListener listener(&cursor); |
|
1394 |
|
1395 cursor.insertText("Hello World"); |
|
1396 cursor.movePosition(QTextCursor::Start, QTextCursor::KeepAnchor); |
|
1397 QCOMPARE(cursor.selectedText(), QString("Hello World")); |
|
1398 |
|
1399 connect(doc, SIGNAL(contentsChanged()), &listener, SLOT(recordCursorPosition())); |
|
1400 listener.recordingCount = 0; |
|
1401 cursor.deleteChar(); |
|
1402 |
|
1403 QCOMPARE(listener.recordingCount, 1); |
|
1404 QCOMPARE(listener.lastRecordedPosition, 0); |
|
1405 QCOMPARE(listener.lastRecordedAnchor, 0); |
|
1406 |
|
1407 QVERIFY(doc->toPlainText().isEmpty()); |
|
1408 } |
|
1409 |
|
1410 void tst_QTextCursor::update_data() |
|
1411 { |
|
1412 QTest::addColumn<QString>("text"); |
|
1413 QTest::addColumn<int>("position"); |
|
1414 QTest::addColumn<int>("anchor"); |
|
1415 QTest::addColumn<int>("modifyPosition"); |
|
1416 QTest::addColumn<int>("modifyAnchor"); |
|
1417 QTest::addColumn<QString>("insertText"); |
|
1418 QTest::addColumn<int>("expectedPosition"); |
|
1419 QTest::addColumn<int>("expectedAnchor"); |
|
1420 |
|
1421 QString text("Hello big world"); |
|
1422 int charsToDelete = 3; |
|
1423 QTest::newRow("removeInsideSelection") |
|
1424 << text |
|
1425 << /*position*/ 0 |
|
1426 << /*anchor*/ text.length() |
|
1427 // delete 'big' |
|
1428 << 6 |
|
1429 << 6 + charsToDelete |
|
1430 << QString() // don't insert anything, just remove |
|
1431 << /*expectedPosition*/ 0 |
|
1432 << /*expectedAnchor*/ text.length() - charsToDelete |
|
1433 ; |
|
1434 |
|
1435 text = "Hello big world"; |
|
1436 charsToDelete = 3; |
|
1437 QTest::newRow("removeInsideSelectionWithSwappedAnchorAndPosition") |
|
1438 << text |
|
1439 << /*position*/ text.length() |
|
1440 << /*anchor*/ 0 |
|
1441 // delete 'big' |
|
1442 << 6 |
|
1443 << 6 + charsToDelete |
|
1444 << QString() // don't insert anything, just remove |
|
1445 << /*expectedPosition*/ text.length() - charsToDelete |
|
1446 << /*expectedAnchor*/ 0 |
|
1447 ; |
|
1448 |
|
1449 |
|
1450 text = "Hello big world"; |
|
1451 charsToDelete = 3; |
|
1452 QString textToInsert("small"); |
|
1453 QTest::newRow("replaceInsideSelection") |
|
1454 << text |
|
1455 << /*position*/ 0 |
|
1456 << /*anchor*/ text.length() |
|
1457 // delete 'big' ... |
|
1458 << 6 |
|
1459 << 6 + charsToDelete |
|
1460 << textToInsert // ... and replace 'big' with 'small' |
|
1461 << /*expectedPosition*/ 0 |
|
1462 << /*expectedAnchor*/ text.length() - charsToDelete + textToInsert.length() |
|
1463 ; |
|
1464 |
|
1465 text = "Hello big world"; |
|
1466 charsToDelete = 3; |
|
1467 textToInsert = "small"; |
|
1468 QTest::newRow("replaceInsideSelectionWithSwappedAnchorAndPosition") |
|
1469 << text |
|
1470 << /*position*/ text.length() |
|
1471 << /*anchor*/ 0 |
|
1472 // delete 'big' ... |
|
1473 << 6 |
|
1474 << 6 + charsToDelete |
|
1475 << textToInsert // ... and replace 'big' with 'small' |
|
1476 << /*expectedPosition*/ text.length() - charsToDelete + textToInsert.length() |
|
1477 << /*expectedAnchor*/ 0 |
|
1478 ; |
|
1479 |
|
1480 |
|
1481 text = "Hello big world"; |
|
1482 charsToDelete = 3; |
|
1483 QTest::newRow("removeBeforeSelection") |
|
1484 << text |
|
1485 << /*position*/ text.length() - 5 |
|
1486 << /*anchor*/ text.length() |
|
1487 // delete 'big' |
|
1488 << 6 |
|
1489 << 6 + charsToDelete |
|
1490 << QString() // don't insert anything, just remove |
|
1491 << /*expectedPosition*/ text.length() - 5 - charsToDelete |
|
1492 << /*expectedAnchor*/ text.length() - charsToDelete |
|
1493 ; |
|
1494 |
|
1495 text = "Hello big world"; |
|
1496 charsToDelete = 3; |
|
1497 QTest::newRow("removeAfterSelection") |
|
1498 << text |
|
1499 << /*position*/ 0 |
|
1500 << /*anchor*/ 4 |
|
1501 // delete 'big' |
|
1502 << 6 |
|
1503 << 6 + charsToDelete |
|
1504 << QString() // don't insert anything, just remove |
|
1505 << /*expectedPosition*/ 0 |
|
1506 << /*expectedAnchor*/ 4 |
|
1507 ; |
|
1508 |
|
1509 } |
|
1510 |
|
1511 void tst_QTextCursor::update() |
|
1512 { |
|
1513 QFETCH(QString, text); |
|
1514 |
|
1515 doc->setPlainText(text); |
|
1516 |
|
1517 QFETCH(int, position); |
|
1518 QFETCH(int, anchor); |
|
1519 |
|
1520 cursor.setPosition(anchor); |
|
1521 cursor.setPosition(position, QTextCursor::KeepAnchor); |
|
1522 |
|
1523 QCOMPARE(cursor.position(), position); |
|
1524 QCOMPARE(cursor.anchor(), anchor); |
|
1525 |
|
1526 QFETCH(int, modifyPosition); |
|
1527 QFETCH(int, modifyAnchor); |
|
1528 |
|
1529 QTextCursor modifyCursor = cursor; |
|
1530 modifyCursor.setPosition(modifyAnchor); |
|
1531 modifyCursor.setPosition(modifyPosition, QTextCursor::KeepAnchor); |
|
1532 |
|
1533 QCOMPARE(modifyCursor.position(), modifyPosition); |
|
1534 QCOMPARE(modifyCursor.anchor(), modifyAnchor); |
|
1535 |
|
1536 QFETCH(QString, insertText); |
|
1537 modifyCursor.insertText(insertText); |
|
1538 |
|
1539 QFETCH(int, expectedPosition); |
|
1540 QFETCH(int, expectedAnchor); |
|
1541 |
|
1542 QCOMPARE(cursor.position(), expectedPosition); |
|
1543 QCOMPARE(cursor.anchor(), expectedAnchor); |
|
1544 } |
|
1545 |
|
1546 void tst_QTextCursor::disallowSettingObjectIndicesOnCharFormats() |
|
1547 { |
|
1548 QTextCharFormat fmt; |
|
1549 fmt.setObjectIndex(42); |
|
1550 cursor.insertText("Hey", fmt); |
|
1551 QCOMPARE(cursor.charFormat().objectIndex(), -1); |
|
1552 |
|
1553 cursor.select(QTextCursor::Document); |
|
1554 cursor.mergeCharFormat(fmt); |
|
1555 QCOMPARE(doc->begin().begin().fragment().charFormat().objectIndex(), -1); |
|
1556 |
|
1557 cursor.select(QTextCursor::Document); |
|
1558 cursor.setCharFormat(fmt); |
|
1559 QCOMPARE(doc->begin().begin().fragment().charFormat().objectIndex(), -1); |
|
1560 |
|
1561 cursor.setBlockCharFormat(fmt); |
|
1562 QCOMPARE(cursor.blockCharFormat().objectIndex(), -1); |
|
1563 |
|
1564 cursor.movePosition(QTextCursor::End); |
|
1565 cursor.insertBlock(QTextBlockFormat(), fmt); |
|
1566 QCOMPARE(cursor.blockCharFormat().objectIndex(), -1); |
|
1567 |
|
1568 doc->clear(); |
|
1569 |
|
1570 QTextTable *table = cursor.insertTable(1, 1); |
|
1571 cursor.select(QTextCursor::Document); |
|
1572 cursor.setCharFormat(fmt); |
|
1573 |
|
1574 cursor = table->cellAt(0, 0).firstCursorPosition(); |
|
1575 QVERIFY(!cursor.isNull()); |
|
1576 QCOMPARE(cursor.blockCharFormat().objectIndex(), table->objectIndex()); |
|
1577 } |
|
1578 |
|
1579 void tst_QTextCursor::blockAndColumnNumber() |
|
1580 { |
|
1581 QCOMPARE(QTextCursor().columnNumber(), 0); |
|
1582 QCOMPARE(QTextCursor().blockNumber(), 0); |
|
1583 |
|
1584 QCOMPARE(cursor.columnNumber(), 0); |
|
1585 QCOMPARE(cursor.blockNumber(), 0); |
|
1586 cursor.insertText("Hello"); |
|
1587 QCOMPARE(cursor.columnNumber(), 5); |
|
1588 QCOMPARE(cursor.blockNumber(), 0); |
|
1589 |
|
1590 cursor.insertBlock(); |
|
1591 QCOMPARE(cursor.columnNumber(), 0); |
|
1592 QCOMPARE(cursor.blockNumber(), 1); |
|
1593 cursor.insertText("Blah"); |
|
1594 QCOMPARE(cursor.blockNumber(), 1); |
|
1595 |
|
1596 // trigger a layout |
|
1597 doc->documentLayout(); |
|
1598 |
|
1599 cursor.insertBlock(); |
|
1600 QCOMPARE(cursor.columnNumber(), 0); |
|
1601 QCOMPARE(cursor.blockNumber(), 2); |
|
1602 cursor.insertText("Test"); |
|
1603 QCOMPARE(cursor.columnNumber(), 4); |
|
1604 QCOMPARE(cursor.blockNumber(), 2); |
|
1605 cursor.insertText(QString(QChar(QChar::LineSeparator))); |
|
1606 QCOMPARE(cursor.columnNumber(), 0); |
|
1607 QCOMPARE(cursor.blockNumber(), 2); |
|
1608 cursor.insertText("A"); |
|
1609 QCOMPARE(cursor.columnNumber(), 1); |
|
1610 QCOMPARE(cursor.blockNumber(), 2); |
|
1611 } |
|
1612 |
|
1613 void tst_QTextCursor::movePositionEndOfLine() |
|
1614 { |
|
1615 cursor.insertText("blah\nblah\n"); |
|
1616 // Select part of the second line ("la") |
|
1617 cursor.setPosition(6); |
|
1618 cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 2); |
|
1619 QCOMPARE(cursor.selectedText(), QLatin1String("la")); |
|
1620 |
|
1621 // trigger a layout |
|
1622 doc->documentLayout(); |
|
1623 |
|
1624 // Remove "la" and append "something" to the end in one undo operation |
|
1625 cursor.beginEditBlock(); |
|
1626 cursor.removeSelectedText(); |
|
1627 QTextCursor c2(doc); |
|
1628 c2.setPosition(7); |
|
1629 c2.insertText("foo"); // append to doc without touching the cursor. |
|
1630 |
|
1631 QCOMPARE(cursor.position(), 6); |
|
1632 cursor.movePosition(QTextCursor::EndOfLine); // in an edit block visual movement is moved to the end of the paragraph |
|
1633 QCOMPARE(cursor.position(), 10); |
|
1634 cursor.endEditBlock(); |
|
1635 } |
|
1636 |
|
1637 void tst_QTextCursor::clearCells() |
|
1638 { |
|
1639 QTextTable *table = cursor.insertTable(3, 5); |
|
1640 cursor.setPosition(table->cellAt(0,0).firstPosition()); // select cell 1 and cell 2 |
|
1641 cursor.setPosition(table->cellAt(0,1).firstPosition(), QTextCursor::KeepAnchor); |
|
1642 cursor.deleteChar(); // should clear the cells, and not crash ;) |
|
1643 } |
|
1644 |
|
1645 void tst_QTextCursor::task244408_wordUnderCursor_data() |
|
1646 { |
|
1647 QTest::addColumn<QString>("input"); |
|
1648 QTest::addColumn<QString>("expected"); |
|
1649 QTest::newRow("trailingSpace") << QString::fromLatin1("foo ") << QString::fromLatin1(""); |
|
1650 QTest::newRow("noTrailingSpace") << QString::fromLatin1("foo") << QString::fromLatin1("foo"); |
|
1651 } |
|
1652 |
|
1653 void tst_QTextCursor::task244408_wordUnderCursor() |
|
1654 { |
|
1655 QFETCH(QString, input); |
|
1656 QFETCH(QString, expected); |
|
1657 cursor.insertText(input); |
|
1658 cursor.movePosition(QTextCursor::End); |
|
1659 cursor.select(QTextCursor::WordUnderCursor); |
|
1660 QCOMPARE(cursor.selectedText(), expected); |
|
1661 } |
|
1662 |
|
1663 void tst_QTextCursor::adjustCursorsOnInsert() |
|
1664 { |
|
1665 cursor.insertText("Some text before "); |
|
1666 int posBefore = cursor.position(); |
|
1667 cursor.insertText("selected text"); |
|
1668 int posAfter = cursor.position(); |
|
1669 cursor.insertText(" some text afterwards"); |
|
1670 |
|
1671 QTextCursor selection = cursor; |
|
1672 selection.setPosition(posBefore); |
|
1673 selection.setPosition(posAfter, QTextCursor::KeepAnchor); |
|
1674 |
|
1675 cursor.setPosition(posBefore-1); |
|
1676 cursor.insertText(QLatin1String("x")); |
|
1677 QCOMPARE(selection.anchor(), posBefore+1); |
|
1678 QCOMPARE(selection.position(), posAfter+1); |
|
1679 doc->undo(); |
|
1680 |
|
1681 cursor.setPosition(posBefore); |
|
1682 cursor.insertText(QLatin1String("x")); |
|
1683 QCOMPARE(selection.anchor(), posBefore+1); |
|
1684 QCOMPARE(selection.position(), posAfter+1); |
|
1685 doc->undo(); |
|
1686 |
|
1687 cursor.setPosition(posBefore+1); |
|
1688 cursor.insertText(QLatin1String("x")); |
|
1689 QCOMPARE(selection.anchor(), posBefore); |
|
1690 QCOMPARE(selection.position(), posAfter+1); |
|
1691 doc->undo(); |
|
1692 |
|
1693 cursor.setPosition(posAfter-1); |
|
1694 cursor.insertText(QLatin1String("x")); |
|
1695 QCOMPARE(selection.anchor(), posBefore); |
|
1696 QCOMPARE(selection.position(), posAfter+1); |
|
1697 doc->undo(); |
|
1698 |
|
1699 cursor.setPosition(posAfter); |
|
1700 cursor.insertText(QLatin1String("x")); |
|
1701 QCOMPARE(selection.anchor(), posBefore); |
|
1702 QCOMPARE(selection.position(), posAfter); |
|
1703 doc->undo(); |
|
1704 |
|
1705 cursor.setPosition(posAfter+1); |
|
1706 cursor.insertText(QLatin1String("x")); |
|
1707 QCOMPARE(selection.anchor(), posBefore); |
|
1708 QCOMPARE(selection.position(), posAfter); |
|
1709 doc->undo(); |
|
1710 |
|
1711 selection.setPosition(posAfter); |
|
1712 selection.setPosition(posBefore, QTextCursor::KeepAnchor); |
|
1713 |
|
1714 cursor.setPosition(posBefore-1); |
|
1715 cursor.insertText(QLatin1String("x")); |
|
1716 QCOMPARE(selection.position(), posBefore+1); |
|
1717 QCOMPARE(selection.anchor(), posAfter+1); |
|
1718 doc->undo(); |
|
1719 |
|
1720 cursor.setPosition(posBefore); |
|
1721 cursor.insertText(QLatin1String("x")); |
|
1722 QCOMPARE(selection.position(), posBefore+1); |
|
1723 QCOMPARE(selection.anchor(), posAfter+1); |
|
1724 doc->undo(); |
|
1725 |
|
1726 cursor.setPosition(posBefore+1); |
|
1727 cursor.insertText(QLatin1String("x")); |
|
1728 QCOMPARE(selection.position(), posBefore); |
|
1729 QCOMPARE(selection.anchor(), posAfter+1); |
|
1730 doc->undo(); |
|
1731 |
|
1732 cursor.setPosition(posAfter-1); |
|
1733 cursor.insertText(QLatin1String("x")); |
|
1734 QCOMPARE(selection.position(), posBefore); |
|
1735 QCOMPARE(selection.anchor(), posAfter+1); |
|
1736 doc->undo(); |
|
1737 |
|
1738 cursor.setPosition(posAfter); |
|
1739 cursor.insertText(QLatin1String("x")); |
|
1740 QCOMPARE(selection.position(), posBefore); |
|
1741 QCOMPARE(selection.anchor(), posAfter+1); |
|
1742 doc->undo(); |
|
1743 |
|
1744 cursor.setPosition(posAfter+1); |
|
1745 cursor.insertText(QLatin1String("x")); |
|
1746 QCOMPARE(selection.position(), posBefore); |
|
1747 QCOMPARE(selection.anchor(), posAfter); |
|
1748 doc->undo(); |
|
1749 |
|
1750 |
|
1751 |
|
1752 |
|
1753 } |
|
1754 |
|
1755 QTEST_MAIN(tst_QTextCursor) |
|
1756 #include "tst_qtextcursor.moc" |