Commit c75ddf6b authored by Ulf Hermann's avatar Ulf Hermann

Timeline: Provide a method to set all timeline models at once

Setting a number of models one by one results in quadratic overhead
when (re)building the UI. If there are many models this quickly gets
out of hand.

Change-Id: I7f017784c3f0a6a9d7e52c881eb1d8ca4cf70886
Reviewed-by: default avatarChristian Kandeler <christian.kandeler@theqtcompany.com>
Reviewed-by: Ulf Hermann's avatarUlf Hermann <ulf.hermann@qt.io>
parent b3b46667
......@@ -70,6 +70,30 @@ void TimelineModelAggregator::addModel(TimelineModel *m)
emit heightChanged();
}
void TimelineModelAggregator::setModels(const QList<TimelineModel *> &models)
{
Q_D(TimelineModelAggregator);
if (d->modelList == models)
return;
int prevHeight = height();
foreach (TimelineModel *m, d->modelList) {
disconnect(m, &TimelineModel::heightChanged, this, &TimelineModelAggregator::heightChanged);
if (d->notesModel)
d->notesModel->removeTimelineModel(m);
}
d->modelList = models;
foreach (TimelineModel *m, models) {
connect(m, &TimelineModel::heightChanged, this, &TimelineModelAggregator::heightChanged);
if (d->notesModel)
d->notesModel->addTimelineModel(m);
}
emit modelsChanged();
if (height() != prevHeight)
emit heightChanged();
}
const TimelineModel *TimelineModelAggregator::model(int modelIndex) const
{
Q_D(const TimelineModelAggregator);
......
......@@ -43,6 +43,7 @@ public:
int height() const;
void addModel(TimelineModel *m);
void setModels(const QList<TimelineModel *> &models);
const TimelineModel *model(int modelIndex) const;
QVariantList models() const;
......
......@@ -52,8 +52,9 @@ int TimelineNotesModel::count() const
void TimelineNotesModel::addTimelineModel(const TimelineModel *timelineModel)
{
Q_D(TimelineNotesModel);
connect(timelineModel, SIGNAL(destroyed(QObject*)),
this, SLOT(_q_removeTimelineModel(QObject*)));
connect(timelineModel, &QObject::destroyed, this, [this](QObject *obj) {
removeTimelineModel(static_cast<TimelineModel *>(obj));
});
d->timelineModels.insert(timelineModel->modelId(), timelineModel);
}
......@@ -178,11 +179,12 @@ void TimelineNotesModel::resetModified()
d->modified = false;
}
void TimelineNotesModel::TimelineNotesModelPrivate::_q_removeTimelineModel(QObject *timelineModel)
void TimelineNotesModel::removeTimelineModel(const TimelineModel *timelineModel)
{
for (auto i = timelineModels.begin(); i != timelineModels.end();) {
Q_D(TimelineNotesModel);
for (auto i = d->timelineModels.begin(); i != d->timelineModels.end();) {
if (i.value() == timelineModel)
i = timelineModels.erase(i);
i = d->timelineModels.erase(i);
else
++i;
}
......
......@@ -39,6 +39,7 @@ public:
int count() const;
void addTimelineModel(const TimelineModel *timelineModel);
void removeTimelineModel(const TimelineModel *timelineModel);
QList<const TimelineModel *> timelineModels() const;
Q_INVOKABLE int typeId(int index) const;
......@@ -73,7 +74,6 @@ private:
TimelineNotesModelPrivate *d_ptr;
Q_DECLARE_PRIVATE(TimelineNotesModel)
Q_PRIVATE_SLOT(d_ptr, void _q_removeTimelineModel(QObject *timelineModel))
};
} // namespace Timeline
......@@ -46,8 +46,6 @@ public:
QHash<int, const TimelineModel *> timelineModels;
bool modified;
void _q_removeTimelineModel(QObject *model);
private:
TimelineNotesModel *q_ptr;
Q_DECLARE_PUBLIC(TimelineNotesModel)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment