54#if QT_CONFIG(fontdialog)
55 if (qobject_cast<
const QFontDialog *>(dialog))
56 return QPlatformTheme::FontDialog;
58#if QT_CONFIG(messagebox)
59 if (qobject_cast<
const QMessageBox *>(dialog))
60 return QPlatformTheme::MessageDialog;
62#if QT_CONFIG(errormessage)
63 if (qobject_cast<
const QErrorMessage *>(dialog))
64 return QPlatformTheme::MessageDialog;
66#if !QT_CONFIG(filedialog) && !QT_CONFIG(colordialog) && !QT_CONFIG(fontdialog) &&
67 !QT_CONFIG(messagebox) && !QT_CONFIG(errormessage)
73QDialogPrivate::~QDialogPrivate()
75 delete m_platformHelper;
78QPlatformDialogHelper *QDialogPrivate::platformHelper()
const
82 if (!m_platformHelperCreated && canBeNativeDialog()) {
83 m_platformHelperCreated =
true;
84 QDialogPrivate *ncThis =
const_cast<QDialogPrivate *>(
this);
85 QDialog *dialog = ncThis->q_func();
86 const int type = themeDialogType(dialog);
88 m_platformHelper = QGuiApplicationPrivate::platformTheme()
89 ->createPlatformDialogHelper(
static_cast<QPlatformTheme::DialogType>(type));
90 if (m_platformHelper) {
91 QObject::connect(m_platformHelper, SIGNAL(accept()), dialog, SLOT(accept()));
92 QObject::connect(m_platformHelper, SIGNAL(reject()), dialog, SLOT(reject()));
93 ncThis->initHelper(m_platformHelper);
97 return m_platformHelper;
100bool QDialogPrivate::canBeNativeDialog()
const
102 if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs))
105 QDialogPrivate *ncThis =
const_cast<QDialogPrivate *>(
this);
106 QDialog *dialog = ncThis->q_func();
107 const int type = themeDialogType(dialog);
109 return QGuiApplicationPrivate::platformTheme()
110 ->usePlatformNativeDialog(
static_cast<QPlatformTheme::DialogType>(type));
115
116
117
118
119void QDialogPrivate::close(
int resultCode)
123 q->setResult(resultCode);
125 if (!data.is_closing) {
129 struct CloseEventEater : QObject
131 using QObject::QObject;
133 bool eventFilter(QObject *o, QEvent *e) override
135 if (e->type() == QEvent::Close)
137 return QObject::eventFilter(o, e);
140 q->installEventFilter(&closeEventEater);
141 QWidgetPrivate::close();
150 resetModalitySetByOpen();
153QWindow *QDialogPrivate::transientParentWindow()
const
156 if (
const QWidget *parent = q->nativeParentWidget())
157 return parent->windowHandle();
158 else if (q->windowHandle())
159 return q->windowHandle()->transientParent();
163bool QDialogPrivate::setNativeDialogVisible(
bool visible)
165 if (QPlatformDialogHelper *helper = platformHelper()) {
168 helperPrepareShow(helper);
169 nativeDialogInUse = helper->show(q->windowFlags(), q->windowModality(), transientParentWindow());
170 }
else if (nativeDialogInUse) {
174 return nativeDialogInUse;
177QVariant QDialogPrivate::styleHint(QPlatformDialogHelper::StyleHint hint)
const
179 if (
const QPlatformDialogHelper *helper = platformHelper())
180 return helper->styleHint(hint);
181 return QPlatformDialogHelper::defaultStyleHint(hint);
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
345
346
347
348
349
350
353
354
355
356
357
358
362
363
364
365
366
367
368
369
370
371
372
373
375QDialog::QDialog(QWidget *parent, Qt::WindowFlags f)
376 : QWidget(*
new QDialogPrivate, parent,
377 f | ((f & Qt::WindowType_Mask) == 0 ? Qt::Dialog : Qt::WindowType(0)))
382
383
384
385QDialog::QDialog(QDialogPrivate &dd, QWidget *parent, Qt::WindowFlags f)
386 : QWidget(dd, parent, f | ((f & Qt::WindowType_Mask) == 0 ? Qt::Dialog : Qt::WindowType(0)))
391
392
406
407
408
409
410
411
412#if QT_CONFIG(pushbutton)
413void QDialogPrivate::setDefault(QPushButton *pushButton)
416 bool hasMain =
false;
417 QList<QPushButton*> list = q->findChildren<QPushButton*>();
418 for (
int i=0; i<list.size(); ++i) {
419 QPushButton *pb = list.at(i);
420 if (pb->window() == q) {
423 if (pb != pushButton)
424 pb->setDefault(
false);
427 if (!pushButton && hasMain)
428 mainDef->setDefault(
true);
430 mainDef = pushButton;
434
435
436
437
438void QDialogPrivate::setMainDefault(QPushButton *pushButton)
441 setDefault(pushButton);
445
446
447
448
449void QDialogPrivate::hideDefault()
452 QList<QPushButton*> list = q->findChildren<QPushButton*>();
453 for (
int i=0; i<list.size(); ++i) {
454 list.at(i)->setDefault(
false);
459void QDialogPrivate::resetModalitySetByOpen()
462 if (resetModalityTo != -1 && !q->testAttribute(Qt::WA_SetWindowModality)) {
464 q->setWindowModality(Qt::WindowModality(resetModalityTo));
465 q->setAttribute(Qt::WA_SetWindowModality, wasModalitySet);
467 Q_ASSERT(resetModalityTo != Qt::WindowModal);
468 q->setParent(q->parentWidget(), Qt::Dialog);
471 resetModalityTo = -1;
475
476
477
478
479
480
481
482
483
484int QDialog::result()
const
491
492
493
494
495
496
497
498void QDialog::setResult(
int r)
505
506
507
508
509
514 Qt::WindowModality modality = windowModality();
515 if (modality != Qt::WindowModal) {
516 d->resetModalityTo = modality;
517 d->wasModalitySet = testAttribute(Qt::WA_SetWindowModality);
518 setWindowModality(Qt::WindowModal);
519 setAttribute(Qt::WA_SetWindowModality,
false);
521 setParent(parentWidget(), Qt::Sheet);
554 if (Q_UNLIKELY(d->eventLoop)) {
555 qWarning(
"QDialog::exec: Recursive call detected");
559 bool deleteOnClose = testAttribute(Qt::WA_DeleteOnClose);
560 setAttribute(Qt::WA_DeleteOnClose,
false);
562 d->resetModalitySetByOpen();
564 bool wasShowModal = testAttribute(Qt::WA_ShowModal);
565 setAttribute(Qt::WA_ShowModal,
true);
570 QPointer<QDialog> guard =
this;
571 if (d->nativeDialogInUse) {
572 d->platformHelper()->exec();
574 QEventLoop eventLoop;
575 d->eventLoop = &eventLoop;
576 (
void) eventLoop.exec(QEventLoop::DialogExec);
579 return QDialog::Rejected;
580 d->eventLoop =
nullptr;
582 setAttribute(Qt::WA_ShowModal, wasShowModal);
585 if (d->nativeDialogInUse)
586 d->helperDone(
static_cast<QDialog::DialogCode>(res), d->platformHelper());
663void QDialog::contextMenuEvent(QContextMenuEvent *e)
665#if !QT_CONFIG(whatsthis) || !QT_CONFIG(menu)
668 QWidget *w = childAt(e->pos());
670 w = rect().contains(e->pos()) ?
this :
nullptr;
674 while (w && w->whatsThis().size() == 0 && !w->testAttribute(Qt::WA_CustomWhatsThis))
675 w = w->isWindow() ?
nullptr : w->parentWidget();
677 QPointer<QMenu> p =
new QMenu(
this);
678 QAction *wt = p.data()->addAction(tr(
"What's This?"));
679 if (p.data()->exec(e->globalPos()) == wt) {
680 QHelpEvent e(QEvent::WhatsThis, w->rect().center(),
681 w->mapToGlobal(w->rect().center()));
682 QCoreApplication::sendEvent(w, &e);
691void QDialog::keyPressEvent(QKeyEvent *e)
693#ifndef QT_NO_SHORTCUT
697 if (e->matches(QKeySequence::Cancel)) {
701 if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {
703#if QT_CONFIG(pushbutton)
705 case Qt::Key_Return: {
706 QList<QPushButton*> list = findChildren<QPushButton*>();
707 for (
int i=0; i<list.size(); ++i) {
708 QPushButton *pb = list.at(i);
709 if (pb->isDefault() && pb->isVisible()) {
756void QDialogPrivate::setVisible(
bool visible)
759 if (!q->testAttribute(Qt::WA_DontShowOnScreen) && canBeNativeDialog() && setNativeDialogVisible(visible))
765 const bool dontBlockWindows = q->testAttribute(Qt::WA_DontShowOnScreen)
766 && styleHint(QPlatformDialogHelper::DialogIsQtWindow).toBool();
767 Qt::WindowModality oldModality;
770 if (dontBlockWindows) {
771 oldModality = q->windowModality();
772 wasModalitySet = q->testAttribute(Qt::WA_SetWindowModality);
773 q->setWindowModality(Qt::NonModal);
777 QWidgetPrivate::setVisible(visible);
781 if (!q->testAttribute(Qt::WA_ShowWithoutActivating)) {
782 QWidget *fw = q->window()->focusWidget();
787
788
789
790
791
792
793
794
795
796#if QT_CONFIG(pushbutton)
797 if (mainDef && fw->focusPolicy() == Qt::NoFocus) {
799 while ((first = first->nextInFocusChain()) != fw && first->focusPolicy() == Qt::NoFocus)
801 if (first != mainDef && qobject_cast<QPushButton*>(first))
804 if (!mainDef && q->isWindow()) {
806 while ((w = w->nextInFocusChain()) != fw) {
807 QPushButton *pb = qobject_cast<QPushButton *>(w);
808 if (pb && pb->autoDefault() && pb->focusPolicy() != Qt::NoFocus) {
809 pb->setDefault(
true);
815 if (fw && !fw->hasFocus()) {
816 QFocusEvent e(QEvent::FocusIn, Qt::TabFocusReason);
817 QCoreApplication::sendEvent(fw, &e);
821#if QT_CONFIG(accessibility)
822 QAccessibleEvent event(q, QAccessible::DialogStart);
823 QAccessible::updateAccessibility(&event);
828#if QT_CONFIG(accessibility)
829 if (q->isVisible()) {
830 QAccessibleEvent event(q, QAccessible::DialogEnd);
831 QAccessible::updateAccessibility(&event);
836 QWidgetPrivate::setVisible(visible);
841 if (dontBlockWindows) {
842 q->setWindowModality(oldModality);
843 q->setAttribute(Qt::WA_SetWindowModality, wasModalitySet);
846#if QT_CONFIG(pushbutton)
847 const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme();
848 if (mainDef && q->isActiveWindow()
849 && theme->themeHint(QPlatformTheme::DialogSnapToDefaultButton).toBool())
850 QCursor::setPos(mainDef->mapToGlobal(mainDef->rect().center()));
855void QDialog::showEvent(QShowEvent *event)
857 if (!event->spontaneous() && !testAttribute(Qt::WA_Moved)) {
858 Qt::WindowStates state = windowState();
859 adjustPosition(parentWidget());
860 setAttribute(Qt::WA_Moved,
false);
861 if (state != windowState())
862 setWindowState(state);
867void QDialog::adjustPosition(QWidget* w)
871 if (
const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme())
872 if (theme->themeHint(QPlatformTheme::WindowAutoPlacement).toBool())
875 int extraw = 0, extrah = 0;
876 const QWindow *parentWindow =
nullptr;
880 parentWindow = d->transientParentWindow();
883 QScreen *scrn =
nullptr;
886 else if (parentWindow)
887 scrn = parentWindow->screen();
888 else if (QGuiApplication::primaryScreen()->virtualSiblings().size() > 1)
889 scrn = QGuiApplication::screenAt(QCursor::pos());
893 desk = scrn->availableGeometry();
895 QWidgetList list = QApplication::topLevelWidgets();
896 for (
int i = 0; (extraw == 0 || extrah == 0) && i < list.size(); ++i) {
897 QWidget * current = list.at(i);
898 if (current->isVisible()) {
899 int framew = current->geometry().x() - current->x();
900 int frameh = current->geometry().y() - current->y();
902 extraw = qMax(extraw, framew);
903 extrah = qMax(extrah, frameh);
909 if (extraw == 0 || extrah == 0 || extraw >= 10 || extrah >= 40) {
918 if (w->windowHandle() && qvariant_cast<WId>(w->windowHandle()->property(
"_q_embedded_native_parent_handle")))
921 pp = w->mapToGlobal(QPoint(0,0));
922 p = QPoint(pp.x() + w->width()/2,
923 pp.y() + w->height()/ 2);
924 }
else if (parentWindow) {
927 QPoint pp = parentWindow->mapToGlobal(QPoint(0, 0));
928 p = QPoint(pp.x() + parentWindow->width() / 2, pp.y() + parentWindow->height() / 2);
931 p = QPoint(desk.x() + desk.width()/2, desk.y() + desk.height()/2);
935 p = QPoint(p.x()-width()/2 - extraw,
936 p.y()-height()/2 - extrah);
939 if (p.x() + extraw + width() > desk.x() + desk.width())
940 p.setX(desk.x() + desk.width() - width() - extraw);
941 if (p.x() < desk.x())
944 if (p.y() + extrah + height() > desk.y() + desk.height())
945 p.setY(desk.y() + desk.height() - height() - extrah);
946 if (p.y() < desk.y())
953 if (QWindow *window = windowHandle())
954 window->setScreen(scrn);
961QSize QDialog::sizeHint()
const
965 if (d->orientation == Qt::Horizontal)
966 return QSize(QWidget::sizeHint().width(),
967 qMax(QWidget::sizeHint().height(),d->extension->sizeHint().height()));
969 return QSize(qMax(QWidget::sizeHint().width(), d->extension->sizeHint().width()),
970 QWidget::sizeHint().height());
972 return QWidget::sizeHint();
977QSize QDialog::minimumSizeHint()
const
981 if (d->orientation == Qt::Horizontal)
982 return QSize(QWidget::minimumSizeHint().width(),
983 qMax(QWidget::minimumSizeHint().height(), d->extension->minimumSizeHint().height()));
985 return QSize(qMax(QWidget::minimumSizeHint().width(), d->extension->minimumSizeHint().width()),
986 QWidget::minimumSizeHint().height());
989 return QWidget::minimumSizeHint();
1023void QDialog::setSizeGripEnabled(
bool enabled)
1025#if !QT_CONFIG(sizegrip)
1029#if QT_CONFIG(sizegrip)
1030 d->sizeGripEnabled = enabled;
1031 if (enabled && d->doShowExtension)
1034 if (!enabled != !d->resizer) {
1036 d->resizer =
new QSizeGrip(
this);
1038 d->resizer->resize(d->resizer->sizeHint());
1039 if (isRightToLeft())
1040 d->resizer->move(rect().bottomLeft() -d->resizer->rect().bottomLeft());
1042 d->resizer->move(rect().bottomRight() -d->resizer->rect().bottomRight());
1043 d->resizer->raise();
1047 d->resizer =
nullptr;