Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[nemo-qml-plugin-calendar] Add participant list model. Contributes to…
… JB#49920
  • Loading branch information
p.chebotarev authored and adenexter committed Jan 5, 2021
1 parent efc4655 commit e3aeadc
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 2 deletions.
115 changes: 115 additions & 0 deletions src/calendarattendeemodel.cpp
@@ -0,0 +1,115 @@
#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;
}
53 changes: 53 additions & 0 deletions src/calendarattendeemodel.h
@@ -0,0 +1,53 @@
#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
2 changes: 2 additions & 0 deletions src/plugin.cpp
Expand Up @@ -47,6 +47,7 @@
#include "calendarnotebookquery.h"
#include "calendarimportmodel.h"
#include "calendarcontactmodel.h"
#include "calendarattendeemodel.h"

class QtDate : public QObject
{
Expand Down Expand Up @@ -131,6 +132,7 @@ class Q_DECL_EXPORT NemoCalendarPlugin : public QQmlExtensionPlugin
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");
}
};

Expand Down
6 changes: 4 additions & 2 deletions src/src.pro
Expand Up @@ -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 \
Expand All @@ -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

Expand Down

0 comments on commit e3aeadc

Please sign in to comment.