Commit 06b0125e authored by Claudio Cambra's avatar Claudio Cambra
Browse files

Port multidayincidencemodel optimisations to hourlyviewincidencemodel



Signed-off-by: Claudio Cambra's avatarClaudio Cambra <claudio.cambra@gmail.com>
parent 5cce1cf8
Pipeline #248292 failed with stage
in 1 minute and 59 seconds
......@@ -386,6 +386,84 @@ void HourlyIncidenceModel::setModel(IncidenceOccurrenceModel *model)
QObject::connect(model, &QAbstractItemModel::rowsRemoved, this, resetModel);
}
void HourlyIncidenceModel::slotSourceDataChanged(const QModelIndex &upperLeft, const QModelIndex &bottomRight)
{
if(!upperLeft.isValid() || !bottomRight.isValid()) {
return;
}
const auto startRow = upperLeft.row();
const auto endRow = bottomRight.row();
scheduleLayoutLinesUpdates(upperLeft.parent(), startRow, endRow);
}
void HourlyIncidenceModel::scheduleLayoutLinesUpdates(const QModelIndex &sourceIndexParent, const int sourceFirstRow, const int sourceLastRow)
{
if (!mSourceModel) {
return;
}
// If we have no existing laid out lines it means we have not done an
// initial setup. Go do that, which will provide incidences anyway
if (m_laidOutLines.empty()) {
resetLayoutLines();
return;
}
// Don't bother if we are going for a full reset soon
if (mRefreshTimer.isActive()) {
return;
}
for (int i = sourceFirstRow; i <= sourceLastRow; ++i) {
if(m_linesToUpdate.count() == m_laidOutLines.count()) {
break;
}
const auto sourceModelIndex = mSourceModel->index(i, 0, sourceIndexParent);
const auto occurrence = sourceModelIndex.data(IncidenceOccurrenceModel::IncidenceOccurrence).value<IncidenceOccurrenceModel::Occurrence>();
const auto sourceModelStartDate = mSourceModel->start();
const auto startDaysFromSourceStart = sourceModelStartDate.daysTo(occurrence.start.date());
const auto endDaysFromSourceStart = sourceModelStartDate.daysTo(occurrence.end.date());
const auto firstPeriodOccurrenceAppears = startDaysFromSourceStart / mPeriodLength;
const auto lastPeriodOccurrenceAppears = endDaysFromSourceStart / mPeriodLength;
if(firstPeriodOccurrenceAppears > m_laidOutLines.count() || lastPeriodOccurrenceAppears < 0) {
continue;
}
const auto lastRow = m_laidOutLines.count() - 1;
const auto minRow = qMin(qMax(static_cast<int>(firstPeriodOccurrenceAppears), 0), lastRow);
const auto maxRow = qMin(static_cast<int>(lastPeriodOccurrenceAppears), lastRow);
// We set the layout lines scheduled to redo
m_linesToUpdate.insert(minRow);
m_linesToUpdate.insert(maxRow);
}
// Throttle how often we are changing the lines or the views will be slow
if (!m_updateLinesTimer.isActive()) {
m_updateLinesTimer.start(100);
}
}
void HourlyIncidenceModel::updateScheduledLayoutLines()
{
for (const auto lineIndex : m_linesToUpdate) {
const auto periodStart = mSourceModel->start().addDays(lineIndex).startOfDay();
const auto idx = index(lineIndex, 0);
const auto laidOutLine = layoutLines(periodStart);
m_laidOutLines.replace(lineIndex, laidOutLine);
Q_EMIT dataChanged(idx, idx);
}
m_linesToUpdate.clear();
}
int HourlyIncidenceModel::periodLength()
{
return mPeriodLength;
......
......@@ -69,12 +69,17 @@ Q_SIGNALS:
private Q_SLOTS:
void resetLayoutLines();
void slotSourceDataChanged(const QModelIndex &upperLeft, const QModelIndex &bottomRight);
void scheduleLayoutLinesUpdates(const QModelIndex &sourceIndexParent, const int sourceFirstRow, const int sourceLastRow);
void updateScheduledLayoutLines();
private:
QList<QModelIndex> sortedIncidencesFromSourceModel(const QDateTime &rowStart) const;
QVariantList layoutLines(const QDateTime &rowStart) const;
QSet<int> m_linesToUpdate;
QTimer mRefreshTimer;
QTimer m_updateLinesTimer;
IncidenceOccurrenceModel *mSourceModel{nullptr};
QVector<QVariantList> m_laidOutLines;
int mPeriodLength{15}; // In minutes
......
Supports Markdown
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