From 65eeffd5e168bd7d73c675d80d5c5d3262b34ac5 Mon Sep 17 00:00:00 2001 From: Uwe Kindler Date: Tue, 11 Feb 2020 09:31:57 +0100 Subject: [PATCH] Added showcase for DockComponentsFactory - a help button is injected into a title bar --- demo/MainWindow.cpp | 64 ++++++++++++++++++++++++----------- demo/demo.qrc | 1 + src/DockComponentsFactory.cpp | 11 ++++-- src/DockComponentsFactory.h | 15 +++++--- 4 files changed, 66 insertions(+), 25 deletions(-) diff --git a/demo/MainWindow.cpp b/demo/MainWindow.cpp index 518e76f..186ad37 100644 --- a/demo/MainWindow.cpp +++ b/demo/MainWindow.cpp @@ -73,6 +73,8 @@ #include "DockAreaTitleBar.h" #include "DockAreaTabBar.h" #include "FloatingDockContainer.h" +#include "DockComponentsFactory.h" + //============================================================================ @@ -142,6 +144,25 @@ static QIcon svgIcon(const QString& File) } +//============================================================================ +class CCustomComponentsFactory : public ads::CDockComponentsFactory +{ +public: + using Super = ads::CDockComponentsFactory; + ads::CDockAreaTitleBar* createDockAreaTitleBar(ads::CDockAreaWidget* DockArea) const override + { + auto TitleBar = Super::createDockAreaTitleBar(DockArea); + auto CustomButton = new QToolButton(DockArea); + CustomButton->setToolTip(QObject::tr("Help")); + CustomButton->setIcon(svgIcon(":/adsdemo/images/help_outline.svg")); + CustomButton->setAutoRaise(true); + int Index = TitleBar->indexOf(TitleBar->button(ads::TitleBarButtonTabsMenu)); + TitleBar->insertWidget(Index + 1, CustomButton); + return TitleBar; + } +}; + + //============================================================================ static ads::CDockWidget* createCalendarDockWidget(QMenu* ViewMenu) { @@ -202,28 +223,29 @@ static ads::CDockWidget* createEditorWidget(QMenu* ViewMenu) return DockWidget; } + //============================================================================ static ads::CDockWidget* createTableWidget(QMenu* ViewMenu) { - static int TableCount = 0; - QTableWidget* w = new QTableWidget(); - ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Table %1").arg(TableCount++)); - static int colCount = 5; - static int rowCount = 30; - w->setColumnCount(colCount); - w->setRowCount(rowCount); - for (int col = 0; col < colCount; ++col) - { - w->setHorizontalHeaderItem(col, new QTableWidgetItem(QString("Col %1").arg(col+1))); - for (int row = 0; row < rowCount; ++row) - { - w->setItem(row, col, new QTableWidgetItem(QString("T %1-%2").arg(row + 1).arg(col+1))); - } - } - DockWidget->setWidget(w); - DockWidget->setIcon(svgIcon(":/adsdemo/images/grid_on.svg")); - ViewMenu->addAction(DockWidget->toggleViewAction()); - return DockWidget; + static int TableCount = 0; + QTableWidget* w = new QTableWidget(); + ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Table %1").arg(TableCount++)); + static int colCount = 5; + static int rowCount = 30; + w->setColumnCount(colCount); + w->setRowCount(rowCount); + for (int col = 0; col < colCount; ++col) + { + w->setHorizontalHeaderItem(col, new QTableWidgetItem(QString("Col %1").arg(col+1))); + for (int row = 0; row < rowCount; ++row) + { + w->setItem(row, col, new QTableWidgetItem(QString("T %1-%2").arg(row + 1).arg(col+1))); + } + } + DockWidget->setWidget(w); + DockWidget->setIcon(svgIcon(":/adsdemo/images/grid_on.svg")); + ViewMenu->addAction(DockWidget->toggleViewAction()); + return DockWidget; } @@ -318,7 +340,11 @@ void MainWindowPrivate::createContent() FileSystemWidget->setFeature(ads::CDockWidget::DockWidgetMovable, false); FileSystemWidget->setFeature(ads::CDockWidget::DockWidgetFloatable, false); appendFeaturStringToWindowTitle(FileSystemWidget); + + // Test custom factory - we inject a help button into the title bar + ads::CDockComponentsFactory::setFactory(new CCustomComponentsFactory()); auto TopDockArea = DockManager->addDockWidget(ads::TopDockWidgetArea, FileSystemWidget); + ads::CDockComponentsFactory::resetDefaultFactory(); // We create a calender widget and clear all flags to prevent the dock area // from closing diff --git a/demo/demo.qrc b/demo/demo.qrc index 7bc93db..ddd49d8 100644 --- a/demo/demo.qrc +++ b/demo/demo.qrc @@ -12,5 +12,6 @@ images/custom-menu-button.svg app.css images/plus.svg + images/help_outline.svg diff --git a/src/DockComponentsFactory.cpp b/src/DockComponentsFactory.cpp index e41dff1..8c717de 100644 --- a/src/DockComponentsFactory.cpp +++ b/src/DockComponentsFactory.cpp @@ -44,17 +44,24 @@ CDockAreaTitleBar* CDockComponentsFactory::createDockAreaTitleBar(CDockAreaWidge //============================================================================ -const CDockComponentsFactory* CDockComponentsFactory::defaultFactory() +const CDockComponentsFactory* CDockComponentsFactory::factory() { return DefaultFactory.get(); } //============================================================================ -void CDockComponentsFactory::setDefaultFactory(CDockComponentsFactory* Factory) +void CDockComponentsFactory::setFactory(CDockComponentsFactory* Factory) { DefaultFactory.reset(Factory); } + + +//============================================================================ +void CDockComponentsFactory::resetDefaultFactory() +{ + DefaultFactory.reset(new CDockComponentsFactory()); +} } // namespace ads //--------------------------------------------------------------------------- diff --git a/src/DockComponentsFactory.h b/src/DockComponentsFactory.h index 8b9925a..92e7f52 100644 --- a/src/DockComponentsFactory.h +++ b/src/DockComponentsFactory.h @@ -10,6 +10,8 @@ //============================================================================ // INCLUDES //============================================================================ +#include "ads_globals.h" + namespace ads { class CDockWidgetTab; @@ -30,7 +32,7 @@ class CDockWidget; * CDockComponentsFactory::setDefaultFactory(new MyComponentsFactory())); * \endcode */ -class CDockComponentsFactory +class ADS_EXPORT CDockComponentsFactory { public: /** @@ -45,13 +47,18 @@ public: /** * Returns the default components factory */ - static const CDockComponentsFactory* defaultFactory(); + static const CDockComponentsFactory* factory(); /** * Sets a new default factory for creation of GUI elements. * This function takes ownership of the given Factory. */ - static void setDefaultFactory(CDockComponentsFactory* Factory); + static void setFactory(CDockComponentsFactory* Factory); + + /** + * Resets the current factory to the + */ + static void resetDefaultFactory(); }; @@ -60,7 +67,7 @@ public: */ inline const CDockComponentsFactory* componentsFactory() { - return CDockComponentsFactory::defaultFactory(); + return CDockComponentsFactory::factory(); } } // namespace ads