Utils::BaseTreeView: Adjust column size by mouse click

Single click on either header or unoccupied part of the
view will toggle the respective column width between
a large value based on contents contents width and a
small fixed one (8ex). Previously it only expanded.

Change-Id: I2c5865a3b0bad7593a47976626d4c516e021c157
Reviewed-by: Eike Ziller <eike.ziller@digia.com>
This commit is contained in:
hjk
2014-05-29 00:56:11 +02:00
parent 26b54747e2
commit f84e5eed74
2 changed files with 12 additions and 6 deletions

View File

@@ -73,7 +73,7 @@ BaseTreeView::BaseTreeView(QWidget *parent)
connect(this, SIGNAL(clicked(QModelIndex)),
SLOT(rowClickedHelper(QModelIndex)));
connect(header(), SIGNAL(sectionClicked(int)),
SLOT(headerSectionClicked(int)));
SLOT(toggleColumnWidth(int)));
m_adjustColumnsAction = new QAction(tr("Adjust Column Widths to Contents"), this);
m_alwaysAdjustColumnsAction = 0;
@@ -122,8 +122,9 @@ void BaseTreeView::setModel(QAbstractItemModel *model)
void BaseTreeView::mousePressEvent(QMouseEvent *ev)
{
Utils::TreeView::mousePressEvent(ev);
if (!indexAt(ev->pos()).isValid())
resizeColumnsToContents();
const QModelIndex mi = indexAt(ev->pos());
if (!mi.isValid())
toggleColumnWidth(columnAt(ev->x()));
}
void BaseTreeView::resizeColumnsToContents()
@@ -140,9 +141,14 @@ void BaseTreeView::setAlwaysResizeColumnsToContents(bool on)
header()->setResizeMode(0, mode);
}
void BaseTreeView::headerSectionClicked(int logicalIndex)
void BaseTreeView::toggleColumnWidth(int logicalIndex)
{
resizeColumnToContents(logicalIndex);
const int hint = sizeHintForColumn(logicalIndex);
const int size = 8 * QFontMetrics(font()).width(QLatin1Char('x'));
if (hint == header()->sectionSize(logicalIndex))
header()->resizeSection(logicalIndex, size);
else
resizeColumnToContents(logicalIndex);
}
void BaseTreeView::reset()