QmlProfiler: New event list with caching, load, save

Change-Id: I640a16649156a49f2d7e7006d6b2ea38fe218620
Reviewed-on: http://codereview.qt.nokia.com/3043
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Kai Koehne <kai.koehne@nokia.com>
This commit is contained in:
Christiaan Janssen
2011-07-26 13:56:14 +02:00
parent 8fbaa0d10a
commit d2911d70f3
17 changed files with 1417 additions and 489 deletions

View File

@@ -34,12 +34,17 @@
#include <qdeclarativecontext.h>
#include <qdeclarativeproperty.h>
#include <QtCore/QTimer>
using namespace QmlProfiler::Internal;
#define CACHE_ENABLED true
#define CACHE_UPDATEDELAY 10
#define CACHE_STEP 200
TimelineView::TimelineView(QDeclarativeItem *parent) :
QDeclarativeItem(parent), m_delegate(0), m_startTime(0), m_endTime(0), m_startX(0),
prevMin(0), prevMax(0), m_totalWidth(0)
QDeclarativeItem(parent), m_delegate(0), m_itemCount(0), m_startTime(0), m_endTime(0), m_startX(0), m_spacing(0),
prevMin(0), prevMax(0), m_eventList(0), m_totalWidth(0), m_lastCachedIndex(0), m_creatingCache(false), m_oldCacheSize(0)
{
}
@@ -50,46 +55,20 @@ void TimelineView::componentComplete()
void TimelineView::clearData()
{
m_rangeList.clear();
m_items.clear();
if (CACHE_ENABLED)
foreach (QDeclarativeItem *item, m_items.values())
item->setVisible(false);
else
foreach (QDeclarativeItem *item, m_items.values())
delete m_items.take(m_items.key(item));
m_startTime = 0;
m_endTime = 0;
m_startX = 0;
prevMin = 0;
prevMax = 0;
m_prevLimits.clear();
m_totalWidth = 0;
}
void TimelineView::setRanges(const QScriptValue &value)
{
//TODO clear old values (always?)
m_ranges = value;
//### below code not yet used anywhere
int length = m_ranges.property("length").toInt32();
for (int i = 0; i < length; ++i) {
int type = m_ranges.property(i).property("type").toNumber();
Q_ASSERT(type >= 0);
while (m_rangeList.count() <= type)
m_rangeList.append(ValueList());
m_rangeList[type] << m_ranges.property(i);
}
for (int i = 0; i < m_rangeList.count(); ++i)
m_prevLimits << PrevLimits(0, 0);
qreal startValue = m_ranges.property(0).property("start").toNumber();
m_starts.clear();
m_starts.reserve(length);
m_ends.clear();
m_ends.reserve(length);
for (int i = 0; i < length; ++i) {
m_starts.append(m_ranges.property(i).property("start").toNumber() - startValue);
m_ends.append(m_ranges.property(i).property("start").toNumber() + m_ranges.property(i).property("duration").toNumber() - startValue);
}
m_lastCachedIndex = 0;
}
void TimelineView::setStartX(qreal arg)
@@ -97,9 +76,6 @@ void TimelineView::setStartX(qreal arg)
if (arg == m_startX)
return;
if (!m_ranges.isArray())
return;
qreal window = m_endTime - m_startTime;
if (window == 0) //###
return;
@@ -118,51 +94,29 @@ void TimelineView::updateTimeline(bool updateStartX)
if (!m_delegate)
return;
if (!m_ranges.isArray())
if (!m_eventList)
return;
int length = m_ranges.property("length").toInt32();
qreal startValue = m_ranges.property(0).property("start").toNumber();
qreal endValue = m_ranges.property(length-1).property("start").toNumber() + m_ranges.property(length-1).property("duration").toNumber();
qreal totalRange = endValue - startValue;
qreal totalRange = m_eventList->lastTimeMark() - m_eventList->firstTimeMark();
qreal window = m_endTime - m_startTime;
if (window == 0) //###
return;
qreal spacing = width() / window;
qreal newSpacing = width() / window;
bool spacingChanged = (newSpacing != m_spacing);
m_spacing = newSpacing;
qreal oldtw = m_totalWidth;
m_totalWidth = totalRange * spacing;
m_totalWidth = totalRange * m_spacing;
// Find region samples
int minsample = 0;
int maxsample = 0;
for (int i = 0; i < length; ++i) {
if (m_ends.at(i) >= m_startTime)
break;
minsample = i;
}
for (int i = minsample + 1; i < length; ++i) {
maxsample = i;
if (m_starts.at(i) > m_endTime)
break;
}
//### overkill (if we can expose whether or not data is nested)
for (int i = maxsample + 1; i < length; ++i) {
if (m_starts.at(i) < m_endTime)
maxsample = i;
}
//qDebug() << maxsample - minsample;
int minsample = m_eventList->findFirstIndex(m_startTime + m_eventList->firstTimeMark());
int maxsample = m_eventList->findLastIndex(m_endTime + m_eventList->firstTimeMark());
if (updateStartX) {
qreal oldStartX = m_startX;
m_startX = qRound(m_startTime * spacing);
m_startX = qRound(m_startTime * m_spacing);
if (m_startX != oldStartX) {
emit startXChanged(m_startX);
}
@@ -172,69 +126,181 @@ void TimelineView::updateTimeline(bool updateStartX)
if (m_totalWidth != oldtw)
emit totalWidthChanged(m_totalWidth);
//clear items no longer in view
while (prevMin < minsample) {
delete m_items.take(prevMin);
++prevMin;
}
while (prevMax > maxsample) {
delete m_items.take(prevMax);
--prevMax;
}
// Show items
int z = 0;
for (int i = maxsample; i >= minsample; --i) {
QDeclarativeItem *item = 0;
item = m_items.value(i);
bool creating = false;
if (!item) {
QDeclarativeContext *ctxt = new QDeclarativeContext(qmlContext(this));
item = qobject_cast<QDeclarativeItem*>(m_delegate->beginCreate(ctxt));
m_items.insert(i, item);
creating = true;
// the next loops have to be modified with the new implementation of the cache
int type = m_ranges.property(i).property("type").toNumber();
ctxt->setParent(item); //### QDeclarative_setParent_noEvent(ctxt, item); instead?
ctxt->setContextProperty("duration", qMax(qRound(m_ranges.property(i).property("duration").toNumber()/qreal(1000)),1));
ctxt->setContextProperty("fileName", m_ranges.property(i).property("fileName").toString());
ctxt->setContextProperty("line", m_ranges.property(i).property("line").toNumber());
ctxt->setContextProperty("index", i);
ctxt->setContextProperty("nestingLevel", m_ranges.property(i).property("nestingLevel").toNumber());
ctxt->setContextProperty("nestingDepth", m_ranges.property(i).property("nestingDepth").toNumber());
QString label;
QVariantList list = m_ranges.property(i).property("label").toVariant().value<QVariantList>();
for (int i = 0; i < list.size(); ++i) {
if (i > 0)
label += QLatin1Char('\n');
QString sub = list.at(i).toString();
//### only do rewrite for bindings...
if (type == 3) {
//### don't construct in loop
QRegExp rewrite("\\(function \\$(\\w+)\\(\\) \\{ return (.+) \\}\\)");
bool match = rewrite.exactMatch(sub);
if (match)
sub = rewrite.cap(1) + ": " + rewrite.cap(2);
// hide items that are not visible any more
if (maxsample < prevMin || minsample > prevMax) {
for (int i = prevMin; i <= prevMax; ++i)
if (m_items.contains(i)) {
if (CACHE_ENABLED)
m_items.value(i)->setVisible(false);
else
delete m_items.take(i);
}
} else {
if (minsample > prevMin && minsample <= prevMax)
for (int i = prevMin; i < minsample; ++i)
if (m_items.contains(i)) {
if (CACHE_ENABLED)
m_items.value(i)->setVisible(false);
else
delete m_items.take(i);
}
label += sub;
}
ctxt->setContextProperty("label", label);
ctxt->setContextProperty("type", type);
item->setParentItem(this);
if (maxsample >= prevMin && maxsample < prevMax)
for (int i = maxsample + 1; i <= prevMax; ++i)
if (m_items.contains(i)) {
if (CACHE_ENABLED)
m_items.value(i)->setVisible(false);
else
delete m_items.take(i);
}
}
// Update visible items
for (int i = minsample; i <= maxsample; ++i) {
if (!m_items.contains(i)) {
createItem(i);
m_items.value(i)->setVisible(true);
}
if (item) {
item->setX(m_starts.at(i)*spacing);
qreal width = (m_ends.at(i)-m_starts.at(i)) * spacing;
item->setWidth(width > 1 ? width : 1);
item->setZValue(++z);
else
if (spacingChanged || !m_items.value(i)->isVisible()) {
m_items.value(i)->setVisible(true);
updateItemPosition(i);
}
if (creating)
m_delegate->completeCreate();
}
prevMin = minsample;
prevMax = maxsample;
}
void TimelineView::createItem(int itemIndex)
{
QDeclarativeContext *ctxt = new QDeclarativeContext(qmlContext(this));
QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(m_delegate->beginCreate(ctxt));
m_items.insert(itemIndex, item);
ctxt->setParent(item); //### QDeclarative_setParent_noEvent(ctxt, item); instead?
ctxt->setContextProperty("index", itemIndex);
ctxt->setContextProperty("type", m_eventList->getType(itemIndex));
ctxt->setContextProperty("nestingLevel", m_eventList->getNestingLevel(itemIndex));
ctxt->setContextProperty("nestingDepth", m_eventList->getNestingDepth(itemIndex));
updateItemPosition(itemIndex);
item->setVisible(false);
item->setParentItem(this);
m_delegate->completeCreate();
m_itemCount++;
}
void TimelineView::updateItemPosition(int itemIndex)
{
QDeclarativeItem *item = m_items.value(itemIndex);
if (item) {
qreal itemStartPos = (m_eventList->getStartTime(itemIndex) - m_eventList->firstTimeMark()) * m_spacing;
item->setX(itemStartPos);
qreal width = (m_eventList->getEndTime(itemIndex) - m_eventList->getStartTime(itemIndex)) * m_spacing;
item->setWidth(width > 1 ? width : 1);
}
}
void TimelineView::rebuildCache()
{
if (CACHE_ENABLED) {
m_lastCachedIndex = 0;
m_creatingCache = false;
m_oldCacheSize = m_items.count();
emit cachedProgressChanged();
QTimer::singleShot(CACHE_UPDATEDELAY, this, SLOT(purgeCache()));
} else {
m_creatingCache = true;
m_lastCachedIndex = m_eventList->count();
emit cacheReady();
}
}
qreal TimelineView::cachedProgress() const
{
qreal progress;
if (!m_creatingCache) {
if (m_oldCacheSize == 0)
progress = 0.5;
else
progress = (m_lastCachedIndex * 0.5) / m_oldCacheSize;
}
else
progress = 0.5 + (m_lastCachedIndex * 0.5) / m_eventList->count();
return progress;
}
void TimelineView::increaseCache()
{
int totalCount = m_eventList->count();
if (m_lastCachedIndex >= totalCount) {
emit cacheReady();
return;
}
for (int i = 0; i < CACHE_STEP; i++) {
createItem(m_lastCachedIndex);
m_lastCachedIndex++;
if (m_lastCachedIndex >= totalCount)
break;
}
emit cachedProgressChanged();
QTimer::singleShot(CACHE_UPDATEDELAY, this, SLOT(increaseCache()));
}
void TimelineView::purgeCache()
{
if (m_items.isEmpty()) {
m_creatingCache = true;
m_lastCachedIndex = 0;
QTimer::singleShot(CACHE_UPDATEDELAY, this, SLOT(increaseCache()));
return;
}
for (int i=0; i < CACHE_STEP; i++)
{
if (m_items.contains(m_lastCachedIndex))
delete m_items.take(m_lastCachedIndex);
m_lastCachedIndex++;
if (m_items.isEmpty())
break;
}
emit cachedProgressChanged();
QTimer::singleShot(CACHE_UPDATEDELAY, this, SLOT(purgeCache()));
}
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);
}