2014-10-07 15:51:02 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-22 10:37:55 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2014-10-07 15:51:02 +02:00
|
|
|
**
|
2016-01-22 10:37:55 +01:00
|
|
|
** This file is part of Qt Creator.
|
2014-10-07 15:51:02 +02:00
|
|
|
**
|
2016-01-22 10:37:55 +01:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
2014-10-07 15:51:02 +02:00
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-22 10:37:55 +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-07 15:51:02 +02:00
|
|
|
**
|
2016-01-22 10:37:55 +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-07 15:51:02 +02:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2015-04-09 15:51:42 +02:00
|
|
|
#include "autotestplugin.h"
|
2014-10-07 15:51:02 +02:00
|
|
|
#include "testresultdelegate.h"
|
|
|
|
|
#include "testresultmodel.h"
|
2015-04-09 15:51:42 +02:00
|
|
|
#include "testsettings.h"
|
2014-10-07 15:51:02 +02:00
|
|
|
|
2016-05-12 15:56:05 +02:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2014-10-07 15:51:02 +02:00
|
|
|
#include <QAbstractItemView>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
#include <QTextLayout>
|
|
|
|
|
|
|
|
|
|
namespace Autotest {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2015-04-09 15:51:42 +02:00
|
|
|
const static int outputLimit = 100000;
|
|
|
|
|
|
2014-10-07 15:51:02 +02:00
|
|
|
TestResultDelegate::TestResultDelegate(QObject *parent)
|
|
|
|
|
: QStyledItemDelegate(parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
|
{
|
2016-02-03 13:37:34 +01:00
|
|
|
QStyleOptionViewItem opt = option;
|
2014-10-07 15:51:02 +02:00
|
|
|
initStyleOption(&opt, index);
|
2015-04-21 07:41:06 +02:00
|
|
|
// make sure we paint the complete delegate instead of keeping an offset
|
|
|
|
|
opt.rect.adjust(-opt.rect.x(), 0, 0, 0);
|
2014-10-07 15:51:02 +02:00
|
|
|
painter->save();
|
|
|
|
|
|
|
|
|
|
QFontMetrics fm(opt.font);
|
|
|
|
|
QColor foreground;
|
|
|
|
|
|
|
|
|
|
const QAbstractItemView *view = qobject_cast<const QAbstractItemView *>(opt.widget);
|
|
|
|
|
const bool selected = view->selectionModel()->currentIndex() == index;
|
|
|
|
|
|
|
|
|
|
if (selected) {
|
|
|
|
|
painter->setBrush(opt.palette.highlight().color());
|
|
|
|
|
foreground = opt.palette.highlightedText().color();
|
|
|
|
|
} else {
|
|
|
|
|
painter->setBrush(opt.palette.background().color());
|
|
|
|
|
foreground = opt.palette.text().color();
|
|
|
|
|
}
|
|
|
|
|
painter->setPen(Qt::NoPen);
|
|
|
|
|
painter->drawRect(opt.rect);
|
|
|
|
|
painter->setPen(foreground);
|
2015-08-20 15:59:15 +02:00
|
|
|
|
2014-10-21 13:10:37 +02:00
|
|
|
TestResultFilterModel *resultFilterModel = static_cast<TestResultFilterModel *>(view->model());
|
2015-08-20 15:59:15 +02:00
|
|
|
LayoutPositions positions(opt, resultFilterModel);
|
2016-04-13 10:20:17 +02:00
|
|
|
const TestResult *testResult = resultFilterModel->testResult(index);
|
|
|
|
|
QTC_ASSERT(testResult, painter->restore();return);
|
2015-08-20 15:59:15 +02:00
|
|
|
|
|
|
|
|
// draw the indicator by ourself as we paint across it with the delegate
|
2016-02-03 13:37:34 +01:00
|
|
|
QStyleOptionViewItem indicatorOpt = option;
|
2015-08-20 15:59:15 +02:00
|
|
|
indicatorOpt.rect = QRect(0, opt.rect.y(), positions.indentation(), opt.rect.height());
|
|
|
|
|
opt.widget->style()->drawPrimitive(QStyle::PE_IndicatorBranch, &indicatorOpt, painter);
|
2014-10-07 15:51:02 +02:00
|
|
|
|
|
|
|
|
QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
|
|
|
|
|
if (!icon.isNull())
|
|
|
|
|
painter->drawPixmap(positions.left(), positions.top(),
|
|
|
|
|
icon.pixmap(positions.iconSize(), positions.iconSize()));
|
|
|
|
|
|
2016-04-13 10:20:17 +02:00
|
|
|
QString typeStr = TestResult::resultToString(testResult->result());
|
2014-10-07 15:51:02 +02:00
|
|
|
if (selected) {
|
|
|
|
|
painter->drawText(positions.typeAreaLeft(), positions.top() + fm.ascent(), typeStr);
|
|
|
|
|
} else {
|
|
|
|
|
QPen tmp = painter->pen();
|
2016-04-13 10:20:17 +02:00
|
|
|
painter->setPen(TestResult::colorForType(testResult->result()));
|
2014-10-07 15:51:02 +02:00
|
|
|
painter->drawText(positions.typeAreaLeft(), positions.top() + fm.ascent(), typeStr);
|
|
|
|
|
painter->setPen(tmp);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-13 10:20:17 +02:00
|
|
|
QString output = testResult->outputString(selected);
|
2014-10-07 15:51:02 +02:00
|
|
|
|
|
|
|
|
if (selected) {
|
|
|
|
|
output.replace(QLatin1Char('\n'), QChar::LineSeparator);
|
2015-04-09 15:51:42 +02:00
|
|
|
|
|
|
|
|
if (AutotestPlugin::instance()->settings()->limitResultOutput
|
|
|
|
|
&& output.length() > outputLimit)
|
|
|
|
|
output = output.left(outputLimit).append(QLatin1String("..."));
|
|
|
|
|
|
2015-08-19 10:22:25 +02:00
|
|
|
recalculateTextLayout(index, output, painter->font(), positions.textAreaWidth());
|
|
|
|
|
|
|
|
|
|
m_lastCalculatedLayout.draw(painter, QPoint(positions.textAreaLeft(), positions.top()));
|
2014-10-07 15:51:02 +02:00
|
|
|
} else {
|
|
|
|
|
painter->setClipRect(positions.textArea());
|
2015-04-09 15:51:42 +02:00
|
|
|
// cut output before generating elided text as this takes quite long for exhaustive output
|
2015-03-18 16:11:00 +01:00
|
|
|
painter->drawText(positions.textAreaLeft(), positions.top() + fm.ascent(),
|
2015-04-09 15:51:42 +02:00
|
|
|
fm.elidedText(output.left(2000), Qt::ElideRight, positions.textAreaWidth()));
|
2014-10-07 15:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
2016-04-13 10:20:17 +02:00
|
|
|
QString file = testResult->fileName();
|
2014-10-07 15:51:02 +02:00
|
|
|
const int pos = file.lastIndexOf(QLatin1Char('/'));
|
|
|
|
|
if (pos != -1)
|
|
|
|
|
file = file.mid(pos + 1);
|
|
|
|
|
|
|
|
|
|
painter->setClipRect(positions.fileArea());
|
|
|
|
|
painter->drawText(positions.fileAreaLeft(), positions.top() + fm.ascent(), file);
|
|
|
|
|
|
|
|
|
|
|
2016-04-13 10:20:17 +02:00
|
|
|
if (testResult->line()) {
|
|
|
|
|
QString line = QString::number(testResult->line());
|
2014-10-07 15:51:02 +02:00
|
|
|
painter->setClipRect(positions.lineArea());
|
|
|
|
|
painter->drawText(positions.lineAreaLeft(), positions.top() + fm.ascent(), line);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
painter->setClipRect(opt.rect);
|
2015-08-20 15:59:15 +02:00
|
|
|
painter->setPen(opt.palette.midlight().color());
|
2014-10-07 15:51:02 +02:00
|
|
|
painter->drawLine(0, opt.rect.bottom(), opt.rect.right(), opt.rect.bottom());
|
|
|
|
|
painter->restore();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QSize TestResultDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
|
{
|
2016-02-03 13:37:34 +01:00
|
|
|
QStyleOptionViewItem opt = option;
|
2015-04-21 07:41:06 +02:00
|
|
|
// make sure opt.rect is initialized correctly - otherwise we might get a width of 0
|
|
|
|
|
opt.initFrom(opt.widget);
|
2014-10-07 15:51:02 +02:00
|
|
|
initStyleOption(&opt, index);
|
|
|
|
|
|
|
|
|
|
const QAbstractItemView *view = qobject_cast<const QAbstractItemView *>(opt.widget);
|
|
|
|
|
const bool selected = view->selectionModel()->currentIndex() == index;
|
|
|
|
|
|
|
|
|
|
QFontMetrics fm(opt.font);
|
|
|
|
|
int fontHeight = fm.height();
|
2014-10-21 13:10:37 +02:00
|
|
|
TestResultFilterModel *resultFilterModel = static_cast<TestResultFilterModel *>(view->model());
|
2015-08-20 15:59:15 +02:00
|
|
|
LayoutPositions positions(opt, resultFilterModel);
|
2014-10-07 15:51:02 +02:00
|
|
|
QSize s;
|
|
|
|
|
s.setWidth(opt.rect.width());
|
|
|
|
|
|
|
|
|
|
if (selected) {
|
2016-04-13 10:20:17 +02:00
|
|
|
const TestResult *testResult = resultFilterModel->testResult(index);
|
|
|
|
|
QTC_ASSERT(testResult, return QSize());
|
|
|
|
|
QString output = testResult->outputString(selected);
|
2014-10-07 15:51:02 +02:00
|
|
|
output.replace(QLatin1Char('\n'), QChar::LineSeparator);
|
|
|
|
|
|
2015-04-09 15:51:42 +02:00
|
|
|
if (AutotestPlugin::instance()->settings()->limitResultOutput
|
|
|
|
|
&& output.length() > outputLimit)
|
|
|
|
|
output = output.left(outputLimit).append(QLatin1String("..."));
|
|
|
|
|
|
2015-08-19 10:22:25 +02:00
|
|
|
recalculateTextLayout(index, output, opt.font, positions.textAreaWidth());
|
2014-10-07 15:51:02 +02:00
|
|
|
|
2015-08-19 10:22:25 +02:00
|
|
|
s.setHeight(m_lastCalculatedHeight + 3);
|
2014-10-07 15:51:02 +02:00
|
|
|
} else {
|
|
|
|
|
s.setHeight(fontHeight + 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (s.height() < positions.minimumHeight())
|
|
|
|
|
s.setHeight(positions.minimumHeight());
|
|
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TestResultDelegate::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
|
|
|
|
|
{
|
|
|
|
|
emit sizeHintChanged(current);
|
|
|
|
|
emit sizeHintChanged(previous);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-19 10:22:25 +02:00
|
|
|
void TestResultDelegate::recalculateTextLayout(const QModelIndex &index, const QString &output,
|
|
|
|
|
const QFont &font, int width) const
|
|
|
|
|
{
|
|
|
|
|
if (m_lastProcessedIndex == index && m_lastProcessedFont == font)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const QFontMetrics fm(font);
|
|
|
|
|
const int leading = fm.leading();
|
|
|
|
|
const int fontHeight = fm.height();
|
|
|
|
|
|
|
|
|
|
m_lastProcessedIndex = index;
|
|
|
|
|
m_lastProcessedFont = font;
|
|
|
|
|
m_lastCalculatedHeight = 0;
|
|
|
|
|
m_lastCalculatedLayout.clearLayout();
|
|
|
|
|
m_lastCalculatedLayout.setText(output);
|
|
|
|
|
m_lastCalculatedLayout.setFont(font);
|
|
|
|
|
QTextOption txtOption;
|
|
|
|
|
txtOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
|
|
|
|
|
m_lastCalculatedLayout.setTextOption(txtOption);
|
|
|
|
|
m_lastCalculatedLayout.beginLayout();
|
|
|
|
|
while (true) {
|
|
|
|
|
QTextLine line = m_lastCalculatedLayout.createLine();
|
|
|
|
|
if (!line.isValid())
|
|
|
|
|
break;
|
|
|
|
|
line.setLineWidth(width);
|
|
|
|
|
m_lastCalculatedHeight += leading;
|
|
|
|
|
line.setPosition(QPoint(0, m_lastCalculatedHeight));
|
|
|
|
|
m_lastCalculatedHeight += fontHeight;
|
|
|
|
|
}
|
|
|
|
|
m_lastCalculatedLayout.endLayout();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-07 15:51:02 +02:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Autotest
|