summaryrefslogtreecommitdiff
path: root/plugins/autotest/testresultdelegate.cpp
blob: 4231278276ac088409d35c795231f4cdd5f960a9 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the Qt Creator Enterprise Auto Test Add-on.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
**
****************************************************************************/

#include "testresultdelegate.h"
#include "testresultmodel.h"

#include <QAbstractItemView>
#include <QDebug>
#include <QPainter>
#include <QTextLayout>

namespace Autotest {
namespace Internal {

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

TestResultDelegate::~TestResultDelegate()
{

}

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

    QFontMetrics fm(opt.font);
    QColor background;
    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());
        background = opt.palette.highlight().color();
        foreground = opt.palette.highlightedText().color();
    } else {
        painter->setBrush(opt.palette.background().color());
        background = opt.palette.background().color();
        foreground = opt.palette.text().color();
    }

    painter->setPen(Qt::NoPen);
    painter->drawRect(opt.rect);

    painter->setPen(foreground);
    TestResultFilterModel *resultFilterModel = static_cast<TestResultFilterModel *>(view->model());
    TestResultModel *resultModel = static_cast<TestResultModel *>(resultFilterModel->sourceModel());
    LayoutPositions positions(opt, resultModel);
    TestResult testResult = resultModel->testResult(resultFilterModel->mapToSource(index));
    ResultType type = testResult.result();

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

    QString typeStr = TestResult::resultToString(testResult.result());
    if (selected) {
        painter->drawText(positions.typeAreaLeft(), positions.top() + fm.ascent(), typeStr);
    } else {
        QPen tmp = painter->pen();
        painter->setPen(TestResult::colorForType(testResult.result()));
        painter->drawText(positions.typeAreaLeft(), positions.top() + fm.ascent(), typeStr);
        painter->setPen(tmp);
    }

    QString output;
    switch (type) {
    case ResultType::PASS:
    case ResultType::FAIL:
    case ResultType::EXPECTED_FAIL:
    case ResultType::UNEXPECTED_PASS:
        output = testResult.className() + QLatin1String("::") + testResult.testCase();
        if (!testResult.dataTag().isEmpty())
            output.append(QString::fromLatin1(" (%1)").arg(testResult.dataTag()));
        if (selected && !testResult.description().isEmpty()) {
            output.append(QLatin1Char('\n'));
            output.append(testResult.description());
        }
        break;
    default:
        output = testResult.description();
        if (!selected)
            output = output.split(QLatin1Char('\n')).first();
    }

    QColor mix;
    mix.setRgb( static_cast<int>(0.7 * foreground.red()   + 0.3 * background.red()),
                static_cast<int>(0.7 * foreground.green() + 0.3 * background.green()),
                static_cast<int>(0.7 * foreground.blue()  + 0.3 * background.blue()));

    if (selected) {
        int height = 0;
        int leading = fm.leading();
        int firstLineBreak = output.indexOf(QLatin1Char('\n'));
        output.replace(QLatin1Char('\n'), QChar::LineSeparator);
        QTextLayout tl(output);
        if (firstLineBreak != -1) {
            QTextLayout::FormatRange fr;
            fr.start = firstLineBreak;
            fr.length = output.length() - firstLineBreak;
            fr.format.setFontStyleHint(QFont::Monospace);
            fr.format.setForeground(mix);
            tl.setAdditionalFormats(QList<QTextLayout::FormatRange>() << fr);
        }
        tl.beginLayout();
        while (true) {
            QTextLine tLine = tl.createLine();
            if (!tLine.isValid())
                break;
            tLine.setLineWidth(positions.textAreaWidth());
            height += leading;
            tLine.setPosition(QPoint(0, height));
            height += fm.ascent() + fm.descent();
        }
        tl.endLayout();
        tl.draw(painter, QPoint(positions.textAreaLeft(), positions.top()));

        painter->setPen(mix);

        int bottomLine = positions.top() + fm.ascent() + height + leading;
        painter->drawText(positions.textAreaLeft(), bottomLine, testResult.fileName());
    } else {
        painter->setClipRect(positions.textArea());
        painter->drawText(positions.textAreaLeft(), positions.top() + fm.ascent(), output);
    }

    QString file = testResult.fileName();
    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);


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

    painter->setClipRect(opt.rect);
    painter->setPen(QColor::fromRgb(150, 150, 150));
    painter->drawLine(0, opt.rect.bottom(), opt.rect.right(), opt.rect.bottom());
    painter->restore();
}

QSize TestResultDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItemV4 opt = option;
    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();
    TestResultFilterModel *resultFilterModel = static_cast<TestResultFilterModel *>(view->model());
    TestResultModel *resultModel = static_cast<TestResultModel *>(resultFilterModel->sourceModel());
    LayoutPositions positions(opt, resultModel);
    QSize s;
    s.setWidth(opt.rect.width());

    if (selected) {
        TestResult testResult = resultModel->testResult(resultFilterModel->mapToSource(index));

        QString output;
        switch (testResult.result()) {
        case ResultType::PASS:
        case ResultType::FAIL:
        case ResultType::EXPECTED_FAIL:
        case ResultType::UNEXPECTED_PASS:
            output = testResult.className() + QLatin1String("::") + testResult.testCase();
            if (!testResult.dataTag().isEmpty())
                output.append(QString::fromLatin1(" (%1)").arg(testResult.dataTag()));
            if (!testResult.description().isEmpty()) {
                output.append(QLatin1Char('\n'));
                output.append(testResult.description());
            }
            break;
        default:
            output = testResult.description();
        }

        output.replace(QLatin1Char('\n'), QChar::LineSeparator);

        int height = 0;
        int leading = fm.leading();
        QTextLayout tl(output);
        tl.beginLayout();
        while (true) {
            QTextLine line = tl.createLine();
            if (!line.isValid())
                break;
            height += leading;
            line.setPosition(QPoint(0, height));
            height += fm.ascent() + fm.descent();
        }
        tl.endLayout();

        s.setHeight(height + leading + 3 + (testResult.fileName().isEmpty() ? 0 : fontHeight));
    } else {
        s.setHeight(fontHeight + 3);
    }

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

    return s;
}

void TestResultDelegate::emitSizeHintChanged(const QModelIndex &index)
{
    emit sizeHintChanged(index);
}

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

} // namespace Internal
} // namespace Autotest