CmvrControlStation/tests/agvmapviewwidget_test.cpp
Codex 22620db699 feat(agv): merge local map editing and manual controls
Bring local station and route editing/deletion, verified map saves, confirmed switches, direct map transport and manual velocity ramps onto latest main. Preserve upstream non-AGV implementations, interface definitions and startup behavior.

Validated with the existing qmake Release build, AGV map/UI/manual-motion tests, loopback upload/deletion/SEER transport tests and function-level retention audits.
2026-11-10 16:10:46 +08:00

684 lines
30 KiB
C++

#include "agvmapviewwidget.h"
#include <QApplication>
#include <QHelpEvent>
#include <QLineF>
#include <QMouseEvent>
#include <QPainter>
#include <QPolygonF>
#include <QSignalSpy>
#include <QToolTip>
#include <QtTest>
#include <QtMath>
#include <limits>
namespace {
QVariantMap mapMetadata()
{
return {{QStringLiteral("mapId"), QStringLiteral("rotated-map")},
{QStringLiteral("dimension"), 2},
{QStringLiteral("originX"), 5.0},
{QStringLiteral("originY"), -3.0},
{QStringLiteral("originTheta"), qDegreesToRadians(90.0)},
{QStringLiteral("resolution"), 0.2},
{QStringLiteral("renderScale"), 0.5}};
}
void prepareView(AgvMapViewWidget &view)
{
// The map viewport is 800x600 after reserving margins and the bottom ruler.
view.resize(824, 652);
QImage image(400, 300, QImage::Format_RGB32);
image.fill(QColor(QStringLiteral("#f8fafc")));
view.setMapFrame(image, mapMetadata());
view.show();
QApplication::processEvents();
}
void mouseEvent(AgvMapViewWidget &view, QEvent::Type type,
const QPoint &position, Qt::MouseButton button,
Qt::MouseButtons buttons)
{
QMouseEvent event(type, QPointF(position), button, buttons, Qt::NoModifier);
QApplication::sendEvent(&view, &event);
}
void click(AgvMapViewWidget &view, const QPoint &position)
{
mouseEvent(view, QEvent::MouseButtonPress, position,
Qt::LeftButton, Qt::LeftButton);
mouseEvent(view, QEvent::MouseButtonRelease, position,
Qt::LeftButton, Qt::NoButton);
}
bool nearPoint(const QPointF &actual, const QPointF &expected)
{
return QLineF(actual, expected).length() < 1e-8;
}
QPointF widgetPointForWorld(const AgvMapViewWidget &view, const QPointF &world)
{
QPointF origin;
QPointF horizontal;
QPointF vertical;
if (!view.widgetToWorld(QPointF(100, 100), &origin)
|| !view.widgetToWorld(QPointF(200, 100), &horizontal)
|| !view.widgetToWorld(QPointF(100, 200), &vertical))
return QPointF(-1000, -1000);
horizontal = (horizontal - origin) / 100.0;
vertical = (vertical - origin) / 100.0;
const QPointF delta = world - origin;
const double determinant = horizontal.x() * vertical.y()
- vertical.x() * horizontal.y();
return QPointF(100 + (delta.x() * vertical.y() - vertical.x() * delta.y()) / determinant,
100 + (horizontal.x() * delta.y() - delta.x() * horizontal.y()) / determinant);
}
}
class AgvMapViewWidgetTest : public QObject
{
Q_OBJECT
private slots:
void projectsRotatedAndDownsampledMap();
void keepsCoordinatesDuringZoomAndPan();
void rejectsInvalidAndThreeDimensionalMaps();
void distinguishesClickFromDrag();
void selectsStationsWithinScreenRadius();
void acceptsCanvasPointsOutsideReceivedImage();
void fitsMapAndObjectsOutsideImage();
void keepsBottomWallAboveRulerOnShortCanvas();
void keepsManualViewDuringTelemetryUpdates();
void paintsReferenceStationRouteAndGridStyle();
void paintsDirectionsOnSampledRoutes_data();
void paintsDirectionsOnSampledRoutes();
void usesMetadataObjectsUntilDocumentOverridesThem();
void displaysStationNamesWithoutChangingIds();
void elidesLongStationNames();
void paintsEditorOverlays();
};
void AgvMapViewWidgetTest::projectsRotatedAndDownsampledMap()
{
AgvMapViewWidget view;
prepareView(view);
QCOMPARE(view.mapId(), QStringLiteral("rotated-map"));
QPointF world;
// The fitted image starts at (12,12), with 2 screen pixels per image pixel.
// Image (100,150) is grid-local (40,60) metres; a 90 degree origin rotates
// it to world (-55,37) after adding the translated origin (5,-3).
QVERIFY(view.widgetToWorld(QPointF(212, 312), &world));
QVERIFY(nearPoint(world, QPointF(-55, 37)));
QVERIFY(view.widgetToWorld(QPointF(12, 612), &world));
QVERIFY(nearPoint(world, QPointF(5, -3)));
}
void AgvMapViewWidgetTest::keepsCoordinatesDuringZoomAndPan()
{
AgvMapViewWidget view;
prepareView(view);
const QPointF anchor(view.width() * 0.5, (view.height() - 28) * 0.5);
QPointF worldBefore;
QVERIFY(view.widgetToWorld(anchor, &worldBefore));
view.zoomIn();
QPointF worldAfter;
QVERIFY(view.widgetToWorld(anchor, &worldAfter));
QVERIFY(nearPoint(worldAfter, worldBefore));
const QPointF zoomedPoint = anchor + (QPointF(212, 312) - anchor) * 1.25;
QVERIFY(view.widgetToWorld(zoomedPoint, &worldAfter));
QVERIFY(nearPoint(worldAfter, QPointF(-55, 37)));
view.setEditMode(AgvMapViewWidget::AddStationMode);
const QPoint dragStart(300, 250);
const QPoint delta(80, -40);
mouseEvent(view, QEvent::MouseButtonPress, dragStart,
Qt::RightButton, Qt::RightButton);
mouseEvent(view, QEvent::MouseMove, dragStart + delta,
Qt::NoButton, Qt::RightButton);
mouseEvent(view, QEvent::MouseButtonRelease, dragStart + delta,
Qt::RightButton, Qt::NoButton);
QVERIFY(view.widgetToWorld(zoomedPoint + delta, &worldAfter));
QVERIFY(nearPoint(worldAfter, QPointF(-55, 37)));
view.resetView();
QVERIFY(view.widgetToWorld(QPointF(212, 312), &worldAfter));
QVERIFY(nearPoint(worldAfter, QPointF(-55, 37)));
}
void AgvMapViewWidgetTest::rejectsInvalidAndThreeDimensionalMaps()
{
AgvMapViewWidget view;
prepareView(view);
QPointF world(42, 42);
QVERIFY(!view.widgetToWorld(QPointF(-100, -100), &world));
QCOMPARE(world, QPointF(42, 42));
QVERIFY(!view.widgetToWorld(QPointF(212, 312), nullptr));
QVERIFY(!view.widgetToWorld(
QPointF(std::numeric_limits<double>::quiet_NaN(), 312), &world));
QImage image(400, 300, QImage::Format_RGB32);
const QList<QString> fields = {QStringLiteral("originX"), QStringLiteral("originY"),
QStringLiteral("originTheta"), QStringLiteral("resolution"),
QStringLiteral("renderScale")};
for (const QString &field : fields) {
QVariantMap metadata = mapMetadata();
metadata.insert(field, std::numeric_limits<double>::infinity());
view.setMapFrame(image, metadata);
QVERIFY2(!view.widgetToWorld(QPointF(212, 312), &world), qPrintable(field));
}
QVariantMap metadata = mapMetadata();
metadata.insert(QStringLiteral("resolution"), 0.0);
view.setMapFrame(image, metadata);
QVERIFY(!view.widgetToWorld(QPointF(212, 312), &world));
metadata = mapMetadata();
metadata.insert(QStringLiteral("dimension"), 3);
view.setMapFrame(image, metadata);
QVERIFY(!view.widgetToWorld(QPointF(212, 312), &world));
view.setEditMode(AgvMapViewWidget::AddStationMode);
QSignalSpy selected(&view, &AgvMapViewWidget::mapPointSelected);
click(view, QPoint(212, 312));
QCOMPARE(selected.count(), 0);
}
void AgvMapViewWidgetTest::distinguishesClickFromDrag()
{
AgvMapViewWidget view;
prepareView(view);
QSignalSpy selected(&view, &AgvMapViewWidget::mapPointSelected);
click(view, QPoint(212, 312));
QCOMPARE(selected.count(), 0);
view.setEditMode(AgvMapViewWidget::AddStationMode);
click(view, QPoint(212, 312));
QCOMPARE(selected.count(), 1);
QVERIFY(qAbs(selected.at(0).at(0).toDouble() + 55.0) < 1e-8);
QVERIFY(qAbs(selected.at(0).at(1).toDouble() - 37.0) < 1e-8);
const QPoint press(212, 312);
const QPoint jitter(qMax(0, QApplication::startDragDistance() - 1), 0);
mouseEvent(view, QEvent::MouseButtonPress, press,
Qt::LeftButton, Qt::LeftButton);
mouseEvent(view, QEvent::MouseMove, press + jitter,
Qt::NoButton, Qt::LeftButton);
mouseEvent(view, QEvent::MouseButtonRelease, press + jitter,
Qt::LeftButton, Qt::NoButton);
QCOMPARE(selected.count(), 2);
QPointF world;
QVERIFY(view.widgetToWorld(press, &world));
QVERIFY(nearPoint(world, QPointF(-55, 37)));
const QPoint delta(QApplication::startDragDistance() + 30, 0);
mouseEvent(view, QEvent::MouseButtonPress, press,
Qt::LeftButton, Qt::LeftButton);
mouseEvent(view, QEvent::MouseMove, press + delta,
Qt::NoButton, Qt::LeftButton);
mouseEvent(view, QEvent::MouseButtonRelease, press + delta,
Qt::LeftButton, Qt::NoButton);
QCOMPARE(selected.count(), 2);
QVERIFY(view.widgetToWorld(press + delta, &world));
QVERIFY(nearPoint(world, QPointF(-55, 37)));
}
void AgvMapViewWidgetTest::selectsStationsWithinScreenRadius()
{
AgvMapViewWidget view;
prepareView(view);
view.setEditorObjects(
{QVariantMap{{QStringLiteral("id"), QStringLiteral("A")},
{QStringLiteral("x"), -55.0}, {QStringLiteral("y"), 37.0}},
QVariantMap{{QStringLiteral("id"), QStringLiteral("B")},
{QStringLiteral("x"), -55.0}, {QStringLiteral("y"), 47.0}}}, {});
view.setEditMode(AgvMapViewWidget::AddRouteMode);
QSignalSpy selected(&view, &AgvMapViewWidget::stationSelected);
QSignalSpy points(&view, &AgvMapViewWidget::mapPointSelected);
click(view, QPoint(225, 312));
QCOMPARE(selected.count(), 1);
QCOMPARE(selected.at(0).at(0).toString(), QStringLiteral("A"));
click(view, QPoint(226, 312));
QCOMPARE(selected.count(), 2);
click(view, QPoint(227, 312));
QCOMPARE(selected.count(), 2);
click(view, QPoint(252, 312));
QCOMPARE(selected.count(), 3);
QCOMPARE(selected.at(2).at(0).toString(), QStringLiteral("B"));
click(view, QPoint(400, 400));
QCOMPARE(points.count(), 0);
const QPointF anchor(view.width() * 0.5, (view.height() - 28) * 0.5);
view.zoomIn();
const QPointF zoomedCenter = anchor + (QPointF(212, 312) - anchor) * 1.25;
click(view, (zoomedCenter + QPointF(13, 0)).toPoint());
QCOMPARE(selected.count(), 4);
click(view, (zoomedCenter + QPointF(15, 0)).toPoint());
QCOMPARE(selected.count(), 4);
}
void AgvMapViewWidgetTest::paintsEditorOverlays()
{
AgvMapViewWidget view;
prepareView(view);
const QVariantMap first{{QStringLiteral("id"), QStringLiteral("A")},
{QStringLiteral("x"), -55.0}, {QStringLiteral("y"), 37.0},
{QStringLiteral("theta"), 0.0}, {QStringLiteral("draft"), false}};
const QVariantMap second{{QStringLiteral("id"), QStringLiteral("B")},
{QStringLiteral("x"), -45.0}, {QStringLiteral("y"), 65.0},
{QStringLiteral("theta"), 1.2}, {QStringLiteral("draft"), true}};
const QVariantMap route{{QStringLiteral("id"), QStringLiteral("A_TO_B")},
{QStringLiteral("points"), QVariantList{first, second}},
{QStringLiteral("draft"), true}};
view.setEditorObjects({first, second}, {route});
view.setSelectedStation(QStringLiteral("A"));
view.updateRobotPose(-65, 50, 0.7, true);
QApplication::processEvents();
QVERIFY(view.grab().save(QStringLiteral("agvmapviewwidget-test-preview.png")));
}
void AgvMapViewWidgetTest::acceptsCanvasPointsOutsideReceivedImage()
{
AgvMapViewWidget view;
prepareView(view);
QPointF world;
// Image begins at x=12. x=9 lies in the canvas around the received image.
QVERIFY(view.widgetToWorld(QPointF(9, 312), &world));
QVERIFY(nearPoint(world, QPointF(-55, -3.6)));
QVERIFY(!view.widgetToWorld(QPointF(2, 312), &world));
view.setEditMode(AgvMapViewWidget::AddStationMode);
QSignalSpy selected(&view, &AgvMapViewWidget::mapPointSelected);
click(view, QPoint(9, 312));
QCOMPARE(selected.count(), 1);
QVERIFY(qAbs(selected.at(0).at(1).toDouble() + 3.6) < 1e-8);
}
void AgvMapViewWidgetTest::fitsMapAndObjectsOutsideImage()
{
AgvMapViewWidget view;
prepareView(view);
const QPointF robotWorld(15, -30);
const QPointF stationWorld(-55, 200);
const QVariantMap station{{QStringLiteral("id"), QStringLiteral("OUTSIDE")},
{QStringLiteral("x"), stationWorld.x()},
{QStringLiteral("y"), stationWorld.y()},
{QStringLiteral("theta"), 0.0}};
view.setEditorObjects({station}, {});
view.updateRobotPose(robotWorld.x(), robotWorld.y(), 0.2, true);
view.resetView();
QApplication::processEvents();
const QPointF robotPosition = widgetPointForWorld(view, robotWorld);
const QPointF stationPosition = widgetPointForWorld(view, stationWorld);
const QRectF safeMarkerArea = QRectF(view.rect()).adjusted(25, 25, -25, -25);
QVERIFY(safeMarkerArea.contains(robotPosition));
QVERIFY(safeMarkerArea.contains(stationPosition));
const QList<QPointF> mapCorners = {QPointF(5, -3), QPointF(5, 157),
QPointF(-115, -3), QPointF(-115, 157)};
for (const QPointF &corner : mapCorners)
QVERIFY(QRectF(view.rect()).adjusted(8, 8, -8, -8)
.contains(widgetPointForWorld(view, corner)));
const QImage rendered = view.grab().toImage();
int blueRobotPixels = 0;
const QPoint center = robotPosition.toPoint();
for (int y = center.y() - 12; y <= center.y() + 12; ++y) {
for (int x = center.x() - 12; x <= center.x() + 12; ++x) {
const QColor color = rendered.pixelColor(x, y);
if (color.blue() > 180 && color.red() < 100 && color.green() < 170)
++blueRobotPixels;
}
}
QVERIFY(blueRobotPixels > 30);
QVERIFY(rendered.save(QStringLiteral("agvmapviewwidget-full-fit-preview.png")));
view.setEditMode(AgvMapViewWidget::AddRouteMode);
QSignalSpy selected(&view, &AgvMapViewWidget::stationSelected);
click(view, stationPosition.toPoint());
QCOMPARE(selected.count(), 1);
QCOMPARE(selected.at(0).at(0).toString(), QStringLiteral("OUTSIDE"));
}
void AgvMapViewWidgetTest::keepsBottomWallAboveRulerOnShortCanvas()
{
AgvMapViewWidget view;
view.resize(824, 340);
QImage map(1600, 800, QImage::Format_ARGB32_Premultiplied);
map.fill(Qt::transparent);
const QColor wallColor(83, 98, 112);
QPainter raster(&map);
raster.fillRect(QRect(0, 0, map.width(), 12), wallColor);
raster.fillRect(QRect(0, map.height() - 12, map.width(), 12), wallColor);
raster.fillRect(QRect(0, 0, 12, map.height()), wallColor);
raster.fillRect(QRect(map.width() - 12, 0, 12, map.height()), wallColor);
raster.end();
QVariantMap metadata = mapMetadata();
metadata.insert(QStringLiteral("originX"), 0.0);
metadata.insert(QStringLiteral("originY"), 0.0);
metadata.insert(QStringLiteral("originTheta"), 0.0);
metadata.insert(QStringLiteral("resolution"), 0.1);
metadata.insert(QStringLiteral("renderScale"), 1.0);
view.setMapFrame(map, metadata);
view.show();
QApplication::processEvents();
// Height limits this wide image to scale .36, in the 288px-tall viewport.
// All four map edges must remain in view, with the bottom above the ruler.
const QPointF topLeft = widgetPointForWorld(view, QPointF(0, 80));
const QPointF bottomRight = widgetPointForWorld(view, QPointF(160, 0));
QVERIFY(nearPoint(topLeft, QPointF(124, 12)));
QVERIFY(nearPoint(bottomRight, QPointF(700, 300)));
const int rulerTop = view.height() - 26;
QVERIFY(bottomRight.y() <= rulerTop - 12);
const QImage rendered = view.grab().toImage();
QCOMPARE(rendered.pixelColor(412, 14), wallColor);
QCOMPARE(rendered.pixelColor(126, 156), wallColor);
QCOMPARE(rendered.pixelColor(698, 156), wallColor);
// This is the last occupied screen row, not a marker or a geometry bound.
QCOMPARE(rendered.pixelColor(412, 299), wallColor);
QVERIFY(rendered.pixelColor(412, rulerTop + 2) != wallColor);
QVERIFY(rendered.save(QStringLiteral("agvmapviewwidget-short-canvas-wall-preview.png")));
const QPointF anchor(412, 156);
QPointF before;
QPointF after;
QVERIFY(view.widgetToWorld(anchor, &before));
view.zoomIn();
QVERIFY(view.widgetToWorld(anchor, &after));
QVERIFY(nearPoint(before, after));
view.zoomOut();
QVERIFY(view.widgetToWorld(anchor, &after));
QVERIFY(nearPoint(before, after));
QVERIFY(nearPoint(widgetPointForWorld(view, QPointF(160, 0)), bottomRight));
}
void AgvMapViewWidgetTest::keepsManualViewDuringTelemetryUpdates()
{
AgvMapViewWidget view;
prepareView(view);
view.zoomIn();
const QPointF center = view.rect().center();
QPointF before;
QPointF after;
QVERIFY(view.widgetToWorld(center, &before));
view.updateRobotPose(15, -30, 0.3, true);
view.setEditorObjects(
{QVariantMap{{QStringLiteral("id"), QStringLiteral("OUTSIDE")},
{QStringLiteral("x"), -55.0}, {QStringLiteral("y"), 200.0}}}, {});
QVERIFY(view.widgetToWorld(center, &after));
QVERIFY(nearPoint(before, after));
view.resetView();
QVERIFY(view.widgetToWorld(center, &after));
QVERIFY(!nearPoint(before, after));
QVERIFY(QRectF(view.rect()).adjusted(25, 25, -25, -25)
.contains(widgetPointForWorld(view, QPointF(15, -30))));
}
void AgvMapViewWidgetTest::paintsReferenceStationRouteAndGridStyle()
{
AgvMapViewWidget view;
view.resize(824, 652);
QImage transparentMap(400, 300, QImage::Format_ARGB32_Premultiplied);
transparentMap.fill(Qt::transparent);
QVariantMap metadata = mapMetadata();
metadata.insert(QStringLiteral("mapId"), QStringLiteral("reference-style"));
metadata.insert(QStringLiteral("originX"), 0.0);
metadata.insert(QStringLiteral("originY"), 0.0);
metadata.insert(QStringLiteral("originTheta"), 0.0);
metadata.insert(QStringLiteral("resolution"), 0.1);
metadata.insert(QStringLiteral("renderScale"), 1.0);
view.setMapFrame(transparentMap, metadata);
const QVariantMap first{{QStringLiteral("id"), QStringLiteral("LM1")},
{QStringLiteral("x"), 5.0}, {QStringLiteral("y"), 10.0}};
const QVariantMap second{{QStringLiteral("id"), QStringLiteral("LM2")},
{QStringLiteral("x"), 15.0}, {QStringLiteral("y"), 10.0}};
const QVariantMap selected{{QStringLiteral("id"), QStringLiteral("LM8")},
{QStringLiteral("x"), 15.0}, {QStringLiteral("y"), 20.0}};
const QVariantMap charging{{QStringLiteral("id"), QStringLiteral("CP5")},
{QStringLiteral("x"), 25.0}, {QStringLiteral("y"), 20.0},
{QStringLiteral("type"), QStringLiteral("ChargingPoint")}};
const QVariantMap forward{{QStringLiteral("id"), QStringLiteral("LM1_TO_LM2")},
{QStringLiteral("points"), QVariantList{first, second}}};
const QVariantMap reverse{{QStringLiteral("id"), QStringLiteral("LM2_TO_LM1")},
{QStringLiteral("points"), QVariantList{second, first}}};
view.setEditorObjects({first, second, selected, charging}, {forward, reverse});
view.setSelectedStation(QStringLiteral("LM8"));
view.show();
QApplication::processEvents();
QImage rendered = view.grab().toImage();
QVERIFY(rendered.save(QStringLiteral("agvmapviewwidget-reference-style-preview.png")));
const QColor orange = rendered.pixelColor(112, 412);
QVERIFY(orange.red() > 220 && orange.green() > 120
&& orange.green() < 190 && orange.blue() < 90);
const QColor purple = rendered.pixelColor(312, 212);
QVERIFY(purple.blue() > 190 && purple.red() > 100
&& purple.red() < 170 && purple.green() < 120);
const QColor halo = rendered.pixelColor(322, 220);
QVERIFY(halo.red() > 200 && halo.blue() > halo.red() + 10);
const QColor teal = rendered.pixelColor(512, 212);
QVERIFY(teal.red() < 80 && teal.green() > 120 && teal.blue() > 100);
QVERIFY(rendered.pixelColor(516, 216).red() > 120);
const auto countBlue = [&rendered](const QRect &area) {
int count = 0;
for (int y = area.top(); y <= area.bottom(); ++y) {
for (int x = area.left(); x <= area.right(); ++x) {
const QColor color = rendered.pixelColor(x, y);
if (color.blue() > 200 && color.red() < 100 && color.green() < 160)
++count;
}
}
return count;
};
// Arrow heads protrude above the thin line at two distinct positions.
QVERIFY(countBlue(QRect(234, 408, 8, 3)) > 2);
QVERIFY(countBlue(QRect(182, 408, 8, 3)) > 2);
const QColor outsideGrid = rendered.pixelColor(9, 412);
QVERIFY2(outsideGrid.blue() > outsideGrid.red() + 12, qPrintable(outsideGrid.name()));
mouseEvent(view, QEvent::MouseButtonPress, QPoint(300, 300),
Qt::RightButton, Qt::RightButton);
mouseEvent(view, QEvent::MouseMove, QPoint(320, 290),
Qt::NoButton, Qt::RightButton);
mouseEvent(view, QEvent::MouseButtonRelease, QPoint(320, 290),
Qt::RightButton, Qt::NoButton);
rendered = view.grab().toImage();
const QColor movedGrid = rendered.pixelColor(9, 402);
QVERIFY2(movedGrid.blue() > movedGrid.red() + 12, qPrintable(movedGrid.name()));
}
void AgvMapViewWidgetTest::paintsDirectionsOnSampledRoutes_data()
{
QTest::addColumn<bool>("curved");
QTest::addColumn<bool>("bidirectional");
QTest::newRow("33-point-straight") << false << false;
QTest::newRow("33-point-bezier") << true << false;
QTest::newRow("33-point-bidirectional-bezier") << true << true;
}
void AgvMapViewWidgetTest::paintsDirectionsOnSampledRoutes()
{
QFETCH(bool, curved);
QFETCH(bool, bidirectional);
AgvMapViewWidget view;
view.resize(824, 652);
QImage transparentMap(400, 300, QImage::Format_ARGB32_Premultiplied);
transparentMap.fill(Qt::transparent);
QVariantMap metadata = mapMetadata();
metadata.insert(QStringLiteral("originX"), 0.0);
metadata.insert(QStringLiteral("originY"), 0.0);
metadata.insert(QStringLiteral("originTheta"), 0.0);
metadata.insert(QStringLiteral("resolution"), 0.1);
metadata.insert(QStringLiteral("renderScale"), 1.0);
view.setMapFrame(transparentMap, metadata);
// These are the 33 samples returned by the SMAP document renderer for a
// straight route or cubic Bezier with controls (5,20) and (15,20).
QVariantList points;
QList<QPointF> worldPoints;
for (int step = 0; step <= 32; ++step) {
const double t = step / 32.0;
const QPointF world(curved ? 5.0 + 10.0 * (3.0 * t * t - 2.0 * t * t * t)
: 5.0 + 10.0 * t,
curved ? 10.0 + 30.0 * t * (1.0 - t) : 10.0);
worldPoints.append(world);
points.append(QVariantMap{{QStringLiteral("x"), world.x()},
{QStringLiteral("y"), world.y()}});
}
view.setEditorObjects({}, {QVariantMap{{QStringLiteral("id"), QStringLiteral("SAMPLED")},
{QStringLiteral("points"), points},
{QStringLiteral("bidirectional"), bidirectional}}});
view.show();
QApplication::processEvents();
QPolygonF geometry;
for (const QPointF &world : worldPoints)
geometry.append(widgetPointForWorld(view, world));
for (int index = 1; index < geometry.size(); ++index)
QVERIFY(QLineF(geometry.at(index - 1), geometry.at(index)).length() < 20.0);
// A three-pixel stroke masks the thin route itself. Filled arrow heads must
// leave blue pixels beyond it, including when every sample segment is short.
QImage centerlineMask(view.size(), QImage::Format_ARGB32_Premultiplied);
centerlineMask.fill(Qt::transparent);
QPainter maskPainter(&centerlineMask);
maskPainter.setRenderHint(QPainter::Antialiasing);
maskPainter.setPen(QPen(Qt::black, 3.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
maskPainter.drawPolyline(geometry);
maskPainter.end();
const QImage rendered = view.grab().toImage();
int arrowPixels = 0;
for (int y = 0; y < rendered.height(); ++y) {
for (int x = 0; x < rendered.width(); ++x) {
const QColor color = rendered.pixelColor(x, y);
if (centerlineMask.pixelColor(x, y).alpha() == 0
&& color.blue() > 200 && color.red() < 100 && color.green() < 160)
++arrowPixels;
}
}
QVERIFY2(arrowPixels >= (bidirectional ? 8 : 4),
qPrintable(QStringLiteral("Only %1 pixels protrude beyond the route").arg(arrowPixels)));
if (!curved) {
// The straight route spans x112..312: its forward tip is at 65%, and
// its head extends above the stroke immediately behind that tip.
const QColor arrowWing = rendered.pixelColor(235, 409);
QVERIFY(arrowWing.blue() > 200 && arrowWing.red() < 100 && arrowWing.green() < 160);
}
QVERIFY(rendered.save(QStringLiteral("agvmapviewwidget-%1-preview.png")
.arg(QString::fromLatin1(QTest::currentDataTag()))));
}
void AgvMapViewWidgetTest::usesMetadataObjectsUntilDocumentOverridesThem()
{
AgvMapViewWidget view;
prepareView(view);
const QVariantMap rawStation{{QStringLiteral("id"), QStringLiteral("STREAM")},
{QStringLiteral("x"), -55.0}, {QStringLiteral("y"), 37.0}};
QVariantMap metadata = mapMetadata();
metadata.insert(QStringLiteral("stations"), QVariantList{rawStation});
QImage image(400, 300, QImage::Format_ARGB32_Premultiplied);
image.fill(Qt::transparent);
view.setMapFrame(image, metadata);
view.setEditMode(AgvMapViewWidget::AddRouteMode);
QSignalSpy selected(&view, &AgvMapViewWidget::stationSelected);
click(view, QPoint(212, 312));
QCOMPARE(selected.count(), 1);
QCOMPARE(selected.at(0).at(0).toString(), QStringLiteral("STREAM"));
QVariantMap documentStation = rawStation;
documentStation.insert(QStringLiteral("id"), QStringLiteral("DOCUMENT"));
view.setEditorObjects({documentStation}, {});
click(view, QPoint(212, 312));
QCOMPARE(selected.count(), 2);
QCOMPARE(selected.at(1).at(0).toString(), QStringLiteral("DOCUMENT"));
view.setEditorObjects({}, {});
click(view, QPoint(212, 312));
QCOMPARE(selected.count(), 3);
QCOMPARE(selected.at(2).at(0).toString(), QStringLiteral("STREAM"));
// Deleting the final station leaves an authoritative empty document;
// stale preview metadata must not bring the deleted station back.
const QImage beforeDeletion = view.grab().toImage();
view.setEditorObjects({}, {}, true);
click(view, QPoint(212, 312));
QCOMPARE(selected.count(), 3);
QVERIFY(view.grab().toImage() != beforeDeletion);
view.setMapFrame(image, metadata);
click(view, QPoint(212, 312));
QCOMPARE(selected.count(), 3);
view.setEditorObjects({documentStation}, {});
metadata.insert(QStringLiteral("mapId"), QStringLiteral("different-map"));
metadata.remove(QStringLiteral("stations"));
view.setMapFrame(image, metadata);
click(view, QPoint(212, 312));
QCOMPARE(selected.count(), 3);
}
void AgvMapViewWidgetTest::displaysStationNamesWithoutChangingIds()
{
AgvMapViewWidget view;
prepareView(view);
const QString sharedName = QStringLiteral("同名站点");
QVariantMap first{{QStringLiteral("id"), QStringLiteral("A")},
{QStringLiteral("name"), sharedName},
{QStringLiteral("x"), -55.0}, {QStringLiteral("y"), 37.0}};
const QVariantMap second{{QStringLiteral("id"), QStringLiteral("B")},
{QStringLiteral("name"), sharedName},
{QStringLiteral("x"), -55.0}, {QStringLiteral("y"), 77.0}};
view.setEditorObjects({first, second}, {});
view.setEditMode(AgvMapViewWidget::AddRouteMode);
QSignalSpy selected(&view, &AgvMapViewWidget::stationSelected);
click(view, QPoint(212, 312));
click(view, QPoint(412, 312));
QCOMPARE(selected.count(), 2);
QCOMPARE(selected.at(0).at(0).toString(), QStringLiteral("A"));
QCOMPARE(selected.at(1).at(0).toString(), QStringLiteral("B"));
for (const auto &station : {QPair<QPoint, QString>{QPoint(212, 312), QStringLiteral("A")},
QPair<QPoint, QString>{QPoint(412, 312), QStringLiteral("B")}}) {
QHelpEvent help(QEvent::ToolTip, station.first, view.mapToGlobal(station.first));
QApplication::sendEvent(&view, &help);
QVERIFY(QToolTip::text().contains(sharedName));
QVERIFY(QToolTip::text().contains(QStringLiteral("ID: ") + station.second));
QToolTip::hideText();
}
view.setSelectedStation(QString());
view.setEditorObjects({first}, {});
const QImage named = view.grab().toImage();
first.remove(QStringLiteral("name"));
view.setEditorObjects({first}, {});
const QImage fallback = view.grab().toImage();
QVERIFY(named != fallback);
first.insert(QStringLiteral("name"), QStringLiteral(" A "));
view.setEditorObjects({first}, {});
QCOMPARE(view.grab().toImage(), fallback);
first.insert(QStringLiteral("name"), QStringLiteral(" \t "));
view.setEditorObjects({first}, {});
QCOMPARE(view.grab().toImage(), fallback);
}
void AgvMapViewWidgetTest::elidesLongStationNames()
{
AgvMapViewWidget view;
prepareView(view);
QVariantMap station{{QStringLiteral("id"), QStringLiteral("A")},
{QStringLiteral("x"), -55.0}, {QStringLiteral("y"), 37.0}};
view.setEditorObjects({station}, {});
const QImage fallback = view.grab().toImage();
const QString name = QStringLiteral("装配工位名称非常长 ").repeated(20);
station.insert(QStringLiteral("name"), name);
view.setEditorObjects({station}, {});
const QImage rendered = view.grab().toImage();
QRect changed;
for (int y = 0; y < rendered.height(); ++y) {
for (int x = 0; x < rendered.width(); ++x) {
if (rendered.pixel(x, y) != fallback.pixel(x, y))
changed = changed.united(QRect(x, y, 1, 1));
}
}
QVERIFY(changed.width() > 100);
QVERIFY(changed.width() <= 278);
QVERIFY(changed.height() <= 28);
QHelpEvent help(QEvent::ToolTip, QPoint(212, 312), view.mapToGlobal(QPoint(212, 312)));
QApplication::sendEvent(&view, &help);
QVERIFY(QToolTip::text().contains(name.trimmed()));
QVERIFY(QToolTip::text().contains(QStringLiteral("ID: A")));
QToolTip::hideText();
QVERIFY(rendered.save(QStringLiteral("agvmapviewwidget-station-name-preview.png")));
}
QTEST_MAIN(AgvMapViewWidgetTest)
#include "agvmapviewwidget_test.moc"