diff --git a/demo/MainWindow.cpp b/demo/MainWindow.cpp index f8835a7..f106804 100644 --- a/demo/MainWindow.cpp +++ b/demo/MainWindow.cpp @@ -224,11 +224,27 @@ static ads::CDockWidget* createEditorWidget(QMenu* ViewMenu) } +//=========================================================================== +/** + * Custom QTableWidget with a minimum size hint to test CDockWidget + * setMinimumSizeHintMode() function of CDockWidget + */ +class CMinSizeTableWidget : public QTableWidget +{ +public: + using QTableWidget::QTableWidget; + virtual QSize minimumSizeHint() const override + { + return QSize(300, 100); + } +}; + + //============================================================================ static ads::CDockWidget* createTableWidget(QMenu* ViewMenu) { static int TableCount = 0; - QTableWidget* w = new QTableWidget(); + auto w = new CMinSizeTableWidget(); ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Table %1").arg(TableCount++)); static int colCount = 5; static int rowCount = 30; @@ -244,6 +260,7 @@ static ads::CDockWidget* createTableWidget(QMenu* ViewMenu) } DockWidget->setWidget(w); DockWidget->setIcon(svgIcon(":/adsdemo/images/grid_on.svg")); + DockWidget->setMinimumSizeHintMode(ads::CDockWidget::MinimumSizeHintFromContent); ViewMenu->addAction(DockWidget->toggleViewAction()); return DockWidget; } diff --git a/sip/DockWidget.sip b/sip/DockWidget.sip index c06e063..3e51d04 100644 --- a/sip/DockWidget.sip +++ b/sip/DockWidget.sip @@ -50,6 +50,12 @@ public: ForceScrollArea, ForceNoScrollArea }; + + enum eMinimumSizeHintMode + { + MinimumSizeHintFromDockWidget, + MinimumSizeHintFromContent + }; enum eToggleViewActionMode { @@ -76,6 +82,7 @@ public: bool isClosed() const; QAction* toggleViewAction() const; void setToggleViewActionMode(ads::CDockWidget::eToggleViewActionMode Mode); + void setMinimumSizeHintMode(ads::CDockWidget::eMinimumSizeHintMode Mode); void setIcon(const QIcon& Icon); QIcon icon() const; QToolBar* toolBar() const; diff --git a/src/DockAreaWidget.cpp b/src/DockAreaWidget.cpp index 4809e64..66420d9 100644 --- a/src/DockAreaWidget.cpp +++ b/src/DockAreaWidget.cpp @@ -247,6 +247,7 @@ struct DockAreaWidgetPrivate CDockManager* DockManager = nullptr; bool UpdateTitleBarButtons = false; DockWidgetAreas AllowedAreas = AllDockAreas; + QSize MinSizeHint; /** * Private data constructor @@ -303,6 +304,20 @@ struct DockAreaWidgetPrivate * Udpates the enable state of the close and detach button */ void updateTitleBarButtonStates(); + + /** + * Scans all contained dock widgets for the max. minimum size hint + */ + void updateMinimumSizeHint() + { + MinSizeHint = QSize(); + for (int i = 0; i < ContentsLayout->count(); ++i) + { + auto Widget = ContentsLayout->widget(i); + MinSizeHint.setHeight(qMax(MinSizeHint.height(), Widget->minimumSizeHint().height())); + MinSizeHint.setWidth(qMax(MinSizeHint.width(), Widget->minimumSizeHint().width())); + } + } }; // struct DockAreaWidgetPrivate @@ -407,6 +422,8 @@ void CDockAreaWidget::insertDockWidget(int index, CDockWidget* DockWidget, d->tabBar()->blockSignals(false); TabWidget->setVisible(!DockWidget->isClosed()); DockWidget->setProperty(INDEX_PROPERTY, index); + d->MinSizeHint.setHeight(qMax(d->MinSizeHint.height(), DockWidget->minimumSizeHint().height())); + d->MinSizeHint.setWidth(qMax(d->MinSizeHint.width(), DockWidget->minimumSizeHint().width())); if (Activate) { setCurrentIndex(index); @@ -447,6 +464,7 @@ void CDockAreaWidget::removeDockWidget(CDockWidget* DockWidget) d->updateTitleBarButtonStates(); updateTitleBarVisibility(); + d->updateMinimumSizeHint(); auto TopLevelDockWidget = DockContainer->topLevelDockWidget(); if (TopLevelDockWidget) { @@ -863,6 +881,13 @@ CDockAreaTitleBar* CDockAreaWidget::titleBar() const { return d->TitleBar; } + + +//============================================================================ +QSize CDockAreaWidget::minimumSizeHint() const +{ + return d->MinSizeHint.isValid() ? d->MinSizeHint : Super::minimumSizeHint(); +} } // namespace ads //--------------------------------------------------------------------------- diff --git a/src/DockAreaWidget.h b/src/DockAreaWidget.h index ebbbe01..50ccd01 100644 --- a/src/DockAreaWidget.h +++ b/src/DockAreaWidget.h @@ -164,6 +164,13 @@ public: */ CDockContainerWidget* dockContainer() const; + /** + * Returns the largest minimumSizeHint() of the dock widgets in this + * area. + * The minimum size hint is updated if a dock widget is removed or added. + */ + virtual QSize minimumSizeHint() const override; + /** * Returns the rectangle of the title area */ diff --git a/src/DockWidget.cpp b/src/DockWidget.cpp index 48fb857..9de729d 100644 --- a/src/DockWidget.cpp +++ b/src/DockWidget.cpp @@ -82,6 +82,7 @@ struct DockWidgetPrivate QSize ToolBarIconSizeFloating = QSize(24, 24); bool IsFloatingTopLevel = false; QList TitleBarActions; + CDockWidget::eMinimumSizeHintMode MinimumSizeHintMode = CDockWidget::MinimumSizeHintFromDockWidget; /** * Private data constructor @@ -424,6 +425,13 @@ void CDockWidget::setToggleViewActionMode(eToggleViewActionMode Mode) } +//============================================================================ +void CDockWidget::setMinimumSizeHintMode(eMinimumSizeHintMode Mode) +{ + d->MinimumSizeHintMode = Mode; +} + + //============================================================================ void CDockWidget::toggleView(bool Open) { @@ -753,7 +761,14 @@ void CDockWidget::setClosedState(bool Closed) //============================================================================ QSize CDockWidget::minimumSizeHint() const { - return QSize(60, 40); + if (d->MinimumSizeHintMode == CDockWidget::MinimumSizeHintFromDockWidget || !d->Widget) + { + return QSize(60, 40); + } + else + { + return d->Widget->minimumSizeHint(); + } } diff --git a/src/DockWidget.h b/src/DockWidget.h index 1cb6f63..9d2e2ee 100644 --- a/src/DockWidget.h +++ b/src/DockWidget.h @@ -189,6 +189,23 @@ public: ForceNoScrollArea }; + + /** + * The mode of the minimumSizeHint() that is returned by the DockWidget + * minimumSizeHint() function. + * To ensure, that a dock widget does not block resizing, the dock widget + * reimplements minimumSizeHint() function to return a very small minimum + * size hint. If you would like to adhere the minimumSizeHint() from the + * content widget, the set the minimumSizeHintMode() to + * MinimumSizeHintFromContent. + */ + enum eMinimumSizeHintMode + { + MinimumSizeHintFromDockWidget, + MinimumSizeHintFromContent + }; + + /** * This mode configures the behavior of the toggle view action. * If the mode if ActionModeToggle, then the toggle view action is @@ -225,7 +242,8 @@ public: virtual ~CDockWidget(); /** - * We return a fixed minimum size hint for all dock widgets + * We return a fixed minimum size hint or the size hint of the content + * widget if minimum size hint mode is MinimumSizeHintFromContent */ virtual QSize minimumSizeHint() const override; @@ -334,6 +352,13 @@ public: */ void setToggleViewActionMode(eToggleViewActionMode Mode); + /** + * Configures the minimum size hint that is returned by the + * minimumSizeHint() function. + * \see eMinimumSizeHintMode for a detailed description + */ + void setMinimumSizeHintMode(eMinimumSizeHintMode Mode); + /** * Sets the dock widget icon that is shown in tabs and in toggle view * actions