Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Instantiate dock widgets in a predictable order
Carsten Hartenfels6e3b2bb1df5a
diff --git a/libs/flake/KoDockFactoryBase.h b/libs/flake/KoDockFactoryBase.hindex c36b7f3a06..2a854adecb 100644--- a/libs/flake/KoDockFactoryBase.h+++ b/libs/flake/KoDockFactoryBase.h@@ -21,6 +21,9 @@ class QDockWidget;class KRITAFLAKE_EXPORT KoDockFactoryBase{public:+ static constexpr int DEFAULT_PRIORITY = 100;+ static constexpr int HIGHEST_PRIORITY = 0;+enum DockPosition {DockTornOff, ///< Floating as its own top level windowDockTop, ///< Above the central widget@@ -36,6 +39,14 @@ public:/// @return the id of the dock widgetvirtual QString id() const = 0;+ /// @return the priority of the dock widget, lower numbers have higher+ // priority, HIGHEST_PRIORITY is the highest. Only the log docker should+ // have that priority so that it starts logging at the earliest chance.+ virtual int priority() const+ {+ return DEFAULT_PRIORITY;+ }+/// @return the dock widget area the widget should appear in by defaultvirtual DockPosition defaultDockPosition() const = 0;diff --git a/libs/flake/KoDockRegistry.cpp b/libs/flake/KoDockRegistry.cppindex ae9c8a47b3..66e54a21cc 100644--- a/libs/flake/KoDockRegistry.cpp+++ b/libs/flake/KoDockRegistry.cpp@@ -17,6 +17,24 @@Q_GLOBAL_STATIC(KoDockRegistry, s_instance)+namespace+{+struct SortableDockFactoryBase {+ QString id;+ KoDockFactoryBase *factory;+ int priority;++ bool operator<(const SortableDockFactoryBase &other) const+ {+ if (priority == other.priority) {+ return id.compare(other.id, Qt::CaseInsensitive) < 0;+ } else {+ return priority < other.priority;+ }+ }+};+} // namespace+KoDockRegistry::KoDockRegistry(): d(0){@@ -38,6 +56,24 @@ KoDockRegistry::~KoDockRegistry()}}+QList<KoDockFactoryBase *> KoDockRegistry::sortedDockWidgetFactories()+{+ QList<SortableDockFactoryBase> sortables;+ sortables.reserve(count());+ for (const QString &id : keys()) {+ KoDockFactoryBase *factory = value(id);+ sortables.append({id, factory, factory->priority()});+ }+ std::sort(sortables.begin(), sortables.end());++ QList<KoDockFactoryBase *> factories;+ factories.reserve(sortables.size());+ for (const SortableDockFactoryBase &s : sortables) {+ factories.append(s.factory);+ }+ return factories;+}+KoDockRegistry* KoDockRegistry::instance(){diff --git a/libs/flake/KoDockRegistry.h b/libs/flake/KoDockRegistry.hindex 18e7794ba8..e4cd4735c4 100644--- a/libs/flake/KoDockRegistry.h+++ b/libs/flake/KoDockRegistry.h@@ -28,6 +28,12 @@ public:KoDockRegistry();~KoDockRegistry() override;+ /**+ * Returns the registered docks in a predictable order: first sorted by+ * priority, then sorted case-insensitively by their id.+ */+ QList<KoDockFactoryBase *> sortedDockWidgetFactories();+/*** Return an instance of the KoDockRegistry* Create a new instance on first call and return the singleton.diff --git a/libs/ui/KisMainWindow.cpp b/libs/ui/KisMainWindow.cppindex db67dbd471..bab8eb42c8 100644--- a/libs/ui/KisMainWindow.cpp+++ b/libs/ui/KisMainWindow.cpp@@ -422,8 +422,8 @@ KisMainWindow::KisMainWindow(QUuid uuid)if (toolbox) {dockwidgetActions[toolbox->toggleViewAction()->text()] = toolbox->toggleViewAction();}- Q_FOREACH (const QString & docker, KoDockRegistry::instance()->keys()) {- KoDockFactoryBase *factory = KoDockRegistry::instance()->value(docker);++ for (KoDockFactoryBase *factory : KoDockRegistry::instance()->sortedDockWidgetFactories()) {QDockWidget *dw = createDockWidget(factory);if (dw) {dockwidgetActions[dw->toggleViewAction()->text()] = dw->toggleViewAction();diff --git a/plugins/dockers/logdocker/LogDocker.cpp b/plugins/dockers/logdocker/LogDocker.cppindex 1a003fc587..9d2f48559e 100644--- a/plugins/dockers/logdocker/LogDocker.cpp+++ b/plugins/dockers/logdocker/LogDocker.cpp@@ -32,6 +32,11 @@ public:return QString( "LogDocker" );}+ int priority() const override+ {+ return HIGHEST_PRIORITY;+ }+virtual Qt::DockWidgetArea defaultDockWidgetArea() const{return Qt::RightDockWidgetArea;