From 1b30990f24e6cf2150b268ece841857eed7222a0 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Thu, 20 Apr 2017 16:17:18 +0200 Subject: [PATCH] Fix left sidebar width after switching modes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After switching to debug mode, hiding left sidebar, switching back to edit, and showing left sidebar, the sidebar had very small width. We may not calculate the splitter sizes for each placeholder individually, because setting them on the splitter might distribute them in a weird way when widgets are hidden. In the above example switching back to edit mode while both sidebars are hidden triggered something similar to the following updates: Left side bar update: - calculated: (300, 1000, 0) - actual resulting: (0, 1000, 0) Right side bar update: - calculated: (0, 1000, 300) - actual resulting: (100, 900, 0) In the longer run it would probably be better handle the resizing in a more centralized way, since now it sets the same sizes twice (once for each side bar update). Task-number: QTCREATORBUG-18009 Change-Id: Ife5d6f1caded55f444245f4c3c98ae05371363b8 Reviewed-by: Serhii Moroz Reviewed-by: André Hartmann Reviewed-by: David Schulz --- src/plugins/coreplugin/navigationwidget.cpp | 61 +++++++++++---------- src/plugins/coreplugin/navigationwidget.h | 3 +- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/plugins/coreplugin/navigationwidget.cpp b/src/plugins/coreplugin/navigationwidget.cpp index 32ef0988df2..5577f05b850 100644 --- a/src/plugins/coreplugin/navigationwidget.cpp +++ b/src/plugins/coreplugin/navigationwidget.cpp @@ -85,35 +85,33 @@ NavigationWidgetPlaceHolder::~NavigationWidgetPlaceHolder() } } -void NavigationWidgetPlaceHolder::applyStoredSize(int width) +void NavigationWidgetPlaceHolder::applyStoredSize() { - if (width) { - QSplitter *splitter = qobject_cast(parentWidget()); - if (splitter) { - // A splitter we need to resize the splitter sizes - QList sizes = splitter->sizes(); - int index = splitter->indexOf(this); - int diff = width - sizes.at(index); - - int count = sizes.count(); - for (int i = 0; i < sizes.count(); ++i) { - if (qobject_cast(splitter->widget(i))) - --count; + QSplitter *splitter = qobject_cast(parentWidget()); + if (splitter) { + // A splitter we need to resize the splitter sizes + QList sizes = splitter->sizes(); + int diff = 0; + int count = sizes.count(); + for (int i = 0; i < sizes.count(); ++i) { + if (auto ph = qobject_cast(splitter->widget(i))) { + --count; + int width = ph->storedWidth(); + diff += width - sizes.at(i); + sizes[i] = width; } - - int adjust = count > 1 ? (diff / (count - 1)) : 0; - for (int i = 0; i < sizes.count(); ++i) { - if (!qobject_cast(splitter->widget(i))) - sizes[i] += adjust; - } - - sizes[index] = width; - splitter->setSizes(sizes); - } else { - QSize s = size(); - s.setWidth(width); - resize(s); } + int adjust = count > 1 ? (diff / (count - 1)) : 0; + for (int i = 0; i < sizes.count(); ++i) { + if (!qobject_cast(splitter->widget(i))) + sizes[i] += adjust; + } + + splitter->setSizes(sizes); + } else { + QSize s = size(); + s.setWidth(storedWidth()); + resize(s); } } @@ -138,17 +136,20 @@ void NavigationWidgetPlaceHolder::currentModeAboutToChange(Id mode) if (m_mode == mode) { setCurrent(m_side, this); - int width = navigationWidget->storedWidth(); - layout()->addWidget(navigationWidget); navigationWidget->show(); - applyStoredSize(width); + applyStoredSize(); setVisible(navigationWidget->isShown()); navigationWidget->placeHolderChanged(this); } } +int NavigationWidgetPlaceHolder::storedWidth() const +{ + return NavigationWidget::instance(m_side)->storedWidth(); +} + struct ActivationInfo { Side side; int position; @@ -474,7 +475,7 @@ void NavigationWidget::restoreSettings(QSettings *settings) // Apply if (NavigationWidgetPlaceHolder::current(d->m_side)) - NavigationWidgetPlaceHolder::current(d->m_side)->applyStoredSize(d->m_width); + NavigationWidgetPlaceHolder::current(d->m_side)->applyStoredSize(); // Restore last activation positions settings->beginGroup(settingsGroup()); diff --git a/src/plugins/coreplugin/navigationwidget.h b/src/plugins/coreplugin/navigationwidget.h index 08d2e29f91b..4de2d8d95c8 100644 --- a/src/plugins/coreplugin/navigationwidget.h +++ b/src/plugins/coreplugin/navigationwidget.h @@ -58,10 +58,11 @@ public: virtual ~NavigationWidgetPlaceHolder(); static NavigationWidgetPlaceHolder *current(Side side); static void setCurrent(Side side, NavigationWidgetPlaceHolder *navWidget); - void applyStoredSize(int width); + void applyStoredSize(); private: void currentModeAboutToChange(Id mode); + int storedWidth() const; Id m_mode; Side m_side;