aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/autotest/testresultdelegate.cpp
blob: 6ec6d23e534c77f015f168771b8959c76276048a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "testresultdelegate.h"

#include "autotestplugin.h"
#include "testresultmodel.h"
#include "testsettings.h"

#include <utils/qtcassert.h>

#include <QAbstractItemView>
#include <QPainter>
#include <QTextLayout>
#include <QWindow>

namespace Autotest {
namespace Internal {

constexpr int outputLimit = 100000;

TestResultDelegate::TestResultDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
}

void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem opt = option;
    initStyleOption(&opt, index);

    QFontMetrics fm(opt.font);
    QBrush background;
    QColor foreground;

    const bool selected = opt.state & QStyle::State_Selected;

    if (selected) {
        background = opt.palette.highlight().color();
        foreground = opt.palette.highlightedText().color();
    } else {
        background = opt.palette.window().color();
        foreground = opt.palette.text().color();
    }

    auto resultFilterModel = qobject_cast<const TestResultFilterModel *>(index.model());
    if (!resultFilterModel)
        return;
    painter->save();
    painter->fillRect(opt.rect, background);
    painter->setPen(foreground);

    LayoutPositions positions(opt, resultFilterModel);
    const TestResult *testResult = resultFilterModel->testResult(index);
    QTC_ASSERT(testResult, painter->restore();return);

    const QWidget *widget = dynamic_cast<const QWidget*>(painter->device());
    QWindow *window = widget ? widget->window()->windowHandle() : nullptr;

    QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
    if (!icon.isNull())
        painter->drawPixmap(positions.left(), positions.top(),
                            icon.pixmap(window, QSize(positions.iconSize(), positions.iconSize())));

    TestResultItem *item = resultFilterModel->itemForIndex(index);
    QTC_ASSERT(item, painter->restore(); return);
    const QString typeStr = item->resultString();
    if (selected) {
        painter->drawText(positions.typeAreaLeft(), positions.top() + fm.ascent(), typeStr);
    } else {
        QPen tmp = painter->pen();
        if (testResult->result() == ResultType::TestStart)
            painter->setPen(opt.palette.mid().color());
        else
            painter->setPen(TestResult::colorForType(testResult->result()));
        painter->drawText(positions.typeAreaLeft(), positions.top() + fm.ascent(), typeStr);
        painter->setPen(tmp);
    }

    QString output = testResult->outputString(selected);

    if (selected) {
        limitTextOutput(output);
        output.replace('\n', QChar::LineSeparator);
        recalculateTextLayout(index, output, painter->font(), positions.textAreaWidth());

        m_lastCalculatedLayout.draw(painter, QPoint(positions.textAreaLeft(), positions.top()));
    } else {
        painter->setClipRect(positions.textArea());
        // cut output before generating elided text as this takes quite long for exhaustive output
        painter->drawText(positions.textAreaLeft(), positions.top() + fm.ascent(),
                          fm.elidedText(output.left(2000), Qt::ElideRight, positions.textAreaWidth()));
    }

    const QString file = testResult->fileName().fileName();
    painter->setClipRect(positions.fileArea());
    painter->drawText(positions.fileAreaLeft(), positions.top() + fm.ascent(), file);

    if (testResult->line()) {
        QString line = QString::number(testResult->line());
        painter->setClipRect(positions.lineArea());
        painter->drawText(positions.lineAreaLeft(), positions.top() + fm.ascent(), line);
    }

    painter->setClipping(false);
    painter->setPen(opt.palette.mid().color());
    const QRectF adjustedRect(QRectF(opt.rect).adjusted(0.5, 0.5, -0.5, -0.5));
    painter->drawLine(adjustedRect.bottomLeft(), adjustedRect.bottomRight());
    painter->restore();
}

QSize TestResultDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem opt = option;
    // make sure opt.rect is initialized correctly - otherwise we might get a width of 0
    opt.initFrom(opt.widget);

    const QAbstractItemView *view = qobject_cast<const QAbstractItemView *>(opt.widget);
    const bool selected = view->selectionModel()->currentIndex() == index;

    QFontMetrics fm(opt.font);
    int fontHeight = fm.height();
    TestResultFilterModel *resultFilterModel = static_cast<TestResultFilterModel *>(view->model());
    LayoutPositions positions(opt, resultFilterModel);
    const int depth = resultFilterModel->itemForIndex(index)->level() + 1;
    const int indentation = depth * view->style()->pixelMetric(QStyle::PM_TreeViewIndentation, &opt);

    QSize s;
    s.setWidth(opt.rect.width() - indentation);

    if (selected) {
        const TestResult *testResult = resultFilterModel->testResult(index);
        QTC_ASSERT(testResult, return QSize());
        QString output = testResult->outputString(selected);
        limitTextOutput(output);
        output.replace('\n', QChar::LineSeparator);
        recalculateTextLayout(index, output, opt.font, positions.textAreaWidth() - indentation);

        s.setHeight(m_lastCalculatedHeight + 3);
    } else {
        s.setHeight(fontHeight + 3);
    }

    if (s.height() < positions.minimumHeight())
        s.setHeight(positions.minimumHeight());

    return s;
}

void TestResultDelegate::currentChanged(const QModelIndex &current, const QModelIndex &previous)
{
    emit sizeHintChanged(current);
    emit sizeHintChanged(previous);
}

void TestResultDelegate::clearCache()
{
    const QModelIndex current = m_lastProcessedIndex;
    m_lastProcessedIndex = QModelIndex();
    m_lastProcessedFont = QFont();
    m_lastWidth = -1;
    if (current.isValid())
        emit sizeHintChanged(current);
}

void TestResultDelegate::limitTextOutput(QString &output) const
{
    int maxLineCount = Internal::AutotestPlugin::settings()->resultDescriptionMaxSize;
    bool limited = false;

    if (Internal::AutotestPlugin::settings()->limitResultDescription && maxLineCount > 0) {
        int index = -1;
        int lastChar = output.size() - 1;

        for (int i = 0; i < maxLineCount; i++) {
            index = output.indexOf('\n', index + 1);
            if (index == -1 || index == lastChar) {
                index = -1;
                break;
            }
        }

        if (index > 0) {
            output = output.left(index);
            limited = true;
        }
    }

    if (AutotestPlugin::settings()->limitResultOutput && output.length() > outputLimit) {
        output = output.left(outputLimit);
        limited = true;
    }

    if (limited)
        output.append("...");
}

void TestResultDelegate::recalculateTextLayout(const QModelIndex &index, const QString &output,
                                               const QFont &font, int width) const
{
    if (m_lastWidth == width && m_lastProcessedIndex == index && m_lastProcessedFont == font)
        return;

    const QFontMetrics fm(font);
    const int leading = fm.leading();
    const int fontHeight = fm.height();

    m_lastWidth = width;
    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();
}

} // namespace Internal
} // namespace Autotest