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 <QPainter>
|
|
|
|
|
#include <QTextLayout>
|
2017-09-19 16:26:33 +02:00
|
|
|
#include <QWindow>
|
2014-10-07 15:51:02 +02:00
|
|
|
|
|
|
|
|
namespace Autotest {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2018-07-11 15:44:51 +02:00
|
|
|
constexpr int outputLimit = 100000;
|
2015-04-09 15:51:42 +02:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
QFontMetrics fm(opt.font);
|
2017-09-19 16:26:33 +02:00
|
|
|
QBrush background;
|
2014-10-07 15:51:02 +02:00
|
|
|
QColor foreground;
|
|
|
|
|
|
2016-10-31 13:11:52 +01:00
|
|
|
const bool selected = opt.state & QStyle::State_Selected;
|
2014-10-07 15:51:02 +02:00
|
|
|
|
|
|
|
|
if (selected) {
|
2017-09-19 16:26:33 +02:00
|
|
|
background = opt.palette.highlight().color();
|
2014-10-07 15:51:02 +02:00
|
|
|
foreground = opt.palette.highlightedText().color();
|
|
|
|
|
} else {
|
2017-09-19 16:26:33 +02:00
|
|
|
background = opt.palette.window().color();
|
2014-10-07 15:51:02 +02:00
|
|
|
foreground = opt.palette.text().color();
|
|
|
|
|
}
|
2018-01-26 07:29:20 +01:00
|
|
|
|
|
|
|
|
auto resultFilterModel = qobject_cast<const TestResultFilterModel *>(index.model());
|
|
|
|
|
if (!resultFilterModel)
|
|
|
|
|
return;
|
|
|
|
|
painter->save();
|
2017-09-19 16:26:33 +02:00
|
|
|
painter->fillRect(opt.rect, background);
|
2014-10-07 15:51:02 +02:00
|
|
|
painter->setPen(foreground);
|
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
|
|
|
|
2017-09-19 16:26:33 +02:00
|
|
|
const QWidget *widget = dynamic_cast<const QWidget*>(painter->device());
|
|
|
|
|
QWindow *window = widget ? widget->window()->windowHandle() : nullptr;
|
|
|
|
|
|
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(),
|
2017-09-19 16:26:33 +02:00
|
|
|
icon.pixmap(window, QSize(positions.iconSize(), positions.iconSize())));
|
2014-10-07 15:51:02 +02:00
|
|
|
|
2019-04-15 16:08:10 +02:00
|
|
|
TestResultItem *item = resultFilterModel->itemForIndex(index);
|
|
|
|
|
QTC_ASSERT(item, painter->restore(); return);
|
|
|
|
|
const QString typeStr = item->resultString();
|
2014-10-07 15:51:02 +02:00
|
|
|
if (selected) {
|
|
|
|
|
painter->drawText(positions.typeAreaLeft(), positions.top() + fm.ascent(), typeStr);
|
|
|
|
|
} else {
|
|
|
|
|
QPen tmp = painter->pen();
|
2019-04-15 16:08:10 +02:00
|
|
|
if (testResult->result() == ResultType::TestStart)
|
2016-12-14 14:42:28 +01:00
|
|
|
painter->setPen(opt.palette.mid().color());
|
|
|
|
|
else
|
|
|
|
|
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) {
|
2016-09-29 12:15:43 +02:00
|
|
|
output.replace('\n', QChar::LineSeparator);
|
2015-04-09 15:51:42 +02:00
|
|
|
|
2018-02-01 09:17:56 +01:00
|
|
|
if (AutotestPlugin::settings()->limitResultOutput && output.length() > outputLimit)
|
2016-09-29 12:15:43 +02:00
|
|
|
output = output.left(outputLimit).append("...");
|
2015-04-09 15:51:42 +02:00
|
|
|
|
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();
|
2016-09-29 12:15:43 +02:00
|
|
|
const int pos = file.lastIndexOf('/');
|
2014-10-07 15:51:02 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 16:26:33 +02:00
|
|
|
painter->setClipping(false);
|
2016-10-26 10:22:38 +02:00
|
|
|
painter->setPen(opt.palette.mid().color());
|
2017-09-19 16:26:33 +02:00
|
|
|
const QRectF adjustedRect(QRectF(opt.rect).adjusted(0.5, 0.5, -0.5, -0.5));
|
|
|
|
|
painter->drawLine(adjustedRect.bottomLeft(), adjustedRect.bottomRight());
|
2014-10-07 15:51:02 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
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);
|
2016-09-29 12:15:43 +02:00
|
|
|
output.replace('\n', QChar::LineSeparator);
|
2014-10-07 15:51:02 +02:00
|
|
|
|
2018-02-01 09:17:56 +01:00
|
|
|
if (AutotestPlugin::settings()->limitResultOutput && output.length() > outputLimit)
|
2016-09-29 12:15:43 +02:00
|
|
|
output = output.left(outputLimit).append("...");
|
2015-04-09 15:51:42 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-12 14:56:21 +02:00
|
|
|
void TestResultDelegate::clearCache()
|
|
|
|
|
{
|
|
|
|
|
m_lastProcessedIndex = QModelIndex();
|
|
|
|
|
m_lastProcessedFont = QFont();
|
|
|
|
|
}
|
|
|
|
|
|
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
|