Example #1
0
void TestStyles::testApplyParagraphStyleWithParent()
{
    KoParagraphStyle style1;
    style1.setStyleId(1002);
    KoParagraphStyle style2;
    style2.setStyleId(1003);
    KoParagraphStyle style3;
    style3.setStyleId(1004);

    style3.setParentStyle(&style2);
    style2.setParentStyle(&style1);

    style1.setAlignment(Qt::AlignRight);
    QCOMPARE(style1.alignment(), Qt::AlignRight);
    QCOMPARE(style2.alignment(), Qt::AlignRight);
    QCOMPARE(style3.alignment(), Qt::AlignRight);

    style2.setAlignment(Qt::AlignCenter);
    QCOMPARE(style1.alignment(), Qt::AlignRight);
    QCOMPARE(style2.alignment(), Qt::AlignCenter);
    QCOMPARE(style3.alignment(), Qt::AlignCenter);

    style3.setAlignment(Qt::AlignLeft | Qt::AlignAbsolute);
    QCOMPARE(style1.alignment(), Qt::AlignRight);
    QCOMPARE(style2.alignment(), Qt::AlignCenter);
    QCOMPARE(style3.alignment(), Qt::AlignLeft | Qt::AlignAbsolute);

    style3.setLineSpacing(23.45);
    style3.setLineHeightPercent(150);
    style3.setLineHeightAbsolute(8.0);
    QCOMPARE(style3.lineHeightPercent(), 0.0);
    QCOMPARE(style3.lineHeightAbsolute(), 8.0);
    QCOMPARE(style3.lineSpacing(), 23.45);
    QVERIFY(!style3.hasNormalLineHeight());

    style3.setNormalLineHeight();
    QCOMPARE(style3.lineHeightPercent(), 0.0);
    QCOMPARE(style3.lineHeightAbsolute(), 0.0);
    QCOMPARE(style3.lineSpacing(), 0.0);
    QVERIFY(style3.hasNormalLineHeight());

    style3.setLineHeightPercent(150);
    style3.setLineSpacing(56.78);
    QCOMPARE(style3.lineHeightPercent(), 150.0);
    QCOMPARE(style3.lineHeightAbsolute(), 0.0);
    QCOMPARE(style3.lineSpacing(), 56.78);
    QVERIFY(!style3.hasNormalLineHeight());

    QTextLength length0(QTextLength::FixedLength, 0.0);
    QTextLength length1(QTextLength::FixedLength, 10.0);
    QTextLength length2(QTextLength::FixedLength, 20.0);

    style1.setLeftMargin(length1);
    QCOMPARE(style1.leftMargin(), 10.0);
    QCOMPARE(style2.leftMargin(), 10.0);
    QCOMPARE(style3.leftMargin(), 10.0);
    style2.setRightMargin(length2);
    QCOMPARE(style1.rightMargin(), 0.0);
    QCOMPARE(style2.rightMargin(), 20.0);
    QCOMPARE(style3.rightMargin(), 20.0);

    // now actually apply it.
    QTextBlockFormat rawFormat;
    style1.applyStyle(rawFormat);
    KoParagraphStyle format(rawFormat, rawFormat.toCharFormat());
    QCOMPARE(rawFormat.properties().count(), 4);
    QCOMPARE(format.alignment(), Qt::AlignRight);
    QCOMPARE(rawFormat.property(KoParagraphStyle::StyleId).toInt(), 1002);
    //since we have not specified any NextStyle it should be the same as the current style
    QCOMPARE(rawFormat.property(KoParagraphStyle::StyleId).toInt(), rawFormat.property(KoParagraphStyle::NextStyle).toInt());
    QCOMPARE(format.leftMargin(), 10.0);
    QCOMPARE(format.rightMargin(), 0.0);

    style2.applyStyle(rawFormat);
    KoParagraphStyle format2(rawFormat, rawFormat.toCharFormat());
    QCOMPARE(rawFormat.properties().count(), 5);
    QCOMPARE(format2.alignment(), Qt::AlignCenter);
    QCOMPARE(rawFormat.property(KoParagraphStyle::StyleId).toInt(), 1003);
    //since we have not specified any NextStyle it should be the same as the current style
    QCOMPARE(rawFormat.property(KoParagraphStyle::StyleId).toInt(), rawFormat.property(KoParagraphStyle::NextStyle).toInt());
    QCOMPARE(format2.leftMargin(), 10.0);
    QCOMPARE(format2.rightMargin(), 20.0);

    style3.applyStyle(rawFormat);
    KoParagraphStyle format3(rawFormat, rawFormat.toCharFormat());
    QCOMPARE(rawFormat.properties().count(), 9);
    QCOMPARE(rawFormat.property(KoParagraphStyle::LineSpacing).toReal(), 56.78);
    QCOMPARE(format3.alignment(), Qt::AlignLeft | Qt::AlignAbsolute);
    QCOMPARE(rawFormat.property(KoParagraphStyle::StyleId).toInt(), 1004);
    //since we have not specified any NextStyle it should be the same as the current style
    QCOMPARE(rawFormat.property(KoParagraphStyle::StyleId).toInt(), rawFormat.property(KoParagraphStyle::NextStyle).toInt());
    QCOMPARE(format3.leftMargin(), 10.0);
    QCOMPARE(format3.rightMargin(), 20.0);
}