1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** 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, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** 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.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "timelineview.h"
#include <qdeclarativecontext.h>
#include <qdeclarativeproperty.h>
#include <QtCore/QTimer>
#include <QtGui/QPixmap>
#include <QtGui/QPainter>
#include <QtGui/QGraphicsSceneMouseEvent>
#include <math.h>
using namespace QmlProfiler::Internal;
const int DefaultRowHeight = 30;
TimelineView::TimelineView(QDeclarativeItem *parent) :
QDeclarativeItem(parent), m_startTime(0), m_endTime(0), m_spacing(0),
m_lastStartTime(0), m_lastEndTime(0), m_eventList(0)
{
clearData();
setFlag(QGraphicsItem::ItemHasNoContents, false);
setAcceptedMouseButtons(Qt::LeftButton);
setAcceptHoverEvents(true);
for (int i=0; i<QmlJsDebugClient::MaximumQmlEventType; i++)
m_rowsExpanded << false;
}
void TimelineView::componentComplete()
{
const QMetaObject *metaObject = this->metaObject();
int propertyCount = metaObject->propertyCount();
int requestPaintMethod = metaObject->indexOfMethod("requestPaint()");
for (int ii = TimelineView::staticMetaObject.propertyCount(); ii < propertyCount; ++ii) {
QMetaProperty p = metaObject->property(ii);
if (p.hasNotifySignal())
QMetaObject::connect(this, p.notifySignalIndex(), this, requestPaintMethod, 0, 0);
}
QDeclarativeItem::componentComplete();
}
void TimelineView::requestPaint()
{
update();
}
void TimelineView::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
{
qint64 windowDuration = m_endTime - m_startTime;
if (windowDuration <= 0)
return;
m_spacing = qreal(width()) / windowDuration;
m_rowWidths.clear();
// The "1+" is because the reference screenshot features an empty row per type, in order to leave space for the title
for (int i=0; i<QmlJsDebugClient::MaximumQmlEventType; i++) {
m_rowWidths << 1 + (m_rowsExpanded[i] ? m_eventList->uniqueEventsOfType(i) : m_eventList->maxNestingForType(i));
}
// event rows
m_rowStarts.clear();
int pos = 0;
for (int i=0; i<QmlJsDebugClient::MaximumQmlEventType; i++) {
m_rowStarts << pos;
pos += DefaultRowHeight * m_rowWidths[i];
}
p->setPen(Qt::transparent);
// speedup: don't draw overlapping events, just skip them
m_rowLastX.clear();
for (int i=0; i<QmlJsDebugClient::MaximumQmlEventType; i++)
for (int j=0; j<m_rowWidths[i]; j++)
m_rowLastX << -m_startTime * m_spacing;
int firstIndex = m_eventList->findFirstIndex(m_startTime);
int lastIndex = m_eventList->findLastIndex(m_endTime);
drawItemsToPainter(p, firstIndex, lastIndex);
drawSelectionBoxes(p);
m_lastStartTime = m_startTime;
m_lastEndTime = m_endTime;
}
QColor TimelineView::colorForItem(int itemIndex)
{
int ndx = m_eventList->getEventId(itemIndex);
return QColor::fromHsl((ndx*25)%360, 76, 166);
}
void TimelineView::drawItemsToPainter(QPainter *p, int fromIndex, int toIndex)
{
int x, y, width, height, rowNumber, eventType;
for (int i = fromIndex; i <= toIndex; i++) {
x = (m_eventList->getStartTime(i) - m_startTime) * m_spacing;
eventType = m_eventList->getType(i);
if (m_rowsExpanded[eventType])
y = m_rowStarts[eventType] + DefaultRowHeight*(m_eventList->eventPosInType(i) + 1);
else
y = m_rowStarts[eventType] + DefaultRowHeight*m_eventList->getNestingLevel(i);
width = m_eventList->getDuration(i)*m_spacing;
if (width<1)
width = 1;
rowNumber = y/DefaultRowHeight;
if (m_rowLastX[rowNumber] > x+width)
continue;
m_rowLastX[rowNumber] = x+width;
// special: animations
if (eventType == 0 && m_eventList->getAnimationCount(i) >= 0) {
double scale = m_eventList->getMaximumAnimationCount() - m_eventList->getMinimumAnimationCount();
double fraction;
if (scale > 1)
fraction = (double)(m_eventList->getAnimationCount(i) - m_eventList->getMinimumAnimationCount()) / scale;
else
fraction = 1.0;
height = DefaultRowHeight * (fraction * 0.85 + 0.15);
y += DefaultRowHeight - height;
double fpsFraction = m_eventList->getFramerate(i) / 60.0;
if (fpsFraction > 1.0)
fpsFraction = 1.0;
p->setBrush(QColor::fromHsl((fpsFraction*96)+10, 76, 166));
p->drawRect(x, y, width, height);
} else {
// normal events
p->setBrush(colorForItem(i));
p->drawRect(x, y, width, DefaultRowHeight);
}
}
}
void TimelineView::drawSelectionBoxes(QPainter *p)
{
if (m_selectedItem == -1)
return;
int fromIndex = m_eventList->findFirstIndex(m_startTime);
int toIndex = m_eventList->findLastIndex(m_endTime);
int id = m_eventList->getEventId(m_selectedItem);
p->setBrush(Qt::transparent);
QColor selectionColor = Qt::blue;
if (m_selectionLocked)
selectionColor = QColor(96,0,255);
QPen strongPen(selectionColor, 3);
QPen lightPen(QBrush(selectionColor.lighter(130)), 2);
lightPen.setJoinStyle(Qt::MiterJoin);
p->setPen(lightPen);
int x, y, width, eventType;
p->setPen(lightPen);
QRect selectedItemRect(0,0,0,0);
for (int i = fromIndex; i <= toIndex; i++) {
if (m_eventList->getEventId(i) != id)
continue;
x = (m_eventList->getStartTime(i) - m_startTime) * m_spacing;
eventType = m_eventList->getType(i);
if (m_rowsExpanded[eventType])
y = m_rowStarts[eventType] + DefaultRowHeight*(m_eventList->eventPosInType(i) + 1);
else
y = m_rowStarts[eventType] + DefaultRowHeight*m_eventList->getNestingLevel(i);
width = m_eventList->getDuration(i)*m_spacing;
if (width<1)
width = 1;
if (i == m_selectedItem)
selectedItemRect = QRect(x, y-1, width, DefaultRowHeight+1);
else
p->drawRect(x,y,width,DefaultRowHeight);
}
// draw the selected item rectangle the last, so that it's overlayed
if (selectedItemRect.width() != 0) {
p->setPen(strongPen);
p->drawRect(selectedItemRect);
}
}
void TimelineView::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
// special case: if there is a drag area below me, don't accept the
// events unless I'm actually clicking inside an item
if (m_currentSelection.eventIndex == -1 &&
event->pos().x()+x() >= m_startDragArea &&
event->pos().x()+x() <= m_endDragArea)
event->setAccepted(false);
}
void TimelineView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(event);
manageClicked();
}
void TimelineView::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
event->setAccepted(false);
}
void TimelineView::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
manageHovered(event->pos().x(), event->pos().y());
if (m_currentSelection.eventIndex == -1)
event->setAccepted(false);
}
void TimelineView::manageClicked()
{
if (m_currentSelection.eventIndex != -1) {
if (m_currentSelection.eventIndex == m_selectedItem)
setSelectionLocked(!m_selectionLocked);
else
setSelectionLocked(true);
emit itemPressed(m_currentSelection.eventIndex);
} else {
// setSelectionLocked(false);
}
setSelectedItem(m_currentSelection.eventIndex);
}
void TimelineView::manageHovered(int x, int y)
{
if (m_endTime - m_startTime <=0 || m_lastEndTime - m_lastStartTime <= 0)
return;
qint64 time = x * (m_endTime - m_startTime) / width() + m_startTime;
int row = y / DefaultRowHeight;
// already covered? nothing to do
if (m_currentSelection.eventIndex != -1 && time >= m_currentSelection.startTime && time <= m_currentSelection.endTime && row == m_currentSelection.row) {
return;
}
// find if there's items in the time range
int eventFrom = m_eventList->findFirstIndex(time);
int eventTo = m_eventList->findLastIndex(time);
if (eventTo < eventFrom) {
m_currentSelection.eventIndex = -1;
return;
}
// find if we are in the right column
int itemRow, eventType;
for (int i=eventTo; i>=eventFrom; --i) {
if (ceil(m_eventList->getEndTime(i)*m_spacing) < floor(time*m_spacing))
continue;
eventType = m_eventList->getType(i);
if (m_rowsExpanded[eventType])
itemRow = m_rowStarts[eventType]/DefaultRowHeight + m_eventList->eventPosInType(i) + 1;
else
itemRow = m_rowStarts[eventType]/DefaultRowHeight + m_eventList->getNestingLevel(i);
if (itemRow == row) {
// match
m_currentSelection.eventIndex = i;
m_currentSelection.startTime = m_eventList->getStartTime(i);
m_currentSelection.endTime = m_eventList->getEndTime(i);
m_currentSelection.row = row;
if (!m_selectionLocked)
setSelectedItem(i);
return;
}
}
m_currentSelection.eventIndex = -1;
return;
}
void TimelineView::clearData()
{
m_startTime = 0;
m_endTime = 0;
m_lastStartTime = 0;
m_lastEndTime = 0;
m_currentSelection.startTime = -1;
m_currentSelection.endTime = -1;
m_currentSelection.row = -1;
m_currentSelection.eventIndex = -1;
m_selectedItem = -1;
m_selectionLocked = true;
}
qint64 TimelineView::getDuration(int index) const
{
Q_ASSERT(m_eventList);
return m_eventList->getEndTime(index) - m_eventList->getStartTime(index);
}
QString TimelineView::getFilename(int index) const
{
Q_ASSERT(m_eventList);
return m_eventList->getFilename(index);
}
int TimelineView::getLine(int index) const
{
Q_ASSERT(m_eventList);
return m_eventList->getLine(index);
}
QString TimelineView::getDetails(int index) const
{
Q_ASSERT(m_eventList);
return m_eventList->getDetails(index);
}
void TimelineView::setRowExpanded(int rowIndex, bool expanded)
{
m_rowsExpanded[rowIndex] = expanded;
update();
}
void TimelineView::selectNext()
{
if (m_eventList->count() == 0)
return;
// select next in view or after
int newIndex = m_selectedItem+1;
if (newIndex >= m_eventList->count())
newIndex = 0;
if (m_eventList->getEndTime(newIndex) < m_startTime)
newIndex = m_eventList->findFirstIndexNoParents(m_startTime);
setSelectedItem(newIndex);
}
void TimelineView::selectPrev()
{
if (m_eventList->count() == 0)
return;
// select last in view or before
int newIndex = m_selectedItem-1;
if (newIndex < 0)
newIndex = m_eventList->count()-1;
if (m_eventList->getStartTime(newIndex) > m_endTime)
newIndex = m_eventList->findLastIndex(m_endTime);
setSelectedItem(newIndex);
}
int TimelineView::nextItemFromId(int eventId) const
{
int ndx = -1;
if (m_selectedItem == -1)
ndx = m_eventList->findFirstIndexNoParents(m_startTime);
else
ndx = m_selectedItem + 1;
if (ndx >= m_eventList->count())
ndx = 0;
int startIndex = ndx;
do {
if (m_eventList->getEventId(ndx) == eventId)
return ndx;
ndx = (ndx + 1) % m_eventList->count();
} while (ndx != startIndex);
return -1;
}
int TimelineView::prevItemFromId(int eventId) const
{
int ndx = -1;
if (m_selectedItem == -1)
ndx = m_eventList->findFirstIndexNoParents(m_startTime);
else
ndx = m_selectedItem - 1;
if (ndx < 0)
ndx = m_eventList->count() - 1;
int startIndex = ndx;
do {
if (m_eventList->getEventId(ndx) == eventId)
return ndx;
if (--ndx < 0)
ndx = m_eventList->count()-1;
} while (ndx != startIndex);
return -1;
}
void TimelineView::selectNextFromId(int eventId)
{
int eventIndex = nextItemFromId(eventId);
if (eventIndex != -1)
setSelectedItem(eventIndex);
}
void TimelineView::selectPrevFromId(int eventId)
{
int eventIndex = prevItemFromId(eventId);
if (eventIndex != -1)
setSelectedItem(eventIndex);
}
|