From 21e6c7ae4745a76b676dfaa9fe17a2dd40fc0c5c Mon Sep 17 00:00:00 2001 From: Timur Pocheptsov Date: Fri, 24 Apr 2015 16:48:14 +0200 Subject: Cocoa integration - implement Qt::WindowModal file dialogs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, for Qt::WindowModal dialogs we were creating modal sheet with beginSheetModalForWindow: and later (from exec) calling -runModal, essentially making a window modal dialog into (now) application modal sheet, which is not right- it's centered relative to desktop and the jump from window modal sheet's position (centered relative to a window) to application modal was quite visible. Now, instead of [panel runModal] (== [NSApp runModalForWindow:panel]) we call [NSApp runModalForWindow:theRequiredWindow]. Change-Id: I793dc72c7d1fe96497bb35754f4d0eac9a5e00e5 Reviewed-by: Morten Johan Sørvig --- src/gui/kernel/qplatformdialoghelper.cpp | 6 +++++ src/gui/kernel/qplatformdialoghelper.h | 1 + .../platforms/cocoa/qcocoafiledialoghelper.h | 1 + .../platforms/cocoa/qcocoafiledialoghelper.mm | 28 ++++++++++++++++------ src/widgets/dialogs/qdialog.cpp | 5 +++- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/gui/kernel/qplatformdialoghelper.cpp b/src/gui/kernel/qplatformdialoghelper.cpp index 3d37088182..3d7c2f7bf0 100644 --- a/src/gui/kernel/qplatformdialoghelper.cpp +++ b/src/gui/kernel/qplatformdialoghelper.cpp @@ -137,6 +137,12 @@ QVariant QPlatformDialogHelper::defaultStyleHint(QPlatformDialogHelper::StyleHi return QVariant(); } +void QPlatformDialogHelper::execModalForWindow(QWindow *parent) +{ + Q_UNUSED(parent); + exec(); +} + // Font dialog class QFontDialogOptionsPrivate : public QSharedData diff --git a/src/gui/kernel/qplatformdialoghelper.h b/src/gui/kernel/qplatformdialoghelper.h index 8b2b9881b7..6d3a367e60 100644 --- a/src/gui/kernel/qplatformdialoghelper.h +++ b/src/gui/kernel/qplatformdialoghelper.h @@ -145,6 +145,7 @@ public: virtual QVariant styleHint(StyleHint hint) const; virtual void exec() = 0; + virtual void execModalForWindow(QWindow *parent); virtual bool show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent) = 0; diff --git a/src/plugins/platforms/cocoa/qcocoafiledialoghelper.h b/src/plugins/platforms/cocoa/qcocoafiledialoghelper.h index 48d7efe174..36943a563e 100644 --- a/src/plugins/platforms/cocoa/qcocoafiledialoghelper.h +++ b/src/plugins/platforms/cocoa/qcocoafiledialoghelper.h @@ -51,6 +51,7 @@ public: virtual ~QCocoaFileDialogHelper(); void exec(); + void execModalForWindow(QWindow *parent); bool defaultNameFilterDisables() const; diff --git a/src/plugins/platforms/cocoa/qcocoafiledialoghelper.mm b/src/plugins/platforms/cocoa/qcocoafiledialoghelper.mm index 4ece1b5a22..19f81c72a1 100644 --- a/src/plugins/platforms/cocoa/qcocoafiledialoghelper.mm +++ b/src/plugins/platforms/cocoa/qcocoafiledialoghelper.mm @@ -254,17 +254,22 @@ static QString strippedText(QString s) || [self panel:nil shouldShowFilename:filepath]; [self updateProperties]; - QCocoaMenuBar::redirectKnownMenuItemsToFirstResponder(); [mSavePanel setDirectoryURL: [NSURL fileURLWithPath:mCurrentDir]]; [mSavePanel setNameFieldStringValue:selectable ? QCFString::toNSString(info.fileName()) : @""]; NSWindow *nsparent = static_cast(qGuiApp->platformNativeInterface()->nativeResourceForWindow("nswindow", parent)); + qApp->processEvents(QEventLoop::ExcludeUserInputEvents | QEventLoop::ExcludeSocketNotifiers); + QCocoaMenuBar::redirectKnownMenuItemsToFirstResponder(); + [mSavePanel beginSheetModalForWindow:nsparent completionHandler:^(NSInteger result){ - mReturnCode = result; - if (mHelper) - mHelper->QNSOpenSavePanelDelegate_panelClosed(result == NSOKButton); + [[NSApplication sharedApplication] stopModalWithCode:result]; }]; + + mReturnCode = [[NSApplication sharedApplication] runModalForWindow:nsparent]; + QAbstractEventDispatcher::instance()->interrupt(); + if (mHelper) + mHelper->QNSOpenSavePanelDelegate_panelClosed(mReturnCode == NSOKButton); } - (BOOL)isHiddenFile:(NSString *)filename isDir:(BOOL)isDir @@ -706,14 +711,15 @@ void QCocoaFileDialogHelper::createNSOpenSavePanelDelegate() bool QCocoaFileDialogHelper::showCocoaFilePanel(Qt::WindowModality windowModality, QWindow *parent) { + Q_UNUSED(parent) + createNSOpenSavePanelDelegate(); if (!mDelegate) return false; if (windowModality == Qt::NonModal) [mDelegate showModelessPanel]; - else if (windowModality == Qt::WindowModal && parent) - [mDelegate showWindowModalSheet:parent]; - // no need to show a Qt::ApplicationModal dialog here, since it will be done in _q_platformRunNativeAppModalPanel() + // no need to show a Qt::ApplicationModal dialog here, since it will be done in exec; + // Qt::WindowModal will be done in execModalForWindow. return true; } @@ -745,6 +751,14 @@ void QCocoaFileDialogHelper::exec() } +void QCocoaFileDialogHelper::execModalForWindow(QWindow *parent) +{ + if (!parent) + return exec(); + + [mDelegate showWindowModalSheet:parent]; +} + bool QCocoaFileDialogHelper::defaultNameFilterDisables() const { return true; diff --git a/src/widgets/dialogs/qdialog.cpp b/src/widgets/dialogs/qdialog.cpp index 65def6d4b8..6676a3ccba 100644 --- a/src/widgets/dialogs/qdialog.cpp +++ b/src/widgets/dialogs/qdialog.cpp @@ -534,7 +534,10 @@ int QDialog::exec() QPointer guard = this; if (d->nativeDialogInUse) { - d->platformHelper()->exec(); + if (windowModality() == Qt::WindowModal) + d->platformHelper()->execModalForWindow(d->parentWindow()); + else + d->platformHelper()->exec(); } else { QEventLoop eventLoop; d->eventLoop = &eventLoop; -- cgit v1.2.3