File indexing completed on 2024-06-23 04:58:51

0001 /*
0002    SPDX-FileCopyrightText: 2020 David Faure <faure@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "delegateutils/textselectionimpl.h"
0010 #include "libruqolawidgets_private_export.h"
0011 #include "messagedelegatehelperbase.h"
0012 #include "messages/messageattachment.h"
0013 
0014 #include <QPersistentModelIndex>
0015 #include <QSize>
0016 
0017 class QTextDocument;
0018 class QPainter;
0019 class QRect;
0020 class QModelIndex;
0021 class QMouseEvent;
0022 class QStyleOptionViewItem;
0023 class QHelpEvent;
0024 
0025 class Message;
0026 class QListView;
0027 class RocketChatAccount;
0028 class QMenu;
0029 class LIBRUQOLAWIDGETS_TESTS_EXPORT MessageAttachmentDelegateHelperBase : public MessageDelegateHelperBase
0030 {
0031 public:
0032     explicit MessageAttachmentDelegateHelperBase(RocketChatAccount *account, QListView *view, TextSelectionImpl *textSelectionImpl);
0033     ~MessageAttachmentDelegateHelperBase() override;
0034 
0035     virtual void
0036     draw(const MessageAttachment &msgAttach, QPainter *painter, QRect attachmentsRect, const QModelIndex &index, const QStyleOptionViewItem &option) const = 0;
0037     [[nodiscard]] virtual QSize
0038     sizeHint(const MessageAttachment &msgAttach, const QModelIndex &index, int maxWidth, const QStyleOptionViewItem &option) const = 0;
0039     virtual bool handleMouseEvent(const MessageAttachment &msgAttach,
0040                                   QMouseEvent *mouseEvent,
0041                                   QRect attachmentsRect,
0042                                   const QStyleOptionViewItem &option,
0043                                   const QModelIndex &index);
0044 
0045     [[nodiscard]] bool
0046     maybeStartDrag(const MessageAttachment &msgAttach, QMouseEvent *event, QRect attachmentsRect, const QStyleOptionViewItem &option, const QModelIndex &index);
0047 
0048     [[nodiscard]] virtual bool
0049     handleHelpEvent(QHelpEvent *helpEvent, QRect messageRect, const MessageAttachment &msgAttach, const QStyleOptionViewItem &option);
0050 
0051     [[nodiscard]] QString urlAt(const QStyleOptionViewItem &option, const MessageAttachment &msgAttach, QRect attachmentsRect, QPoint pos);
0052 
0053     [[nodiscard]] virtual bool contextMenu(const QPoint &pos,
0054                                            const QPoint &globalPos,
0055                                            const MessageAttachment &msgAttach,
0056                                            QRect attachmentsRect,
0057                                            const QStyleOptionViewItem &option,
0058                                            QMenu *menu);
0059 
0060 protected:
0061     [[nodiscard]] virtual QPoint
0062     adaptMousePosition(const QPoint &pos, const MessageAttachment &msgAttach, QRect attachmentsRect, const QStyleOptionViewItem &option) = 0;
0063 
0064     [[nodiscard]] int
0065     charPosition(const QTextDocument *doc, const MessageAttachment &msgAttach, QRect attachmentsRect, const QPoint &pos, const QStyleOptionViewItem &option);
0066     void drawDescription(const MessageAttachment &msgAttach,
0067                          QRect messageRect,
0068                          QPainter *painter,
0069                          int topPos,
0070                          const QModelIndex &index,
0071                          const QStyleOptionViewItem &option) const;
0072 
0073     /**
0074      * Creates (or retrieves from a cache) the QTextDocument for a given @p index.
0075      * @param width The width for layouting that QTextDocument. -1 if no layouting is desired (e.g. for converting to text or HTML)
0076      * @param widget The view to update when fetching thread context on demand. nullptr if this isn't needed (e.g. from SelectionManager)
0077      * @return the QTextDocument. Ownership remains with the cache, don't delete it.
0078      */
0079     [[nodiscard]] QTextDocument *documentForAttachement(const MessageAttachment &msgAttach) const override;
0080 
0081     [[nodiscard]] MessageDelegateHelperBase::DocumentDescriptionInfo convertAttachmentToDocumentDescriptionInfo(const MessageAttachment &msgAttach,
0082                                                                                                                 int width) const;
0083 
0084     void drawTitle(const MessageAttachment &msgAttach, QPainter *painter);
0085     [[nodiscard]] MessageDelegateHelperBase::DocumentDescriptionInfo convertAttachmentToDocumentTitleInfo(const MessageAttachment &msgAttach, int width) const;
0086 
0087 private:
0088     enum class DocumentIdType : int {
0089         Unknown = 0,
0090         Title,
0091         Description,
0092     };
0093 
0094     [[nodiscard]] LIBRUQOLAWIDGETS_NO_EXPORT QString documendIdPrefix(MessageAttachmentDelegateHelperBase::DocumentIdType type) const;
0095     [[nodiscard]] LIBRUQOLAWIDGETS_NO_EXPORT MessageDelegateHelperBase::DocumentDescriptionInfo
0096     convertAttachmentToDocumentTypeInfo(DocumentIdType type, const MessageAttachment &msgAttach, int width) const;
0097     QPersistentModelIndex mCurrentIndex;
0098 };