From 420baeedfebc8a91cd4e30310b4646a9a6a46227 Mon Sep 17 00:00:00 2001 From: Uwe Kindler Date: Thu, 19 Oct 2023 10:25:23 +0200 Subject: [PATCH] Added support for global dock manager toolbar style and custom dock widget toolbar style for dock widgets --- demo/MainWindow.cpp | 5 ++++ src/DockManager.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++ src/DockManager.h | 30 ++++++++++++++++++++++ src/DockWidget.cpp | 49 ++++++++++++++++++++++++++++++++++++ src/DockWidget.h | 17 +++++++++++++ 5 files changed, 162 insertions(+) diff --git a/demo/MainWindow.cpp b/demo/MainWindow.cpp index 3609461..9b79fb4 100644 --- a/demo/MainWindow.cpp +++ b/demo/MainWindow.cpp @@ -271,6 +271,10 @@ struct MainWindowPrivate auto ToolBar = DockWidget->createDefaultToolBar(); ToolBar->addAction(ui.actionSaveState); ToolBar->addAction(ui.actionRestoreState); + // For testing all calendar dock widgets have a the tool button style + // Qt::ToolButtonTextUnderIcon + DockWidget->setToolBarStyleSource(ads::CDockWidget::ToolBarStyleFromDockWidget); + DockWidget->setToolBarStyle(Qt::ToolButtonTextUnderIcon, ads::CDockWidget::StateFloating); return DockWidget; } @@ -764,6 +768,7 @@ CMainWindow::CMainWindow(QWidget *parent) : // Now create the dock manager and its content d->DockManager = new CDockManager(this); + d->DockManager->setDockWidgetToolBarStyle(Qt::ToolButtonIconOnly, ads::CDockWidget::StateFloating); #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) connect(d->PerspectiveComboBox, SIGNAL(activated(QString)), diff --git a/src/DockManager.cpp b/src/DockManager.cpp index 2dc2fb0..9948202 100644 --- a/src/DockManager.cpp +++ b/src/DockManager.cpp @@ -118,6 +118,10 @@ struct DockManagerPrivate CDockFocusController* FocusController = nullptr; CDockWidget* CentralWidget = nullptr; bool IsLeavingMinimized = false; + Qt::ToolButtonStyle ToolBarStyleDocked = Qt::ToolButtonIconOnly; + Qt::ToolButtonStyle ToolBarStyleFloating = Qt::ToolButtonTextUnderIcon; + QSize ToolBarIconSizeDocked = QSize(16, 16); + QSize ToolBarIconSizeFloating = QSize(24, 24); /** * Private data constructor @@ -1361,6 +1365,63 @@ QString CDockManager::floatingContainersTitle() return FloatingContainersTitle; } + +//=========================================================================== +void CDockManager::setDockWidgetToolBarStyle(Qt::ToolButtonStyle Style, CDockWidget::eState State) +{ + if (CDockWidget::StateFloating == State) + { + d->ToolBarStyleFloating = Style; + } + else + { + d->ToolBarStyleDocked = Style; + } +} + + +//=========================================================================== +Qt::ToolButtonStyle CDockManager::dockWidgetToolBarStyle(CDockWidget::eState State) const +{ + if (CDockWidget::StateFloating == State) + { + return d->ToolBarStyleFloating; + } + else + { + return d->ToolBarStyleDocked; + } +} + + +//=========================================================================== +void CDockManager::setDockWidgetToolBarIconSize(const QSize& IconSize, CDockWidget::eState State) +{ + if (CDockWidget::StateFloating == State) + { + d->ToolBarIconSizeFloating = IconSize; + } + else + { + d->ToolBarIconSizeDocked = IconSize; + } +} + + +//=========================================================================== +QSize CDockManager::dockWidgetToolBarIconSize(CDockWidget::eState State) const +{ + if (CDockWidget::StateFloating == State) + { + return d->ToolBarIconSizeFloating; + } + else + { + return d->ToolBarIconSizeDocked; + } +} + + } // namespace ads //--------------------------------------------------------------------------- diff --git a/src/DockManager.h b/src/DockManager.h index 7f382d3..85482d0 100644 --- a/src/DockManager.h +++ b/src/DockManager.h @@ -627,6 +627,36 @@ public: */ static QString floatingContainersTitle(); + /** + * This function sets the tool button style for the given dock widget state. + * It is possible to switch the tool button style depending on the state. + * If a dock widget is floating, then here are more space and it is + * possible to select a style that requires more space like + * Qt::ToolButtonTextUnderIcon. For the docked state Qt::ToolButtonIconOnly + * might be better. + */ + void setDockWidgetToolBarStyle(Qt::ToolButtonStyle Style, CDockWidget::eState State); + + /** + * Returns the tool button style for the given docking state. + * \see setToolBarStyle() + */ + Qt::ToolButtonStyle dockWidgetToolBarStyle(CDockWidget::eState State) const; + + /** + * This function sets the tool button icon size for the given state. + * If a dock widget is floating, there is more space and increasing the + * icon size is possible. For docked widgets, small icon sizes, eg. 16 x 16 + * might be better. + */ + void setDockWidgetToolBarIconSize(const QSize& IconSize, CDockWidget::eState State); + + /** + * Returns the icon size for a given docking state. + * \see setToolBarIconSize() + */ + QSize dockWidgetToolBarIconSize(CDockWidget::eState State) const; + public Q_SLOTS: /** * Opens the perspective with the given name. diff --git a/src/DockWidget.cpp b/src/DockWidget.cpp index d799faf..4298af6 100644 --- a/src/DockWidget.cpp +++ b/src/DockWidget.cpp @@ -95,6 +95,7 @@ struct DockWidgetPrivate CDockWidget::eMinimumSizeHintMode MinimumSizeHintMode = CDockWidget::MinimumSizeHintFromDockWidget; WidgetFactory* Factory = nullptr; QPointer SideTabWidget; + CDockWidget::eToolBarStyleSource ToolBarStyleSource = CDockWidget::ToolBarStyleFromDockManager; /** * Private data constructor @@ -139,6 +140,11 @@ struct DockWidgetPrivate * returns true on success. */ bool createWidgetFromFactory(); + + /** + * Use the dock manager toolbar style and icon size for the different states + */ + void setToolBarStyleFromDockManager(); }; // struct DockWidgetPrivate @@ -331,6 +337,22 @@ bool DockWidgetPrivate::createWidgetFromFactory() } +//============================================================================ +void DockWidgetPrivate::setToolBarStyleFromDockManager() +{ + if (!DockManager) + { + return; + } + auto State = CDockWidget::StateDocked; + _this->setToolBarIconSize(DockManager->dockWidgetToolBarIconSize(State), State); + _this->setToolBarStyle(DockManager->dockWidgetToolBarStyle(State), State); + State = CDockWidget::StateFloating; + _this->setToolBarIconSize(DockManager->dockWidgetToolBarIconSize(State), State); + _this->setToolBarStyle(DockManager->dockWidgetToolBarStyle(State), State); +} + + //============================================================================ CDockWidget::CDockWidget(const QString &title, QWidget *parent) : QFrame(parent), @@ -523,6 +545,15 @@ CDockManager* CDockWidget::dockManager() const void CDockWidget::setDockManager(CDockManager* DockManager) { d->DockManager = DockManager; + if (!DockManager) + { + return; + } + + if (ToolBarStyleFromDockManager == d->ToolBarStyleSource) + { + d->setToolBarStyleFromDockManager(); + } } @@ -1284,6 +1315,24 @@ void CDockWidget::toggleAutoHide(SideBarLocation Location) } +//============================================================================ +void CDockWidget::setToolBarStyleSource(eToolBarStyleSource Source) +{ + d->ToolBarStyleSource = Source; + if (ToolBarStyleFromDockManager == d->ToolBarStyleSource) + { + d->setToolBarStyleFromDockManager(); + } +} + + +//============================================================================ +CDockWidget::eToolBarStyleSource CDockWidget::toolBarStyleSource() const +{ + return d->ToolBarStyleSource; +} + + } // namespace ads //--------------------------------------------------------------------------- diff --git a/src/DockWidget.h b/src/DockWidget.h index b563c63..e4af5ea 100644 --- a/src/DockWidget.h +++ b/src/DockWidget.h @@ -176,6 +176,12 @@ public: StateFloating }; + enum eToolBarStyleSource + { + ToolBarStyleFromDockManager, + ToolBarStyleFromDockWidget + }; + /** * Sets the widget for the dock widget to widget. * The InsertMode defines how the widget is inserted into the dock widget. @@ -473,6 +479,17 @@ public: */ void setToolBar(QToolBar* ToolBar); + /** + * Configures, if the dock widget uses the global tool bar styles from + * dock manager or if it uses its own tool bar style + */ + void setToolBarStyleSource(eToolBarStyleSource Source); + + /** + * Returns the configured tool bar style source + */ + eToolBarStyleSource toolBarStyleSource() const; + /** * This function sets the tool button style for the given dock widget state. * It is possible to switch the tool button style depending on the state.