Files
qelectrotech-source-mirror/sources/foliogrid.h
T
Beat Hangartner ede7bd99e1 Never use a folio grid step below 1 from the settings
The folio grid steps, diagrameditor/Xgrid and Ygrid, are read from the
settings in six places with a plain toInt() and used as they come. The
preferences page cannot store anything below 1, but a hand-edited or
damaged settings file can hold 0, a negative number or text:

- Diagram::snapToGrid() divides by the step, so 0 is a SIGFPE on the
  first click that places a symbol;
- Diagram::drawBackground() runs "while (g_x % xGrid)" and then loops
  "gx += xGrid", so 0 crashes every repaint and a negative step never
  ends;
- the paste, duplicate and align paths divide by it or multiply with it.

Add foliogrid.h, a header-only helper: FolioGrid::step() reads a
settings entry and returns the built-in step (Diagram::xGrid, 10) when
the entry is missing, not a number, below 1 or above what an int
holds (QVariant::toInt() wraps such a value around). Every reader of
the two keys goes through it; the settings page, which only writes the
spin box values, is unchanged.

No file-format change, and no change for any step the preferences page
can produce.

Tests: tst_foliogrid covers the helper: 1, 10, "7", int max are kept;
missing, 0, -5, "ten", "", "nan", 99999999999 (as text and as a
number) and int max + 1 fall back; 7.9 rounds to 8 as before; and the
two settings keys through a QSettings scope of the test's own. The
helper is new, so the test cannot fail on master; the six readers are
the replacements in the diff.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: Beat Hangartner <beat@hangartners.ch>
2026-10-08 06:04:35 +02:00

55 lines
1.8 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/>.
*/
#ifndef FOLIOGRID_H
#define FOLIOGRID_H
#include <QString>
#include <QVariant>
#include <limits>
/**
The folio grid: the step, in pixels, that symbols snap to. Both steps
are kept in the settings; the preferences page only offers 1 and up,
but a hand-edited or damaged settings file can hold 0, a negative
number or text, and the code that divides by the step or loops over it
must never see such a value.
*/
namespace FolioGrid
{
/// QSettings keys holding the two steps.
inline const QString x_key{QStringLiteral("diagrameditor/Xgrid")};
inline const QString y_key{QStringLiteral("diagrameditor/Ygrid")};
/**
@return the step held in @p value, a settings entry; or @p fallback,
the built-in step, when the entry is missing, not a number, less
than 1, or more than an int holds (QVariant::toInt() wraps such a
number around instead of failing).
*/
inline int step(const QVariant &value, int fallback)
{
bool ok = false;
const qlonglong s = value.toLongLong(&ok);
if (!ok || s < 1 || s > std::numeric_limits<int>::max())
return fallback;
return int(s);
}
}
#endif // FOLIOGRID_H