qmlprofilertool.cpp 24.5 KB
Newer Older
Kai Koehne's avatar
Kai Koehne committed
1
2
3
4
5
6
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
Tobias Hunger's avatar
Tobias Hunger committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
Kai Koehne's avatar
Kai Koehne committed
8
9
10
11
**
**
** GNU Lesser General Public License Usage
**
Thomas Murach's avatar
Thomas Murach committed
12
13
14
15
16
17
** 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.
Kai Koehne's avatar
Kai Koehne committed
18
19
**
** In addition, as a special exception, Nokia gives you certain additional
Thomas Murach's avatar
Thomas Murach committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
Kai Koehne's avatar
Kai Koehne committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
Thomas Murach's avatar
Thomas Murach committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
Kai Koehne's avatar
Kai Koehne committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
Kai Koehne's avatar
Kai Koehne committed
30
31
32
**
**************************************************************************/

Christiaan Janssen's avatar
Christiaan Janssen committed
33
34
35
#include "qmlprofilertool.h"
#include "qmlprofilerengine.h"
#include "qmlprofilerplugin.h"
36
37
#include "qmlprofilerconstants.h"
#include "qmlprofilerattachdialog.h"
38
#include "qmlprofilereventview.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
39
40

#include "tracewindow.h"
41
42
#include "timelineview.h"

43
#include <qmljsdebugclient/qmlprofilereventlist.h>
44
#include <qmljsdebugclient/qdeclarativedebugclient.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
45
46
47

#include <analyzerbase/analyzermanager.h>
#include <analyzerbase/analyzerconstants.h>
48
#include <analyzerbase/analyzerruncontrol.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
49
50
51
52
53
54

#include "canvas/qdeclarativecanvas_p.h"
#include "canvas/qdeclarativecontext2d_p.h"
#include "canvas/qdeclarativetiledcanvas_p.h"

#include <qmlprojectmanager/qmlprojectrunconfiguration.h>
55
#include <utils/fancymainwindow.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
56
#include <utils/fileinprojectfinder.h>
57
#include <utils/qtcassert.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
58
59
60
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/project.h>
61
#include <projectexplorer/target.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
62
63

#include <texteditor/itexteditor.h>
64
#include <coreplugin/coreconstants.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
65
#include <coreplugin/editormanager/editormanager.h>
66
#include <coreplugin/icore.h>
67
#include <coreplugin/messagemanager.h>
68
#include <coreplugin/helpmanager.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
69

70
#include <qt4projectmanager/qt4buildconfiguration.h>
71
72
#include <qt4projectmanager/qt-s60/s60deployconfiguration.h>

Christiaan Janssen's avatar
Christiaan Janssen committed
73
74
#include <QtCore/QFile>

75
#include <QtGui/QApplication>
Christiaan Janssen's avatar
Christiaan Janssen committed
76
77
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
78
#include <QtGui/QTabWidget>
79
#include <QtGui/QToolButton>
80
#include <QtGui/QMessageBox>
81
#include <QtGui/QDockWidget>
82
83
#include <QtGui/QFileDialog>
#include <QtGui/QMenu>
84

Christiaan Janssen's avatar
Christiaan Janssen committed
85
using namespace Analyzer;
Kai Koehne's avatar
Kai Koehne committed
86
using namespace QmlProfiler::Internal;
87
using namespace QmlJsDebugClient;
88
using namespace ProjectExplorer;
Christiaan Janssen's avatar
Christiaan Janssen committed
89
90
91
92
93
94
95
96
97
98

class QmlProfilerTool::QmlProfilerToolPrivate
{
public:
    QmlProfilerToolPrivate(QmlProfilerTool *qq) : q(qq) {}
    ~QmlProfilerToolPrivate() {}

    QmlProfilerTool *q;

    QDeclarativeDebugConnection *m_client;
Kai Koehne's avatar
Kai Koehne committed
99
100
    QTimer m_connectionTimer;
    int m_connectionAttempts;
Christiaan Janssen's avatar
Christiaan Janssen committed
101
    TraceWindow *m_traceWindow;
102
103
104
    QmlProfilerEventsView *m_eventsView;
    QmlProfilerEventsView *m_calleeView;
    QmlProfilerEventsView *m_callerView;
Christiaan Janssen's avatar
Christiaan Janssen committed
105
    QmlProfilerEventsView *m_v8profilerView;
106
    Project *m_project;
107
    Utils::FileInProjectFinder m_projectFinder;
108
    RunConfiguration *m_runConfiguration;
109
    bool m_isAttached;
110
    QToolButton *m_recordButton;
111
    QToolButton *m_clearButton;
112
    bool m_recordingEnabled;
113
    bool m_appIsRunning;
114
115
116
117
118
119
120
121
122

    enum ConnectMode {
        TcpConnection, OstConnection
    };

    ConnectMode m_connectMode;
    QString m_tcpHost;
    quint64 m_tcpPort;
    QString m_ostDevice;
123
    QString m_sysroot;
Christiaan Janssen's avatar
Christiaan Janssen committed
124
125
};

126
QmlProfilerTool::QmlProfilerTool(QObject *parent)
Christiaan Janssen's avatar
Christiaan Janssen committed
127
128
    : IAnalyzerTool(parent), d(new QmlProfilerToolPrivate(this))
{
129
    setObjectName("QmlProfilerTool");
hjk's avatar
hjk committed
130
131
132
133
134
135
136
    d->m_client = 0;
    d->m_connectionAttempts = 0;
    d->m_traceWindow = 0;
    d->m_project = 0;
    d->m_runConfiguration = 0;
    d->m_isAttached = false;
    d->m_recordingEnabled = true;
137
    d->m_appIsRunning = false;
hjk's avatar
hjk committed
138
139
140

    d->m_connectionTimer.setInterval(200);
    connect(&d->m_connectionTimer, SIGNAL(timeout()), SLOT(tryToConnect()));
141
142
143
144
145
146
147

    qmlRegisterType<Canvas>("Monitor", 1, 0, "Canvas");
    qmlRegisterType<TiledCanvas>("Monitor", 1, 0, "TiledCanvas");
    qmlRegisterType<Context2D>();
    qmlRegisterType<CanvasImage>();
    qmlRegisterType<CanvasGradient>();
    qmlRegisterType<TimelineView>("Monitor", 1, 0,"TimelineView");
Christiaan Janssen's avatar
Christiaan Janssen committed
148
149
150
151
}

QmlProfilerTool::~QmlProfilerTool()
{
152
    delete d->m_client;
Christiaan Janssen's avatar
Christiaan Janssen committed
153
154
155
    delete d;
}

156
QByteArray QmlProfilerTool::id() const
Christiaan Janssen's avatar
Christiaan Janssen committed
157
{
158
    return "QmlProfiler";
Christiaan Janssen's avatar
Christiaan Janssen committed
159
160
161
162
}

QString QmlProfilerTool::displayName() const
{
163
    return tr("QML Profiler");
Christiaan Janssen's avatar
Christiaan Janssen committed
164
165
}

166
167
168
169
170
171
QString QmlProfilerTool::description() const
{
    return tr("The QML Profiler can be used to find performance bottlenecks in "
              "applications using QML.");
}

172
IAnalyzerTool::ToolMode QmlProfilerTool::toolMode() const
Christiaan Janssen's avatar
Christiaan Janssen committed
173
{
174
    return AnyMode;
Christiaan Janssen's avatar
Christiaan Janssen committed
175
176
}

177
178
void QmlProfilerTool::showContextMenu(const QPoint &position)
{
179
180
    QmlProfilerEventsView *senderView = qobject_cast<QmlProfilerEventsView *>(sender());

181
182
183
    QMenu menu;
    QAction *loadAction = menu.addAction(tr("Load QML Trace"));
    QAction *saveAction = menu.addAction(tr("Save QML Trace"));
184
185
186
187
188
189
190
    QAction *copyRowAction;
    QAction *copyTableAction;
    if (senderView) {
        if (senderView->selectedItem().isValid())
            copyRowAction = menu.addAction(tr("Copy Row"));
        copyTableAction = menu.addAction(tr("Copy Table"));
    }
191
192
193
194
195
196

    QAction *selectedAction = menu.exec(position);
    if (selectedAction == loadAction)
        showLoadDialog();
    if (selectedAction == saveAction)
        showSaveDialog();
197
198
199
200
    if (selectedAction == copyRowAction)
        senderView->copyRowToClipboard();
    if (selectedAction == copyTableAction)
        senderView->copyTableToClipboard();
201
202
}

203
IAnalyzerEngine *QmlProfilerTool::createEngine(const AnalyzerStartParameters &sp,
204
    RunConfiguration *runConfiguration)
Christiaan Janssen's avatar
Christiaan Janssen committed
205
{
206
    QmlProfilerEngine *engine = new QmlProfilerEngine(this, sp, runConfiguration);
207

208
209
    d->m_connectMode = QmlProfilerToolPrivate::TcpConnection;

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
    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);
        if (Qt4ProjectManager::Qt4BuildConfiguration *qt4Config
                = qobject_cast<Qt4ProjectManager::Qt4BuildConfiguration*>(
                    runConfiguration->target()->activeBuildConfiguration())) {
            if (qt4Config->qtVersion()->isValid() && qt4Config->qtVersion()->qtVersion() < minimumVersion) {
                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;
            }
        }

        // Check whether we should use OST instead of TCP
        if (Qt4ProjectManager::S60DeployConfiguration *deployConfig
                = qobject_cast<Qt4ProjectManager::S60DeployConfiguration*>(
                    runConfiguration->target()->activeDeployConfiguration())) {
            if (deployConfig->communicationChannel()
                    == Qt4ProjectManager::S60DeployConfiguration::CommunicationCodaSerialConnection) {
                d->m_connectMode = QmlProfilerToolPrivate::OstConnection;
                d->m_ostDevice = deployConfig->serialPortName();
            }
236
237
238
        }
    }

239
    // FIXME: Check that there's something sensible in sp.connParams
240
241
242
243
    if (d->m_connectMode == QmlProfilerToolPrivate::TcpConnection) {
        d->m_tcpHost = sp.connParams.host;
        d->m_tcpPort = sp.connParams.port;
    }
244

245
    d->m_runConfiguration = runConfiguration;
246
247
248
249
250
251

    if (runConfiguration)
        d->m_project = runConfiguration->target()->project();
    else
        d->m_project = ProjectExplorerPlugin::instance()->currentProject();

252
253
254
255
256
257
    if (d->m_project) {
        d->m_projectFinder.setProjectDirectory(d->m_project->projectDirectory());
        updateProjectFileList();
        connect(d->m_project, SIGNAL(fileListChanged()), this, SLOT(updateProjectFileList()));
    }

258
259
    d->m_projectFinder.setSysroot(sp.sysroot);

260
    connect(engine, SIGNAL(processRunning(int)), this, SLOT(connectClient(int)));
261
    connect(engine, SIGNAL(finished()), this, SLOT(disconnectClient()));
262
    connect(engine, SIGNAL(finished()), this, SLOT(correctTimer()));
Christiaan Janssen's avatar
Christiaan Janssen committed
263
    connect(engine, SIGNAL(stopRecording()), this, SLOT(stopRecording()));
264
    connect(d->m_traceWindow, SIGNAL(viewUpdated()), engine, SLOT(dataReceived()));
265
    connect(this, SIGNAL(connectionFailed()), engine, SLOT(finishProcess()));
266
    connect(this, SIGNAL(fetchingData(bool)), engine, SLOT(setFetchingData(bool)));
267
268
269
    connect(engine, SIGNAL(starting(const Analyzer::IAnalyzerEngine*)), this, SLOT(setAppIsRunning()));
    connect(engine, SIGNAL(finished()), this, SLOT(setAppIsStopped()));
    connect(this, SIGNAL(cancelRun()), engine, SLOT(finishProcess()));
270
    emit fetchingData(d->m_recordButton->isChecked());
Christiaan Janssen's avatar
Christiaan Janssen committed
271
272
273
274

    return engine;
}

275
276
277
278
279
280
281
QWidget *QmlProfilerTool::createWidgets()
{
    QTC_ASSERT(!d->m_traceWindow, return 0);

    //
    // DockWidgets
    //
282

283
    Utils::FancyMainWindow *mw = AnalyzerManager::mainWindow();
284

285
    d->m_traceWindow = new TraceWindow(mw);
Christiaan Janssen's avatar
Christiaan Janssen committed
286
    d->m_traceWindow->reset(d->m_client);
287

288
    connect(d->m_traceWindow, SIGNAL(gotoSourceLocation(QString,int)),this, SLOT(gotoSourceLocation(QString,int)));
289
    connect(d->m_traceWindow, SIGNAL(timeChanged(qreal)), this, SLOT(updateTimer(qreal)));
290
    connect(d->m_traceWindow, SIGNAL(contextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
291
    connect(d->m_traceWindow->getEventList(), SIGNAL(error(QString)), this, SLOT(showErrorDialog(QString)));
292

293
    d->m_eventsView = new QmlProfilerEventsView(mw, d->m_traceWindow->getEventList());
294
    d->m_eventsView->setViewType(QmlProfilerEventsView::EventsView);
295

296
    connect(d->m_eventsView, SIGNAL(gotoSourceLocation(QString,int)),
297
            this, SLOT(gotoSourceLocation(QString,int)));
298
    connect(d->m_eventsView, SIGNAL(contextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
299

300
    d->m_calleeView = new QmlProfilerEventsView(mw, d->m_traceWindow->getEventList());
301
302
    d->m_calleeView->setViewType(QmlProfilerEventsView::CalleesView);
    connect(d->m_calleeView, SIGNAL(gotoSourceLocation(QString,int)),
303
            this, SLOT(gotoSourceLocation(QString,int)));
304
    connect(d->m_calleeView, SIGNAL(contextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
305

306
    d->m_callerView = new QmlProfilerEventsView(mw, d->m_traceWindow->getEventList());
307
308
    d->m_callerView->setViewType(QmlProfilerEventsView::CallersView);
    connect(d->m_callerView, SIGNAL(gotoSourceLocation(QString,int)),
309
            this, SLOT(gotoSourceLocation(QString,int)));
310
    connect(d->m_callerView, SIGNAL(contextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
311

Christiaan Janssen's avatar
Christiaan Janssen committed
312
313
314
315
316
    d->m_v8profilerView = new QmlProfilerEventsView(mw, d->m_traceWindow->getEventList());
    d->m_v8profilerView->setViewType(QmlProfilerEventsView::V8ProfileView);
    connect(d->m_v8profilerView, SIGNAL(gotoSourceLocation(QString,int)), this, SLOT(gotoSourceLocation(QString,int)));
    connect(d->m_v8profilerView, SIGNAL(contextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));

317
318
    QDockWidget *eventsDock = AnalyzerManager::createDockWidget
            (this, tr("Events"), d->m_eventsView, Qt::BottomDockWidgetArea);
319
    QDockWidget *timelineDock = AnalyzerManager::createDockWidget
320
            (this, tr("Timeline"), d->m_traceWindow, Qt::BottomDockWidgetArea);
321
    QDockWidget *calleeDock = AnalyzerManager::createDockWidget
322
            (this, tr("Callees"), d->m_calleeView, Qt::BottomDockWidgetArea);
323
    QDockWidget *callerDock = AnalyzerManager::createDockWidget
324
            (this, tr("Callers"), d->m_callerView, Qt::BottomDockWidgetArea);
Christiaan Janssen's avatar
Christiaan Janssen committed
325
326
    QDockWidget *v8profilerDock = AnalyzerManager::createDockWidget
            (this, tr("JavaScript"), d->m_v8profilerView, Qt::BottomDockWidgetArea);
327

328
329
330
331
    eventsDock->show();
    timelineDock->show();
    calleeDock->show();
    callerDock->show();
Christiaan Janssen's avatar
Christiaan Janssen committed
332
    v8profilerDock->show();
333

334
335
    mw->splitDockWidget(mw->toolBarDockWidget(), eventsDock, Qt::Vertical);
    mw->tabifyDockWidget(eventsDock, timelineDock);
336
337
    mw->tabifyDockWidget(timelineDock, calleeDock);
    mw->tabifyDockWidget(calleeDock, callerDock);
Christiaan Janssen's avatar
Christiaan Janssen committed
338
    mw->tabifyDockWidget(callerDock, v8profilerDock);
339

340
341
342
    //
    // Toolbar
    //
Christiaan Janssen's avatar
Christiaan Janssen committed
343
344
345
346
347
348
349
    QWidget *toolbarWidget = new QWidget;
    toolbarWidget->setObjectName(QLatin1String("QmlProfilerToolBarWidget"));

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

350
    d->m_recordButton = new QToolButton(toolbarWidget);
351
    // icon and tooltip set in setRecording(), called later
352
353
354
355
356
357
    d->m_recordButton->setCheckable(true);

    connect(d->m_recordButton,SIGNAL(toggled(bool)), this, SLOT(setRecording(bool)));
    d->m_recordButton->setChecked(true);
    layout->addWidget(d->m_recordButton);

358
359
    d->m_clearButton = new QToolButton(toolbarWidget);
    d->m_clearButton->setIcon(QIcon(QLatin1String(":/qmlprofiler/clean_pane_small.png")));
Christiaan Janssen's avatar
Christiaan Janssen committed
360
    d->m_clearButton->setToolTip(tr("Discard data"));
361
362
363
    connect(d->m_clearButton,SIGNAL(clicked()), this, SLOT(clearDisplay()));
    layout->addWidget(d->m_clearButton);

364
    QLabel *timeLabel = new QLabel(tr("Elapsed:      0 s"));
Christiaan Janssen's avatar
Christiaan Janssen committed
365
366
367
    QPalette palette = timeLabel->palette();
    palette.setColor(QPalette::WindowText, Qt::white);
    timeLabel->setPalette(palette);
368
    timeLabel->setIndent(10);
369
    connect(d->m_traceWindow, SIGNAL(viewUpdated()), this, SLOT(correctTimer()));
hjk's avatar
hjk committed
370
    connect(this, SIGNAL(setTimeLabel(QString)), timeLabel, SLOT(setText(QString)));
371
    correctTimer();
Christiaan Janssen's avatar
Christiaan Janssen committed
372
    layout->addWidget(timeLabel);
373

Christiaan Janssen's avatar
Christiaan Janssen committed
374
375
376
    toolbarWidget->setLayout(layout);

    return toolbarWidget;
Christiaan Janssen's avatar
Christiaan Janssen committed
377
378
}

379
void QmlProfilerTool::connectClient(int port)
Kai Koehne's avatar
Kai Koehne committed
380
{
381
382
    QTC_ASSERT(!d->m_client, return;)
    d->m_client = new QDeclarativeDebugConnection;
383
    d->m_traceWindow->reset(d->m_client);
384
385
    connect(d->m_client, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
            this, SLOT(connectionStateChanged()));
Kai Koehne's avatar
Kai Koehne committed
386
    d->m_connectionTimer.start();
387
    d->m_tcpPort = port;
Kai Koehne's avatar
Kai Koehne committed
388
389
390
}

void QmlProfilerTool::connectToClient()
Christiaan Janssen's avatar
Christiaan Janssen committed
391
{
392
393
    if (!d->m_client || d->m_client->state() != QAbstractSocket::UnconnectedState)
        return;
Christiaan Janssen's avatar
Christiaan Janssen committed
394

395
    if (d->m_connectMode == QmlProfilerToolPrivate::TcpConnection) {
396
        logStatus(QString("QML Profiler: Connecting to %1:%2 ...").arg(d->m_tcpHost, QString::number(d->m_tcpPort)));
397
398
        d->m_client->connectToHost(d->m_tcpHost, d->m_tcpPort);
    } else {
399
        logStatus(QString("QML Profiler: Connecting to %1 ...").arg(d->m_tcpHost));
400
401
        d->m_client->connectToOst(d->m_ostDevice);
    }
Christiaan Janssen's avatar
Christiaan Janssen committed
402
403
404
405
}

void QmlProfilerTool::disconnectClient()
{
406
407
    // this might be actually be called indirectly by QDDConnectionPrivate::readyRead(), therefore allow
    // method to complete before deleting object
408
409
410
411
    if (d->m_client) {
        d->m_client->deleteLater();
        d->m_client = 0;
    }
Christiaan Janssen's avatar
Christiaan Janssen committed
412
413
}

414
415
void QmlProfilerTool::startRecording()
{
416
    if (d->m_client && d->m_client->isConnected()) {
417
        clearDisplay();
418
        d->m_traceWindow->setRecording(true);
419
    }
420
421
422
    emit fetchingData(true);
}

Christiaan Janssen's avatar
Christiaan Janssen committed
423
424
void QmlProfilerTool::stopRecording()
{
425
    d->m_traceWindow->setRecording(false);
426
    emit fetchingData(false);
427
428
429
430

    // manage early stop
    if (d->m_client && !d->m_client->isConnected() && d->m_appIsRunning)
        emit cancelRun();
431
432
433
434
}

void QmlProfilerTool::setRecording(bool recording)
{
435
    d->m_recordingEnabled = recording;
436
437

    // update record button
Christiaan Janssen's avatar
Christiaan Janssen committed
438
    d->m_recordButton->setToolTip( d->m_recordingEnabled ? tr("Disable profiling") : tr("Enable profiling"));
439
440
441
    d->m_recordButton->setIcon(QIcon(d->m_recordingEnabled ? QLatin1String(":/qmlprofiler/recordOn.png") :
                                                             QLatin1String(":/qmlprofiler/recordOff.png")));

442
443
444
445
    if (recording)
        startRecording();
    else
        stopRecording();
Christiaan Janssen's avatar
Christiaan Janssen committed
446
447
}

448
449
450
451
452
453
454
455
456
457
void QmlProfilerTool::setAppIsRunning()
{
    d->m_appIsRunning = true;
}

void QmlProfilerTool::setAppIsStopped()
{
    d->m_appIsRunning = false;
}

Kai Koehne's avatar
Kai Koehne committed
458
void QmlProfilerTool::gotoSourceLocation(const QString &fileUrl, int lineNumber)
Christiaan Janssen's avatar
Christiaan Janssen committed
459
{
Kai Koehne's avatar
Kai Koehne committed
460
    if (lineNumber < 0 || fileUrl.isEmpty())
Christiaan Janssen's avatar
Christiaan Janssen committed
461
462
        return;

463
    const QString projectFileName = d->m_projectFinder.findFile(fileUrl);
Christiaan Janssen's avatar
Christiaan Janssen committed
464
465
466
467
468
469
470
471
472
473
474

    Core::EditorManager *editorManager = Core::EditorManager::instance();
    Core::IEditor *editor = editorManager->openEditor(projectFileName);
    TextEditor::ITextEditor *textEditor = qobject_cast<TextEditor::ITextEditor*>(editor);

    if (textEditor) {
        editorManager->addCurrentPositionToNavigationHistory();
        textEditor->gotoLine(lineNumber);
        textEditor->widget()->setFocus();
    }
}
Christiaan Janssen's avatar
Christiaan Janssen committed
475

476
void QmlProfilerTool::correctTimer() {
477
    if (d->m_traceWindow->getEventList()->count() == 0)
478
479
480
        updateTimer(0);
}

Christiaan Janssen's avatar
Christiaan Janssen committed
481
482
483
484
void QmlProfilerTool::updateTimer(qreal elapsedSeconds)
{
    QString timeString = QString::number(elapsedSeconds,'f',1);
    timeString = QString("      ").left(6-timeString.length()) + timeString;
485
    emit setTimeLabel(tr("Elapsed: %1 s").arg(timeString));
Christiaan Janssen's avatar
Christiaan Janssen committed
486
}
487
488
489
490

void QmlProfilerTool::updateProjectFileList()
{
    d->m_projectFinder.setProjectFiles(
491
                d->m_project->files(Project::ExcludeGeneratedFiles));
492
}
493

494
495
496
void QmlProfilerTool::clearDisplay()
{
    d->m_traceWindow->clearDisplay();
497
498
499
    d->m_eventsView->clear();
    d->m_calleeView->clear();
    d->m_callerView->clear();
Christiaan Janssen's avatar
Christiaan Janssen committed
500
    d->m_v8profilerView->clear();
501
502
}

503
static void startRemoteTool(IAnalyzerTool *tool, StartMode mode)
504
{
505
    Q_UNUSED(tool);
506
507
508

    QString host;
    quint16 port;
509
    QString sysroot;
510
511
512
513
514
515

    {
        QSettings *settings = Core::ICore::instance()->settings();

        host = settings->value(QLatin1String("AnalyzerQmlAttachDialog/host"), QLatin1String("localhost")).toString();
        port = settings->value(QLatin1String("AnalyzerQmlAttachDialog/port"), 3768).toInt();
516
        sysroot = settings->value(QLatin1String("AnalyzerQmlAttachDialog/sysroot")).toString();
517
518
519
520
521

        QmlProfilerAttachDialog dialog;

        dialog.setAddress(host);
        dialog.setPort(port);
522
        dialog.setSysroot(sysroot);
523
524
525
526
527
528

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

        host = dialog.address();
        port = dialog.port();
529
        sysroot = dialog.sysroot();
530
531
532

        settings->setValue(QLatin1String("AnalyzerQmlAttachDialog/host"), host);
        settings->setValue(QLatin1String("AnalyzerQmlAttachDialog/port"), port);
533
        settings->setValue(QLatin1String("AnalyzerQmlAttachDialog/sysroot"), sysroot);
534
    }
535

536
537
538
    AnalyzerStartParameters sp;
    sp.toolId = tool->id();
    sp.startMode = mode;
539
540
    sp.connParams.host = host;
    sp.connParams.port = port;
541
    sp.sysroot = sysroot;
542

543
544
    AnalyzerRunControl *rc = new AnalyzerRunControl(tool, sp, 0);
    QObject::connect(AnalyzerManager::stopAction(), SIGNAL(triggered()), rc, SLOT(stopIt()));
545

546
    ProjectExplorerPlugin::instance()->startRunControl(rc, tool->id());
547
}
548

Kai Koehne's avatar
Kai Koehne committed
549
550
551
552
void QmlProfilerTool::tryToConnect()
{
    ++d->m_connectionAttempts;

553
    if (d->m_client && d->m_client->isConnected()) {
Kai Koehne's avatar
Kai Koehne committed
554
555
556
557
558
        d->m_connectionTimer.stop();
        d->m_connectionAttempts = 0;
    } else if (d->m_connectionAttempts == 50) {
        d->m_connectionTimer.stop();
        d->m_connectionAttempts = 0;
559

560
561
562
563
564
565
566
567
568
569
570
571
572
573
        Core::ICore * const core = Core::ICore::instance();
        QMessageBox *infoBox = new QMessageBox(core->mainWindow());
        infoBox->setIcon(QMessageBox::Critical);
        infoBox->setWindowTitle(tr("Qt Creator"));
        infoBox->setText(tr("Could not connect to the in-process QML profiler.\n"
                            "Do you want to retry?"));
        infoBox->setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel | QMessageBox::Help);
        infoBox->setDefaultButton(QMessageBox::Retry);
        infoBox->setModal(true);

        connect(infoBox, SIGNAL(finished(int)),
                this, SLOT(retryMessageBoxFinished(int)));

        infoBox->show();
Kai Koehne's avatar
Kai Koehne committed
574
575
576
577
    } else {
        connectToClient();
    }
}
578
579
580
581
582
583
584
585
586

void QmlProfilerTool::connectionStateChanged()
{
    if (!d->m_client)
        return;
    switch (d->m_client->state()) {
    case QAbstractSocket::UnconnectedState:
    {
        if (QmlProfilerPlugin::debugOutput)
587
            qWarning("QML Profiler: disconnected");
588
589
590
591
592
593
        break;
    }
    case QAbstractSocket::HostLookupState:
        break;
    case QAbstractSocket::ConnectingState: {
        if (QmlProfilerPlugin::debugOutput)
594
            qWarning("QML Profiler: Connecting to debug server ...");
595
596
597
598
599
        break;
    }
    case QAbstractSocket::ConnectedState:
    {
        if (QmlProfilerPlugin::debugOutput)
600
            qWarning("QML Profiler: connected and running");
601
        updateRecordingState();
602
603
604
605
        break;
    }
    case QAbstractSocket::ClosingState:
        if (QmlProfilerPlugin::debugOutput)
606
            qWarning("QML Profiler: closing ...");
607
608
609
610
611
612
613
        break;
    case QAbstractSocket::BoundState:
    case QAbstractSocket::ListeningState:
        break;
    }
}

614
void QmlProfilerTool::updateRecordingState()
615
616
617
618
619
620
621
622
623
624
{
    if (d->m_client->isConnected()) {
        d->m_traceWindow->setRecording(d->m_recordingEnabled);
    } else {
        d->m_traceWindow->setRecording(false);
    }

    if (d->m_traceWindow->isRecording())
        clearDisplay();
}
625
626
627

void QmlProfilerTool::startTool(StartMode mode)
{
628
629
630
631
632
    using namespace ProjectExplorer;

    // Make sure mode is shown.
    AnalyzerManager::showMode();

633
634
635
636
637
638
639
640
    if (mode == StartLocal) {
        ProjectExplorerPlugin *pe = ProjectExplorerPlugin::instance();
        // ### not sure if we're supposed to check if the RunConFiguration isEnabled
        Project *pro = pe->startupProject();
        pe->runProject(pro, id());
    } else if (mode == StartRemote) {
        startRemoteTool(this, mode);
    }
641
}
642
643
644
645
646
647
648
649
650
651
652
653
654

void QmlProfilerTool::logStatus(const QString &msg)
{
    Core::MessageManager *messageManager = Core::MessageManager::instance();
    messageManager->printToOutputPane(msg, false);
}

void QmlProfilerTool::logError(const QString &msg)
{
    // TODO: Rather show errors in the application ouput
    Core::MessageManager *messageManager = Core::MessageManager::instance();
    messageManager->printToOutputPane(msg, true);
}
655
656
657
658

void QmlProfilerTool::showSaveDialog()
{
    Core::ICore *core = Core::ICore::instance();
659
    QString filename = QFileDialog::getSaveFileName(core->mainWindow(), tr("Save QML Trace"), QString(), tr("QML traces (*%1)").arg(TraceFileExtension));
660
    if (!filename.isEmpty()) {
661
662
        if (!filename.endsWith(QLatin1String(TraceFileExtension)))
            filename += QLatin1String(TraceFileExtension);
663
664
665
666
667
668
669
        d->m_traceWindow->getEventList()->save(filename);
    }
}

void QmlProfilerTool::showLoadDialog()
{
    Core::ICore *core = Core::ICore::instance();
670
    QString filename = QFileDialog::getOpenFileName(core->mainWindow(), tr("Load QML Trace"), QString(), tr("QML traces (*%1)").arg(TraceFileExtension));
671
672
673
674
675
676
677

    if (!filename.isEmpty()) {
        // delayed load (prevent graphical artifacts due to long load time)
        d->m_traceWindow->getEventList()->setFilename(filename);
        QTimer::singleShot(100, d->m_traceWindow->getEventList(), SLOT(load()));
    }
}
678
679
680
681
682
683
684
685
686
687
688
689
690

void QmlProfilerTool::showErrorDialog(const QString &error)
{
    Core::ICore *core = Core::ICore::instance();
    QMessageBox *errorDialog = new QMessageBox(core->mainWindow());
    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();
}
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716

void QmlProfilerTool::retryMessageBoxFinished(int result)
{
    switch (result) {
    case QMessageBox::Retry: {
        d->m_connectionAttempts = 0;
        d->m_connectionTimer.start();
        break;
    }
    case QMessageBox::Help: {
        Core::HelpManager *helpManager = Core::HelpManager::instance();
        helpManager->handleHelpRequest("qthelp://com.nokia.qtcreator/doc/creator-debugging-qml.html");
        // fall through
    }
    default: {
        if (d->m_client) {
            logStatus("QML Profiler: Failed to connect! " + d->m_client->errorString());
        } else {
            logStatus("QML Profiler: Failed to connect!");
        }

        emit connectionFailed();
        break;
    }
    }
}