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)),
|
connect(this, SIGNAL(clicked(QModelIndex)),
|
||||||
SLOT(rowClickedHelper(QModelIndex)));
|
SLOT(rowClickedHelper(QModelIndex)));
|
||||||
connect(header(), SIGNAL(sectionClicked(int)),
|
connect(header(), SIGNAL(sectionClicked(int)),
|
||||||
SLOT(headerSectionClicked(int)));
|
SLOT(toggleColumnWidth(int)));
|
||||||
|
|
||||||
m_adjustColumnsAction = new QAction(tr("Adjust Column Widths to Contents"), this);
|
m_adjustColumnsAction = new QAction(tr("Adjust Column Widths to Contents"), this);
|
||||||
m_alwaysAdjustColumnsAction = 0;
|
m_alwaysAdjustColumnsAction = 0;
|
||||||
@@ -122,8 +122,9 @@ void BaseTreeView::setModel(QAbstractItemModel *model)
|
|||||||
void BaseTreeView::mousePressEvent(QMouseEvent *ev)
|
void BaseTreeView::mousePressEvent(QMouseEvent *ev)
|
||||||
{
|
{
|
||||||
Utils::TreeView::mousePressEvent(ev);
|
Utils::TreeView::mousePressEvent(ev);
|
||||||
if (!indexAt(ev->pos()).isValid())
|
const QModelIndex mi = indexAt(ev->pos());
|
||||||
resizeColumnsToContents();
|
if (!mi.isValid())
|
||||||
|
toggleColumnWidth(columnAt(ev->x()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseTreeView::resizeColumnsToContents()
|
void BaseTreeView::resizeColumnsToContents()
|
||||||
@@ -140,8 +141,13 @@ void BaseTreeView::setAlwaysResizeColumnsToContents(bool on)
|
|||||||
header()->setResizeMode(0, mode);
|
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);
|
resizeColumnToContents(logicalIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ protected slots:
|
|||||||
private slots:
|
private slots:
|
||||||
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 headerSectionClicked(int logicalIndex);
|
void toggleColumnWidth(int logicalIndex);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QAction *m_alwaysAdjustColumnsAction;
|
QAction *m_alwaysAdjustColumnsAction;
|
||||||
|
|||||||
Reference in New Issue
Block a user