Exemple #1
0
void TestPageTextInfo::testPageNumber()
{
    KWPageManager manager;
    QCOMPARE(manager.pageCount(), 0);

    KWPage page1 = manager.appendPage();
    KWPage page2 = manager.appendPage();
    KWPage page3 = manager.appendPage();
    KWPage page4 = manager.appendPage();
    KWPage page5 = manager.appendPage();

    KWPageStyle style2("foo");
    manager.addPageStyle(style2);
    KWPage page40 = manager.appendPage(style2);
    QCOMPARE(page40.pageNumber(), 6);
    page40.setPageNumber(40);
    QCOMPARE(page5.pageNumber(), 5);
    QCOMPARE(page40.pageNumber(), 40);
    KWPage page41 = manager.appendPage();
    QCOMPARE(page41.pageNumber(), 41);

    KWPageTextInfo info1(page1);
    QCOMPARE(info1.pageNumber(KoTextPage::CurrentPage, 0), 1);
    QCOMPARE(info1.pageNumber(KoTextPage::PreviousPage, 0), -1);
    QCOMPARE(info1.pageNumber(KoTextPage::NextPage, 0), 2);

    QCOMPARE(info1.pageNumber(KoTextPage::CurrentPage, 4), 5);
    QCOMPARE(info1.pageNumber(KoTextPage::CurrentPage, 5), -1);
    QCOMPARE(info1.pageNumber(KoTextPage::CurrentPage, 40), 41);
    QCOMPARE(info1.pageNumber(KoTextPage::PreviousPage, 1), -1);
    QCOMPARE(info1.pageNumber(KoTextPage::NextPage, 3), 5);
    QCOMPARE(info1.pageNumber(KoTextPage::NextPage, 4), -1);

    KWPageTextInfo info2(page2);
    QCOMPARE(info2.pageNumber(KoTextPage::CurrentPage, 0), 2);
    QCOMPARE(info2.pageNumber(KoTextPage::PreviousPage, 0), 1);
    QCOMPARE(info2.pageNumber(KoTextPage::NextPage, 0), 3);

    KWPageTextInfo info3(page5);
    QCOMPARE(info3.pageNumber(KoTextPage::CurrentPage, 0), 5);
    QCOMPARE(info3.pageNumber(KoTextPage::PreviousPage, 0), 4);
    QCOMPARE(info3.pageNumber(KoTextPage::NextPage, 0), 40);

    KWPageTextInfo info40(page40);
    QCOMPARE(info40.pageNumber(KoTextPage::CurrentPage, 0), 40);
    QCOMPARE(info40.pageNumber(KoTextPage::PreviousPage, 0), 5);
    QCOMPARE(info40.pageNumber(KoTextPage::NextPage, 0), 41);

    KWPageTextInfo info41(page41);
    QCOMPARE(info41.pageNumber(KoTextPage::CurrentPage, 0), 41);
    QCOMPARE(info41.pageNumber(KoTextPage::PreviousPage, 0), 40);
    QCOMPARE(info41.pageNumber(KoTextPage::NextPage, 0), -1);
}
void TestViewMode::testPageSetupChanged()
{
    KWPageManager pageManager;

    //set up as a page spread;
    KoPageLayout pageLayout = pageManager.defaultPageStyle().pageLayout();
    pageLayout.leftMargin = -1;
    pageLayout.rightMargin = -1;
    pageLayout.pageEdge = 20;
    pageLayout.bindingSide = 25;
    pageManager.defaultPageStyle().setPageLayout(pageLayout);

    KoZoomHandler zoomer;
    zoomer.setZoom(1.0);
    zoomer.setDpi(72, 72); // BORING :)

    MyViewModeNormal viewMode;
    viewMode.setPageManager(&pageManager);
    viewMode.setViewConverter(&zoomer);

    pageManager.appendPage(); // page 1
    pageManager.appendPage(); // page 2 & 3
    QCOMPARE(pageManager.pageCount(), 3);

    viewMode.pageSetupChanged();
    QCOMPARE(viewMode.pageTops().count(), 3);
    QCOMPARE(viewMode.pageSpreadMode(), true);

    KWPage lastPage = pageManager.appendPage(); // page 4 & 5
    viewMode.pageSetupChanged();
    QCOMPARE(viewMode.pageTops().count(), 5);
    QCOMPARE(viewMode.pageSpreadMode(), true);

    pageManager.removePage(lastPage);
    viewMode.pageSetupChanged();
    QCOMPARE(viewMode.pageTops().count(), 3);
    QCOMPARE(viewMode.pageSpreadMode(), true);
}
void TestPageCommands::testMakePageSpread()
{
    KWDocument document;
    KWPageManager *manager = document.pageManager();

    KWPageStyle style("pagestyle1");
    const KoPageLayout oldLayout = style.pageLayout();
    KoPageLayout layout = style.pageLayout();

    manager->addPageStyle(style);
    KWPage page1 = manager->appendPage(style);
    QCOMPARE(page1.width(), layout.width);
    QCOMPARE(page1.pageNumber(), 1);
    QCOMPARE(page1.pageSide(), KWPage::Right);
    QCOMPARE(manager->pageCount(), 1);

    // make it a pagespread
    KWPageStyle pageSpread = style;
    pageSpread.detach("dummy");
    layout.leftMargin = -1;
    layout.rightMargin = -1;
    layout.pageEdge = 7;
    layout.bindingSide = 13;
    pageSpread.setPageLayout(layout);

    KWPageStylePropertiesCommand cmd1(&document, style, pageSpread);
    cmd1.redo();
    QCOMPARE(page1.pageNumber(), 1);
    QCOMPARE(page1.pageSide(), KWPage::Right);
    QCOMPARE(page1.width(), style.pageLayout().width);
    QCOMPARE(manager->pageCount(), 1);
    KoPageLayout newLayout = style.pageLayout();
    QCOMPARE(newLayout.width, layout.width);
    QCOMPARE(newLayout.leftMargin, layout.leftMargin);
    QCOMPARE(newLayout.rightMargin, layout.rightMargin);
    QCOMPARE(newLayout.pageEdge, layout.pageEdge);
    QCOMPARE(newLayout.bindingSide, layout.bindingSide);

    cmd1.undo();
    QCOMPARE(page1.width(), oldLayout.width);
    QCOMPARE(page1.pageNumber(), 1);
    QCOMPARE(page1.pageSide(), KWPage::Right);
    QCOMPARE(manager->pageCount(), 1);

    // create another page. So we have 2 single sided pages. (Right/Left)
    KWPage page2 = manager->appendPage(style);
    QCOMPARE(page2.width(), oldLayout.width);
    QCOMPARE(page2.pageNumber(), 2);
    QCOMPARE(page2.pageSide(), KWPage::Left);
    QCOMPARE(manager->pageCount(), 2);

    // avoid reusing cmd1 as that assumes the constructor doesn't do anything. Which is
    // not a restriction we put on the command. (i.e. that doesn't *have* to work)
    KWPageStylePropertiesCommand cmd2(&document, style, pageSpread);
    cmd2.redo();

    QCOMPARE(page1.width(), style.pageLayout().width);
    QCOMPARE(page1.pageNumber(), 1);
    QCOMPARE(page1.pageSide(), KWPage::Right);
    QCOMPARE(page2.pageNumber(), 2);
QEXPECT_FAIL("", "Not done yet", Abort);
    QCOMPARE(page2.pageSide(), KWPage::PageSpread);
    QCOMPARE(page2.width(), style.pageLayout().width * 2);
    QCOMPARE(manager->pageCount(), 3);

    cmd2.undo();
    // test for page side etc.
    QCOMPARE(page1.width(), oldLayout.width);
    QCOMPARE(page1.pageNumber(), 1);
    QCOMPARE(page1.pageSide(), KWPage::Right);
    QCOMPARE(page2.pageNumber(), 2);
    QCOMPARE(page2.pageSide(), KWPage::Left);
    QCOMPARE(page2.width(), oldLayout.width);
    QCOMPARE(manager->pageCount(), 2);
}
void TestPageCommands::testPageSpread()
{
    // setup a doc with multiple pages like;
    //   1: S1, 2: S1, 3: S1, 4: S2, 5: S1, 6:S2, 7: S2

    KWDocument document;
    KWPageManager *manager = document.pageManager();

    KWPageStyle style("pagestyle1");
    KoPageLayout lay = style.pageLayout();
    lay.width = 300;
    lay.height = 500;
    style.setPageLayout(lay);

    KWPageStyle style2("pagestyle2");
    lay = style.pageLayout();
    lay.width = 400;
    lay.height = 400;
    style2.setPageLayout(lay);
    KWPage p1 = manager->appendPage(style);
    KWPage p2 = manager->appendPage(style);
    KWPage p3 = manager->appendPage(style);
    KWPage p4 = manager->appendPage(style2);
    KWPage p5 = manager->appendPage(style);
    KWPage p6 = manager->appendPage(style2);
    KWPage p7 = manager->appendPage(style2);

    KWFrameSet *fs = new KWFrameSet();
    for (int i = 1; i <= manager->pageCount(); ++i) {
        // create one frame per page. Positioned relative to the top of the page.
        KWPage page = manager->page(i);
        QVERIFY(page.isValid());
        MockShape *shape = new MockShape();
        new KWFrame(shape, fs);
        shape->setPosition(QPointF(-10, page.offsetInDocument() + 10));
    }
    document.addFrameSet(fs);

    // when changing S1 from normal to pagespread I expect
    //  page 1 to stay the same.
    //  page 2 to become a pagespread
    //  page 3 to be deleted
    //  page 4 to stay the same.
    //  page 5 to become a page spread and gets pageNumber 6
    //  page 6 to become page 7 and become a Right page
    //  page 7 to become page 8 and become a Left page
    //  there will be a new KWPage for page 5
    // all frames to be moved so they are still at the same relative location
    // to their original page' top

    KWPageStyle style3("dummy");
    lay.leftMargin = -1;
    lay.rightMargin = -1;
    lay.pageEdge = 7;
    lay.bindingSide = 13;
    style2.setPageLayout(lay);
    KWPageStylePropertiesCommand cmd(&document, style, style3);
    cmd.redo();

    QCOMPARE(manager->pageCount(), 7);
QEXPECT_FAIL("", "Not done yet", Abort);
    QCOMPARE(p1.height(), 500.);
    QCOMPARE(p1.width(), 300.);
    QCOMPARE(p1.pageSide(), KWPage::Right);
    QCOMPARE(p1.pageNumber(), 1);
    QCOMPARE(p2.height(), 500.);
    QCOMPARE(p2.width(), 600.);
    QCOMPARE(p2.pageSide(), KWPage::PageSpread);
    QCOMPARE(p2.pageNumber(), 2);
    QVERIFY(!p3.isValid());
    QCOMPARE(p4.pageSide(), KWPage::Left);
    QCOMPARE(p4.width(), 300.);
    QCOMPARE(p4.height(), 500.);
    QCOMPARE(p4.pageNumber(), 4);
    QCOMPARE(p5.width(), 600.);
    QCOMPARE(p5.height(), 500.);
    QCOMPARE(p5.pageSide(), KWPage::PageSpread);
    QCOMPARE(p5.pageNumber(), 6);
    QCOMPARE(p6.pageSide(), KWPage::Right);
    QCOMPARE(p6.width(), 400.);
    QCOMPARE(p6.pageNumber(), 7);
    QCOMPARE(p7.pageSide(), KWPage::Left);
    QCOMPARE(p7.width(), 400.);
    QCOMPARE(p7.pageNumber(), 8);

    KWPage newPage5 = manager->page(5);
    QCOMPARE(newPage5.pageStyle(), style);
    QCOMPARE(newPage5.width(), 400.);
    QCOMPARE(newPage5.height(), 400.);
    QCOMPARE(newPage5.pageSide(), KWPage::Left);

    QCOMPARE(fs->frames()[0]->shape()->position(), QPointF(-10, 10));
    // TODO figure out what the actual numbers should be below
    QCOMPARE(fs->frames()[1]->shape()->position(), QPointF(-10, 10));
    QCOMPARE(fs->frames()[2]->shape()->position(), QPointF(-10, 10));
    QCOMPARE(fs->frames()[3]->shape()->position(), QPointF(-10, 10));
    QCOMPARE(fs->frames()[4]->shape()->position(), QPointF(-10, 10));
    QCOMPARE(fs->frames()[5]->shape()->position(), QPointF(-10, 10));
    QCOMPARE(fs->frames()[6]->shape()->position(), QPointF(-10, 10));
}
void TestPageCommands::testPageStylePropertiesCommand2()
{
    /*
      make setup where multiple, interspaced pages use the style we want to change.
      add some frames.
      check if the frames moved properly when the page gets a new size
      check that all pages got their new size
      check that the name of the style did not change
    */
    KWDocument document;
    KWPageManager *manager = document.pageManager();

    KWPageStyle style("pagestyle1");
    KoPageLayout lay = style.pageLayout();
    lay.width = 300;
    lay.height = 500;
    style.setPageLayout(lay);

    KWPageStyle style2("pagestyle2");
    lay = style.pageLayout();
    lay.width = 400;
    lay.height = 400;
    style2.setPageLayout(lay);
    KWPage p1 = manager->appendPage(style2);
    KWPage p2 = manager->appendPage(style);
    KWPage p3 = manager->appendPage(style);
    KWPage p4 = manager->appendPage(style2);
    KWPage p5 = manager->appendPage(style2);
    KWPage p6 = manager->appendPage(style);
    KWPage p7 = manager->appendPage(style);
    KWPage p8 = manager->appendPage(style);
    KWPage p9 = manager->appendPage(style2);
    QCOMPARE(manager->pageCount(), 9);

    KWFrameSet *fs = new KWFrameSet();
    for (int i = 1; i <= manager->pageCount(); ++i) {
        // create one frame per page. Positioned relative to the top of the page.
        KWPage page = manager->page(i);
        QVERIFY(page.isValid());
        MockShape *shape = new MockShape();
        new KWFrame(shape, fs);
        shape->setPosition(QPointF(-10, page.offsetInDocument() + 10));
    }
    document.addFrameSet(fs);

    // lets prepare to change the style to a new page size.
    KWPageStyle newStyle("foo");
    lay = newStyle.pageLayout();
    lay.width = 350;
    lay.height = 1000;
    newStyle.setPageLayout(lay);

    KWPageStylePropertiesCommand command(&document, style, newStyle);

    // nothing happens in constructor before redo;
    for (int i = 1; i <= manager->pageCount(); ++i) {
        KWPage page = manager->page(i);
        QVERIFY(page.height() <= 500); // not 1000 yet
        QVERIFY(page.isValid());
        KoShape *shape = fs->frames().at(i-1)->shape();
        QCOMPARE(shape->position(), QPointF(-10, page.offsetInDocument() + 10));
    }

    const qreal lastPageOffset = p9.offsetInDocument();

    command.redo();
    QCOMPARE(manager->pageCount(), 9);
    QCOMPARE(p1.height(), 400.);
    QCOMPARE(p4.height(), 400.);
    QCOMPARE(p5.height(), 400.);
    QCOMPARE(p9.height(), 400.);
    QCOMPARE(p2.height(), 1000.);
    QCOMPARE(p3.height(), 1000.);
    QCOMPARE(p6.height(), 1000.);
    QCOMPARE(p7.height(), 1000.);
    QCOMPARE(p8.height(), 1000.);
    QCOMPARE(lastPageOffset + 2500, p9.offsetInDocument());
    for (int i = 1; i <= manager->pageCount(); ++i) {
        KWPage page = manager->page(i);
        QVERIFY(page.isValid());
        KoShape *shape = fs->frames().at(i-1)->shape();
        QCOMPARE(shape->position(), QPointF(-10, page.offsetInDocument() + 10));
    }
}
void TestPageCommands::testPageStylePropertiesCommand() // basic properties change
{
    KWDocument document;
    KWPageManager *manager = document.pageManager();

    KWPageStyle style("pagestyle1");
    KoPageLayout oldLayout;
    oldLayout.format = KoPageFormat::IsoA4Size;
    oldLayout.width = 101;
    oldLayout.height = 102;
    oldLayout.leftMargin = -1;
    oldLayout.rightMargin = -1;
    oldLayout.pageEdge = 7;
    oldLayout.bindingSide = 13;
    style.setPageLayout(oldLayout);

    KoColumns oldColumns;
    oldColumns.count = 4;
    oldColumns.gapWidth = 21;
    style.setColumns(oldColumns);
    KWPage page1 = manager->appendPage(style);
    page1.setDirectionHint(KoText::LeftRightTopBottom);

    QCOMPARE(page1.pageNumber(), 1);
    QCOMPARE(page1.width(), 101.);
    QCOMPARE(page1.height(), 102.);
    QCOMPARE(page1.leftMargin(), 13.); // its a right-sided page
    QCOMPARE(page1.pageEdgeMargin(), 7.);
    QCOMPARE(page1.directionHint(), KoText::LeftRightTopBottom);
    QCOMPARE(page1.pageStyle().columns().count, 4);
    QCOMPARE(page1.pageSide(), KWPage::Right);

    // new ;)
    KWPageStyle style2("pagestyle2");
    KoPageLayout newLayout;
    newLayout.width = 401;
    newLayout.height = 405;
    newLayout.leftMargin = 11;
    newLayout.rightMargin = 18;
    newLayout.pageEdge = -1;
    newLayout.bindingSide = -1;
    style2.setPageLayout(newLayout);
    KoColumns newColumns;
    newColumns.count = 2;
    newColumns.columnData.append(ColumnDatum(1.0, 2.0, 1.0, 1.0, 25));
    newColumns.columnData.append(ColumnDatum(1.0, 1.0, 1.0, 1.0, 50));
    style2.setColumns(newColumns);
    style2.setDirection(KoText::RightLeftTopBottom);

    KWPageStylePropertiesCommand command1(&document, style, style2);

    // nothing changed before the redo
    QCOMPARE(page1.width(), 101.);
    QCOMPARE(page1.height(), 102.);
    QCOMPARE(page1.leftMargin(), 13.); // its a right-sided page
    QCOMPARE(page1.pageEdgeMargin(), 7.);
    QCOMPARE(page1.directionHint(), KoText::LeftRightTopBottom);

    // redo
    command1.redo();
    QCOMPARE(page1.pageStyle().name(), QString("pagestyle1")); // name didn't change
    QCOMPARE(page1.width(), 401.);
    QCOMPARE(page1.height(), 405.);
    QCOMPARE(page1.leftMargin(), 11.);
    QCOMPARE(page1.pageEdgeMargin(), -1.); // its a right-sided page
    QCOMPARE(page1.pageStyle().direction(), KoText::RightLeftTopBottom);
    QCOMPARE(page1.directionHint(), KoText::LeftRightTopBottom);
    page1.setDirectionHint(KoText::InheritDirection); // reset to what the style says
    QCOMPARE(page1.directionHint(), KoText::RightLeftTopBottom);
    QCOMPARE(style.pageLayout().width, 401.); // style changed
    QCOMPARE(page1.pageStyle().columns().count, 2);
    QCOMPARE(page1.pageNumber(), 1);
    QCOMPARE(page1.pageSide(), KWPage::Right);

    QCOMPARE(manager->pageCount(), 1);
    QVERIFY(manager->page(1).isValid());
    QVERIFY(!manager->page(2).isValid());

    KWPage page2 = manager->appendPage(style);
    QCOMPARE(manager->pageCount(), 2);
    QVERIFY(manager->page(1).isValid());
    QVERIFY(manager->page(2).isValid());
    QVERIFY(!manager->page(3).isValid());
    QCOMPARE(page2.pageNumber(), 2);
    QCOMPARE(page2.pageSide(), KWPage::Left);

    // undo
    command1.undo();
    QCOMPARE(page1.pageStyle().name(), QString("pagestyle1")); // name didn't change
    QCOMPARE(page1.width(), 101.);
    QCOMPARE(page1.height(), 102.);
    QCOMPARE(page1.leftMargin(), 13.);
    QCOMPARE(page1.directionHint(), KoText::AutoDirection);
    QCOMPARE(style.pageLayout().width, 101.);
    QCOMPARE(page1.pageStyle().columns().count, 4);
    QCOMPARE(page1.pageNumber(), 1);
    QCOMPARE(page1.pageSide(), KWPage::Right);
    QCOMPARE(manager->pageCount(), 2);
    QVERIFY(manager->page(1).isValid());
    QVERIFY(manager->page(2).isValid());
    QVERIFY(!manager->page(3).isValid());
    QCOMPARE(page2.pageNumber(), 2);
    QCOMPARE(page2.pageSide(), KWPage::Left);
    QCOMPARE(page2.width(), 101.); // same style
    QCOMPARE(page2.height(), 102.);
    QCOMPARE(page2.leftMargin(), 7.);
    QCOMPARE(page2.directionHint(), KoText::AutoDirection);
}
void TestViewMode::testClipRectForPageSpread()
{
    KWPageManager pageManager;

    //set up as a page spread;
    KoPageLayout pageLayout = pageManager.defaultPageStyle().pageLayout();
    pageLayout.leftMargin = -1;
    pageLayout.rightMargin = -1;
    pageLayout.pageEdge = 20;
    pageLayout.bindingSide = 25;
    pageLayout.height = 200;
    pageLayout.width = 300;
    pageManager.defaultPageStyle().setPageLayout(pageLayout);

    KoZoomHandler zoomer;
    zoomer.setZoom(1.0);
    zoomer.setDpi(72, 72); // BORING :)

    MyViewModeNormal viewMode;
    viewMode.setPageManager(&pageManager);
    viewMode.setViewConverter(&zoomer);

    pageManager.appendPage(); // page 1
    pageManager.appendPage(); // page 2 & 3
    QCOMPARE(pageManager.pageCount(), 3);
    viewMode.pageSetupChanged();

    QList<KWViewMode::ViewMap> answer;
    answer = viewMode.mapExposedRects(QRect(50, 180, 300, 100));
    QCOMPARE(answer.count(), 2);
    QCOMPARE(answer[0].clipRect, QRect(50, 180, 250, 20)); // page 1
    QCOMPARE(answer[0].distance, QPointF());
    QCOMPARE(answer[1].clipRect, QRect(50, 200, 300, 75)); // page 2 & 3
    QCOMPARE(answer[1].distance, QPointF(0, PAGEGAP));

    answer = viewMode.mapExposedRects(QRect(0, 250, 1000, 50));
    QCOMPARE(answer.count(), 1);
    QCOMPARE(answer[0].clipRect, QRect(0, 245, 600, 50)); // page 2&3
    QCOMPARE(answer[0].distance, QPointF(0, PAGEGAP));

    pageManager.appendPage(); // page 4 & 5
    QCOMPARE(pageManager.pageCount(), 5);
    viewMode.pageSetupChanged();

    answer = viewMode.mapExposedRects(QRect(0, 250, 1000, 50));
    QCOMPARE(answer.count(), 1);
    QCOMPARE(answer[0].clipRect, QRect(0, 245, 600, 50)); // page 2&3
    QCOMPARE(answer[0].distance, QPointF(0, PAGEGAP));

    answer = viewMode.mapExposedRects(QRect(0, 450, 1000, 50));
    QCOMPARE(answer.count(), 1);
    QCOMPARE(answer[0].clipRect, QRect(0, 440, 600, 50)); // page 4&5
    QCOMPARE(answer[0].distance, QPointF(0, PAGEGAP * 2));

    answer = viewMode.mapExposedRects(QRect(0, 250, 1000, 500));
    QCOMPARE(answer.count(), 2);
    QCOMPARE(answer[0].clipRect, QRect(0, 245, 600, 155)); // page 2&3
    QCOMPARE(answer[0].distance, QPointF(0, PAGEGAP));
    QCOMPARE(answer[1].clipRect, QRect(0, 400, 600, 200)); // page 4&5
    QCOMPARE(answer[1].distance, QPointF(0, PAGEGAP * 2));
}