Files
qelectrotech-source-mirror/tests/qttest/tst_foliogrid.cpp
T
DaTweety 62b37bd752 Grid: 7.5 px by default, and only 1 to 50 px from the settings
With 10 px and the default step, the grid went below 100 %, including
the zoom of a freshly fitted folio. 7.5 px keeps it down to 75 %: on a
24" 1920x1200 screen 5 px (the old cutoff at 50 %) reads as a gray
haze, 7.5 px still as dots.

minDotSpacing() now takes only what the preferences page offers, 1 to
50 px, both kept as constants the page uses for its range: "inf", NaN,
1e9 or 0.5 from a hand-edited settings file fall back to the default
instead of hiding the grid for good while the page shows another value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 22:00:56 +01:00

135 lines
5.0 KiB
C++

/*
Copyright 2006-2026 The QElectroTech Team
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "foliogrid.h"
#include <QtTest>
#include <QSettings>
/**
FolioGrid::step() -- the folio grid step as read from the settings.
Every reader of diagrameditor/Xgrid and Ygrid goes through it, so a
settings file holding 0 (a division by zero in Diagram::snapToGrid()
and an endless "% 0" in drawBackground()), a negative number (an
endless loop) or text can no longer reach the code that uses the step.
*/
class tst_foliogrid : public QObject
{
Q_OBJECT
private slots:
void dotsGoWhenTheyCrowd()
{
//The default step and spacing: the dots go below 75 %
QVERIFY(FolioGrid::dotsShown(10, 10, 0.75));
QVERIFY(!FolioGrid::dotsShown(10, 10, 0.74));
//A finer step goes sooner, a coarser one later; the finer of
//the two directions decides
QVERIFY(!FolioGrid::dotsShown(5, 5, 1.4));
QVERIFY(FolioGrid::dotsShown(20, 20, 0.4));
QVERIFY(!FolioGrid::dotsShown(20, 5, 1.0));
//A spacing set in the settings: 7.8 px keeps them down to 78 %
QVERIFY(FolioGrid::dotsShown(10, 10, 0.78, 7.8));
QVERIFY(!FolioGrid::dotsShown(10, 10, 0.77, 7.8));
}
void dotSpacingFromSettings()
{
QCOMPARE(FolioGrid::minDotSpacing(QVariant()), 7.5);
QCOMPARE(FolioGrid::minDotSpacing(QVariant(7.8)), 7.8);
QCOMPARE(FolioGrid::minDotSpacing(QVariant(QStringLiteral("12"))), 12.0);
QCOMPARE(FolioGrid::minDotSpacing(QVariant(0)), 7.5);
QCOMPARE(FolioGrid::minDotSpacing(QVariant(-3)), 7.5);
QCOMPARE(FolioGrid::minDotSpacing(QVariant(QStringLiteral("ten"))), 7.5);
//Only what the preferences page offers, 1 to 50 px: no
//endless or huge value hides the grid for good
QCOMPARE(FolioGrid::minDotSpacing(QVariant(1.0)), 1.0);
QCOMPARE(FolioGrid::minDotSpacing(QVariant(50.0)), 50.0);
QCOMPARE(FolioGrid::minDotSpacing(QVariant(0.5)), 7.5);
QCOMPARE(FolioGrid::minDotSpacing(QVariant(50.5)), 7.5);
QCOMPARE(FolioGrid::minDotSpacing(QVariant(1e9)), 7.5);
QCOMPARE(FolioGrid::minDotSpacing(QVariant(QStringLiteral("inf"))), 7.5);
QCOMPARE(FolioGrid::minDotSpacing(QVariant(std::numeric_limits<double>::infinity())), 7.5);
QCOMPARE(FolioGrid::minDotSpacing(QVariant(std::numeric_limits<double>::quiet_NaN())), 7.5);
}
void usableValuesAreKept_data()
{
QTest::addColumn<QVariant>("value");
QTest::addColumn<int>("expected");
QTest::newRow("one") << QVariant(1) << 1;
QTest::newRow("default") << QVariant(10) << 10;
QTest::newRow("text number") << QVariant(QStringLiteral("7")) << 7;
QTest::newRow("large") << QVariant(100000) << 100000;
QTest::newRow("int max") << QVariant(2147483647) << 2147483647;
}
void usableValuesAreKept()
{
QFETCH(QVariant, value);
QFETCH(int, expected);
QCOMPARE(FolioGrid::step(value, 10), expected);
}
void unusableValuesFallBack_data()
{
QTest::addColumn<QVariant>("value");
QTest::newRow("missing") << QVariant();
QTest::newRow("zero") << QVariant(0);
QTest::newRow("negative") << QVariant(-5);
QTest::newRow("text") << QVariant(QStringLiteral("ten"));
QTest::newRow("empty") << QVariant(QString());
QTest::newRow("nan") << QVariant(QStringLiteral("nan"));
QTest::newRow("too large") << QVariant(QStringLiteral("99999999999"));
QTest::newRow("too large number") << QVariant(qlonglong(99999999999));
QTest::newRow("int max + 1") << QVariant(qlonglong(2147483648));
}
void unusableValuesFallBack()
{
QFETCH(QVariant, value);
QCOMPARE(FolioGrid::step(value, 10), 10);
QCOMPARE(FolioGrid::step(value, 20), 20);
}
// A decimal in the file is rounded to a whole number, as the plain
// QVariant::toInt() read did before.
void decimalIsRoundedLikeBefore()
{
QCOMPARE(FolioGrid::step(QVariant(7.9), 10), 8);
QCOMPARE(FolioGrid::step(QVariant(0.4), 10), 10);
}
// Through a real settings file, in a scope of this test's own.
void readsTheSettingsKeys()
{
QCoreApplication::setOrganizationName(QStringLiteral("QElectroTech-tst_foliogrid"));
QCoreApplication::setApplicationName(QStringLiteral("tst_foliogrid"));
QSettings settings;
settings.clear();
QCOMPARE(FolioGrid::step(settings.value(FolioGrid::x_key), 10), 10);
settings.setValue(FolioGrid::x_key, 0);
settings.setValue(FolioGrid::y_key, 15);
QCOMPARE(FolioGrid::step(settings.value(FolioGrid::x_key), 10), 10);
QCOMPARE(FolioGrid::step(settings.value(FolioGrid::y_key), 10), 15);
settings.clear();
}
};
QTEST_GUILESS_MAIN(tst_foliogrid)
#include "tst_foliogrid.moc"