diff --git a/src/blackgui/guiutility.cpp b/src/blackgui/guiutility.cpp index ebf86fed4..d4bd24b30 100644 --- a/src/blackgui/guiutility.cpp +++ b/src/blackgui/guiutility.cpp @@ -11,6 +11,7 @@ #include "blackgui/overlaymessagesframe.h" #include "blackmisc/icon.h" #include "blackmisc/verify.h" +#include "guiutility.h" #include #include @@ -948,4 +949,166 @@ namespace BlackGui if (wizard->button(QWizard::CustomButton2)) { wizard->button(QWizard::CustomButton2)->setMinimumWidth(minW); } if (wizard->button(QWizard::CustomButton3)) { wizard->button(QWizard::CustomButton3)->setMinimumWidth(minW); } } + + QWidgetList CGuiUtility::getAllModallWidgets() + { + const QWidgetList widgets = QApplication::topLevelWidgets(); + QWidgetList openWidgets; + for (QWidget *w : widgets) + { + if (w->isModal()) { openWidgets.push_back(w); } + } + return openWidgets; + } + + QStringList CGuiUtility::getAllWidgetTitles(const QWidgetList widgets) + { + QStringList titles; + for (const QWidget *w : widgets) + { + if (!w) { continue; } + if (!w->windowTitle().isEmpty()) + { + titles.push_back(w->windowTitle()); + } + else + { + titles.push_back(QStringLiteral("name: ") % w->objectName()); + } + } + return titles; + } + + QStringList CGuiUtility::getAllWidgetNames(const QWidgetList widgets) + { + QStringList titles; + for (const QWidget *w : widgets) + { + if (!w) { continue; } + titles.push_back(QStringLiteral("name: ") % w->objectName()); + } + return titles; + } + + QList CGuiUtility::getAllDockWidgets(QWidget *parent, bool floatingOnly) + { + QList docks; + if (parent) + { + const auto children = parent->findChildren(); + for (QDockWidget *w : children) + { + if (!w) { continue; } + if (!floatingOnly || w->isFloating()) + { + docks.push_back(w); + } + } + } + return docks; + } + + QList CGuiUtility::getAllDockWidgets(QWindow *parent, bool floatingOnly) + { + QList docks; + if (parent) + { + const auto children = parent->findChildren(); + for (QDockWidget *w : children) + { + if (!w) { continue; } + if (!floatingOnly || w->isFloating()) + { + docks.push_back(w); + } + } + } + return docks; + } + + QWidgetList CGuiUtility::closeAllModalWidgets() + { + QWidgetList modals = getAllModallWidgets(); + for (QWidget *w : modals) + { + if (!w) { continue; } + w->close(); + } + return modals; + } + + QStringList CGuiUtility::closeAllModalWidgetsGetTitles() + { + const QWidgetList modals = getAllModallWidgets(); + QStringList titles; + for (QWidget *w : modals) + { + if (!w) { continue; } + titles << w->windowTitle(); + w->close(); + } + return titles; + } + + QList CGuiUtility::closeAllDockWidgets(QWidget *parent, bool floatingOnly) + { + QList dws = getAllDockWidgets(parent, floatingOnly); + for (QWidget *w : dws) + { + if (!w) { continue; } + w->close(); + } + return dws; + } + + QList CGuiUtility::closeAllDockWidgets(QWindow *parent, bool floatingOnly) + { + QList dws = getAllDockWidgets(parent, floatingOnly); + for (QWidget *w : dws) + { + if (!w) { continue; } + w->close(); + } + return dws; + } + + QStringList CGuiUtility::closeAllDockWidgetsGetTitles(QWidget *parent, bool floatingOnly) + { + const QList dws = getAllDockWidgets(parent, floatingOnly); + QStringList titles; + for (QWidget *w : dws) + { + if (!w) { continue; } + titles << w->windowTitle(); + w->close(); + } + return titles; + } + + QStringList CGuiUtility::closeAllDockWidgetsGetTitles(QWindow *parent, bool floatingOnly) + { + const QList dws = getAllDockWidgets(parent, floatingOnly); + QStringList titles; + for (QWidget *w : dws) + { + if (!w) { continue; } + titles << w->windowTitle(); + w->close(); + } + return titles; + } + + QStringList CGuiUtility::deleteLaterAllDockWidgetsGetTitles(QWidget *parent, bool floatingOnly) + { + const QList dws = getAllDockWidgets(parent, floatingOnly); + QStringList titles; + for (QWidget *w : dws) + { + if (!w) { continue; } + titles << w->windowTitle(); + w->deleteLater(); // DANGEROUS + } + return titles; + } + } // ns diff --git a/src/blackgui/guiutility.h b/src/blackgui/guiutility.h index 41a7cdb2b..2e65d5cae 100644 --- a/src/blackgui/guiutility.h +++ b/src/blackgui/guiutility.h @@ -18,9 +18,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -281,6 +283,43 @@ namespace BlackGui //! Set button widths for a wizard static void setWizardButtonWidths(QWizard *wizard); + //! All modal widgets + static QWidgetList getAllModallWidgets(); + + //! All titles + static QStringList getAllWidgetTitles(const QWidgetList widgets); + + //! All names + static QStringList getAllWidgetNames(const QWidgetList widgets); + + //! Get all dock widgets + static QList getAllDockWidgets(QWidget *parent, bool floatingOnly); + + //! Get all dock widgets + static QList getAllDockWidgets(QWindow *parent, bool floatingOnly); + + //! Close all modal widgets + static QWidgetList closeAllModalWidgets(); + + //! Close all modal widgets and get titles + static QStringList closeAllModalWidgetsGetTitles(); + + //! Close all dock widgets + static QList closeAllDockWidgets(QWidget *parent, bool floatingOnly); + + //! Close all dock widgets + static QList closeAllDockWidgets(QWindow *parent, bool floatingOnly); + + //! Close all dock widgets + static QStringList closeAllDockWidgetsGetTitles(QWidget *parent, bool floatingOnly); + + //! close all dock widgets + static QStringList closeAllDockWidgetsGetTitles(QWindow *parent, bool floatingOnly); + + //! "deleteLater" all dock widgets + //! \remark do NOT use unless you know what you are doing + static QStringList deleteLaterAllDockWidgetsGetTitles(QWidget *parent, bool floatingOnly); + private: //! No constructor, use static functions only CGuiUtility() {}