Fix crash when there is already exactly 5 events displayed
And simplify code: * Add constant for the max number of events that can be displayed at the same time * Use std::min to simplify a bit the code * Rename nextIndex to addedEventCount
This commit is contained in:
parent
6ca69fb7ca
commit
c1517f728e
@ -15,6 +15,8 @@
|
|||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QMetaObject>
|
#include <QMetaObject>
|
||||||
|
|
||||||
|
constexpr int maxEventDisplayed = 5;
|
||||||
|
|
||||||
DaysModel::DaysModel(QObject *parent)
|
DaysModel::DaysModel(QObject *parent)
|
||||||
: QAbstractItemModel(parent)
|
: QAbstractItemModel(parent)
|
||||||
, m_pluginsManager(nullptr)
|
, m_pluginsManager(nullptr)
|
||||||
@ -49,7 +51,7 @@ int DaysModel::rowCount(const QModelIndex &parent) const
|
|||||||
} else {
|
} else {
|
||||||
// event count
|
// event count
|
||||||
const auto &eventDatas = data(parent, Roles::Events).value<QList<CalendarEvents::EventData>>();
|
const auto &eventDatas = data(parent, Roles::Events).value<QList<CalendarEvents::EventData>>();
|
||||||
Q_ASSERT(eventDatas.count() <= 5);
|
Q_ASSERT(eventDatas.count() <= maxEventDisplayed);
|
||||||
return eventDatas.count();
|
return eventDatas.count();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -142,25 +144,19 @@ void DaysModel::onDataReady(const QMultiHash<QDate, CalendarEvents::EventData> &
|
|||||||
const DayData ¤tData = m_data->at(i);
|
const DayData ¤tData = m_data->at(i);
|
||||||
const QDate currentDate(currentData.yearNumber, currentData.monthNumber, currentData.dayNumber);
|
const QDate currentDate(currentData.yearNumber, currentData.monthNumber, currentData.dayNumber);
|
||||||
if (!data.values(currentDate).isEmpty()) {
|
if (!data.values(currentDate).isEmpty()) {
|
||||||
// Make sure we don't display more than 5 events
|
// Make sure we don't display more than maxEventDisplayed events.
|
||||||
const int currentCount = m_eventsData.values(currentDate).count();
|
const int currentCount = m_eventsData.values(currentDate).count();
|
||||||
|
if (currentCount >= maxEventDisplayed) {
|
||||||
if (currentCount > 5) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int additionalCount = data.values(currentDate).count();
|
const int addedEventCount = std::min(currentCount + data.values(currentDate).count(), maxEventDisplayed) - currentCount;
|
||||||
|
|
||||||
int nextIndex = data.values(currentDate).count() - 1;
|
|
||||||
if (currentCount + additionalCount > 5) {
|
|
||||||
nextIndex = 5 - currentCount - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add event
|
// Add event
|
||||||
beginInsertRows(index(i, 0), 0, nextIndex);
|
beginInsertRows(index(i, 0), 0, addedEventCount - 1);
|
||||||
int stopCounter = 0;
|
int stopCounter = currentCount;
|
||||||
for (const auto &dataDay : data.values(currentDate)) {
|
for (const auto &dataDay : data.values(currentDate)) {
|
||||||
if (stopCounter > nextIndex) {
|
if (stopCounter >= maxEventDisplayed) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
stopCounter++;
|
stopCounter++;
|
||||||
|
Loading…
Reference in New Issue
Block a user