1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtGui module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qplatformdialoghelper.h" |
41 | |
42 | #include <QtCore/QCoreApplication> |
43 | #include <QtCore/QList> |
44 | #if QT_CONFIG(regularexpression) |
45 | #include <QtCore/QRegularExpression> |
46 | #endif |
47 | #if QT_CONFIG(settings) |
48 | #include <QtCore/QSettings> |
49 | #endif |
50 | #include <QtCore/QSharedData> |
51 | #include <QtCore/QUrl> |
52 | #include <QtCore/QVariant> |
53 | #include <QtGui/QColor> |
54 | |
55 | #include <algorithm> |
56 | |
57 | QT_BEGIN_NAMESPACE |
58 | |
59 | /*! |
60 | \class QPlatformDialogHelper |
61 | \since 5.0 |
62 | \internal |
63 | \ingroup qpa |
64 | |
65 | \brief The QPlatformDialogHelper class allows for platform-specific customization of dialogs. |
66 | |
67 | */ |
68 | |
69 | /*! |
70 | \enum QPlatformDialogHelper::StyleHint |
71 | |
72 | This enum type specifies platform-specific style hints. |
73 | |
74 | \value DialogIsQtWindow Indicates that a platform-specific dialog is implemented |
75 | as in-process Qt window. It allows to prevent blocking the |
76 | dialog by an invisible proxy Qt dialog. |
77 | |
78 | \sa styleHint() |
79 | */ |
80 | |
81 | static const int buttonRoleLayouts[2][6][14] = |
82 | { |
83 | // Qt::Horizontal |
84 | { |
85 | // WinLayout |
86 | { QPlatformDialogHelper::ResetRole, QPlatformDialogHelper::Stretch, QPlatformDialogHelper::YesRole, QPlatformDialogHelper::AcceptRole, |
87 | QPlatformDialogHelper::AlternateRole, QPlatformDialogHelper::DestructiveRole, QPlatformDialogHelper::NoRole, |
88 | QPlatformDialogHelper::ActionRole, QPlatformDialogHelper::RejectRole, QPlatformDialogHelper::ApplyRole, |
89 | QPlatformDialogHelper::HelpRole, QPlatformDialogHelper::EOL, QPlatformDialogHelper::EOL, QPlatformDialogHelper::EOL }, |
90 | |
91 | // MacLayout |
92 | { QPlatformDialogHelper::HelpRole, QPlatformDialogHelper::ResetRole, QPlatformDialogHelper::ApplyRole, QPlatformDialogHelper::ActionRole, |
93 | QPlatformDialogHelper::Stretch, QPlatformDialogHelper::DestructiveRole | QPlatformDialogHelper::Reverse, |
94 | QPlatformDialogHelper::AlternateRole | QPlatformDialogHelper::Reverse, QPlatformDialogHelper::RejectRole | QPlatformDialogHelper::Reverse, |
95 | QPlatformDialogHelper::AcceptRole | QPlatformDialogHelper::Reverse, QPlatformDialogHelper::NoRole | QPlatformDialogHelper::Reverse, |
96 | QPlatformDialogHelper::YesRole | QPlatformDialogHelper::Reverse, QPlatformDialogHelper::EOL, QPlatformDialogHelper::EOL }, |
97 | |
98 | // KdeLayout |
99 | { QPlatformDialogHelper::HelpRole, QPlatformDialogHelper::ResetRole, QPlatformDialogHelper::Stretch, QPlatformDialogHelper::YesRole, |
100 | QPlatformDialogHelper::NoRole, QPlatformDialogHelper::ActionRole, QPlatformDialogHelper::AcceptRole, QPlatformDialogHelper::AlternateRole, |
101 | QPlatformDialogHelper::ApplyRole, QPlatformDialogHelper::DestructiveRole, QPlatformDialogHelper::RejectRole, QPlatformDialogHelper::EOL }, |
102 | |
103 | // GnomeLayout |
104 | { QPlatformDialogHelper::HelpRole, QPlatformDialogHelper::ResetRole, QPlatformDialogHelper::Stretch, QPlatformDialogHelper::ActionRole, |
105 | QPlatformDialogHelper::ApplyRole | QPlatformDialogHelper::Reverse, QPlatformDialogHelper::DestructiveRole | QPlatformDialogHelper::Reverse, |
106 | QPlatformDialogHelper::AlternateRole | QPlatformDialogHelper::Reverse, QPlatformDialogHelper::RejectRole | QPlatformDialogHelper::Reverse, |
107 | QPlatformDialogHelper::AcceptRole | QPlatformDialogHelper::Reverse, QPlatformDialogHelper::NoRole | QPlatformDialogHelper::Reverse, |
108 | QPlatformDialogHelper::YesRole | QPlatformDialogHelper::Reverse, QPlatformDialogHelper::EOL }, |
109 | |
110 | // AndroidLayout (neutral, stretch, dismissive, affirmative) |
111 | // https://material.io/guidelines/components/dialogs.html#dialogs-specs |
112 | { QPlatformDialogHelper::HelpRole, QPlatformDialogHelper::ResetRole, QPlatformDialogHelper::ApplyRole, QPlatformDialogHelper::ActionRole, |
113 | QPlatformDialogHelper::Stretch, QPlatformDialogHelper::RejectRole | QPlatformDialogHelper::Reverse, |
114 | QPlatformDialogHelper::NoRole | QPlatformDialogHelper::Reverse, QPlatformDialogHelper::DestructiveRole | QPlatformDialogHelper::Reverse, |
115 | QPlatformDialogHelper::AlternateRole | QPlatformDialogHelper::Reverse, QPlatformDialogHelper::AcceptRole | QPlatformDialogHelper::Reverse, |
116 | QPlatformDialogHelper::YesRole | QPlatformDialogHelper::Reverse, QPlatformDialogHelper::EOL, QPlatformDialogHelper::EOL } |
117 | }, |
118 | |
119 | // Qt::Vertical |
120 | { |
121 | // WinLayout |
122 | { QPlatformDialogHelper::ActionRole, QPlatformDialogHelper::YesRole, QPlatformDialogHelper::AcceptRole, QPlatformDialogHelper::AlternateRole, |
123 | QPlatformDialogHelper::DestructiveRole, QPlatformDialogHelper::NoRole, QPlatformDialogHelper::RejectRole, QPlatformDialogHelper::ApplyRole, QPlatformDialogHelper::ResetRole, |
124 | QPlatformDialogHelper::HelpRole, QPlatformDialogHelper::Stretch, QPlatformDialogHelper::EOL, QPlatformDialogHelper::EOL, QPlatformDialogHelper::EOL }, |
125 | |
126 | // MacLayout |
127 | { QPlatformDialogHelper::YesRole, QPlatformDialogHelper::NoRole, QPlatformDialogHelper::AcceptRole, QPlatformDialogHelper::RejectRole, |
128 | QPlatformDialogHelper::AlternateRole, QPlatformDialogHelper::DestructiveRole, QPlatformDialogHelper::Stretch, QPlatformDialogHelper::ActionRole, QPlatformDialogHelper::ApplyRole, |
129 | QPlatformDialogHelper::ResetRole, QPlatformDialogHelper::HelpRole, QPlatformDialogHelper::EOL, QPlatformDialogHelper::EOL }, |
130 | |
131 | // KdeLayout |
132 | { QPlatformDialogHelper::AcceptRole, QPlatformDialogHelper::AlternateRole, QPlatformDialogHelper::ApplyRole, QPlatformDialogHelper::ActionRole, |
133 | QPlatformDialogHelper::YesRole, QPlatformDialogHelper::NoRole, QPlatformDialogHelper::Stretch, QPlatformDialogHelper::ResetRole, |
134 | QPlatformDialogHelper::DestructiveRole, QPlatformDialogHelper::RejectRole, QPlatformDialogHelper::HelpRole, QPlatformDialogHelper::EOL }, |
135 | |
136 | // GnomeLayout |
137 | { QPlatformDialogHelper::YesRole, QPlatformDialogHelper::NoRole, QPlatformDialogHelper::AcceptRole, QPlatformDialogHelper::RejectRole, |
138 | QPlatformDialogHelper::AlternateRole, QPlatformDialogHelper::DestructiveRole, QPlatformDialogHelper::ApplyRole, QPlatformDialogHelper::ActionRole, QPlatformDialogHelper::Stretch, |
139 | QPlatformDialogHelper::ResetRole, QPlatformDialogHelper::HelpRole, QPlatformDialogHelper::EOL, QPlatformDialogHelper::EOL, QPlatformDialogHelper::EOL }, |
140 | |
141 | // AndroidLayout |
142 | // (affirmative |
143 | // dismissive |
144 | // neutral) |
145 | // https://material.io/guidelines/components/dialogs.html#dialogs-specs |
146 | { QPlatformDialogHelper::YesRole, QPlatformDialogHelper::AcceptRole, QPlatformDialogHelper::AlternateRole, QPlatformDialogHelper::DestructiveRole, |
147 | QPlatformDialogHelper::NoRole, QPlatformDialogHelper::RejectRole, QPlatformDialogHelper::Stretch, QPlatformDialogHelper::ActionRole, QPlatformDialogHelper::ApplyRole, |
148 | QPlatformDialogHelper::ResetRole, QPlatformDialogHelper::HelpRole, QPlatformDialogHelper::EOL, QPlatformDialogHelper::EOL } |
149 | } |
150 | }; |
151 | |
152 | |
153 | QPlatformDialogHelper::QPlatformDialogHelper() |
154 | { |
155 | qRegisterMetaType<StandardButton>(); |
156 | qRegisterMetaType<ButtonRole>(); |
157 | } |
158 | |
159 | QPlatformDialogHelper::~QPlatformDialogHelper() |
160 | { |
161 | } |
162 | |
163 | QVariant QPlatformDialogHelper::styleHint(StyleHint hint) const |
164 | { |
165 | return QPlatformDialogHelper::defaultStyleHint(hint); |
166 | } |
167 | |
168 | QVariant QPlatformDialogHelper::defaultStyleHint(QPlatformDialogHelper::StyleHint hint) |
169 | { |
170 | Q_UNUSED(hint); |
171 | return QVariant(); |
172 | } |
173 | |
174 | // Font dialog |
175 | |
176 | class QFontDialogOptionsPrivate : public QSharedData |
177 | { |
178 | public: |
179 | QFontDialogOptionsPrivate() = default; |
180 | |
181 | QFontDialogOptions::FontDialogOptions options; |
182 | QString windowTitle; |
183 | }; |
184 | |
185 | QFontDialogOptions::QFontDialogOptions(QFontDialogOptionsPrivate *dd) |
186 | : d(dd) |
187 | { |
188 | } |
189 | |
190 | QFontDialogOptions::~QFontDialogOptions() |
191 | { |
192 | } |
193 | |
194 | namespace { |
195 | struct FontDialogCombined : QFontDialogOptionsPrivate, QFontDialogOptions |
196 | { |
197 | FontDialogCombined() : QFontDialogOptionsPrivate(), QFontDialogOptions(this) {} |
198 | FontDialogCombined(const FontDialogCombined &other) |
199 | : QFontDialogOptionsPrivate(other), QFontDialogOptions(this) {} |
200 | }; |
201 | } |
202 | |
203 | // static |
204 | QSharedPointer<QFontDialogOptions> QFontDialogOptions::create() |
205 | { |
206 | return QSharedPointer<FontDialogCombined>::create(); |
207 | } |
208 | |
209 | QSharedPointer<QFontDialogOptions> QFontDialogOptions::clone() const |
210 | { |
211 | return QSharedPointer<FontDialogCombined>::create(*static_cast<const FontDialogCombined*>(this)); |
212 | } |
213 | |
214 | QString QFontDialogOptions::windowTitle() const |
215 | { |
216 | return d->windowTitle; |
217 | } |
218 | |
219 | void QFontDialogOptions::setWindowTitle(const QString &title) |
220 | { |
221 | d->windowTitle = title; |
222 | } |
223 | |
224 | void QFontDialogOptions::setOption(QFontDialogOptions::FontDialogOption option, bool on) |
225 | { |
226 | if (!(d->options & option) != !on) |
227 | setOptions(d->options ^ option); |
228 | } |
229 | |
230 | bool QFontDialogOptions::testOption(QFontDialogOptions::FontDialogOption option) const |
231 | { |
232 | return d->options & option; |
233 | } |
234 | |
235 | void QFontDialogOptions::setOptions(FontDialogOptions options) |
236 | { |
237 | if (options != d->options) |
238 | d->options = options; |
239 | } |
240 | |
241 | QFontDialogOptions::FontDialogOptions QFontDialogOptions::options() const |
242 | { |
243 | return d->options; |
244 | } |
245 | |
246 | /*! |
247 | \class QPlatformFontDialogHelper |
248 | \since 5.0 |
249 | \internal |
250 | \ingroup qpa |
251 | |
252 | \brief The QPlatformFontDialogHelper class allows for platform-specific customization of font dialogs. |
253 | |
254 | */ |
255 | const QSharedPointer<QFontDialogOptions> &QPlatformFontDialogHelper::options() const |
256 | { |
257 | return m_options; |
258 | } |
259 | |
260 | void QPlatformFontDialogHelper::setOptions(const QSharedPointer<QFontDialogOptions> &options) |
261 | { |
262 | m_options = options; |
263 | } |
264 | |
265 | // Color dialog |
266 | |
267 | class QColorDialogStaticData |
268 | { |
269 | public: |
270 | enum { CustomColorCount = 16, StandardColorCount = 6 * 8 }; |
271 | |
272 | QColorDialogStaticData(); |
273 | inline void readSettings(); |
274 | inline void writeSettings() const; |
275 | |
276 | QRgb customRgb[CustomColorCount]; |
277 | QRgb standardRgb[StandardColorCount]; |
278 | bool customSet; |
279 | }; |
280 | |
281 | QColorDialogStaticData::QColorDialogStaticData() : customSet(false) |
282 | { |
283 | int i = 0; |
284 | for (int g = 0; g < 4; ++g) |
285 | for (int r = 0; r < 4; ++r) |
286 | for (int b = 0; b < 3; ++b) |
287 | standardRgb[i++] = qRgb(r * 255 / 3, g * 255 / 3, b * 255 / 2); |
288 | std::fill(customRgb, customRgb + CustomColorCount, 0xffffffff); |
289 | readSettings(); |
290 | } |
291 | |
292 | void QColorDialogStaticData::readSettings() |
293 | { |
294 | #if QT_CONFIG(settings) |
295 | const QSettings settings(QSettings::UserScope, QStringLiteral("QtProject" )); |
296 | for (int i = 0; i < int(CustomColorCount); ++i) { |
297 | const QVariant v = settings.value(QLatin1String("Qt/customColors/" ) + QString::number(i)); |
298 | if (v.isValid()) |
299 | customRgb[i] = v.toUInt(); |
300 | } |
301 | #endif |
302 | } |
303 | |
304 | void QColorDialogStaticData::writeSettings() const |
305 | { |
306 | #if QT_CONFIG(settings) |
307 | if (customSet) { |
308 | const_cast<QColorDialogStaticData*>(this)->customSet = false; |
309 | QSettings settings(QSettings::UserScope, QStringLiteral("QtProject" )); |
310 | for (int i = 0; i < int(CustomColorCount); ++i) |
311 | settings.setValue(QLatin1String("Qt/customColors/" ) + QString::number(i), customRgb[i]); |
312 | } |
313 | #endif |
314 | } |
315 | |
316 | Q_GLOBAL_STATIC(QColorDialogStaticData, qColorDialogStaticData) |
317 | |
318 | class QColorDialogOptionsPrivate : public QSharedData |
319 | { |
320 | public: |
321 | QColorDialogOptionsPrivate() = default; |
322 | // Write out settings around destruction of dialogs |
323 | ~QColorDialogOptionsPrivate() { qColorDialogStaticData()->writeSettings(); } |
324 | |
325 | QColorDialogOptions::ColorDialogOptions options; |
326 | QString windowTitle; |
327 | }; |
328 | |
329 | QColorDialogOptions::QColorDialogOptions(QColorDialogOptionsPrivate *dd) |
330 | : d(dd) |
331 | { |
332 | } |
333 | |
334 | QColorDialogOptions::~QColorDialogOptions() |
335 | { |
336 | } |
337 | |
338 | namespace { |
339 | struct ColorDialogCombined : QColorDialogOptionsPrivate, QColorDialogOptions |
340 | { |
341 | ColorDialogCombined() : QColorDialogOptionsPrivate(), QColorDialogOptions(this) {} |
342 | ColorDialogCombined(const ColorDialogCombined &other) |
343 | : QColorDialogOptionsPrivate(other), QColorDialogOptions(this) {} |
344 | }; |
345 | } |
346 | |
347 | // static |
348 | QSharedPointer<QColorDialogOptions> QColorDialogOptions::create() |
349 | { |
350 | return QSharedPointer<ColorDialogCombined>::create(); |
351 | } |
352 | |
353 | QSharedPointer<QColorDialogOptions> QColorDialogOptions::clone() const |
354 | { |
355 | return QSharedPointer<ColorDialogCombined>::create(*static_cast<const ColorDialogCombined*>(this)); |
356 | } |
357 | |
358 | QString QColorDialogOptions::windowTitle() const |
359 | { |
360 | return d->windowTitle; |
361 | } |
362 | |
363 | void QColorDialogOptions::setWindowTitle(const QString &title) |
364 | { |
365 | d->windowTitle = title; |
366 | } |
367 | |
368 | void QColorDialogOptions::setOption(QColorDialogOptions::ColorDialogOption option, bool on) |
369 | { |
370 | if (!(d->options & option) != !on) |
371 | setOptions(d->options ^ option); |
372 | } |
373 | |
374 | bool QColorDialogOptions::testOption(QColorDialogOptions::ColorDialogOption option) const |
375 | { |
376 | return d->options & option; |
377 | } |
378 | |
379 | void QColorDialogOptions::setOptions(ColorDialogOptions options) |
380 | { |
381 | if (options != d->options) |
382 | d->options = options; |
383 | } |
384 | |
385 | QColorDialogOptions::ColorDialogOptions QColorDialogOptions::options() const |
386 | { |
387 | return d->options; |
388 | } |
389 | |
390 | int QColorDialogOptions::customColorCount() |
391 | { |
392 | return QColorDialogStaticData::CustomColorCount; |
393 | } |
394 | |
395 | QRgb QColorDialogOptions::customColor(int index) |
396 | { |
397 | if (uint(index) >= uint(QColorDialogStaticData::CustomColorCount)) |
398 | return qRgb(255, 255, 255); |
399 | return qColorDialogStaticData()->customRgb[index]; |
400 | } |
401 | |
402 | QRgb *QColorDialogOptions::customColors() |
403 | { |
404 | return qColorDialogStaticData()->customRgb; |
405 | } |
406 | |
407 | void QColorDialogOptions::setCustomColor(int index, QRgb color) |
408 | { |
409 | if (uint(index) >= uint(QColorDialogStaticData::CustomColorCount)) |
410 | return; |
411 | qColorDialogStaticData()->customSet = true; |
412 | qColorDialogStaticData()->customRgb[index] = color; |
413 | } |
414 | |
415 | QRgb *QColorDialogOptions::standardColors() |
416 | { |
417 | return qColorDialogStaticData()->standardRgb; |
418 | } |
419 | |
420 | QRgb QColorDialogOptions::standardColor(int index) |
421 | { |
422 | if (uint(index) >= uint(QColorDialogStaticData::StandardColorCount)) |
423 | return qRgb(255, 255, 255); |
424 | return qColorDialogStaticData()->standardRgb[index]; |
425 | } |
426 | |
427 | void QColorDialogOptions::setStandardColor(int index, QRgb color) |
428 | { |
429 | if (uint(index) >= uint(QColorDialogStaticData::StandardColorCount)) |
430 | return; |
431 | qColorDialogStaticData()->standardRgb[index] = color; |
432 | } |
433 | |
434 | /*! |
435 | \class QPlatformColorDialogHelper |
436 | \since 5.0 |
437 | \internal |
438 | \ingroup qpa |
439 | |
440 | \brief The QPlatformColorDialogHelper class allows for platform-specific customization of color dialogs. |
441 | |
442 | */ |
443 | const QSharedPointer<QColorDialogOptions> &QPlatformColorDialogHelper::options() const |
444 | { |
445 | return m_options; |
446 | } |
447 | |
448 | void QPlatformColorDialogHelper::setOptions(const QSharedPointer<QColorDialogOptions> &options) |
449 | { |
450 | m_options = options; |
451 | } |
452 | |
453 | // File dialog |
454 | |
455 | class QFileDialogOptionsPrivate : public QSharedData |
456 | { |
457 | public: |
458 | QFileDialogOptions::FileDialogOptions options; |
459 | QString windowTitle; |
460 | |
461 | QFileDialogOptions::ViewMode viewMode = QFileDialogOptions::Detail; |
462 | QFileDialogOptions::FileMode fileMode = QFileDialogOptions::AnyFile; |
463 | QFileDialogOptions::AcceptMode acceptMode = QFileDialogOptions::AcceptOpen; |
464 | QString labels[QFileDialogOptions::DialogLabelCount]; |
465 | QDir::Filters filters = QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs; |
466 | QList<QUrl> ; |
467 | bool useDefaultNameFilters = true; |
468 | QStringList nameFilters; |
469 | QStringList mimeTypeFilters; |
470 | QString defaultSuffix; |
471 | QStringList history; |
472 | QUrl initialDirectory; |
473 | QString initiallySelectedMimeTypeFilter; |
474 | QString initiallySelectedNameFilter; |
475 | QList<QUrl> initiallySelectedFiles; |
476 | QStringList supportedSchemes; |
477 | }; |
478 | |
479 | QFileDialogOptions::QFileDialogOptions(QFileDialogOptionsPrivate *dd) |
480 | : d(dd) |
481 | { |
482 | } |
483 | |
484 | QFileDialogOptions::~QFileDialogOptions() |
485 | { |
486 | } |
487 | |
488 | namespace { |
489 | struct FileDialogCombined : QFileDialogOptionsPrivate, QFileDialogOptions |
490 | { |
491 | FileDialogCombined() : QFileDialogOptionsPrivate(), QFileDialogOptions(this) {} |
492 | FileDialogCombined(const FileDialogCombined &other) : QFileDialogOptionsPrivate(other), QFileDialogOptions(this) {} |
493 | }; |
494 | } |
495 | |
496 | // static |
497 | QSharedPointer<QFileDialogOptions> QFileDialogOptions::create() |
498 | { |
499 | return QSharedPointer<FileDialogCombined>::create(); |
500 | } |
501 | |
502 | QSharedPointer<QFileDialogOptions> QFileDialogOptions::clone() const |
503 | { |
504 | return QSharedPointer<FileDialogCombined>::create(*static_cast<const FileDialogCombined*>(this)); |
505 | } |
506 | |
507 | QString QFileDialogOptions::windowTitle() const |
508 | { |
509 | return d->windowTitle; |
510 | } |
511 | |
512 | void QFileDialogOptions::setWindowTitle(const QString &title) |
513 | { |
514 | d->windowTitle = title; |
515 | } |
516 | |
517 | void QFileDialogOptions::setOption(QFileDialogOptions::FileDialogOption option, bool on) |
518 | { |
519 | if (!(d->options & option) != !on) |
520 | setOptions(d->options ^ option); |
521 | } |
522 | |
523 | bool QFileDialogOptions::testOption(QFileDialogOptions::FileDialogOption option) const |
524 | { |
525 | return d->options & option; |
526 | } |
527 | |
528 | void QFileDialogOptions::setOptions(FileDialogOptions options) |
529 | { |
530 | if (options != d->options) |
531 | d->options = options; |
532 | } |
533 | |
534 | QFileDialogOptions::FileDialogOptions QFileDialogOptions::options() const |
535 | { |
536 | return d->options; |
537 | } |
538 | |
539 | QDir::Filters QFileDialogOptions::filter() const |
540 | { |
541 | return d->filters; |
542 | } |
543 | |
544 | void QFileDialogOptions::setFilter(QDir::Filters filters) |
545 | { |
546 | d->filters = filters; |
547 | } |
548 | |
549 | void QFileDialogOptions::setViewMode(QFileDialogOptions::ViewMode mode) |
550 | { |
551 | d->viewMode = mode; |
552 | } |
553 | |
554 | QFileDialogOptions::ViewMode QFileDialogOptions::viewMode() const |
555 | { |
556 | return d->viewMode; |
557 | } |
558 | |
559 | void QFileDialogOptions::setFileMode(QFileDialogOptions::FileMode mode) |
560 | { |
561 | d->fileMode = mode; |
562 | } |
563 | |
564 | QFileDialogOptions::FileMode QFileDialogOptions::fileMode() const |
565 | { |
566 | return d->fileMode; |
567 | } |
568 | |
569 | void QFileDialogOptions::setAcceptMode(QFileDialogOptions::AcceptMode mode) |
570 | { |
571 | d->acceptMode = mode; |
572 | } |
573 | |
574 | QFileDialogOptions::AcceptMode QFileDialogOptions::acceptMode() const |
575 | { |
576 | return d->acceptMode; |
577 | } |
578 | |
579 | void QFileDialogOptions::(const QList<QUrl> &urls) |
580 | { |
581 | d->sidebarUrls = urls; |
582 | } |
583 | |
584 | QList<QUrl> QFileDialogOptions::() const |
585 | { |
586 | return d->sidebarUrls; |
587 | } |
588 | |
589 | /*! |
590 | \since 5.7 |
591 | \internal |
592 | The bool property useDefaultNameFilters indicates that no name filters have been |
593 | set or that they are equivalent to \gui{All Files (*)}. If it is true, the |
594 | platform can choose to hide the filter combo box. |
595 | |
596 | \sa defaultNameFilterString(). |
597 | */ |
598 | bool QFileDialogOptions::useDefaultNameFilters() const |
599 | { |
600 | return d->useDefaultNameFilters; |
601 | } |
602 | |
603 | void QFileDialogOptions::setUseDefaultNameFilters(bool dnf) |
604 | { |
605 | d->useDefaultNameFilters = dnf; |
606 | } |
607 | |
608 | void QFileDialogOptions::setNameFilters(const QStringList &filters) |
609 | { |
610 | d->useDefaultNameFilters = filters.size() == 1 |
611 | && filters.first() == QFileDialogOptions::defaultNameFilterString(); |
612 | d->nameFilters = filters; |
613 | } |
614 | |
615 | QStringList QFileDialogOptions::nameFilters() const |
616 | { |
617 | return d->useDefaultNameFilters ? |
618 | QStringList(QFileDialogOptions::defaultNameFilterString()) : d->nameFilters; |
619 | } |
620 | |
621 | /*! |
622 | \since 5.6 |
623 | \internal |
624 | \return The translated default name filter string (\gui{All Files (*)}). |
625 | \sa defaultNameFilters(), nameFilters() |
626 | */ |
627 | |
628 | QString QFileDialogOptions::defaultNameFilterString() |
629 | { |
630 | return QCoreApplication::translate("QFileDialog" , "All Files (*)" ); |
631 | } |
632 | |
633 | void QFileDialogOptions::setMimeTypeFilters(const QStringList &filters) |
634 | { |
635 | d->mimeTypeFilters = filters; |
636 | } |
637 | |
638 | QStringList QFileDialogOptions::mimeTypeFilters() const |
639 | { |
640 | return d->mimeTypeFilters; |
641 | } |
642 | |
643 | void QFileDialogOptions::setDefaultSuffix(const QString &suffix) |
644 | { |
645 | d->defaultSuffix = suffix; |
646 | if (d->defaultSuffix.size() > 1 && d->defaultSuffix.startsWith(QLatin1Char('.'))) |
647 | d->defaultSuffix.remove(0, 1); // Silently change ".txt" -> "txt". |
648 | } |
649 | |
650 | QString QFileDialogOptions::defaultSuffix() const |
651 | { |
652 | return d->defaultSuffix; |
653 | } |
654 | |
655 | void QFileDialogOptions::setHistory(const QStringList &paths) |
656 | { |
657 | d->history = paths; |
658 | } |
659 | |
660 | QStringList QFileDialogOptions::history() const |
661 | { |
662 | return d->history; |
663 | } |
664 | |
665 | void QFileDialogOptions::setLabelText(QFileDialogOptions::DialogLabel label, const QString &text) |
666 | { |
667 | if (unsigned(label) < unsigned(DialogLabelCount)) |
668 | d->labels[label] = text; |
669 | } |
670 | |
671 | QString QFileDialogOptions::labelText(QFileDialogOptions::DialogLabel label) const |
672 | { |
673 | return (unsigned(label) < unsigned(DialogLabelCount)) ? d->labels[label] : QString(); |
674 | } |
675 | |
676 | bool QFileDialogOptions::isLabelExplicitlySet(DialogLabel label) |
677 | { |
678 | return unsigned(label) < unsigned(DialogLabelCount) && !d->labels[label].isEmpty(); |
679 | } |
680 | |
681 | QUrl QFileDialogOptions::initialDirectory() const |
682 | { |
683 | return d->initialDirectory; |
684 | } |
685 | |
686 | void QFileDialogOptions::setInitialDirectory(const QUrl &directory) |
687 | { |
688 | d->initialDirectory = directory; |
689 | } |
690 | |
691 | QString QFileDialogOptions::initiallySelectedMimeTypeFilter() const |
692 | { |
693 | return d->initiallySelectedMimeTypeFilter; |
694 | } |
695 | |
696 | void QFileDialogOptions::setInitiallySelectedMimeTypeFilter(const QString &filter) |
697 | { |
698 | d->initiallySelectedMimeTypeFilter = filter; |
699 | } |
700 | |
701 | QString QFileDialogOptions::initiallySelectedNameFilter() const |
702 | { |
703 | return d->initiallySelectedNameFilter; |
704 | } |
705 | |
706 | void QFileDialogOptions::setInitiallySelectedNameFilter(const QString &filter) |
707 | { |
708 | d->initiallySelectedNameFilter = filter; |
709 | } |
710 | |
711 | QList<QUrl> QFileDialogOptions::initiallySelectedFiles() const |
712 | { |
713 | return d->initiallySelectedFiles; |
714 | } |
715 | |
716 | void QFileDialogOptions::setInitiallySelectedFiles(const QList<QUrl> &files) |
717 | { |
718 | d->initiallySelectedFiles = files; |
719 | } |
720 | |
721 | // Schemes supported by the application |
722 | void QFileDialogOptions::setSupportedSchemes(const QStringList &schemes) |
723 | { |
724 | d->supportedSchemes = schemes; |
725 | } |
726 | |
727 | QStringList QFileDialogOptions::supportedSchemes() const |
728 | { |
729 | return d->supportedSchemes; |
730 | } |
731 | |
732 | void QPlatformFileDialogHelper::selectMimeTypeFilter(const QString &filter) |
733 | { |
734 | Q_UNUSED(filter); |
735 | } |
736 | |
737 | QString QPlatformFileDialogHelper::selectedMimeTypeFilter() const |
738 | { |
739 | return QString(); |
740 | } |
741 | |
742 | // Return true if the URL is supported by the filedialog implementation *and* by the application. |
743 | bool QPlatformFileDialogHelper::isSupportedUrl(const QUrl &url) const |
744 | { |
745 | return url.isLocalFile(); |
746 | } |
747 | |
748 | /*! |
749 | \class QPlatformFileDialogHelper |
750 | \since 5.0 |
751 | \internal |
752 | \ingroup qpa |
753 | |
754 | \brief The QPlatformFileDialogHelper class allows for platform-specific customization of file dialogs. |
755 | |
756 | */ |
757 | const QSharedPointer<QFileDialogOptions> &QPlatformFileDialogHelper::options() const |
758 | { |
759 | return m_options; |
760 | } |
761 | |
762 | void QPlatformFileDialogHelper::setOptions(const QSharedPointer<QFileDialogOptions> &options) |
763 | { |
764 | m_options = options; |
765 | } |
766 | |
767 | const char QPlatformFileDialogHelper::filterRegExp[] = |
768 | "^(.*)\\(([a-zA-Z0-9_.,*? +;#\\-\\[\\]@\\{\\}/!<>\\$%&=^~:\\|]*)\\)$" ; |
769 | |
770 | // Makes a list of filters from a normal filter string "Image Files (*.png *.jpg)" |
771 | QStringList QPlatformFileDialogHelper::cleanFilterList(const QString &filter) |
772 | { |
773 | #if QT_CONFIG(regularexpression) |
774 | QRegularExpression regexp(QString::fromLatin1(filterRegExp)); |
775 | Q_ASSERT(regexp.isValid()); |
776 | QString f = filter; |
777 | QRegularExpressionMatch match; |
778 | filter.indexOf(regexp, 0, &match); |
779 | if (match.hasMatch()) |
780 | f = match.captured(2); |
781 | return f.split(QLatin1Char(' '), Qt::SkipEmptyParts); |
782 | #else |
783 | Q_UNUSED(filter); |
784 | return QStringList(); |
785 | #endif |
786 | } |
787 | |
788 | // Message dialog |
789 | |
790 | class QMessageDialogOptionsPrivate : public QSharedData |
791 | { |
792 | public: |
793 | QMessageDialogOptionsPrivate() : |
794 | icon(QMessageDialogOptions::NoIcon), |
795 | buttons(QPlatformDialogHelper::Ok), |
796 | nextCustomButtonId(QPlatformDialogHelper::LastButton + 1) |
797 | {} |
798 | |
799 | QString windowTitle; |
800 | QMessageDialogOptions::Icon icon; |
801 | QString text; |
802 | QString informativeText; |
803 | QString detailedText; |
804 | QPlatformDialogHelper::StandardButtons buttons; |
805 | QList<QMessageDialogOptions::CustomButton> customButtons; |
806 | int nextCustomButtonId; |
807 | }; |
808 | |
809 | QMessageDialogOptions::QMessageDialogOptions(QMessageDialogOptionsPrivate *dd) |
810 | : d(dd) |
811 | { |
812 | } |
813 | |
814 | QMessageDialogOptions::~QMessageDialogOptions() |
815 | { |
816 | } |
817 | |
818 | namespace { |
819 | struct MessageDialogCombined : QMessageDialogOptionsPrivate, QMessageDialogOptions |
820 | { |
821 | MessageDialogCombined() : QMessageDialogOptionsPrivate(), QMessageDialogOptions(this) {} |
822 | MessageDialogCombined(const MessageDialogCombined &other) |
823 | : QMessageDialogOptionsPrivate(other), QMessageDialogOptions(this) {} |
824 | }; |
825 | } |
826 | |
827 | // static |
828 | QSharedPointer<QMessageDialogOptions> QMessageDialogOptions::create() |
829 | { |
830 | return QSharedPointer<MessageDialogCombined>::create(); |
831 | } |
832 | |
833 | QSharedPointer<QMessageDialogOptions> QMessageDialogOptions::clone() const |
834 | { |
835 | return QSharedPointer<MessageDialogCombined>::create(*static_cast<const MessageDialogCombined*>(this)); |
836 | } |
837 | |
838 | QString QMessageDialogOptions::windowTitle() const |
839 | { |
840 | return d->windowTitle; |
841 | } |
842 | |
843 | void QMessageDialogOptions::setWindowTitle(const QString &title) |
844 | { |
845 | d->windowTitle = title; |
846 | } |
847 | |
848 | QMessageDialogOptions::Icon QMessageDialogOptions::icon() const |
849 | { |
850 | return d->icon; |
851 | } |
852 | |
853 | void QMessageDialogOptions::setIcon(Icon icon) |
854 | { |
855 | d->icon = icon; |
856 | } |
857 | |
858 | QString QMessageDialogOptions::text() const |
859 | { |
860 | return d->text; |
861 | } |
862 | |
863 | void QMessageDialogOptions::setText(const QString &text) |
864 | { |
865 | d->text = text; |
866 | } |
867 | |
868 | QString QMessageDialogOptions::informativeText() const |
869 | { |
870 | return d->informativeText; |
871 | } |
872 | |
873 | void QMessageDialogOptions::setInformativeText(const QString &informativeText) |
874 | { |
875 | d->informativeText = informativeText; |
876 | } |
877 | |
878 | QString QMessageDialogOptions::detailedText() const |
879 | { |
880 | return d->detailedText; |
881 | } |
882 | |
883 | void QMessageDialogOptions::setDetailedText(const QString &detailedText) |
884 | { |
885 | d->detailedText = detailedText; |
886 | } |
887 | |
888 | void QMessageDialogOptions::setStandardButtons(QPlatformDialogHelper::StandardButtons buttons) |
889 | { |
890 | d->buttons = buttons; |
891 | } |
892 | |
893 | QPlatformDialogHelper::StandardButtons QMessageDialogOptions::standardButtons() const |
894 | { |
895 | return d->buttons; |
896 | } |
897 | |
898 | int QMessageDialogOptions::addButton(const QString &label, QPlatformDialogHelper::ButtonRole role, |
899 | void *buttonImpl) |
900 | { |
901 | const CustomButton b(d->nextCustomButtonId++, label, role, buttonImpl); |
902 | d->customButtons.append(b); |
903 | return b.id; |
904 | } |
905 | |
906 | static inline bool operator==(const QMessageDialogOptions::CustomButton &a, |
907 | const QMessageDialogOptions::CustomButton &b) { |
908 | return a.id == b.id; |
909 | } |
910 | |
911 | void QMessageDialogOptions::removeButton(int id) |
912 | { |
913 | d->customButtons.removeOne(CustomButton(id)); |
914 | } |
915 | |
916 | const QList<QMessageDialogOptions::CustomButton> &QMessageDialogOptions::customButtons() |
917 | { |
918 | return d->customButtons; |
919 | } |
920 | |
921 | const QMessageDialogOptions::CustomButton *QMessageDialogOptions::customButton(int id) |
922 | { |
923 | int i = d->customButtons.indexOf(CustomButton(id)); |
924 | return (i < 0 ? nullptr : &d->customButtons.at(i)); |
925 | } |
926 | |
927 | QPlatformDialogHelper::ButtonRole QPlatformDialogHelper::buttonRole(QPlatformDialogHelper::StandardButton button) |
928 | { |
929 | switch (button) { |
930 | case Ok: |
931 | case Save: |
932 | case Open: |
933 | case SaveAll: |
934 | case Retry: |
935 | case Ignore: |
936 | return AcceptRole; |
937 | |
938 | case Cancel: |
939 | case Close: |
940 | case Abort: |
941 | return RejectRole; |
942 | |
943 | case Discard: |
944 | return DestructiveRole; |
945 | |
946 | case Help: |
947 | return HelpRole; |
948 | |
949 | case Apply: |
950 | return ApplyRole; |
951 | |
952 | case Yes: |
953 | case YesToAll: |
954 | return YesRole; |
955 | |
956 | case No: |
957 | case NoToAll: |
958 | return NoRole; |
959 | |
960 | case RestoreDefaults: |
961 | case Reset: |
962 | return ResetRole; |
963 | |
964 | default: |
965 | break; |
966 | } |
967 | return InvalidRole; |
968 | } |
969 | |
970 | const int *QPlatformDialogHelper::buttonLayout(Qt::Orientation orientation, ButtonLayout policy) |
971 | { |
972 | if (policy == UnknownLayout) { |
973 | #if defined (Q_OS_MACOS) |
974 | policy = MacLayout; |
975 | #elif defined (Q_OS_LINUX) || defined (Q_OS_UNIX) |
976 | policy = KdeLayout; |
977 | #elif defined (Q_OS_ANDROID) |
978 | policy = AndroidLayout; |
979 | #else |
980 | policy = WinLayout; |
981 | #endif |
982 | } |
983 | return buttonRoleLayouts[orientation == Qt::Vertical][policy]; |
984 | } |
985 | |
986 | /*! |
987 | \class QPlatformMessageDialogHelper |
988 | \since 5.0 |
989 | \internal |
990 | \ingroup qpa |
991 | |
992 | \brief The QPlatformMessageDialogHelper class allows for platform-specific customization of Message dialogs. |
993 | |
994 | */ |
995 | const QSharedPointer<QMessageDialogOptions> &QPlatformMessageDialogHelper::options() const |
996 | { |
997 | return m_options; |
998 | } |
999 | |
1000 | void QPlatformMessageDialogHelper::setOptions(const QSharedPointer<QMessageDialogOptions> &options) |
1001 | { |
1002 | m_options = options; |
1003 | } |
1004 | |
1005 | QT_END_NAMESPACE |
1006 | |