Files
qelectrotech-source-mirror/tests/qttest/tst_qeticons.cpp
T
Jeff Patterson 8ef130a59d Add a "qet" icon theme and load icons by name
QET had no icon theme: the 446 entries of the icon table and the 116
iconsets in .ui files each named a resource path, so an icon could only
ever be one file, and a variant for another palette or a vector source
had nowhere to go (GitHub #466, #690, #870). This adds the theme layout
without changing a single pixel; a dark variant comes in a follow-up.

The theme "qet" follows the freedesktop layout Qt's icon loader
understands. misc/make_icon_themes.py generates ico/icon-themes.qrc,
which aliases the existing ico/<size>/<name>.png files into
themes/qet/<size>/<name>.png, and ico/themes/qet/index.theme. No file
moves. The four table entries that paired a 16 pixel file with a 22
pixel file of another name (ConductorSettings, DiagramAdd,
DiagramDelete, DialogInformation) get the 22 pixel file aliased under
the 16 pixel name.

QETApp::initIconTheme() registers the theme before initIcons() and makes
it current on every platform, so a desktop icon theme cannot replace
QET's icons. Icons are then looked up by name: QIcon::fromTheme() in
qeticons.cpp and in the few places that built a QIcon from a resource
path directly, and theme="..." on the iconsets in .ui files, with the
resource path kept as fallback. Flags, color swatches, application and
MIME icons stay on their paths.

One entry does not go through the theme. The elements panel draws the
project root with ProjectFileGP in the 50 pixel slot it reserves for
element previews, and the name "project" also carries the 128 pixel
file the configuration dialog uses. On a 2x display Qt's loader picks
that file for a 50 pixel request and fills the slot. ProjectFileGP
loads the 16 and 22 pixel files directly, as before.

tests/qttest/tst_qeticons: every name in the theme resolves, the four
aliases resolve at 22 pixels, a Fusion tool button shows its icon at
3:1 with disabled weaker than enabled, and the project root icon stays
at 22 pixels or less when asked for 50 at 2x while the configuration
dialog still gets its 128 pixel file. The rendering helpers shared
with tst_qetpalette moved to tests/qttest/inkcontrast.h.
2026-09-17 05:41:50 -05:00

167 lines
5.5 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 <QtTest>
#include <QApplication>
#include <QDir>
#include <QIcon>
#include <QMainWindow>
#include <QStyleFactory>
#include <QToolBar>
#include <QToolButton>
#include "inkcontrast.h"
#include "qeticons.h"
#include "qetpalette.h"
/**
Checks on QET's icon theme "qet", generated by misc/make_icon_themes.py
into ico/icon-themes.qrc, and on the icon table that uses it.
Every icon name must resolve. A toolbar button painted with Fusion must
show its icon at 3:1 (WCAG 1.4.11), with the disabled state reading
weaker than the enabled one. And the icons the elements panel draws in
its 50 px slots must stay small (GitHub #870).
*/
class tst_qeticons : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void everyIconResolves();
void toolbarIconIsReadable_data();
void toolbarIconIsReadable();
void panelProjectIconStaysSmall();
};
namespace {
const double kIconRatio = 3.0;
QStringList iconNames(const QString &theme)
{
QStringList names;
for (const QString &size : {"16x16", "22x22", "32x32", "48x48", "128x128", "scalable"})
{
QDir dir(QString(":/ico/themes/%1/%2").arg(theme, size));
for (const QString &file : dir.entryList(QDir::Files))
names << file.section('.', 0, -2);
}
names.removeDuplicates();
names.sort();
return names;
}
}
void tst_qeticons::initTestCase()
{
QStringList paths = QIcon::themeSearchPaths();
paths.prepend(QStringLiteral(":/ico/themes"));
QIcon::setThemeSearchPaths(paths);
QIcon::setThemeName(QStringLiteral("qet"));
QVERIFY2(!iconNames("qet").isEmpty(), "theme has no icons: is ico/icon-themes.qrc compiled in?");
}
void tst_qeticons::everyIconResolves()
{
QVERIFY(QIcon::hasThemeIcon("list-add"));
for (const QString &name : iconNames("qet"))
QVERIFY2(!QIcon::fromTheme(name).isNull(), qPrintable(QString("%1 missing").arg(name)));
// Names the icon table relies on that come from the alias list in
// misc/make_icon_themes.py, not from a file of that name at 22 pixels.
for (const QString &name : {"folio-new", "folio-delete", "folio-properties", "conductor-reset"})
QVERIFY2(!QIcon::fromTheme(name).pixmap(22).isNull(), qPrintable(name));
}
void tst_qeticons::toolbarIconIsReadable_data()
{
QTest::addColumn<QPalette>("palette");
QTest::newRow("light") << QET::Palette::fusionLight();
}
/**
A line-art icon on a Fusion tool button, as in the diagram editor
toolbar. Enabled must reach 3:1; disabled must be weaker than enabled.
*/
void tst_qeticons::toolbarIconIsReadable()
{
QFETCH(QPalette, palette);
QApplication::setStyle(QStyleFactory::create("Fusion"));
QApplication::setPalette(palette);
QMainWindow window;
QToolBar *toolbar = window.addToolBar("view");
auto make = [&](const char *name, bool enabled) {
auto *button = new QToolButton;
button->setIcon(QIcon::fromTheme("zoom-fit-best"));
button->setAutoRaise(true);
button->setEnabled(enabled);
button->setObjectName(name);
toolbar->addWidget(button);
return button;
};
QToolButton *enabled = make("enabled", true);
QToolButton *disabled = make("disabled", false);
window.resize(300, 100);
window.show();
QVERIFY(QTest::qWaitForWindowExposed(&window));
const double enabled_contrast = QET::Test::inkContrast(QET::Test::grab(enabled, "enabled"), enabled->rect());
const double disabled_contrast = QET::Test::inkContrast(QET::Test::grab(disabled, "disabled"), disabled->rect());
QVERIFY2(enabled_contrast >= kIconRatio,
qPrintable(QString("enabled icon: %1").arg(enabled_contrast)));
QVERIFY2(disabled_contrast < enabled_contrast,
qPrintable(QString("disabled %1 reads better than enabled %2")
.arg(disabled_contrast).arg(enabled_contrast)));
}
/**
The elements panel sets a 50 px icon size so element previews are
large. Its project root item uses ProjectFileGP, whose theme name also
has a 128 px file for the configuration dialog. On a 2x display the
theme loader would pick that file for a 50 px request and fill the
slot, so the entry must not resolve through the theme name.
*/
void tst_qeticons::panelProjectIconStaysSmall()
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QSKIP("QIcon::pixmap(size, devicePixelRatio) needs Qt 6");
#else
QET::Icons::initIcons();
const QPixmap panel = QET::Icons::ProjectFileGP.pixmap(QSize(50, 50), 2.0);
QVERIFY(!panel.isNull());
const int logical = qRound(panel.width() / panel.devicePixelRatio());
QVERIFY2(logical <= 22, qPrintable(QString("project root icon is %1 px in a 50 px slot").arg(logical)));
// The configuration dialog's page list still gets the large file.
QCOMPARE(QET::Icons::Projects.pixmap(QSize(128, 128), 1.0).width(), 128);
#endif
}
int main(int argc, char **argv)
{
if (qEnvironmentVariableIsEmpty("QT_QPA_PLATFORM"))
qputenv("QT_QPA_PLATFORM", "offscreen");
QApplication app(argc, argv);
tst_qeticons test;
QTEST_SET_MAIN_SOURCE_PATH
return QTest::qExec(&test, argc, argv);
}
#include "tst_qeticons.moc"