File indexing completed on 2024-04-28 16:54:24

0001 /*
0002     SPDX-FileCopyrightText: 2004 Esben Mose Hansen <kde@mosehansen.dk>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #pragma once
0007 
0008 #include <QPixmap>
0009 
0010 class HistoryModel;
0011 class QString;
0012 class QMimeData;
0013 class QDataStream;
0014 
0015 class HistoryItem;
0016 typedef QSharedPointer<HistoryItem> HistoryItemPtr;
0017 typedef QSharedPointer<const HistoryItem> HistoryItemConstPtr;
0018 
0019 enum class HistoryItemType {
0020     Text,
0021     Image,
0022     Url,
0023 };
0024 
0025 /**
0026  * An entry in the clipboard history.
0027  */
0028 class HistoryItem
0029 {
0030 public:
0031     explicit HistoryItem(const QByteArray &uuid);
0032     virtual ~HistoryItem();
0033 
0034     /**
0035      * Returns the item type.
0036      */
0037     virtual HistoryItemType type() const = 0;
0038 
0039     /**
0040      * Return the current item as text
0041      * An image would be returned as a descriptive
0042      * text, such as 32x43 image.
0043      */
0044     virtual QString text() const = 0;
0045 
0046     /**
0047      * @return uuid of current item.
0048      */
0049     const QByteArray &uuid() const
0050     {
0051         return m_uuid;
0052     }
0053 
0054     /**
0055      * Return the current item as pixmap
0056      * A text would be returned as a null pixmap,
0057      * which is also the default implementation
0058      */
0059     inline virtual QPixmap image() const;
0060 
0061     /**
0062      * Returns a pointer to a QMimeData suitable for QClipboard::setMimeData().
0063      */
0064     virtual QMimeData *mimeData() const = 0;
0065 
0066     /**
0067      * Write object on datastream
0068      */
0069     virtual void write(QDataStream &stream) const = 0;
0070 
0071     /**
0072      * Equality.
0073      */
0074     virtual bool operator==(const HistoryItem &rhs) const = 0;
0075 
0076     /**
0077      * Create an HistoryItem from MimeSources (i.e., clipboard data)
0078      * returns null if create fails (e.g, unsupported mimetype)
0079      */
0080     static HistoryItemPtr create(const QMimeData *data);
0081 
0082     /**
0083      * Create an HistoryItem from data stream (i.e., disk file)
0084      * returns null if creation fails. In this case, the datastream
0085      * is left in an undefined state.
0086      */
0087     static HistoryItemPtr create(QDataStream &dataStream);
0088 
0089     /**
0090      * previous item's uuid
0091      * TODO: drop, only used in unit test now
0092      */
0093     QByteArray previous_uuid() const;
0094 
0095     /**
0096      * next item's uuid
0097      * TODO: drop, only used in unit test now
0098      */
0099     QByteArray next_uuid() const;
0100 
0101     void setModel(HistoryModel *model);
0102 
0103 protected:
0104     HistoryModel *m_model;
0105 
0106 private:
0107     QByteArray m_uuid;
0108 };
0109 
0110 inline QPixmap HistoryItem::image() const
0111 {
0112     return QPixmap();
0113 }
0114 
0115 inline QDataStream &operator<<(QDataStream &lhs, HistoryItem const *const rhs)
0116 {
0117     if (rhs) {
0118         rhs->write(lhs);
0119     }
0120     return lhs;
0121 }
0122 
0123 Q_DECLARE_METATYPE(HistoryItem *)
0124 Q_DECLARE_METATYPE(HistoryItemPtr)
0125 Q_DECLARE_METATYPE(HistoryItemConstPtr)
0126 Q_DECLARE_METATYPE(HistoryItemType)