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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <qbuffer.h>
#include <qtextdocument.h>
#include <qtextdocumentfragment.h>
#include <qtexttable.h>
#include <qdebug.h>
#include <qtextcursor.h>
#include <qtextdocument.h>
#ifndef QT_NO_WIDGETS
#include <qtextedit.h>
#endif
#ifndef QT_NO_PRINTER
#include <QPagedPaintDevice>
#include <QPainter>
#include <QPaintEngine>
#endif
#include <private/qtextdocumentlayout_p.h>
#include <private/qpagedpaintdevice_p.h>
typedef QList<int> IntList;
QT_FORWARD_DECLARE_CLASS(QTextDocument)
Q_DECLARE_METATYPE(QTextFrameFormat::BorderStyle);
class tst_QTextTable : public QObject
{
Q_OBJECT
private slots:
void init();
void cleanup();
void cursorPositioning();
void variousTableModifications();
void tableShrinking();
void spans();
void variousModifications2();
void tableManager_undo();
void tableManager_removeCell();
void rowAt();
void rowAtWithSpans();
void multiBlockCells();
void insertRows();
void deleteInTable();
void mergeCells();
void mergeAndInsert();
void splitCells();
void blocksForTableShouldHaveEmptyFormat();
void removeTableByRemoveRows();
void removeTableByRemoveColumns();
void setCellFormat();
void removeRows1();
void removeRows2();
void removeRows3();
void removeRows4();
void removeRows5();
void removeColumns1();
void removeColumns2();
void removeColumns3();
void removeColumns4();
void removeColumns5();
void removeColumnsInTableWithMergedRows();
#ifndef QT_NO_WIDGETS
void QTBUG11282_insertBeforeMergedEnding_data();
void QTBUG11282_insertBeforeMergedEnding();
#endif
void QTBUG22011_insertBeforeRowSpan();
#if !defined(QT_NO_PRINTER) && defined(QT_BUILD_INTERNAL)
void QTBUG31330_renderBackground();
#endif
void checkBorderAttributes_data();
void checkBorderAttributes();
#ifndef QT_NO_WIDGETS
void columnWidthWithSpans();
void columnWidthWithImage_data();
void columnWidthWithImage();
#endif
private:
QTextTable *create2x2Table();
QTextTable *create4x4Table();
QTextTable *createTable(int rows, int cols);
QTextDocument *doc;
QTextCursor cursor;
};
void tst_QTextTable::init()
{
doc = new QTextDocument;
cursor = QTextCursor(doc);
}
void tst_QTextTable::cleanup()
{
cursor = QTextCursor();
delete doc;
doc = 0;
}
void tst_QTextTable::cursorPositioning()
{
// ensure the cursor is placed at the beginning of the first cell upon
// table creation
QTextTable *table = cursor.insertTable(2, 2);
QVERIFY(cursor == table->cellAt(0, 0).firstCursorPosition());
QVERIFY(table->cellAt(0, 0).firstPosition() == table->firstPosition());
}
void tst_QTextTable::variousTableModifications()
{
QTextTableFormat tableFmt;
QTextTable *tab = cursor.insertTable(2, 2, tableFmt);
QCOMPARE(doc->toPlainText().length(), 5);
QCOMPARE(tab, cursor.currentTable());
QCOMPARE(tab->columns(), 2);
QCOMPARE(tab->rows(), 2);
QCOMPARE(cursor.position(), 1);
QTextCharFormat fmt = cursor.charFormat();
QCOMPARE(fmt.objectIndex(), -1);
QTextTableCell cell = tab->cellAt(cursor);
QVERIFY(cell.isValid());
QCOMPARE(cell.row(), 0);
QCOMPARE(cell.column(), 0);
cursor.movePosition(QTextCursor::NextBlock);
QCOMPARE(cursor.position(), 2);
fmt = cursor.charFormat();
QCOMPARE(fmt.objectIndex(), -1);
cell = tab->cellAt(cursor);
QVERIFY(cell.isValid());
QCOMPARE(cell.row(), 0);
QCOMPARE(cell.column(), 1);
cursor.movePosition(QTextCursor::NextBlock);
QCOMPARE(cursor.position(), 3);
fmt = cursor.charFormat();
QCOMPARE(fmt.objectIndex(), -1);
cell = tab->cellAt(cursor);
QVERIFY(cell.isValid());
QCOMPARE(cell.row(), 1);
QCOMPARE(cell.column(), 0);
cursor.movePosition(QTextCursor::NextBlock);
QCOMPARE(cursor.position(), 4);
fmt = cursor.charFormat();
QCOMPARE(fmt.objectIndex(), -1);
cell = tab->cellAt(cursor);
QVERIFY(cell.isValid());
QCOMPARE(cell.row(), 1);
QCOMPARE(cell.column(), 1);
cursor.movePosition(QTextCursor::NextBlock);
QCOMPARE(cursor.position(), 5);
fmt = cursor.charFormat();
QCOMPARE(fmt.objectIndex(), -1);
cell = tab->cellAt(cursor);
QVERIFY(!cell.isValid());
cursor.movePosition(QTextCursor::NextBlock);
QCOMPARE(cursor.position(), 5);
// check we can't delete the cells with the cursor
cursor.movePosition(QTextCursor::Start);
cursor.movePosition(QTextCursor::NextBlock);
QCOMPARE(cursor.position(), 1);
cursor.deleteChar();
QCOMPARE(doc->toPlainText().length(), 5);
cursor.movePosition(QTextCursor::NextBlock);
QCOMPARE(cursor.position(), 2);
cursor.deleteChar();
QCOMPARE(doc->toPlainText().length(), 5);
cursor.deletePreviousChar();
QCOMPARE(cursor.position(), 2);
QCOMPARE(doc->toPlainText().length(), 5);
QTextTable *table = cursor.currentTable();
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 2);
table->insertRows(2, 1);
QCOMPARE(table->rows(), 3);
QCOMPARE(table->columns(), 2);
QCOMPARE(doc->toPlainText().length(), 7);
table->insertColumns(2, 2);
QCOMPARE(table->rows(), 3);
QCOMPARE(table->columns(), 4);
QCOMPARE(doc->toPlainText().length(), 13);
table->resize(4, 5);
QCOMPARE(table->rows(), 4);
QCOMPARE(table->columns(), 5);
QCOMPARE(doc->toPlainText().length(), 21);
}
void tst_QTextTable::tableShrinking()
{
QTextTableFormat tableFmt;
cursor.insertTable(3, 4, tableFmt);
QCOMPARE(doc->toPlainText().length(), 13);
QTextTable *table = cursor.currentTable();
QCOMPARE(table->rows(), 3);
QCOMPARE(table->columns(), 4);
table->removeRows(1, 1);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 4);
QCOMPARE(doc->toPlainText().length(), 9);
table->removeColumns(1, 2);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 2);
QCOMPARE(doc->toPlainText().length(), 5);
table->resize(1, 1);
QCOMPARE(table->rows(), 1);
QCOMPARE(table->columns(), 1);
QCOMPARE(doc->toPlainText().length(), 2);
}
void tst_QTextTable::spans()
{
QTextTableFormat tableFmt;
cursor.insertTable(2, 2, tableFmt);
QTextTable *table = cursor.currentTable();
QVERIFY(table->cellAt(0, 0) != table->cellAt(0, 1));
table->mergeCells(0, 0, 1, 2);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 2);
QVERIFY(table->cellAt(0, 0) == table->cellAt(0, 1));
table->mergeCells(0, 0, 2, 2);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 2);
}
void tst_QTextTable::variousModifications2()
{
QTextTableFormat tableFmt;
cursor.insertTable(2, 5, tableFmt);
QCOMPARE(doc->toPlainText().length(), 11);
QTextTable *table = cursor.currentTable();
QCOMPARE(cursor.position(), 1);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 5);
table->insertColumns(0, 1);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 6);
table->insertColumns(6, 1);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 7);
table->insertRows(0, 1);
QCOMPARE(table->rows(), 3);
QCOMPARE(table->columns(), 7);
table->insertRows(3, 1);
QCOMPARE(table->rows(), 4);
QCOMPARE(table->columns(), 7);
table->removeRows(0, 1);
QCOMPARE(table->rows(), 3);
QCOMPARE(table->columns(), 7);
table->removeRows(2, 1);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 7);
table->removeColumns(0, 1);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 6);
table->removeColumns(5, 1);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 5);
tableFmt = table->format();
table->insertColumns(2, 1);
table->setFormat(tableFmt);
table->insertColumns(2, 1);
QCOMPARE(table->columns(), 7);
}
void tst_QTextTable::tableManager_undo()
{
QTextTableFormat fmt;
fmt.setBorder(10);
QTextTable *table = cursor.insertTable(2, 2, fmt);
QVERIFY(table);
QCOMPARE(table->format().border(), qreal(10));
fmt.setBorder(20);
table->setFormat(fmt);
QCOMPARE(table->format().border(), qreal(20));
doc->undo();
QCOMPARE(table->format().border(), qreal(10));
}
void tst_QTextTable::tableManager_removeCell()
{
// essentially a test for TableManager::removeCell, in particular to remove empty items from the rowlist.
// If it fails it'll triger assertions inside TableManager. Yeah, not pretty, should VERIFY here ;(
cursor.insertTable(2, 2, QTextTableFormat());
doc->undo();
// ###
QVERIFY(true);
}
void tst_QTextTable::rowAt()
{
// test TablePrivate::rowAt
QTextTable *table = cursor.insertTable(4, 2);
QCOMPARE(table->rows(), 4);
QCOMPARE(table->columns(), 2);
QTextCursor cell00Cursor = table->cellAt(0, 0).firstCursorPosition();
QTextCursor cell10Cursor = table->cellAt(1, 0).firstCursorPosition();
QTextCursor cell20Cursor = table->cellAt(2, 0).firstCursorPosition();
QTextCursor cell21Cursor = table->cellAt(2, 1).firstCursorPosition();
QTextCursor cell30Cursor = table->cellAt(3, 0).firstCursorPosition();
QCOMPARE(table->cellAt(cell00Cursor).firstCursorPosition(), cell00Cursor);
QCOMPARE(table->cellAt(cell10Cursor).firstCursorPosition(), cell10Cursor);
QCOMPARE(table->cellAt(cell20Cursor).firstCursorPosition(), cell20Cursor);
QCOMPARE(table->cellAt(cell30Cursor).firstCursorPosition(), cell30Cursor);
table->mergeCells(1, 0, 2, 1);
QCOMPARE(table->rows(), 4);
QCOMPARE(table->columns(), 2);
QVERIFY(cell00Cursor == table->cellAt(0, 0).firstCursorPosition());
QVERIFY(cell10Cursor == table->cellAt(1, 0).firstCursorPosition());
QVERIFY(cell10Cursor == table->cellAt(2, 0).firstCursorPosition());
QVERIFY(cell21Cursor == table->cellAt(2, 1).firstCursorPosition());
QVERIFY(cell30Cursor == table->cellAt(3, 0).firstCursorPosition());
table->mergeCells(1, 0, 2, 2);
QCOMPARE(table->rows(), 4);
QCOMPARE(table->columns(), 2);
QVERIFY(cell00Cursor == table->cellAt(0, 0).firstCursorPosition());
QVERIFY(cell00Cursor == table->cellAt(0, 0).firstCursorPosition());
QVERIFY(cell10Cursor == table->cellAt(1, 0).firstCursorPosition());
QVERIFY(cell10Cursor == table->cellAt(1, 1).firstCursorPosition());
QVERIFY(cell10Cursor == table->cellAt(2, 0).firstCursorPosition());
QVERIFY(cell10Cursor == table->cellAt(2, 1).firstCursorPosition());
QVERIFY(cell30Cursor == table->cellAt(3, 0).firstCursorPosition());
}
void tst_QTextTable::rowAtWithSpans()
{
QTextTable *table = cursor.insertTable(2, 2);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 2);
table->mergeCells(0, 0, 2, 1);
QVERIFY(table->cellAt(0, 0).rowSpan() == 2);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 2);
table->mergeCells(0, 0, 2, 2);
QVERIFY(table->cellAt(0, 0).columnSpan() == 2);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 2);
}
void tst_QTextTable::multiBlockCells()
{
// little testcase for multi-block cells
QTextTable *table = cursor.insertTable(2, 2);
QVERIFY(cursor == table->cellAt(0, 0).firstCursorPosition());
cursor.insertText("Hello");
cursor.insertBlock(QTextBlockFormat());
cursor.insertText("World");
cursor.movePosition(QTextCursor::Left);
QVERIFY(table->cellAt(0, 0) == table->cellAt(cursor));
}
void tst_QTextTable::insertRows()
{
// little testcase for multi-block cells
QTextTable *table = cursor.insertTable(2, 2);
QVERIFY(cursor == table->cellAt(0, 0).firstCursorPosition());
table->insertRows(0, 1);
QCOMPARE(table->rows(), 3);
table->insertRows(1, 1);
QCOMPARE(table->rows(), 4);
table->insertRows(-1, 1);
QCOMPARE(table->rows(), 5);
table->insertRows(5, 2);
QCOMPARE(table->rows(), 7);
table = cursor.insertTable(5,5);
table->mergeCells(0,0,3,3);
table->insertRows(2,1);
QCOMPARE(table->rows(), 6);
}
void tst_QTextTable::deleteInTable()
{
QTextTable *table = cursor.insertTable(2, 2);
table->cellAt(0, 0).firstCursorPosition().insertText("Blah");
table->cellAt(0, 1).firstCursorPosition().insertText("Foo");
table->cellAt(1, 0).firstCursorPosition().insertText("Bar");
table->cellAt(1, 1).firstCursorPosition().insertText("Hah");
cursor = table->cellAt(1, 1).firstCursorPosition();
cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::KeepAnchor);
QCOMPARE(table->cellAt(cursor.position()).row(), 1);
QCOMPARE(table->cellAt(cursor.position()).column(), 0);
cursor.removeSelectedText();
QCOMPARE(table->columns(), 2);
QCOMPARE(table->rows(), 2);
// verify table is still all in shape. Only the text inside should get deleted
for (int row = 0; row < table->rows(); ++row)
for (int col = 0; col < table->columns(); ++col) {
const QTextTableCell cell = table->cellAt(row, col);
QVERIFY(cell.isValid());
QCOMPARE(cell.rowSpan(), 1);
QCOMPARE(cell.columnSpan(), 1);
}
}
QTextTable *tst_QTextTable::create2x2Table()
{
cleanup();
init();
QTextTable *table = cursor.insertTable(2, 2);
table->cellAt(0, 0).firstCursorPosition().insertText("Blah");
table->cellAt(0, 1).firstCursorPosition().insertText("Foo");
table->cellAt(1, 0).firstCursorPosition().insertText("Bar");
table->cellAt(1, 1).firstCursorPosition().insertText("Hah");
return table;
}
QTextTable *tst_QTextTable::create4x4Table()
{
cleanup();
init();
QTextTable *table = cursor.insertTable(4, 4);
table->cellAt(0, 0).firstCursorPosition().insertText("Blah");
table->cellAt(0, 1).firstCursorPosition().insertText("Foo");
table->cellAt(1, 0).firstCursorPosition().insertText("Bar");
table->cellAt(1, 1).firstCursorPosition().insertText("Hah");
return table;
}
QTextTable *tst_QTextTable::createTable(int rows, int cols)
{
cleanup();
init();
QTextTable *table = cursor.insertTable(rows, cols);
return table;
}
void tst_QTextTable::mergeCells()
{
QTextTable *table = create4x4Table();
table->mergeCells(1, 1, 1, 2);
QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
table->mergeCells(1, 1, 2, 2);
QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 2));
table = create4x4Table();
table->mergeCells(1, 1, 2, 1);
QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
table->mergeCells(1, 1, 2, 2);
QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 2));
table = create4x4Table();
table->mergeCells(1, 1, 2, 2);
QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 2));
// should do nothing
table->mergeCells(1, 1, 1, 1);
QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 2));
table = create2x2Table();
table->mergeCells(0, 1, 2, 1);
table->mergeCells(0, 0, 2, 2);
QVERIFY(table->cellAt(0, 0) == table->cellAt(0, 1));
QVERIFY(table->cellAt(0, 0) == table->cellAt(1, 0));
QVERIFY(table->cellAt(0, 0) == table->cellAt(1, 1));
QTextBlock block = table->cellAt(0, 0).firstCursorPosition().block();
QCOMPARE(block.text(), QLatin1String("Blah Foo"));
QCOMPARE(block.next().text(), QLatin1String("Hah"));
QCOMPARE(block.next().next().text(), QLatin1String("Bar"));
table = create4x4Table();
QTextCursor cursor = table->cellAt(3, 3).firstCursorPosition();
QTextTable *t2 = cursor.insertTable(2, 2);
t2->cellAt(0, 0).firstCursorPosition().insertText("Test");
table->mergeCells(2, 2, 2, 2);
cursor = table->cellAt(2, 2).firstCursorPosition();
QTextFrame *frame = cursor.currentFrame();
QTextFrame::iterator it = frame->begin();
// find the embedded table
while (it != frame->end() && !it.currentFrame())
++it;
table = qobject_cast<QTextTable *>(it.currentFrame());
QVERIFY(table);
if (table) {
cursor = table->cellAt(0, 0).firstCursorPosition();
QCOMPARE(cursor.block().text(), QLatin1String("Test"));
}
table = create2x2Table();
table->mergeCells(0, 1, 2, 1);
QVERIFY(table->cellAt(0, 0) != table->cellAt(0, 1));
QVERIFY(table->cellAt(0, 1) == table->cellAt(1, 1));
// should do nothing
table->mergeCells(0, 0, 1, 2);
QVERIFY(table->cellAt(0, 0) != table->cellAt(0, 1));
QVERIFY(table->cellAt(0, 1) == table->cellAt(1, 1));
}
void tst_QTextTable::mergeAndInsert()
{
QTextTable *table = cursor.insertTable(4,3);
table->mergeCells(0,1,3,2);
table->mergeCells(3,0,1,3);
//Don't crash !
table->insertColumns(1,2);
QCOMPARE(table->columns(), 5);
}
void tst_QTextTable::splitCells()
{
QTextTable *table = create4x4Table();
table->mergeCells(1, 1, 2, 2);
QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 2));
table->splitCell(1, 1, 1, 2);
QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 1));
QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 2));
table->splitCell(1, 1, 1, 1);
QVERIFY(table->cellAt(1, 1) != table->cellAt(1, 2));
QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 1));
QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 2));
table = create4x4Table();
table->mergeCells(1, 1, 2, 2);
QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 2));
table->splitCell(1, 1, 2, 1);
QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
QVERIFY(table->cellAt(1, 1) != table->cellAt(1, 2));
QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 2));
table->splitCell(1, 1, 1, 1);
QVERIFY(table->cellAt(1, 1) != table->cellAt(1, 2));
QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 1));
QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 2));
table = create4x4Table();
table->mergeCells(1, 1, 2, 2);
QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 2));
table->splitCell(1, 1, 1, 1);
QVERIFY(table->cellAt(1, 1) != table->cellAt(1, 2));
QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 1));
QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 2));
table = createTable(2, 5);
table->mergeCells(0, 0, 2, 1);
table->mergeCells(0, 1, 2, 1);
QVERIFY(table->cellAt(0, 0) == table->cellAt(1, 0));
QVERIFY(table->cellAt(0, 1) == table->cellAt(1, 1));
table->splitCell(0, 0, 1, 1);
QVERIFY(table->cellAt(0, 0) != table->cellAt(1, 0));
QVERIFY(table->cellAt(0, 1) == table->cellAt(1, 1));
table = createTable(2, 5);
table->mergeCells(0, 4, 2, 1);
QVERIFY(table->cellAt(0, 4) == table->cellAt(1, 4));
table->splitCell(0, 4, 1, 1);
QVERIFY(table->cellAt(0, 4) != table->cellAt(1, 4));
}
void tst_QTextTable::blocksForTableShouldHaveEmptyFormat()
{
QTextBlockFormat fmt;
fmt.setProperty(QTextFormat::UserProperty, true);
cursor.insertBlock(fmt);
QVERIFY(cursor.blockFormat().hasProperty(QTextFormat::UserProperty));
QTextTable *table = cursor.insertTable(1, 1);
QVERIFY(!table->cellAt(0, 0).firstCursorPosition().blockFormat().hasProperty(QTextFormat::UserProperty));
int userPropCount = 0;
for (QTextBlock block = doc->begin();
block.isValid(); block = block.next()) {
if (block.blockFormat().hasProperty(QTextFormat::UserProperty))
userPropCount++;
}
QCOMPARE(userPropCount, 1);
}
void tst_QTextTable::removeTableByRemoveRows()
{
QPointer<QTextTable> table1 = QTextCursor(cursor).insertTable(4, 4);
QPointer<QTextTable> table2 = QTextCursor(cursor).insertTable(4, 4);
QPointer<QTextTable> table3 = QTextCursor(cursor).insertTable(4, 4);
QVERIFY(table1);
QVERIFY(table2);
QVERIFY(table3);
table2->removeRows(1, 1);
QVERIFY(table1);
QVERIFY(table2);
QVERIFY(table3);
table2->removeRows(0, table2->rows());
QVERIFY(table1);
QVERIFY(!table2);
QVERIFY(table3);
}
void tst_QTextTable::removeTableByRemoveColumns()
{
QPointer<QTextTable> table1 = QTextCursor(cursor).insertTable(4, 4);
QPointer<QTextTable> table2 = QTextCursor(cursor).insertTable(4, 4);
QPointer<QTextTable> table3 = QTextCursor(cursor).insertTable(4, 4);
QVERIFY(table1);
QVERIFY(table2);
QVERIFY(table3);
table2->removeColumns(1, 1);
QVERIFY(table1);
QVERIFY(table2);
QVERIFY(table3);
table2->removeColumns(0, table2->columns());
QVERIFY(table1);
QVERIFY(!table2);
QVERIFY(table3);
}
void tst_QTextTable::setCellFormat()
{
QTextTable *table = cursor.insertTable(2, 2);
table->cellAt(0, 0).firstCursorPosition().insertText("First");
table->cellAt(0, 1).firstCursorPosition().insertText("Second");
table->cellAt(1, 0).firstCursorPosition().insertText("Third");
table->cellAt(1, 1).firstCursorPosition().insertText("Fourth");
QTextTableCell cell = table->cellAt(0, 0);
QTextCharFormat fmt;
fmt.setObjectIndex(23);
fmt.setBackground(Qt::blue);
fmt.setTableCellColumnSpan(25);
fmt.setTableCellRowSpan(42);
cell.setFormat(fmt);
QCOMPARE(cell.format().background().color(), QColor(Qt::blue));
QCOMPARE(cell.format().tableCellColumnSpan(), 1);
QCOMPARE(cell.format().tableCellRowSpan(), 1);
}
void tst_QTextTable::removeRows1()
{
QTextTable *table = cursor.insertTable(2, 2);
table->cellAt(0, 0).firstCursorPosition().insertText("First");
table->cellAt(0, 1).firstCursorPosition().insertText("Second");
table->cellAt(1, 0).firstCursorPosition().insertText("Third");
table->cellAt(1, 1).firstCursorPosition().insertText("Fourth");
table->removeRows(0, 1);
QCOMPARE(table->rows(), 1);
QCOMPARE(table->columns(), 2);
QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("Third"));
QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("Fourth"));
}
void tst_QTextTable::removeRows2()
{
QTextTable *table = cursor.insertTable(2, 2);
table->cellAt(0, 0).firstCursorPosition().insertText("First");
table->cellAt(0, 1).firstCursorPosition().insertText("Second");
table->cellAt(1, 0).firstCursorPosition().insertText("Third");
table->cellAt(1, 1).firstCursorPosition().insertText("Fourth");
table->removeRows(1, 1);
QCOMPARE(table->rows(), 1);
QCOMPARE(table->columns(), 2);
QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First"));
QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("Second"));
}
void tst_QTextTable::removeRows3()
{
QTextTable *table = cursor.insertTable(3, 2);
table->cellAt(0, 0).firstCursorPosition().insertText("First");
table->cellAt(0, 1).firstCursorPosition().insertText("Second");
table->cellAt(1, 0).firstCursorPosition().insertText("Third");
table->cellAt(1, 1).firstCursorPosition().insertText("Fourth");
table->cellAt(2, 0).firstCursorPosition().insertText("Fifth");
table->cellAt(2, 1).firstCursorPosition().insertText("Sixth");
table->removeRows(1, 1);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 2);
QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First"));
QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("Second"));
QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Fifth"));
QCOMPARE(table->cellAt(1, 1).firstCursorPosition().block().text(), QString("Sixth"));
}
void tst_QTextTable::removeRows4()
{
QTextTable *table = cursor.insertTable(4, 2);
table->cellAt(0, 0).firstCursorPosition().insertText("First");
table->cellAt(0, 1).firstCursorPosition().insertText("Second");
table->cellAt(1, 0).firstCursorPosition().insertText("Third");
table->cellAt(1, 1).firstCursorPosition().insertText("Fourth");
table->cellAt(2, 0).firstCursorPosition().insertText("Fifth");
table->cellAt(2, 1).firstCursorPosition().insertText("Sixth");
table->cellAt(3, 0).firstCursorPosition().insertText("Seventh");
table->cellAt(3, 1).firstCursorPosition().insertText("Eighth");
table->removeRows(1, 2);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 2);
QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First"));
QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("Second"));
QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Seventh"));
QCOMPARE(table->cellAt(1, 1).firstCursorPosition().block().text(), QString("Eighth"));
}
void tst_QTextTable::removeRows5()
{
QTextTable *table = cursor.insertTable(2,2);
table->cellAt(0, 0).firstCursorPosition().insertText("First");
table->cellAt(0, 1).firstCursorPosition().insertText("Second");
table->cellAt(1, 0).firstCursorPosition().insertText("Third");
table->cellAt(1, 1).firstCursorPosition().insertText("Fourth");
table->insertRows(1,1);
table->mergeCells(1,0,1,2);
table->removeRows(1,1);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 2);
QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First"));
QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("Second"));
QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Third"));
QCOMPARE(table->cellAt(1, 1).firstCursorPosition().block().text(), QString("Fourth"));
}
void tst_QTextTable::removeColumns1()
{
QTextTable *table = cursor.insertTable(2, 2);
table->cellAt(0, 0).firstCursorPosition().insertText("First");
table->cellAt(0, 1).firstCursorPosition().insertText("Second");
table->cellAt(1, 0).firstCursorPosition().insertText("Third");
table->cellAt(1, 1).firstCursorPosition().insertText("Fourth");
table->removeColumns(0, 1);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 1);
QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("Second"));
QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Fourth"));
}
void tst_QTextTable::removeColumns2()
{
QTextTable *table = cursor.insertTable(2, 2);
table->cellAt(0, 0).firstCursorPosition().insertText("First");
table->cellAt(0, 1).firstCursorPosition().insertText("Second");
table->cellAt(1, 0).firstCursorPosition().insertText("Third");
table->cellAt(1, 1).firstCursorPosition().insertText("Fourth");
table->removeColumns(1, 1);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 1);
QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First"));
QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Third"));
}
void tst_QTextTable::removeColumns3()
{
QTextTable *table = cursor.insertTable(2, 3);
table->cellAt(0, 0).firstCursorPosition().insertText("First");
table->cellAt(0, 1).firstCursorPosition().insertText("Second");
table->cellAt(0, 2).firstCursorPosition().insertText("Third");
table->cellAt(1, 0).firstCursorPosition().insertText("Fourth");
table->cellAt(1, 1).firstCursorPosition().insertText("Fifth");
table->cellAt(1, 2).firstCursorPosition().insertText("Sixth");
table->removeColumns(1, 1);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 2);
QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First"));
QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("Third"));
QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Fourth"));
QCOMPARE(table->cellAt(1, 1).firstCursorPosition().block().text(), QString("Sixth"));
}
void tst_QTextTable::removeColumns4()
{
QTextTable *table = cursor.insertTable(2, 4);
table->cellAt(0, 0).firstCursorPosition().insertText("First");
table->cellAt(0, 1).firstCursorPosition().insertText("Second");
table->cellAt(0, 2).firstCursorPosition().insertText("Third");
table->cellAt(0, 3).firstCursorPosition().insertText("Fourth");
table->cellAt(1, 0).firstCursorPosition().insertText("Fifth");
table->cellAt(1, 1).firstCursorPosition().insertText("Sixth");
table->cellAt(1, 2).firstCursorPosition().insertText("Seventh");
table->cellAt(1, 3).firstCursorPosition().insertText("Eighth");
table->removeColumns(1, 2);
QCOMPARE(table->rows(), 2);
QCOMPARE(table->columns(), 2);
QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First"));
QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("Fourth"));
QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Fifth"));
QCOMPARE(table->cellAt(1, 1).firstCursorPosition().block().text(), QString("Eighth"));
}
void tst_QTextTable::removeColumns5()
{
QTextTable *table = cursor.insertTable(4, 4);
QTextCursor tc (doc);
tc.setPosition(table->cellAt(2,0).firstPosition());
tc.setPosition(table->cellAt(3,1).firstPosition(), QTextCursor::KeepAnchor);
table->mergeCells(tc);
QCOMPARE(table->rows(), 4);
QCOMPARE(table->columns(), 4);
QCOMPARE(table->cellAt(0, 0).firstPosition(), 1);
QCOMPARE(table->cellAt(0, 1).firstPosition(), 2);
QCOMPARE(table->cellAt(0, 2).firstPosition(), 3);
QCOMPARE(table->cellAt(0, 3).firstPosition(), 4);
QCOMPARE(table->cellAt(1, 0).firstPosition(), 5);
QCOMPARE(table->cellAt(1, 1).firstPosition(), 6);
QCOMPARE(table->cellAt(1, 2).firstPosition(), 7);
QCOMPARE(table->cellAt(1, 3).firstPosition(), 8);
QCOMPARE(table->cellAt(2, 0).firstPosition(), 9);
QCOMPARE(table->cellAt(2, 0).rowSpan(), 2);
QCOMPARE(table->cellAt(2, 0).columnSpan(), 2);
QCOMPARE(table->cellAt(2, 1).firstPosition(), 9);
QCOMPARE(table->cellAt(2, 2).firstPosition(), 10);
QCOMPARE(table->cellAt(2, 3).firstPosition(), 11);
QCOMPARE(table->cellAt(3, 0).firstPosition(), 9);
QCOMPARE(table->cellAt(3, 1).firstPosition(), 9);
QCOMPARE(table->cellAt(3, 2).firstPosition(), 12);
QCOMPARE(table->cellAt(3, 3).firstPosition(), 13);
table->removeColumns(1, 1);
QCOMPARE(table->rows(), 4);
QCOMPARE(table->columns(), 3);
QCOMPARE(table->cellAt(0, 0).firstPosition(), 1);
QCOMPARE(table->cellAt(0, 1).firstPosition(), 2);
QCOMPARE(table->cellAt(0, 2).firstPosition(), 3);
QCOMPARE(table->cellAt(1, 0).firstPosition(), 4);
QCOMPARE(table->cellAt(1, 1).firstPosition(), 5);
QCOMPARE(table->cellAt(1, 2).firstPosition(), 6);
QCOMPARE(table->cellAt(2, 0).firstPosition(), 7);
QCOMPARE(table->cellAt(2, 0).rowSpan(), 2);
QCOMPARE(table->cellAt(2, 0).columnSpan(), 1);
QCOMPARE(table->cellAt(2, 1).firstPosition(), 8);
QCOMPARE(table->cellAt(2, 2).firstPosition(), 9);
QCOMPARE(table->cellAt(3, 0).firstPosition(), 7);
QCOMPARE(table->cellAt(3, 1).firstPosition(), 10);
QCOMPARE(table->cellAt(3, 2).firstPosition(), 11);
}
void tst_QTextTable::removeColumnsInTableWithMergedRows()
{
QTextTable *table = cursor.insertTable(3, 4);
table->mergeCells(0, 0, 1, 4);
QCOMPARE(table->rows(), 3);
QCOMPARE(table->columns(), 4);
table->removeColumns(0, table->columns() - 1);
QCOMPARE(table->rows(), 3);
QCOMPARE(table->columns(), 1);
}
#ifndef QT_NO_WIDGETS
void tst_QTextTable::QTBUG11282_insertBeforeMergedEnding_data()
{
QTest::addColumn<int>("rows");
QTest::addColumn<int>("columns");
QTest::addColumn<QList<int> >("merge");
QTest::addColumn<QList<int> >("insert");
QTest::newRow("2x3, merge two, insert one") << 2 << 3 << (QList<int>() << 1 << 2 << 2)
<< (QList<int>() << 1 << 1) ;
QTest::newRow("3x4, merge three, insert one") << 3 << 4 << (QList<int>() << 1 << 3 << 3)
<< (QList<int>() << 1 << 1) ;
QTest::newRow("4x3, merge two, insert two") << 4 << 3 << (QList<int>() << 1 << 4 << 2)
<< (QList<int>() << 1 << 2) ;
QTest::newRow("4x4, merge middle two, insert one") << 4 << 4 << (QList<int>() << 1 << 4 << 2)
<< (QList<int>() << 1 << 1) ;
}
void tst_QTextTable::QTBUG11282_insertBeforeMergedEnding()
{
QFETCH(int, rows);
QFETCH(int, columns);
QFETCH(QList<int>, merge);
QFETCH(QList<int>, insert);
QTextTable *table = cursor.insertTable(rows, columns);
QTextEdit *textEdit = new QTextEdit;
textEdit->setDocument(doc);
textEdit->show();
QVERIFY(QTest::qWaitForWindowExposed(textEdit));
table->mergeCells(0,merge.at(0), merge.at(1), merge.at(2));
//Don't crash !
table->insertColumns(insert.at(0), insert.at(1));
//Check that the final size is what we expected
QCOMPARE(table->rows(), rows);
QCOMPARE(table->columns(), columns + insert.at(1));
delete textEdit;
}
#endif
void tst_QTextTable::QTBUG22011_insertBeforeRowSpan()
{
QTextDocument doc;
QTextCursor cursor(&doc);
QTextTable *table = cursor.insertTable(1,1); // 1x1
table->appendColumns(1); // 1x2
table->appendRows(1); // 2x2
table->mergeCells(0, 0, 2, 1); // 2x2
table->insertColumns(1, 1); // 2x3
table->mergeCells(0, 1, 1, 2); // 2x3
table->appendRows(1); // 3x3
table->mergeCells(0, 0, 3, 1); // 3x3
table->appendRows(1); // 4x3
table->insertColumns(1, 1); // 4x4
table->mergeCells(0, 1, 1, 3);
table->mergeCells(1, 1, 1, 2);
table->mergeCells(2, 1, 1, 2);
table->mergeCells(3, 0, 1, 2);
table->insertColumns(3, 1); // 4x5
table->mergeCells(0, 1, 1, 4);
table->appendColumns(1); // 4x6
QCOMPARE(table->rows(), 4);
QCOMPARE(table->columns(), 6);
}
#if !defined(QT_NO_PRINTER) && defined(QT_BUILD_INTERNAL)
namespace {
class QTBUG31330_PaintDevice : public QPagedPaintDevice
{
public:
class PaintEngine : public QPaintEngine
{
public:
QList<QRectF> rects;
PaintEngine()
: QPaintEngine(QPaintEngine::PaintEngineFeatures{ })
{}
virtual Type type() const
{
return User;
}
virtual bool begin(QPaintDevice *)
{
return true;
}
virtual bool end()
{
return true;
}
virtual void updateState(const QPaintEngineState &)
{}
virtual void drawRects(const QRect *, int)
{}
virtual void drawRects(const QRectF *r, int)
{
if (painter()->brush() == QBrush(Qt::green))
{
rects.append(*r);
}
}
virtual void drawPixmap(const QRectF &, const QPixmap &, const QRectF &)
{}
};
class QDummyPagedPaintDevicePrivate : public QPagedPaintDevicePrivate
{
bool setPageLayout(const QPageLayout &newPageLayout) override
{
m_pageLayout = newPageLayout;
return m_pageLayout.isEquivalentTo(newPageLayout);
}
bool setPageSize(const QPageSize &pageSize) override
{
m_pageLayout.setPageSize(pageSize);
return m_pageLayout.pageSize().isEquivalentTo(pageSize);
}
bool setPageOrientation(QPageLayout::Orientation orientation) override
{
m_pageLayout.setOrientation(orientation);
return m_pageLayout.orientation() == orientation;
}
bool setPageMargins(const QMarginsF &margins, QPageLayout::Unit units) override
{
m_pageLayout.setUnits(units);
m_pageLayout.setMargins(margins);
return m_pageLayout.margins() == margins && m_pageLayout.units() == units;
}
QPageLayout pageLayout() const override
{
return m_pageLayout;
}
QPageLayout m_pageLayout;
};
int pages;
QPaintEngine* engine;
QTBUG31330_PaintDevice(QPaintEngine* engine)
: QPagedPaintDevice(new QDummyPagedPaintDevicePrivate), pages(1), engine(engine)
{
QPageLayout layout = pageLayout();
layout.setUnits(QPageLayout::Point);
setPageLayout(layout);
}
virtual int metric(PaintDeviceMetric metric) const
{
if (PdmDevicePixelRatio == metric)
return 1;
if (PdmDevicePixelRatioScaled == metric)
return 1 * QPaintDevice::devicePixelRatioFScale();
if (PdmDpiY == metric)
return 96;
if (PdmDpiX == metric)
return 96;
if (PdmHeight == metric)
return 1000;
if (PdmWidth == metric)
return 700;
return 900;
}
virtual QPaintEngine *paintEngine() const
{
return engine;
}
bool newPage()
{
++pages;
return true;
}
};
}
void tst_QTextTable::QTBUG31330_renderBackground()
{
QTextDocument doc;
QTextCursor cursor(&doc);
QTextTable* table = cursor.insertTable(4, 2);
QTextTableCell cell = table->cellAt(3, 0);
QTextCharFormat cellFormat = cell.format();
cellFormat.setBackground(QBrush(Qt::green));
cell.setFormat(cellFormat);
QTextCursor tc = cell.firstCursorPosition();
for (int i = 0; i < 60; ++i) {
tc.insertBlock();
}
QTBUG31330_PaintDevice::PaintEngine engine;
QTBUG31330_PaintDevice paintDevice(&engine);
paintDevice.setPageSize(QPagedPaintDevice::A4);
doc.print(&paintDevice);
QVERIFY(paintDevice.pages >= 2);
QCOMPARE(engine.rects.count(), paintDevice.pages);
for (int i = 0; i < engine.rects.count(); ++i) {
QRectF rect = engine.rects[i];
QVERIFY(rect.top() > 0);
QVERIFY(rect.bottom() < 1000);
}
}
#endif
void tst_QTextTable::checkBorderAttributes_data()
{
QTest::addColumn<QString>("html");
QTest::addColumn<qreal>("topBorderWidth");
QTest::addColumn<qreal>("bottomBorderWidth");
QTest::addColumn<qreal>("leftBorderWidth");
QTest::addColumn<qreal>("rightBorderWidth");
QTest::addColumn<QTextFrameFormat::BorderStyle>("topBorderStyle");
QTest::addColumn<QTextFrameFormat::BorderStyle>("bottomBorderStyle");
QTest::addColumn<QTextFrameFormat::BorderStyle>("leftBorderStyle");
QTest::addColumn<QTextFrameFormat::BorderStyle>("rightBorderStyle");
QTest::addColumn<QBrush>("topBorderBrush");
QTest::addColumn<QBrush>("bottomBorderBrush");
QTest::addColumn<QBrush>("leftBorderBrush");
QTest::addColumn<QBrush>("rightBorderBrush");
const QString tableHtmlStart = QStringLiteral("<html><head><style>");
const QString tableHtmlEnd = QStringLiteral("</style></head><body>"
"<table border=\"1\"><tr><td>One</td><td>Two</td></tr>"
"<tr><td>Three</td><td>Four</td></tr></table></body></html>");
QTest::newRow("1px-solid-colors")
<< QString("%1"
"td {"
"border-top: 1px solid red;"
"border-bottom: 1px solid blue;"
"border-left: 1px solid green;"
"border-right: 1px solid yellow;"
"}"
"%2").arg(tableHtmlStart).arg(tableHtmlEnd)
<< 1.0 << 1.0 << 1.0 << 1.0
<< QTextFrameFormat::BorderStyle_Solid << QTextFrameFormat::BorderStyle_Solid
<< QTextFrameFormat::BorderStyle_Solid << QTextFrameFormat::BorderStyle_Solid
<< QBrush(Qt::red) << QBrush(Qt::blue) << QBrush(Qt::darkGreen) << QBrush(Qt::yellow);
QTest::newRow("MixedWidth-solid-colors")
<< QString("%1"
"td {"
"border-top: 1px solid red;"
"border-bottom: 2px solid blue;"
"border-left: 3px solid green;"
"border-right: 4px solid yellow;"
"}"
"%2").arg(tableHtmlStart).arg(tableHtmlEnd)
<< 1.0 << 2.0 << 3.0 << 4.0
<< QTextFrameFormat::BorderStyle_Solid << QTextFrameFormat::BorderStyle_Solid
<< QTextFrameFormat::BorderStyle_Solid << QTextFrameFormat::BorderStyle_Solid
<< QBrush(Qt::red) << QBrush(Qt::blue) << QBrush(Qt::darkGreen) << QBrush(Qt::yellow);
QTest::newRow("MixedWidth-MixedStyle-colors")
<< QString("%1"
"td {"
"border-top: 1px solid red;"
"border-bottom: 2px dotted blue;"
"border-left: 3px dashed green;"
"border-right: 4px inset yellow;"
"}"
"%2").arg(tableHtmlStart).arg(tableHtmlEnd)
<< 1.0 << 2.0 << 3.0 << 4.0
<< QTextFrameFormat::BorderStyle_Solid << QTextFrameFormat::BorderStyle_Dotted
<< QTextFrameFormat::BorderStyle_Dashed << QTextFrameFormat::BorderStyle_Inset
<< QBrush(Qt::red) << QBrush(Qt::blue) << QBrush(Qt::darkGreen) << QBrush(Qt::yellow);
}
void tst_QTextTable::checkBorderAttributes()
{
QFETCH(QString, html);
QFETCH(qreal, topBorderWidth);
QFETCH(qreal, bottomBorderWidth);
QFETCH(qreal, leftBorderWidth);
QFETCH(qreal, rightBorderWidth);
QFETCH(QTextFrameFormat::BorderStyle, topBorderStyle);
QFETCH(QTextFrameFormat::BorderStyle, bottomBorderStyle);
QFETCH(QTextFrameFormat::BorderStyle, leftBorderStyle);
QFETCH(QTextFrameFormat::BorderStyle, rightBorderStyle);
QFETCH(QBrush, topBorderBrush);
QFETCH(QBrush, bottomBorderBrush);
QFETCH(QBrush, leftBorderBrush);
QFETCH(QBrush, rightBorderBrush);
QTextDocument doc;
doc.setHtml(html);
QTextCursor cursor(doc.firstBlock());
cursor.movePosition(QTextCursor::Right);
QTextTable *currentTable = cursor.currentTable();
QVERIFY(currentTable);
for (int row = 0; row < 2; row++) {
for (int column = 0; column < 2; column++) {
QTextTableCell cell = currentTable->cellAt(row, column);
QTextCharFormat cellFormat = cell.format();
QCOMPARE(cellFormat.doubleProperty(QTextFormat::TableCellTopBorder), topBorderWidth);
QCOMPARE(cellFormat.doubleProperty(QTextFormat::TableCellBottomBorder), bottomBorderWidth);
QCOMPARE(cellFormat.doubleProperty(QTextFormat::TableCellLeftBorder), leftBorderWidth);
QCOMPARE(cellFormat.doubleProperty(QTextFormat::TableCellRightBorder), rightBorderWidth);
QCOMPARE(cellFormat.property(QTextFormat::TableCellTopBorderStyle), topBorderStyle);
QCOMPARE(cellFormat.property(QTextFormat::TableCellBottomBorderStyle), bottomBorderStyle);
QCOMPARE(cellFormat.property(QTextFormat::TableCellLeftBorderStyle), leftBorderStyle);
QCOMPARE(cellFormat.property(QTextFormat::TableCellRightBorderStyle), rightBorderStyle);
QCOMPARE(cellFormat.brushProperty(QTextFormat::TableCellTopBorderBrush), topBorderBrush);
QCOMPARE(cellFormat.brushProperty(QTextFormat::TableCellBottomBorderBrush), bottomBorderBrush);
QCOMPARE(cellFormat.brushProperty(QTextFormat::TableCellLeftBorderBrush), leftBorderBrush);
QCOMPARE(cellFormat.brushProperty(QTextFormat::TableCellRightBorderBrush), rightBorderBrush);
}
}
}
#ifndef QT_NO_WIDGETS
void tst_QTextTable::columnWidthWithSpans()
{
cleanup();
init();
QTextTable *table = cursor.insertTable(4, 4);
QTextEdit textEdit;
textEdit.setDocument(doc);
textEdit.show();
QVERIFY(QTest::qWaitForWindowExposed(&textEdit));
for (int i = 0; i < table->columns(); ++i)
table->cellAt(0, i).firstCursorPosition().insertText(QString("Header %1").arg(i));
QTextBlock block = table->cellAt(0, 0).firstCursorPosition().block();
const QRectF beforeRect = table->document()->documentLayout()->blockBoundingRect(block);
table->mergeCells(1, 0, 1, table->columns());
block = table->cellAt(0, 0).firstCursorPosition().block();
const QRectF afterRect = table->document()->documentLayout()->blockBoundingRect(block);
QCOMPARE(afterRect, beforeRect);
}
void tst_QTextTable::columnWidthWithImage_data()
{
const auto imageHtml = [](int width, int height) {
QImage image(width, height, QImage::Format_RGB32);
image.fill(Qt::red);
QByteArray imageBytes;
QBuffer buffer(&imageBytes);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "png");
return QString("<td><img src='data:image/png;base64,%1'/></td>")
.arg(QString::fromLatin1(imageBytes.toBase64()));
};
QTest::addColumn<QString>("leftHtml");
QTest::addColumn<QString>("rightHtml");
QTest::addColumn<QSize>("imageSize");
QTest::addRow("image")
<< imageHtml(500, 32) << "<td></td>" << QSize(500, 32);
QTest::addRow("image, text")
<< imageHtml(32, 32) << "<td>abc</td>" << QSize(32, 32);
QTest::addRow("image, 100%% text")
<< imageHtml(32, 32) << "<td style='background-color: grey' width='100%'>abc</td>"
<< QSize(32, 32);
QTest::addRow("image, image")
<< imageHtml(256, 32) << imageHtml(256, 32) << QSize(256, 32);
}
void tst_QTextTable::columnWidthWithImage()
{
const QString tableTemplate = "<table><tr>%1 %2</tr></table>";
QFETCH(QString, leftHtml);
QFETCH(QString, rightHtml);
QFETCH(QSize, imageSize);
QTextDocument doc;
doc.setHtml(tableTemplate.arg(leftHtml).arg(rightHtml));
QTextEdit textEdit;
textEdit.setDocument(&doc);
textEdit.show();
QVERIFY(QTest::qWaitForWindowExposed(&textEdit));
QTextCursor cursor(doc.firstBlock());
cursor.movePosition(QTextCursor::Right);
QTextTable *currentTable = cursor.currentTable();
QVERIFY(currentTable);
QTextBlock block = currentTable->cellAt(0, 0).firstCursorPosition().block();
const QRectF leftRect = currentTable->document()->documentLayout()->blockBoundingRect(block);
block = currentTable->cellAt(0, 1).firstCursorPosition().block();
const QRectF rightRect = currentTable->document()->documentLayout()->blockBoundingRect(block);
QCOMPARE(leftRect.size().toSize(), imageSize);
QVERIFY(rightRect.left() > leftRect.right());
}
#endif
QTEST_MAIN(tst_QTextTable)
#include "tst_qtexttable.moc"
|