2023-09-22 07:24:55 +02:00
|
|
|
// For license of this file, see <project-root-folder>/LICENSE.md.
|
|
|
|
|
|
|
|
#include "core/articlelistnotificationmodel.h"
|
|
|
|
|
2023-09-22 13:14:04 +02:00
|
|
|
#include "definitions/definitions.h"
|
2023-09-29 11:16:34 +02:00
|
|
|
#include "exceptions/applicationexception.h"
|
2023-09-22 13:14:04 +02:00
|
|
|
|
2023-09-22 07:24:55 +02:00
|
|
|
ArticleListNotificationModel::ArticleListNotificationModel(QObject* parent)
|
|
|
|
: QAbstractListModel(parent), m_currentPage(-1) {}
|
|
|
|
|
|
|
|
ArticleListNotificationModel::~ArticleListNotificationModel() {}
|
|
|
|
|
|
|
|
void ArticleListNotificationModel::setArticles(const QList<Message>& msgs) {
|
|
|
|
m_currentPage = 0;
|
2023-09-22 13:37:10 +02:00
|
|
|
m_articles = msgs;
|
2023-09-22 13:14:04 +02:00
|
|
|
|
|
|
|
reloadWholeLayout();
|
2023-09-22 13:37:10 +02:00
|
|
|
emit nextPagePossibleChanged(nextPageAvailable());
|
|
|
|
emit previousPagePossibleChanged(previousPageAvailable());
|
2023-09-22 13:14:04 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 14:32:47 +02:00
|
|
|
Message ArticleListNotificationModel::message(const QModelIndex& idx) const {
|
2023-09-29 11:16:34 +02:00
|
|
|
int list_position = (m_currentPage * NOTIFICATIONS_PAGE_SIZE) + idx.row();
|
|
|
|
|
|
|
|
if (list_position < 0 || list_position >= m_articles.size()) {
|
|
|
|
throw ApplicationException(QSL("message cannot be loaded, wrong index"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_articles.at(list_position);
|
2023-09-22 14:32:47 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 13:14:04 +02:00
|
|
|
void ArticleListNotificationModel::nextPage() {
|
2023-09-22 13:37:10 +02:00
|
|
|
m_currentPage++;
|
|
|
|
reloadWholeLayout();
|
|
|
|
|
|
|
|
emit nextPagePossibleChanged(nextPageAvailable());
|
|
|
|
emit previousPagePossibleChanged(previousPageAvailable());
|
2023-09-22 13:14:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleListNotificationModel::previousPage() {
|
2023-09-22 13:37:10 +02:00
|
|
|
m_currentPage--;
|
|
|
|
reloadWholeLayout();
|
|
|
|
|
|
|
|
emit nextPagePossibleChanged(nextPageAvailable());
|
|
|
|
emit previousPagePossibleChanged(previousPageAvailable());
|
2023-09-22 07:24:55 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 13:14:04 +02:00
|
|
|
int ArticleListNotificationModel::rowCount(const QModelIndex& parent) const {
|
|
|
|
return std::min(int(m_articles.size() - (NOTIFICATIONS_PAGE_SIZE * m_currentPage)), NOTIFICATIONS_PAGE_SIZE);
|
|
|
|
}
|
2023-09-22 07:24:55 +02:00
|
|
|
|
2023-09-22 13:14:04 +02:00
|
|
|
int ArticleListNotificationModel::columnCount(const QModelIndex& parent) const {
|
|
|
|
return 1;
|
|
|
|
}
|
2023-09-22 07:24:55 +02:00
|
|
|
|
2023-09-22 13:14:04 +02:00
|
|
|
QVariant ArticleListNotificationModel::data(const QModelIndex& index, int role) const {
|
|
|
|
switch (role) {
|
|
|
|
case Qt::ItemDataRole::DisplayRole:
|
2023-09-22 13:37:10 +02:00
|
|
|
case Qt::ItemDataRole::ToolTipRole:
|
2023-09-22 13:14:04 +02:00
|
|
|
return m_articles.at((m_currentPage * NOTIFICATIONS_PAGE_SIZE) + index.row()).m_title;
|
|
|
|
}
|
2023-09-22 07:24:55 +02:00
|
|
|
|
2023-09-22 13:14:04 +02:00
|
|
|
return QVariant();
|
|
|
|
}
|
2023-09-22 07:24:55 +02:00
|
|
|
|
2023-09-22 13:14:04 +02:00
|
|
|
void ArticleListNotificationModel::reloadWholeLayout() {
|
|
|
|
emit layoutAboutToBeChanged();
|
|
|
|
emit layoutChanged();
|
|
|
|
}
|
2023-09-22 13:37:10 +02:00
|
|
|
|
|
|
|
bool ArticleListNotificationModel::nextPageAvailable() const {
|
|
|
|
return m_articles.size() - (NOTIFICATIONS_PAGE_SIZE * (m_currentPage + 1)) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ArticleListNotificationModel::previousPageAvailable() const {
|
|
|
|
return m_currentPage > 0;
|
|
|
|
}
|