timelinerenderer.cpp 15.5 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
** 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
Eike Ziller's avatar
Eike Ziller committed
13 14
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
Kai Koehne's avatar
Kai Koehne committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24 25 26
**
** 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
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
Kai Koehne's avatar
Kai Koehne committed
30

Christiaan Janssen's avatar
Christiaan Janssen committed
31
#include "timelinerenderer.h"
32
#include "timelinerenderpass.h"
33
#include "qmlprofilernotesmodel.h"
34 35 36 37
#include "timelineitemsrenderpass.h"
#include "qmlprofilerbindingloopsrenderpass.h"
#include "timelineselectionrenderpass.h"
#include "timelinenotesrenderpass.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
38

39
#include <QElapsedTimer>
40 41
#include <QQmlContext>
#include <QQmlProperty>
42 43 44
#include <QTimer>
#include <QPixmap>
#include <QGraphicsSceneMouseEvent>
Christiaan Janssen's avatar
Christiaan Janssen committed
45
#include <QVarLengthArray>
46 47
#include <QSGTransformNode>
#include <QSGSimpleRectNode>
48 49

#include <math.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
50

51
namespace Timeline {
Kai Koehne's avatar
Kai Koehne committed
52

53 54 55 56
TimelineRenderer::TimelineRenderer(QQuickItem *parent) :
    QQuickItem(parent), m_model(0), m_zoomer(0), m_notes(0),
    m_selectedItem(-1), m_selectionLocked(true), m_modelDirty(false),
    m_rowHeightsDirty(false), m_rowCountsDirty(false), m_lastState(0)
Christiaan Janssen's avatar
Christiaan Janssen committed
57
{
58
    setFlag(QQuickItem::ItemHasContents);
59
    resetCurrentSelection();
60 61
    setAcceptedMouseButtons(Qt::LeftButton);
    setAcceptHoverEvents(true);
Christiaan Janssen's avatar
Christiaan Janssen committed
62 63
}

64
void TimelineRenderer::setModel(TimelineModel *model)
Christiaan Janssen's avatar
Christiaan Janssen committed
65
{
66 67 68 69
    if (m_model == model)
        return;

    if (m_model) {
70 71 72 73 74 75 76
        disconnect(m_model, SIGNAL(expandedChanged()), this, SLOT(update()));
        disconnect(m_model, SIGNAL(hiddenChanged()), this, SLOT(update()));
        disconnect(m_model, SIGNAL(expandedRowHeightChanged(int,int)),
                   this, SLOT(setRowHeightsDirty()));
        disconnect(m_model, SIGNAL(emptyChanged()), this, SLOT(setModelDirty()));
        disconnect(m_model, SIGNAL(expandedRowCountChanged()), this, SLOT(setRowCountsDirty()));
        disconnect(m_model, SIGNAL(collapsedRowCountChanged()), this, SLOT(setRowCountsDirty()));
77
    }
78 79 80

    m_model = model;
    if (m_model) {
81 82 83 84 85 86 87 88
        connect(m_model, SIGNAL(expandedChanged()), this, SLOT(update()));
        connect(m_model, SIGNAL(hiddenChanged()), this, SLOT(update()));
        connect(m_model, SIGNAL(expandedRowHeightChanged(int,int)),
                this, SLOT(setRowHeightsDirty()));
        connect(m_model, SIGNAL(emptyChanged()), this, SLOT(setModelDirty()));
        connect(m_model, SIGNAL(expandedRowCountChanged()), this, SLOT(setRowCountsDirty()));
        connect(m_model, SIGNAL(collapsedRowCountChanged()), this, SLOT(setRowCountsDirty()));
        m_renderPasses = model->supportedRenderPasses();
89
    }
90

91 92 93
    setModelDirty();
    setRowHeightsDirty();
    setRowCountsDirty();
94
    emit modelChanged(m_model);
Christiaan Janssen's avatar
Christiaan Janssen committed
95 96
}

97
void TimelineRenderer::setZoomer(TimelineZoomControl *zoomer)
98 99 100
{
    if (zoomer != m_zoomer) {
        if (m_zoomer != 0)
101
            disconnect(m_zoomer, SIGNAL(windowChanged(qint64,qint64)), this, SLOT(update()));
102 103
        m_zoomer = zoomer;
        if (m_zoomer != 0)
104
            connect(m_zoomer, SIGNAL(windowChanged(qint64,qint64)), this, SLOT(update()));
105 106 107 108 109
        emit zoomerChanged(zoomer);
        update();
    }
}

110
void TimelineRenderer::setNotes(TimelineNotesModel *notes)
Christiaan Janssen's avatar
Christiaan Janssen committed
111
{
112 113
    if (m_notes == notes)
        return;
Christiaan Janssen's avatar
Christiaan Janssen committed
114

115
    if (m_notes)
116
        disconnect(m_notes, &TimelineNotesModel::changed,
117
                   this, &TimelineRenderer::setNotesDirty);
118 119 120

    m_notes = notes;
    if (m_notes)
121
        connect(m_notes, &TimelineNotesModel::changed,
122
                this, &TimelineRenderer::setNotesDirty);
123 124

    emit notesChanged(m_notes);
125
    update();
Christiaan Janssen's avatar
Christiaan Janssen committed
126 127
}

128
bool TimelineRenderer::modelDirty() const
129
{
130
    return m_modelDirty;
131 132
}

133
bool TimelineRenderer::notesDirty() const
134
{
135 136 137 138 139 140
    return m_notesDirty;
}

bool TimelineRenderer::rowHeightsDirty() const
{
    return m_rowHeightsDirty;
141 142
}

143 144 145 146 147 148 149 150
void TimelineRenderer::resetCurrentSelection()
{
    m_currentSelection.startTime = -1;
    m_currentSelection.endTime = -1;
    m_currentSelection.row = -1;
    m_currentSelection.eventIndex = -1;
}

151
TimelineRenderState *TimelineRenderer::findRenderState()
Christiaan Janssen's avatar
Christiaan Janssen committed
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
    int newLevel = 0;
    int newOffset = 0;
    int level;
    int offset;

    qint64 newStart = m_zoomer->traceStart();
    qint64 newEnd = m_zoomer->traceEnd();
    qint64 start;
    qint64 end;
    do {
        level = newLevel;
        offset = newOffset;
        start = newStart;
        end = newEnd;

        newLevel = level + 1;
        qint64 range = m_zoomer->traceDuration() >> newLevel;
        newOffset = (m_zoomer->windowStart() - m_zoomer->traceStart() + range / 2) / range;
        newStart = m_zoomer->traceStart() + newOffset * range - range / 2;
        newEnd = newStart + range;
    } while (newStart < m_zoomer->windowStart() && newEnd > m_zoomer->windowEnd());


    if (m_renderStates.length() <= level)
        m_renderStates.resize(level + 1);
    if (m_renderStates[level].length() <= offset)
        m_renderStates[level].resize(offset + 1);
    TimelineRenderState *state = m_renderStates[level][offset];
    if (state == 0) {
        state = new TimelineRenderState(start, end, 1.0 / static_cast<qreal>(SafeFloatMax),
                                        m_renderPasses.size());
        m_renderStates[level][offset] = state;
185
    }
186
    return state;
187
}
Christiaan Janssen's avatar
Christiaan Janssen committed
188

189 190
QSGNode *TimelineRenderer::updatePaintNode(QSGNode *node,
                                           UpdatePaintNodeData *updatePaintNodeData)
191
{
192
    Q_UNUSED(updatePaintNodeData)
193

194 195 196 197 198 199
    if (!m_model || m_model->hidden() || m_model->isEmpty() || m_zoomer->windowDuration() <= 0) {
        delete node;
        return 0;
    } else if (node == 0) {
        node = new QSGTransformNode;
    }
200

201
    qreal spacing = width() / m_zoomer->windowDuration();
202

203 204 205 206 207 208 209
    if (m_modelDirty || m_rowCountsDirty) {
        node->removeAllChildNodes();
        foreach (QVector<TimelineRenderState *> stateVector, m_renderStates)
            qDeleteAll(stateVector);
        m_renderStates.clear();
        m_lastState = 0;
    }
210

211
    TimelineRenderState *state = findRenderState();
Christiaan Janssen's avatar
Christiaan Janssen committed
212

213 214
    int lastIndex = m_model->lastIndex(m_zoomer->windowEnd());
    int firstIndex = m_model->firstIndex(m_zoomer->windowStart());
215

216 217 218 219
    for (int i = 0; i < m_renderPasses.length(); ++i)
        state->setPassState(i, m_renderPasses[i]->update(this, state, state->passState(i),
                                                         firstIndex, lastIndex + 1,
                                                         state != m_lastState, spacing));
220

221 222 223 224 225 226 227 228 229 230
    if (state->isEmpty()) { // new state
        for (int pass = 0; pass < m_renderPasses.length(); ++pass) {
            const TimelineRenderPass::State *passState = state->passState(pass);
            if (!passState)
                continue;
            if (passState->expandedOverlay)
                state->expandedOverlayRoot()->appendChildNode(passState->expandedOverlay);
            if (passState->collapsedOverlay)
                state->collapsedOverlayRoot()->appendChildNode(passState->collapsedOverlay);
        }
231

232 233 234 235 236 237 238 239 240 241
        int row = 0;
        for (int i = 0; i < m_model->expandedRowCount(); ++i) {
            QSGTransformNode *rowNode = new QSGTransformNode;
            for (int pass = 0; pass < m_renderPasses.length(); ++pass) {
                const TimelineRenderPass::State *passState = state->passState(pass);
                if (passState && passState->expandedRows.length() > row) {
                    QSGNode *rowChildNode = passState->expandedRows[row];
                    if (rowChildNode)
                        rowNode->appendChildNode(rowChildNode);
                }
242
            }
243 244
            state->expandedRowRoot()->appendChildNode(rowNode);
            ++row;
245 246
        }

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
        for (int row = 0; row < m_model->collapsedRowCount(); ++row) {
            QSGTransformNode *rowNode = new QSGTransformNode;
            QMatrix4x4 matrix;
            matrix.translate(0, row * TimelineModel::defaultRowHeight(), 0);
            rowNode->setMatrix(matrix);
            for (int pass = 0; pass < m_renderPasses.length(); ++pass) {
                const TimelineRenderPass::State *passState = state->passState(pass);
                if (passState && passState->collapsedRows.length() > row) {
                    QSGNode *rowChildNode = passState->collapsedRows[row];
                    if (rowChildNode)
                        rowNode->appendChildNode(rowChildNode);
                }
            }
            state->collapsedRowRoot()->appendChildNode(rowNode);
        }
262
    }
Christiaan Janssen's avatar
Christiaan Janssen committed
263

264 265 266 267 268 269 270 271 272 273 274 275 276
    if (m_rowHeightsDirty || state != m_lastState) {
        int row = 0;
        qreal offset = 0;
        for (QSGNode *rowNode = state->expandedRowRoot()->firstChild(); rowNode != 0;
             rowNode = rowNode->nextSibling()) {
            qreal rowHeight = m_model->expandedRowHeight(row++);
            QMatrix4x4 matrix;
            matrix.translate(0, offset, 0);
            matrix.scale(1, rowHeight / TimelineModel::defaultRowHeight(), 1);
            offset += rowHeight;
            static_cast<QSGTransformNode *>(rowNode)->setMatrix(matrix);
        }
    }
277

278 279 280 281 282
    m_modelDirty = false;
    m_notesDirty = false;
    m_rowCountsDirty = false;
    m_rowHeightsDirty = false;
    m_lastState = state;
283

284 285 286
    QSGNode *rowNode = m_model->expanded() ? state->expandedRowRoot() : state->collapsedRowRoot();
    QSGNode *overlayNode = m_model->expanded() ? state->expandedOverlayRoot() :
                                                 state->collapsedOverlayRoot();
287

288 289 290
    QMatrix4x4 matrix;
    matrix.translate((state->start() - m_zoomer->windowStart()) * spacing, 0, 0);
    matrix.scale(spacing / state->scale(), 1, 1);
291

292 293 294 295 296 297 298
    QSGTransformNode *transform = static_cast<QSGTransformNode *>(node);
    transform->setMatrix(matrix);

    if (node->firstChild() != rowNode || node->lastChild() != overlayNode) {
        node->removeAllChildNodes();
        node->appendChildNode(rowNode);
        node->appendChildNode(overlayNode);
299
    }
300
    return node;
301 302
}

303
void TimelineRenderer::mousePressEvent(QMouseEvent *event)
304
{
305
    Q_UNUSED(event);
306 307
}

308 309
int TimelineRenderer::rowFromPosition(int y)
{
310 311
    if (!m_model->expanded())
        return y / TimelineModel::defaultRowHeight();
312

313 314 315 316
    int ret = 0;
    for (int row = 0; row < m_model->expandedRowCount(); ++row) {
        y -= m_model->expandedRowHeight(row);
        if (y <= 0) return ret;
317
        ++ret;
Christiaan Janssen's avatar
Christiaan Janssen committed
318
    }
319

320
    return ret;
321 322
}

323
void TimelineRenderer::mouseReleaseEvent(QMouseEvent *event)
324
{
325
    Q_UNUSED(event);
326
    if (!m_model->isEmpty())
327
        manageClicked();
328 329
}

330
void TimelineRenderer::mouseMoveEvent(QMouseEvent *event)
331
{
332 333
    event->setAccepted(false);
}
334

335

336
void TimelineRenderer::hoverMoveEvent(QHoverEvent *event)
337 338 339 340 341
{
    Q_UNUSED(event);
    manageHovered(event->pos().x(), event->pos().y());
    if (m_currentSelection.eventIndex == -1)
        event->setAccepted(false);
342 343
}

Christiaan Janssen's avatar
Christiaan Janssen committed
344
void TimelineRenderer::manageClicked()
345
{
346
    if (m_currentSelection.eventIndex != -1) {
347
        if (m_currentSelection.eventIndex == m_selectedItem)
348 349 350
            setSelectionLocked(!m_selectionLocked);
        else
            setSelectionLocked(true);
351 352 353

        // itemPressed() will trigger an update of the events and JavaScript views. Make sure the
        // correct event is already selected when that happens, to prevent confusion.
354 355
        setSelectedItem(m_currentSelection.eventIndex);
        emit itemPressed(m_currentSelection.eventIndex);
356
    } else {
Christiaan Janssen's avatar
Christiaan Janssen committed
357
        setSelectionLocked(false);
358 359
        setSelectedItem(-1);
        emit itemPressed(-1);
360
    }
361
}
362

363
void TimelineRenderer::manageHovered(int mouseX, int mouseY)
364
{
365
    qint64 duration = m_zoomer->windowDuration();
366
    if (duration <= 0)
367
        return;
368

369
    // Make the "selected" area 3 pixels wide by adding/subtracting 1 to catch very narrow events.
370 371
    qint64 startTime = (mouseX - 1) * duration / width() + m_zoomer->windowStart();
    qint64 endTime = (mouseX + 1) * duration / width() + m_zoomer->windowStart();
372
    qint64 exactTime = (startTime + endTime) / 2;
373
    int row = rowFromPosition(mouseY);
374

375
    // already covered? Only recheck selectionLocked and make sure m_selectedItem is correct.
Christiaan Janssen's avatar
Christiaan Janssen committed
376
    if (m_currentSelection.eventIndex != -1 &&
377 378
            exactTime >= m_currentSelection.startTime &&
            exactTime < m_currentSelection.endTime &&
Christiaan Janssen's avatar
Christiaan Janssen committed
379
            row == m_currentSelection.row) {
380 381
        if (!m_selectionLocked)
            setSelectedItem(m_currentSelection.eventIndex);
382 383
        return;
    }
384

385
    // find if there's items in the time range
386 387
    int eventFrom = m_model->firstIndex(startTime);
    int eventTo = m_model->lastIndex(endTime);
388 389 390

    m_currentSelection.eventIndex = -1;
    if (eventFrom == -1 || eventTo < eventFrom || eventTo >= m_model->count())
391 392
        return;

393
    // find if we are in the right column
394
    qint64 bestOffset = std::numeric_limits<qint64>::max();
395
    for (int i=eventTo; i>=eventFrom; --i) {
396
        if ( m_model->row(i) == row) {
397 398
            // There can be small events that don't reach the cursor position after large events
            // that do but are in a different row.
399
            qint64 itemEnd = m_model->endTime(i);
400 401 402
            if (itemEnd < startTime)
                continue;

403 404 405 406 407 408 409 410 411 412 413
            qint64 itemStart = m_model->startTime(i);

            qint64 offset = qAbs(itemEnd - exactTime) + qAbs(itemStart - exactTime);
            if (offset < bestOffset) {
                // match
                m_currentSelection.eventIndex = i;
                m_currentSelection.startTime = itemStart;
                m_currentSelection.endTime = itemEnd;
                m_currentSelection.row = row;
                bestOffset = offset;
            }
414
        }
415
    }
416 417
    if (!m_selectionLocked && m_currentSelection.eventIndex != -1)
        setSelectedItem(m_currentSelection.eventIndex);
418 419
}

Christiaan Janssen's avatar
Christiaan Janssen committed
420
void TimelineRenderer::clearData()
421
{
422 423 424
    resetCurrentSelection();
    setSelectedItem(-1);
    setSelectionLocked(true);
425 426
}

427
void TimelineRenderer::selectNextFromSelectionId(int selectionId)
428
{
429 430
    setSelectedItem(m_model->nextItemBySelectionId(selectionId, m_zoomer->rangeStart(),
                                                   m_selectedItem));
431 432
}

433
void TimelineRenderer::selectPrevFromSelectionId(int selectionId)
434
{
435 436
    setSelectedItem(m_model->prevItemBySelectionId(selectionId, m_zoomer->rangeStart(),
                                                   m_selectedItem));
437
}
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

void TimelineRenderer::setModelDirty()
{
    m_modelDirty = true;
    update();
}

void TimelineRenderer::setRowHeightsDirty()
{
    m_rowHeightsDirty = true;
    update();
}

void TimelineRenderer::setNotesDirty()
{
    m_notesDirty = true;
    update();
}

void TimelineRenderer::setRowCountsDirty()
{
    m_rowCountsDirty = true;
}

462
} // namespace Timeline