Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
dialoggui.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant reason:default
4
5#include "dialoggui_p.h"
6
7#include <QtWidgets/qfileiconprovider.h>
8#include <QtGui/qicon.h>
9#include <QtGui/qimage.h>
10#include <QtGui/qimagereader.h>
11#include <QtGui/qpixmap.h>
12
13#include <QtCore/qfileinfo.h>
14#include <QtCore/qfile.h>
15#include <QtCore/qset.h>
16
17// QFileDialog on X11 does not provide an image preview. Display icons.
18#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
19# define IMAGE_PREVIEW
20#endif
21
23
24namespace qdesigner_internal {
25
26// Icon provider that reads out the known image formats
29
30public:
32 QIcon icon (const QFileInfo &info) const override;
33
34 inline bool loadCheck(const QFileInfo &info) const;
35 QImage loadImage(const QString &fileName) const;
36
37private:
38 QSet<QString> m_imageFormats;
39};
40
41IconProvider::IconProvider()
42{
43 // Determine a list of readable extensions (upper and lower case)
44 const auto &fmts = QImageReader::supportedImageFormats();
45 for (const QByteArray &fmt : fmts) {
46 const QString suffix = QString::fromUtf8(fmt);
47 m_imageFormats.insert(suffix.toLower());
48 m_imageFormats.insert(suffix.toUpper());
49 }
50}
51
52// Check by extension and type if this appears to be a loadable image
53bool IconProvider::loadCheck(const QFileInfo &info) const
54{
55 if (info.isFile() && info.isReadable()) {
56 const QString suffix = info.suffix();
57 if (!suffix.isEmpty())
58 return m_imageFormats.contains(suffix);
59 }
60 return false;
61}
62
63QImage IconProvider::loadImage(const QString &fileName) const
64{
65 QFile file(fileName);
66 if (file.open(QIODevice::ReadOnly)) {
67 QImageReader imgReader(&file);
68 if (imgReader.canRead()) {
69 QImage image;
70 if (imgReader.read(&image))
71 return image;
72 }
73 }
74 return QImage();
75}
76
77QIcon IconProvider::icon (const QFileInfo &info) const
78{
79 // Don't get stuck on large images.
80 const qint64 maxSize = 131072;
81 if (loadCheck(info) && info.size() < maxSize) {
82 const QImage image = loadImage(info.absoluteFilePath());
83 if (!image.isNull())
84 return QIcon(QPixmap::fromImage(image, Qt::ThresholdDither|Qt::AutoColor));
85 }
86 return QFileIconProvider::icon(info);
87}
88
89// ---------------- DialogGui
90DialogGui::DialogGui() = default;
91
93{
94 delete m_iconProvider;
95}
96
98{
99 if (!m_iconProvider)
101 return m_iconProvider;
102}
103
128
139
151
156
161
166
171
173{
178}
179
201
219
220}
221
222QT_END_NAMESPACE
QImage loadImage(const QString &fileName) const
Definition dialoggui.cpp:63
QIcon icon(const QFileInfo &info) const override
Returns an icon for the file described by info, using the current icon theme.
Definition dialoggui.cpp:77
bool loadCheck(const QFileInfo &info) const
Definition dialoggui.cpp:53
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.