timelinemodelaggregator.cpp 7.33 KB
Newer Older
Christiaan Janssen's avatar
Christiaan Janssen committed
1
2
/****************************************************************************
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
Christiaan Janssen's avatar
Christiaan Janssen committed
4
5
6
7
8
9
10
11
12
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** 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.
Christiaan Janssen's avatar
Christiaan Janssen committed
15
16
17
**
** GNU Lesser General Public License Usage
** 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.
Christiaan Janssen's avatar
Christiaan Janssen committed
24
25
26
27
28
29
30
31
32
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "timelinemodelaggregator.h"

33
34
#include "qmlprofilerrangemodel.h"
#include "qmlprofileranimationsmodel.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
35
#include "qmlprofilerplugin.h"
36
#include "qmlprofilernotesmodel.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#include <QStringList>
#include <QVariant>

namespace QmlProfiler {
namespace Internal {


class TimelineModelAggregator::TimelineModelAggregatorPrivate {
public:
    TimelineModelAggregatorPrivate(TimelineModelAggregator *qq):q(qq) {}
    ~TimelineModelAggregatorPrivate() {}

    TimelineModelAggregator *q;

52
    QList <TimelineModel *> modelList;
53
    TimelineNotesModel *notesModel;
Christiaan Janssen's avatar
Christiaan Janssen committed
54
55
};

56
TimelineModelAggregator::TimelineModelAggregator(TimelineNotesModel *notes, QObject *parent)
Christiaan Janssen's avatar
Christiaan Janssen committed
57
58
    : QObject(parent), d(new TimelineModelAggregatorPrivate(this))
{
59
    d->notesModel = notes;
60
    connect(this,SIGNAL(modelsChanged()),this,SIGNAL(heightChanged()));
61
    connect(this,SIGNAL(stateChanged()),this,SIGNAL(heightChanged()));
Christiaan Janssen's avatar
Christiaan Janssen committed
62
63
64
65
66
67
68
}

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

69
70
int TimelineModelAggregator::height() const
{
71
    return modelOffset(d->modelList.length());
72
73
}

74
void TimelineModelAggregator::addModel(TimelineModel *m)
Christiaan Janssen's avatar
Christiaan Janssen committed
75
76
{
    d->modelList << m;
77
    connect(m,SIGNAL(heightChanged()),this,SIGNAL(heightChanged()));
78
    d->notesModel->addTimelineModel(m);
79
    emit modelsChanged();
80
81
}

82
const TimelineModel *TimelineModelAggregator::model(int modelIndex) const
83
84
85
86
{
    return d->modelList[modelIndex];
}

87
88
89
QVariantList TimelineModelAggregator::models() const
{
    QVariantList ret;
90
    foreach (TimelineModel *model, d->modelList)
91
92
        ret << QVariant::fromValue(model);
    return ret;
Christiaan Janssen's avatar
Christiaan Janssen committed
93
94
}

95
TimelineNotesModel *TimelineModelAggregator::notes() const
96
{
97
    return d->notesModel;
98
99
}

100
101
102
103
104
105
106
107
int TimelineModelAggregator::modelOffset(int modelIndex) const
{
    int ret = 0;
    for (int i = 0; i < modelIndex; ++i)
        ret += d->modelList[i]->height();
    return ret;
}

Christiaan Janssen's avatar
Christiaan Janssen committed
108
109
110
111
112
int TimelineModelAggregator::modelCount() const
{
    return d->modelList.count();
}

113
114
115
116
117
118
119
120
QVariantMap TimelineModelAggregator::nextItem(int selectedModel, int selectedItem,
                                              qint64 time) const
{
    if (selectedItem != -1)
        time = model(selectedModel)->startTime(selectedItem);

    QVarLengthArray<int> itemIndexes(modelCount());
    for (int i = 0; i < modelCount(); i++) {
121
        const TimelineModel *currentModel = model(i);
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
        if (currentModel->count() > 0) {
            if (selectedModel == i) {
                itemIndexes[i] = (selectedItem + 1) % currentModel->count();
            } else {
                if (currentModel->startTime(0) > time)
                    itemIndexes[i] = 0;
                else
                    itemIndexes[i] = (currentModel->lastIndex(time) + 1) % currentModel->count();
            }
        } else {
            itemIndexes[i] = -1;
        }
    }

    int candidateModelIndex = -1;
    qint64 candidateStartTime = std::numeric_limits<qint64>::max();
    for (int i = 0; i < modelCount(); i++) {
        if (itemIndexes[i] == -1)
            continue;
        qint64 newStartTime = model(i)->startTime(itemIndexes[i]);
        if (newStartTime > time && newStartTime < candidateStartTime) {
            candidateStartTime = newStartTime;
            candidateModelIndex = i;
        }
    }

    int itemIndex;
    if (candidateModelIndex != -1) {
        itemIndex = itemIndexes[candidateModelIndex];
    } else {
        itemIndex = -1;
        candidateStartTime = std::numeric_limits<qint64>::max();
        for (int i = 0; i < modelCount(); i++) {
155
            const TimelineModel *currentModel = model(i);
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
            if (currentModel->count() > 0 && currentModel->startTime(0) < candidateStartTime) {
                candidateModelIndex = i;
                itemIndex = 0;
                candidateStartTime = currentModel->startTime(0);
            }
        }
    }

    QVariantMap ret;
    ret.insert(QLatin1String("model"), candidateModelIndex);
    ret.insert(QLatin1String("item"), itemIndex);
    return ret;
}

QVariantMap TimelineModelAggregator::prevItem(int selectedModel, int selectedItem,
                                              qint64 time) const
{
    if (selectedItem != -1)
        time = model(selectedModel)->startTime(selectedItem);

    QVarLengthArray<int> itemIndexes(modelCount());
    for (int i = 0; i < modelCount(); i++) {
        if (selectedModel == i) {
            itemIndexes[i] = selectedItem - 1;
            if (itemIndexes[i] < 0)
                itemIndexes[i] = model(selectedModel)->count() -1;
        }
        else
            itemIndexes[i] = model(i)->lastIndex(time);
    }

    int candidateModelIndex = -1;
    qint64 candidateStartTime = std::numeric_limits<qint64>::min();
    for (int i = 0; i < modelCount(); i++) {
190
        const TimelineModel *currentModel = model(i);
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
        if (itemIndexes[i] == -1 || itemIndexes[i] >= currentModel->count())
            continue;
        qint64 newStartTime = currentModel->startTime(itemIndexes[i]);
        if (newStartTime < time && newStartTime > candidateStartTime) {
            candidateStartTime = newStartTime;
            candidateModelIndex = i;
        }
    }

    int itemIndex = -1;
    if (candidateModelIndex != -1) {
        itemIndex = itemIndexes[candidateModelIndex];
    } else {
        candidateStartTime = std::numeric_limits<qint64>::min();
        for (int i = 0; i < modelCount(); i++) {
206
            const TimelineModel *currentModel = model(i);
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
            if (currentModel->count() > 0 &&
                    currentModel->startTime(currentModel->count() - 1) > candidateStartTime) {
                candidateModelIndex = i;
                itemIndex = currentModel->count() - 1;
                candidateStartTime = currentModel->startTime(itemIndex);
            }
        }
    }

    QVariantMap ret;
    ret.insert(QLatin1String("model"), candidateModelIndex);
    ret.insert(QLatin1String("item"), itemIndex);
    return ret;
}

Christiaan Janssen's avatar
Christiaan Janssen committed
222
223
} // namespace Internal
} // namespace QmlProfiler