aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmlprofiler/qmlprofilerstatewidget.cpp
blob: 859f26bada28155705054c3b52edc8959f11dc36 (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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** 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.
**
** 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.
**
****************************************************************************/

#include "qmlprofilerstatewidget.h"

#include <utils/qtcassert.h>
#include <utils/theme/theme.h>

#include <QPainter>
#include <QVBoxLayout>
#include <QLabel>
#include <QProgressBar>
#include <QTime>
#include <QDebug>
#include <QTimer>
#include <QPointer>

#include <functional>

namespace QmlProfiler {
namespace Internal {

class QmlProfilerStateWidget::QmlProfilerStateWidgetPrivate
{
    public:
    QmlProfilerStateWidgetPrivate(QmlProfilerStateWidget *qq) : text(nullptr) { Q_UNUSED(qq); }

    QLabel *text;

    QPointer<QmlProfilerStateManager> m_profilerState;
    QPointer<QmlProfilerModelManager> m_modelManager;
    QTimer timer;
};

QmlProfilerStateWidget::QmlProfilerStateWidget(QmlProfilerStateManager *stateManager,
                                QmlProfilerModelManager *modelManager, QWidget *parent)
    : QFrame(parent), d(new QmlProfilerStateWidgetPrivate(this))
{
    setObjectName(QLatin1String("QML Profiler State Display"));
    setFrameStyle(QFrame::StyledPanel);

    // UI elements
    auto layout = new QVBoxLayout(this);
    resize(200,70);

    d->text = new QLabel(this);
    d->text->setAlignment(Qt::AlignCenter);
    setAutoFillBackground(true);
    layout->addWidget(d->text);

    setLayout(layout);

    // profiler state
    d->m_modelManager = modelManager;

    modelManager->registerFeatures(0, QmlProfilerModelManager::QmlEventLoader(),
                                   std::bind(&QmlProfilerStateWidget::initialize, this),
                                   std::bind(&QmlProfilerStateWidget::clear, this),
                                   std::bind(&QmlProfilerStateWidget::clear, this));
    d->m_profilerState = stateManager;
    connect(&d->timer, &QTimer::timeout, this, &QmlProfilerStateWidget::updateDisplay);

    d->timer.setInterval(1000);
    setVisible(false);
}

QmlProfilerStateWidget::~QmlProfilerStateWidget()
{
    delete d;
}

void QmlProfilerStateWidget::reposition()
{
    QWidget *parentWidget = qobject_cast<QWidget *>(parent());
    // positioning it at 2/3 height (it looks better)
    move(parentWidget->width()/2 - width()/2, parentWidget->height()/3 - height()/2);
}

void QmlProfilerStateWidget::initialize()
{
    connect(d->m_profilerState, &QmlProfilerStateManager::stateChanged,
            this, &QmlProfilerStateWidget::updateDisplay);
    connect(d->m_profilerState, &QmlProfilerStateManager::serverRecordingChanged,
            this, &QmlProfilerStateWidget::updateDisplay);
    d->timer.start();
    updateDisplay();
}

void QmlProfilerStateWidget::showText(const QString &text)
{
    setVisible(true);
    d->text->setText(text);
    resize(300, 70);
    reposition();
}

void QmlProfilerStateWidget::clear()
{
    disconnect(d->m_profilerState, &QmlProfilerStateManager::stateChanged,
               this, &QmlProfilerStateWidget::updateDisplay);
    disconnect(d->m_profilerState, &QmlProfilerStateManager::serverRecordingChanged,
               this, &QmlProfilerStateWidget::updateDisplay);
    d->timer.stop();
    setVisible(false);
}

void QmlProfilerStateWidget::updateDisplay()
{
    QTC_ASSERT(d->m_modelManager, return);
    QTC_ASSERT(d->m_profilerState, return);

    // When application is being profiled
    if (d->m_profilerState->serverRecording()) {
        // Heuristic to not show the number if the application will only send the events when it
        // stops. The number is still > 0 then because we get some StartTrace etc.
        const int numEvents = d->m_modelManager->numEvents();
        showText(numEvents > 256 ? tr("Profiling application: %n events", nullptr, numEvents) :
                                   tr("Profiling application"));
    } else if (d->m_modelManager->traceDuration() > 0 && d->m_modelManager->isEmpty()) {
        // After profiling, there is an empty trace
        showText(tr("No QML events recorded"));
    } else if (!d->m_modelManager->isEmpty()) {
        // When datamodel is acquiring data
        if (d->m_profilerState->currentState() != QmlProfilerStateManager::Idle) {
            // we don't know how much more, so progress numbers are strange here
            showText(tr("Loading buffered data: %n events", nullptr,
                        d->m_modelManager->numEvents()));
        } else {
            // Application died before all data could be read
            showText(tr("Loading offline data: %n events", nullptr,
                        d->m_modelManager->numEvents()));
        }
    } else {
        showText(tr("Waiting for data"));
    }
}

} // namespace Internal
} // namespace QmlProfiler