void QmlProfilerStateWidget::updateDisplay()
{
    // When datamodel is acquiring data
    if (!d->loadingDone && !d->emptyList && !d->appKilled) {
        setVisible(true);
        d->text->setText(tr("Loading data"));
        if (d->isRecording) {
            d->isRecording = false;
            d->estimatedProfilingTime = d->profilingTimer.elapsed();
            emit newTimeEstimation(d->estimatedProfilingTime);
        }
        d->progressBar->setValue(d->m_modelManager->progress() * 1000);
        d->progressBar->setVisible(true);
        resize(300,70);
        reposition();
        return;
    }

    // When application is being profiled
    if (d->isRecording) {
        setVisible(true);
        d->progressBar->setVisible(false);
        d->text->setText(tr("Profiling application"));
        resize(200,70);
        reposition();
        return;
    }

    // After profiling, there is an empty trace
    if (d->traceAvailable && d->loadingDone && d->emptyList) {
        setVisible(true);
        d->progressBar->setVisible(false);
        d->text->setText(tr("No QML events recorded"));
        resize(200,70);
        reposition();
        return;
    }

    // Application died before all data could be read
    if (!d->loadingDone && !d->emptyList && d->appKilled) {
        setVisible(true);
        d->text->setText(tr("Application stopped before loading all data"));
        if (d->isRecording) {
            d->isRecording = false;
            d->estimatedProfilingTime = d->profilingTimer.elapsed();
            emit newTimeEstimation(d->estimatedProfilingTime);
        }
        d->progressBar->setValue(d->m_modelManager->progress() * 1000);
        d->progressBar->setVisible(true);
        resize(300,70);
        reposition();
        return;
    }

    // There is a trace on view, hide this dialog
    d->progressBar->setVisible(false);
    setVisible(false);
}
QmlProfilerStateWidget::QmlProfilerStateWidget(QmlProfilerStateManager *stateManager,
                                QmlProfilerModelManager *modelManager, QWidget *parent)
    : QWidget(parent), d(new QmlProfilerStateWidgetPrivate(this))
{
    setObjectName(QLatin1String("QML Profiler State Display"));

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

    d->shadowPic.load(QLatin1String(":/qmlprofiler/dialog_shadow.png"));

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

    d->progressBar = new QProgressBar(this);
    layout->addWidget(d->progressBar);
    d->progressBar->setMaximum(1000);
    d->progressBar->setVisible(false);

    setLayout(layout);

    // internal state
    d->isRecording = false;
    d->appKilled = false;
    d->traceAvailable = false;
    d->loadingDone = true;
    d->emptyList = true;

    // profiler state
    d->m_modelManager = modelManager;
    connect(d->m_modelManager,SIGNAL(stateChanged()), this, SLOT(dataStateChanged()));
    connect(d->m_modelManager,SIGNAL(progressChanged()), this, SLOT(dataStateChanged()));
    connect(this, SIGNAL(newTimeEstimation(qint64)), d->m_modelManager, SLOT(newTimeEstimation(qint64)));
    d->m_profilerState = stateManager;
    connect(d->m_profilerState,SIGNAL(stateChanged()), this, SLOT(profilerStateChanged()));
    connect(d->m_profilerState, SIGNAL(serverRecordingChanged()),
            this, SLOT(profilerStateChanged()));

    updateDisplay();
    connect(parent,SIGNAL(resized()),this,SLOT(reposition()));
}
void QmlProfilerStateWidget::showText(const QString &text, bool showProgress)
{
    setVisible(true);
    if (showProgress) {
        if (d->isRecording) {
            d->isRecording = false;
            d->estimatedProfilingTime = d->profilingTimer.elapsed();
            emit newTimeEstimation(d->estimatedProfilingTime);
        }
        d->progressBar->setValue(d->m_modelManager->progress() * 1000);
    }
    d->progressBar->setVisible(showProgress);
    d->text->setText(text);
    resize(300, 70);
    reposition();
}
void QmlProfilerStateWidget::profilerStateChanged()
{
    if (d->m_profilerState->currentState() == QmlProfilerStateManager::AppKilled)
        d->appKilled = true;
    else
        if (d->m_profilerState->currentState() == QmlProfilerStateManager::AppStarting)
            d->appKilled = false;

    d->isRecording = d->m_profilerState->serverRecording();
    if (d->isRecording)
        d->profilingTimer.start();
    else {
        // estimated time in ns
        d->estimatedProfilingTime = d->profilingTimer.elapsed() * 1e6;
        emit newTimeEstimation(d->estimatedProfilingTime);
    }
    updateDisplay();
}