Skip to content

Commit

Permalink
Add Config::setDockWidgetTabIndexOverrideFunc()
Browse files Browse the repository at this point in the history
Allows the user to manipulate the tab index when dockwidget
is being redocked.

Fixes issue #165, or is at least the workaround we can manage.
  • Loading branch information
iamsergio committed Jul 4, 2024
1 parent ae75f9a commit cec9ef9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Config::Private
DropIndicatorAllowedFunc m_dropIndicatorAllowedFunc = nullptr;
DragAboutToStartFunc m_dragAboutToStartFunc = nullptr;
DragEndedFunc m_dragEndedFunc = nullptr;
DockWidgetTabIndexOverrideFunc m_restorePlaceholderTabIndexOverride = nullptr;
ViewFactory *m_viewFactory = nullptr;
Flags m_flags = Flag_Default;
MDIFlags m_mdiFlags = MDIFlag_None;
Expand Down Expand Up @@ -276,6 +277,16 @@ DragEndedFunc Config::dragEndedFunc() const
return d->m_dragEndedFunc;
}

void Config::setDockWidgetTabIndexOverrideFunc(DockWidgetTabIndexOverrideFunc func)
{
d->m_restorePlaceholderTabIndexOverride = func;
}

DockWidgetTabIndexOverrideFunc Config::dockWidgetTabIndexOverrideFunc() const
{
return d->m_restorePlaceholderTabIndexOverride;
}

void Config::setAbsoluteWidgetMinSize(Size size)
{
if (!DockRegistry::self()->isEmpty(/*excludeBeingDeleted=*/false)) {
Expand Down
9 changes: 9 additions & 0 deletions src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ class MainWindow;
class DropArea;
class Draggable;
class ViewFactory;
class Group;
}

typedef KDDockWidgets::Core::DockWidget *(*DockWidgetFactoryFunc)(const QString &name);
typedef KDDockWidgets::Core::MainWindow *(*MainWindowFactoryFunc)(const QString &name, KDDockWidgets::MainWindowOptions);
typedef bool (*DragAboutToStartFunc)(Core::Draggable *draggable);
typedef void (*DragEndedFunc)();
typedef int (*DockWidgetTabIndexOverrideFunc)(Core::DockWidget *dw, Core::Group *group, int tabIndex);

/// @brief Function to allow more granularity to disallow where widgets are dropped
///
Expand Down Expand Up @@ -344,6 +346,13 @@ class DOCKS_EXPORT Config
void setDragEndedFunc(DragEndedFunc func);
DragEndedFunc dragEndedFunc() const;

/// Allows to override the tabindex when restoring a dock widget to a tab
/// When a dock widget is un-floated (by double click on titlebar) it goes to its
/// previous know position in the main window. If that position was tabbed, the provided
/// func can be used to change the actual tabIndex that's used.
void setDockWidgetTabIndexOverrideFunc(DockWidgetTabIndexOverrideFunc func);
DockWidgetTabIndexOverrideFunc dockWidgetTabIndexOverrideFunc() const;

///@brief Used internally by the framework. Returns the function which was passed to
/// setDropIndicatorAllowedFunc()
/// By default it's nullptr.
Expand Down
11 changes: 8 additions & 3 deletions src/core/Layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,15 @@ void Layout::restorePlaceholder(Core::DockWidget *dw, Core::Item *item, int tabI
KDDW_ERROR("Layout::restorePlaceholder: Trying to use a group that's being deleted");
}

if (tabIndex != -1 && group->dockWidgetCount() >= tabIndex) {
group->insertWidget(dw, tabIndex);
if (auto tabIndexFunc = Config::self().dockWidgetTabIndexOverrideFunc()) {
// The user wishes to control the tabIndex himself
group->insertWidget(dw, tabIndexFunc(dw, group, tabIndex));
} else {
group->addTab(dw);
if (tabIndex != -1 && group->dockWidgetCount() >= tabIndex) {
group->insertWidget(dw, tabIndex);
} else {
group->addTab(dw);
}
}

group->Controller::setVisible(true);
Expand Down
24 changes: 24 additions & 0 deletions tests/tst_docks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3081,6 +3081,29 @@ KDDW_QCORO_TASK tst_restoreGroupOptions()
KDDW_TEST_RETURN(true);
}

KDDW_QCORO_TASK tst_dockWidgetTabIndexOverride()
{
EnsureTopLevelsDeleted e;
Config::self().setDockWidgetTabIndexOverrideFunc([](Core::DockWidget *, Core::Group *, int) {
return 0;
});

auto m =
createMainWindow(Size(500, 500), MainWindowOption_None);
auto dock1 = createDockWidget("1", Platform::instance()->tests_createView({ true }));
auto dock2 = createDockWidget("2", Platform::instance()->tests_createView({ true }));
m->addDockWidget(dock1, Location_OnTop);
dock1->addDockWidgetAsTab(dock2);

/// Redocking will use our override func and move it to index 0
CHECK(dock2->tabIndex() == 1);
dock2->setFloating(true);
dock2->setFloating(false);
CHECK(dock2->tabIndex() == 0);

KDDW_TEST_RETURN(true);
}

KDDW_QCORO_TASK tst_restoreWithCentralFrameWithTabs()
{
EnsureTopLevelsDeleted e;
Expand Down Expand Up @@ -6398,6 +6421,7 @@ static const auto s_tests = std::vector<KDDWTest>
TEST(tst_mainWindowToggle),
TEST(tst_startDragging),
#if !defined(KDDW_FRONTEND_FLUTTER)
TEST(tst_dockWidgetTabIndexOverride),
TEST(tst_restoreWithCentralFrameWithTabs),
TEST(tst_preferredInitialSize),
TEST(tst_preferredInitialSizeVsMinSize),
Expand Down
1 change: 1 addition & 0 deletions tests/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ struct EnsureTopLevelsDeleted
// Other cleanup, since we use this class everywhere
Config::self().setDockWidgetFactoryFunc(nullptr);
Config::self().setMainWindowFactoryFunc(nullptr);
Config::self().setDockWidgetTabIndexOverrideFunc(nullptr);
Config::self().setInternalFlags(m_originalInternalFlags);
Config::self().setFlags(m_originalFlags);
Config::self().setMDIFlags(m_originalMDIFlags);
Expand Down

0 comments on commit cec9ef9

Please sign in to comment.