1 /* 2 SPDX-FileCopyrightText: 2004 Esben Mose Hansen <kde@mosehansen.dk> 3 4 SPDX-License-Identifier: GPL-2.0-or-later 5 */ 6 #pragma once 7 8 #include <QPixmap> 9 10 class HistoryModel; 11 class QString; 12 class QMimeData; 13 class QDataStream; 14 15 class HistoryItem; 16 typedef QSharedPointer<HistoryItem> HistoryItemPtr; 17 typedef QSharedPointer<const HistoryItem> HistoryItemConstPtr; 18 /** 19 * An entry in the clipboard history. 20 */ 21 class HistoryItem 22 { 23 public: 24 explicit HistoryItem(const QByteArray &uuid); 25 virtual ~HistoryItem(); 26 27 /** 28 * Return the current item as text 29 * An image would be returned as a descriptive 30 * text, such as 32x43 image. 31 */ 32 virtual QString text() const = 0; 33 34 /** 35 * @return uuid of current item. 36 */ uuid()37 const QByteArray &uuid() const 38 { 39 return m_uuid; 40 } 41 42 /** 43 * Return the current item as pixmap 44 * A text would be returned as a null pixmap, 45 * which is also the default implementation 46 */ 47 inline virtual const QPixmap &image() const; 48 49 /** 50 * Returns a pointer to a QMimeData suitable for QClipboard::setMimeData(). 51 */ 52 virtual QMimeData *mimeData() const = 0; 53 54 /** 55 * Write object on datastream 56 */ 57 virtual void write(QDataStream &stream) const = 0; 58 59 /** 60 * Equality. 61 */ 62 virtual bool operator==(const HistoryItem &rhs) const = 0; 63 64 /** 65 * Create an HistoryItem from MimeSources (i.e., clipboard data) 66 * returns null if create fails (e.g, unsupported mimetype) 67 */ 68 static HistoryItemPtr create(const QMimeData *data); 69 70 /** 71 * Create an HistoryItem from data stream (i.e., disk file) 72 * returns null if creation fails. In this case, the datastream 73 * is left in an undefined state. 74 */ 75 static HistoryItemPtr create(QDataStream &dataStream); 76 77 /** 78 * previous item's uuid 79 * TODO: drop, only used in unit test now 80 */ 81 QByteArray previous_uuid() const; 82 83 /** 84 * next item's uuid 85 * TODO: drop, only used in unit test now 86 */ 87 QByteArray next_uuid() const; 88 89 void setModel(HistoryModel *model); 90 91 protected: 92 HistoryModel *m_model; 93 94 private: 95 QByteArray m_uuid; 96 }; 97 image()98inline const QPixmap &HistoryItem::image() const 99 { 100 static QPixmap nullPixmap; 101 return nullPixmap; 102 } 103 104 inline QDataStream &operator<<(QDataStream &lhs, HistoryItem const *const rhs) 105 { 106 if (rhs) { 107 rhs->write(lhs); 108 } 109 return lhs; 110 } 111 112 Q_DECLARE_METATYPE(HistoryItem *) 113 Q_DECLARE_METATYPE(HistoryItemPtr) 114 Q_DECLARE_METATYPE(HistoryItemConstPtr) 115