2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2012-04-02 13:09:37 +02:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2012-04-02 13:09:37 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2012-04-02 13:09:37 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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.
|
2012-04-02 13:09:37 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2012-04-02 13:09:37 +02:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2012-04-02 13:09:37 +02:00
|
|
|
|
|
|
|
|
#include "basetreeview.h"
|
|
|
|
|
|
2014-06-26 12:49:14 +02:00
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QFontMetrics>
|
2012-04-02 13:09:37 +02:00
|
|
|
#include <QHeaderView>
|
2012-11-08 10:19:42 +01:00
|
|
|
#include <QItemDelegate>
|
|
|
|
|
#include <QLabel>
|
2012-04-02 13:09:37 +02:00
|
|
|
#include <QMenu>
|
|
|
|
|
#include <QMouseEvent>
|
2014-06-03 11:34:52 +02:00
|
|
|
#include <QTimer>
|
2012-04-02 13:09:37 +02:00
|
|
|
|
|
|
|
|
namespace Utils {
|
|
|
|
|
|
2012-11-08 10:19:42 +01:00
|
|
|
class BaseTreeViewDelegate : public QItemDelegate
|
|
|
|
|
{
|
|
|
|
|
public:
|
2013-08-14 11:03:24 +02:00
|
|
|
BaseTreeViewDelegate(QObject *parent): QItemDelegate(parent) {}
|
2012-11-08 10:19:42 +01:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-04-02 13:09:37 +02:00
|
|
|
BaseTreeView::BaseTreeView(QWidget *parent)
|
2014-04-09 17:39:41 +02:00
|
|
|
: Utils::TreeView(parent)
|
2012-04-02 13:09:37 +02:00
|
|
|
{
|
|
|
|
|
setAttribute(Qt::WA_MacShowFocusRect, false);
|
|
|
|
|
setFrameStyle(QFrame::NoFrame);
|
|
|
|
|
setRootIsDecorated(false);
|
|
|
|
|
setIconSize(QSize(10, 10));
|
|
|
|
|
setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|
|
|
|
setUniformRowHeights(true);
|
2013-08-14 11:03:24 +02:00
|
|
|
setItemDelegate(new BaseTreeViewDelegate(this));
|
2012-04-02 13:09:37 +02:00
|
|
|
header()->setDefaultAlignment(Qt::AlignLeft);
|
|
|
|
|
header()->setClickable(true);
|
|
|
|
|
|
|
|
|
|
connect(this, SIGNAL(activated(QModelIndex)),
|
|
|
|
|
SLOT(rowActivatedHelper(QModelIndex)));
|
2012-11-23 13:35:44 +01:00
|
|
|
connect(this, SIGNAL(clicked(QModelIndex)),
|
|
|
|
|
SLOT(rowClickedHelper(QModelIndex)));
|
2012-04-02 13:09:37 +02:00
|
|
|
connect(header(), SIGNAL(sectionClicked(int)),
|
2014-05-29 00:56:11 +02:00
|
|
|
SLOT(toggleColumnWidth(int)));
|
2012-04-02 13:09:37 +02:00
|
|
|
}
|
|
|
|
|
|
2014-06-26 12:49:14 +02:00
|
|
|
void BaseTreeView::setModel(QAbstractItemModel *m)
|
2012-04-02 13:09:37 +02:00
|
|
|
{
|
2014-06-26 12:49:14 +02:00
|
|
|
const char *sig = "columnAdjustmentRequested()";
|
|
|
|
|
if (model()) {
|
|
|
|
|
int index = model()->metaObject()->indexOfSignal(sig);
|
|
|
|
|
if (index != -1)
|
|
|
|
|
disconnect(model(), SIGNAL(columnAdjustmentRequested()), this, SLOT(resizeColumns()));
|
2012-04-02 13:09:37 +02:00
|
|
|
}
|
2014-06-26 12:49:14 +02:00
|
|
|
Utils::TreeView::setModel(m);
|
|
|
|
|
if (m) {
|
|
|
|
|
int index = m->metaObject()->indexOfSignal(sig);
|
|
|
|
|
if (index != -1)
|
|
|
|
|
connect(m, SIGNAL(columnAdjustmentRequested()), this, SLOT(resizeColumns()));
|
2014-06-03 11:34:52 +02:00
|
|
|
}
|
2012-04-02 13:09:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseTreeView::mousePressEvent(QMouseEvent *ev)
|
|
|
|
|
{
|
2014-04-09 17:39:41 +02:00
|
|
|
Utils::TreeView::mousePressEvent(ev);
|
2014-05-29 00:56:11 +02:00
|
|
|
const QModelIndex mi = indexAt(ev->pos());
|
|
|
|
|
if (!mi.isValid())
|
|
|
|
|
toggleColumnWidth(columnAt(ev->x()));
|
2012-04-02 13:09:37 +02:00
|
|
|
}
|
|
|
|
|
|
2014-06-03 11:34:52 +02:00
|
|
|
void BaseTreeView::resizeColumns()
|
2012-04-02 13:09:37 +02:00
|
|
|
{
|
2014-06-03 11:34:52 +02:00
|
|
|
QHeaderView *h = header();
|
|
|
|
|
if (!h)
|
|
|
|
|
return;
|
2014-06-26 12:49:14 +02:00
|
|
|
|
|
|
|
|
for (int i = 0, n = h->count(); i != n; ++i) {
|
|
|
|
|
int targetSize = suggestedColumnSize(i);
|
|
|
|
|
if (targetSize > 0)
|
|
|
|
|
h->resizeSection(i, targetSize);
|
2014-06-03 11:34:52 +02:00
|
|
|
}
|
2012-04-02 13:09:37 +02:00
|
|
|
}
|
|
|
|
|
|
2014-06-26 12:49:14 +02:00
|
|
|
int BaseTreeView::suggestedColumnSize(int column) const
|
2012-04-02 13:09:37 +02:00
|
|
|
{
|
2014-06-03 11:34:52 +02:00
|
|
|
QHeaderView *h = header();
|
|
|
|
|
if (!h)
|
2014-06-26 12:49:14 +02:00
|
|
|
return -1;
|
2014-06-03 11:34:52 +02:00
|
|
|
|
2014-06-26 12:49:14 +02:00
|
|
|
QModelIndex a = indexAt(QPoint(1, 1));
|
|
|
|
|
a = a.sibling(a.row(), column);
|
2014-06-03 11:34:52 +02:00
|
|
|
QFontMetrics fm(font());
|
2014-06-26 12:49:14 +02:00
|
|
|
int m = fm.width(model()->headerData(column, Qt::Horizontal).toString());
|
|
|
|
|
const int ind = indentation();
|
|
|
|
|
for (int i = 0; i < 100 && a.isValid(); ++i) {
|
|
|
|
|
const QString s = model()->data(a).toString();
|
|
|
|
|
int w = fm.width(s) + 10;
|
|
|
|
|
if (column == 0) {
|
|
|
|
|
for (QModelIndex b = a.parent(); b.isValid(); b = b.parent())
|
|
|
|
|
w += ind;
|
2014-06-03 11:34:52 +02:00
|
|
|
}
|
2014-06-26 12:49:14 +02:00
|
|
|
if (w > m)
|
|
|
|
|
m = w;
|
|
|
|
|
a = indexBelow(a);
|
2014-06-03 11:34:52 +02:00
|
|
|
}
|
2014-06-26 12:49:14 +02:00
|
|
|
return m;
|
2012-04-02 13:09:37 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-29 00:56:11 +02:00
|
|
|
void BaseTreeView::toggleColumnWidth(int logicalIndex)
|
2012-04-02 13:09:37 +02:00
|
|
|
{
|
2014-06-03 11:34:52 +02:00
|
|
|
QHeaderView *h = header();
|
|
|
|
|
const int currentSize = h->sectionSize(logicalIndex);
|
2014-06-26 12:49:14 +02:00
|
|
|
const int suggestedSize = suggestedColumnSize(logicalIndex);
|
|
|
|
|
if (currentSize == suggestedSize) {
|
2014-06-03 11:34:52 +02:00
|
|
|
QFontMetrics fm(font());
|
|
|
|
|
int headerSize = fm.width(model()->headerData(logicalIndex, Qt::Horizontal).toString());
|
|
|
|
|
int minSize = 10 * fm.width(QLatin1Char('x'));
|
|
|
|
|
h->resizeSection(logicalIndex, qMax(minSize, headerSize));
|
|
|
|
|
} else {
|
2014-06-26 12:49:14 +02:00
|
|
|
h->resizeSection(logicalIndex, suggestedSize);
|
2014-06-03 11:34:52 +02:00
|
|
|
}
|
2012-04-02 13:09:37 +02:00
|
|
|
}
|
|
|
|
|
|
2013-01-10 15:23:12 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-02 13:09:37 +02:00
|
|
|
} // namespace Utils
|