| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #ifndef MESSAGESPROXYMODEL_H |
| 4 | #define MESSAGESPROXYMODEL_H |
| 5 | |
| 6 | #include <QSortFilterProxyModel> |
| 7 | |
| 8 | class MessagesModel; |
| 9 | class Message; |
| 10 | |
| 11 | class MessagesProxyModel : public QSortFilterProxyModel { |
| 12 | Q_OBJECT |
| 13 | |
| 14 | public: |
| 15 | // Enum which describes basic filtering schemes |
| 16 | // for messages. |
| 17 | enum class MessageListFilter { |
| 18 | NoFiltering = 1, |
| 19 | ShowUnread = 2, |
| 20 | ShowImportant = 4, |
| 21 | ShowToday = 8, |
| 22 | ShowYesterday = 16, |
| 23 | ShowLast24Hours = 32, |
| 24 | ShowLast48Hours = 64, |
| 25 | ShowThisWeek = 128, |
| 26 | ShowLastWeek = 256, |
| 27 | ShowOnlyWithAttachments = 512, |
| 28 | ShowOnlyWithScore = 1024, |
| 29 | ShowRead = 2048 |
| 30 | }; |
| 31 | |
| 32 | explicit MessagesProxyModel(MessagesModel* source_model, QObject* parent = nullptr); |
| 33 | virtual ~MessagesProxyModel(); |
| 34 | |
| 35 | QModelIndex getNextPreviousImportantItemIndex(int default_row) const; |
| 36 | QModelIndex getNextPreviousUnreadItemIndex(int default_row) const; |
| 37 | |
| 38 | QModelIndex indexFromMessage(const Message& msg) const; |
| 39 | |
| 40 | // Maps list of indexes. |
| 41 | QModelIndexList mapListToSource(const QModelIndexList& indexes) const; |
| 42 | QModelIndexList mapListFromSource(const QModelIndexList& indexes, bool deep = false) const; |
| 43 | |
| 44 | void setMessageListFilter(MessageListFilter filter); |
| 45 | |
| 46 | // Fix for matching indexes with respect to specifics of the message model. |
| 47 | virtual QModelIndexList match(const QModelIndex& start, |
| 48 | int role, |
| 49 | const QVariant& entered_value, |
| 50 | int hits, |
| 51 | Qt::MatchFlags flags) const; |
| 52 | |
| 53 | // Performs sort of items. |
| 54 | virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder); |
| 55 | |
| 56 | int additionalArticleId() const; |
| 57 | void setAdditionalArticleId(int additional_article_id); |
| 58 | |
| 59 | private: |
| 60 | void initializeFilters(); |
| 61 | |
| 62 | QModelIndex getNextImportantItemIndex(int default_row, int max_row) const; |
| 63 | QModelIndex getNextUnreadItemIndex(int default_row, int max_row) const; |
| 64 | |
| 65 | bool filterAcceptsMessage(int msg_row_index) const; |
| 66 | |
| 67 | virtual bool lessThan(const QModelIndex& left, const QModelIndex& right) const; |
| 68 | virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const; |
| 69 | |
| 70 | // Source model pointer. |
| 71 | MessagesModel* m_sourceModel; |
| 72 | MessageListFilter m_filter; |
| 73 | QMap<MessageListFilter, std::function<bool(int)>> m_filters; |
| 74 | QList<MessageListFilter> m_filterKeys; |
| 75 | int m_additionalArticleId; |
| 76 | }; |
| 77 | |
| 78 | Q_DECLARE_METATYPE(MessagesProxyModel::MessageListFilter) |
| 79 | |
| 80 | #endif // MESSAGESPROXYMODEL_H |