Commit e3aeadc4 authored by p_chebotarev's avatar p_chebotarev Committed by Andrew den Exter

[nemo-qml-plugin-calendar] Add participant list model. Contributes to JB#49920

parent efc46552
#include "calendarattendeemodel.h"
#include <algorithm>
static CalendarAttendeeModel::SectionRoles sectionRole(const Person *person)
{
return person->isOrganizer()
? CalendarAttendeeModel::OrganizerSection
: CalendarAttendeeModel::SectionRoles(qBound<int>(0, person->participationRole(), 3));
}
CalendarAttendeeModel::CalendarAttendeeModel(QObject *parent)
: QAbstractListModel(parent)
{
}
CalendarAttendeeModel::~CalendarAttendeeModel()
{
qDeleteAll(m_people);
}
int CalendarAttendeeModel::rowCount(const QModelIndex &index) const
{
if (index != QModelIndex())
return 0;
return m_people.size();
}
QVariant CalendarAttendeeModel::data(const QModelIndex &index, int role) const
{
QVariant ret;
if (!index.isValid() || index.row() < 0 || index.row() >= m_people.size())
return ret;
const Person* person = m_people[index.row()];
switch (role) {
case NameRole:
ret = person->name();
break;
case EmailRole:
ret = person->email();
break;
case IsOrganizerRole:
ret = person->isOrganizer();
break;
case ParticipationRoleRole:
ret = person->participationRole();
break;
case ParticipationStatusRole:
ret = person->participationStatus();
break;
case ParticipationSectionRole:
ret = sectionRole(person);
break;
}
return ret;
}
int CalendarAttendeeModel::count() const
{
return m_people.size();
}
void CalendarAttendeeModel::doFill(const QList<QObject*> &people)
{
const int rulesTable[] = {
1, // Required
3, // Optional
4, // Non,
2, // Chair,
0, // Organizer
};
auto groupingAndSorting = [&rulesTable](const Person* lhs, const Person* rhs) -> bool {
const SectionRoles lhsRole = sectionRole(lhs);
const SectionRoles rhsRole = sectionRole(rhs);
return lhsRole != rhsRole
? rulesTable[lhsRole] < rulesTable[rhsRole]
: QString::localeAwareCompare(lhs->name(), rhs->name()) < 0;
};
beginResetModel();
for (auto it : people) {
auto person = qobject_cast<Person*>(it);
Q_ASSERT_X(person != nullptr,
"qobject cast to class Person",
"The container doesn't contain an instance of Person class");
m_people.push_back(new Person(person->name(),
person->email(),
person->isOrganizer(),
Person::AttendeeRole(person->participationRole()),
Person::ParticipationStatus(person->participationStatus())));
}
std::sort(m_people.begin(), m_people.end(), groupingAndSorting);
endResetModel();
if (people.size() != m_people.size()) {
emit countChanged();
}
}
QHash<int, QByteArray> CalendarAttendeeModel::roleNames() const
{
static QHash<int, QByteArray> roleNames {
{ NameRole, "name" },
{ EmailRole, "email" },
{ IsOrganizerRole, "isOrganizer" },
{ ParticipationRoleRole, "participationRole" },
{ ParticipationStatusRole, "participationStatus" },
{ ParticipationSectionRole, "participationSection" }
};
return roleNames;
}
#ifndef CALENDARATTENDEEMODEL_H
#define CALENDARATTENDEEMODEL_H
#include <QVector>
#include <QAbstractListModel>
#include "calendareventquery.h"
class CalendarAttendeeModel : public QAbstractListModel
{
Q_OBJECT
Q_ENUMS(SectionRoles)
Q_PROPERTY(int count READ count NOTIFY countChanged)
public:
enum AttendeeRoles {
NameRole = Qt::UserRole,
EmailRole,
IsOrganizerRole,
ParticipationRoleRole,
ParticipationStatusRole,
ParticipationSectionRole
};
enum SectionRoles {
RequiredSection = Person::RequiredParticipant,
OptionalSection = Person::OptionalParticipant,
NonSection = Person::NonParticipant,
ChairSection = Person::ChairParticipant,
OrganizerSection
};
explicit CalendarAttendeeModel(QObject *parent = nullptr);
virtual ~CalendarAttendeeModel();
int rowCount(const QModelIndex &index) const override;
QVariant data(const QModelIndex &index, int role) const override;
int count() const;
Q_INVOKABLE void doFill(const QList<QObject*>& people);
signals:
void countChanged();
protected:
virtual QHash<int, QByteArray> roleNames() const override;
private:
QVector<Person*> m_people;
};
#endif // CALENDARATTENDEEMODEL_H
......@@ -47,6 +47,7 @@
#include "calendarnotebookquery.h"
#include "calendarimportmodel.h"
#include "calendarcontactmodel.h"
#include "calendarattendeemodel.h"
class QtDate : public QObject
{
......@@ -131,6 +132,7 @@ public:
qmlRegisterSingletonType<CalendarApi>(uri, 1, 0, "Calendar", CalendarApi::New);
qmlRegisterType<CalendarImportModel>(uri, 1, 0, "ImportModel");
qmlRegisterType<CalendarContactModel>(uri, 1, 0, "ContactModel");
qmlRegisterType<CalendarAttendeeModel>(uri, 1, 0, "AttendeeModel");
}
};
......
......@@ -41,7 +41,8 @@ SOURCES += \
$$SRCDIR/calendarutils.cpp \
$$SRCDIR/calendarimportmodel.cpp \
$$SRCDIR/calendarimportevent.cpp \
$$SRCDIR/calendarcontactmodel.cpp
$$SRCDIR/calendarcontactmodel.cpp \
$$SRCDIR/calendarattendeemodel.cpp
HEADERS += \
$$SRCDIR/calendarevent.h \
......@@ -60,7 +61,8 @@ HEADERS += \
$$SRCDIR/calendarutils.h \
$$SRCDIR/calendarimportmodel.h \
$$SRCDIR/calendarimportevent.h \
$$SRCDIR/calendarcontactmodel.h
$$SRCDIR/calendarcontactmodel.h \
$$SRCDIR/calendarattendeemodel.h
OTHER_FILES += qmldir
......
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