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
|
/*
* This file is part of Calligra tests
*
* Copyright (C) 2006-2010 Thomas Zander <zander@kde.org>
* Copyright (C) 2011 C. Boemann <cbo@boemann.dk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "TestTableLayout.h"
#include "MockRootAreaProvider.h"
#include <KoParagraphStyle.h>
#include <KoListStyle.h>
#include <KoListLevelProperties.h>
#include <KoStyleManager.h>
#include <KoTextBlockData.h>
#include <KoTextBlockBorderData.h>
#include <KoTextDocument.h>
#include <KoInlineTextObjectManager.h>
#include <KoTableColumnAndRowStyleManager.h>
#include <KoTableColumnStyle.h>
#include <KoTableRowStyle.h>
#include <KoTableStyle.h>
#include <KoTextPage.h>
#include <KoTextDebug.h>
#include <FrameIterator.h>
#include <KoPointedAt.h>
#include <QTextCursor>
#include <QTextTable>
#include <QTest>
#include <TextLayoutDebug.h>
#define FRAME_SPACING 10.0
// NOTE: The first area seems to layout a line (or block) even when none has been added,
// which means it must be taken into considderation when using hitTest() to find a table.
// TODO: The offset needed are based on guesswork and experiments, so needs investigation.
#define TABLE_OFFSET 10.
#define BLOCK_HEIGHT 13.2
#define TABLE_OFFSET_FIRST_AREA BLOCK_HEIGHT + 6
void TestTableLayout::initTestCase()
{
m_doc = 0;
m_layout = 0;
m_loremIpsum = QString("Lorem ipsum dolor sit amet, XgXgectetuer adiXiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.");
}
void TestTableLayout::cleanupTestCase()
{
delete m_doc;
m_doc = 0;
}
QTextCursor TestTableLayout::setupTest()
{
delete m_doc;
m_doc = new QTextDocument;
Q_ASSERT(m_doc);
MockRootAreaProvider *provider = new MockRootAreaProvider();
Q_ASSERT(provider);
KoTextDocument(m_doc).setInlineTextObjectManager(new KoInlineTextObjectManager);
m_doc->setDefaultFont(QFont("Sans Serif", 12, QFont::Normal, false)); //do it manually since we do not load the appDefaultStyle
m_styleManager = new KoStyleManager(0);
KoTextDocument(m_doc).setStyleManager(m_styleManager);
m_layout = new KoTextDocumentLayout(m_doc, provider);
Q_ASSERT(m_layout);
m_doc->setDocumentLayout(m_layout);
m_block = m_doc->begin();
QTextCursor cursor(m_doc);
return cursor;
}
void TestTableLayout::setupTest(const QString &mergedText, const QString &topRightText, const QString &midRightText, const QString &bottomLeftText, const QString &bottomMidText, const QString &bottomRightText, KoTableStyle* tableStyle)
{
QTextCursor cursor = setupTest();
KoParagraphStyle style;
style.setStyleId(101); // needed to do manually since we don't use the stylemanager
style.applyStyle(m_block);
QTextTableFormat tableFormat;
if (tableStyle)
tableStyle->applyStyle(tableFormat);
m_table = cursor.insertTable(3,3,tableFormat);
m_table->mergeCells(0,0,2,2);
if (mergedText.length() > 0) {
m_table->cellAt(0,0).firstCursorPosition().insertText(mergedText);
QTextBlock b2 = m_table->cellAt(0,0).firstCursorPosition().block();
while (b2.isValid()) {
style.applyStyle(b2);
b2 = b2.next();
}
}
if (topRightText.length() > 0) {
m_table->cellAt(0,2).firstCursorPosition().insertText(topRightText);
QTextBlock b2 = m_table->cellAt(0,2).firstCursorPosition().block();
while (b2.isValid()) {
style.applyStyle(b2);
b2 = b2.next();
}
}
if (midRightText.length() > 0) {
m_table->cellAt(1,2).firstCursorPosition().insertText(midRightText);
QTextBlock b2 = m_table->cellAt(1,2).firstCursorPosition().block();
while (b2.isValid()) {
style.applyStyle(b2);
b2 = b2.next();
}
}
if (bottomLeftText.length() > 0) {
m_table->cellAt(2,0).firstCursorPosition().insertText(bottomLeftText);
QTextBlock b2 = m_table->cellAt(2,0).firstCursorPosition().block();
while (b2.isValid()) {
style.applyStyle(b2);
b2 = b2.next();
}
}
if (bottomMidText.length() > 0) {
m_table->cellAt(2,1).firstCursorPosition().insertText(bottomMidText);
QTextBlock b2 = m_table->cellAt(2,1).firstCursorPosition().block();
while (b2.isValid()) {
style.applyStyle(b2);
b2 = b2.next();
}
}
if (bottomRightText.length() > 0) {
m_table->cellAt(2,2).firstCursorPosition().insertText(bottomRightText);
QTextBlock b2 = m_table->cellAt(2,2).firstCursorPosition().block();
while (b2.isValid()) {
style.applyStyle(b2);
b2 = b2.next();
}
}
}
QTextBlock TestTableLayout::mergedCellBlock() const
{
return m_table->cellAt(0,0).firstCursorPosition().block();
}
QTextBlock TestTableLayout::topRightCellBlock() const
{
return m_table->cellAt(0,2).firstCursorPosition().block();
}
QTextBlock TestTableLayout::midRightCellBlock() const
{
return m_table->cellAt(1,2).firstCursorPosition().block();
}
QTextBlock TestTableLayout::bottomLeftCellBlock() const
{
return m_table->cellAt(2,0).firstCursorPosition().block();
}
QTextBlock TestTableLayout::bottomMidCellBlock() const
{
return m_table->cellAt(2,1).firstCursorPosition().block();
}
QTextBlock TestTableLayout::bottomRightCellBlock() const
{
return m_table->cellAt(2,2).firstCursorPosition().block();
}
void TestTableLayout::testSetupTest()
{
setupTest("m","02","12","20","21","22");
m_layout->layout();
QCOMPARE(mergedCellBlock().text(), QString("m"));
QCOMPARE(topRightCellBlock().text(), QString("02"));
QCOMPARE(midRightCellBlock().text(), QString("12"));
QCOMPARE(bottomLeftCellBlock().text(), QString("20"));
QCOMPARE(bottomMidCellBlock().text(), QString("21"));
QCOMPARE(bottomRightCellBlock().text(), QString("22"));
}
void TestTableLayout::testMergedCells()
{
QTextCursor cursor = setupTest();
m_table = cursor.insertTable(3, 5);
m_table->mergeCells(0,0,2,2);
m_table->mergeCells(0,2,3,1);
m_table->mergeCells(0,3,3,1);
m_table->mergeCells(0,4,3,1);
m_table->mergeCells(2,0,1,1);
m_table->mergeCells(2,1,1,1);
m_layout->layout();
MockRootAreaProvider *provider = dynamic_cast<MockRootAreaProvider*>(m_layout->provider());
QVERIFY(!provider->m_askedForMoreThenOneArea);
// check if table is layed out
for (int i = 0; i < provider->m_areas.count(); ++i) {
qInfo()<<"area:"<<i<<"referenceRect:"<<provider->area(i)->referenceRect();
}
QPointF point = provider->area(0)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET_FIRST_AREA);
KoPointedAt p = provider->area(0)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(m_table == p.table, "m_table not found in area 0");
}
void TestTableLayout::testColumnWidthUndefined()
{
setupTest("","","","","","");
m_layout->layout();
QCOMPARE(m_table->columns(), 3);
QCOMPARE(m_table->rows(), 3);
QVERIFY(qAbs(m_block.layout()->lineAt(0).width() - 200.0) < ROUNDING);
QTextLayout *lay = bottomLeftCellBlock().layout();
QVERIFY(lay);
QCOMPARE(lay->lineCount(), 1);
QVERIFY(qAbs(lay->lineAt(0).width() - 200.0/3) < ROUNDING);
lay = bottomMidCellBlock().layout();
QVERIFY(lay);
QCOMPARE(lay->lineCount(), 1);
QVERIFY(qAbs(lay->lineAt(0).width() - 200.0/3) < ROUNDING);
lay = bottomRightCellBlock().layout();
QVERIFY(lay);
QCOMPARE(lay->lineCount(), 1);
QVERIFY(qAbs(lay->lineAt(0).width() - 200.0/3) < ROUNDING);
}
void TestTableLayout::testColumnWidthFixed()
{
KoTableStyle *tableStyle = new KoTableStyle;
tableStyle->setWidth(QTextLength(QTextLength::FixedLength, 150.0));
setupTest("merged text", "top right text", "mid right text", "bottom left text", "bottom mid text", "bottom right text", tableStyle);
KoTableColumnAndRowStyleManager styleManager = KoTableColumnAndRowStyleManager::getManager(m_table);
KoTableColumnStyle column1style;
column1style.setColumnWidth(2.3);
styleManager.setColumnStyle(0, column1style);
KoTableColumnStyle column2style;
column2style.setColumnWidth(122.5);
styleManager.setColumnStyle(1, column2style);
KoTableColumnStyle column3style;
column3style.setColumnWidth(362.9);
styleManager.setColumnStyle(2, column3style);
m_layout->layout();
QVERIFY(qAbs(QTextCursor(m_table->parentFrame()).block().layout()->lineAt(0).width() - 200.0) < ROUNDING); // table should grow to 200
QVERIFY(qAbs(mergedCellBlock().layout()->lineAt(0).width() - 124.8) < ROUNDING);
QVERIFY(qAbs(topRightCellBlock().layout()->lineAt(0).width() - 362.9) < ROUNDING);
QVERIFY(qAbs(bottomLeftCellBlock().layout()->lineAt(0).width() - 2.3) < ROUNDING);
QVERIFY(qAbs(bottomMidCellBlock().layout()->lineAt(0).width() - 122.5) < ROUNDING);
QVERIFY(qAbs(bottomRightCellBlock().layout()->lineAt(0).width() - 362.9) < ROUNDING);
}
void TestTableLayout::testColumnWidthFixedZero()
{
setupTest("merged text", "top right text", "mid right text", "bottom left text", "bottom mid text", "bottom right text");
KoTableColumnAndRowStyleManager styleManager = KoTableColumnAndRowStyleManager::getManager(m_table);
KoTableColumnStyle column1style;
column1style.setColumnWidth(0.0);
styleManager.setColumnStyle(0, column1style);
KoTableColumnStyle column2style;
column2style.setColumnWidth(120.5);
styleManager.setColumnStyle(1, column2style);
KoTableColumnStyle column3style;
column3style.setColumnWidth(0.1);
styleManager.setColumnStyle(2, column3style);
m_layout->layout();
QVERIFY(qAbs(mergedCellBlock().layout()->lineAt(0).width() - 120.5) < ROUNDING);
QVERIFY(qAbs(topRightCellBlock().layout()->lineAt(0).width() - 0.1) < ROUNDING);
//FIXME QVERIFY(qAbs(bottomLeftCellBlock().layout()->lineAt(0).width() - 0.0) < ROUNDING);
QVERIFY(qAbs(bottomMidCellBlock().layout()->lineAt(0).width() - 120.5) < ROUNDING);
//FIXME QVERIFY(qAbs(bottomRightCellBlock().layout()->lineAt(0).width() - 0.0) < ROUNDING);
}
void TestTableLayout::testColumnWidthFixedShrink()
{
KoTableStyle *tableStyle = new KoTableStyle;
//tableStyle->setWidth(QTextLength(QTextLength::FixedLength, 200.0)); // no table-width defined
setupTest("merged text", "top right text", "mid right text", "bottom left text", "bottom mid text", "bottom right text", tableStyle);
KoTableColumnAndRowStyleManager styleManager = KoTableColumnAndRowStyleManager::getManager(m_table);
KoTableColumnStyle column1style;
column1style.setColumnWidth(2.3);
styleManager.setColumnStyle(0, column1style);
KoTableColumnStyle column2style;
column2style.setColumnWidth(122.5);
styleManager.setColumnStyle(1, column2style);
KoTableColumnStyle column3style;
column3style.setColumnWidth(362.9);
styleManager.setColumnStyle(2, column3style);
m_layout->layout();
QVERIFY(qAbs(QTextCursor(m_table->parentFrame()).block().layout()->lineAt(0).width() - 200.0) < ROUNDING); // table should grow to 200
QVERIFY(qAbs(mergedCellBlock().layout()->lineAt(0).width() - 26.5938) < ROUNDING);
QVERIFY(qAbs(topRightCellBlock().layout()->lineAt(0).width() - 267) < ROUNDING);
QVERIFY(qAbs(bottomMidCellBlock().layout()->lineAt(0).width() - 26.5938) < ROUNDING);
QVERIFY(qAbs(bottomRightCellBlock().layout()->lineAt(0).width() - 267) < ROUNDING);
}
void TestTableLayout::testColumnWidthRelative()
{
setupTest("merged text", "top right text", "mid right text", "bottom left text", "bottom mid text", "bottom right text");
KoTableColumnAndRowStyleManager styleManager = KoTableColumnAndRowStyleManager::getManager(m_table);
KoTableColumnStyle column1style;
column1style.setRelativeColumnWidth(0.2);
styleManager.setColumnStyle(0, column1style);
KoTableColumnStyle column2style;
column2style.setRelativeColumnWidth(0.5);
styleManager.setColumnStyle(1, column2style);
KoTableColumnStyle column3style;
column3style.setRelativeColumnWidth(0.1);
styleManager.setColumnStyle(2, column3style);
m_layout->layout();
QVERIFY(qAbs(mergedCellBlock().layout()->lineAt(0).width() - 200.0*0.2 - 200.0*0.5) < ROUNDING);
QVERIFY(qAbs(topRightCellBlock().layout()->lineAt(0).width() - 200.0*0.1) < ROUNDING);
QVERIFY(qAbs(bottomLeftCellBlock().layout()->lineAt(0).width() - 200.0*0.2) < ROUNDING);
QVERIFY(qAbs(bottomMidCellBlock().layout()->lineAt(0).width() - 200.0*0.5) < ROUNDING);
QVERIFY(qAbs(bottomRightCellBlock().layout()->lineAt(0).width() - 200.0*0.1) < ROUNDING);
}
void TestTableLayout::testRowHeightFixed()
{
KoTableStyle *tableStyle = new KoTableStyle;
setupTest("merged text", "top right text", "mid right text", "bottom left text", "bottom mid text", "bottom right text", tableStyle);
KoTableColumnAndRowStyleManager styleManager = KoTableColumnAndRowStyleManager::getManager(m_table);
KoTableRowStyle row1style;
row1style.setRowHeight(3.2);
styleManager.setRowStyle(1, row1style);
m_layout->layout();
QVERIFY(!dynamic_cast<MockRootAreaProvider*>(m_layout->provider())->m_askedForMoreThenOneArea);
//QVERIFY(qAbs(mergedCellBlock().layout()->lineAt(0).height() - 14) < ROUNDING);
}
void TestTableLayout::testRowHeightMinimum()
{
KoTableStyle *tableStyle = new KoTableStyle;
setupTest("merged text", "top right text", "mid right text", "bottom left text", "bottom mid text", "bottom right text", tableStyle);
KoTableColumnAndRowStyleManager styleManager = KoTableColumnAndRowStyleManager::getManager(m_table);
KoTableRowStyle row1style;
row1style.setMinimumRowHeight(3.2);
styleManager.setRowStyle(1, row1style);
m_layout->layout();
QVERIFY(!dynamic_cast<MockRootAreaProvider*>(m_layout->provider())->m_askedForMoreThenOneArea);
//QVERIFY(qAbs(mergedCellBlock().layout()->lineAt(0).height() - 14) < ROUNDING);
}
QTextTable *TestTableLayout::addTable(QTextCursor cursor, int rows, int columns, KoTableStyle* tableStyle)
{
KoParagraphStyle style;
style.setStyleId(101); // needed to do manually since we don't use the stylemanager
style.applyStyle(m_block);
QTextTableFormat tableFormat;
if (tableStyle) {
tableStyle->applyStyle(tableFormat);
}
QTextTable *table = cursor.insertTable(rows, columns, tableFormat);
for (int r = 0; r < table->rows(); ++r) {
for (int c = 0; c < table->columns(); ++c) {
QString s = QString("Cell %1, %2").arg(r, c);
table->cellAt(r,c).firstCursorPosition().insertText(s);
QTextBlock b2 = table->cellAt(r,c).firstCursorPosition().block();
while (b2.isValid()) {
style.applyStyle(b2);
b2 = b2.next();
}
}
}
return table;
}
void TestTableLayout::testOneTableNeedsTwoRootAreas()
{
QTextCursor c = setupTest();
QTextTable *table1 = addTable(c, 6, 3);
MockRootAreaProvider *provider = dynamic_cast<MockRootAreaProvider*>(m_layout->provider());
QVERIFY(provider);
provider->setSuggestedRect(QRect(100, 100, 200, 90));
provider->maxPosition = 10; // guard against loop
m_layout->layout();
for (int i = 0; i < provider->m_areas.count(); ++i) {
qInfo()<<"area:"<<i<<"referenceRect:"<<provider->area(i)->referenceRect();
QPointF point = provider->area(i)->referenceRect().topLeft() + QPointF(0., i == 0 ? TABLE_OFFSET_FIRST_AREA : TABLE_OFFSET);
KoPointedAt p = provider->area(i)->hitTest(point, Qt::FuzzyHit);
if (table1 == p.table) qInfo()<<"table1 is in area"<<i;
else qInfo()<<"table1 is not in area"<<i;
}
QCOMPARE(provider->m_areas.count(), 2);
QPointF point = provider->area(0)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET_FIRST_AREA);
KoPointedAt p = provider->area(0)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table1 == p.table, "table1 not found in area 0");
// table2 starts in first area and continues into second area
point = provider->area(1)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET);
p = provider->area(1)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table1 == p.table, "table1 not found in area 1");
}
void TestTableLayout::testTwoTablesNeedsTwoRootAreas()
{
QTextCursor c = setupTest();
QTextTable *table1 = addTable(c, 2, 3);
QTextTable *table2 = addTable(c, 3, 3);
MockRootAreaProvider *provider = dynamic_cast<MockRootAreaProvider*>(m_layout->provider());
QVERIFY(provider);
provider->setSuggestedRect(QRect(100, 100, 200, 90));
m_layout->layout();
for (int i = 0; i < provider->m_areas.count(); ++i) {
qInfo()<<"area:"<<i<<"referenceRect:"<<provider->area(i)->referenceRect();
QPointF point = provider->area(i)->referenceRect().topLeft() + QPointF(0., i == 0 ? TABLE_OFFSET_FIRST_AREA : TABLE_OFFSET);
KoPointedAt p = provider->area(i)->hitTest(point, Qt::FuzzyHit);
if (table1 == p.table) qInfo()<<"table1 is in area"<<i;
else qInfo()<<"table1 is not in area"<<i;
if (table2 == p.table) qInfo()<<"table2 is in area"<<i;
else qInfo()<<"table2 is not in area"<<i;
}
QCOMPARE(provider->m_areas.count(), 2);
QPointF point = provider->area(0)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET_FIRST_AREA);
KoPointedAt p = provider->area(0)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table1 == p.table, "table1 not found in area 0");
// table2 starts in first area and continues into second area
point = provider->area(1)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET);
p = provider->area(1)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table2 == p.table, "table2 not found in area 1");
}
void TestTableLayout::testMergedRowsSpansAreas()
{
QTextCursor c = setupTest();
QTextTable *table1 = addTable(c, 6, 3);
MockRootAreaProvider *provider = dynamic_cast<MockRootAreaProvider*>(m_layout->provider());
QVERIFY(provider);
provider->setSuggestedRect(QRect(100, 100, 200, 90));
provider->maxPosition = 10; // guard against loop
table1->mergeCells(0,0,2,1);
table1->mergeCells(0,1,2,1);
table1->mergeCells(0,2,2,1);
table1->mergeCells(2,0,2,1);
table1->mergeCells(2,1,2,1);
table1->mergeCells(2,2,2,1);
table1->mergeCells(4,0,2,1);
table1->mergeCells(4,1,2,1);
table1->mergeCells(4,2,2,1);
m_layout->layout();
for (int i = 0; i < provider->m_areas.count(); ++i) {
qInfo()<<"area:"<<i<<"referenceRect:"<<provider->area(i)->referenceRect();
}
// should be room in two areas (it is if cells are not merged)
QCOMPARE(provider->m_areas.count(), 2);
QPointF point = provider->area(0)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET_FIRST_AREA);
KoPointedAt p = provider->area(0)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table1 == p.table, "table1 not found in area 0");
point = provider->area(1)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET);
p = provider->area(1)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table1 == p.table, "table1 not found in area 1");
}
void TestTableLayout::testMergedRowsSpansAreas2()
{
QTextCursor c = setupTest();
QTextTable *table1 = addTable(c, 10, 3);
MockRootAreaProvider *provider = dynamic_cast<MockRootAreaProvider*>(m_layout->provider());
QVERIFY(provider);
provider->setSuggestedRect(QRect(100, 100, 200, 110));
provider->maxPosition = 10; // guard against loop
table1->mergeCells(0,0,2,1);
table1->mergeCells(0,1,2,1);
table1->mergeCells(0,2,2,1);
table1->mergeCells(2,0,2,1);
table1->mergeCells(2,1,2,1);
table1->mergeCells(2,2,2,1);
table1->mergeCells(4,0,2,1);
table1->mergeCells(4,1,2,1);
table1->mergeCells(4,2,2,1);
table1->mergeCells(6,0,2,1);
table1->mergeCells(6,1,2,1);
table1->mergeCells(6,2,2,1);
table1->mergeCells(8,0,2,1);
table1->mergeCells(8,1,2,1);
table1->mergeCells(8,2,2,1);
m_layout->layout();
for (int i = 0; i < provider->m_areas.count(); ++i) {
qInfo()<<"area:"<<i<<"referenceRect:"<<provider->area(i)->referenceRect();
}
QVERIFY2(provider->m_areas.count() <= 5, "Table should fit in max 5 areas, possibly in 3");
// check if table is layed out at all
QPointF point = provider->area(0)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET_FIRST_AREA);
KoPointedAt p = provider->area(0)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table1 == p.table, "table1 not found in area 0");
}
void TestTableLayout::testTwoTablesMergedRowsSpansAreas()
{
QTextCursor c = setupTest();
QTextTable *table1 = addTable(c, 4, 3);
QTextTable *table2 = addTable(c, 3, 3);
MockRootAreaProvider *provider = dynamic_cast<MockRootAreaProvider*>(m_layout->provider());
QVERIFY(provider);
provider->setSuggestedRect(QRect(100, 100, 200, 90));
provider->maxPosition = 10; // guard against loop
table2->mergeCells(0,0,2,1);
table2->mergeCells(0,1,2,1);
table2->mergeCells(0,2,2,1);
m_layout->layout();
for (int i = 0; i < provider->m_areas.count(); ++i) {
qInfo()<<"area:"<<i<<"referenceRect:"<<provider->area(i)->referenceRect();
}
QCOMPARE(provider->m_areas.count(), 2);
QPointF point = provider->area(0)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET_FIRST_AREA);
KoPointedAt p = provider->area(0)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table1 == p.table, "table1 not found in area 0");
// intention: there should not be room left for another row in area 0, so table2 should start in next area
point = provider->area(0)->referenceRect().bottomLeft() + QPointF(0., -TABLE_OFFSET);
p = provider->area(0)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table2 != p.table, "table2 found in area 0, possibly due font size");
point = provider->area(1)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET);
p = provider->area(1)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table2 == p.table, "table2 not found in area 1");
}
void TestTableLayout::testTwoTablesMergedRowsSpansThreeAreas()
{
QTextCursor c = setupTest();
QTextTable *table1 = addTable(c, 4, 3);
QTextTable *table2 = addTable(c, 8, 3); // should need 2 areas
MockRootAreaProvider *provider = dynamic_cast<MockRootAreaProvider*>(m_layout->provider());
QVERIFY(provider);
provider->setSuggestedRect(QRect(100, 100, 200, 90));
provider->maxPosition = 10; // guard against loop
table2->mergeCells(0,0,2,1);
table2->mergeCells(0,1,2,1);
table2->mergeCells(0,2,2,1);
m_layout->layout();
for (int i = 0; i < provider->m_areas.count(); ++i) {
qInfo()<<"area:"<<i<<"referenceRect:"<<provider->area(i)->referenceRect();
}
QCOMPARE(provider->m_areas.count(), 3);
QPointF point = provider->area(0)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET_FIRST_AREA);
KoPointedAt p = provider->area(0)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table1 == p.table, "table1 not found in area 0");
// intention: there should not be room left for another row in area 0, so table2 should start in next area
point = provider->area(0)->referenceRect().bottomLeft() + QPointF(0., -TABLE_OFFSET);
p = provider->area(0)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table2 != p.table, "table2 found in area 0, possibly due font size");
point = provider->area(1)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET);
p = provider->area(1)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table2 == p.table, "table2 not found in area 1");
point = provider->area(2)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET);
p = provider->area(2)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table2 == p.table, "table2 not found in area 2");
}
void TestTableLayout::testRowHeightLargerThanAreaHeight()
{
QTextCursor c = setupTest();
QTextTable *table1 = addTable(c, 1, 3);
MockRootAreaProvider *provider = dynamic_cast<MockRootAreaProvider*>(m_layout->provider());
QVERIFY(provider);
provider->setSuggestedRect(QRect(100, 100, 200, 22));
provider->maxPosition = 10; // guard against loop
m_layout->layout();
for (int i = 0; i < provider->m_areas.count(); ++i) {
qInfo()<<"area:"<<i<<"referenceRect:"<<provider->area(i)->referenceRect();
QPointF point = provider->area(i)->referenceRect().topLeft() + QPointF(0., i == 0 ? TABLE_OFFSET_FIRST_AREA : TABLE_OFFSET);
KoPointedAt p = provider->area(i)->hitTest(point, Qt::FuzzyHit);
if (table1 == p.table) qInfo()<<"table1 is in area"<<i;
else qInfo()<<"table1 is not in area"<<i;
}
// Atm. a table row larger than the area will result in the creation of three areas,
// and the table is not placed in any of them.
QEXPECT_FAIL("", "TODO: Table shall be placed in area 0 & 1", Continue);
QVERIFY2(provider->m_areas.count() == 1, "Table should have been placed in area 0");
QPointF point = provider->area(0)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET_FIRST_AREA);
KoPointedAt p = provider->area(0)->hitTest(point, Qt::FuzzyHit);
QEXPECT_FAIL("", "TODO: Table shall be placed in area 0 & 1", Continue);
QVERIFY2(table1 == p.table, "table1 not found in area 0");
}
void TestTableLayout::testSingleHeaderRow()
{
QTextCursor c = setupTest();
QTextTable *table1 = addTable(c, 1, 3);
QTextTableFormat f;
f.setHeaderRowCount(1);
table1->setFormat(f);
MockRootAreaProvider *provider = dynamic_cast<MockRootAreaProvider*>(m_layout->provider());
QVERIFY(provider);
provider->setSuggestedRect(QRect(100, 100, 200, 100));
provider->maxPosition = 10; // guard against loop
m_layout->layout();
for (int i = 0; i < provider->m_areas.count(); ++i) {
qInfo()<<"area:"<<i<<"referenceRect:"<<provider->area(i)->referenceRect();
QPointF point = provider->area(i)->referenceRect().topLeft() + QPointF(0., i == 0 ? TABLE_OFFSET_FIRST_AREA : TABLE_OFFSET);
KoPointedAt p = provider->area(i)->hitTest(point, Qt::FuzzyHit);
if (table1 == p.table) qInfo()<<"table1 is in area"<<i;
else qInfo()<<"table1 is not in area"<<i;
}
QVERIFY2(provider->m_areas.count() == 1, "Table should have been placed in area 0");
QPointF point = provider->area(0)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET_FIRST_AREA);
KoPointedAt p = provider->area(0)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table1 == p.table, "table1 not found in area 0");
}
void TestTableLayout::testMultipleHeaderRows()
{
QTextCursor c = setupTest();
QTextTable *table1 = addTable(c, 2, 3);
QTextTableFormat f;
f.setHeaderRowCount(2);
table1->setFormat(f);
MockRootAreaProvider *provider = dynamic_cast<MockRootAreaProvider*>(m_layout->provider());
QVERIFY(provider);
provider->setSuggestedRect(QRect(100, 100, 200, 100));
provider->maxPosition = 10; // guard against loop
m_layout->layout();
for (int i = 0; i < provider->m_areas.count(); ++i) {
qInfo()<<"area:"<<i<<"referenceRect:"<<provider->area(i)->referenceRect();
QPointF point = provider->area(i)->referenceRect().topLeft() + QPointF(0., i == 0 ? TABLE_OFFSET_FIRST_AREA : TABLE_OFFSET);
KoPointedAt p = provider->area(i)->hitTest(point, Qt::FuzzyHit);
if (table1 == p.table) qInfo()<<"table1 is in area"<<i;
else qInfo()<<"table1 is not in area"<<i;
}
QVERIFY2(provider->m_areas.count() == 1, "Table should have been placed in area 0");
QPointF point = provider->area(0)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET_FIRST_AREA);
KoPointedAt p = provider->area(0)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table1 == p.table, "table1 not found in area 0");
}
void TestTableLayout::testTableWithSingleHeaderRow()
{
QTextCursor c = setupTest();
QTextTable *table1 = addTable(c, 2, 3);
QTextTableFormat f;
f.setHeaderRowCount(1);
table1->setFormat(f);
MockRootAreaProvider *provider = dynamic_cast<MockRootAreaProvider*>(m_layout->provider());
QVERIFY(provider);
provider->setSuggestedRect(QRect(100, 100, 200, 100));
provider->maxPosition = 10; // guard against loop
m_layout->layout();
for (int i = 0; i < provider->m_areas.count(); ++i) {
qInfo()<<"area:"<<i<<"referenceRect:"<<provider->area(i)->referenceRect();
QPointF point = provider->area(i)->referenceRect().topLeft() + QPointF(0., (i == 0 ? TABLE_OFFSET_FIRST_AREA : TABLE_OFFSET));
KoPointedAt p = provider->area(i)->hitTest(point, Qt::FuzzyHit);
if (table1 == p.table) qInfo()<<"table1 is in area"<<i;
else qInfo()<<"table1 is not in area"<<i;
}
QVERIFY2(provider->m_areas.count() == 1, "Table should have been placed in area 0");
QPointF point = provider->area(0)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET_FIRST_AREA);
KoPointedAt p = provider->area(0)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table1 == p.table, "table1 not found in area 0");
}
void TestTableLayout::testTableWithMultipleHeaderRow()
{
QTextCursor c = setupTest();
QTextTable *table1 = addTable(c, 3, 3);
QTextTableFormat f;
f.setHeaderRowCount(2);
table1->setFormat(f);
MockRootAreaProvider *provider = dynamic_cast<MockRootAreaProvider*>(m_layout->provider());
QVERIFY(provider);
provider->setSuggestedRect(QRect(100, 100, 200, 100));
provider->maxPosition = 10; // guard against loop
m_layout->layout();
for (int i = 0; i < provider->m_areas.count(); ++i) {
qInfo()<<"area:"<<i<<"referenceRect:"<<provider->area(i)->referenceRect();
QPointF point = provider->area(i)->referenceRect().topLeft() + QPointF(0., (i == 0 ? TABLE_OFFSET_FIRST_AREA : TABLE_OFFSET));
KoPointedAt p = provider->area(i)->hitTest(point, Qt::FuzzyHit);
if (table1 == p.table) qInfo()<<"table1 is in area"<<i;
else qInfo()<<"table1 is not in area"<<i;
}
QVERIFY2(provider->m_areas.count() == 1, "Table should have been placed in area 0");
QPointF point = provider->area(0)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET_FIRST_AREA);
KoPointedAt p = provider->area(0)->hitTest(point, Qt::FuzzyHit);
QVERIFY2(table1 == p.table, "table1 not found in area 0");
}
void TestTableLayout::testHeaderRowHeightLargerThanAreaHeight()
{
QTextCursor c = setupTest();
QTextTable *table1 = addTable(c, 1, 3);
QTextTableFormat f;
f.setHeaderRowCount(1);
table1->setFormat(f);
MockRootAreaProvider *provider = dynamic_cast<MockRootAreaProvider*>(m_layout->provider());
QVERIFY(provider);
provider->setSuggestedRect(QRect(100, 100, 200, 20));
provider->maxPosition = 10; // guard against loop
m_layout->layout();
for (int i = 0; i < provider->m_areas.count(); ++i) {
qInfo()<<"area:"<<i<<"referenceRect:"<<provider->area(i)->referenceRect();
QPointF point = provider->area(i)->referenceRect().topLeft() + QPointF(0., (i == 0 ? TABLE_OFFSET_FIRST_AREA : TABLE_OFFSET));
KoPointedAt p = provider->area(i)->hitTest(point, Qt::FuzzyHit);
if (table1 == p.table) qInfo()<<"table1 is in area"<<i;
else qInfo()<<"table1 is not in area"<<i;
}
// Atm. a table row larger than the area will result in the creation of three areas,
// and the table is not placed in any of them.
QEXPECT_FAIL("", "TODO: Table shall be placed in area 0 & 1", Continue);
QVERIFY2(provider->m_areas.count() == 2, "Table should have been placed in area 0 & 1");
QPointF point = provider->area(0)->referenceRect().topLeft() + QPointF(0., TABLE_OFFSET_FIRST_AREA);
KoPointedAt p = provider->area(0)->hitTest(point, Qt::FuzzyHit);
QEXPECT_FAIL("", "TODO: Table shall be placed in area 0 & 1", Continue);
QVERIFY2(table1 == p.table, "table1 not found in area 0");
}
QTEST_MAIN(TestTableLayout)
|