qmlprofilertool.cpp 21.7 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Kai Koehne's avatar
Kai Koehne committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Kai Koehne's avatar
Kai Koehne committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Kai Koehne's avatar
Kai Koehne committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
Kai Koehne's avatar
Kai Koehne committed
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
Kai Koehne's avatar
Kai Koehne committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Kai Koehne's avatar
Kai Koehne committed
29

Christiaan Janssen's avatar
Christiaan Janssen committed
30
#include "qmlprofilertool.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
31
#include "qmlprofilerstatemanager.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
32
#include "qmlprofilerengine.h"
33
34
#include "qmlprofilerconstants.h"
#include "qmlprofilerattachdialog.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
35
36
#include "qmlprofilerviewmanager.h"
#include "qmlprofilerclientmanager.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
37
#include "qmlprofilermodelmanager.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
38
39
#include "qmlprofilerdetailsrewriter.h"
#include "timelinerenderer.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
40
41

#include <analyzerbase/analyzermanager.h>
42
#include <analyzerbase/analyzerruncontrol.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
43

44
#include <utils/fancymainwindow.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
45
#include <utils/fileinprojectfinder.h>
46
#include <utils/qtcassert.h>
47
#include <projectexplorer/environmentaspect.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
48
49
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/project.h>
50
#include <projectexplorer/target.h>
51
#include <projectexplorer/session.h>
52
#include <projectexplorer/kitinformation.h>
53
#include <projectexplorer/localapplicationrunconfiguration.h>
54
#include <texteditor/itexteditor.h>
55

56
#include <coreplugin/coreconstants.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
57
#include <coreplugin/editormanager/editormanager.h>
58
#include <coreplugin/icore.h>
59
#include <coreplugin/messagemanager.h>
60
#include <coreplugin/helpmanager.h>
61
62
63
64
65
#include <coreplugin/modemanager.h>
#include <coreplugin/imode.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
66

Tobias Hunger's avatar
Tobias Hunger committed
67
#include <qtsupport/qtkitinformation.h>
Tobias Hunger's avatar
Tobias Hunger committed
68

69
#include <QApplication>
hjk's avatar
hjk committed
70
#include <QFileDialog>
71
72
73
#include <QHBoxLayout>
#include <QLabel>
#include <QMenu>
hjk's avatar
hjk committed
74
#include <QMessageBox>
75
#include <QTcpServer>
hjk's avatar
hjk committed
76
77
78
#include <QTime>
#include <QTimer>
#include <QToolButton>
79

80
81
using namespace Core;
using namespace Core::Constants;
Christiaan Janssen's avatar
Christiaan Janssen committed
82
using namespace Analyzer;
83
using namespace Analyzer::Constants;
Kai Koehne's avatar
Kai Koehne committed
84
using namespace QmlProfiler::Internal;
Christiaan Janssen's avatar
Christiaan Janssen committed
85
using namespace QmlProfiler::Constants;
86
using namespace QmlDebug;
87
using namespace ProjectExplorer;
Christiaan Janssen's avatar
Christiaan Janssen committed
88
89
90
91

class QmlProfilerTool::QmlProfilerToolPrivate
{
public:
Christiaan Janssen's avatar
Christiaan Janssen committed
92
93
    QmlProfilerStateManager *m_profilerState;
    QmlProfilerClientManager *m_profilerConnections;
Christiaan Janssen's avatar
Christiaan Janssen committed
94
    QmlProfilerModelManager *m_profilerModelManager;
Christiaan Janssen's avatar
Christiaan Janssen committed
95
96

    QmlProfilerViewManager *m_viewContainer;
97
    Utils::FileInProjectFinder m_projectFinder;
98
    QToolButton *m_recordButton;
99
    QToolButton *m_clearButton;
Christiaan Janssen's avatar
Christiaan Janssen committed
100
101
102
103
104
105
106

    // elapsed time display
    QTimer m_recordingTimer;
    QTime m_recordingElapsedTime;
    QLabel *m_timeLabel;

    // save and load actions
107
    QAction *m_saveQmlTrace;
Christiaan Janssen's avatar
Christiaan Janssen committed
108
    QAction *m_loadQmlTrace;
Christiaan Janssen's avatar
Christiaan Janssen committed
109
110
};

111
QmlProfilerTool::QmlProfilerTool(QObject *parent)
hjk's avatar
hjk committed
112
    : IAnalyzerTool(parent), d(new QmlProfilerToolPrivate)
Christiaan Janssen's avatar
Christiaan Janssen committed
113
{
114
    setObjectName(QLatin1String("QmlProfilerTool"));
115
116
    setRunMode(QmlProfilerRunMode);
    setToolMode(AnyMode);
hjk's avatar
hjk committed
117

Christiaan Janssen's avatar
Christiaan Janssen committed
118
119
    d->m_profilerState = 0;
    d->m_viewContainer = 0;
120

Christiaan Janssen's avatar
Christiaan Janssen committed
121
122
123
124
125
126
127
128
129
    qmlRegisterType<TimelineRenderer>("Monitor", 1, 0,"TimelineRenderer");

    d->m_profilerState = new QmlProfilerStateManager(this);
    connect(d->m_profilerState, SIGNAL(stateChanged()), this, SLOT(profilerStateChanged()));
    connect(d->m_profilerState, SIGNAL(clientRecordingChanged()), this, SLOT(clientRecordingChanged()));
    connect(d->m_profilerState, SIGNAL(serverRecordingChanged()), this, SLOT(serverRecordingChanged()));

    d->m_profilerConnections = new QmlProfilerClientManager(this);
    d->m_profilerConnections->registerProfilerStateManager(d->m_profilerState);
130
    connect(d->m_profilerConnections, SIGNAL(connectionClosed()), this, SLOT(clientsDisconnected()));
Christiaan Janssen's avatar
Christiaan Janssen committed
131

Christiaan Janssen's avatar
Christiaan Janssen committed
132
133
134
    d->m_profilerModelManager = new QmlProfilerModelManager(&d->m_projectFinder, this);
    connect(d->m_profilerModelManager, SIGNAL(stateChanged()), this, SLOT(profilerDataModelStateChanged()));
    connect(d->m_profilerModelManager, SIGNAL(error(QString)), this, SLOT(showErrorDialog(QString)));
135

Christiaan Janssen's avatar
Christiaan Janssen committed
136
    d->m_profilerConnections->setModelManager(d->m_profilerModelManager);
137
138
139
    Command *command = 0;
    const Context globalContext(C_GLOBAL);

hjk's avatar
hjk committed
140
141
    ActionContainer *menu = ActionManager::actionContainer(M_DEBUG_ANALYZER);
    ActionContainer *options = ActionManager::createMenu(M_DEBUG_ANALYZER_QML_OPTIONS);
142
143
144
145
    options->menu()->setTitle(tr("QML Profiler Options"));
    menu->addMenu(options, G_ANALYZER_OPTIONS);
    options->menu()->setEnabled(true);

Christiaan Janssen's avatar
Christiaan Janssen committed
146
    QAction *act = d->m_loadQmlTrace = new QAction(tr("Load QML Trace"), options);
hjk's avatar
hjk committed
147
    command = ActionManager::registerAction(act, "Analyzer.Menu.StartAnalyzer.QMLProfilerOptions.LoadQMLTrace", globalContext);
148
149
150
151
152
    connect(act, SIGNAL(triggered()), this, SLOT(showLoadDialog()));
    options->addAction(command);

    act = d->m_saveQmlTrace = new QAction(tr("Save QML Trace"), options);
    d->m_saveQmlTrace->setEnabled(false);
hjk's avatar
hjk committed
153
    command = ActionManager::registerAction(act, "Analyzer.Menu.StartAnalyzer.QMLProfilerOptions.SaveQMLTrace", globalContext);
154
155
    connect(act, SIGNAL(triggered()), this, SLOT(showSaveDialog()));
    options->addAction(command);
Christiaan Janssen's avatar
Christiaan Janssen committed
156
157
158

    d->m_recordingTimer.setInterval(100);
    connect(&d->m_recordingTimer, SIGNAL(timeout()), this, SLOT(updateTimeDisplay()));
Christiaan Janssen's avatar
Christiaan Janssen committed
159
160
161
162
163
164
165
}

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

166
AnalyzerRunControl *QmlProfilerTool::createRunControl(const AnalyzerStartParameters &sp,
167
    RunConfiguration *runConfiguration)
Christiaan Janssen's avatar
Christiaan Janssen committed
168
{
169
    QmlProfilerRunControl *engine = new QmlProfilerRunControl(sp, runConfiguration);
170

Christiaan Janssen's avatar
Christiaan Janssen committed
171
172
173
    engine->registerProfilerStateManager(d->m_profilerState);

    bool isTcpConnection = true;
174

175
176
177
178
    if (runConfiguration) {
        // Check minimum Qt Version. We cannot really be sure what the Qt version
        // at runtime is, but guess that the active build configuraiton has been used.
        QtSupport::QtVersionNumber minimumVersion(4, 7, 4);
Tobias Hunger's avatar
Tobias Hunger committed
179
        QtSupport::BaseQtVersion *version = QtSupport::QtKitInformation::qtVersion(runConfiguration->target()->kit());
Tobias Hunger's avatar
Tobias Hunger committed
180
181
        if (version) {
            if (version->isValid() && version->qtVersion() < minimumVersion) {
182
183
184
185
186
187
188
189
                int result = QMessageBox::warning(QApplication::activeWindow(), tr("QML Profiler"),
                     tr("The QML profiler requires Qt 4.7.4 or newer.\n"
                     "The Qt version configured in your active build configuration is too old.\n"
                     "Do you want to continue?"), QMessageBox::Yes, QMessageBox::No);
                if (result == QMessageBox::No)
                    return 0;
            }
        }
190
191
    }

192
    // FIXME: Check that there's something sensible in sp.connParams
193
    if (isTcpConnection)
194
        d->m_profilerConnections->setTcpConnection(sp.analyzerHost, sp.analyzerPort);
195

196
197
198
199
200
    //
    // Initialize m_projectFinder
    //

    QString projectDirectory;
201
202
    if (runConfiguration) {
        Project *project = runConfiguration->target()->project();
203
        projectDirectory = project->projectDirectory().toString();
204
    }
205

206
    populateFileFinder(projectDirectory, sp.sysroot);
207

Christiaan Janssen's avatar
Christiaan Janssen committed
208
209
    connect(engine, SIGNAL(processRunning(quint16)), d->m_profilerConnections, SLOT(connectClient(quint16)));
    connect(d->m_profilerConnections, SIGNAL(connectionFailed()), engine, SLOT(cancelProcess()));
Christiaan Janssen's avatar
Christiaan Janssen committed
210
211
212
213

    return engine;
}

214
215
216
static QString sysroot(RunConfiguration *runConfig)
{
    QTC_ASSERT(runConfig, return QString());
hjk's avatar
hjk committed
217
218
219
    Kit *k = runConfig->target()->kit();
    if (k && SysRootKitInformation::hasSysRoot(k))
        return SysRootKitInformation::sysRoot(runConfig->target()->kit()).toString();
220
221
222
    return QString();
}

223
224
QWidget *QmlProfilerTool::createWidgets()
{
Christiaan Janssen's avatar
Christiaan Janssen committed
225
    QTC_ASSERT(!d->m_viewContainer, return 0);
226

Christiaan Janssen's avatar
Christiaan Janssen committed
227

Christiaan Janssen's avatar
Christiaan Janssen committed
228
229
    d->m_viewContainer = new QmlProfilerViewManager(this,
                                                    this,
Christiaan Janssen's avatar
Christiaan Janssen committed
230
                                                    d->m_profilerModelManager,
Christiaan Janssen's avatar
Christiaan Janssen committed
231
232
233
                                                    d->m_profilerState);
    connect(d->m_viewContainer, SIGNAL(gotoSourceLocation(QString,int,int)),
            this, SLOT(gotoSourceLocation(QString,int,int)));
234

235
236
237
    //
    // Toolbar
    //
Christiaan Janssen's avatar
Christiaan Janssen committed
238
239
240
241
242
243
244
    QWidget *toolbarWidget = new QWidget;
    toolbarWidget->setObjectName(QLatin1String("QmlProfilerToolBarWidget"));

    QHBoxLayout *layout = new QHBoxLayout;
    layout->setMargin(0);
    layout->setSpacing(0);

245
246
247
    d->m_recordButton = new QToolButton(toolbarWidget);
    d->m_recordButton->setCheckable(true);

248
    connect(d->m_recordButton,SIGNAL(clicked(bool)), this, SLOT(recordingButtonChanged(bool)));
249
    d->m_recordButton->setChecked(true);
Christiaan Janssen's avatar
Christiaan Janssen committed
250
    setRecording(d->m_profilerState->clientRecording());
251
252
    layout->addWidget(d->m_recordButton);

253
254
    d->m_clearButton = new QToolButton(toolbarWidget);
    d->m_clearButton->setIcon(QIcon(QLatin1String(":/qmlprofiler/clean_pane_small.png")));
Christiaan Janssen's avatar
Christiaan Janssen committed
255
    d->m_clearButton->setToolTip(tr("Discard data"));
Christiaan Janssen's avatar
Christiaan Janssen committed
256
257
258

    connect(d->m_clearButton,SIGNAL(clicked()), this, SLOT(clearData()));

259
260
    layout->addWidget(d->m_clearButton);

Christiaan Janssen's avatar
Christiaan Janssen committed
261
    d->m_timeLabel = new QLabel();
Lorenz Haas's avatar
Lorenz Haas committed
262
    QPalette palette;
Christiaan Janssen's avatar
Christiaan Janssen committed
263
    palette.setColor(QPalette::WindowText, Qt::white);
Christiaan Janssen's avatar
Christiaan Janssen committed
264
265
266
267
    d->m_timeLabel->setPalette(palette);
    d->m_timeLabel->setIndent(10);
    updateTimeDisplay();
    layout->addWidget(d->m_timeLabel);
268

Christiaan Janssen's avatar
Christiaan Janssen committed
269
270
    toolbarWidget->setLayout(layout);

271
272
273
274
    // When the widgets are requested we assume that the session data
    // is available, then we can populate the file finder
    populateFileFinder();

Christiaan Janssen's avatar
Christiaan Janssen committed
275
    return toolbarWidget;
Christiaan Janssen's avatar
Christiaan Janssen committed
276
277
}

278
279
280
281
void QmlProfilerTool::populateFileFinder(QString projectDirectory, QString activeSysroot)
{
    // Initialize filefinder with some sensible default
    QStringList sourceFiles;
hjk's avatar
hjk committed
282
283
    QList<Project *> projects = SessionManager::projects();
    if (Project *startupProject = SessionManager::startupProject()) {
284
        // startup project first
hjk's avatar
hjk committed
285
        projects.removeOne(startupProject);
286
287
288
289
290
291
        projects.insert(0, startupProject);
    }
    foreach (Project *project, projects)
        sourceFiles << project->files(Project::ExcludeGeneratedFiles);

    if (!projects.isEmpty()) {
292
        if (projectDirectory.isEmpty())
293
            projectDirectory = projects.first()->projectDirectory().toString();
294
295
296
297
298
299
300
301
302
303
304
305
306

        if (activeSysroot.isEmpty()) {
            if (Target *target = projects.first()->activeTarget())
                if (RunConfiguration *rc = target->activeRunConfiguration())
                    activeSysroot = sysroot(rc);
        }
    }

    d->m_projectFinder.setProjectDirectory(projectDirectory);
    d->m_projectFinder.setProjectFiles(sourceFiles);
    d->m_projectFinder.setSysroot(activeSysroot);
}

307
void QmlProfilerTool::recordingButtonChanged(bool recording)
308
{
Christiaan Janssen's avatar
Christiaan Janssen committed
309
    d->m_profilerState->setClientRecording(recording);
310
311
312
313
}

void QmlProfilerTool::setRecording(bool recording)
{
Christiaan Janssen's avatar
Christiaan Janssen committed
314
    // update display
315
316
317
318
319
    d->m_recordButton->setToolTip( recording ? tr("Disable profiling") : tr("Enable profiling"));
    d->m_recordButton->setIcon(QIcon(recording ? QLatin1String(":/qmlprofiler/recordOn.png") :
                                                 QLatin1String(":/qmlprofiler/recordOff.png")));

    d->m_recordButton->setChecked(recording);
Christiaan Janssen's avatar
Christiaan Janssen committed
320

Christiaan Janssen's avatar
Christiaan Janssen committed
321
322
323
324
325
326
327
328
329
    // manage timer
    if (d->m_profilerState->currentState() == QmlProfilerStateManager::AppRunning) {
        if (recording) {
            d->m_recordingTimer.start();
            d->m_recordingElapsedTime.start();
        } else {
            d->m_recordingTimer.stop();
        }
    }
330
331
}

332
void QmlProfilerTool::gotoSourceLocation(const QString &fileUrl, int lineNumber, int columnNumber)
Christiaan Janssen's avatar
Christiaan Janssen committed
333
{
Kai Koehne's avatar
Kai Koehne committed
334
    if (lineNumber < 0 || fileUrl.isEmpty())
Christiaan Janssen's avatar
Christiaan Janssen committed
335
336
        return;

337
    const QString projectFileName = d->m_projectFinder.findFile(fileUrl);
Christiaan Janssen's avatar
Christiaan Janssen committed
338

339
340
341
342
    QFileInfo fileInfo(projectFileName);
    if (!fileInfo.exists() || !fileInfo.isReadable())
        return;

hjk's avatar
hjk committed
343
    IEditor *editor = EditorManager::openEditor(projectFileName);
Christiaan Janssen's avatar
Christiaan Janssen committed
344
345
346
    TextEditor::ITextEditor *textEditor = qobject_cast<TextEditor::ITextEditor*>(editor);

    if (textEditor) {
347
        EditorManager::addCurrentPositionToNavigationHistory();
348
349
350
        // textEditor counts columns starting with 0, but the ASTs store the
        // location starting with 1, therefore the -1 in the call to gotoLine
        textEditor->gotoLine(lineNumber, columnNumber - 1);
Christiaan Janssen's avatar
Christiaan Janssen committed
351
352
353
        textEditor->widget()->setFocus();
    }
}
Christiaan Janssen's avatar
Christiaan Janssen committed
354

Christiaan Janssen's avatar
Christiaan Janssen committed
355
void QmlProfilerTool::updateTimeDisplay()
356
{
Christiaan Janssen's avatar
Christiaan Janssen committed
357
358
359
360
    double seconds = 0;
    if (d->m_profilerState->serverRecording() &&
        d->m_profilerState->currentState() == QmlProfilerStateManager::AppRunning) {
            seconds = d->m_recordingElapsedTime.elapsed() / 1000.0;
361
362
    } else if (d->m_profilerModelManager->state() != QmlProfilerDataState::Empty &&
               d->m_profilerModelManager->state() != QmlProfilerDataState::ClearingData) {
Christiaan Janssen's avatar
Christiaan Janssen committed
363
        seconds = d->m_profilerModelManager->traceTime()->duration() / 1.0e9;
Christiaan Janssen's avatar
Christiaan Janssen committed
364
    }
365
    QString timeString = QString::number(seconds,'f',1);
Christiaan Janssen's avatar
Christiaan Janssen committed
366
367
    QString profilerTimeStr = QmlProfilerTool::tr("%1 s").arg(timeString, 6);
    d->m_timeLabel->setText(tr("Elapsed: %1").arg(profilerTimeStr));
368
369
}

Christiaan Janssen's avatar
Christiaan Janssen committed
370
void QmlProfilerTool::clearData()
371
{
Christiaan Janssen's avatar
Christiaan Janssen committed
372
    d->m_profilerModelManager->clear();
373
    d->m_profilerConnections->discardPendingData();
Christiaan Janssen's avatar
Christiaan Janssen committed
374
}
375

376
377
void QmlProfilerTool::clearDisplay()
{
Christiaan Janssen's avatar
Christiaan Janssen committed
378
379
380
    d->m_profilerConnections->clearBufferedData();
    d->m_viewContainer->clear();
    updateTimeDisplay();
381
382
}

383
static void startRemoteTool(IAnalyzerTool *tool, StartMode mode)
384
{
385
    Id kitId;
386
    quint16 port;
387
    Kit *kit = 0;
388
389

    {
hjk's avatar
hjk committed
390
        QSettings *settings = ICore::settings();
391

392
393
        kitId = Id::fromSetting(settings->value(QLatin1String("AnalyzerQmlAttachDialog/kitId")));
        port = settings->value(QLatin1String("AnalyzerQmlAttachDialog/port"), 3768).toUInt();
394
395
396

        QmlProfilerAttachDialog dialog;

397
        dialog.setKitId(kitId);
398
399
400
401
402
        dialog.setPort(port);

        if (dialog.exec() != QDialog::Accepted)
            return;

403
        kit = dialog.kit();
404
405
        port = dialog.port();

406
        settings->setValue(QLatin1String("AnalyzerQmlAttachDialog/kitId"), kit->id().toSetting());
407
408
        settings->setValue(QLatin1String("AnalyzerQmlAttachDialog/port"), port);
    }
409

410
411
    AnalyzerStartParameters sp;
    sp.startMode = mode;
412
413
414
415

    IDevice::ConstPtr device = DeviceKitInformation::device(kit);
    if (device) {
        sp.connParams = device->sshParameters();
hjk's avatar
hjk committed
416
        sp.analyzerHost = device->qmlProfilerHost();
417
418
    }
    sp.sysroot = SysRootKitInformation::sysRoot(kit).toString();
419
    sp.analyzerPort = port;
420

421
    AnalyzerRunControl *rc = tool->createRunControl(sp, 0);
422
    QObject::connect(AnalyzerManager::stopAction(), SIGNAL(triggered()), rc, SLOT(stopIt()));
423

424
    ProjectExplorerPlugin::instance()->startRunControl(rc, tool->runMode());
425
}
426

427
428
void QmlProfilerTool::startTool(StartMode mode)
{
429
430
431
    // Make sure mode is shown.
    AnalyzerManager::showMode();

432
433
    if (mode == StartLocal) {
        // ### not sure if we're supposed to check if the RunConFiguration isEnabled
hjk's avatar
hjk committed
434
435
        Project *pro = SessionManager::startupProject();
        ProjectExplorerPlugin::instance()->runProject(pro, runMode());
436
437
438
    } else if (mode == StartRemote) {
        startRemoteTool(this, mode);
    }
439
}
440

441
void QmlProfilerTool::logState(const QString &msg)
442
{
hjk's avatar
hjk committed
443
    MessageManager::write(msg, MessageManager::Flash);
444
445
446
447
}

void QmlProfilerTool::logError(const QString &msg)
{
hjk's avatar
hjk committed
448
    MessageManager::write(msg);
449
}
450

Christiaan Janssen's avatar
Christiaan Janssen committed
451
452
void QmlProfilerTool::showErrorDialog(const QString &error)
{
hjk's avatar
hjk committed
453
    QMessageBox *errorDialog = new QMessageBox(ICore::mainWindow());
Christiaan Janssen's avatar
Christiaan Janssen committed
454
455
456
457
458
459
460
461
462
    errorDialog->setIcon(QMessageBox::Warning);
    errorDialog->setWindowTitle(tr("QML Profiler"));
    errorDialog->setText(error);
    errorDialog->setStandardButtons(QMessageBox::Ok);
    errorDialog->setDefaultButton(QMessageBox::Ok);
    errorDialog->setModal(false);
    errorDialog->show();
}

463
464
void QmlProfilerTool::showSaveOption()
{
Christiaan Janssen's avatar
Christiaan Janssen committed
465
    d->m_saveQmlTrace->setEnabled(!d->m_profilerModelManager->isEmpty());
466
467
}

468
469
void QmlProfilerTool::showSaveDialog()
{
hjk's avatar
hjk committed
470
    QString filename = QFileDialog::getSaveFileName(ICore::mainWindow(), tr("Save QML Trace"), QString(),
471
                                                    tr("QML traces (*%1)").arg(QLatin1String(TraceFileExtension)));
472
    if (!filename.isEmpty()) {
473
474
        if (!filename.endsWith(QLatin1String(TraceFileExtension)))
            filename += QLatin1String(TraceFileExtension);
Christiaan Janssen's avatar
Christiaan Janssen committed
475
        d->m_profilerModelManager->save(filename);
476
477
478
479
480
    }
}

void QmlProfilerTool::showLoadDialog()
{
481
    if (ModeManager::currentMode()->id() != MODE_ANALYZE)
482
483
        AnalyzerManager::showMode();

484
    AnalyzerManager::selectTool(this, StartRemote);
485

hjk's avatar
hjk committed
486
    QString filename = QFileDialog::getOpenFileName(ICore::mainWindow(), tr("Load QML Trace"), QString(),
487
                                                    tr("QML traces (*%1)").arg(QLatin1String(TraceFileExtension)));
488
489
490

    if (!filename.isEmpty()) {
        // delayed load (prevent graphical artifacts due to long load time)
Christiaan Janssen's avatar
Christiaan Janssen committed
491
492
        d->m_profilerModelManager->setFilename(filename);
        QTimer::singleShot(100, d->m_profilerModelManager, SLOT(load()));
493
494
    }
}
495

496
497
498
499
void QmlProfilerTool::clientsDisconnected()
{
    // If the application stopped by itself, check if we have all the data
    if (d->m_profilerState->currentState() == QmlProfilerStateManager::AppDying) {
Christiaan Janssen's avatar
Christiaan Janssen committed
500
        if (d->m_profilerModelManager->state() == QmlProfilerDataState::AcquiringData)
501
502
503
504
505
506
507
508
509
510
            d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppKilled);
        else
            d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppStopped);

        // ... and return to the "base" state
        d->m_profilerState->setCurrentState(QmlProfilerStateManager::Idle);
    }
    // If the connection is closed while the app is still running, no special action is needed
}

Christiaan Janssen's avatar
Christiaan Janssen committed
511
void QmlProfilerTool::profilerDataModelStateChanged()
512
{
Christiaan Janssen's avatar
Christiaan Janssen committed
513
514
    switch (d->m_profilerModelManager->state()) {
    case QmlProfilerDataState::Empty :
515
516
        break;
    case QmlProfilerDataState::ClearingData :
Christiaan Janssen's avatar
Christiaan Janssen committed
517
518
        clearDisplay();
        break;
Christiaan Janssen's avatar
Christiaan Janssen committed
519
520
    case QmlProfilerDataState::AcquiringData :
    case QmlProfilerDataState::ProcessingData :
Christiaan Janssen's avatar
Christiaan Janssen committed
521
522
        // nothing to be done for these two
        break;
Christiaan Janssen's avatar
Christiaan Janssen committed
523
    case QmlProfilerDataState::Done :
Christiaan Janssen's avatar
Christiaan Janssen committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
        if (d->m_profilerState->currentState() == QmlProfilerStateManager::AppStopRequested)
            d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppReadyToStop);
        showSaveOption();
        updateTimeDisplay();
    break;
    default:
        break;
    }
}

QList <QAction *> QmlProfilerTool::profilerContextMenuActions() const
{
    QList <QAction *> commonActions;
    commonActions << d->m_loadQmlTrace << d->m_saveQmlTrace;
    return commonActions;
}

void QmlProfilerTool::showNonmodalWarning(const QString &warningMsg)
{
hjk's avatar
hjk committed
543
    QMessageBox *noExecWarning = new QMessageBox(ICore::mainWindow());
Christiaan Janssen's avatar
Christiaan Janssen committed
544
545
546
547
548
549
550
551
552
553
554
    noExecWarning->setIcon(QMessageBox::Warning);
    noExecWarning->setWindowTitle(tr("QML Profiler"));
    noExecWarning->setText(warningMsg);
    noExecWarning->setStandardButtons(QMessageBox::Ok);
    noExecWarning->setDefaultButton(QMessageBox::Ok);
    noExecWarning->setModal(false);
    noExecWarning->show();
}

QMessageBox *QmlProfilerTool::requestMessageBox()
{
hjk's avatar
hjk committed
555
    return new QMessageBox(ICore::mainWindow());
Christiaan Janssen's avatar
Christiaan Janssen committed
556
557
558
559
}

void QmlProfilerTool::handleHelpRequest(const QString &link)
{
560
    HelpManager::handleHelpRequest(link);
561
}
562

Christiaan Janssen's avatar
Christiaan Janssen committed
563
void QmlProfilerTool::profilerStateChanged()
564
{
Christiaan Janssen's avatar
Christiaan Janssen committed
565
    switch (d->m_profilerState->currentState()) {
566
567
568
569
570
571
    case QmlProfilerStateManager::AppDying : {
        // If already disconnected when dying, check again that all data was read
        if (!d->m_profilerConnections->isConnected())
            QTimer::singleShot(0, this, SLOT(clientsDisconnected()));
        break;
    }
Christiaan Janssen's avatar
Christiaan Janssen committed
572
    case QmlProfilerStateManager::AppKilled : {
Jarek Kobus's avatar
Jarek Kobus committed
573
        showNonmodalWarning(tr("Application finished before loading profiled data.\nPlease use the stop button instead."));
Christiaan Janssen's avatar
Christiaan Janssen committed
574
        d->m_profilerModelManager->clear();
575
576
        break;
    }
Christiaan Janssen's avatar
Christiaan Janssen committed
577
578
579
580
581
582
    case QmlProfilerStateManager::Idle :
        // when the app finishes, set recording display to client status
        setRecording(d->m_profilerState->clientRecording());
        break;
    default:
        // no special action needed for other states
583
584
        break;
    }
Christiaan Janssen's avatar
Christiaan Janssen committed
585
586
587
588
589
590
}

void QmlProfilerTool::clientRecordingChanged()
{
    // if application is running, display server record changes
    // if application is stopped, display client record changes
591
    if (d->m_profilerState->currentState() != QmlProfilerStateManager::AppRunning)
Christiaan Janssen's avatar
Christiaan Janssen committed
592
        setRecording(d->m_profilerState->clientRecording());
593
}
594

Christiaan Janssen's avatar
Christiaan Janssen committed
595
void QmlProfilerTool::serverRecordingChanged()
596
{
Christiaan Janssen's avatar
Christiaan Janssen committed
597
598
599
600
    if (d->m_profilerState->currentState() == QmlProfilerStateManager::AppRunning) {
        setRecording(d->m_profilerState->serverRecording());
        // clear the old data each time we start a new profiling session
        if (d->m_profilerState->serverRecording()) {
Christiaan Janssen's avatar
Christiaan Janssen committed
601
            d->m_clearButton->setEnabled(false);
Christiaan Janssen's avatar
Christiaan Janssen committed
602
            clearData();
Christiaan Janssen's avatar
Christiaan Janssen committed
603
604
605
            d->m_profilerModelManager->prepareForWriting();
        } else {
            d->m_clearButton->setEnabled(true);
Christiaan Janssen's avatar
Christiaan Janssen committed
606
        }
Christiaan Janssen's avatar
Christiaan Janssen committed
607
608
    } else {
        d->m_clearButton->setEnabled(true);
609
610
    }
}