| 1 | // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-only |
| 4 | |
| 5 | #include "widgetutil.h" |
| 6 | |
| 7 | #include <DApplication> |
| 8 | |
| 9 | #include <QFile> |
| 10 | #include <QImageReader> |
| 11 | #include <QIcon> |
| 12 | |
| 13 | namespace WidgetUtil { |
| 14 | QString getQss(const QString &qssFilename) |
| 15 | { |
| 16 | QString qss; |
| 17 | |
| 18 | QFile themeFile(qssFilename); |
| 19 | |
| 20 | if (themeFile.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 21 | qss = themeFile.readAll(); |
| 22 | |
| 23 | themeFile.close(); |
| 24 | } |
| 25 | |
| 26 | return qss; |
| 27 | } |
| 28 | |
| 29 | QPixmap getDpiPixmap(const QString filename, DWidget *w) |
| 30 | { |
| 31 | QPixmap pixmap; |
| 32 | qreal devicePixelRatio = qApp->devicePixelRatio(); |
| 33 | if (w) { |
| 34 | devicePixelRatio = w->devicePixelRatioF(); |
| 35 | } |
| 36 | |
| 37 | qreal ratio = 1.0; |
| 38 | if (!qFuzzyCompare(ratio, devicePixelRatio)) { |
| 39 | QImageReader reader; |
| 40 | reader.setFileName(qt_findAtNxFile(filename, devicePixelRatio, &ratio)); |
| 41 | if (reader.canRead()) { |
| 42 | reader.setScaledSize(reader.size() * (devicePixelRatio / ratio)); |
| 43 | pixmap = QPixmap::fromImage(reader.read()); |
| 44 | pixmap.setDevicePixelRatio(devicePixelRatio); |
| 45 | } |
| 46 | } else { |
| 47 | pixmap.load(filename); |
| 48 | } |
| 49 | |
| 50 | return pixmap; |
| 51 | } |
| 52 | } |