File indexing completed on 2024-04-28 05:35:28

0001 /*
0002     SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #pragma once
0007 
0008 #include <QAbstractListModel>
0009 #include <QRecursiveMutex>
0010 #include <memory>
0011 
0012 class HistoryItem;
0013 
0014 class HistoryModel : public QAbstractListModel
0015 {
0016     Q_OBJECT
0017 public:
0018     enum RoleType {
0019         HistoryItemConstPtrRole = Qt::UserRole,
0020         UuidRole,
0021         TypeRole,
0022         Base64UuidRole,
0023         TypeIntRole,
0024     };
0025     Q_ENUM(RoleType)
0026 
0027     explicit HistoryModel(QObject *parent = nullptr);
0028     ~HistoryModel() override;
0029     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0030     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0031     bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
0032     QHash<int, QByteArray> roleNames() const override;
0033     bool remove(const QByteArray &uuid);
0034 
0035     int maxSize() const;
0036     void setMaxSize(int size);
0037 
0038     bool displayImages() const;
0039     void setDisplayImages(bool show);
0040 
0041     void clear();
0042     void moveToTop(const QByteArray &uuid);
0043     void moveTopToBack();
0044     void moveBackToTop();
0045 
0046     QModelIndex indexOf(const QByteArray &uuid) const;
0047     QModelIndex indexOf(const HistoryItem *item) const;
0048 
0049     void insert(const std::shared_ptr<HistoryItem> &item);
0050     void clearAndBatchInsert(const QList<std::shared_ptr<HistoryItem>> &items);
0051 
0052     QRecursiveMutex *mutex()
0053     {
0054         return &m_mutex;
0055     }
0056 
0057 private:
0058     void moveToTop(int row);
0059     QList<std::shared_ptr<HistoryItem>> m_items;
0060     int m_maxSize;
0061     bool m_displayImages;
0062     QRecursiveMutex m_mutex;
0063 };
0064 
0065 inline int HistoryModel::maxSize() const
0066 {
0067     return m_maxSize;
0068 }
0069 
0070 inline bool HistoryModel::displayImages() const
0071 {
0072     return m_displayImages;
0073 }
0074 
0075 inline void HistoryModel::setDisplayImages(bool show)
0076 {
0077     m_displayImages = show;
0078 }