diff options
-rw-r--r-- | kelakon.pro | 4 | ||||
-rw-r--r-- | tasklist.cxx | 40 | ||||
-rw-r--r-- | tasklist.hxx | 37 |
3 files changed, 80 insertions, 1 deletions
diff --git a/kelakon.pro b/kelakon.pro index 96a5a12..8b9ead1 100644 --- a/kelakon.pro +++ b/kelakon.pro @@ -7,12 +7,14 @@ debug: DEFINES += DEBUG HEADERS += \ user.hxx \ worker.hxx \ - controller.hxx + controller.hxx \ + tasklist.hxx SOURCES += \ user.cxx \ worker.cxx \ controller.cxx \ + tasklist.cxx \ main.cxx RESOURCES += kelakon.qrc diff --git a/tasklist.cxx b/tasklist.cxx new file mode 100644 index 0000000..5850584 --- /dev/null +++ b/tasklist.cxx @@ -0,0 +1,40 @@ +#include "tasklist.hxx" + +int TaskList::rowCount(QModelIndex const& parent) const +{ + Q_UNUSED(parent) + return tasks.count(); +} + +QVariant TaskList::data(QModelIndex const& index, int role) const +{ + auto row = index.row(); + + if (row < 0 || row >= tasks.count()) return QVariant(); + + auto task = tasks[row]; + switch (role) { + case Task::IdRole: + return task.id; + case Task::SubjectRole: + return task.subject; + default: + return QVariant(); + } +} + +QHash<int, QByteArray> TaskList::roleNames() const +{ + return QHash<int, QByteArray>{ + {Task::IdRole, "id"}, + {Task::SubjectRole, "subject"} + }; +} + +void TaskList::addTask(Task const& task) +{ + beginInsertRows(QModelIndex(), rowCount(), rowCount()); + tasks << task; + endInsertRows(); + emit rowCountChanged(); +} diff --git a/tasklist.hxx b/tasklist.hxx new file mode 100644 index 0000000..716c365 --- /dev/null +++ b/tasklist.hxx @@ -0,0 +1,37 @@ +#ifndef TASKLIST_HXX +#define TASKLIST_HXX + +#include <QAbstractListModel> + +struct Task +{ + enum TaskRoles { + IdRole = Qt::UserRole + 1, + SubjectRole + }; + QString id; + QString subject; +}; + +class TaskList : public QAbstractListModel +{ + Q_OBJECT + Q_PROPERTY(int rowCount READ rowCount NOTIFY rowCountChanged) + + public: + explicit TaskList(QObject* parent = nullptr) : QAbstractListModel{parent} {} + int rowCount(QModelIndex const& parent = QModelIndex()) const Q_DECL_OVERRIDE; + QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; + + protected: + QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE; + + signals: + void rowCountChanged(); + + private: + QList<Task> tasks; + void addTask(Task const& task); +}; + +#endif // TASKLIST_HXX |