Debian Codegraph @

src/util/dicon.cpp

raw
1 // SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dicon.h"
6 #include <QGuiApplication>
7 #include <private/qicon_p.h>
8 #include <QImageReader>
9
10 DGUI_BEGIN_NAMESPACE
11 DIcon::DIcon(const QIcon &other)
12 : QIcon(other)
13 {
14
15 }
16
17 DIcon::~DIcon()
18 {
19
20 }
21
22 /*! Icon::pixmap
23 * Returns a pixmap with the requested \a size, \a devicePixelRatio, \a mode, and \a state, generating one if necessary.
24 *
25 * \sa QIcon::actualsize(), QIcon::paint()
26 */
27 QPixmap DIcon::pixmap(const QSize &size, qreal devicePixelRatio, QIcon::Mode mode, QIcon::State state)
28 {
29 DataPtr d = data_ptr();
30 if (!d)
31 return QPixmap();
32
33 if (qFuzzyCompare(devicePixelRatio, -1))
34 devicePixelRatio = qApp->devicePixelRatio();
35
36 if (!(devicePixelRatio > 1.0)) {
37 QPixmap pixmap = d->engine->pixmap(size, mode, state);
38 pixmap.setDevicePixelRatio(1.0);
39 return pixmap;
40 }
41
42 QPixmap pixmap = d->engine->scaledPixmap(size * devicePixelRatio, mode, state, devicePixelRatio);
43
44 auto pixmapDevicePixelRatio = [](qreal displayDevicePixelRatio,
45 const QSize &requestedSize, const QSize &actualSize)->qreal {
46 QSize targetSize = requestedSize * displayDevicePixelRatio;
47 if ((actualSize.width() == targetSize.width() && actualSize.height() <= targetSize.height()) ||
48 (actualSize.width() <= targetSize.width() && actualSize.height() == targetSize.height())) {
49 return displayDevicePixelRatio;
50 }
51 qreal scale = 0.5 * (qreal(actualSize.width()) / qreal(targetSize.width()) +
52 qreal(actualSize.height() / qreal(targetSize.height())));
53 return qMax(qreal(1.0), displayDevicePixelRatio * scale);
54 };
55
56 pixmap.setDevicePixelRatio(pixmapDevicePixelRatio(devicePixelRatio, size, pixmap.size()));
57 return pixmap;
58 }
59
60 /*!
61 * @brief DIcon::loadNxPixmap loads the suitable @Nx image.
62 * @param fileName is the original resource file name.
63 * @return the hiDPI ready QPixmap.
64 */
65 QPixmap DIcon::loadNxPixmap(const QString &fileName)
66 {
67 qreal sourceDevicePixelRatio = 1.0;
68 qreal devicePixelRatio = qApp->devicePixelRatio();
69 QPixmap pixmap;
70
71 if (!qFuzzyCompare(sourceDevicePixelRatio, devicePixelRatio)) {
72 QImageReader reader;
73 reader.setFileName(qt_findAtNxFile(fileName, devicePixelRatio, &sourceDevicePixelRatio));
74 if (reader.canRead()) {
75 reader.setScaledSize(reader.size() * (devicePixelRatio / sourceDevicePixelRatio));
76 pixmap = QPixmap::fromImage(reader.read());
77 pixmap.setDevicePixelRatio(devicePixelRatio);
78 }
79 } else {
80 pixmap.load(fileName);
81 }
82
83 return pixmap;
84 }
85 DGUI_END_NAMESPACE