Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
174 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters