Skip to content

Commit

Permalink
Fix splits leaving empty gap after being deleted (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
falbru authored Nov 15, 2024
1 parent c4b4ebe commit 0b15032
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void Container::connectWidget(QWidget *widget)
connect(widget, &QWidget::destroyed, this, [=]() {
if (m_splitter->count() == 0)
{
setParent(nullptr);
deleteLater();
}
else
Expand Down
48 changes: 28 additions & 20 deletions src/splitcontainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,39 @@ SplitContainer::SplitContainer(Qt::Orientation orientation, QWidget *parent) : C

void SplitContainer::split(QWidget *source_widget, QWidget *new_widget, Qt::Orientation orientation)
{
disconnectWidget(source_widget);
if (m_splitter->count() <= 1)
{
addWidget(new_widget);
m_splitter->setOrientation(orientation);
}
else
{
disconnectWidget(source_widget);

int index = m_splitter->indexOf(source_widget);
int index = m_splitter->indexOf(source_widget);

SplitContainer *container = new SplitContainer(orientation, this);
SplitContainer *container = new SplitContainer(orientation, this);

// NOTE: replaceWidget() will move focus to another widget, which will screw up the focus order in
// LastFocusedFilter. We therefore clear the focus before calling the function, and reapply the focus after.
QWidget *regain_focus_widget = nullptr;
if (source_widget->isAncestorOf(focusWidget()))
{
regain_focus_widget = focusWidget();
regain_focus_widget->clearFocus();
}
// NOTE: replaceWidget() will move focus to another widget, which will screw up the focus order in
// LastFocusedFilter. We therefore clear the focus before calling the function, and reapply the focus after.
QWidget *regain_focus_widget = nullptr;
if (source_widget->isAncestorOf(focusWidget()))
{
regain_focus_widget = focusWidget();
regain_focus_widget->clearFocus();
}

m_splitter->replaceWidget(index, container);
m_splitter->replaceWidget(index, container);

if (regain_focus_widget)
regain_focus_widget->setFocus();
if (regain_focus_widget)
regain_focus_widget->setFocus();

// NOTE: Adding the source_widget to the container after calling replaceWidget() will ensure that the splitter ratio
// will be preserved.
container->addWidget(source_widget);
container->addWidget(new_widget);
container->m_splitter->setSizes(QList<int>({INT_MAX, INT_MAX}));
// NOTE: Adding the source_widget to the container after calling replaceWidget() will ensure that the splitter
// ratio will be preserved.
container->addWidget(source_widget);
container->addWidget(new_widget);
container->m_splitter->setSizes(QList<int>({INT_MAX, INT_MAX}));

connectWidget(container);
connectWidget(container);
}
}

0 comments on commit 0b15032

Please sign in to comment.