qmlprofilertraceview.cpp 22.5 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Christiaan Janssen's avatar
Christiaan Janssen committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Christiaan Janssen's avatar
Christiaan Janssen committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Christiaan Janssen's avatar
Christiaan Janssen 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.
Christiaan Janssen's avatar
Christiaan Janssen 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
Christiaan Janssen's avatar
Christiaan Janssen committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Christiaan Janssen's avatar
Christiaan Janssen committed
29 30 31 32

#include "qmlprofilertraceview.h"
#include "qmlprofilertool.h"
#include "qmlprofilerstatemanager.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
33 34 35
#include "qmlprofilermodelmanager.h"
#include "qmlprofilertimelinemodelproxy.h"
#include "timelinemodelaggregator.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
36 37 38 39

// Needed for the load&save actions in the context menu
#include <analyzerbase/ianalyzertool.h>

Christiaan Janssen's avatar
Christiaan Janssen committed
40
// Communication with the other views (limit events to range)
Christiaan Janssen's avatar
Christiaan Janssen committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include "qmlprofilerviewmanager.h"

#include <utils/styledbar.h>

#include <QDeclarativeContext>
#include <QToolButton>
#include <QEvent>
#include <QVBoxLayout>
#include <QGraphicsObject>
#include <QScrollBar>
#include <QSlider>
#include <QMenu>

#include <math.h>

56
using namespace QmlDebug;
Christiaan Janssen's avatar
Christiaan Janssen committed
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

namespace QmlProfiler {
namespace Internal {

const int sliderTicks = 10000;
const qreal sliderExp = 3;


/////////////////////////////////////////////////////////
bool MouseWheelResizer::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::Wheel) {
        QWheelEvent *ev = static_cast<QWheelEvent *>(event);
        if (ev->modifiers() & Qt::ControlModifier) {
            emit mouseWheelMoved(ev->pos().x(), ev->pos().y(), ev->delta());
            return true;
        }
    }
    return QObject::eventFilter(obj, event);
}

/////////////////////////////////////////////////////////
void ZoomControl::setRange(qint64 startTime, qint64 endTime)
{
    if (m_startTime != startTime || m_endTime != endTime) {
        m_startTime = startTime;
        m_endTime = endTime;
        emit rangeChanged();
    }
}

/////////////////////////////////////////////////////////
ScrollableDeclarativeView::ScrollableDeclarativeView(QWidget *parent)
    : QDeclarativeView(parent)
{
}

ScrollableDeclarativeView::~ScrollableDeclarativeView()
{
}

void ScrollableDeclarativeView::scrollContentsBy(int dx, int dy)
{
    // special workaround to track the scrollbar
    if (rootObject()) {
        int scrollY = rootObject()->property("scrollY").toInt();
        rootObject()->setProperty("scrollY", QVariant(scrollY - dy));
    }
    QDeclarativeView::scrollContentsBy(dx,dy);
}

/////////////////////////////////////////////////////////
class QmlProfilerTraceView::QmlProfilerTraceViewPrivate
{
public:
    QmlProfilerTraceViewPrivate(QmlProfilerTraceView *qq) : q(qq) {}
    QmlProfilerTraceView *q;

    QmlProfilerStateManager *m_profilerState;
    Analyzer::IAnalyzerTool *m_profilerTool;
    QmlProfilerViewManager *m_viewContainer;

    QSize m_sizeHint;

    ScrollableDeclarativeView *m_mainView;
    QDeclarativeView *m_timebar;
    QDeclarativeView *m_overview;
Christiaan Janssen's avatar
Christiaan Janssen committed
124 125 126
    QmlProfilerModelManager *m_modelManager;
    TimelineModelAggregator *m_modelProxy;

Christiaan Janssen's avatar
Christiaan Janssen committed
127 128 129 130 131 132 133 134 135

    ZoomControl *m_zoomControl;

    QToolButton *m_buttonRange;
    QToolButton *m_buttonLock;
    QWidget *m_zoomToolbar;
    int m_currentZoomLevel;
};

Christiaan Janssen's avatar
Christiaan Janssen committed
136
QmlProfilerTraceView::QmlProfilerTraceView(QWidget *parent, Analyzer::IAnalyzerTool *profilerTool, QmlProfilerViewManager *container, QmlProfilerModelManager *modelManager, QmlProfilerStateManager *profilerState)
Christiaan Janssen's avatar
Christiaan Janssen committed
137 138
    : QWidget(parent), d(new QmlProfilerTraceViewPrivate(this))
{
139
    setObjectName(QLatin1String("QML Profiler"));
Christiaan Janssen's avatar
Christiaan Janssen committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

    d->m_zoomControl = new ZoomControl(this);
    connect(d->m_zoomControl, SIGNAL(rangeChanged()), this, SLOT(updateRange()));

    QVBoxLayout *groupLayout = new QVBoxLayout;
    groupLayout->setContentsMargins(0, 0, 0, 0);
    groupLayout->setSpacing(0);

    d->m_mainView = new ScrollableDeclarativeView(this);
    d->m_mainView->setResizeMode(QDeclarativeView::SizeViewToRootObject);
    d->m_mainView->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    d->m_mainView->setBackgroundBrush(QBrush(Qt::white));
    d->m_mainView->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    d->m_mainView->setFocus();

    MouseWheelResizer *resizer = new MouseWheelResizer(this);
    connect(resizer,SIGNAL(mouseWheelMoved(int,int,int)), this, SLOT(mouseWheelMoved(int,int,int)));
    d->m_mainView->viewport()->installEventFilter(resizer);

    QHBoxLayout *toolsLayout = new QHBoxLayout;

    d->m_timebar = new QDeclarativeView(this);
    d->m_timebar->setResizeMode(QDeclarativeView::SizeRootObjectToView);
    d->m_timebar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    d->m_timebar->setFixedHeight(24);

    d->m_overview = new QDeclarativeView(this);
    d->m_overview->setResizeMode(QDeclarativeView::SizeRootObjectToView);
    d->m_overview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    d->m_overview->setMaximumHeight(50);

    d->m_zoomToolbar = createZoomToolbar();
    d->m_zoomToolbar->move(0, d->m_timebar->height());
    d->m_zoomToolbar->setVisible(false);

    toolsLayout->addWidget(createToolbar());
    toolsLayout->addWidget(d->m_timebar);
    emit enableToolbar(false);

    groupLayout->addLayout(toolsLayout);
    groupLayout->addWidget(d->m_mainView);
    groupLayout->addWidget(d->m_overview);

    setLayout(groupLayout);

    d->m_profilerTool = profilerTool;
    d->m_viewContainer = container;
Christiaan Janssen's avatar
Christiaan Janssen committed
187 188 189 190
    d->m_modelManager = modelManager;
    d->m_modelProxy = new TimelineModelAggregator(this);
    d->m_modelProxy->setModelManager(modelManager);
    connect(d->m_modelManager, SIGNAL(stateChanged()),
Christiaan Janssen's avatar
Christiaan Janssen committed
191
            this, SLOT(profilerDataModelStateChanged()));
Christiaan Janssen's avatar
Christiaan Janssen committed
192 193 194 195
    d->m_mainView->rootContext()->setContextProperty(QLatin1String("qmlProfilerModelProxy"),
                                                     d->m_modelProxy);
    d->m_overview->rootContext()->setContextProperty(QLatin1String("qmlProfilerModelProxy"),
                                                     d->m_modelProxy);
Christiaan Janssen's avatar
Christiaan Janssen committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

    d->m_profilerState = profilerState;
    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()));

    // Minimum height: 5 rows of 20 pixels + scrollbar of 50 pixels + 20 pixels margin
    setMinimumHeight(170);
    d->m_currentZoomLevel = 0;
}

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

/////////////////////////////////////////////////////////
// Initialize widgets
void QmlProfilerTraceView::reset()
{
219 220 221
    d->m_mainView->rootContext()->setContextProperty(QLatin1String("zoomControl"), d->m_zoomControl);
    d->m_timebar->rootContext()->setContextProperty(QLatin1String("zoomControl"), d->m_zoomControl);
    d->m_overview->rootContext()->setContextProperty(QLatin1String("zoomControl"), d->m_zoomControl);
Christiaan Janssen's avatar
Christiaan Janssen committed
222

223 224
    d->m_timebar->setSource(QUrl(QLatin1String("qrc:/qmlprofiler/TimeDisplay.qml")));
    d->m_overview->setSource(QUrl(QLatin1String("qrc:/qmlprofiler/Overview.qml")));
Christiaan Janssen's avatar
Christiaan Janssen committed
225

226
    d->m_mainView->setSource(QUrl(QLatin1String("qrc:/qmlprofiler/MainView.qml")));
227 228 229 230 231 232 233 234 235 236 237 238
    QGraphicsObject *rootObject = d->m_mainView->rootObject();
    rootObject->setProperty("width", QVariant(width()));
    rootObject->setProperty("candidateHeight", QVariant(height() - d->m_timebar->height() - d->m_overview->height()));

    connect(rootObject, SIGNAL(updateCursorPosition()), this, SLOT(updateCursorPosition()));
    connect(rootObject, SIGNAL(updateRangeButton()), this, SLOT(updateRangeButton()));
    connect(rootObject, SIGNAL(updateLockButton()), this, SLOT(updateLockButton()));
    connect(this, SIGNAL(jumpToPrev()), rootObject, SLOT(prevEvent()));
    connect(this, SIGNAL(jumpToNext()), rootObject, SLOT(nextEvent()));
    connect(rootObject, SIGNAL(selectedEventChanged(int)), this, SIGNAL(selectedEventChanged(int)));
    connect(rootObject, SIGNAL(changeToolTip(QString)), this, SLOT(updateToolTip(QString)));
    connect(rootObject, SIGNAL(updateVerticalScroll(int)), this, SLOT(updateVerticalScroll(int)));
Christiaan Janssen's avatar
Christiaan Janssen committed
239 240 241 242 243
}

QWidget *QmlProfilerTraceView::createToolbar()
{
    Utils::StyledBar *bar = new Utils::StyledBar(this);
244
    bar->setStyleSheet(QLatin1String("background: #9B9B9B"));
Christiaan Janssen's avatar
Christiaan Janssen committed
245 246 247 248 249 250 251 252 253
    bar->setSingleRow(true);
    bar->setFixedWidth(150);
    bar->setFixedHeight(24);

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

    QToolButton *buttonPrev= new QToolButton;
254
    buttonPrev->setIcon(QIcon(QLatin1String(":/qmlprofiler/ico_prev.png")));
Christiaan Janssen's avatar
Christiaan Janssen committed
255 256 257 258 259
    buttonPrev->setToolTip(tr("Jump to previous event"));
    connect(buttonPrev, SIGNAL(clicked()), this, SIGNAL(jumpToPrev()));
    connect(this, SIGNAL(enableToolbar(bool)), buttonPrev, SLOT(setEnabled(bool)));

    QToolButton *buttonNext= new QToolButton;
260
    buttonNext->setIcon(QIcon(QLatin1String(":/qmlprofiler/ico_next.png")));
Christiaan Janssen's avatar
Christiaan Janssen committed
261 262 263 264 265
    buttonNext->setToolTip(tr("Jump to next event"));
    connect(buttonNext, SIGNAL(clicked()), this, SIGNAL(jumpToNext()));
    connect(this, SIGNAL(enableToolbar(bool)), buttonNext, SLOT(setEnabled(bool)));

    QToolButton *buttonZoomControls = new QToolButton;
266
    buttonZoomControls->setIcon(QIcon(QLatin1String(":/qmlprofiler/ico_zoom.png")));
Christiaan Janssen's avatar
Christiaan Janssen committed
267 268 269 270 271 272 273
    buttonZoomControls->setToolTip(tr("Show zoom slider"));
    buttonZoomControls->setCheckable(true);
    buttonZoomControls->setChecked(false);
    connect(buttonZoomControls, SIGNAL(toggled(bool)), d->m_zoomToolbar, SLOT(setVisible(bool)));
    connect(this, SIGNAL(enableToolbar(bool)), buttonZoomControls, SLOT(setEnabled(bool)));

    d->m_buttonRange = new QToolButton;
274
    d->m_buttonRange->setIcon(QIcon(QLatin1String(":/qmlprofiler/ico_rangeselection.png")));
Christiaan Janssen's avatar
Christiaan Janssen committed
275 276 277 278 279 280 281 282
    d->m_buttonRange->setToolTip(tr("Select range"));
    d->m_buttonRange->setCheckable(true);
    d->m_buttonRange->setChecked(false);
    connect(d->m_buttonRange, SIGNAL(clicked(bool)), this, SLOT(toggleRangeMode(bool)));
    connect(this, SIGNAL(enableToolbar(bool)), d->m_buttonRange, SLOT(setEnabled(bool)));
    connect(this, SIGNAL(rangeModeChanged(bool)), d->m_buttonRange, SLOT(setChecked(bool)));

    d->m_buttonLock = new QToolButton;
283
    d->m_buttonLock->setIcon(QIcon(QLatin1String(":/qmlprofiler/ico_selectionmode.png")));
Christiaan Janssen's avatar
Christiaan Janssen committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    d->m_buttonLock->setToolTip(tr("View event information on mouseover"));
    d->m_buttonLock->setCheckable(true);
    d->m_buttonLock->setChecked(false);
    connect(d->m_buttonLock, SIGNAL(clicked(bool)), this, SLOT(toggleLockMode(bool)));
    connect(this, SIGNAL(enableToolbar(bool)), d->m_buttonLock, SLOT(setEnabled(bool)));
    connect(this, SIGNAL(lockModeChanged(bool)), d->m_buttonLock, SLOT(setChecked(bool)));

    toolBarLayout->addWidget(buttonPrev);
    toolBarLayout->addWidget(buttonNext);
    toolBarLayout->addWidget(new Utils::StyledSeparator());
    toolBarLayout->addWidget(buttonZoomControls);
    toolBarLayout->addWidget(new Utils::StyledSeparator());
    toolBarLayout->addWidget(d->m_buttonRange);
    toolBarLayout->addWidget(d->m_buttonLock);

    return bar;
}


QWidget *QmlProfilerTraceView::createZoomToolbar()
{
    Utils::StyledBar *bar = new Utils::StyledBar(this);
306
    bar->setStyleSheet(QLatin1String("background: #9B9B9B"));
Christiaan Janssen's avatar
Christiaan Janssen committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    bar->setSingleRow(true);
    bar->setFixedWidth(150);
    bar->setFixedHeight(24);

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

    QSlider *zoomSlider = new QSlider(Qt::Horizontal);
    zoomSlider->setFocusPolicy(Qt::NoFocus);
    zoomSlider->setRange(1, sliderTicks);
    zoomSlider->setInvertedAppearance(true);
    zoomSlider->setPageStep(sliderTicks/100);
    connect(this, SIGNAL(enableToolbar(bool)), zoomSlider, SLOT(setEnabled(bool)));
    connect(zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(setZoomLevel(int)));
    connect(this, SIGNAL(zoomLevelChanged(int)), zoomSlider, SLOT(setValue(int)));
323
    zoomSlider->setStyleSheet(QLatin1String("\
Christiaan Janssen's avatar
Christiaan Janssen committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337
        QSlider:horizontal {\
            background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #444444, stop: 1 #5a5a5a);\
            border: 1px #313131;\
            height: 20px;\
            margin: 0px 0px 0px 0px;\
        }\
        QSlider::add-page:horizontal {\
            background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #5a5a5a, stop: 1 #444444);\
            border: 1px #313131;\
        }\
        QSlider::sub-page:horizontal {\
            background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #5a5a5a, stop: 1 #444444);\
            border: 1px #313131;\
        }\
338
        "));
Christiaan Janssen's avatar
Christiaan Janssen committed
339 340 341 342 343 344 345 346 347

    toolBarLayout->addWidget(zoomSlider);

    return bar;
}

/////////////////////////////////////////////////////////
bool QmlProfilerTraceView::hasValidSelection() const
{
348 349 350
    QGraphicsObject *rootObject = d->m_mainView->rootObject();
    if (rootObject)
        return rootObject->property("selectionRangeReady").toBool();
Christiaan Janssen's avatar
Christiaan Janssen committed
351 352 353 354 355
    return false;
}

qint64 QmlProfilerTraceView::selectionStart() const
{
356 357 358
    QGraphicsObject *rootObject = d->m_mainView->rootObject();
    if (rootObject)
        return rootObject->property("selectionRangeStart").toLongLong();
Christiaan Janssen's avatar
Christiaan Janssen committed
359 360 361 362 363
    return 0;
}

qint64 QmlProfilerTraceView::selectionEnd() const
{
364 365 366
    QGraphicsObject *rootObject = d->m_mainView->rootObject();
    if (rootObject)
        return rootObject->property("selectionRangeEnd").toLongLong();
Christiaan Janssen's avatar
Christiaan Janssen committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380
    return 0;
}

void QmlProfilerTraceView::clearDisplay()
{
    d->m_zoomControl->setRange(0,0);

    updateVerticalScroll(0);
    d->m_mainView->rootObject()->setProperty("scrollY", QVariant(0));

    QMetaObject::invokeMethod(d->m_mainView->rootObject(), "clearAll");
    QMetaObject::invokeMethod(d->m_overview->rootObject(), "clearDisplay");
}

Christiaan Janssen's avatar
Christiaan Janssen committed
381
void QmlProfilerTraceView::selectNextEventByHash(const QString &hash)
Christiaan Janssen's avatar
Christiaan Janssen committed
382
{
383
    QGraphicsObject *rootObject = d->m_mainView->rootObject();
Christiaan Janssen's avatar
Christiaan Janssen committed
384

385
    if (rootObject)
Christiaan Janssen's avatar
Christiaan Janssen committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399
        QMetaObject::invokeMethod(rootObject, "selectNextByHash",
                                  Q_ARG(QVariant,QVariant(hash)));
}

void QmlProfilerTraceView::selectNextEventByLocation(const QString &filename, const int line, const int column)
{
    int eventId = d->m_modelProxy->getEventIdForLocation(filename, line, column);

    if (eventId != -1) {
        QGraphicsObject *rootObject = d->m_mainView->rootObject();
        if (rootObject)
            QMetaObject::invokeMethod(rootObject, "selectNextById",
                                      Q_ARG(QVariant,QVariant(eventId)));
    }
Christiaan Janssen's avatar
Christiaan Janssen committed
400 401 402 403 404 405
}

/////////////////////////////////////////////////////////
// Goto source location
void QmlProfilerTraceView::updateCursorPosition()
{
406 407 408 409
    QGraphicsObject *rootObject = d->m_mainView->rootObject();
    emit gotoSourceLocation(rootObject->property("fileName").toString(),
                            rootObject->property("lineNumber").toInt(),
                            rootObject->property("columnNumber").toInt());
Christiaan Janssen's avatar
Christiaan Janssen committed
410 411 412 413 414 415
}

/////////////////////////////////////////////////////////
// Toolbar buttons
void QmlProfilerTraceView::toggleRangeMode(bool active)
{
416 417
    QGraphicsObject *rootObject = d->m_mainView->rootObject();
    bool rangeMode = rootObject->property("selectionRangeMode").toBool();
Christiaan Janssen's avatar
Christiaan Janssen committed
418 419
    if (active != rangeMode) {
        if (active)
420
            d->m_buttonRange->setIcon(QIcon(QLatin1String(":/qmlprofiler/ico_rangeselected.png")));
Christiaan Janssen's avatar
Christiaan Janssen committed
421
        else
422
            d->m_buttonRange->setIcon(QIcon(QLatin1String(":/qmlprofiler/ico_rangeselection.png")));
423
        rootObject->setProperty("selectionRangeMode", QVariant(active));
Christiaan Janssen's avatar
Christiaan Janssen committed
424 425 426 427 428 429 430
    }
}

void QmlProfilerTraceView::updateRangeButton()
{
    bool rangeMode = d->m_mainView->rootObject()->property("selectionRangeMode").toBool();
    if (rangeMode)
431
        d->m_buttonRange->setIcon(QIcon(QLatin1String(":/qmlprofiler/ico_rangeselected.png")));
Christiaan Janssen's avatar
Christiaan Janssen committed
432
    else
433
        d->m_buttonRange->setIcon(QIcon(QLatin1String(":/qmlprofiler/ico_rangeselection.png")));
Christiaan Janssen's avatar
Christiaan Janssen committed
434 435 436 437 438
    emit rangeModeChanged(rangeMode);
}

void QmlProfilerTraceView::toggleLockMode(bool active)
{
439 440
    QGraphicsObject *rootObject = d->m_mainView->rootObject();
    bool lockMode = !rootObject->property("selectionLocked").toBool();
Christiaan Janssen's avatar
Christiaan Janssen committed
441
    if (active != lockMode) {
442 443
        rootObject->setProperty("selectionLocked", QVariant(!active));
        rootObject->setProperty("selectedItem", QVariant(-1));
Christiaan Janssen's avatar
Christiaan Janssen committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
    }
}

void QmlProfilerTraceView::updateLockButton()
{
    bool lockMode = !d->m_mainView->rootObject()->property("selectionLocked").toBool();
    emit lockModeChanged(lockMode);
}

////////////////////////////////////////////////////////
// Zoom control
void QmlProfilerTraceView::setZoomLevel(int zoomLevel)
{
    if (d->m_currentZoomLevel != zoomLevel && d->m_mainView->rootObject()) {
        QVariant newFactor = pow(qreal(zoomLevel) / qreal(sliderTicks), sliderExp);
        d->m_currentZoomLevel = zoomLevel;
        QMetaObject::invokeMethod(d->m_mainView->rootObject(), "updateWindowLength", Q_ARG(QVariant, newFactor));
    }
}

void QmlProfilerTraceView::updateRange()
{
Christiaan Janssen's avatar
Christiaan Janssen committed
466
    if (!d->m_modelManager)
Christiaan Janssen's avatar
Christiaan Janssen committed
467 468 469 470
        return;
    qreal duration = d->m_zoomControl->endTime() - d->m_zoomControl->startTime();
    if (duration <= 0)
        return;
Christiaan Janssen's avatar
Christiaan Janssen committed
471
    if (d->m_modelManager->traceTime()->duration() <= 0)
Christiaan Janssen's avatar
Christiaan Janssen committed
472
        return;
Christiaan Janssen's avatar
Christiaan Janssen committed
473
    int newLevel = pow(duration / d->m_modelManager->traceTime()->duration(), 1/sliderExp) * sliderTicks;
Christiaan Janssen's avatar
Christiaan Janssen committed
474 475 476 477 478 479 480 481 482
    if (d->m_currentZoomLevel != newLevel) {
        d->m_currentZoomLevel = newLevel;
        emit zoomLevelChanged(newLevel);
    }
}

void QmlProfilerTraceView::mouseWheelMoved(int mouseX, int mouseY, int wheelDelta)
{
    Q_UNUSED(mouseY);
483 484 485
    QGraphicsObject *rootObject = d->m_mainView->rootObject();
    if (rootObject) {
        QMetaObject::invokeMethod(rootObject, "wheelZoom",
Christiaan Janssen's avatar
Christiaan Janssen committed
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
                                  Q_ARG(QVariant, QVariant(mouseX)),
                                  Q_ARG(QVariant, QVariant(wheelDelta)));
    }
}
////////////////////////////////////////////////////////
void QmlProfilerTraceView::updateToolTip(const QString &text)
{
    setToolTip(text);
}

void QmlProfilerTraceView::updateVerticalScroll(int newPosition)
{
    d->m_mainView->verticalScrollBar()->setValue(newPosition);
}

void QmlProfilerTraceView::resizeEvent(QResizeEvent *event)
{
503
    QWidget::resizeEvent(event);
504 505 506
    QGraphicsObject *rootObject = d->m_mainView->rootObject();
    if (rootObject) {
        rootObject->setProperty("width", QVariant(event->size().width()));
Christiaan Janssen's avatar
Christiaan Janssen committed
507
        int newHeight = event->size().height() - d->m_timebar->height() - d->m_overview->height();
508
        rootObject->setProperty("candidateHeight", QVariant(newHeight));
Christiaan Janssen's avatar
Christiaan Janssen committed
509
    }
510
    emit resized();
Christiaan Janssen's avatar
Christiaan Janssen committed
511 512 513 514 515 516 517 518 519 520 521
}

////////////////////////////////////////////////////////////////
// Context menu
void QmlProfilerTraceView::contextMenuEvent(QContextMenuEvent *ev)
{
    QMenu menu;
    QAction *viewAllAction = 0;

    QmlProfilerTool *profilerTool = qobject_cast<QmlProfilerTool *>(d->m_profilerTool);

522 523
    if (profilerTool)
        menu.addActions(profilerTool->profilerContextMenuActions());
Christiaan Janssen's avatar
Christiaan Janssen committed
524 525

    menu.addSeparator();
526 527

    QAction *getLocalStatsAction = menu.addAction(tr("Limit Events Pane to Current Range"));
Christiaan Janssen's avatar
Christiaan Janssen committed
528 529
    if (!d->m_viewContainer->hasValidSelection())
        getLocalStatsAction->setEnabled(false);
530 531

    QAction *getGlobalStatsAction = menu.addAction(tr("Reset Events Pane"));
Christiaan Janssen's avatar
Christiaan Janssen committed
532 533 534
    if (d->m_viewContainer->hasGlobalStats())
        getGlobalStatsAction->setEnabled(false);

Christiaan Janssen's avatar
Christiaan Janssen committed
535
    if (!d->m_modelProxy->isEmpty()) {
Christiaan Janssen's avatar
Christiaan Janssen committed
536 537 538 539
        menu.addSeparator();
        viewAllAction = menu.addAction(tr("Reset Zoom"));
    }

540
    QAction *selectedAction = menu.exec(ev->globalPos());
Christiaan Janssen's avatar
Christiaan Janssen committed
541 542 543 544

    if (selectedAction) {
        if (selectedAction == viewAllAction) {
            d->m_zoomControl->setRange(
Christiaan Janssen's avatar
Christiaan Janssen committed
545 546
                        d->m_modelManager->traceTime()->startTime(),
                        d->m_modelManager->traceTime()->endTime());
Christiaan Janssen's avatar
Christiaan Janssen committed
547 548 549 550 551 552 553
        }
        if (selectedAction == getLocalStatsAction) {
            d->m_viewContainer->getStatisticsInRange(
                        d->m_viewContainer->selectionStart(),
                        d->m_viewContainer->selectionEnd());
        }
        if (selectedAction == getGlobalStatsAction) {
Christiaan Janssen's avatar
Christiaan Janssen committed
554
            d->m_viewContainer->getStatisticsInRange(-1, -1);
Christiaan Janssen's avatar
Christiaan Janssen committed
555 556 557 558 559 560 561 562
        }
    }
}

/////////////////////////////////////////////////
// Tell QML the state of the profiler
void QmlProfilerTraceView::setRecording(bool recording)
{
563 564 565
    QGraphicsObject *rootObject = d->m_mainView->rootObject();
    if (rootObject)
        rootObject->setProperty("recordingEnabled", QVariant(recording));
Christiaan Janssen's avatar
Christiaan Janssen committed
566 567 568 569
}

void QmlProfilerTraceView::setAppKilled()
{
570 571 572
    QGraphicsObject *rootObject = d->m_mainView->rootObject();
    if (rootObject)
        rootObject->setProperty("appKilled",QVariant(true));
Christiaan Janssen's avatar
Christiaan Janssen committed
573 574 575 576 577
}
////////////////////////////////////////////////////////////////
// Profiler State
void QmlProfilerTraceView::profilerDataModelStateChanged()
{
Christiaan Janssen's avatar
Christiaan Janssen committed
578 579 580
    switch (d->m_modelManager->state()) {
        case QmlProfilerDataState::Empty:
            emit enableToolbar(false);
Christiaan Janssen's avatar
Christiaan Janssen committed
581
        break;
Christiaan Janssen's avatar
Christiaan Janssen committed
582 583 584 585
        case QmlProfilerDataState::AcquiringData: break;
        case QmlProfilerDataState::ProcessingData: break;
        case QmlProfilerDataState::Done:
            emit enableToolbar(true);
Christiaan Janssen's avatar
Christiaan Janssen committed
586 587 588 589 590 591 592 593 594 595
        break;
    default:
        break;
    }
}

void QmlProfilerTraceView::profilerStateChanged()
{
    switch (d->m_profilerState->currentState()) {
    case QmlProfilerStateManager::AppKilled : {
Christiaan Janssen's avatar
Christiaan Janssen committed
596
        if (d->m_modelManager->state() == QmlProfilerDataState::AcquiringData)
Christiaan Janssen's avatar
Christiaan Janssen committed
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
            setAppKilled();
        break;
    }
    default:
        // no special action needed for other states
        break;
    }
}

void QmlProfilerTraceView::clientRecordingChanged()
{
    // nothing yet
}

void QmlProfilerTraceView::serverRecordingChanged()
{
    setRecording(d->m_profilerState->serverRecording());
}

} // namespace Internal
} // namespace QmlProfiler