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: Christian Kandeler <christian.kandeler@theqtcompany.com>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
Ulf Hermann
2016-05-25 18:17:45 +02:00
parent b3b4666745
commit c75ddf6b93
5 changed files with 33 additions and 8 deletions

View File

@@ -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);

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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)