From 10c2de079790255f74c06ae649dfddc3e2fec6ba Mon Sep 17 00:00:00 2001 From: hjk Date: Thu, 26 Jun 2014 12:49:14 +0200 Subject: [PATCH] Utils: Even more treeview column size fine tuning Do it manually now, directly. Changing behavior flags and waiting for the view to act by itself turned out to be too fragile. Change-Id: I31014219b8b20582401bf0431fb805b683aa953f Reviewed-by: Tim Sander Reviewed-by: hjk --- src/libs/utils/basetreeview.cpp | 94 ++++++++++--------------- src/libs/utils/basetreeview.h | 12 +--- src/plugins/debugger/debuggerplugin.cpp | 17 ++--- src/plugins/debugger/watchhandler.cpp | 3 + 4 files changed, 46 insertions(+), 80 deletions(-) diff --git a/src/libs/utils/basetreeview.cpp b/src/libs/utils/basetreeview.cpp index 1bced761d2e..7114021e63b 100644 --- a/src/libs/utils/basetreeview.cpp +++ b/src/libs/utils/basetreeview.cpp @@ -29,6 +29,8 @@ #include "basetreeview.h" +#include +#include #include #include #include @@ -75,36 +77,21 @@ BaseTreeView::BaseTreeView(QWidget *parent) SLOT(rowClickedHelper(QModelIndex))); connect(header(), SIGNAL(sectionClicked(int)), SLOT(toggleColumnWidth(int))); - - m_alwaysAdjustColumns = false; - - m_layoutTimer.setSingleShot(true); - m_layoutTimer.setInterval(20); - connect(&m_layoutTimer, SIGNAL(timeout()), this, SLOT(resizeColumnsFinish())); } -void BaseTreeView::setModel(QAbstractItemModel *model) +void BaseTreeView::setModel(QAbstractItemModel *m) { - disconnectColumnAdjustment(); - Utils::TreeView::setModel(model); - connectColumnAdjustment(); -} - -void BaseTreeView::connectColumnAdjustment() -{ - if (m_alwaysAdjustColumns && model()) { - connect(this, SIGNAL(expanded(QModelIndex)), this, SLOT(resizeColumns())); - connect(this, SIGNAL(collapsed(QModelIndex)), this, SLOT(resizeColumns())); - connect(model(), SIGNAL(layoutChanged()), this, SLOT(resizeColumns())); + const char *sig = "columnAdjustmentRequested()"; + if (model()) { + int index = model()->metaObject()->indexOfSignal(sig); + if (index != -1) + disconnect(model(), SIGNAL(columnAdjustmentRequested()), this, SLOT(resizeColumns())); } -} - -void BaseTreeView::disconnectColumnAdjustment() -{ - if (m_alwaysAdjustColumns && model()) { - disconnect(this, SIGNAL(expanded(QModelIndex)), this, SLOT(resizeColumns())); - disconnect(this, SIGNAL(collapsed(QModelIndex)), this, SLOT(resizeColumns())); - disconnect(model(), SIGNAL(layoutChanged()), this, SLOT(resizeColumns())); + Utils::TreeView::setModel(m); + if (m) { + int index = m->metaObject()->indexOfSignal(sig); + if (index != -1) + connect(m, SIGNAL(columnAdjustmentRequested()), this, SLOT(resizeColumns())); } } @@ -121,61 +108,54 @@ void BaseTreeView::resizeColumns() QHeaderView *h = header(); if (!h) return; - const int n = h->count(); - if (n) { - for (int i = 0; i != n; ++i) - h->setResizeMode(i, QHeaderView::ResizeToContents); - m_layoutTimer.start(); + + for (int i = 0, n = h->count(); i != n; ++i) { + int targetSize = suggestedColumnSize(i); + if (targetSize > 0) + h->resizeSection(i, targetSize); } } -void BaseTreeView::resizeColumnsFinish() +int BaseTreeView::suggestedColumnSize(int column) const { QHeaderView *h = header(); if (!h) - return; + return -1; + QModelIndex a = indexAt(QPoint(1, 1)); + a = a.sibling(a.row(), column); QFontMetrics fm(font()); - for (int i = 0, n = h->count(); i != n; ++i) { - int headerSize = fm.width(model()->headerData(i, Qt::Horizontal).toString()); - int targetSize = qMax(sizeHintForColumn(i), headerSize); - if (targetSize > 0) { - h->setResizeMode(i, QHeaderView::Interactive); - h->resizeSection(i, targetSize); + int m = fm.width(model()->headerData(column, Qt::Horizontal).toString()); + const int ind = indentation(); + for (int i = 0; i < 100 && a.isValid(); ++i) { + const QString s = model()->data(a).toString(); + int w = fm.width(s) + 10; + if (column == 0) { + for (QModelIndex b = a.parent(); b.isValid(); b = b.parent()) + w += ind; } + if (w > m) + m = w; + a = indexBelow(a); } + return m; } void BaseTreeView::toggleColumnWidth(int logicalIndex) { QHeaderView *h = header(); const int currentSize = h->sectionSize(logicalIndex); - if (currentSize == sizeHintForColumn(logicalIndex)) { + const int suggestedSize = suggestedColumnSize(logicalIndex); + if (currentSize == suggestedSize) { QFontMetrics fm(font()); int headerSize = fm.width(model()->headerData(logicalIndex, Qt::Horizontal).toString()); int minSize = 10 * fm.width(QLatin1Char('x')); h->resizeSection(logicalIndex, qMax(minSize, headerSize)); } else { - resizeColumnToContents(logicalIndex); + h->resizeSection(logicalIndex, suggestedSize); } } -void BaseTreeView::reset() -{ - Utils::TreeView::reset(); - if (m_alwaysAdjustColumns) - resizeColumns(); -} - -void BaseTreeView::setAlwaysAdjustColumns(bool on) -{ - if (on == m_alwaysAdjustColumns) - return; - disconnectColumnAdjustment(); - m_alwaysAdjustColumns = on; - connectColumnAdjustment(); -} - QModelIndexList BaseTreeView::activeRows() const { QItemSelectionModel *selection = selectionModel(); diff --git a/src/libs/utils/basetreeview.h b/src/libs/utils/basetreeview.h index cdfd1ce7dd7..f034abb1dbc 100644 --- a/src/libs/utils/basetreeview.h +++ b/src/libs/utils/basetreeview.h @@ -34,8 +34,6 @@ #include "itemviews.h" -#include - namespace Utils { class QTCREATOR_UTILS_EXPORT BaseTreeView : public Utils::TreeView @@ -53,24 +51,16 @@ public: void mousePressEvent(QMouseEvent *ev); public slots: - void reset(); - void resizeColumns(); void setAlternatingRowColorsHelper(bool on) { setAlternatingRowColors(on); } - void setAlwaysAdjustColumns(bool on); private slots: - void resizeColumnsFinish(); void rowActivatedHelper(const QModelIndex &index) { rowActivated(index); } void rowClickedHelper(const QModelIndex &index) { rowClicked(index); } void toggleColumnWidth(int logicalIndex); private: - void connectColumnAdjustment(); - void disconnectColumnAdjustment(); - - bool m_alwaysAdjustColumns; - QTimer m_layoutTimer; + int suggestedColumnSize(int column) const; }; } // namespace Utils diff --git a/src/plugins/debugger/debuggerplugin.cpp b/src/plugins/debugger/debuggerplugin.cpp index 59c018f2796..378461135f9 100644 --- a/src/plugins/debugger/debuggerplugin.cpp +++ b/src/plugins/debugger/debuggerplugin.cpp @@ -531,20 +531,13 @@ public: /////////////////////////////////////////////////////////////////////// static QWidget *addSearch(BaseTreeView *treeView, const QString &title, - const char *objectName, bool adjustColumns = false) + const char *objectName) { QAction *act = debuggerCore()->action(UseAlternatingRowColors); treeView->setAlternatingRowColors(act->isChecked()); QObject::connect(act, SIGNAL(toggled(bool)), treeView, SLOT(setAlternatingRowColorsHelper(bool))); - if (adjustColumns) { - act = debuggerCore()->action(AlwaysAdjustColumnWidths); - treeView->setAlwaysAdjustColumns(act->isChecked()); - QObject::connect(act, SIGNAL(toggled(bool)), - treeView, SLOT(setAlwaysAdjustColumns(bool))); - } - QWidget *widget = TreeViewFind::createSearchableWrapper(treeView); widget->setObjectName(QLatin1String(objectName)); widget->setWindowTitle(title); @@ -2815,16 +2808,16 @@ void DebuggerPluginPrivate::extensionsInitialized() m_threadsWindow = addSearch(m_threadsView, tr("Threads"), DOCKWIDGET_THREADS); m_returnView = new WatchTreeView(ReturnType); - m_returnWindow = addSearch(m_returnView, tr("Locals and Expressions"), "CppDebugReturn", true); + m_returnWindow = addSearch(m_returnView, tr("Locals and Expressions"), "CppDebugReturn"); m_localsView = new WatchTreeView(LocalsType); - m_localsWindow = addSearch(m_localsView, tr("Locals and Expressions"), "CppDebugLocals", true); + m_localsWindow = addSearch(m_localsView, tr("Locals and Expressions"), "CppDebugLocals"); m_watchersView = new WatchTreeView(WatchersType); - m_watchersWindow = addSearch(m_watchersView, tr("Locals and Expressions"), "CppDebugWatchers", true); + m_watchersWindow = addSearch(m_watchersView, tr("Locals and Expressions"), "CppDebugWatchers"); m_inspectorView = new WatchTreeView(InspectType); - m_inspectorWindow = addSearch(m_inspectorView, tr("Locals and Expressions"), "Inspector", true); + m_inspectorWindow = addSearch(m_inspectorView, tr("Locals and Expressions"), "Inspector"); // Snapshot m_snapshotHandler = new SnapshotHandler; diff --git a/src/plugins/debugger/watchhandler.cpp b/src/plugins/debugger/watchhandler.cpp index ec1e6623c6f..754e09b67b5 100644 --- a/src/plugins/debugger/watchhandler.cpp +++ b/src/plugins/debugger/watchhandler.cpp @@ -242,6 +242,7 @@ public: signals: void currentIndexRequested(const QModelIndex &idx); void itemIsExpanded(const QModelIndex &idx); + void columnAdjustmentRequested(); private: QVariant data(const QModelIndex &idx, int role) const; @@ -1248,6 +1249,7 @@ bool WatchModel::setData(const QModelIndex &idx, const QVariant &value, int role } else { m_expandedINames.remove(data.iname); } + emit columnAdjustmentRequested(); break; case LocalsTypeFormatRole: @@ -1566,6 +1568,7 @@ void WatchModel::insertBulkData(const QList &list) insertDataItem(list.at(i), false); #endif CHECK(checkTree()); + emit columnAdjustmentRequested(); } static void debugRecursion(QDebug &d, const WatchItem *item, int depth)