forked from qt-creator/qt-creator
Since it's easier to shrink columns nowadays (left click on header section or somewhere in the empty space) this might "feel" better than last time we tried. This is an experiment for now, might get reverted before 3.2. Task-number: QTCREATORBUG-9918 Change-Id: I379d9310e232a16c8b8ee3c6cb0d91746fd7553c Reviewed-by: Eike Ziller <eike.ziller@digia.com>
176 lines
5.6 KiB
C++
176 lines
5.6 KiB
C++
/****************************************************************************
|
|
**
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
|
** Contact: http://www.qt-project.org/legal
|
|
**
|
|
** This file is part of Qt Creator.
|
|
**
|
|
** Commercial License Usage
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
** accordance with the commercial license agreement provided with the
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
**
|
|
** GNU Lesser General Public License Usage
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
** General Public License version 2.1 as published by the Free Software
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
** packaging of this file. Please review the following information to
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
**
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
**
|
|
****************************************************************************/
|
|
|
|
#include "basetreeview.h"
|
|
|
|
#include <QHeaderView>
|
|
#include <QItemDelegate>
|
|
#include <QLabel>
|
|
#include <QMenu>
|
|
#include <QMouseEvent>
|
|
|
|
namespace Utils {
|
|
|
|
class BaseTreeViewDelegate : public QItemDelegate
|
|
{
|
|
public:
|
|
BaseTreeViewDelegate(QObject *parent): QItemDelegate(parent) {}
|
|
|
|
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
|
|
const QModelIndex &index) const
|
|
{
|
|
Q_UNUSED(option);
|
|
QLabel *label = new QLabel(parent);
|
|
label->setAutoFillBackground(true);
|
|
label->setTextInteractionFlags(Qt::TextSelectableByMouse
|
|
| Qt::LinksAccessibleByMouse);
|
|
label->setText(index.data().toString());
|
|
return label;
|
|
}
|
|
};
|
|
|
|
BaseTreeView::BaseTreeView(QWidget *parent)
|
|
: Utils::TreeView(parent)
|
|
{
|
|
setAttribute(Qt::WA_MacShowFocusRect, false);
|
|
setFrameStyle(QFrame::NoFrame);
|
|
setRootIsDecorated(false);
|
|
setIconSize(QSize(10, 10));
|
|
setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|
setUniformRowHeights(true);
|
|
setItemDelegate(new BaseTreeViewDelegate(this));
|
|
header()->setDefaultAlignment(Qt::AlignLeft);
|
|
header()->setClickable(true);
|
|
|
|
connect(this, SIGNAL(activated(QModelIndex)),
|
|
SLOT(rowActivatedHelper(QModelIndex)));
|
|
connect(this, SIGNAL(clicked(QModelIndex)),
|
|
SLOT(rowClickedHelper(QModelIndex)));
|
|
connect(header(), SIGNAL(sectionClicked(int)),
|
|
SLOT(toggleColumnWidth(int)));
|
|
|
|
m_adjustColumnsAction = new QAction(tr("Adjust Column Widths to Contents"), this);
|
|
m_alwaysAdjustColumnsAction = 0;
|
|
}
|
|
|
|
void BaseTreeView::setAlwaysAdjustColumnsAction(QAction *action)
|
|
{
|
|
m_alwaysAdjustColumnsAction = action;
|
|
connect(action, SIGNAL(toggled(bool)),
|
|
SLOT(setAlwaysResizeColumnsToContents(bool)));
|
|
}
|
|
|
|
void BaseTreeView::addBaseContextActions(QMenu *menu)
|
|
{
|
|
menu->addSeparator();
|
|
if (m_alwaysAdjustColumnsAction)
|
|
menu->addAction(m_alwaysAdjustColumnsAction);
|
|
menu->addAction(m_adjustColumnsAction);
|
|
menu->addSeparator();
|
|
}
|
|
|
|
bool BaseTreeView::handleBaseContextAction(QAction *act)
|
|
{
|
|
if (act == 0)
|
|
return true;
|
|
if (act == m_adjustColumnsAction) {
|
|
resizeColumnsToContents();
|
|
return true;
|
|
}
|
|
if (act == m_alwaysAdjustColumnsAction) {
|
|
if (act->isChecked())
|
|
resizeColumnsToContents();
|
|
// Action triggered automatically.
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void BaseTreeView::setModel(QAbstractItemModel *model)
|
|
{
|
|
Utils::TreeView::setModel(model);
|
|
if (header() && m_alwaysAdjustColumnsAction)
|
|
setAlwaysResizeColumnsToContents(m_alwaysAdjustColumnsAction->isChecked());
|
|
}
|
|
|
|
void BaseTreeView::mousePressEvent(QMouseEvent *ev)
|
|
{
|
|
Utils::TreeView::mousePressEvent(ev);
|
|
const QModelIndex mi = indexAt(ev->pos());
|
|
if (!mi.isValid())
|
|
toggleColumnWidth(columnAt(ev->x()));
|
|
}
|
|
|
|
void BaseTreeView::resizeColumnsToContents()
|
|
{
|
|
const int columnCount = model()->columnCount();
|
|
for (int c = 0 ; c != columnCount; ++c)
|
|
resizeColumnToContents(c);
|
|
}
|
|
|
|
void BaseTreeView::setAlwaysResizeColumnsToContents(bool on)
|
|
{
|
|
QHeaderView::ResizeMode mode = on
|
|
? QHeaderView::ResizeToContents : QHeaderView::Interactive;
|
|
for (int i = 0, n = header()->count(); i != n; ++i)
|
|
header()->setResizeMode(i, mode);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
void BaseTreeView::reset()
|
|
{
|
|
Utils::TreeView::reset();
|
|
if (header() && m_alwaysAdjustColumnsAction
|
|
&& m_alwaysAdjustColumnsAction->isChecked())
|
|
resizeColumnsToContents();
|
|
}
|
|
|
|
QModelIndexList BaseTreeView::activeRows() const
|
|
{
|
|
QItemSelectionModel *selection = selectionModel();
|
|
QModelIndexList indices = selection->selectedRows();
|
|
if (indices.isEmpty()) {
|
|
QModelIndex current = selection->currentIndex();
|
|
if (current.isValid())
|
|
indices.append(current);
|
|
}
|
|
return indices;
|
|
}
|
|
|
|
} // namespace Utils
|