Utils: Make injected default dialog parent better re-usable

... and use it for CheckableMessageBox. This enables using
the proper parent e.g. in upcoming changes to ExtensionSystem.

Change-Id: Ib0e52cbd3e73dc628955918e3f443ff65cccb189
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2024-05-23 15:25:33 +02:00
parent a87ed65dbb
commit 1e9c11c3b3
6 changed files with 23 additions and 22 deletions

View File

@@ -3,6 +3,7 @@
#include "checkablemessagebox.h" #include "checkablemessagebox.h"
#include "guiutils.h"
#include "hostosinfo.h" #include "hostosinfo.h"
#include "qtcassert.h" #include "qtcassert.h"
#include "qtcsettings.h" #include "qtcsettings.h"
@@ -95,7 +96,7 @@ static QMessageBox::StandardButton exec(
return acceptButton; return acceptButton;
} }
QMessageBox msgBox(parent); QMessageBox msgBox(dialogParent(parent));
prepare(icon, title, text, decider, buttons, defaultButton, buttonTextOverrides, msg, msgBox); prepare(icon, title, text, decider, buttons, defaultButton, buttonTextOverrides, msg, msgBox);
msgBox.exec(); msgBox.exec();
@@ -131,7 +132,7 @@ static void show(QWidget *parent,
return; return;
} }
QMessageBox *msgBox = new QMessageBox(parent); QMessageBox *msgBox = new QMessageBox(dialogParent(parent));
prepare(icon, title, text, decider, buttons, defaultButton, buttonTextOverrides, msg, *msgBox); prepare(icon, title, text, decider, buttons, defaultButton, buttonTextOverrides, msg, *msgBox);
std::optional<QPointer<QObject>> guardPtr; std::optional<QPointer<QObject>> guardPtr;

View File

@@ -5,7 +5,6 @@
#include "savefile.h" #include "savefile.h"
#include "algorithm.h" #include "algorithm.h"
#include "devicefileaccess.h"
#include "environment.h" #include "environment.h"
#include "qtcassert.h" #include "qtcassert.h"
#include "utilstr.h" #include "utilstr.h"
@@ -24,6 +23,8 @@
#include <qplatformdefs.h> #include <qplatformdefs.h>
#ifdef QT_GUI_LIB #ifdef QT_GUI_LIB
#include "guiutils.h"
#include <QMessageBox> #include <QMessageBox>
#include <QGuiApplication> #include <QGuiApplication>
#endif #endif
@@ -414,18 +415,6 @@ void withNtfsPermissions(const std::function<void()> &task)
#ifdef QT_WIDGETS_LIB #ifdef QT_WIDGETS_LIB
static std::function<QWidget *()> s_dialogParentGetter;
void FileUtils::setDialogParentGetter(const std::function<QWidget *()> &getter)
{
s_dialogParentGetter = getter;
}
static QWidget *dialogParent(QWidget *parent)
{
return parent ? parent : s_dialogParentGetter ? s_dialogParentGetter() : nullptr;
}
static QUrl filePathToQUrl(const FilePath &filePath) static QUrl filePathToQUrl(const FilePath &filePath)
{ {
return QUrl::fromLocalFile(filePath.toFSPathString()); return QUrl::fromLocalFile(filePath.toFSPathString());

View File

@@ -89,8 +89,6 @@ public:
static FilePaths usefulExtraSearchPaths(); static FilePaths usefulExtraSearchPaths();
#ifdef QT_WIDGETS_LIB #ifdef QT_WIDGETS_LIB
static void setDialogParentGetter(const std::function<QWidget *()> &getter);
static bool hasNativeFileDialog(); static bool hasNativeFileDialog();
static FilePath getOpenFilePath(QWidget *parent, static FilePath getOpenFilePath(QWidget *parent,
@@ -236,7 +234,5 @@ private:
QTCREATOR_UTILS_EXPORT QTextStream &operator<<(QTextStream &s, const FilePath &fn); QTCREATOR_UTILS_EXPORT QTextStream &operator<<(QTextStream &s, const FilePath &fn);
bool isRelativePathHelper(const QString &path, OsType osType);
} // namespace Utils } // namespace Utils

View File

@@ -46,4 +46,16 @@ void QTCREATOR_UTILS_EXPORT setWheelScrollingWithoutFocusBlocked(QWidget *widget
widget->setFocusPolicy(Qt::StrongFocus); widget->setFocusPolicy(Qt::StrongFocus);
} }
static QWidget *(*s_dialogParentGetter)() = nullptr;
void setDialogParentGetter(QWidget *(*getter)())
{
s_dialogParentGetter = getter;
}
QWidget *dialogParent(QWidget *parent)
{
return parent ? parent : s_dialogParentGetter ? s_dialogParentGetter() : nullptr;
}
} // namespace Utils } // namespace Utils

View File

@@ -9,6 +9,9 @@ class QWidget;
namespace Utils { namespace Utils {
void QTCREATOR_UTILS_EXPORT setWheelScrollingWithoutFocusBlocked(QWidget *widget); QTCREATOR_UTILS_EXPORT void setWheelScrollingWithoutFocusBlocked(QWidget *widget);
QTCREATOR_UTILS_EXPORT QWidget *dialogParent(QWidget *parent);
QTCREATOR_UTILS_EXPORT void setDialogParentGetter(QWidget *(*getter)());
} // namespace Utils } // namespace Utils

View File

@@ -54,9 +54,9 @@
#include <utils/checkablemessagebox.h> #include <utils/checkablemessagebox.h>
#include <utils/dropsupport.h> #include <utils/dropsupport.h>
#include <utils/environment.h> #include <utils/environment.h>
#include <utils/fileutils.h>
#include <utils/fsengine/fileiconprovider.h> #include <utils/fsengine/fileiconprovider.h>
#include <utils/fsengine/fsengine.h> #include <utils/fsengine/fsengine.h>
#include <utils/guiutils.h>
#include <utils/historycompleter.h> #include <utils/historycompleter.h>
#include <utils/hostosinfo.h> #include <utils/hostosinfo.h>
#include <utils/mimeutils.h> #include <utils/mimeutils.h>
@@ -397,7 +397,7 @@ ICore::ICore()
QCoreApplication::exit(exitCode); QCoreApplication::exit(exitCode);
}); });
Utils::FileUtils::setDialogParentGetter(&ICore::dialogParent); Utils::setDialogParentGetter(&ICore::dialogParent);
d->m_progressManager->init(); // needs the status bar manager d->m_progressManager->init(); // needs the status bar manager
MessageManager::init(); MessageManager::init();