File indexing completed on 2024-05-12 15:45:39
0001 /* 0002 SPDX-FileCopyrightText: 2008 Andreas Pakulat <apaku@gmx.de> 0003 SPDX-FileCopyrightText: 2008-2018 Dominik Haumann <dhaumann@kde.org> 0004 SPDX-FileCopyrightText: 2017-2018 Friedrich W. H. Kossebau <kossebau@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #ifndef KTEXTEDITOR_ANNOTATIONINTERFACE_H 0010 #define KTEXTEDITOR_ANNOTATIONINTERFACE_H 0011 0012 #include <ktexteditor_export.h> 0013 0014 #include <QObject> 0015 0016 class QMenu; 0017 0018 namespace KTextEditor 0019 { 0020 class View; 0021 class AbstractAnnotationItemDelegate; 0022 0023 /** 0024 * \brief An model for providing line annotation information 0025 * 0026 * \section annomodel_intro Introduction 0027 * 0028 * AnnotationModel is a model-like interface that can be implemented 0029 * to provide annotation information for each line in a document. It provides 0030 * means to retrieve several kinds of data for a given line in the document. 0031 * 0032 * \section annomodel_impl Implementing a AnnotationModel 0033 * 0034 * The public interface of this class is loosely based on the QAbstractItemModel 0035 * interfaces. It only has a single method to override which is the \ref data() 0036 * method to provide the actual data for a line and role combination. 0037 * 0038 * \since 4.1 0039 * \see KTextEditor::AnnotationInterface, KTextEditor::AnnotationViewInterface 0040 */ 0041 class KTEXTEDITOR_EXPORT AnnotationModel : public QObject 0042 { 0043 Q_OBJECT 0044 public: 0045 ~AnnotationModel() override 0046 { 0047 } 0048 0049 enum { GroupIdentifierRole = Qt::UserRole }; 0050 // KF6: add AnnotationModelUserRole = Qt::UserRole + 0x100 0051 0052 /** 0053 * data() is used to retrieve the information needed to present the 0054 * annotation information from the annotation model. The provider 0055 * should return useful information for the line and the data role. 0056 * 0057 * The following roles are supported: 0058 * - Qt::DisplayRole - a short display text to be placed in the border 0059 * - Qt::TooltipRole - a tooltip information, longer text possible 0060 * - Qt::BackgroundRole - a brush to be used to paint the background on the border 0061 * - Qt::ForegroundRole - a brush to be used to paint the text on the border 0062 * - AnnotationModel::GroupIdentifierRole - a string which identifies a 0063 * group of items which will be highlighted on mouseover; return the same 0064 * string for all items in a group (KDevelop uses a VCS revision number, for example) 0065 * 0066 * 0067 * \param line the line for which the data is to be retrieved 0068 * \param role the role to identify which kind of annotation is to be retrieved 0069 * 0070 * \returns a QVariant that contains the data for the given role. 0071 */ 0072 virtual QVariant data(int line, Qt::ItemDataRole role) const = 0; // KF6: use int for role 0073 0074 Q_SIGNALS: 0075 /** 0076 * The model should emit the signal reset() when the text of almost all 0077 * lines changes. In most cases it is enough to call lineChanged(). 0078 * 0079 * \note Kate Part implementation details: Whenever reset() is emitted Kate 0080 * Part iterates over all lines of the document. Kate Part searches 0081 * for the longest text to determine the annotation border's width. 0082 * 0083 * \see lineChanged() 0084 */ 0085 void reset(); 0086 0087 /** 0088 * The model should emit the signal lineChanged() when a line has to be 0089 * updated. 0090 * 0091 * \note Kate Part implementation details: lineChanged() repaints the whole 0092 * annotation border automatically. 0093 */ 0094 void lineChanged(int line); 0095 }; 0096 0097 /** 0098 * \class AnnotationInterface annotationinterface.h <KTextEditor/AnnotationInterface> 0099 * 0100 * \brief A Document extension interface for handling Annotation%s 0101 * 0102 * \ingroup kte_group_doc_extensions 0103 * 0104 * \section annoiface_intro Introduction 0105 * 0106 * The AnnotationInterface is designed to provide line annotation information 0107 * for a document. This interface provides means to associate a document with a 0108 * annotation model, which provides some annotation information for each line 0109 * in the document. 0110 * 0111 * Setting a model for a Document makes the model data available for all views. 0112 * If you only want to provide annotations in exactly one view, you can use 0113 * the AnnotationViewInterface directly. See the AnnotationViewInterface for 0114 * further details. To summarize, the two use cases are 0115 * - (1) show annotations in all views. This means you set an AnnotationModel 0116 * with this interface, and then call setAnnotationBorderVisible() for 0117 * each view. 0118 * - (2) show annotations only in one view. This means to \e not use this 0119 * interface. Instead, use the AnnotationViewInterface, which inherits 0120 * this interface. This means you set a model for the specific View. 0121 * 0122 * If you set a model to the Document \e and the View, the View's model has 0123 * higher priority. 0124 * 0125 * \section annoiface_access Accessing the AnnotationInterface 0126 * 0127 * The AnnotationInterface is an extension interface for a Document, i.e. the 0128 * Document inherits the interface \e provided that the 0129 * used KTextEditor library implements the interface. Use qobject_cast to 0130 * access the interface: 0131 * \code 0132 * // document is of type KTextEditor::Document* 0133 * auto iface = qobject_cast<KTextEditor::AnnotationInterface*>(document); 0134 * 0135 * if (iface) { 0136 * // the implementation supports the interface 0137 * // do stuff 0138 * } else { 0139 * // the implementation does not support the interface 0140 * } 0141 * \endcode 0142 * 0143 * \section annoiface_usage Using the AnnotationInterface 0144 * 0145 * \since 4.1 0146 * \see KTextEditor::AnnotationModel, KTextEditor::AnnotationViewInterface 0147 */ 0148 class KTEXTEDITOR_EXPORT AnnotationInterface 0149 { 0150 public: 0151 virtual ~AnnotationInterface() 0152 { 0153 } 0154 0155 /** 0156 * Sets a new \ref AnnotationModel for this document to provide 0157 * annotation information for each line. 0158 * 0159 * \param model the new AnnotationModel 0160 */ 0161 virtual void setAnnotationModel(AnnotationModel *model) = 0; 0162 0163 /** 0164 * returns the currently set \ref AnnotationModel or 0 if there's none 0165 * set 0166 * @returns the current \ref AnnotationModel 0167 */ 0168 virtual AnnotationModel *annotationModel() const = 0; 0169 }; 0170 0171 /** 0172 * \brief Annotation interface for the View 0173 * 0174 * \ingroup kte_group_view_extensions 0175 * 0176 * \section annoview_intro Introduction 0177 * 0178 * The AnnotationViewInterface allows to do two things: 0179 * - (1) show/hide the annotation border along with the possibility to add actions 0180 * into its context menu. 0181 * - (2) set a separate AnnotationModel for the View: Note that this interface 0182 * inherits the AnnotationInterface. 0183 * 0184 * For a more detailed explanation about whether you want an AnnotationModel 0185 * in the Document or the View, read the detailed documentation about the 0186 * AnnotationInterface. 0187 * 0188 * \section annoview_access Accessing the AnnotationViewInterface 0189 * 0190 * The AnnotationViewInterface is an extension interface for a 0191 * View, i.e. the View inherits the interface \e provided that the 0192 * used KTextEditor library implements the interface. Use qobject_cast to 0193 * access the interface: 0194 * \code 0195 * // view is of type KTextEditor::View* 0196 * auto iface = qobject_cast<KTextEditor::AnnotationViewInterface*>(view); 0197 * 0198 * if (iface) { 0199 * // the implementation supports the interface 0200 * // do stuff 0201 * iface->setAnnotationBorderVisible(true); 0202 * } else { 0203 * // the implementation does not support the interface 0204 * } 0205 * \endcode 0206 * 0207 * \since 4.1 0208 */ 0209 class KTEXTEDITOR_EXPORT AnnotationViewInterface : public AnnotationInterface 0210 { 0211 public: 0212 ~AnnotationViewInterface() override 0213 { 0214 } 0215 0216 /** 0217 * This function can be used to show or hide the annotation border 0218 * The annotation border is hidden by default. 0219 * 0220 * @param visible if \e true the annotation border is shown, otherwise hidden 0221 */ 0222 virtual void setAnnotationBorderVisible(bool visible) = 0; 0223 0224 /** 0225 * Checks whether the View's annotation border is visible. 0226 */ 0227 virtual bool isAnnotationBorderVisible() const = 0; 0228 0229 // 0230 // SIGNALS!!! 0231 // 0232 public: 0233 /** 0234 * This signal is emitted before a context menu is shown on the annotation 0235 * border for the given line and view. 0236 * 0237 * \note Kate Part implementation detail: In Kate Part, the menu has an 0238 * entry to hide the annotation border. 0239 * 0240 * \param view the view that the annotation border belongs to 0241 * \param menu the context menu that will be shown 0242 * \param line the annotated line for which the context menu is shown 0243 */ 0244 virtual void annotationContextMenuAboutToShow(KTextEditor::View *view, QMenu *menu, int line) = 0; 0245 0246 /** 0247 * This signal is emitted when an entry on the annotation border was activated, 0248 * for example by clicking or double-clicking it. This follows the KDE wide 0249 * setting for activation via click or double-clcik 0250 * 0251 * \param view the view to which the activated border belongs to 0252 * \param line the document line that the activated position belongs to 0253 */ 0254 virtual void annotationActivated(KTextEditor::View *view, int line) = 0; 0255 0256 /** 0257 * This signal is emitted when the annotation border is shown or hidden. 0258 * 0259 * \param view the view to which the border belongs to 0260 * \param visible the current visibility state 0261 */ 0262 virtual void annotationBorderVisibilityChanged(KTextEditor::View *view, bool visible) = 0; 0263 }; 0264 0265 /** 0266 * \brief Annotation interface for the View, version 2 0267 * 0268 * \ingroup kte_group_view_extensions 0269 * 0270 * \section annoview_intro Introduction 0271 * 0272 * The AnnotationViewInterfaceV2 allows to do the same as AnnotationViewInterface 0273 * and additionally 0274 * - (1) set a custom AbstractAnnotationItemDelegate for the View. 0275 * 0276 * For a more detailed explanation about whether you want to set a custom 0277 * delegate for rendering the annotations, read the detailed documentation about the 0278 * AbstractAnnotationItemDelegate. 0279 * 0280 * \section annoview_access Accessing the AnnotationViewInterfaceV2 0281 * 0282 * The AnnotationViewInterfaceV2 is an extension interface for a 0283 * View, i.e. the View inherits the interface \e provided that the 0284 * used KTextEditor library implements the interface. Use qobject_cast to 0285 * access the interface: 0286 * \code 0287 * // view is of type KTextEditor::View* 0288 * auto iface = qobject_cast<KTextEditor::AnnotationViewInterfaceV2*>(view); 0289 * 0290 * if (iface) { 0291 * // the implementation supports the interface 0292 * // do stuff 0293 * iface->setAnnotationItemDelegate(myDelegate); 0294 * iface->setAnnotationUniformItemSizes(true); 0295 * } else { 0296 * // the implementation does not support the interface 0297 * } 0298 * \endcode 0299 * 0300 * \since 5.53 0301 */ 0302 class KTEXTEDITOR_EXPORT AnnotationViewInterfaceV2 : public AnnotationViewInterface 0303 { 0304 // KF6: Merge KTextEditor::AnnotationViewInterfaceV2 into KTextEditor::AnnotationViewInterface (kossebau) 0305 public: 0306 ~AnnotationViewInterfaceV2() override 0307 { 0308 } 0309 0310 /** 0311 * Sets the AbstractAnnotationItemDelegate for this view and the model 0312 * to provide custom rendering of annotation information for each line. 0313 * Ownership is not transferred. 0314 * 0315 * \param delegate the new AbstractAnnotationItemDelegate, or \c nullptr to reset to the default delegate 0316 */ 0317 virtual void setAnnotationItemDelegate(KTextEditor::AbstractAnnotationItemDelegate *delegate) = 0; 0318 0319 /** 0320 * Returns the currently used AbstractAnnotationItemDelegate 0321 * 0322 * @returns the current AbstractAnnotationItemDelegate 0323 */ 0324 virtual KTextEditor::AbstractAnnotationItemDelegate *annotationItemDelegate() const = 0; 0325 0326 /** 0327 * This function can be used to declare whether it is known that the annotation items 0328 * rendered by the set delegate all have the same size. 0329 * This enables the view to do some optimizations for performance purposes. 0330 * 0331 * By default the value of this property is \c false . 0332 * 0333 * @param uniformItemSizes if \c true the annotation items are considered to all have the same size 0334 */ 0335 virtual void setAnnotationUniformItemSizes(bool uniformItemSizes) = 0; 0336 0337 /** 0338 * Checks whether the annotation items all have the same size. 0339 */ 0340 virtual bool uniformAnnotationItemSizes() const = 0; 0341 }; 0342 0343 } 0344 0345 Q_DECLARE_INTERFACE(KTextEditor::AnnotationInterface, "org.kde.KTextEditor.AnnotationInterface") 0346 Q_DECLARE_INTERFACE(KTextEditor::AnnotationViewInterface, "org.kde.KTextEditor.AnnotationViewInterface") 0347 Q_DECLARE_INTERFACE(KTextEditor::AnnotationViewInterfaceV2, "org.kde.KTextEditor.AnnotationViewInterfaceV2") 0348 0349 #endif