diff --git a/src/attendeetablemodel.cpp b/src/attendeetablemodel.cpp index 8496648..5495193 100644 --- a/src/attendeetablemodel.cpp +++ b/src/attendeetablemodel.cpp @@ -1,313 +1,314 @@ /* * Copyright (C) 2014 Sandro Knauß * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public * License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301, USA. */ #include "attendeetablemodel.h" #include #include #include using namespace IncidenceEditorNG; AttendeeTableModel::AttendeeTableModel(QObject *parent) : QAbstractTableModel(parent) , mKeepEmpty(false) , mRemoveEmptyLines(false) { } int AttendeeTableModel::rowCount(const QModelIndex & /*parent*/) const { return mAttendeeList.count(); } int AttendeeTableModel::columnCount(const QModelIndex & /*parent*/) const { return 8; } Qt::ItemFlags AttendeeTableModel::flags(const QModelIndex &index) const { if (!index.isValid()) { return Qt::ItemIsEnabled; } if (index.column() == Available || index.column() == Name || index.column() == Email) { //Available is read only return QAbstractTableModel::flags(index); } else { return QAbstractTableModel::flags(index) | Qt::ItemIsEditable; } } QVariant AttendeeTableModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } if (index.row() >= mAttendeeList.size()) { return QVariant(); } KCalCore::Attendee::Ptr attendee = mAttendeeList[index.row()]; if (role == Qt::DisplayRole || role == Qt::EditRole) { switch (index.column()) { case Role: return attendee->role(); case FullName: return attendee->fullName(); case Available: { - AvailableStatus available = mAttendeeAvailable[attendee]; + AvailableStatus available = mAttendeeAvailable[index.row()]; if (role == Qt::DisplayRole) { switch (available) { case Free: return i18n("Free"); case Busy: return i18n("Busy"); case Accepted: return i18n("Accepted"); case Unknown: return i18n("Unknown"); default: return i18n("Unknown"); } } else { return available; } } case Status: return attendee->status(); case CuType: return attendee->cuType(); case Response: return attendee->RSVP(); case Name: return attendee->name(); case Email: return attendee->email(); } } if (role == AttendeeRole) { return QVariant::fromValue(attendee); } return QVariant(); } bool AttendeeTableModel::setData(const QModelIndex &index, const QVariant &value, int role) { QString email, name; if (index.isValid() && role == Qt::EditRole) { KCalCore::Attendee::Ptr attendee = mAttendeeList[index.row()]; switch (index.column()) { case Role: attendee->setRole(static_cast(value.toInt())); break; case FullName: if (mRemoveEmptyLines && value.toString().trimmed().isEmpty()) { // Do not remove last empty line if mKeepEmpty==true // (only works if initially there is only one empty line) if (!mKeepEmpty || !(attendee->name().isEmpty() && attendee->email().isEmpty())) { removeRows(index.row(), 1); return true; } } KEmailAddress::extractEmailAddressAndName(value.toString(), email, name); attendee->setName(name); attendee->setEmail(email); addEmptyAttendee(); break; case Available: - mAttendeeAvailable[attendee] = static_cast(value.toInt()); + mAttendeeAvailable[index.row()] = static_cast(value.toInt()); break; case Status: attendee->setStatus(static_cast(value.toInt())); break; case CuType: attendee->setCuType(static_cast(value.toInt())); break; case Response: attendee->setRSVP(value.toBool()); break; default: return false; } Q_EMIT dataChanged(index, index); return true; } return false; } QVariant AttendeeTableModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role != Qt::DisplayRole) { return QVariant(); } if (orientation == Qt::Horizontal) { switch (section) { case Role: return i18nc("vCard attendee role", "Role"); case FullName: return i18nc("Attendees (name+emailaddress)", "Name"); case Available: return i18nc("Is attendee available for incidence", "Available"); case Status: return i18nc("Status of attendee in an incidence (accepted, declined, delegated, ...)", "Status"); case CuType: return i18nc("Type of calendar user (vCard attribute)", "User Type"); case Response: return i18nc("Has attendee to respond to the invitation", "Response"); case Name: return i18nc("Attendee name", "Name"); case Email: return i18nc("Attendee email", "Email"); } } return QVariant(); } bool AttendeeTableModel::insertRows(int position, int rows, const QModelIndex &parent) { beginInsertRows(parent, position, position + rows - 1); for (int row = 0; row < rows; ++row) { KCalCore::Attendee::Ptr attendee(new KCalCore::Attendee(QLatin1String(""), QLatin1String(""))); mAttendeeList.insert(position, attendee); + mAttendeeAvailable.insert(mAttendeeAvailable.begin() + position, AvailableStatus{}); } endInsertRows(); return true; } bool AttendeeTableModel::removeRows(int position, int rows, const QModelIndex &parent) { beginRemoveRows(parent, position, position + rows - 1); for (int row = 0; row < rows; ++row) { - mAttendeeAvailable.remove(mAttendeeList.at(position)); + mAttendeeAvailable.erase(mAttendeeAvailable.begin() + position); mAttendeeList.remove(position); } endRemoveRows(); return true; } bool AttendeeTableModel::insertAttendee(int position, const KCalCore::Attendee::Ptr &attendee) { beginInsertRows(QModelIndex(), position, position); - mAttendeeList.insert(position, attendee); - + mAttendeeAvailable.insert(mAttendeeAvailable.begin() + position, AvailableStatus{}); endInsertRows(); addEmptyAttendee(); return true; } void AttendeeTableModel::setAttendees(const KCalCore::Attendee::List &attendees) { Q_EMIT layoutAboutToBeChanged(); mAttendeeList = attendees; - mAttendeeAvailable = QMap(); + mAttendeeAvailable.clear(); + mAttendeeAvailable.resize(attendees.size()); addEmptyAttendee(); Q_EMIT layoutChanged(); } KCalCore::Attendee::List AttendeeTableModel::attendees() const { return mAttendeeList; } void AttendeeTableModel::addEmptyAttendee() { if (mKeepEmpty) { bool create = true; for (const KCalCore::Attendee::Ptr &attendee : qAsConst(mAttendeeList)) { if (attendee->fullName().isEmpty()) { create = false; break; } } if (create) { insertRows(rowCount(), 1); } } } bool AttendeeTableModel::keepEmpty() const { return mKeepEmpty; } void AttendeeTableModel::setKeepEmpty(bool keepEmpty) { if (keepEmpty != mKeepEmpty) { mKeepEmpty = keepEmpty; addEmptyAttendee(); } } bool AttendeeTableModel::removeEmptyLines() const { return mRemoveEmptyLines; } void AttendeeTableModel::setRemoveEmptyLines(bool removeEmptyLines) { mRemoveEmptyLines = removeEmptyLines; } ResourceFilterProxyModel::ResourceFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent) { } bool ResourceFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const { QModelIndex cuTypeIndex = sourceModel()->index(sourceRow, AttendeeTableModel::CuType, sourceParent); KCalCore::Attendee::CuType cuType = static_cast(sourceModel()->data(cuTypeIndex).toUInt()); return cuType == KCalCore::Attendee::Resource || cuType == KCalCore::Attendee::Room; } AttendeeFilterProxyModel::AttendeeFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent) { } bool AttendeeFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const { QModelIndex cuTypeIndex = sourceModel()->index(sourceRow, AttendeeTableModel::CuType, sourceParent); KCalCore::Attendee::CuType cuType = static_cast(sourceModel()->data(cuTypeIndex).toUInt()); return !(cuType == KCalCore::Attendee::Resource || cuType == KCalCore::Attendee::Room); } diff --git a/src/attendeetablemodel.h b/src/attendeetablemodel.h index d6f7eab..9461cb1 100644 --- a/src/attendeetablemodel.h +++ b/src/attendeetablemodel.h @@ -1,107 +1,107 @@ /* * Copyright (C) 2014 Sandro Knauß * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public * License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301, USA. */ #ifndef INCIDENCEEDITOR_ATTENDEETABLEMODEL_H #define INCIDENCEEDITOR_ATTENDEETABLEMODEL_H #include #include #include #include namespace IncidenceEditorNG { class AttendeeTableModel : public QAbstractTableModel { Q_OBJECT public: enum Roles { AttendeeRole = Qt::UserRole }; enum Columns { CuType, Role, FullName, Name, Email, Available, Status, Response }; enum AvailableStatus { Unknown, Free, Accepted, Busy, Tentative }; explicit AttendeeTableModel(QObject *parent = nullptr); Q_REQUIRED_RESULT int rowCount(const QModelIndex &parent = QModelIndex()) const override; Q_REQUIRED_RESULT int columnCount(const QModelIndex &parent = QModelIndex()) const override; Q_REQUIRED_RESULT QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; Q_REQUIRED_RESULT QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; Q_REQUIRED_RESULT Qt::ItemFlags flags(const QModelIndex &index) const override; Q_REQUIRED_RESULT bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; Q_REQUIRED_RESULT bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex()) override; Q_REQUIRED_RESULT bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex()) override; Q_REQUIRED_RESULT bool insertAttendee(int position, const KCalCore::Attendee::Ptr &attendee); void setAttendees(const KCalCore::Attendee::List &resources); Q_REQUIRED_RESULT KCalCore::Attendee::List attendees() const; void setKeepEmpty(bool keepEmpty); Q_REQUIRED_RESULT bool keepEmpty() const; void setRemoveEmptyLines(bool removeEmptyLines); Q_REQUIRED_RESULT bool removeEmptyLines() const; private: void addEmptyAttendee(); KCalCore::Attendee::List mAttendeeList; - QMap mAttendeeAvailable; + std::vector mAttendeeAvailable; bool mKeepEmpty; bool mRemoveEmptyLines; }; class ResourceFilterProxyModel : public QSortFilterProxyModel { Q_OBJECT public: explicit ResourceFilterProxyModel(QObject *parent = nullptr); bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; }; class AttendeeFilterProxyModel : public QSortFilterProxyModel { Q_OBJECT public: explicit AttendeeFilterProxyModel(QObject *parent = nullptr); bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; }; } #endif