diff options
Diffstat (limited to 'src/widgets/dialogs/qdialog.cpp')
-rw-r--r-- | src/widgets/dialogs/qdialog.cpp | 236 |
1 files changed, 106 insertions, 130 deletions
diff --git a/src/widgets/dialogs/qdialog.cpp b/src/widgets/dialogs/qdialog.cpp index 9f4e95d83c..27466d03d6 100644 --- a/src/widgets/dialogs/qdialog.cpp +++ b/src/widgets/dialogs/qdialog.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include <QtWidgets/qtwidgetsglobal.h> #if QT_CONFIG(colordialog) @@ -70,7 +34,7 @@ #include <qpa/qplatformtheme.h> #include "private/qdialog_p.h" #include "private/qguiapplication_p.h" -#ifndef QT_NO_ACCESSIBILITY +#if QT_CONFIG(accessibility) #include "qaccessible.h" #endif @@ -134,6 +98,9 @@ QPlatformDialogHelper *QDialogPrivate::platformHelper() const bool QDialogPrivate::canBeNativeDialog() const { + if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)) + return false; + QDialogPrivate *ncThis = const_cast<QDialogPrivate *>(this); QDialog *dialog = ncThis->q_func(); const int type = themeDialogType(dialog); @@ -153,7 +120,7 @@ void QDialogPrivate::close(int resultCode) Q_Q(QDialog); q->setResult(resultCode); - q->hide(); + if (!data.is_closing) { // Until Qt 6.3 we didn't close dialogs, so they didn't receive a QCloseEvent. // It is likely that subclasses implement closeEvent and handle them as rejection @@ -171,30 +138,17 @@ void QDialogPrivate::close(int resultCode) } closeEventEater; q->installEventFilter(&closeEventEater); QWidgetPrivate::close(); + } else { + // If the close was initiated outside of QDialog we will end up + // here via QDialog::closeEvent calling reject(), in which case + // we need to hide the dialog to ensure QDialog::closeEvent does + // not ignore the close event. FIXME: Why is QDialog doing this? + q->hide(); } resetModalitySetByOpen(); } -/*! - \internal - - Emits finished() signal with \a resultCode. If the \a dialogCode - is equal to 0 emits rejected(), if the \a dialogCode is equal to - 1 emits accepted(). - */ -void QDialogPrivate::finalize(int resultCode, int dialogCode) -{ - Q_Q(QDialog); - - if (dialogCode == QDialog::Accepted) - emit q->accepted(); - else if (dialogCode == QDialog::Rejected) - emit q->rejected(); - - emit q->finished(resultCode); -} - QWindow *QDialogPrivate::transientParentWindow() const { Q_Q(const QDialog); @@ -226,14 +180,6 @@ QVariant QDialogPrivate::styleHint(QPlatformDialogHelper::StyleHint hint) const return QPlatformDialogHelper::defaultStyleHint(hint); } -void QDialogPrivate::deletePlatformHelper() -{ - delete m_platformHelper; - m_platformHelper = nullptr; - m_platformHelperCreated = false; - nativeDialogInUse = false; -} - /*! \class QDialog \brief The QDialog class is the base class of dialog windows. @@ -334,7 +280,8 @@ void QDialogPrivate::deletePlatformHelper() \section1 Escape Key If the user presses the Esc key in a dialog, QDialog::reject() - will be called. This will cause the window to close: The \l{QCloseEvent}{close event} cannot be \l{QEvent::ignore()}{ignored}. + will be called. This will cause the window to close: + The \l{QCloseEvent}{close event} cannot be \l{QEvent::ignore()}{ignored}. \section1 Extensibility @@ -342,9 +289,8 @@ void QDialogPrivate::deletePlatformHelper() partial dialog that shows the most commonly used options, and a full dialog that shows all the options. Typically an extensible dialog will initially appear as a partial dialog, but with a - \uicontrol More toggle button. If the user presses the \uicontrol More button down, - the dialog is expanded. The \l{Extension Example} shows how to achieve - extensible dialogs using Qt. + \uicontrol More toggle button. If the user presses the + \uicontrol More button down, the dialog is expanded. \target return \section1 Return Value (Modal Dialogs) @@ -374,8 +320,15 @@ void QDialogPrivate::deletePlatformHelper() \snippet dialogs/dialogs.cpp 0 + A dialog with an extension: + + \snippet dialogs/dialogs.cpp extension + + By setting the \l{QLayout::}{sizeConstraint} property of the dialog's + layout to \l{QLayout::}{SetFixedSize}, the dialog will not be resizable + by the user, and will automatically shrink when the extension gets hidden. + \sa QDialogButtonBox, QTabWidget, QWidget, QProgressDialog, - {fowler}{GUI Design Handbook: Dialogs, Standard}, {Extension Example}, {Standard Dialogs Example} */ @@ -640,7 +593,7 @@ int QDialog::exec() As with QWidget::close(), done() deletes the dialog if the Qt::WA_DeleteOnClose flag is set. If the dialog is the application's main widget, the application terminates. If the dialog is the - last window closed, the QApplication::lastWindowClosed() signal is + last window closed, the QGuiApplication::lastWindowClosed() signal is emitted. \sa accept(), reject(), QApplication::activeWindow(), QCoreApplication::quit() @@ -648,9 +601,22 @@ int QDialog::exec() void QDialog::done(int r) { + QPointer<QDialog> guard(this); + Q_D(QDialog); d->close(r); - d->finalize(r, r); + + if (!guard) + return; + + int dialogCode = d->dialogCode(); + if (dialogCode == QDialog::Accepted) + emit accepted(); + else if (dialogCode == QDialog::Rejected) + emit rejected(); + + if (guard) + emit finished(r); } /*! @@ -778,98 +744,108 @@ void QDialog::closeEvent(QCloseEvent *e) void QDialog::setVisible(bool visible) { Q_D(QDialog); - if (!testAttribute(Qt::WA_DontShowOnScreen) && d->canBeNativeDialog() && d->setNativeDialogVisible(visible)) + + if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden) != visible) + return; + + d->setVisible(visible); +} + +void QDialogPrivate::setVisible(bool visible) +{ + Q_Q(QDialog); + if (!q->testAttribute(Qt::WA_DontShowOnScreen) && canBeNativeDialog() && setNativeDialogVisible(visible)) return; // We should not block windows by the invisible modal dialog // if a platform-specific dialog is implemented as an in-process // Qt window, because in this case it will also be blocked. - const bool dontBlockWindows = testAttribute(Qt::WA_DontShowOnScreen) - && d->styleHint(QPlatformDialogHelper::DialogIsQtWindow).toBool(); + const bool dontBlockWindows = q->testAttribute(Qt::WA_DontShowOnScreen) + && styleHint(QPlatformDialogHelper::DialogIsQtWindow).toBool(); Qt::WindowModality oldModality; bool wasModalitySet; if (dontBlockWindows) { - oldModality = windowModality(); - wasModalitySet = testAttribute(Qt::WA_SetWindowModality); - setWindowModality(Qt::NonModal); + oldModality = q->windowModality(); + wasModalitySet = q->testAttribute(Qt::WA_SetWindowModality); + q->setWindowModality(Qt::NonModal); } if (visible) { - if (testAttribute(Qt::WA_WState_ExplicitShowHide) && !testAttribute(Qt::WA_WState_Hidden)) - return; - - QWidget::setVisible(visible); - QWidget *fw = window()->focusWidget(); - if (!fw) - fw = this; - - /* - The following block is to handle a special case, and does not - really follow propper logic in concern of autoDefault and TAB - order. However, it's here to ease usage for the users. If a - dialog has a default QPushButton, and first widget in the TAB - order also is a QPushButton, then we give focus to the main - default QPushButton. This simplifies code for the developers, - and actually catches most cases... If not, then they simply - have to use [widget*]->setFocus() themselves... - */ + q->QWidget::setVisible(visible); + + // Window activation might be prevented. We can't test isActiveWindow here, + // as the window will be activated asynchronously by the window manager. + if (!q->testAttribute(Qt::WA_ShowWithoutActivating)) { + QWidget *fw = q->window()->focusWidget(); + if (!fw) + fw = q; + + /* + The following block is to handle a special case, and does not + really follow proper logic in concern of autoDefault and TAB + order. However, it's here to ease usage for the users. If a + dialog has a default QPushButton, and first widget in the TAB + order also is a QPushButton, then we give focus to the main + default QPushButton. This simplifies code for the developers, + and actually catches most cases... If not, then they simply + have to use [widget*]->setFocus() themselves... + */ #if QT_CONFIG(pushbutton) - if (d->mainDef && fw->focusPolicy() == Qt::NoFocus) { - QWidget *first = fw; - while ((first = first->nextInFocusChain()) != fw && first->focusPolicy() == Qt::NoFocus) - ; - if (first != d->mainDef && qobject_cast<QPushButton*>(first)) - d->mainDef->setFocus(); - } - if (!d->mainDef && isWindow()) { - QWidget *w = fw; - while ((w = w->nextInFocusChain()) != fw) { - QPushButton *pb = qobject_cast<QPushButton *>(w); - if (pb && pb->autoDefault() && pb->focusPolicy() != Qt::NoFocus) { - pb->setDefault(true); - break; + if (mainDef && fw->focusPolicy() == Qt::NoFocus) { + QWidget *first = fw; + while ((first = first->nextInFocusChain()) != fw && first->focusPolicy() == Qt::NoFocus) + ; + if (first != mainDef && qobject_cast<QPushButton*>(first)) + mainDef->setFocus(); + } + if (!mainDef && q->isWindow()) { + QWidget *w = fw; + while ((w = w->nextInFocusChain()) != fw) { + QPushButton *pb = qobject_cast<QPushButton *>(w); + if (pb && pb->autoDefault() && pb->focusPolicy() != Qt::NoFocus) { + pb->setDefault(true); + break; + } } } - } #endif - if (fw && !fw->hasFocus()) { - QFocusEvent e(QEvent::FocusIn, Qt::TabFocusReason); - QCoreApplication::sendEvent(fw, &e); + if (fw && !fw->hasFocus()) { + QFocusEvent e(QEvent::FocusIn, Qt::TabFocusReason); + QCoreApplication::sendEvent(fw, &e); + } } -#ifndef QT_NO_ACCESSIBILITY - QAccessibleEvent event(this, QAccessible::DialogStart); +#if QT_CONFIG(accessibility) + QAccessibleEvent event(q, QAccessible::DialogStart); QAccessible::updateAccessibility(&event); #endif } else { - if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden)) - return; -#ifndef QT_NO_ACCESSIBILITY - if (isVisible()) { - QAccessibleEvent event(this, QAccessible::DialogEnd); +#if QT_CONFIG(accessibility) + if (q->isVisible()) { + QAccessibleEvent event(q, QAccessible::DialogEnd); QAccessible::updateAccessibility(&event); } #endif // Reimplemented to exit a modal event loop when the dialog is hidden. - QWidget::setVisible(visible); - if (d->eventLoop) - d->eventLoop->exit(); + q->QWidget::setVisible(visible); + if (eventLoop) + eventLoop->exit(); } if (dontBlockWindows) { - setWindowModality(oldModality); - setAttribute(Qt::WA_SetWindowModality, wasModalitySet); + q->setWindowModality(oldModality); + q->setAttribute(Qt::WA_SetWindowModality, wasModalitySet); } #if QT_CONFIG(pushbutton) const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme(); - if (d->mainDef && isActiveWindow() + if (mainDef && q->isActiveWindow() && theme->themeHint(QPlatformTheme::DialogSnapToDefaultButton).toBool()) - QCursor::setPos(d->mainDef->mapToGlobal(d->mainDef->rect().center())); + QCursor::setPos(mainDef->mapToGlobal(mainDef->rect().center())); #endif } |