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
4#include "dialoggui_p.h"
5
6#include <QtWidgets/qfileiconprovider.h>
7#include <QtGui/qicon.h>
8#include <QtGui/qimage.h>
9#include <QtGui/qimagereader.h>
10#include <QtGui/qpixmap.h>
11
12#include <QtCore/qfileinfo.h>
13#include <QtCore/qfile.h>
14#include <QtCore/qset.h>
15
16// QFileDialog on X11 does not provide an image preview. Display icons.
17#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
18# define IMAGE_PREVIEW
19#endif
20
22
23namespace qdesigner_internal {
24
25// Icon provider that reads out the known image formats
28
29public:
31 QIcon icon (const QFileInfo &info) const override;
32
33 inline bool loadCheck(const QFileInfo &info) const;
34 QImage loadImage(const QString &fileName) const;
35
36private:
37 QSet<QString> m_imageFormats;
38};
39
40IconProvider::IconProvider()
41{
42 // Determine a list of readable extensions (upper and lower case)
43 const auto &fmts = QImageReader::supportedImageFormats();
44 for (const QByteArray &fmt : fmts) {
45 const QString suffix = QString::fromUtf8(fmt);
46 m_imageFormats.insert(suffix.toLower());
47 m_imageFormats.insert(suffix.toUpper());
48 }
49}
50
51// Check by extension and type if this appears to be a loadable image
52bool IconProvider::loadCheck(const QFileInfo &info) const
53{
54 if (info.isFile() && info.isReadable()) {
55 const QString suffix = info.suffix();
56 if (!suffix.isEmpty())
57 return m_imageFormats.contains(suffix);
58 }
59 return false;
60}
61
62QImage IconProvider::loadImage(const QString &fileName) const
63{
64 QFile file(fileName);
65 if (file.open(QIODevice::ReadOnly)) {
66 QImageReader imgReader(&file);
67 if (imgReader.canRead()) {
68 QImage image;
69 if (imgReader.read(&image))
70 return image;
71 }
72 }
73 return QImage();
74}
75
76QIcon IconProvider::icon (const QFileInfo &info) const
77{
78 // Don't get stuck on large images.
79 const qint64 maxSize = 131072;
80 if (loadCheck(info) && info.size() < maxSize) {
81 const QImage image = loadImage(info.absoluteFilePath());
82 if (!image.isNull())
83 return QIcon(QPixmap::fromImage(image, Qt::ThresholdDither|Qt::AutoColor));
84 }
85 return QFileIconProvider::icon(info);
86}
87
88// ---------------- DialogGui
89DialogGui::DialogGui() = default;
90
92{
93 delete m_iconProvider;
94}
95
97{
98 if (!m_iconProvider)
100 return m_iconProvider;
101}
102
127
138
150
155
160
165
170
172{
177}
178
200
218
219}
220
221QT_END_NAMESPACE
QImage loadImage(const QString &fileName) const
Definition dialoggui.cpp:62
QIcon icon(const QFileInfo &info) const override
Returns an icon for the file described by info, using the current icon theme.
Definition dialoggui.cpp:76
bool loadCheck(const QFileInfo &info) const
Definition dialoggui.cpp:52
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.