Commit f84e5eed authored by hjk's avatar hjk

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: default avatarEike Ziller <eike.ziller@digia.com>
parent 26b54747
......@@ -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()
......
......@@ -64,7 +64,7 @@ protected slots:
private slots:
void rowActivatedHelper(const QModelIndex &index) { rowActivated(index); }
void rowClickedHelper(const QModelIndex &index) { rowClicked(index); }
void headerSectionClicked(int logicalIndex);
void toggleColumnWidth(int logicalIndex);
private:
QAction *m_alwaysAdjustColumnsAction;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment