File indexing completed on 2024-04-28 05:35:27
0001 /* 0002 SPDX-FileCopyrightText: 2004 Esben Mose Hansen <kde@mosehansen.dk> 0003 SPDX-FileCopyrightText: Andrew Stanley-Jones <asj@cban.com> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 #pragma once 0008 0009 #include <QByteArray> 0010 #include <QHash> 0011 #include <QObject> 0012 #include <memory> 0013 0014 class HistoryItem; 0015 class HistoryModel; 0016 class QAction; 0017 0018 class History : public QObject 0019 { 0020 Q_OBJECT 0021 public: 0022 explicit History(QObject *parent); 0023 ~History() override; 0024 0025 /** 0026 * Inserts item into clipboard history top 0027 * if duplicate entry exist, the older duplicate is deleted. 0028 * The duplicate concept is "deep", so that two text string 0029 * are considerd duplicate if identical. 0030 */ 0031 void insert(const std::shared_ptr<HistoryItem> &item); 0032 0033 /** 0034 * Inserts items into clipboard without any checks 0035 * Used when restoring a saved history and internally. 0036 * Don't use this unless you're reasonable the list 0037 * should be reset. 0038 */ 0039 void clearAndBatchInsert(const QList<std::shared_ptr<HistoryItem>> &items); 0040 0041 /** 0042 * Remove (first) history item equal to item from history 0043 */ 0044 void remove(const std::shared_ptr<const HistoryItem> &item); 0045 0046 /** 0047 * Traversal: Get first item 0048 */ 0049 std::shared_ptr<const HistoryItem> first() const; 0050 0051 /** 0052 * Get item identified by uuid 0053 */ 0054 std::shared_ptr<const HistoryItem> find(const QByteArray &uuid) const; 0055 0056 /** 0057 * @return next item in cycle, or null if at end 0058 */ 0059 std::shared_ptr<const HistoryItem> nextInCycle() const; 0060 0061 /** 0062 * @return previous item in cycle, or null if at top 0063 */ 0064 std::shared_ptr<const HistoryItem> prevInCycle() const; 0065 0066 /** 0067 * True if no history items 0068 */ 0069 bool empty() const; 0070 0071 /** 0072 * Set maximum history size 0073 */ 0074 void setMaxSize(unsigned max_size); 0075 0076 /** 0077 * Get the maximum history size 0078 */ 0079 unsigned maxSize() const; 0080 0081 /** 0082 * returns true if the user has selected the top item 0083 */ 0084 bool topIsUserSelected() 0085 { 0086 return m_topIsUserSelected; 0087 } 0088 0089 /** 0090 * Cycle to next item 0091 */ 0092 void cycleNext(); 0093 0094 /** 0095 * Cycle to prev item 0096 */ 0097 void cyclePrev(); 0098 0099 HistoryModel *model() 0100 { 0101 return m_model; 0102 } 0103 0104 public Q_SLOTS: 0105 /** 0106 * move the history in position pos to top 0107 */ 0108 void slotMoveToTop(QAction *action); 0109 0110 /** 0111 * move the history in position pos to top 0112 */ 0113 void slotMoveToTop(const QByteArray &uuid); 0114 0115 /** 0116 * Clear history 0117 */ 0118 void slotClear(); 0119 0120 Q_SIGNALS: 0121 void changed(); 0122 0123 /** 0124 * Emitted when the first history item has changed. 0125 */ 0126 void topChanged(); 0127 0128 void topIsUserSelectedSet(); 0129 0130 private: 0131 /** 0132 * True if the top is selected by the user 0133 */ 0134 bool m_topIsUserSelected; 0135 0136 HistoryModel *m_model; 0137 0138 QByteArray m_cycleStartUuid; 0139 };