Commit fd0e36c9 authored by Claudio Cambra's avatar Claudio Cambra
Browse files

Refactor aspects of incidenceoccurrencemodel for readability and reuse



Signed-off-by: Claudio Cambra's avatarClaudio Cambra <claudio.cambra@gmail.com>
parent 1b3ce728
......@@ -120,35 +120,14 @@ void IncidenceOccurrenceModel::updateFromSource()
occurrenceIterator.next();
const auto incidence = occurrenceIterator.incidence();
if (mFilter && mFilter->tags().length() > 0) {
auto match = false;
const auto tags = mFilter->tags();
for (const auto &tag : tags) {
if (incidence->categories().contains(tag)) {
match = true;
break;
}
}
if (!match) {
continue;
}
}
auto start = occurrenceIterator.occurrenceStartDate();
const auto end = incidence->endDateForStart(start);
if (incidence->type() == KCalendarCore::Incidence::IncidenceType::TypeTodo) {
KCalendarCore::Todo::Ptr todo = incidence.staticCast<KCalendarCore::Todo>();
if (!start.isValid()) { // Todos are very likely not to have a set start date
start = todo->dtDue();
}
if(!incidencePassesFilter(incidence)) {
continue;
}
const auto occurrenceHashKey = qHash(QString::number(start.toSecsSinceEpoch()) +
QString::number(end.toSecsSinceEpoch()) +
incidence->uid());
const auto occurrenceStartEnd = incidenceOccurrenceStartEnd(occurrenceIterator.occurrenceStartDate(), incidence);
const auto start = occurrenceStartEnd.first;
const auto end = occurrenceStartEnd.second;
const auto occurrenceHashKey = incidenceOccurrenceHash(start, end, incidence->uid());
const Occurrence occurrence{
start,
end,
......@@ -208,20 +187,10 @@ void IncidenceOccurrenceModel::slotSourceDataChanged(const QModelIndex &upperLef
while (occurrenceIterator.hasNext()) {
occurrenceIterator.next();
auto start = occurrenceIterator.occurrenceStartDate();
const auto end = incidence->endDateForStart(start);
if (incidence->type() == KCalendarCore::Incidence::IncidenceType::TypeTodo) {
KCalendarCore::Todo::Ptr todo = incidence.staticCast<KCalendarCore::Todo>();
if (!start.isValid()) { // Todos are very likely not to have a set start date
start = todo->dtDue();
}
}
const auto occurrenceHashKey = qHash(QString::number(start.toSecsSinceEpoch()) +
QString::number(end.toSecsSinceEpoch()) +
incidence->uid());
const auto occurrenceStartEnd = incidenceOccurrenceStartEnd(occurrenceIterator.occurrenceStartDate(), incidence);
const auto start = occurrenceStartEnd.first;
const auto end = occurrenceStartEnd.second;
const auto occurrenceHashKey = incidenceOccurrenceHash(start, end, incidence->uid());
if(!m_occurrenceIndexHash.contains(occurrenceHashKey)) {
continue;
......@@ -397,3 +366,44 @@ void IncidenceOccurrenceModel::load()
m_colors[key] = color;
}
}
std::pair<QDateTime, QDateTime> IncidenceOccurrenceModel::incidenceOccurrenceStartEnd(const QDateTime &ocStart, const KCalendarCore::Incidence::Ptr &incidence)
{
auto start = ocStart;
const auto end = incidence->endDateForStart(start);
if (incidence->type() == KCalendarCore::Incidence::IncidenceType::TypeTodo) {
KCalendarCore::Todo::Ptr todo = incidence.staticCast<KCalendarCore::Todo>();
if (!start.isValid()) { // Todos are very likely not to have a set start date
start = todo->dtDue();
}
}
return {start, end};
}
uint IncidenceOccurrenceModel::incidenceOccurrenceHash(const QDateTime &ocStart, const QDateTime &ocEnd, const QString &incidenceUid)
{
return qHash(QString::number(ocStart.toSecsSinceEpoch()) +
QString::number(ocEnd.toSecsSinceEpoch()) +
incidenceUid);
}
bool IncidenceOccurrenceModel::incidencePassesFilter(const KCalendarCore::Incidence::Ptr &incidence)
{
if(!mFilter || mFilter->tags().empty()) {
return true;
}
auto match = false;
const auto tags = mFilter->tags();
for (const auto &tag : tags) {
if (incidence->categories().contains(tag)) {
match = true;
break;
}
}
return match;
}
......@@ -108,10 +108,14 @@ Q_SIGNALS:
private Q_SLOTS:
void slotSourceDataChanged(const QModelIndex &upperLeft, const QModelIndex &bottomRight);
private:
void refreshView();
void updateFromSource();
private:
static std::pair<QDateTime, QDateTime> incidenceOccurrenceStartEnd(const QDateTime &ocStart, const KCalendarCore::Incidence::Ptr &incidence);
static uint incidenceOccurrenceHash(const QDateTime &ocStart, const QDateTime &ocEnd, const QString &incidenceUid);
bool incidencePassesFilter(const KCalendarCore::Incidence::Ptr &incidence);
QColor getColor(const KCalendarCore::Incidence::Ptr &incidence);
qint64 getCollectionId(const KCalendarCore::Incidence::Ptr &incidence);
......
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