2014-10-09 17:04:22 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2014-10-09 17:04:22 +02:00
|
|
|
**
|
|
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2014-10-09 17:04:22 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2014-10-09 17:04:22 +02:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "detailederrorview.h"
|
|
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
#include "diagnosticlocation.h"
|
|
|
|
|
|
2015-11-23 16:41:54 +01:00
|
|
|
#include <coreplugin/coreicons.h>
|
2014-10-09 17:04:22 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
#include <QAbstractTextDocumentLayout>
|
2015-02-26 13:38:54 +01:00
|
|
|
#include <QAction>
|
2015-02-11 16:12:58 +01:00
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QClipboard>
|
|
|
|
|
#include <QContextMenuEvent>
|
2015-06-25 10:00:49 +02:00
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QHeaderView>
|
2015-02-26 13:38:54 +01:00
|
|
|
#include <QMenu>
|
2014-10-09 17:04:22 +02:00
|
|
|
#include <QPainter>
|
2015-06-25 10:00:49 +02:00
|
|
|
#include <QSharedPointer>
|
|
|
|
|
#include <QTextDocument>
|
2014-10-09 17:04:22 +02:00
|
|
|
|
|
|
|
|
namespace Analyzer {
|
2015-06-25 10:00:49 +02:00
|
|
|
namespace Internal {
|
2014-10-09 17:04:22 +02:00
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
class DetailedErrorDelegate : public QStyledItemDelegate
|
2014-10-09 17:04:22 +02:00
|
|
|
{
|
2015-06-25 10:00:49 +02:00
|
|
|
Q_OBJECT
|
2014-11-25 13:57:34 +01:00
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
public:
|
|
|
|
|
DetailedErrorDelegate(QTreeView *parent) : QStyledItemDelegate(parent) { }
|
2014-10-09 17:04:22 +02:00
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
private:
|
|
|
|
|
QString actualText(const QModelIndex &index) const
|
|
|
|
|
{
|
|
|
|
|
const auto location = index.model()->data(index, DetailedErrorView::LocationRole)
|
|
|
|
|
.value<DiagnosticLocation>();
|
|
|
|
|
return location.isValid()
|
|
|
|
|
? QString::fromLatin1("<a href=\"file://%1\">%2:%3")
|
|
|
|
|
.arg(location.filePath, QFileInfo(location.filePath).fileName())
|
|
|
|
|
.arg(location.line)
|
|
|
|
|
: QString();
|
2014-10-09 17:04:22 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
using DocConstPtr = QSharedPointer<const QTextDocument>;
|
|
|
|
|
DocConstPtr document(const QStyleOptionViewItem &option) const
|
|
|
|
|
{
|
|
|
|
|
const auto doc = QSharedPointer<QTextDocument>::create();
|
|
|
|
|
doc->setHtml(option.text);
|
|
|
|
|
doc->setTextWidth(option.rect.width());
|
|
|
|
|
doc->setDocumentMargin(0);
|
|
|
|
|
return doc;
|
2014-10-09 17:04:22 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
|
|
|
|
|
{
|
|
|
|
|
QStyleOptionViewItem opt = option;
|
|
|
|
|
opt.text = actualText(index);
|
|
|
|
|
initStyleOption(&opt, index);
|
2014-10-09 17:04:22 +02:00
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
const DocConstPtr doc = document(opt);
|
|
|
|
|
return QSize(doc->idealWidth(), doc->size().height());
|
2014-10-09 17:04:22 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
void paint(QPainter *painter, const QStyleOptionViewItem &option,
|
|
|
|
|
const QModelIndex &index) const override
|
|
|
|
|
{
|
|
|
|
|
QStyleOptionViewItem opt = option;
|
|
|
|
|
initStyleOption(&opt, index);
|
2014-10-09 17:04:22 +02:00
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
QStyle *style = opt.widget? opt.widget->style() : QApplication::style();
|
2014-10-09 17:04:22 +02:00
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
// Painting item without text
|
|
|
|
|
opt.text.clear();
|
|
|
|
|
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter);
|
|
|
|
|
opt.text = actualText(index);
|
2014-10-09 17:04:22 +02:00
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
QAbstractTextDocumentLayout::PaintContext ctx;
|
2014-10-09 17:04:22 +02:00
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
// Highlighting text if item is selected
|
|
|
|
|
if (opt.state & QStyle::State_Selected) {
|
|
|
|
|
ctx.palette.setColor(QPalette::Text, opt.palette.color(QPalette::Active,
|
|
|
|
|
QPalette::HighlightedText));
|
|
|
|
|
}
|
2014-10-09 17:04:22 +02:00
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &opt);
|
|
|
|
|
painter->save();
|
|
|
|
|
painter->translate(textRect.topLeft());
|
|
|
|
|
painter->setClipRect(textRect.translated(-textRect.topLeft()));
|
|
|
|
|
document(opt)->documentLayout()->draw(painter, ctx);
|
|
|
|
|
painter->restore();
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-09 17:04:22 +02:00
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
} // namespace Internal
|
2014-10-09 17:04:22 +02:00
|
|
|
|
2015-02-11 16:12:58 +01:00
|
|
|
|
2015-02-25 12:40:31 +03:00
|
|
|
DetailedErrorView::DetailedErrorView(QWidget *parent) :
|
2015-06-25 10:00:49 +02:00
|
|
|
QTreeView(parent),
|
|
|
|
|
m_copyAction(new QAction(this))
|
2014-10-09 17:04:22 +02:00
|
|
|
{
|
2015-06-25 10:00:49 +02:00
|
|
|
header()->setSectionResizeMode(QHeaderView::ResizeToContents);
|
|
|
|
|
setItemDelegateForColumn(LocationColumn, new Internal::DetailedErrorDelegate(this));
|
2014-10-09 17:04:22 +02:00
|
|
|
|
2015-02-11 16:12:58 +01:00
|
|
|
m_copyAction->setText(tr("Copy"));
|
2015-11-23 16:41:54 +01:00
|
|
|
m_copyAction->setIcon(Core::Icons::COPY.icon());
|
2015-02-12 12:48:35 +01:00
|
|
|
m_copyAction->setShortcut(QKeySequence::Copy);
|
2015-02-11 16:12:58 +01:00
|
|
|
m_copyAction->setShortcutContext(Qt::WidgetWithChildrenShortcut);
|
2015-06-25 10:00:49 +02:00
|
|
|
connect(m_copyAction, &QAction::triggered, [this] {
|
|
|
|
|
const QModelIndexList selectedRows = selectionModel()->selectedRows();
|
|
|
|
|
QTC_ASSERT(selectedRows.count() == 1, return);
|
|
|
|
|
QApplication::clipboard()->setText(model()->data(selectedRows.first(),
|
|
|
|
|
FullTextRole).toString());
|
|
|
|
|
});
|
|
|
|
|
connect(this, &QAbstractItemView::clicked, [](const QModelIndex &index) {
|
|
|
|
|
if (index.column() == LocationColumn) {
|
|
|
|
|
const auto loc = index.model()
|
|
|
|
|
->data(index, Analyzer::DetailedErrorView::LocationRole)
|
|
|
|
|
.value<DiagnosticLocation>();
|
|
|
|
|
if (loc.isValid())
|
|
|
|
|
Core::EditorManager::openEditorAt(loc.filePath, loc.line, loc.column - 1);
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-10-09 17:04:22 +02:00
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
addAction(m_copyAction);
|
2014-10-09 17:04:22 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 10:00:49 +02:00
|
|
|
DetailedErrorView::~DetailedErrorView()
|
2014-10-09 17:04:22 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 16:12:58 +01:00
|
|
|
void DetailedErrorView::contextMenuEvent(QContextMenuEvent *e)
|
|
|
|
|
{
|
|
|
|
|
if (selectionModel()->selectedRows().isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QMenu menu;
|
|
|
|
|
menu.addActions(commonActions());
|
|
|
|
|
const QList<QAction *> custom = customActions();
|
|
|
|
|
if (!custom.isEmpty()) {
|
|
|
|
|
menu.addSeparator();
|
|
|
|
|
menu.addActions(custom);
|
|
|
|
|
}
|
|
|
|
|
menu.exec(e->globalPos());
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-09 17:04:22 +02:00
|
|
|
void DetailedErrorView::goNext()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(rowCount(), return);
|
|
|
|
|
setCurrentRow((currentRow() + 1) % rowCount());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DetailedErrorView::goBack()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(rowCount(), return);
|
|
|
|
|
const int prevRow = currentRow() - 1;
|
|
|
|
|
setCurrentRow(prevRow >= 0 ? prevRow : rowCount() - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int DetailedErrorView::rowCount() const
|
|
|
|
|
{
|
|
|
|
|
return model() ? model()->rowCount() : 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 16:12:58 +01:00
|
|
|
QList<QAction *> DetailedErrorView::commonActions() const
|
|
|
|
|
{
|
|
|
|
|
QList<QAction *> actions;
|
|
|
|
|
actions << m_copyAction;
|
|
|
|
|
return actions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<QAction *> DetailedErrorView::customActions() const
|
|
|
|
|
{
|
|
|
|
|
return QList<QAction *>();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-09 17:04:22 +02:00
|
|
|
int DetailedErrorView::currentRow() const
|
|
|
|
|
{
|
|
|
|
|
const QModelIndex index = selectionModel()->currentIndex();
|
|
|
|
|
return index.row();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DetailedErrorView::setCurrentRow(int row)
|
|
|
|
|
{
|
|
|
|
|
const QModelIndex index = model()->index(row, 0);
|
|
|
|
|
selectionModel()->setCurrentIndex(index,
|
|
|
|
|
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
|
|
|
|
|
scrollTo(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Analyzer
|
2015-06-25 10:00:49 +02:00
|
|
|
|
|
|
|
|
#include "detailederrorview.moc"
|