aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/projectexplorer/devicesupport/devicetestdialog.cpp
blob: 0277fc827a0dd4d02501060bc4bef0c58fbf43e0 (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "devicetestdialog.h"

#include "../projectexplorertr.h"

#include <utils/fileutils.h>
#include <utils/layoutbuilder.h>

#include <QBrush>
#include <QColor>
#include <QDialogButtonBox>
#include <QFont>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QTextCharFormat>

namespace ProjectExplorer {
namespace Internal {

class DeviceTestDialog::DeviceTestDialogPrivate
{
public:
    DeviceTestDialogPrivate(DeviceTester *tester)
        : deviceTester(tester), finished(false)
    {
    }

    DeviceTester * const deviceTester;
    bool finished;
    QPlainTextEdit *textEdit;
    QDialogButtonBox *buttonBox;
};

DeviceTestDialog::DeviceTestDialog(const IDevice::Ptr &deviceConfiguration,
                                   QWidget *parent)
    : QDialog(parent)
    , d(std::make_unique<DeviceTestDialogPrivate>(deviceConfiguration->createDeviceTester()))
{
    resize(620, 580);
    d->textEdit = new QPlainTextEdit;
    d->textEdit->setReadOnly(true);
    d->buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel);

    using namespace Layouting;
    Column {
        d->textEdit,
        d->buttonBox,
    }.attachTo(this);

    d->deviceTester->setParent(this);
    connect(d->buttonBox, &QDialogButtonBox::rejected, this, &DeviceTestDialog::reject);
    connect(d->deviceTester, &DeviceTester::progressMessage,
            this, &DeviceTestDialog::handleProgressMessage);
    connect(d->deviceTester, &DeviceTester::errorMessage,
            this, &DeviceTestDialog::handleErrorMessage);
    connect(d->deviceTester, &DeviceTester::finished,
            this, &DeviceTestDialog::handleTestFinished);
    d->deviceTester->testDevice(deviceConfiguration);
}

DeviceTestDialog::~DeviceTestDialog() = default;

void DeviceTestDialog::reject()
{
    if (!d->finished) {
        d->deviceTester->disconnect(this);
        d->deviceTester->stopTest();
    }
    QDialog::reject();
}

void DeviceTestDialog::handleProgressMessage(const QString &message)
{
    addText(message, Utils::Theme::OutputPanes_NormalMessageTextColor, false);
}

void DeviceTestDialog::handleErrorMessage(const QString &message)
{
    addText(message, Utils::Theme::OutputPanes_ErrorMessageTextColor, false);
}

void DeviceTestDialog::handleTestFinished(DeviceTester::TestResult result)
{
    d->finished = true;
    d->buttonBox->button(QDialogButtonBox::Cancel)->setText(Tr::tr("Close"));

    if (result == DeviceTester::TestSuccess)
        addText(Tr::tr("Device test finished successfully."),
                Utils::Theme::OutputPanes_NormalMessageTextColor, true);
    else
        addText(Tr::tr("Device test failed."), Utils::Theme::OutputPanes_ErrorMessageTextColor, true);
}

void DeviceTestDialog::addText(const QString &text, Utils::Theme::Color color, bool bold)
{
    Utils::Theme *theme = Utils::creatorTheme();

    QTextCharFormat format = d->textEdit->currentCharFormat();
    format.setForeground(QBrush(theme->color(color)));
    QFont font = format.font();
    font.setBold(bold);
    format.setFont(font);
    d->textEdit->setCurrentCharFormat(format);
    d->textEdit->appendPlainText(text);
}

} // namespace Internal
} // namespace ProjectExplorer