diff --git a/Changelog b/Changelog index 204b9bee..751d3068 100644 --- a/Changelog +++ b/Changelog @@ -3,6 +3,7 @@ - Added static DockWidgetBase::byName() (#126) - SOURCE-COMPAT-BREAK: The enum KDDockWidgets::AddingOption has been renamed to KDDockWidgets::InitialVisibilityOption + - You can now pass a preferred initial size to MainWindow::addDockWidget() (#95) * v1.2.1 (unreleased) - Support for resizing dock widgets when they are in overlay/popup mode (autohide/sidebar feature) diff --git a/examples/minimal/main.cpp b/examples/minimal/main.cpp index 2d99c5dd..dd0ad3bf 100644 --- a/examples/minimal/main.cpp +++ b/examples/minimal/main.cpp @@ -62,6 +62,10 @@ int main(int argc, char **argv) QStringLiteral(":/assets/KDAB_bubble_fulcolor.png")); dock4->setWidget(widget4); + auto dock5 = new KDDockWidgets::DockWidget(QStringLiteral("MyDock5")); + auto widget5 = new MyWidget(QStringLiteral(":/assets/base.png"), + QStringLiteral(":/assets/KDAB_bubble_fulcolor.png")); + dock5->setWidget(widget5); // 3. Add them to the main window mainWindow.addDockWidget(dock1, KDDockWidgets::Location_OnLeft); @@ -70,8 +74,13 @@ int main(int argc, char **argv) // 4. Add dock3 to the right of dock2 mainWindow.addDockWidget(dock3, KDDockWidgets::Location_OnRight, dock2); - // 5. dock4 will be its own top level (floating window) - dock4->show(); + // 5. dock4 is docked at the bottom, with 200px height + const QSize preferredSize(QSize(/*ignored*/0, 200)); + mainWindow.addDockWidget(dock4, KDDockWidgets::Location_OnBottom, nullptr, preferredSize); + + + // 5. dock5 will be its own top level (floating window) + dock5->show(); return app.exec(); } diff --git a/src/KDDockWidgets.h b/src/KDDockWidgets.h index 5778d9b1..4a37aff8 100644 --- a/src/KDDockWidgets.h +++ b/src/KDDockWidgets.h @@ -88,18 +88,27 @@ namespace KDDockWidgets InitialOption(InitialVisibilityOption v) : visibility(v) {} - /*InitialOption(QSize size) + InitialOption(QSize size) : preferredSize(size) {} InitialOption(InitialVisibilityOption v, QSize size) : visibility(v) , preferredSize(size) - {}*/ + {} bool startsHidden() const { return visibility == InitialVisibilityOption::StartHidden; } + int preferredLength(Qt::Orientation o) const { + return o == Qt::Horizontal ? preferredSize.width() + : preferredSize.height(); + } + + bool hasPreferredLength(Qt::Orientation o) const { + return preferredLength(o) > 0; + } + /** * @brief Allows a dock widget to be docked as hidden. * @@ -117,7 +126,7 @@ namespace KDDockWidgets * dock widget to the left then only the preferred width will be taken into account, as the * height will simply fill the whole layout. */ - //const QSize preferredSize; not yet done. + const QSize preferredSize; private: friend class Layouting::Item; diff --git a/src/private/multisplitter/Item.cpp b/src/private/multisplitter/Item.cpp index b9fad5b4..af580121 100644 --- a/src/private/multisplitter/Item.cpp +++ b/src/private/multisplitter/Item.cpp @@ -3603,23 +3603,28 @@ void SizingInfo::fromVariantMap(const QVariantMap &map) int ItemContainer::Private::defaultLengthFor(Item *item, InitialOption option) const { int result = 0; - switch (option.sizeMode) { - case DefaultSizeMode::None: - break; - case DefaultSizeMode::Fair: { - const int numVisibleChildren = q->numVisibleChildren() + 1; // +1 so it counts with @p item too, which we're adding - const int usableLength = q->length() - (Item::separatorThickness*(numVisibleChildren - 1)); - result = usableLength / numVisibleChildren; - break; - } - case DefaultSizeMode::FairButFloor: { - int length = defaultLengthFor(item, DefaultSizeMode::Fair); - result = qMin(length, item->length(m_orientation)); - break; - } - case DefaultSizeMode::ItemSize: - result = item->length(m_orientation); - break; + + if (option.hasPreferredLength(m_orientation) && option.sizeMode != DefaultSizeMode::None) { + result = option.preferredLength(m_orientation); + } else { + switch (option.sizeMode) { + case DefaultSizeMode::None: + break; + case DefaultSizeMode::Fair: { + const int numVisibleChildren = q->numVisibleChildren() + 1; // +1 so it counts with @p item too, which we're adding + const int usableLength = q->length() - (Item::separatorThickness*(numVisibleChildren - 1)); + result = usableLength / numVisibleChildren; + break; + } + case DefaultSizeMode::FairButFloor: { + int length = defaultLengthFor(item, DefaultSizeMode::Fair); + result = qMin(length, item->length(m_orientation)); + break; + } + case DefaultSizeMode::ItemSize: + result = item->length(m_orientation); + break; + } } result = qMax(item->minLength(m_orientation), result); // bound with max-size too diff --git a/tests/tst_docks.cpp b/tests/tst_docks.cpp index b03d5058..7344f39c 100644 --- a/tests/tst_docks.cpp +++ b/tests/tst_docks.cpp @@ -161,6 +161,7 @@ private Q_SLOTS: void tst_placeholderDisappearsOnReadd(); void tst_placeholdersAreRemovedProperly(); void tst_floatMaintainsSize(); + void tst_preferredInitialSize(); void tst_crash2_data(); void tst_crash2(); @@ -2245,6 +2246,19 @@ void TestDocks::tst_floatMaintainsSize() delete dw2->window(); } +void TestDocks::tst_preferredInitialSize() +{ + EnsureTopLevelsDeleted e; + auto dw1 = new DockWidgetType("1"); + auto dw2 = new DockWidgetType("2"); + auto m = createMainWindow(QSize(1200, 1200), MainWindowOption_None); + + m->addDockWidget(dw1, Location_OnTop); + m->addDockWidget(dw2, Location_OnBottom, nullptr, QSize(0, 200)); + + QCOMPARE(dw2->frame()->height(), 200); +} + void TestDocks::tst_crash2_data() { QTest::addColumn("show");