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 <tim@krieglstein.org>
Reviewed-by: hjk <hjk121@nokiamail.com>
This commit is contained in:
hjk
2014-06-26 12:49:14 +02:00
parent 9a7fd4582b
commit 10c2de0797
4 changed files with 46 additions and 80 deletions

View File

@@ -29,6 +29,8 @@
#include "basetreeview.h"
#include <QDebug>
#include <QFontMetrics>
#include <QHeaderView>
#include <QItemDelegate>
#include <QLabel>
@@ -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();

View File

@@ -34,8 +34,6 @@
#include "itemviews.h"
#include <QTimer>
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

View File

@@ -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;

View File

@@ -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<WatchData> &list)
insertDataItem(list.at(i), false);
#endif
CHECK(checkTree());
emit columnAdjustmentRequested();
}
static void debugRecursion(QDebug &d, const WatchItem *item, int depth)