forked from qt-creator/qt-creator
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:
@@ -29,6 +29,8 @@
|
|||||||
|
|
||||||
#include "basetreeview.h"
|
#include "basetreeview.h"
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QFontMetrics>
|
||||||
#include <QHeaderView>
|
#include <QHeaderView>
|
||||||
#include <QItemDelegate>
|
#include <QItemDelegate>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
@@ -75,36 +77,21 @@ BaseTreeView::BaseTreeView(QWidget *parent)
|
|||||||
SLOT(rowClickedHelper(QModelIndex)));
|
SLOT(rowClickedHelper(QModelIndex)));
|
||||||
connect(header(), SIGNAL(sectionClicked(int)),
|
connect(header(), SIGNAL(sectionClicked(int)),
|
||||||
SLOT(toggleColumnWidth(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();
|
const char *sig = "columnAdjustmentRequested()";
|
||||||
Utils::TreeView::setModel(model);
|
if (model()) {
|
||||||
connectColumnAdjustment();
|
int index = model()->metaObject()->indexOfSignal(sig);
|
||||||
}
|
if (index != -1)
|
||||||
|
disconnect(model(), SIGNAL(columnAdjustmentRequested()), this, SLOT(resizeColumns()));
|
||||||
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()));
|
|
||||||
}
|
}
|
||||||
}
|
Utils::TreeView::setModel(m);
|
||||||
|
if (m) {
|
||||||
void BaseTreeView::disconnectColumnAdjustment()
|
int index = m->metaObject()->indexOfSignal(sig);
|
||||||
{
|
if (index != -1)
|
||||||
if (m_alwaysAdjustColumns && model()) {
|
connect(m, SIGNAL(columnAdjustmentRequested()), this, SLOT(resizeColumns()));
|
||||||
disconnect(this, SIGNAL(expanded(QModelIndex)), this, SLOT(resizeColumns()));
|
|
||||||
disconnect(this, SIGNAL(collapsed(QModelIndex)), this, SLOT(resizeColumns()));
|
|
||||||
disconnect(model(), SIGNAL(layoutChanged()), this, SLOT(resizeColumns()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,61 +108,54 @@ void BaseTreeView::resizeColumns()
|
|||||||
QHeaderView *h = header();
|
QHeaderView *h = header();
|
||||||
if (!h)
|
if (!h)
|
||||||
return;
|
return;
|
||||||
const int n = h->count();
|
|
||||||
if (n) {
|
for (int i = 0, n = h->count(); i != n; ++i) {
|
||||||
for (int i = 0; i != n; ++i)
|
int targetSize = suggestedColumnSize(i);
|
||||||
h->setResizeMode(i, QHeaderView::ResizeToContents);
|
if (targetSize > 0)
|
||||||
m_layoutTimer.start();
|
h->resizeSection(i, targetSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseTreeView::resizeColumnsFinish()
|
int BaseTreeView::suggestedColumnSize(int column) const
|
||||||
{
|
{
|
||||||
QHeaderView *h = header();
|
QHeaderView *h = header();
|
||||||
if (!h)
|
if (!h)
|
||||||
return;
|
return -1;
|
||||||
|
|
||||||
|
QModelIndex a = indexAt(QPoint(1, 1));
|
||||||
|
a = a.sibling(a.row(), column);
|
||||||
QFontMetrics fm(font());
|
QFontMetrics fm(font());
|
||||||
for (int i = 0, n = h->count(); i != n; ++i) {
|
int m = fm.width(model()->headerData(column, Qt::Horizontal).toString());
|
||||||
int headerSize = fm.width(model()->headerData(i, Qt::Horizontal).toString());
|
const int ind = indentation();
|
||||||
int targetSize = qMax(sizeHintForColumn(i), headerSize);
|
for (int i = 0; i < 100 && a.isValid(); ++i) {
|
||||||
if (targetSize > 0) {
|
const QString s = model()->data(a).toString();
|
||||||
h->setResizeMode(i, QHeaderView::Interactive);
|
int w = fm.width(s) + 10;
|
||||||
h->resizeSection(i, targetSize);
|
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)
|
void BaseTreeView::toggleColumnWidth(int logicalIndex)
|
||||||
{
|
{
|
||||||
QHeaderView *h = header();
|
QHeaderView *h = header();
|
||||||
const int currentSize = h->sectionSize(logicalIndex);
|
const int currentSize = h->sectionSize(logicalIndex);
|
||||||
if (currentSize == sizeHintForColumn(logicalIndex)) {
|
const int suggestedSize = suggestedColumnSize(logicalIndex);
|
||||||
|
if (currentSize == suggestedSize) {
|
||||||
QFontMetrics fm(font());
|
QFontMetrics fm(font());
|
||||||
int headerSize = fm.width(model()->headerData(logicalIndex, Qt::Horizontal).toString());
|
int headerSize = fm.width(model()->headerData(logicalIndex, Qt::Horizontal).toString());
|
||||||
int minSize = 10 * fm.width(QLatin1Char('x'));
|
int minSize = 10 * fm.width(QLatin1Char('x'));
|
||||||
h->resizeSection(logicalIndex, qMax(minSize, headerSize));
|
h->resizeSection(logicalIndex, qMax(minSize, headerSize));
|
||||||
} else {
|
} 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
|
QModelIndexList BaseTreeView::activeRows() const
|
||||||
{
|
{
|
||||||
QItemSelectionModel *selection = selectionModel();
|
QItemSelectionModel *selection = selectionModel();
|
||||||
|
@@ -34,8 +34,6 @@
|
|||||||
|
|
||||||
#include "itemviews.h"
|
#include "itemviews.h"
|
||||||
|
|
||||||
#include <QTimer>
|
|
||||||
|
|
||||||
namespace Utils {
|
namespace Utils {
|
||||||
|
|
||||||
class QTCREATOR_UTILS_EXPORT BaseTreeView : public Utils::TreeView
|
class QTCREATOR_UTILS_EXPORT BaseTreeView : public Utils::TreeView
|
||||||
@@ -53,24 +51,16 @@ public:
|
|||||||
void mousePressEvent(QMouseEvent *ev);
|
void mousePressEvent(QMouseEvent *ev);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void reset();
|
|
||||||
|
|
||||||
void resizeColumns();
|
void resizeColumns();
|
||||||
void setAlternatingRowColorsHelper(bool on) { setAlternatingRowColors(on); }
|
void setAlternatingRowColorsHelper(bool on) { setAlternatingRowColors(on); }
|
||||||
void setAlwaysAdjustColumns(bool on);
|
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void resizeColumnsFinish();
|
|
||||||
void rowActivatedHelper(const QModelIndex &index) { rowActivated(index); }
|
void rowActivatedHelper(const QModelIndex &index) { rowActivated(index); }
|
||||||
void rowClickedHelper(const QModelIndex &index) { rowClicked(index); }
|
void rowClickedHelper(const QModelIndex &index) { rowClicked(index); }
|
||||||
void toggleColumnWidth(int logicalIndex);
|
void toggleColumnWidth(int logicalIndex);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void connectColumnAdjustment();
|
int suggestedColumnSize(int column) const;
|
||||||
void disconnectColumnAdjustment();
|
|
||||||
|
|
||||||
bool m_alwaysAdjustColumns;
|
|
||||||
QTimer m_layoutTimer;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Utils
|
} // namespace Utils
|
||||||
|
@@ -531,20 +531,13 @@ public:
|
|||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
static QWidget *addSearch(BaseTreeView *treeView, const QString &title,
|
static QWidget *addSearch(BaseTreeView *treeView, const QString &title,
|
||||||
const char *objectName, bool adjustColumns = false)
|
const char *objectName)
|
||||||
{
|
{
|
||||||
QAction *act = debuggerCore()->action(UseAlternatingRowColors);
|
QAction *act = debuggerCore()->action(UseAlternatingRowColors);
|
||||||
treeView->setAlternatingRowColors(act->isChecked());
|
treeView->setAlternatingRowColors(act->isChecked());
|
||||||
QObject::connect(act, SIGNAL(toggled(bool)),
|
QObject::connect(act, SIGNAL(toggled(bool)),
|
||||||
treeView, SLOT(setAlternatingRowColorsHelper(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);
|
QWidget *widget = TreeViewFind::createSearchableWrapper(treeView);
|
||||||
widget->setObjectName(QLatin1String(objectName));
|
widget->setObjectName(QLatin1String(objectName));
|
||||||
widget->setWindowTitle(title);
|
widget->setWindowTitle(title);
|
||||||
@@ -2815,16 +2808,16 @@ void DebuggerPluginPrivate::extensionsInitialized()
|
|||||||
m_threadsWindow = addSearch(m_threadsView, tr("Threads"), DOCKWIDGET_THREADS);
|
m_threadsWindow = addSearch(m_threadsView, tr("Threads"), DOCKWIDGET_THREADS);
|
||||||
|
|
||||||
m_returnView = new WatchTreeView(ReturnType);
|
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_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_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_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
|
// Snapshot
|
||||||
m_snapshotHandler = new SnapshotHandler;
|
m_snapshotHandler = new SnapshotHandler;
|
||||||
|
@@ -242,6 +242,7 @@ public:
|
|||||||
signals:
|
signals:
|
||||||
void currentIndexRequested(const QModelIndex &idx);
|
void currentIndexRequested(const QModelIndex &idx);
|
||||||
void itemIsExpanded(const QModelIndex &idx);
|
void itemIsExpanded(const QModelIndex &idx);
|
||||||
|
void columnAdjustmentRequested();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QVariant data(const QModelIndex &idx, int role) const;
|
QVariant data(const QModelIndex &idx, int role) const;
|
||||||
@@ -1248,6 +1249,7 @@ bool WatchModel::setData(const QModelIndex &idx, const QVariant &value, int role
|
|||||||
} else {
|
} else {
|
||||||
m_expandedINames.remove(data.iname);
|
m_expandedINames.remove(data.iname);
|
||||||
}
|
}
|
||||||
|
emit columnAdjustmentRequested();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LocalsTypeFormatRole:
|
case LocalsTypeFormatRole:
|
||||||
@@ -1566,6 +1568,7 @@ void WatchModel::insertBulkData(const QList<WatchData> &list)
|
|||||||
insertDataItem(list.at(i), false);
|
insertDataItem(list.at(i), false);
|
||||||
#endif
|
#endif
|
||||||
CHECK(checkTree());
|
CHECK(checkTree());
|
||||||
|
emit columnAdjustmentRequested();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void debugRecursion(QDebug &d, const WatchItem *item, int depth)
|
static void debugRecursion(QDebug &d, const WatchItem *item, int depth)
|
||||||
|
Reference in New Issue
Block a user