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
21
QT_BEGIN_NAMESPACE
22
23
namespace
qdesigner_internal
{
24
25
// Icon provider that reads out the known image formats
26
class
IconProvider
:
public
QFileIconProvider
{
27
Q_DISABLE_COPY_MOVE
(
IconProvider
)
28
29
public
:
30
IconProvider
();
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
36
private
:
37
QSet<QString> m_imageFormats;
38
};
39
40
IconProvider
::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
52
bool
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
62
QImage
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
76
QIcon
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
89
DialogGui
::
DialogGui
() =
default
;
90
91
DialogGui
::~
DialogGui
()
92
{
93
delete
m_iconProvider
;
94
}
95
96
QFileIconProvider
*
DialogGui
::
ensureIconProvider
()
97
{
98
if
(!
m_iconProvider
)
99
m_iconProvider
=
new
IconProvider
;
100
return
m_iconProvider
;
101
}
102
103
QMessageBox
::
StandardButton
104
DialogGui
::
message
(
QWidget
*
parent
,
Message
/*context*/
,
QMessageBox
::
Icon
icon
,
105
const
QString
&
title
,
const
QString
&
text
,
QMessageBox
::
StandardButtons
buttons
,
106
QMessageBox
::
StandardButton
defaultButton
)
107
{
108
QMessageBox
::
StandardButton
rc
=
QMessageBox
::
NoButton
;
109
switch
(
icon
) {
110
case
QMessageBox
::
Information
:
111
rc
=
QMessageBox
::
information
(
parent
,
title
,
text
,
buttons
,
defaultButton
);
112
break
;
113
case
QMessageBox
::
Warning
:
114
rc
=
QMessageBox
::
warning
(
parent
,
title
,
text
,
buttons
,
defaultButton
);
115
break
;
116
case
QMessageBox
::
Critical
:
117
rc
=
QMessageBox
::
critical
(
parent
,
title
,
text
,
buttons
,
defaultButton
);
118
break
;
119
case
QMessageBox
::
Question
:
120
rc
=
QMessageBox
::
question
(
parent
,
title
,
text
,
buttons
,
defaultButton
);
121
break
;
122
case
QMessageBox
::
NoIcon
:
123
break
;
124
}
125
return
rc
;
126
}
127
128
QMessageBox
::
StandardButton
129
DialogGui
::
message
(
QWidget
*
parent
,
Message
/*context*/
,
QMessageBox
::
Icon
icon
,
130
const
QString
&
title
,
const
QString
&
text
,
const
QString
&
informativeText
,
131
QMessageBox
::
StandardButtons
buttons
,
QMessageBox
::
StandardButton
defaultButton
)
132
{
133
QMessageBox
msgBox
(
icon
,
title
,
text
,
buttons
,
parent
);
134
msgBox
.
setDefaultButton
(
defaultButton
);
135
msgBox
.
setInformativeText
(
informativeText
);
136
return
static_cast
<
QMessageBox
::
StandardButton
>(
msgBox
.
exec
());
137
}
138
139
QMessageBox
::
StandardButton
140
DialogGui
::
message
(
QWidget
*
parent
,
Message
/*context*/
,
QMessageBox
::
Icon
icon
,
141
const
QString
&
title
,
const
QString
&
text
,
const
QString
&
informativeText
,
const
QString
&
detailedText
,
142
QMessageBox
::
StandardButtons
buttons
,
QMessageBox
::
StandardButton
defaultButton
)
143
{
144
QMessageBox
msgBox
(
icon
,
title
,
text
,
buttons
,
parent
);
145
msgBox
.
setDefaultButton
(
defaultButton
);
146
msgBox
.
setInformativeText
(
informativeText
);
147
msgBox
.
setDetailedText
(
detailedText
);
148
return
static_cast
<
QMessageBox
::
StandardButton
>(
msgBox
.
exec
());
149
}
150
151
QString
DialogGui
::
getExistingDirectory
(
QWidget
*
parent
,
const
QString
&
caption
,
const
QString
&
dir
,
QFileDialog
::
Options
options
)
152
{
153
return
QFileDialog
::
getExistingDirectory
(
parent
,
caption
,
dir
,
options
);
154
}
155
156
QString
DialogGui
::
getOpenFileName
(
QWidget
*
parent
,
const
QString
&
caption
,
const
QString
&
dir
,
const
QString
&
filter
,
QString
*
selectedFilter
,
QFileDialog
::
Options
options
)
157
{
158
return
QFileDialog
::
getOpenFileName
(
parent
,
caption
,
dir
,
filter
,
selectedFilter
,
options
);
159
}
160
161
QStringList
DialogGui
::
getOpenFileNames
(
QWidget
*
parent
,
const
QString
&
caption
,
const
QString
&
dir
,
const
QString
&
filter
,
QString
*
selectedFilter
,
QFileDialog
::
Options
options
)
162
{
163
return
QFileDialog
::
getOpenFileNames
(
parent
,
caption
,
dir
,
filter
,
selectedFilter
,
options
);
164
}
165
166
QString
DialogGui
::
getSaveFileName
(
QWidget
*
parent
,
const
QString
&
caption
,
const
QString
&
dir
,
const
QString
&
filter
,
QString
*
selectedFilter
,
QFileDialog
::
Options
options
)
167
{
168
return
QFileDialog
::
getSaveFileName
(
parent
,
caption
,
dir
,
filter
,
selectedFilter
,
options
);
169
}
170
171
void
DialogGui
::
initializeImageFileDialog
(
QFileDialog
&
fileDialog
,
QFileDialog
::
Options
options
,
QFileDialog
::
FileMode
fm
)
172
{
173
fileDialog
.
setOption
(
QFileDialog
::
DontConfirmOverwrite
,
options
.
testFlag
(
QFileDialog
::
DontConfirmOverwrite
));
174
fileDialog
.
setOption
(
QFileDialog
::
DontResolveSymlinks
,
options
.
testFlag
(
QFileDialog
::
DontResolveSymlinks
));
175
fileDialog
.
setIconProvider
(
ensureIconProvider
());
176
fileDialog
.
setFileMode
(
fm
);
177
}
178
179
QString
DialogGui
::
getOpenImageFileName
(
QWidget
*
parent
,
const
QString
&
caption
,
const
QString
&
dir
,
const
QString
&
filter
,
QString
*
selectedFilter
,
QFileDialog
::
Options
options
)
180
{
181
182
#
ifdef
IMAGE_PREVIEW
183
QFileDialog
fileDialog
(
parent
,
caption
,
dir
,
filter
);
184
initializeImageFileDialog
(
fileDialog
,
options
,
QFileDialog
::
ExistingFile
);
185
if
(
fileDialog
.
exec
() !=
QDialog
::
Accepted
)
186
return
QString
();
187
188
const
QStringList
selectedFiles
=
fileDialog
.
selectedFiles
();
189
if
(
selectedFiles
.
isEmpty
())
190
return
QString
();
191
192
if
(
selectedFilter
)
193
*
selectedFilter
=
fileDialog
.
selectedNameFilter
();
194
195
return
selectedFiles
.
constFirst
();
196
#
else
197
return
getOpenFileName
(
parent
,
caption
,
dir
,
filter
,
selectedFilter
,
options
);
198
#
endif
199
}
200
201
QStringList
DialogGui
::
getOpenImageFileNames
(
QWidget
*
parent
,
const
QString
&
caption
,
const
QString
&
dir
,
const
QString
&
filter
,
QString
*
selectedFilter
,
QFileDialog
::
Options
options
)
202
{
203
#
ifdef
IMAGE_PREVIEW
204
QFileDialog
fileDialog
(
parent
,
caption
,
dir
,
filter
);
205
initializeImageFileDialog
(
fileDialog
,
options
,
QFileDialog
::
ExistingFiles
);
206
if
(
fileDialog
.
exec
() !=
QDialog
::
Accepted
)
207
return
QStringList
();
208
209
const
QStringList
selectedFiles
=
fileDialog
.
selectedFiles
();
210
if
(!
selectedFiles
.
isEmpty
() &&
selectedFilter
)
211
*
selectedFilter
=
fileDialog
.
selectedNameFilter
();
212
213
return
selectedFiles
;
214
#
else
215
return
getOpenFileNames
(
parent
,
caption
,
dir
,
filter
,
selectedFilter
,
options
);
216
#
endif
217
}
218
219
}
220
221
QT_END_NAMESPACE
qdesigner_internal::IconProvider
Definition
dialoggui.cpp:26
qdesigner_internal::IconProvider::loadImage
QImage loadImage(const QString &fileName) const
Definition
dialoggui.cpp:62
qdesigner_internal::IconProvider::icon
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
qdesigner_internal::IconProvider::loadCheck
bool loadCheck(const QFileInfo &info) const
Definition
dialoggui.cpp:52
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qstandardpaths_haiku.cpp:21
qdesigner_internal
Auxiliary methods to store/retrieve settings.
Definition
buddyeditor.cpp:66
qttools
src
designer
src
lib
shared
dialoggui.cpp
Generated on Sat Sep 21 2024 00:50:31 for Qt by
1.12.0