void TestDocumentLayout::placeAnchoredFrame() { initForNewTest(QString()); MockShape *picture = new MockShape(); picture->setSize(QSizeF(100, 100)); KTextAnchor *anchor = new KTextAnchor(picture); anchor->setOffset(QPointF(23, 45)); QTextCursor cursor(doc); KInlineTextObjectManager *manager = new KInlineTextObjectManager(); layout->setInlineTextObjectManager(manager); MockLayoutState *state = new MockLayoutState(doc); layout->setLayout(state); state->shape = shape1; QCOMPARE(doc->begin().text().length(), 0); manager->insertInlineObject(cursor, anchor); QCOMPARE(doc->begin().text().length(), 1); QCOMPARE(cursor.position(), 1); shape1->setPosition(QPointF(300, 300)); layout->layout(); QCOMPARE(picture->parent(), shape1); QCOMPARE(picture->position(), QPointF(23, 59.4)); cursor.setPosition(0); cursor.insertText("foo"); // moves my anchors slightly to the right/down and gives line height layout->layout(); QCOMPARE(picture->parent(), shape1); QPointF newPos = picture->position(); QVERIFY(newPos.x() > 23); QVERIFY(newPos.y() > 45); // it adds the baseline now cursor.movePosition(QTextCursor::End); cursor.insertText("\nNew Line\nAnd another"); layout->layout(); QCOMPARE(picture->position(), newPos); QTextLayout *firstLineLayout = doc->begin().layout(); QTextOption option = firstLineLayout->textOption(); option.setAlignment(Qt::AlignHCenter); firstLineLayout->setTextOption(option); layout->layout(); QTextLine first = doc->begin().layout()->lineAt(0); QVERIFY(first.isValid()); QVERIFY(first.naturalTextRect().x() > 10); newPos.setX(newPos.x() + first.naturalTextRect().x()); // text is moved due to alignment QCOMPARE(picture->position(), newPos); anchor->setOffset(QPointF()); anchor->setAlignment(KTextAnchor::Left); anchor->setAlignment(KTextAnchor::TopOfParagraph); layout->layout(); // image is 100 wide, now centered in a parent of 200 so X = 50 QCOMPARE(picture->position(), QPointF(50, 0)); }
void TestShapeContainer::testSetParent() { MockContainerModel *model1 = new MockContainerModel(); MockContainer container1(model1); MockContainerModel *model2 = new MockContainerModel(); MockContainer container2(model2); MockShape shape; // init test QCOMPARE(model1->shapes().count(), 0); QCOMPARE(model2->shapes().count(), 0); shape.setParent(&container1); QCOMPARE(model1->shapes().count(), 1); QCOMPARE(model2->shapes().count(), 0); QCOMPARE(shape.parent(), &container1); shape.setParent(&container2); QCOMPARE(model1->shapes().count(), 0); QCOMPARE(container1.shapes().count(), 0); QCOMPARE(model2->shapes().count(), 1); QCOMPARE(container2.shapes().count(), 1); QCOMPARE(shape.parent(), &container2); }
void TestDocumentLayout::placeAnchoredFrame2() { QFETCH(int, horizontalAlignment); QFETCH(int, verticalAlignment); QFETCH(QPointF, startPosition); QFETCH(QPointF, imagePosition); initForNewTest(QString(loremIpsum)); MockShape *picture = new MockShape(); picture->setSize(QSizeF(198, 400)); KTextAnchor *anchor = new KTextAnchor(picture); anchor->setAlignment(KTextAnchor::AnchorHorizontal(horizontalAlignment)); anchor->setAlignment(KTextAnchor::AnchorVertical(verticalAlignment)); picture->setPosition(startPosition); QTextCursor cursor(doc); KInlineTextObjectManager *manager = new KInlineTextObjectManager(); layout->setInlineTextObjectManager(manager); MockLayoutState *state = new MockLayoutState(doc); layout->setLayout(state); state->shape = shape1; manager->insertInlineObject(cursor, anchor); QCOMPARE(cursor.position(), 1); layout->layout(); QCOMPARE(picture->parent(), shape1); QCOMPARE(picture->position(), imagePosition); // test if rest of text is below picture. QTextLayout *lay = doc->begin().layout(); QVERIFY(lay->lineCount() >= 1); QTextLine line = lay->lineForTextPosition(1); // the first char of real text. QVERIFY(line.isValid()); // qDebug() << line.y() << line.height(); QVERIFY(line.y() + line.height() >= 412); // test that text is below image }