forked from qt-creator/qt-creator
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:
@@ -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,8 +141,13 @@ void BaseTreeView::setAlwaysResizeColumnsToContents(bool on)
|
||||
header()->setResizeMode(0, mode);
|
||||
}
|
||||
|
||||
void BaseTreeView::headerSectionClicked(int logicalIndex)
|
||||
void BaseTreeView::toggleColumnWidth(int 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user