| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "core/articlelistnotificationmodel.h" |
| 4 | |
| 5 | #include "definitions/definitions.h" |
| 6 | #include "exceptions/applicationexception.h" |
| 7 | |
| 8 | ArticleListNotificationModel::ArticleListNotificationModel(QObject* parent) |
| 9 | : QAbstractListModel(parent), m_currentPage(-1) {} |
| 10 | |
| 11 | ArticleListNotificationModel::~ArticleListNotificationModel() {} |
| 12 | |
| 13 | void ArticleListNotificationModel::setArticles(const QList<Message>& msgs) { |
| 14 | m_currentPage = 0; |
| 15 | m_articles = msgs; |
| 16 | |
| 17 | reloadWholeLayout(); |
| 18 | emit nextPagePossibleChanged(nextPageAvailable()); |
| 19 | emit previousPagePossibleChanged(previousPageAvailable()); |
| 20 | } |
| 21 | |
| 22 | Message ArticleListNotificationModel::message(const QModelIndex& idx) const { |
| 23 | int list_position = (m_currentPage * NOTIFICATIONS_PAGE_SIZE) + idx.row(); |
| 24 | |
| 25 | if (list_position < 0 || list_position >= m_articles.size()) { |
| 26 | throw ApplicationException(QSL("message cannot be loaded, wrong index")); |
| 27 | } |
| 28 | |
| 29 | return m_articles.at(list_position); |
| 30 | } |
| 31 | |
| 32 | void ArticleListNotificationModel::nextPage() { |
| 33 | m_currentPage++; |
| 34 | reloadWholeLayout(); |
| 35 | |
| 36 | emit nextPagePossibleChanged(nextPageAvailable()); |
| 37 | emit previousPagePossibleChanged(previousPageAvailable()); |
| 38 | } |
| 39 | |
| 40 | void ArticleListNotificationModel::previousPage() { |
| 41 | m_currentPage--; |
| 42 | reloadWholeLayout(); |
| 43 | |
| 44 | emit nextPagePossibleChanged(nextPageAvailable()); |
| 45 | emit previousPagePossibleChanged(previousPageAvailable()); |
| 46 | } |
| 47 | |
| 48 | int ArticleListNotificationModel::rowCount(const QModelIndex& parent) const { |
| 49 | return std::min(int(m_articles.size() - (NOTIFICATIONS_PAGE_SIZE * m_currentPage)), NOTIFICATIONS_PAGE_SIZE); |
| 50 | } |
| 51 | |
| 52 | int ArticleListNotificationModel::columnCount(const QModelIndex& parent) const { |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | QVariant ArticleListNotificationModel::data(const QModelIndex& index, int role) const { |
| 57 | switch (role) { |
| 58 | case Qt::ItemDataRole::DisplayRole: |
| 59 | case Qt::ItemDataRole::ToolTipRole: |
| 60 | return m_articles.at((m_currentPage * NOTIFICATIONS_PAGE_SIZE) + index.row()).m_title; |
| 61 | } |
| 62 | |
| 63 | return QVariant(); |
| 64 | } |
| 65 | |
| 66 | void ArticleListNotificationModel::reloadWholeLayout() { |
| 67 | emit layoutAboutToBeChanged(); |
| 68 | emit layoutChanged(); |
| 69 | } |
| 70 | |
| 71 | bool ArticleListNotificationModel::nextPageAvailable() const { |
| 72 | return m_articles.size() - (NOTIFICATIONS_PAGE_SIZE * (m_currentPage + 1)) > 0; |
| 73 | } |
| 74 | |
| 75 | bool ArticleListNotificationModel::previousPageAvailable() const { |
| 76 | return m_currentPage > 0; |
| 77 | } |