File indexing completed on 2024-05-19 03:50:39

0001 /*
0002     File                 : InfoElementTest.cpp
0003     Project              : LabPlot
0004     Description          : Tests for InfoElement
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2023 Martin Marmsoler <martin.marmsoler@gmail.com>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "InfoElementTest.h"
0012 #include "backend/core/Project.h"
0013 #include "backend/worksheet/InfoElement.h"
0014 #include "backend/worksheet/Worksheet.h"
0015 #include "backend/worksheet/plots/cartesian/CustomPoint.h"
0016 #include "backend/worksheet/plots/cartesian/XYEquationCurve.h"
0017 
0018 void InfoElementTest::addPlot() {
0019     Project project;
0020     auto* ws = new Worksheet(QStringLiteral("worksheet"));
0021     QVERIFY(ws != nullptr);
0022     project.addChild(ws);
0023 
0024     auto* p = new CartesianPlot(QStringLiteral("plot"));
0025     QVERIFY(p != nullptr);
0026     ws->addChild(p);
0027 
0028     auto* curve{new XYEquationCurve(QStringLiteral("f(x)"))};
0029     curve->setCoordinateSystemIndex(p->defaultCoordinateSystemIndex());
0030     p->addChild(curve);
0031 
0032     XYEquationCurve::EquationData data;
0033     data.min = QStringLiteral("0");
0034     data.max = QStringLiteral("10");
0035     data.count = 11;
0036     data.expression1 = QStringLiteral("x");
0037     curve->setEquationData(data);
0038     curve->recalculate();
0039 
0040     auto* curve2{new XYEquationCurve(QStringLiteral("f(x^2)"))};
0041     curve2->setCoordinateSystemIndex(p->defaultCoordinateSystemIndex());
0042     p->addChild(curve2);
0043 
0044     data.min = QStringLiteral("0");
0045     data.max = QStringLiteral("10");
0046     data.count = 11;
0047     data.expression1 = QStringLiteral("x^2");
0048     curve2->setEquationData(data);
0049     curve2->recalculate();
0050 
0051     CHECK_RANGE(p, curve, Dimension::X, 0., 10.);
0052     CHECK_RANGE(p, curve, Dimension::Y, 0., 100.);
0053 
0054     auto* ie = new InfoElement(QStringLiteral("InfoElement"), p, curve, 4.9);
0055     QVERIFY(ie != nullptr);
0056     p->addChild(ie);
0057 
0058     {
0059         const auto points = ie->children<CustomPoint>();
0060         QCOMPARE(ie->markerPointsCount(), 1);
0061         QCOMPARE(ie->markerPointAt(0).curve, curve);
0062         QCOMPARE(points.count(), 1);
0063         QCOMPARE(ie->markerPointAt(0).customPoint, points.at(0));
0064         QCOMPARE(points.at(0)->positionLogical(), QPointF(5, 5));
0065     }
0066 
0067     ie->addCurve(curve2);
0068 
0069     {
0070         const auto points = ie->children<CustomPoint>();
0071         QCOMPARE(points.count(), 2);
0072 
0073         QCOMPARE(ie->markerPointsCount(), 2);
0074         QCOMPARE(ie->markerPointAt(0).curve, curve);
0075         QCOMPARE(ie->markerPointAt(0).customPoint, points.at(0));
0076         QCOMPARE(points.at(0)->positionLogical(), QPointF(5, 5));
0077         QCOMPARE(ie->markerPointAt(1).curve, curve2);
0078         QCOMPARE(ie->markerPointAt(1).customPoint, points.at(1));
0079         QCOMPARE(points.at(1)->positionLogical(), QPointF(5, 25));
0080     }
0081 }
0082 
0083 void InfoElementTest::addRemoveCurve() {
0084     Project project;
0085     auto* ws = new Worksheet(QStringLiteral("worksheet"));
0086     QVERIFY(ws != nullptr);
0087     project.addChild(ws);
0088 
0089     auto* p = new CartesianPlot(QStringLiteral("plot"));
0090     QVERIFY(p != nullptr);
0091     ws->addChild(p);
0092 
0093     auto* curve{new XYEquationCurve(QStringLiteral("f(x)"))};
0094     curve->setCoordinateSystemIndex(p->defaultCoordinateSystemIndex());
0095     p->addChild(curve);
0096 
0097     XYEquationCurve::EquationData data;
0098     data.min = QStringLiteral("0");
0099     data.max = QStringLiteral("10");
0100     data.count = 11;
0101     data.expression1 = QStringLiteral("x");
0102     curve->setEquationData(data);
0103     curve->recalculate();
0104 
0105     auto* curve2{new XYEquationCurve(QStringLiteral("f(x^2)"))};
0106     curve2->setCoordinateSystemIndex(p->defaultCoordinateSystemIndex());
0107     p->addChild(curve2);
0108 
0109     data.min = QStringLiteral("0");
0110     data.max = QStringLiteral("10");
0111     data.count = 11;
0112     data.expression1 = QStringLiteral("x^2");
0113     curve2->setEquationData(data);
0114     curve2->recalculate();
0115 
0116     CHECK_RANGE(p, curve, Dimension::X, 0., 10.);
0117     CHECK_RANGE(p, curve, Dimension::Y, 0., 100.);
0118 
0119     auto* ie = new InfoElement(QStringLiteral("InfoElement"), p, curve, 4.9);
0120     QVERIFY(ie != nullptr);
0121     p->addChild(ie);
0122 
0123     {
0124         const auto points = ie->children<CustomPoint>();
0125         QCOMPARE(ie->markerPointsCount(), 1);
0126         QCOMPARE(ie->markerPointAt(0).curve, curve);
0127         QCOMPARE(points.count(), 1);
0128         QCOMPARE(ie->markerPointAt(0).customPoint, points.at(0));
0129         QCOMPARE(points.at(0)->positionLogical(), QPointF(5, 5));
0130     }
0131     QCOMPARE(ie->connectionLineCurveName(), curve->name());
0132 
0133     ie->addCurve(curve2);
0134 
0135     QCOMPARE(ie->connectionLineCurveName(), curve->name()); // No change here
0136 
0137     {
0138         const auto points = ie->children<CustomPoint>();
0139         QCOMPARE(points.count(), 2);
0140 
0141         QCOMPARE(ie->markerPointsCount(), 2);
0142         QCOMPARE(ie->markerPointAt(0).curve, curve);
0143         QCOMPARE(ie->markerPointAt(0).customPoint, points.at(0));
0144         QCOMPARE(points.at(0)->positionLogical(), QPointF(5, 5));
0145         QCOMPARE(ie->markerPointAt(1).curve, curve2);
0146         QCOMPARE(ie->markerPointAt(1).customPoint, points.at(1));
0147         QCOMPARE(points.at(1)->positionLogical(), QPointF(5, 25));
0148     }
0149 
0150     ie->removeCurve(curve);
0151 
0152     {
0153         const auto points = ie->children<CustomPoint>();
0154         QCOMPARE(points.count(), 1);
0155 
0156         QCOMPARE(ie->markerPointsCount(), 1);
0157         QCOMPARE(ie->markerPointAt(0).curve, curve2);
0158         QCOMPARE(ie->markerPointAt(0).customPoint, points.at(0));
0159         QCOMPARE(points.at(0)->positionLogical(), QPointF(5, 25));
0160     }
0161 
0162     QCOMPARE(ie->connectionLineCurveName(), curve2->name());
0163 }
0164 
0165 QTEST_MAIN(InfoElementTest)