timelinemodelaggregator.cpp 7.46 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

#include <QStringList>
#include <QVariant>

41
namespace Timeline {
Christiaan Janssen's avatar
Christiaan Janssen committed
42
43
44
45
46
47
48
49

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

    TimelineModelAggregator *q;

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

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

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

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

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

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

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

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

98
99
100
101
102
103
104
105
106
void TimelineModelAggregator::clear()
{
    qDeleteAll(d->modelList);
    d->modelList.clear();
    if (d->notesModel)
        d->notesModel->clear();
    emit modelsChanged();
}

107
108
109
110
111
112
113
114
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
115
116
117
118
119
int TimelineModelAggregator::modelCount() const
{
    return d->modelList.count();
}

120
121
122
123
124
125
126
127
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++) {
128
        const TimelineModel *currentModel = model(i);
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
        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++) {
162
            const TimelineModel *currentModel = model(i);
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
            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++) {
197
        const TimelineModel *currentModel = model(i);
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
        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++) {
213
            const TimelineModel *currentModel = model(i);
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
            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;
}

229
} // namespace Timeline