Fix left sidebar width after switching modes

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 <frost.asm@gmail.com>
Reviewed-by: André Hartmann <aha_1980@gmx.de>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2017-04-20 16:17:18 +02:00
parent 8eacd5af69
commit 1b30990f24
2 changed files with 33 additions and 31 deletions

View File

@@ -85,35 +85,33 @@ NavigationWidgetPlaceHolder::~NavigationWidgetPlaceHolder()
} }
} }
void NavigationWidgetPlaceHolder::applyStoredSize(int width) void NavigationWidgetPlaceHolder::applyStoredSize()
{ {
if (width) { QSplitter *splitter = qobject_cast<QSplitter *>(parentWidget());
QSplitter *splitter = qobject_cast<QSplitter *>(parentWidget()); if (splitter) {
if (splitter) { // A splitter we need to resize the splitter sizes
// A splitter we need to resize the splitter sizes QList<int> sizes = splitter->sizes();
QList<int> sizes = splitter->sizes(); int diff = 0;
int index = splitter->indexOf(this); int count = sizes.count();
int diff = width - sizes.at(index); for (int i = 0; i < sizes.count(); ++i) {
if (auto ph = qobject_cast<NavigationWidgetPlaceHolder *>(splitter->widget(i))) {
int count = sizes.count(); --count;
for (int i = 0; i < sizes.count(); ++i) { int width = ph->storedWidth();
if (qobject_cast<NavigationWidgetPlaceHolder *>(splitter->widget(i))) diff += width - sizes.at(i);
--count; sizes[i] = width;
} }
int adjust = count > 1 ? (diff / (count - 1)) : 0;
for (int i = 0; i < sizes.count(); ++i) {
if (!qobject_cast<NavigationWidgetPlaceHolder *>(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<NavigationWidgetPlaceHolder *>(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) { if (m_mode == mode) {
setCurrent(m_side, this); setCurrent(m_side, this);
int width = navigationWidget->storedWidth();
layout()->addWidget(navigationWidget); layout()->addWidget(navigationWidget);
navigationWidget->show(); navigationWidget->show();
applyStoredSize(width); applyStoredSize();
setVisible(navigationWidget->isShown()); setVisible(navigationWidget->isShown());
navigationWidget->placeHolderChanged(this); navigationWidget->placeHolderChanged(this);
} }
} }
int NavigationWidgetPlaceHolder::storedWidth() const
{
return NavigationWidget::instance(m_side)->storedWidth();
}
struct ActivationInfo { struct ActivationInfo {
Side side; Side side;
int position; int position;
@@ -474,7 +475,7 @@ void NavigationWidget::restoreSettings(QSettings *settings)
// Apply // Apply
if (NavigationWidgetPlaceHolder::current(d->m_side)) 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 // Restore last activation positions
settings->beginGroup(settingsGroup()); settings->beginGroup(settingsGroup());

View File

@@ -58,10 +58,11 @@ public:
virtual ~NavigationWidgetPlaceHolder(); virtual ~NavigationWidgetPlaceHolder();
static NavigationWidgetPlaceHolder *current(Side side); static NavigationWidgetPlaceHolder *current(Side side);
static void setCurrent(Side side, NavigationWidgetPlaceHolder *navWidget); static void setCurrent(Side side, NavigationWidgetPlaceHolder *navWidget);
void applyStoredSize(int width); void applyStoredSize();
private: private:
void currentModeAboutToChange(Id mode); void currentModeAboutToChange(Id mode);
int storedWidth() const;
Id m_mode; Id m_mode;
Side m_side; Side m_side;