File indexing completed on 2024-05-12 15:45:39

0001 /*
0002     SPDX-FileCopyrightText: 2017-2018 Friedrich W. H. Kossebau <kossebau@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KTEXTEDITOR_ABSTRACTANNOTATIONITEMDELEGATE_H
0008 #define KTEXTEDITOR_ABSTRACTANNOTATIONITEMDELEGATE_H
0009 
0010 #include <ktexteditor_export.h>
0011 
0012 #include <QObject>
0013 #include <QStyleOption>
0014 
0015 class QHelpEvent;
0016 class QPoint;
0017 
0018 namespace KTextEditor
0019 {
0020 class AnnotationModel;
0021 class View;
0022 
0023 /**
0024  * \brief The style option set for an annotation item, as painted by AbstractAnnotationItemDelegate
0025  *
0026  * \since 5.53
0027  * \see KTextEditor::AbstractAnnotationItemDelegate
0028  */
0029 class KTEXTEDITOR_EXPORT StyleOptionAnnotationItem : public QStyleOption
0030 {
0031 public:
0032     // TODO: not sure what SO_Default implies, but no clue how to maintain a user type registry?
0033     enum StyleOptionType { Type = SO_Default };
0034     enum StyleOptionVersion { Version = 1 };
0035 
0036     /**
0037      * Index of the displayed line in the wrapped lines for the given real line
0038      */
0039     int wrappedLine = 0;
0040     /**
0041      * Number of wrapped lines for the given real line
0042      *
0043      * A value of 1 means no wrapping has happened and the real line is displayed as one line.
0044      */
0045     int wrappedLineCount = 1;
0046     /**
0047      * Index of the displayed line in the displayed lines for the same group
0048      */
0049     int visibleWrappedLineInGroup = 0;
0050 
0051     /**
0052      * The view where the annotation is shown
0053      *
0054      * There is always a view set.
0055      */
0056     KTextEditor::View *view = nullptr;
0057     /**
0058      * Recommended size for icons or other symbols that will be rendered by the delegate
0059      *
0060      * The default value is QSize(-1, -1).
0061      */
0062     QSize decorationSize;
0063     /**
0064      * The metrics of the font used for rendering the text document
0065      */
0066     QFontMetricsF contentFontMetrics;
0067 
0068     /**
0069      * Enum for describing the relative position of a real line in the row of consecutive
0070      * displayed lines which belong to the same group of annotation items
0071      * @see AnnotationItemGroupPositions
0072      */
0073     enum AnnotationItemGroupPosition {
0074         InvalidGroupPosition = 0, ///< Position not specified or not belonging to a group
0075         InGroup = 0x1 << 0, ///< Real line belongs to a group
0076         GroupBegin = 0x1 << 1, ///< Real line is first of consecutive lines from same group
0077         GroupEnd = 0x1 << 2, ///< Real line is last of consecutive lines from same group
0078     };
0079     /// Stores a combination of #AnnotationItemGroupPosition values.
0080     Q_DECLARE_FLAGS(AnnotationItemGroupPositions, AnnotationItemGroupPosition)
0081 
0082     /**
0083      * Relative position of the real line in the row of consecutive displayed lines
0084      * which belong to the same group of annotation items
0085      */
0086     AnnotationItemGroupPositions annotationItemGroupingPosition = InvalidGroupPosition;
0087 
0088 public:
0089     StyleOptionAnnotationItem();
0090     StyleOptionAnnotationItem(const StyleOptionAnnotationItem &other);
0091     StyleOptionAnnotationItem &operator=(const StyleOptionAnnotationItem &) = default;
0092 
0093 protected:
0094     explicit StyleOptionAnnotationItem(int version);
0095 };
0096 
0097 /**
0098  * \class AbstractAnnotationItemDelegate abstractannotationitemdelegate.h <KTextEditor/AbstractAnnotationItemDelegate>
0099  *
0100  * \brief A delegate for rendering line annotation information and handling events
0101  *
0102  * \section annodelegate_intro Introduction
0103  *
0104  * AbstractAnnotationItemDelegate is a base class that can be reimplemented
0105  * to customize the rendering of annotation information for each line in a document.
0106  * It provides also the hooks to define handling of help events like tooltip or of
0107  * the request for a context menu.
0108  *
0109  * \section annodelegate_impl Implementing an AbstractAnnotationItemDelegate
0110  *
0111  * The public interface of this class is loosely based on the QAbstractItemDelegate
0112  * interfaces. It has five methods to implement.
0113  *
0114  * \since 5.53
0115  * \see KTextEditor::AnnotationModel, KTextEditor::AnnotationViewInterfaceV2
0116  */
0117 class KTEXTEDITOR_EXPORT AbstractAnnotationItemDelegate : public QObject
0118 {
0119     Q_OBJECT
0120 
0121 protected:
0122     explicit AbstractAnnotationItemDelegate(QObject *parent = nullptr);
0123 
0124 public:
0125     ~AbstractAnnotationItemDelegate() override;
0126 
0127 public:
0128     /**
0129      * This pure abstract function must be reimplemented to provide custom rendering.
0130      * Use the painter and style option to render the annotation information for the line
0131      * specified by the arguments @p model and @p line.
0132      * @param painter the painter object
0133      * @param option the option object with the info needed for styling
0134      * @param model the annotation model providing the annotation information
0135      * @param line index of the real line the annotation information should be painted for
0136      *
0137      * Reimplement this in line with sizeHint().
0138      */
0139     virtual void paint(QPainter *painter, const KTextEditor::StyleOptionAnnotationItem &option, KTextEditor::AnnotationModel *model, int line) const = 0;
0140     /**
0141      * This pure abstract function must be reimplemented to provide custom rendering.
0142      * Use the style option to calculate the best size for the annotation information
0143      * for the line specified by the arguments @p model and @p line.
0144      * This should be the size for the display for a single displayed content line,
0145      * i.e. with no line wrapping or consecutive multiple annotation item of the same group assumed.
0146      *
0147      * @note If AnnotationViewInterfaceV2::uniformAnnotationItemSizes() is @c true for the view
0148      * this delegate is used by, it is assumed that the returned value is the same for
0149      * any line.
0150      *
0151      * @param option the option object with the info needed for styling
0152      * @param model the annotation model providing the annotation information
0153      * @param line index of the real line the annotation information should be painted for
0154      * @return best size for the annotation information
0155      *
0156      * Reimplement this in line with paint().
0157      */
0158     virtual QSize sizeHint(const KTextEditor::StyleOptionAnnotationItem &option, KTextEditor::AnnotationModel *model, int line) const = 0;
0159     /**
0160      * Whenever a help event occurs, this function is called with the event view option
0161      * and @p model and @p line specifying the item where the event occurs.
0162      * This pure abstract function must be reimplemented to provide custom tooltips.
0163      * @param event the help event
0164      * @param view the view for which the help event is requested
0165      * @param option the style option object with the info needed for styling, including the rect of the annotation
0166      * @param model the annotation model providing the annotation information
0167      * @param line index of the real line the annotation information should be painted for
0168      * @return @c true if the event could be handled (implies that the data obtained from the model had the required role), @c false otherwise
0169      *
0170      * Reimplement this in line with hideTooltip().
0171      */
0172     virtual bool helpEvent(QHelpEvent *event,
0173                            KTextEditor::View *view,
0174                            const KTextEditor::StyleOptionAnnotationItem &option,
0175                            KTextEditor::AnnotationModel *model,
0176                            int line) = 0;
0177     /**
0178      * This pure abstract function must be reimplemented to provide custom tooltips.
0179      * It is called whenever a possible still shown tooltip no longer is valid,
0180      * e.g. if the annotations have been hidden.
0181      * @param view the view for which the tooltip was requested
0182      *
0183      * Reimplement this in line with helpEvent().
0184      */
0185     virtual void hideTooltip(KTextEditor::View *view) = 0;
0186 
0187 Q_SIGNALS:
0188     /**
0189      * This signal must be emitted when the sizeHint() for @p model and @p line changed.
0190      * The view automatically connects to this signal and relayouts as necessary.
0191      * If AnnotationViewInterfaceV2::uniformAnnotationItemSizes is set on the view,
0192      * it is sufficient to emit sizeHintChanged only for one line.
0193      * @param model the annotation model providing the annotation information
0194      * @param line index of the real line the annotation information should be painted for
0195      */
0196     void sizeHintChanged(KTextEditor::AnnotationModel *model, int line);
0197 };
0198 
0199 }
0200 
0201 #endif