diff --git a/autotests/ElementsTest.cpp b/autotests/ElementsTest.cpp index 27bda4f8..5dcc6770 100644 --- a/autotests/ElementsTest.cpp +++ b/autotests/ElementsTest.cpp @@ -1,231 +1,250 @@ /* This file is part of the KDE project * Copyright (C) 2015 Jarosław Staniek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "ElementsTest.h" #include "KReportTestUtils.h" #include #include #include #include +#include #include QTEST_MAIN(ElementsTest) Q_DECLARE_METATYPE(KReportElement) Q_DECLARE_METATYPE(KReportLabelElement) Q_DECLARE_METATYPE(KReportSection) void ElementsTest::testElements() { KReportElement e; QCOMPARE(e, e); QCOMPARE(e.name(), ""); QCOMPARE(e.rect(), QRectF()); QCOMPARE(e.z(), 0.0); QCOMPARE(e.backgroundColor(), QColor()); QCOMPARE(e.foregroundColor(), QColor()); QCOMPARE(e.backgroundOpacity(), 0.0); e.setZ(10.3); QCOMPARE(e.z(), 10.3); e.setName("element1"); QCOMPARE(e.name(), "element1"); e.setRect(QRect(1, 2, 3, 4)); QCOMPARE(e.rect(), QRectF(1, 2, 3, 4)); e.setBackgroundColor(Qt::red); QCOMPARE(e.backgroundColor(), QColor(Qt::red)); e.setForegroundColor(Qt::white); QCOMPARE(e.foregroundColor(), QColor(Qt::white)); QCOMPARE(e, e); KReportElement e2 = e; e.setName(""); QCOMPARE(e, e2); QCOMPARE(e.name(), e2.name()); QCOMPARE(e.rect(), e2.rect()); e2.setName("element2"); KReportLabelElement lbl1(e2); QCOMPARE(e2.name(), lbl1.name()); e2.setName(""); QCOMPARE(lbl1.name(), "element2"); lbl1.setText("label1"); QCOMPARE(lbl1.text(), "label1"); e = lbl1; KReportLabelElement lbl2(e); // e points to lbl1 so shares lbl1 in fact QCOMPARE(lbl1, lbl2); QCOMPARE(KReportLabelElement(e), lbl2); lbl2 = e; QCOMPARE(KReportLabelElement(e), lbl2); lbl1 = KReportLabelElement(); QCOMPARE(lbl1.alignment(), Qt::AlignLeft | Qt::AlignVCenter); // defaults lbl1.setAlignment(Qt::AlignHCenter|Qt::AlignBottom); QCOMPARE(lbl1.alignment(), Qt::AlignHCenter|Qt::AlignBottom); lbl1.setBackgroundOpacity(20.0); QCOMPARE(lbl1.backgroundOpacity(), 1.0); // 1.0 is max lbl1.setBackgroundOpacity(-100.10); QCOMPARE(lbl1.backgroundOpacity(), 0.0); // 0.0 is min lbl1.setFont(QApplication::font()); QFont f = lbl1.font(); QCOMPARE(f, QApplication::font()); QCOMPARE(lbl1.borderStyle().weight(), 0.0); // default QCOMPARE(lbl1.borderStyle().penStyle(), Qt::NoPen); // default QCOMPARE(lbl1.borderStyle().color(), QColor()); // default KReportLineStyle lineStyle; lineStyle.setWeight(2.0); lineStyle.setPenStyle(Qt::DashLine); lineStyle.setColor(QColor("brown")); lbl1.setBorderStyle(lineStyle); QCOMPARE(lbl1.borderStyle(), lineStyle); lineStyle.setColor(Qt::yellow); QCOMPARE(lbl1.borderStyle().color(), QColor(Qt::yellow)); // shared } void ElementsTest::testElementCloning() { KReportLabelElement lbl1("text1"), lbl2; lbl1.setName("label1"); lbl2 = lbl1.clone(); QVERIFY(lbl1 != lbl2); // a deep copy, not equal lbl1.setText(""); QCOMPARE(lbl1.name(), lbl2.name()); QCOMPARE(lbl1.text(), ""); QCOMPARE(lbl2.text(), "text1"); // this is another copy, not affected by lbl1.setText("") lbl1.setName("label1"); lbl1.setText("text1"); KReportElement e1 = lbl1; KReportElement e2 = e1.clone(); QVERIFY2(e1 != e2, "Cloned element not equal to the original"); QCOMPARE(e1.name(), "label1"); QCOMPARE(e2.name(), "label1"); QVERIFY2(KReportLabelElement(e2) != lbl1, "Reference to original not equal to a clone"); QVERIFY2(KReportLabelElement(e2) != KReportLabelElement(e1), "Reference to original not equal to reference to a clone"); lbl1.setName(""); lbl1.setText(""); QCOMPARE(e1.name(), ""); // shared with lbl1 QCOMPARE(e2.name(), "label1"); // shared with lbl1's clone QCOMPARE(KReportLabelElement(e1).text(), ""); QCOMPARE(KReportLabelElement(e2).text(), "text1"); KReportLabelElement lbl3 = e2; // e2 points to a lbl1's clone with name=label1, text=text1, so: KReportLabelElement(e2).setText("text2"); // This affect e2 and lbl2 because // KReportLabelElement(e2) upcasts it to the KReportLabelElement type QCOMPARE(KReportLabelElement(e2).text(), "text2"); QCOMPARE(lbl3.text(), "text2"); QCOMPARE(lbl1.text(), ""); // lbl1 unaffected } void ElementsTest::testSections() { // types KReportSection sec1; QCOMPARE(sec1.type(), KReportSection::Type::Invalid); QCOMPARE(KReportSection(sec1).type(), KReportSection::Type::Invalid); sec1.setType(KReportSection::Type::PageFooterFirst); KReportSection sec2(sec1); QCOMPARE(sec1.type(), sec2.type()); QCOMPARE(sec1, sec2); sec1.setType(KReportSection::Type::PageFooterEven); QCOMPARE(sec1.type(), sec2.type()); // shared QCOMPARE(sec1, sec2); // shared // height KReportSection::setDefaultHeight(-1); // sanity: reset QCOMPARE(sec1.height(), KReportSection::defaultHeight()); // initially: default height KReportSection::setDefaultHeight(CM_TO_POINT(10.0)); // sanity: reset QCOMPARE(KReportSection::defaultHeight(), CM_TO_POINT(10.0)); QCOMPARE(sec1.height(), KReportSection::defaultHeight()); // still default height KReportSection::setDefaultHeight(-1); // reset QCOMPARE(sec1.height(), KReportSection::defaultHeight()); // still default height sec1.setHeight(CM_TO_POINT(7.0)); QCOMPARE(sec1.height(), CM_TO_POINT(7.0)); // custom height // background color KReportSection::setDefaultBackgroundColor(QColor()); // sanity: reset QCOMPARE(sec1.backgroundColor(), KReportSection::defaultBackgroundColor()); // initially: default color KReportSection::setDefaultBackgroundColor(Qt::yellow); // sanity: reset QCOMPARE(KReportSection::defaultBackgroundColor(), QColor(Qt::yellow)); QCOMPARE(sec1.backgroundColor(), KReportSection::defaultBackgroundColor()); // still default color KReportSection::setDefaultBackgroundColor(QColor()); // reset QCOMPARE(sec1.backgroundColor(), KReportSection::defaultBackgroundColor()); // still default color sec1.setBackgroundColor(Qt::lightGray); QCOMPARE(sec1.backgroundColor(), QColor(Qt::lightGray)); // custom color // elements QVERIFY2(sec1.elements().isEmpty(), "Initially section has no elements inside"); QVERIFY2(sec2.elements().isEmpty(), "Initially section has no elements inside"); KReportLabelElement lbl1("text1"); + QVERIFY(lbl1.name().isEmpty()); QVERIFY(sec1.addElement(lbl1)); QCOMPARE(sec1.elements().count(), 1); QCOMPARE(sec2.elements().count(), 1); QCOMPARE(sec1.elements().first(), sec1.elements().last()); QCOMPARE(KReportLabelElement(sec1.elements().first()), lbl1); QCOMPARE(KReportLabelElement(sec1.elements().at(0)), lbl1); + QRegularExpression sectionAlreadyContainsElement("Section already contains element KReportElement: name=\"\".*"); + QTest::ignoreMessage(QtWarningMsg, sectionAlreadyContainsElement); QVERIFY2(!sec1.addElement(lbl1), "Adding the same element twice isn't possible"); QCOMPARE(sec1.elements().count(), 1); KReportLabelElement lbl2("text2"); + QTest::ignoreMessage(QtWarningMsg, + QRegularExpression("Could not find element KReportElement: name=\"\".* in section")); QVERIFY2(!sec1.removeElement(lbl2), "Removing not added element isn't possible"); QCOMPARE(sec1.elements().count(), 1); QVERIFY(sec1.removeElement(lbl1)); QCOMPARE(sec1.elements().count(), 0); QCOMPARE(sec2.elements().count(), 0); lbl2 = lbl1.clone(); QVERIFY(sec1.addElement(lbl1)); QVERIFY(sec1.addElement(lbl2)); QCOMPARE(sec1.elements().count(), 2); QCOMPARE(sec2.elements().count(), 2); // insert sec1 = KReportSection(); QVERIFY(sec1.insertElement(0, lbl1)); + QCOMPARE(sec1.elements().count(), 1); + QTest::ignoreMessage(QtWarningMsg, sectionAlreadyContainsElement); QVERIFY2(!sec1.insertElement(0, lbl1), "Cannot insert the same element twice"); + const QString couldNotInsertElementMessage("Could not insert element KReportElement: name=\"\".* at index %1 into section"); + QTest::ignoreMessage(QtWarningMsg, QRegularExpression(couldNotInsertElementMessage.arg(2))); QVERIFY2(!sec1.insertElement(2, lbl2), "Cannot insert element at position 2"); + QCOMPARE(sec1.elements().count(), 1); + QTest::ignoreMessage(QtWarningMsg, QRegularExpression(couldNotInsertElementMessage.arg(-1))); QVERIFY2(!sec1.insertElement(-1, lbl2), "Cannot insert element at position -1"); + QCOMPARE(sec1.elements().count(), 1); QVERIFY(sec1.insertElement(0, lbl2)); + QCOMPARE(sec1.elements().count(), 2); // indexOf QCOMPARE(sec1.elements().indexOf(lbl1), 1); QCOMPARE(sec1.elements().indexOf(lbl2), 0); QCOMPARE(KReportLabelElement(sec1.elements().last()), lbl1); // removeAt + const QString couldNotFindElementMessage("Could not find element at index %1 in section"); + QTest::ignoreMessage(QtWarningMsg, QRegularExpression(couldNotFindElementMessage.arg(2))); QVERIFY2(!sec1.removeElementAt(2), "Cannot remove element at position 2"); + QTest::ignoreMessage(QtWarningMsg, QRegularExpression(couldNotFindElementMessage.arg(-1))); QVERIFY2(!sec1.removeElementAt(-1), "Cannot remove element at position -1"); QVERIFY(sec1.removeElementAt(1)); + QCOMPARE(sec1.elements().count(), 1); QVERIFY(sec1.removeElementAt(0)); + QCOMPARE(sec1.elements().count(), 0); // cloning sections sec1 = KReportSection(); QVERIFY(sec1.addElement(lbl1)); QVERIFY(sec1.addElement(lbl2)); sec2 = sec1.clone(); QCOMPARE(sec1.elements().count(), sec2.elements().count()); QVERIFY2(sec1.elements() != sec2.elements(), "Cloned sections have different sets of elements"); QVERIFY2(sec1 != sec2, "Cloned section not equal to the original"); QVERIFY2(sec1.elements().first() != sec2.elements().first(), "Elements of cloned sections have different sets of elements"); } diff --git a/src/common/KReportSection.cpp b/src/common/KReportSection.cpp index 9020566f..34009b79 100644 --- a/src/common/KReportSection.cpp +++ b/src/common/KReportSection.cpp @@ -1,201 +1,201 @@ /* This file is part of the KDE project * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com) * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk) * Copyright (C) 2010-2015 Jarosław Staniek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "KReportSection.h" #include "KReportDesign_p.h" #include "KReportDocument.h" #include "KReportPluginInterface.h" #include "KReportPluginManager.h" #include "KReportItemLine.h" #include "kreport_debug.h" #if 0 // needed by lupdate to avoid "Qualifying with unknown namespace/class" class KReportSection { Q_DECLARE_TR_FUNCTIONS(KReportSection) }; #endif #if 0 KReportSection::KReportSection(const QDomElement & elemSource, KReportReportData* report) : QObject(report) { setObjectName(elemSource.tagName()); if (objectName() != QLatin1String("report:section")) { m_valid = false; return; } m_type = sectionTypeFromString(KReportUtils::readSectionTypeNameAttribute(elemSource)); if (m_type == KReportSection::None) { m_valid = false; return; } createProperties(elemSource); m_backgroundColor->setValue(QColor(elemSource.attribute(QLatin1String("fo:background-color")))); KReportPluginManager* manager = KReportPluginManager::self(); QDomNodeList section = elemSource.childNodes(); for (int nodeCounter = 0; nodeCounter < section.count(); nodeCounter++) { QDomElement elemThis = section.item(nodeCounter).toElement(); QString n = elemThis.tagName(); if (n.startsWith(QLatin1String("report:"))) { QString reportItemName = n.mid(qstrlen("report:")); if (reportItemName == QLatin1String("line")) { KReportItemLine * line = new KReportItemLine(elemThis); m_objects.append(line); continue; } KReportPluginInterface *plugin = manager->plugin(reportItemName); if (plugin) { QObject *obj = plugin->createRendererInstance(elemThis); if (obj) { KReportItemBase *krobj = dynamic_cast(obj); if (krobj) { m_objects.append(krobj); } continue; } } } kreportWarning() << "While parsing section encountered an unknown element: " << n; } qSort(m_objects.begin(), m_objects.end(), zLessThan); m_valid = true; } bool KReportSection::zLessThan(KReportItemBase* s1, KReportItemBase* s2) { return s1->Z < s2->Z; } bool KReportSection::xLessThan(KReportItemBase* s1, KReportItemBase* s2) { return s1->position().toPoint().x() < s2->position().toPoint().x(); } #endif KReportSection::~KReportSection() { } qreal KReportSection::height() const { return d->height >= 0.0 ? d->height : KReportSection::defaultHeight(); } QColor KReportSection::backgroundColor() const { return d->backgroundColor.isValid() ? d->backgroundColor : KReportSection::defaultBackgroundColor(); } QList KReportSection::elements() const { return d->elements; } bool KReportSection::addElement(const KReportElement &element) { if (d->elementsSet.contains(element)) { kreportWarning() << "Section already contains element" << element; return false; } d->elements.append(element); d->elementsSet.insert(element); return true; } bool KReportSection::insertElement(int i, const KReportElement &element) { if (i < 0 || i > d->elements.count()) { - kreportWarning() << "Could not insert element at index" << i << "into section"; + kreportWarning() << "Could not insert element" << element << "at index" << i << "into section"; return false; } if (d->elementsSet.contains(element)) { kreportWarning() << "Section already contains element" << element; return false; } d->elements.insert(i, element); d->elementsSet.insert(element); return true; } bool KReportSection::removeElement(const KReportElement &element) { if (!d->elementsSet.remove(element)) { kreportWarning() << "Could not find element" << element << "in section"; return false; } if (!d->elements.removeOne(element)) { kreportCritical() << "Could not find element" << element << "in section's list but found in section's set"; return false; } return true; } bool KReportSection::removeElementAt(int i) { if (i < 0 || i > (d->elements.count() - 1)) { kreportWarning() << "Could not find element at index" << i << "in section"; return false; } KReportElement e = d->elements.takeAt(i); if (!d->elementsSet.remove(e)) { kreportWarning() << "Could not find element" << e << "in section"; return false; } return true; } KReportSection::Data* KReportSection::Data::clone() const { Data *data = new Data(*this); data->elements.clear(); KReportElement eClone; foreach(const KReportElement &el, elements) { eClone = el.clone(); data->elements.append(eClone); data->elementsSet.insert(eClone); } return data; } //static qreal KReportSection::defaultHeight() { return KReportDesignGlobal::self()->defaultSectionHeight; } //static void KReportSection::setDefaultHeight(qreal ptHeight) { KReportDesignGlobal::self()->defaultSectionHeight = ptHeight; } //static QColor KReportSection::defaultBackgroundColor() { return KReportDesignGlobal::self()->defaultSectionBackgroundColor; } //static void KReportSection::setDefaultBackgroundColor(const QColor &color) { KReportDesignGlobal::self()->defaultSectionBackgroundColor = color; }