diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a4b6b1..578dfc1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,6 +42,7 @@ set(ads_SRCS src/FloatingDockContainer.cpp src/FloatingDragPreview.cpp src/IconProvider.cpp + src/DockComponentsFactory.cpp src/ads.qrc src/linux/FloatingWidgetTitleBar.cpp ) @@ -61,6 +62,7 @@ set(ads_INSTALL_INCLUDE src/FloatingDockContainer.h src/FloatingDragPreview.h src/IconProvider.h + src/DockComponentsFactory.h src/linux/FloatingWidgetTitleBar.h ) if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4") diff --git a/demo/MainWindow.cpp b/demo/MainWindow.cpp index 518e76f..bc40a6a 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 = new ads::CDockAreaTitleBar(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,9 +340,13 @@ void MainWindowPrivate::createContent() FileSystemWidget->setFeature(ads::CDockWidget::DockWidgetMovable, false); FileSystemWidget->setFeature(ads::CDockWidget::DockWidgetFloatable, false); appendFeaturStringToWindowTitle(FileSystemWidget); - auto TopDockArea = DockManager->addDockWidget(ads::TopDockWidgetArea, FileSystemWidget); - // We create a calender widget and clear all flags to prevent the dock area + // 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 calendar widget and clear all flags to prevent the dock area // from closing DockWidget = createCalendarDockWidget(ViewMenu); DockWidget->setFeature(ads::CDockWidget::DockWidgetClosable, false); @@ -481,6 +507,12 @@ CMainWindow::CMainWindow(QWidget *parent) : // uncomment the following line if you want to show tabs menu button on DockArea's title bar only when there are more than one tab and at least of them has elided title //CDockManager::setConfigFlag(CDockManager::DockAreaDynamicTabsMenuButtonVisibility, true); + // uncomment the following line if you want floating container to always show application title instead of active dock widget's title + //CDockManager::setConfigFlag(CDockManager::FloatingContainerHasWidgetTitle, false); + + // uncomment the following line if you want floating container to show active dock widget's icon instead of always showing application icon + //CDockManager::setConfigFlag(CDockManager::FloatingContainerHasWidgetIcon, true); + // Now create the dock manager and its content d->DockManager = new CDockManager(this); 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/demo/images/help_outline.svg b/demo/images/help_outline.svg new file mode 100644 index 0000000..66f85ee --- /dev/null +++ b/demo/images/help_outline.svg @@ -0,0 +1,6 @@ + + help_outline icon - Licensed under Apache License v2.0 (http://www.apache.org/licenses/LICENSE-2.0) - Created with Iconfu.com - Derivative work of Material icons (Copyright Google Inc.) + + + + \ No newline at end of file diff --git a/src/DockAreaTitleBar.cpp b/src/DockAreaTitleBar.cpp index 8aa724e..a5024e5 100644 --- a/src/DockAreaTitleBar.cpp +++ b/src/DockAreaTitleBar.cpp @@ -49,6 +49,7 @@ #include "DockWidgetTab.h" #include "DockAreaTabBar.h" #include "IconProvider.h" +#include "DockComponentsFactory.h" #include @@ -273,7 +274,7 @@ void DockAreaTitleBarPrivate::createButtons() //============================================================================ void DockAreaTitleBarPrivate::createTabBar() { - TabBar = new CDockAreaTabBar(DockArea); + TabBar = componentsFactory()->createDockAreaTabBar(DockArea); TabBar->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); Layout->addWidget(TabBar); _this->connect(TabBar, SIGNAL(tabClosed(int)), SLOT(markTabsMenuOutdated())); diff --git a/src/DockAreaWidget.cpp b/src/DockAreaWidget.cpp index 9600bba..63b9d89 100644 --- a/src/DockAreaWidget.cpp +++ b/src/DockAreaWidget.cpp @@ -28,9 +28,10 @@ //============================================================================ // INCLUDES //============================================================================ -#include "DockWidgetTab.h" #include "DockAreaWidget.h" +#include + #include #include #include @@ -53,8 +54,8 @@ #include "DockAreaTabBar.h" #include "DockSplitter.h" #include "DockAreaTitleBar.h" - -#include +#include "DockComponentsFactory.h" +#include "DockWidgetTab.h" namespace ads @@ -317,7 +318,7 @@ DockAreaWidgetPrivate::DockAreaWidgetPrivate(CDockAreaWidget* _public) : //============================================================================ void DockAreaWidgetPrivate::createTitleBar() { - TitleBar = new CDockAreaTitleBar(_this); + TitleBar = componentsFactory()->createDockAreaTitleBar(_this); Layout->addWidget(TitleBar); QObject::connect(tabBar(), &CDockAreaTabBar::tabCloseRequested, _this, &CDockAreaWidget::onTabCloseRequested); QObject::connect(TitleBar, &CDockAreaTitleBar::tabBarClicked, _this, &CDockAreaWidget::setCurrentIndex); diff --git a/src/DockComponentsFactory.cpp b/src/DockComponentsFactory.cpp new file mode 100644 index 0000000..85a5475 --- /dev/null +++ b/src/DockComponentsFactory.cpp @@ -0,0 +1,69 @@ +//============================================================================ +/// \file DockComponentsFactory.cpp +/// \author Uwe Kindler +/// \date 10.02.2020 +/// \brief Implementation of DockComponentsFactory +//============================================================================ + +//============================================================================ +// INCLUDES +//============================================================================ +#include + +#include + +#include "DockWidgetTab.h" +#include "DockAreaTabBar.h" +#include "DockAreaTitleBar.h" +#include "DockWidget.h" +#include "DockAreaWidget.h" + +namespace ads +{ +static std::unique_ptr DefaultFactory(new CDockComponentsFactory()); + + +//============================================================================ +CDockWidgetTab* CDockComponentsFactory::createDockWidgetTab(CDockWidget* DockWidget) const +{ + return new CDockWidgetTab(DockWidget); +} + + +//============================================================================ +CDockAreaTabBar* CDockComponentsFactory::createDockAreaTabBar(CDockAreaWidget* DockArea) const +{ + return new CDockAreaTabBar(DockArea); +} + + +//============================================================================ +CDockAreaTitleBar* CDockComponentsFactory::createDockAreaTitleBar(CDockAreaWidget* DockArea) const +{ + return new CDockAreaTitleBar(DockArea); +} + + +//============================================================================ +const CDockComponentsFactory* CDockComponentsFactory::factory() +{ + return DefaultFactory.get(); +} + + +//============================================================================ +void CDockComponentsFactory::setFactory(CDockComponentsFactory* Factory) +{ + DefaultFactory.reset(Factory); +} + + +//============================================================================ +void CDockComponentsFactory::resetDefaultFactory() +{ + DefaultFactory.reset(new CDockComponentsFactory()); +} +} // namespace ads + +//--------------------------------------------------------------------------- +// EOF DockComponentsFactory.cpp diff --git a/src/DockComponentsFactory.h b/src/DockComponentsFactory.h new file mode 100644 index 0000000..f0598e9 --- /dev/null +++ b/src/DockComponentsFactory.h @@ -0,0 +1,90 @@ +#ifndef DockComponentsFactoryH +#define DockComponentsFactoryH +//============================================================================ +/// \file DockComponentsFactory.h +/// \author Uwe Kindler +/// \date 10.02.2020 +/// \brief Declaration of DockComponentsFactory +//============================================================================ + +//============================================================================ +// INCLUDES +//============================================================================ +#include "ads_globals.h" + +namespace ads +{ +class CDockWidgetTab; +class CDockAreaTitleBar; +class CDockAreaTabBar; +class CDockAreaWidget; +class CDockWidget; + + + +/** + * Factory for creation of certain GUI elements for the docking framework. + * A default unique instance provided by CDockComponentsFactory is used for + * creation of all supported components. To inject your custom components, + * you can create your own derived dock components factory and register + * it via setDefaultFactory() function. + * \code + * CDockComponentsFactory::setDefaultFactory(new MyComponentsFactory())); + * \endcode + */ +class ADS_EXPORT CDockComponentsFactory +{ +public: + /** + * Force virtual destructor + */ + virtual ~CDockComponentsFactory() {} + + /** + * This default implementation just creates a dock widget tab with + * new CDockWidgetTab(DockWIdget). + */ + virtual CDockWidgetTab* createDockWidgetTab(CDockWidget* DockWidget) const; + + /** + * This default implementation just creates a dock area tab bar with + * new CDockAreaTabBar(DockArea). + */ + virtual CDockAreaTabBar* createDockAreaTabBar(CDockAreaWidget* DockArea) const; + + /** + * This default implementation just creates a dock area title bar with + * new CDockAreaTitleBar(DockArea). + */ + virtual CDockAreaTitleBar* createDockAreaTitleBar(CDockAreaWidget* DockArea) const; + + /** + * Returns the default components factory + */ + static const CDockComponentsFactory* factory(); + + /** + * Sets a new default factory for creation of GUI elements. + * This function takes ownership of the given Factory. + */ + static void setFactory(CDockComponentsFactory* Factory); + + /** + * Resets the current factory to the + */ + static void resetDefaultFactory(); +}; + + +/** + * Convenience function to ease factory instance access + */ +inline const CDockComponentsFactory* componentsFactory() +{ + return CDockComponentsFactory::factory(); +} + +} // namespace ads + +//--------------------------------------------------------------------------- +#endif // DockComponentsFactoryH diff --git a/src/DockManager.h b/src/DockManager.h index 73f6c22..4b97727 100644 --- a/src/DockManager.h +++ b/src/DockManager.h @@ -59,6 +59,7 @@ class CDockWidgetTab; struct DockWidgetTabPrivate; struct DockAreaWidgetPrivate; class CIconProvider; +class CDockComponentsFactory; /** * The central dock manager that maintains the complete docking system. @@ -162,6 +163,8 @@ public: DockAreaHasTabsMenuButton = 0x8000, //!< If the flag is set each dock area has a tabs menu button DockAreaHideDisabledButtons = 0x10000, //!< If the flag is set disabled dock area buttons will not appear on the tollbar at all (enabling them will bring them back) DockAreaDynamicTabsMenuButtonVisibility = 0x20000, //!< If the flag is set dock area will disable a tabs menu button when there is only one tab in the area + FloatingContainerHasWidgetTitle = 0x40000, + FloatingContainerHasWidgetIcon = 0x80000, DefaultDockAreaButtons = DockAreaHasCloseButton @@ -170,7 +173,8 @@ public: DefaultBaseConfig = DefaultDockAreaButtons | ActiveTabHasCloseButton - | XmlCompressionEnabled,///< default base configuration settings + | XmlCompressionEnabled + | FloatingContainerHasWidgetTitle,///< default base configuration settings DefaultOpaqueConfig = DefaultBaseConfig | OpaqueSplitterResize diff --git a/src/DockWidget.cpp b/src/DockWidget.cpp index 3080a62..338a5fa 100644 --- a/src/DockWidget.cpp +++ b/src/DockWidget.cpp @@ -54,6 +54,7 @@ #include "DockManager.h" #include "FloatingDockContainer.h" #include "DockSplitter.h" +#include "DockComponentsFactory.h" #include "ads_globals.h" @@ -220,7 +221,7 @@ CDockWidget::CDockWidget(const QString &title, QWidget *parent) : setWindowTitle(title); setObjectName(title); - d->TabWidget = new CDockWidgetTab(this); + d->TabWidget = componentsFactory()->createDockWidgetTab(this); d->ToggleViewAction = new QAction(title, this); d->ToggleViewAction->setCheckable(true); connect(d->ToggleViewAction, SIGNAL(triggered(bool)), this, diff --git a/src/FloatingDockContainer.cpp b/src/FloatingDockContainer.cpp index e04dce5..ea782d4 100644 --- a/src/FloatingDockContainer.cpp +++ b/src/FloatingDockContainer.cpp @@ -79,6 +79,14 @@ struct FloatingDockContainerPrivate void titleMouseReleaseEvent(); void updateDropOverlays(const QPoint &GlobalPos); + /** + * Returns true if the given config flag is set + */ + static bool testConfigFlag(CDockManager::eConfigFlag Flag) + { + return CDockManager::configFlags().testFlag(Flag); + } + /** * Tests is a certain state is active */ @@ -100,6 +108,31 @@ struct FloatingDockContainerPrivate _this->setWindowTitle(Text); #endif } + + void reflectCurrentWidget(CDockWidget* CurrentWidget) + { + // reflect CurrentWidget's title if configured to do so, otherwise display application name as window title + if (testConfigFlag(CDockManager::FloatingContainerHasWidgetTitle)) + { + setWindowTitle(CurrentWidget->windowTitle()); + } + else + { + setWindowTitle(qApp->applicationDisplayName()); + } + + // reflect CurrentWidget's icon if configured to do so, otherwise display application icon as window icon + QIcon CurrentWidgetIcon = CurrentWidget->icon(); + if (testConfigFlag(CDockManager::FloatingContainerHasWidgetIcon) + && !CurrentWidgetIcon.isNull()) + { + _this->setWindowIcon(CurrentWidget->icon()); + } + else + { + _this->setWindowIcon(QApplication::windowIcon()); + } + } }; // struct FloatingDockContainerPrivate @@ -537,8 +570,8 @@ void CFloatingDockContainer::onDockAreasAddedOrRemoved() if (TopLevelDockArea) { d->SingleDockArea = TopLevelDockArea; - d->setWindowTitle( - d->SingleDockArea->currentDockWidget()->windowTitle()); + CDockWidget* CurrentWidget = d->SingleDockArea->currentDockWidget(); + d->reflectCurrentWidget(CurrentWidget); connect(d->SingleDockArea, SIGNAL(currentChanged(int)), this, SLOT(onDockAreaCurrentChanged(int))); } @@ -551,6 +584,7 @@ void CFloatingDockContainer::onDockAreasAddedOrRemoved() d->SingleDockArea = nullptr; } d->setWindowTitle(qApp->applicationDisplayName()); + setWindowIcon(QApplication::windowIcon()); } } @@ -560,11 +594,13 @@ void CFloatingDockContainer::updateWindowTitle() auto TopLevelDockArea = d->DockContainer->topLevelDockArea(); if (TopLevelDockArea) { - d->setWindowTitle(TopLevelDockArea->currentDockWidget()->windowTitle()); + CDockWidget* CurrentWidget = TopLevelDockArea->currentDockWidget(); + d->reflectCurrentWidget(CurrentWidget); } else { d->setWindowTitle(qApp->applicationDisplayName()); + setWindowIcon(QApplication::windowIcon()); } } @@ -572,7 +608,8 @@ void CFloatingDockContainer::updateWindowTitle() void CFloatingDockContainer::onDockAreaCurrentChanged(int Index) { Q_UNUSED(Index); - d->setWindowTitle(d->SingleDockArea->currentDockWidget()->windowTitle()); + CDockWidget* CurrentWidget = d->SingleDockArea->currentDockWidget(); + d->reflectCurrentWidget(CurrentWidget); } //============================================================================ diff --git a/src/src.pro b/src/src.pro index 755c7e5..73367b0 100644 --- a/src/src.pro +++ b/src/src.pro @@ -43,7 +43,8 @@ HEADERS += \ DockSplitter.h \ DockAreaTitleBar.h \ ElidingLabel.h \ - IconProvider.h + IconProvider.h \ + DockComponentsFactory.h SOURCES += \ @@ -61,7 +62,8 @@ SOURCES += \ DockSplitter.cpp \ DockAreaTitleBar.cpp \ ElidingLabel.cpp \ - IconProvider.cpp + IconProvider.cpp \ + DockComponentsFactory.cpp unix {