File indexing completed on 2024-05-19 04:00:03

0001 /*
0002     SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org>
0003 
0004     Documentation:
0005     SPDX-FileCopyrightText: 2005 Dominik Haumann <dhdev@gmx.de>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef KTEXTEDITOR_VIEW_H
0011 #define KTEXTEDITOR_VIEW_H
0012 
0013 #include <ktexteditor_export.h>
0014 
0015 // gui merging
0016 #include <KSyntaxHighlighting/Theme>
0017 #include <KXMLGUIClient>
0018 #include <ktexteditor/codecompletionmodel.h>
0019 
0020 // widget
0021 #include <QSet>
0022 #include <QWidget>
0023 
0024 class QMenu;
0025 class QScrollBar;
0026 class KConfigGroup;
0027 
0028 namespace KSyntaxHighlighting
0029 {
0030 class Theme;
0031 }
0032 
0033 namespace KTextEditor
0034 {
0035 class Document;
0036 class MainWindow;
0037 class ViewPrivate;
0038 class Attribute;
0039 class AttributeBlock;
0040 class Range;
0041 class Cursor;
0042 class AnnotationModel;
0043 class AbstractAnnotationItemDelegate;
0044 class InlineNoteProvider;
0045 class TextHintProvider;
0046 class CodeCompletionModel;
0047 
0048 /**
0049  * \class View view.h <KTextEditor/View>
0050  *
0051  * \brief A text widget with KXMLGUIClient that represents a Document.
0052  *
0053  * Topics:
0054  *  - \ref view_intro
0055  *  - \ref view_hook_into_gui
0056  *  - \ref view_selection
0057  *  - \ref view_cursors
0058  *  - \ref view_mouse_tracking
0059  *  - \ref view_modes
0060  *  - \ref view_config
0061  *  - \ref view_annoview
0062  *  - \ref view_inlinenote
0063  *  - \ref view_texthint
0064  *  - \ref view_compiface
0065  *
0066  * \section view_intro Introduction
0067  *
0068  * The View class represents a single view of a KTextEditor::Document,
0069  * get the document on which the view operates with document().
0070  * A view provides both the graphical representation of the text and the
0071  * KXMLGUIClient for the actions. The view itself does not provide
0072  * text manipulation, use the methods from the Document instead. The only
0073  * method to insert text is insertText(), which inserts the given text
0074  * at the current cursor position and emits the signal textInserted().
0075  *
0076  * Usually a view is created by using Document::createView().
0077  * Furthermore a view can have a context menu. Set it with setContextMenu()
0078  * and get it with contextMenu().
0079  *
0080  * \section view_hook_into_gui Merging the View's GUI
0081  *
0082  * A View is derived from the class KXMLGUIClient, so its GUI elements (like
0083  * menu entries and toolbar items) can be merged into the application's GUI
0084  * (or into a KXMLGUIFactory) by calling
0085  * \code
0086  * // view is of type KTextEditor::View*
0087  * mainWindow()->guiFactory()->addClient( view );
0088  * \endcode
0089  * You can add only one view as client, so if you have several views, you first
0090  * have to remove the current view, and then add the new one, like this
0091  * \code
0092  * mainWindow()->guiFactory()->removeClient( currentView );
0093  * mainWindow()->guiFactory()->addClient( newView );
0094  * \endcode
0095  *
0096  * \section view_selection Text Selection
0097  *
0098  * As the view is a graphical text editor it provides \e normal and \e block
0099  * text selection. You can check with selection() whether a selection exists.
0100  * removeSelection() will remove the selection without removing the text,
0101  * whereas removeSelectionText() also removes both, the selection and the
0102  * selected text. Use selectionText() to get the selected text and
0103  * setSelection() to specify the selected text range. The signal
0104  * selectionChanged() is emitted whenever the selection changed.
0105  *
0106  * \section view_cursors Cursor Positions
0107  *
0108  * A view has one Cursor which represents a line/column tuple. Two different
0109  * kinds of cursor positions are supported: first is the \e real cursor
0110  * position where a \e tab character only counts one character. Second is the
0111  * \e virtual cursor position, where a \e tab character counts as many
0112  * spaces as defined. Get the real position with cursorPosition() and the
0113  * virtual position with cursorPositionVirtual(). Set the real cursor
0114  * position with setCursorPosition(). The signal cursorPositionChanged() is
0115  * emitted whenever the cursor position changed.
0116  *
0117  * Screen coordinates of the current text cursor position in pixels are obtained
0118  * through cursorPositionCoordinates(). Further conversion of screen pixel
0119  * coordinates and text cursor positions are provided by cursorToCoordinate()
0120  * and coordinatesToCursor().
0121  *
0122  * \section view_mouse_tracking Mouse Tracking
0123  *
0124  * It is possible to get notified via the signal mousePositionChanged() for
0125  * mouse move events, if mouseTrackingEnabled() returns \e true. Mouse tracking
0126  * can be turned on/off by calling setMouseTrackingEnabled(). If an editor
0127  * implementation does not support mouse tracking, mouseTrackingEnabled() will
0128  * always return \e false.
0129  *
0130  * \section view_modes Input/View Modes
0131  *
0132  * A view supports several input modes. Common input modes are
0133  * \e NormalInputMode and \e ViInputMode. Which input modes the editor supports depends on the
0134  * implementation. The getter viewInputMode() returns enum \InputMode representing the current mode.
0135  *
0136  * Input modes can have their own view modes. In case of default \e NormalInputMode those are
0137  * \e NormalModeInsert and \e NormalModeOverwrite. You can use viewMode() getter to obtain those.
0138  *
0139  * For viewMode() and viewInputMode() there are also variants with \e Human suffix, which
0140  * returns the human readable representation (i18n) usable for displaying in user interface.
0141  *
0142  * Whenever the input/view mode changes the signals
0143  * viewInputModeChanged()/viewModeChanged() are emitted.
0144  *
0145  * \section view_config View Config
0146  * Config provides methods to access and modify the low level config information for a given
0147  * View
0148  * KTextEditor::View has support for the following keys:
0149  *  - line-numbers [bool], show/hide line numbers
0150  *  - icon-bar [bool], show/hide icon bar
0151  *  - folding-bar [bool], show/hide the folding bar
0152  *  - folding-preview [bool], enable/disable folding preview when mouse hovers
0153  *    on folded region
0154  *  - dynamic-word-wrap [bool], enable/disable dynamic word wrap
0155  *  - background-color [QColor], read/set the default background color
0156  *  - selection-color [QColor], read/set the default color for selections
0157  *  - search-highlight-color [QColor], read/set the background color for search
0158  *  - replace-highlight-color [QColor], read/set the background color for replaces
0159  *  - default-mark-type [uint], read/set the default mark type
0160  *  - allow-mark-menu [bool], enable/disable the menu shown when right clicking
0161  *    on the left gutter. When disabled, click on the gutter will always set
0162  *    or clear the mark of default type.
0163  *  - icon-border-color [QColor] read/set the icon border color (on the left,
0164  *    with the line numbers)
0165  *  - folding-marker-color [QColor] read/set folding marker colors (in the icon border)
0166  *  - line-number-color [QColor] read/set line number colors (in the icon border)
0167  *  - current-line-number-color [QColor] read/set current line number color (in the icon border)
0168  *  - modification-markers [bool] read/set whether the modification markers are shown
0169  *  - word-count [bool] enable/disable the counting of words and characters in the statusbar
0170  *  - line-count [bool] show/hide the total number of lines in the status bar (@since 5.66)
0171  *  - scrollbar-minimap [bool] enable/disable scrollbar minimap
0172  *  - scrollbar-preview [bool] enable/disable scrollbar text preview on hover
0173  *  - font [QFont] change the font
0174  *  - theme [QString] change the theme
0175  *  - word-completion-minimal-word-length [int] minimal word length to trigger word completion
0176  *  - enter-to-insert-completion [bool] enable/disable whether pressing enter inserts completion
0177  *
0178  * You can retrieve the value of a config key using configValue() and set the value
0179  * for a config key using setConfigValue().
0180  *
0181  * \section view_annoview Annotation Interface
0182  *
0183  * The Annotation Interface allows to do these things:
0184  * - (1) show/hide the annotation border along with the possibility to add actions
0185  *       into its context menu.
0186  * - (2) set a separate AnnotationModel for the View: Note that this interface
0187  *       inherits the AnnotationInterface.
0188  * - (3) set a custom AbstractAnnotationItemDelegate for the View.
0189  *
0190  * For a more detailed explanation about whether you want to set a custom
0191  * delegate for rendering the annotations, read the detailed documentation about the
0192  * AbstractAnnotationItemDelegate.
0193  *
0194  * \section view_inlinenote Inline Notes
0195  *
0196  * The inline notes interface provides a way to render arbitrary things in
0197  * the text. The text layout of the line is adapted to create space for the
0198  * note. Possible applications include showing a name of a function parameter
0199  * in a function call or rendering a square with a color preview next to CSS
0200  * color property.
0201  *
0202  * \image html inlinenote.png "Inline note showing a CSS color preview"
0203  *
0204  * To register as inline note provider, call registerInlineNoteProvider() with
0205  * an instance that inherits InlineNoteProvider. Finally, make sure you remove
0206  * your inline note provider by calling unregisterInlineNoteProvider().
0207  *
0208  * \section view_texthint Introduction
0209  *
0210  * The text hint interface provides a way to show tool tips for text located
0211  * under the mouse. Possible applications include showing a value of a variable
0212  * when debugging an application, or showing a complete path of an include
0213  * directive.
0214  *
0215  * \image html texthint.png "Text hint showing the contents of a variable"
0216  *
0217  * To register as text hint provider, call registerTextHintProvider() with an
0218  * instance that inherits TextHintProvider. Finally, make sure you remove your
0219  * text hint provider by calling unregisterTextHintProvider().
0220  *
0221  * Text hints are shown after the user hovers with the mouse for a delay of
0222  * textHintDelay() milliseconds over the same word. To change the delay, call
0223  * setTextHintDelay().
0224  *
0225  * \section view_compiface Completion Interface
0226  *
0227  * The Completion Interface is designed to provide code completion
0228  * functionality for a KTextEditor::View. This interface provides the basic
0229  * mechanisms to display a list of completions, update this list according
0230  * to user input, and allow the user to select a completion.
0231  *
0232  * Essentially, this provides an item view for the available completions. In
0233  * order to use this interface, you will need to implement a
0234  * CodeCompletionModel that generates the relevant completions given the
0235  * current input.
0236  *
0237  * More information about interfaces for the view can be found in
0238  * \ref kte_group_view_extensions.
0239  *
0240  * \see KTextEditor::Document, KXMLGUIClient
0241  * \author Christoph Cullmann \<cullmann@kde.org\>
0242  */
0243 class KTEXTEDITOR_EXPORT View : public QWidget, public KXMLGUIClient
0244 {
0245     Q_OBJECT
0246 
0247 protected:
0248     /**
0249      * Constructor.
0250      *
0251      * Create a view attached to the widget \p parent.
0252      *
0253      * Pass it the internal implementation to store a d-pointer.
0254      *
0255      * \param impl d-pointer to use
0256      * \param parent parent widget
0257      * \see Document::createView()
0258      */
0259     View(ViewPrivate *impl, QWidget *parent);
0260 
0261 public:
0262     /**
0263      * Virtual destructor.
0264      */
0265     ~View() override;
0266 
0267     /*
0268      * Accessor for the document
0269      */
0270 public:
0271     /**
0272      * Get the view's \e document, that means the view is a view of the
0273      * returned document.
0274      * \return the view's document
0275      */
0276     virtual Document *document() const = 0;
0277 
0278     /*
0279      * General information about this view
0280      */
0281 public:
0282     /**
0283      * Possible input modes.
0284      * These correspond to various modes the text editor might be in.
0285      */
0286     enum InputMode {
0287         NormalInputMode = 0, /**< Normal Mode. */
0288         ViInputMode = 1 /**< Vi mode. The view will behave like the editor vi(m) */
0289     };
0290 
0291     /**
0292      * Possible view modes
0293      * These correspond to various modes the text editor might be in.
0294      */
0295     enum ViewMode {
0296         NormalModeInsert = 0, /**< Insert mode. Characters will be added. */
0297         NormalModeOverwrite = 1, /**< Overwrite mode. Characters will be replaced. */
0298 
0299         ViModeNormal = 10,
0300         ViModeInsert = 11,
0301         ViModeVisual = 12,
0302         ViModeVisualLine = 13,
0303         ViModeVisualBlock = 14,
0304         ViModeReplace = 15
0305     };
0306 
0307     /**
0308      * Possible line types
0309      * \since 5.33
0310      */
0311     enum LineType {
0312         RealLine = 0, /**< Real line */
0313         VisibleLine = 1 /**< Visible line. Line that is not folded. */
0314     };
0315     /**
0316      * Get the current view mode/state.
0317      * This can be used to detect the view's current mode. For
0318      * example \NormalInputMode, \ViInputMode or whatever other input modes are
0319      * supported. \see viewModeHuman() for translated version.
0320      * \return current view mode/state
0321      * \see viewModeChanged()
0322      */
0323     virtual ViewMode viewMode() const = 0;
0324 
0325     /**
0326      * Get the current view mode state.
0327      * This can be used to visually indicate the view's current mode, for
0328      * example \e INSERT mode, \e OVERWRITE mode or \e COMMAND mode - or
0329      * whatever other edit modes are supported. The string should be
0330      * translated (i18n), as this is a user aimed representation of the view
0331      * state, which should be shown in the GUI, for example in the status bar.
0332      * This string may be rich-text.
0333      * \return Human-readable version of the view mode state
0334      * \see viewModeChanged()
0335      */
0336     virtual QString viewModeHuman() const = 0;
0337 
0338     /**
0339      * Set the view's new input mode.
0340      * \param inputMode new InputMode value
0341      * \see viewInputMode()
0342      * @since 5.54
0343      */
0344     virtual void setViewInputMode(InputMode inputMode) = 0;
0345 
0346     /**
0347      * Get the view's current input mode.
0348      * The current mode can be \NormalInputMode and \ViInputMode.
0349      * For human translated version \see viewInputModeHuman.
0350      *
0351      * \return the current input mode of this view
0352      * \see viewInputModeChanged()
0353      */
0354     virtual InputMode viewInputMode() const = 0;
0355 
0356     /**
0357      * Get the view's current input mode in human readable form.
0358      * The string should be translated (i18n). For id like version \see viewInputMode
0359      *
0360      * \return the current input mode of this view in human readable form
0361      */
0362     virtual QString viewInputModeHuman() const = 0;
0363 
0364     /**
0365      * Get the view's main window, if any
0366      * \return the view's main window, will always return at least some non-nullptr dummy interface
0367      */
0368     virtual KTextEditor::MainWindow *mainWindow() const = 0;
0369 
0370     /*
0371      * SIGNALS
0372      * following signals should be emitted by the editor view
0373      */
0374 Q_SIGNALS:
0375     /**
0376      * This signal is emitted whenever the \p view gets the focus.
0377      * \param view view which gets focus
0378      * \see focusOut()
0379      */
0380     void focusIn(KTextEditor::View *view);
0381 
0382     /**
0383      * This signal is emitted whenever the \p view loses the focus.
0384      * \param view view which lost focus
0385      * \see focusIn()
0386      */
0387     void focusOut(KTextEditor::View *view);
0388 
0389     /**
0390      * This signal is emitted whenever the view mode of \p view changes.
0391      * \param view the view which changed its mode
0392      * \param mode new view mode
0393      * \see viewMode()
0394      */
0395     void viewModeChanged(KTextEditor::View *view, KTextEditor::View::ViewMode mode);
0396 
0397     /**
0398      * This signal is emitted whenever the \p view's input \p mode changes.
0399      * \param view view which changed its input mode
0400      * \param mode new input mode
0401      * \see viewInputMode()
0402      */
0403     void viewInputModeChanged(KTextEditor::View *view, KTextEditor::View::InputMode mode);
0404 
0405     /**
0406      * This signal is emitted from \p view whenever the users inserts \p text
0407      * at \p position, that means the user typed/pasted text.
0408      * \param view view in which the text was inserted
0409      * \param position position where the text was inserted
0410      * \param text the text the user has typed into the editor
0411      * \see insertText()
0412      */
0413     void textInserted(KTextEditor::View *view, KTextEditor::Cursor position, const QString &text);
0414 
0415     /*
0416      * Context menu handling
0417      */
0418 public:
0419     /**
0420      * Set a context menu for this view to \p menu.
0421      *
0422      * \note any previously assigned menu is not deleted.  If you are finished
0423      *       with the previous menu, you may delete it.
0424      *
0425      * \warning Use this with care! Plugin xml gui clients are not merged
0426      *          into this menu!
0427      * \warning !!!!!! DON'T USE THIS FUNCTION, UNLESS YOU ARE SURE YOU DON'T WANT PLUGINS TO WORK !!!!!!
0428      *
0429      * \param menu new context menu object for this view
0430      * \see contextMenu()
0431      */
0432     virtual void setContextMenu(QMenu *menu) = 0;
0433 
0434     /**
0435      * Get the context menu for this view. The return value can be NULL
0436      * if no context menu object was set and kxmlgui is not initialized yet.
0437      * If there is no user set menu, the kxmlgui menu is returned. Do not delete this menu, if
0438      * if it is the xmlgui menu.
0439      * \return context menu object
0440      * \see setContextMenu()
0441      */
0442     virtual QMenu *contextMenu() const = 0;
0443 
0444     /**
0445      * Populate \a menu with default text editor actions.  If \a menu is
0446      * null, a menu will be created with the view as its parent.
0447      *
0448      * \note to use this menu, you will next need to call setContextMenu(),
0449      *       as this does not assign the new context menu.
0450      *
0451      * \warning This contains only basic options from the editor component
0452      *          (katepart). Plugins are \p not merged/integrated into it!
0453      *          If you want to be a better citizen and take full advantage
0454      *          of KTextEditor plugins do something like:
0455      * \code
0456      * KXMLGUIClient* client = view;
0457      * // search parent XmlGuiClient
0458      * while (client->parentClient()) {
0459      *   client = client->parentClient();
0460      * }
0461      *
0462      * if (client->factory()) {
0463      *   const QList<QWidget*> menuContainers = client->factory()->containers("menu");
0464      *   for (QWidget *w : menuContainers) {
0465      *     if (w->objectName() == "ktexteditor_popup") {
0466      *       // do something with the menu (ie adding an onshow handler)
0467      *       break;
0468      *     }
0469      *   }
0470      * }
0471      * \endcode
0472      * \warning or simply use the aboutToShow, aboutToHide signals !!!!!
0473      *
0474      * \param menu the menu to be populated, or null to create a new menu.
0475      * \return the menu, whether created or passed initially
0476      */
0477     virtual QMenu *defaultContextMenu(QMenu *menu = nullptr) const = 0;
0478 
0479 Q_SIGNALS:
0480     /**
0481      * Signal which is emitted immediately prior to showing the current
0482      * context \a menu.
0483      */
0484     void contextMenuAboutToShow(KTextEditor::View *view, QMenu *menu);
0485 
0486     /*
0487      * Cursor handling
0488      */
0489 public:
0490     /**
0491      * Set the view's new cursor to \p position. A \e TAB character
0492      * is handled as only on character.
0493      * \param position new cursor position
0494      * \return \e true on success, otherwise \e false
0495      * \see cursorPosition()
0496      */
0497     virtual bool setCursorPosition(Cursor position) = 0;
0498 
0499     /**
0500      * Set the view's new cursors to \p positions. A \e TAB character
0501      * is handled as only on character.
0502      *
0503      * This allows to create multiple cursors in this view.
0504      *
0505      * The first passed position will be used for the primary cursor
0506      * just like if you would call \ref setCursorPosition.
0507      *
0508      * \param positions new cursor positions
0509      * \see cursorPositions()
0510      *
0511      * @since 5.95
0512      */
0513     void setCursorPositions(const QList<KTextEditor::Cursor> &positions);
0514 
0515     /**
0516      * Get the view's current cursor position. A \e TAB character is
0517      * handled as only one character.
0518      * \return current cursor position
0519      * \see setCursorPosition()
0520      */
0521     virtual Cursor cursorPosition() const = 0;
0522 
0523     /**
0524      * Get the view's current cursor positions. A \e TAB character is
0525      * handled as only one character.
0526      *
0527      * The returned vector contains the primary cursor as first element.
0528      *
0529      * @since 5.95
0530      *
0531      * \return all currently existing cursors
0532      */
0533     QList<KTextEditor::Cursor> cursorPositions() const;
0534 
0535     /**
0536      * Get the current \e virtual cursor position, \e virtual means the
0537      * tabulator character (TAB) counts \e multiple characters, as configured
0538      * by the user (e.g. one TAB is 8 spaces). The virtual cursor
0539      * position provides access to the user visible values of the current
0540      * cursor position.
0541      *
0542      * \return virtual cursor position
0543      * \see cursorPosition()
0544      */
0545     virtual Cursor cursorPositionVirtual() const = 0;
0546 
0547     /**
0548      * Get the screen coordinates (x, y) of the supplied \a cursor relative
0549      * to the view widget in pixels. Thus, (0, 0) represents the top left hand
0550      * of the view widget.
0551      *
0552      * To map pixel coordinates to a Cursor position (the reverse transformation)
0553      * use coordinatesToCursor().
0554      *
0555      * \param cursor cursor to determine coordinate for.
0556      * \return cursor screen coordinates relative to the view widget
0557      *
0558      * \see cursorPositionCoordinates(), coordinatesToCursor()
0559      */
0560     virtual QPoint cursorToCoordinate(KTextEditor::Cursor cursor) const = 0;
0561 
0562     /**
0563      * Get the screen coordinates (x, y) of the cursor position in pixels.
0564      * The returned coordinates are relative to the View such that (0, 0)
0565      * represents tht top-left corner of the View.
0566      *
0567      * If global screen coordinates are required, e.g. for showing a QToolTip,
0568      * convert the view coordinates to global screen coordinates as follows:
0569      * \code
0570      * QPoint viewCoordinates = view->cursorPositionCoordinates();
0571      * QPoint globalCoorinates = view->mapToGlobal(viewCoordinates);
0572      * \endcode
0573      * \return cursor screen coordinates
0574      * \see cursorToCoordinate(), coordinatesToCursor()
0575      */
0576     virtual QPoint cursorPositionCoordinates() const = 0;
0577 
0578     /**
0579      * Get the text-cursor in the document from the screen coordinates,
0580      * relative to the view widget.
0581      *
0582      * To map a cursor to pixel coordinates (the reverse transformation)
0583      * use cursorToCoordinate().
0584      *
0585      * \param coord coordinates relative to the view widget
0586      * \return cursor in the View, that points onto the character under
0587      *         the given coordinate. May be KTextEditor::Cursor::invalid().
0588      */
0589     virtual KTextEditor::Cursor coordinatesToCursor(const QPoint &coord) const = 0;
0590 
0591     /*
0592      * SIGNALS
0593      * following signals should be emitted by the editor view
0594      * if the cursor position changes
0595      */
0596 Q_SIGNALS:
0597     /**
0598      * This signal is emitted whenever the \p view's cursor position changed.
0599      * \param view view which emitted the signal
0600      * \param newPosition new position of the cursor (Kate will pass the real
0601      *        cursor position, not the virtual)
0602      * \see cursorPosition(), cursorPositionVirtual()
0603      */
0604     void cursorPositionChanged(KTextEditor::View *view, KTextEditor::Cursor newPosition);
0605 
0606     /**
0607      * This signal should be emitted whenever the \p view is scrolled vertically.
0608      * \param view view which emitted the signal
0609      * \param newPos the new scroll position
0610      */
0611     void verticalScrollPositionChanged(KTextEditor::View *view, KTextEditor::Cursor newPos);
0612 
0613     /**
0614      * This signal should be emitted whenever the \p view is scrolled horizontally.
0615      * \param view view which emitted the signal
0616      */
0617     void horizontalScrollPositionChanged(KTextEditor::View *view);
0618     /*
0619      * Mouse position
0620      */
0621 public:
0622     /**
0623      * Check, whether mouse tracking is enabled.
0624      *
0625      * Mouse tracking is required to have the signal mousePositionChanged()
0626      * emitted.
0627      * \return \e true, if mouse tracking is enabled, otherwise \e false
0628      * \see setMouseTrackingEnabled(), mousePositionChanged()
0629      */
0630     virtual bool mouseTrackingEnabled() const = 0;
0631 
0632     /**
0633      * Try to enable or disable mouse tracking according to \p enable.
0634      * The return value contains the state of mouse tracking \e after the
0635      * request. Mouse tracking is required to have the mousePositionChanged()
0636      * signal emitted.
0637      *
0638      * \note Implementation Notes: An implementation is not forced to support
0639      *       this, and should always return \e false if it does not have
0640      *       support.
0641      *
0642      * \param enable if \e true, try to enable mouse tracking, otherwise disable
0643      *        it.
0644      * \return the current state of mouse tracking
0645      * \see mouseTrackingEnabled(), mousePositionChanged()
0646      */
0647     virtual bool setMouseTrackingEnabled(bool enable) = 0;
0648 
0649 Q_SIGNALS:
0650     /**
0651      * This signal is emitted whenever the position of the mouse changes over
0652      * this \a view. If the mouse moves off the view, an invalid cursor position
0653      * should be emitted, i.e. Cursor::invalid().
0654      * \note If mouseTrackingEnabled() returns \e false, this signal is never
0655      *       emitted.
0656      * \param view view which emitted the signal
0657      * \param newPosition new position of the mouse or Cursor::invalid(), if the
0658      *        mouse moved out of the \p view.
0659      * \see mouseTrackingEnabled()
0660      */
0661     void mousePositionChanged(KTextEditor::View *view, KTextEditor::Cursor newPosition);
0662 
0663     /*
0664      * Selection methods.
0665      * This deals with text selection and copy&paste
0666      */
0667 public:
0668     /**
0669      * Set the view's selection to the range \p selection.
0670      * The old selection will be discarded.
0671      * \param range the range of the new selection
0672      * \return \e true on success, otherwise \e false (e.g. when the cursor
0673      *         range is invalid)
0674      * \see selectionRange(), selection()
0675      */
0676     virtual bool setSelection(Range range) = 0;
0677 
0678     /**
0679      * Set the view's selection to the range \p selection.
0680      * The old selection will be discarded.
0681      * \param ranges the ranges of the new selections
0682      * \see selectionRanges(), selection()
0683      *
0684      * @since 5.95
0685      */
0686     void setSelections(const QList<KTextEditor::Range> &ranges);
0687 
0688     /**
0689      * Query the view whether it has selected text, i.e. whether a selection
0690      * exists.
0691      * \return \e true if a text selection exists, otherwise \e false
0692      * \see setSelection(), selectionRange()
0693      */
0694     virtual bool selection() const = 0;
0695 
0696     /**
0697      * Get the range occupied by the current selection.
0698      * \return selection range, valid only if a selection currently exists.
0699      * \see setSelection()
0700      */
0701     virtual Range selectionRange() const = 0;
0702 
0703     /**
0704      * Get the ranges occupied by the current selections.
0705      * \return selection ranges, valid only if a selection currently exists.
0706      * \see setSelections()
0707      *
0708      * @since 5.95
0709      */
0710     QList<KTextEditor::Range> selectionRanges() const;
0711 
0712     /**
0713      * Get the view's selected text.
0714      * \return the selected text
0715      * \see setSelection()
0716      */
0717     virtual QString selectionText() const = 0;
0718 
0719     /**
0720      * Remove the view's current selection, \e without deleting the selected
0721      * text.
0722      * \return \e true on success, otherwise \e false
0723      * \see removeSelectionText()
0724      */
0725     virtual bool removeSelection() = 0;
0726 
0727     /**
0728      * Remove the view's current selection \e including the selected text.
0729      * \return \e true on success, otherwise \e false
0730      * \see removeSelection()
0731      */
0732     virtual bool removeSelectionText() = 0;
0733 
0734     /*
0735      * Blockselection stuff
0736      */
0737 public:
0738     /**
0739      * Set block selection mode to state \p on.
0740      * \param on if \e true, block selection mode is turned on, otherwise off
0741      * \return \e true on success, otherwise \e false
0742      * \see blockSelection()
0743      */
0744     virtual bool setBlockSelection(bool on) = 0;
0745 
0746     /**
0747      * Get the status of the selection mode. \e true indicates that block
0748      * selection mode is on. If this is \e true, selections applied via the
0749      * SelectionInterface are handled as block selections and the Copy&Paste
0750      * functions work on rectangular blocks of text rather than normal.
0751      * \return \e true, if block selection mode is enabled, otherwise \e false
0752      * \see setBlockSelection()
0753      */
0754     virtual bool blockSelection() const = 0;
0755 
0756     /*
0757      * SIGNALS
0758      * following signals should be emitted by the editor view for selection
0759      * handling.
0760      */
0761 Q_SIGNALS:
0762     /**
0763      * This signal is emitted whenever the \p view's selection changes.
0764      * \note If the mode switches from block selection to normal selection
0765      *       or vice versa this signal should also be emitted.
0766      * \param view view in which the selection changed
0767      * \see selection(), selectionRange(), selectionText()
0768      */
0769     void selectionChanged(KTextEditor::View *view);
0770 
0771 public:
0772     /**
0773      * This is a convenience function which inserts \p text at the view's
0774      * current cursor position. You do not necessarily need to reimplement
0775      * it, except you want to do some special things.
0776      * \param text Text to be inserted
0777      * \return \e true on success of insertion, otherwise \e false
0778      * \see textInserted()
0779      */
0780     virtual bool insertText(const QString &text);
0781 
0782     /**
0783      * Insert a template into the document. The template can have editable fields
0784      * which can be filled by the user. You can create editable fields
0785      * with ${fieldname}; multiple fields with the same name will have their
0786      * contents synchronized automatically, and only the first one is editable
0787      * in this case.
0788      * Fields can have a default value specified by writing ${fieldname=default}.
0789      * Note that `default' is a JavaScript expression and strings need to be quoted.
0790      * You can also provide a piece of JavaScript for more complex logic.
0791      * To create a field which provides text based on a JS function call and the values
0792      * of the other, editable fields, use the ${func()} syntax. func() must be a callable
0793      * object defined in @p script. You can pass arguments to the function by just
0794      * writing any constant expression or a field name.
0795      * \param insertPosition where to insert the template
0796      * \param templateString template to insert using the above syntax
0797      * \param script script with functions which can be used in @p templateScript
0798      * \return true on success, false if insertion failed (e.g. read-only mode)
0799      */
0800     bool insertTemplate(KTextEditor::Cursor insertPosition, const QString &templateString, const QString &script = QString());
0801 
0802     /**
0803      * Scroll view to cursor.
0804      *
0805      * \param cursor the cursor position to scroll to.
0806      *
0807      * \since 5.33
0808      */
0809     void setScrollPosition(KTextEditor::Cursor cursor);
0810 
0811     /**
0812      * Horizontally scroll view to position.
0813      *
0814      * \param x the pixel position to scroll to.
0815      *
0816      * \since 5.33
0817      */
0818     void setHorizontalScrollPosition(int x);
0819 
0820     /**
0821      * Get the cursor corresponding to the maximum position
0822      * the view can vertically scroll to.
0823      *
0824      * \return cursor position of the maximum vertical scroll position.
0825      *
0826      * \since 5.33
0827      */
0828     KTextEditor::Cursor maxScrollPosition() const;
0829 
0830     /**
0831      * Get the first displayed line in the view.
0832      *
0833      * \note If code is folded, many hundred lines can be
0834      * between firstDisplayedLine() and lastDisplayedLine().
0835      *
0836      * \param lineType if RealLine (the default), it returns the real line number
0837      * accounting for folded regions. In that case it walks over all folded
0838      * regions
0839      * O(n) for n == number of folded ranges
0840      * \return the first displayed line
0841      *
0842      * \see lastDisplayedLine()
0843      * \since 5.33
0844      */
0845     int firstDisplayedLine(LineType lineType = RealLine) const;
0846 
0847     /**
0848      * Get the last displayed line in the view.
0849      *
0850      * \note If code is folded, many hundred lines can be
0851      * between firstDisplayedLine() and lastDisplayedLine().
0852      *
0853      * \param lineType if RealLine (the default), it returns the real line number
0854      * accounting for folded regions. In that case it walks over all folded
0855      * regions
0856      * O(n) for n == number of folded ranges
0857      * \return the last displayed line
0858      *
0859      * \see firstDisplayedLine()
0860      * \since 5.33
0861      */
0862     int lastDisplayedLine(LineType lineType = RealLine) const;
0863 
0864     /**
0865      * Get the view's text area rectangle excluding border, scrollbars, etc.
0866      *
0867      * \return the view's text area rectangle
0868      *
0869      * \since 5.33
0870      */
0871     QRect textAreaRect() const;
0872 
0873     /**
0874      * \return The vertical scrollbar of this view
0875      * \since 6.0
0876      */
0877     virtual QScrollBar *verticalScrollBar() const = 0;
0878 
0879     /**
0880      * \return The horizontal scrollbar of this view
0881      * \since 6.0
0882      */
0883     virtual QScrollBar *horizontalScrollBar() const = 0;
0884 
0885 public:
0886     /**
0887      * Print the document. This should result in showing the print dialog.
0888      *
0889      * @returns true if document was printed
0890      */
0891     virtual bool print() = 0;
0892 
0893     /**
0894      * Shows the print preview dialog/
0895      */
0896     virtual void printPreview() = 0;
0897 
0898     /**
0899      * Is the status bar enabled?
0900      *
0901      * @return status bar enabled?
0902      */
0903     bool isStatusBarEnabled() const;
0904 
0905     /**
0906      * Show/hide the status bar of the view.
0907      * Per default, the status bar is enabled.
0908      *
0909      * @param enable should the status bar be enabled?
0910      */
0911     void setStatusBarEnabled(bool enable);
0912 
0913 Q_SIGNALS:
0914     /**
0915      * This signal is emitted whenever the status bar of \p view is toggled.
0916      *
0917      * @param enabled Whether the status bar is currently enabled or not
0918      */
0919     void statusBarEnabledChanged(KTextEditor::View *view, bool enabled);
0920 
0921 public:
0922     /**
0923      * Read session settings from the given \p config.
0924      *
0925      * Known flags:
0926      *  none atm
0927      *
0928      * \param config read the session settings from this KConfigGroup
0929      * \param flags additional flags
0930      * \see writeSessionConfig()
0931      */
0932     virtual void readSessionConfig(const KConfigGroup &config, const QSet<QString> &flags = QSet<QString>()) = 0;
0933 
0934     /**
0935      * Write session settings to the \p config.
0936      * See readSessionConfig() for more details.
0937      *
0938      * \param config write the session settings to this KConfigGroup
0939      * \param flags additional flags
0940      * \see readSessionConfig()
0941      */
0942     virtual void writeSessionConfig(KConfigGroup &config, const QSet<QString> &flags = QSet<QString>()) = 0;
0943 
0944 public:
0945     /**
0946      * Returns the attribute for the default style \p defaultStyle.
0947      * @param defaultStyle default style to get the attribute for
0948      * @see KTextEditor::Attribute
0949      */
0950     virtual QExplicitlySharedDataPointer<KTextEditor::Attribute> defaultStyleAttribute(KSyntaxHighlighting::Theme::TextStyle defaultStyle) const = 0;
0951 
0952     /**
0953      * Get the list of AttributeBlocks for a given \p line in the document.
0954      *
0955      * \return list of AttributeBlocks for given \p line.
0956      */
0957     virtual QList<KTextEditor::AttributeBlock> lineAttributes(int line) = 0;
0958 
0959 Q_SIGNALS:
0960     /**
0961      * This signal is emitted whenever the current view configuration is changed.
0962      *
0963      * \param view the view which's config has changed
0964      *
0965      * \since 5.79
0966      */
0967     void configChanged(KTextEditor::View *view);
0968 
0969     /**
0970      * View Config
0971      */
0972 public:
0973     /**
0974      * Get a list of all available keys.
0975      */
0976     virtual QStringList configKeys() const = 0;
0977     /**
0978      * Get a value for the \p key.
0979      */
0980     virtual QVariant configValue(const QString &key) = 0;
0981     /**
0982      * Set a the \p key's value to \p value.
0983      */
0984     virtual void setConfigValue(const QString &key, const QVariant &value) = 0;
0985 
0986     /**
0987      * View Annotation Interface
0988      */
0989 public:
0990     /**
0991      * Sets a new \ref AnnotationModel for this document to provide
0992      * annotation information for each line.
0993      *
0994      * \param model the new AnnotationModel
0995      */
0996     virtual void setAnnotationModel(AnnotationModel *model) = 0;
0997 
0998     /**
0999      * returns the currently set \ref AnnotationModel or 0 if there's none
1000      * set
1001      * @returns the current \ref AnnotationModel
1002      */
1003     virtual AnnotationModel *annotationModel() const = 0;
1004 
1005     /**
1006      * This function can be used to show or hide the annotation border
1007      * The annotation border is hidden by default.
1008      *
1009      * @param visible if \e true the annotation border is shown, otherwise hidden
1010      */
1011     virtual void setAnnotationBorderVisible(bool visible) = 0;
1012 
1013     /**
1014      * Checks whether the View's annotation border is visible.
1015      */
1016     virtual bool isAnnotationBorderVisible() const = 0;
1017 
1018     /**
1019      * Sets the AbstractAnnotationItemDelegate for this view and the model
1020      * to provide custom rendering of annotation information for each line.
1021      * Ownership is not transferred.
1022      *
1023      * \param delegate the new AbstractAnnotationItemDelegate, or \c nullptr to reset to the default delegate
1024      *
1025      * @since 6.0
1026      */
1027     virtual void setAnnotationItemDelegate(KTextEditor::AbstractAnnotationItemDelegate *delegate) = 0;
1028 
1029     /**
1030      * Returns the currently used AbstractAnnotationItemDelegate
1031      *
1032      * @returns the current AbstractAnnotationItemDelegate
1033      *
1034      * @since 6.0
1035      */
1036     virtual KTextEditor::AbstractAnnotationItemDelegate *annotationItemDelegate() const = 0;
1037 
1038     /**
1039      * This function can be used to declare whether it is known that the annotation items
1040      * rendered by the set delegate all have the same size.
1041      * This enables the view to do some optimizations for performance purposes.
1042      *
1043      * By default the value of this property is \c false .
1044      *
1045      * @param uniformItemSizes if \c true the annotation items are considered to all have the same size
1046      *
1047      * @since 6.0
1048      */
1049     virtual void setAnnotationUniformItemSizes(bool uniformItemSizes) = 0;
1050 
1051     /**
1052      * Checks whether the annotation items all have the same size.
1053      *
1054      * @since 6.0
1055      */
1056     virtual bool uniformAnnotationItemSizes() const = 0;
1057 
1058 Q_SIGNALS:
1059     /**
1060      * This signal is emitted before a context menu is shown on the annotation
1061      * border for the given line and view.
1062      *
1063      * \note Kate Part implementation detail: In Kate Part, the menu has an
1064      *       entry to hide the annotation border.
1065      *
1066      * \param view the view that the annotation border belongs to
1067      * \param menu the context menu that will be shown
1068      * \param line the annotated line for which the context menu is shown
1069      */
1070     void annotationContextMenuAboutToShow(KTextEditor::View *view, QMenu *menu, int line);
1071 
1072     /**
1073      * This signal is emitted when an entry on the annotation border was activated,
1074      * for example by clicking or double-clicking it. This follows the KDE wide
1075      * setting for activation via click or double-clcik
1076      *
1077      * \param view the view to which the activated border belongs to
1078      * \param line the document line that the activated position belongs to
1079      */
1080     void annotationActivated(KTextEditor::View *view, int line);
1081 
1082     /**
1083      * This signal is emitted when the annotation border is shown or hidden.
1084      *
1085      * \param view the view to which the border belongs to
1086      * \param visible the current visibility state
1087      */
1088     void annotationBorderVisibilityChanged(KTextEditor::View *view, bool visible);
1089 
1090     /**
1091      * Inline Note
1092      */
1093 public:
1094     /**
1095      * Register the inline note provider @p provider.
1096      *
1097      * Whenever a line is painted, the @p provider will be queried for notes
1098      * that should be painted in it. When the provider is about to be
1099      * destroyed, make sure to call unregisterInlineNoteProvider() to avoid a
1100      * dangling pointer.
1101      *
1102      * @param provider inline note provider
1103      * @see unregisterInlineNoteProvider(), InlineNoteProvider
1104      */
1105     virtual void registerInlineNoteProvider(KTextEditor::InlineNoteProvider *provider) = 0;
1106 
1107     /**
1108      * Unregister the inline note provider @p provider.
1109      *
1110      * @param provider inline note provider to unregister
1111      * @see registerInlineNoteProvider(), InlineNoteProvider
1112      */
1113     virtual void unregisterInlineNoteProvider(KTextEditor::InlineNoteProvider *provider) = 0;
1114 
1115     /**
1116      * Text Hint
1117      */
1118 public:
1119     /**
1120      * Register the text hint provider \p provider.
1121      *
1122      * Whenever the user hovers over text, \p provider will be asked for
1123      * a text hint. When the provider is about to be destroyed, make
1124      * sure to call unregisterTextHintProvider() to avoid a dangling pointer.
1125      *
1126      * @param provider text hint provider
1127      * @see unregisterTextHintProvider(), TextHintProvider
1128      */
1129     virtual void registerTextHintProvider(KTextEditor::TextHintProvider *provider) = 0;
1130 
1131     /**
1132      * Unregister the text hint provider \p provider.
1133      *
1134      * @param provider text hint provider to unregister
1135      * @see registerTextHintProvider(), TextHintProvider
1136      */
1137     virtual void unregisterTextHintProvider(KTextEditor::TextHintProvider *provider) = 0;
1138 
1139     /**
1140      * Set the text hint delay to \p delay milliseconds.
1141      *
1142      * The delay specifies the time the user needs to hover over the text
1143      * before the tool tip is shown. Therefore, \p delay should not be
1144      * too large, a value of 500 milliseconds is recommended and set by
1145      * default.
1146      *
1147      * If \p delay is <= 0, the default delay will be set.
1148      *
1149      * \param delay tool tip delay in milliseconds
1150      */
1151     virtual void setTextHintDelay(int delay) = 0;
1152 
1153     /**
1154      * Get the text hint delay in milliseconds.
1155      * By default, the text hint delay is set to 500 milliseconds.
1156      * It can be changed by calling \p setTextHintDelay().
1157      */
1158     virtual int textHintDelay() const = 0;
1159 
1160     /**
1161      * Completion
1162      */
1163 public:
1164     /**
1165      * Query whether the code completion box is currently displayed.
1166      */
1167     virtual bool isCompletionActive() const = 0;
1168 
1169     /**
1170      * Invoke code completion over a given range, with a specific \a model.
1171      */
1172     virtual void startCompletion(Range word, CodeCompletionModel *model) = 0;
1173 
1174     /**
1175      * Abort the currently displayed code completion without executing any currently
1176      * selected completion. This is safe, even when the completion box is not currently
1177      * active.
1178      * \see isCompletionActive()
1179      */
1180     virtual void abortCompletion() = 0;
1181 
1182     /**
1183      * Force execution of the currently selected completion, and hide the code completion
1184      * box.
1185      */
1186     virtual void forceCompletion() = 0;
1187 
1188     /**
1189      * Register a new code completion \p model.
1190      * \param model new completion model
1191      * \see unregisterCompletionModel()
1192      */
1193     virtual void registerCompletionModel(CodeCompletionModel *model) = 0;
1194 
1195     /**
1196      * Unregister a code completion \p model.
1197      * \param model the model that should be unregistered
1198      * \see registerCompletionModel()
1199      */
1200     virtual void unregisterCompletionModel(CodeCompletionModel *model) = 0;
1201 
1202     /**
1203      * Determine the status of automatic code completion invocation.
1204      */
1205     virtual bool isAutomaticInvocationEnabled() const = 0;
1206 
1207     /**
1208      * Enable or disable automatic code completion invocation.
1209      */
1210     virtual void setAutomaticInvocationEnabled(bool enabled = true) = 0;
1211 
1212     /**
1213      * Invoke code completion over a given range, with specific models and invocation type.
1214      * \param models list of models to start. If this is an empty list, all registered models are started.
1215      */
1216     virtual void startCompletion(const Range &word,
1217                                  const QList<CodeCompletionModel *> &models = QList<CodeCompletionModel *>(),
1218                                  KTextEditor::CodeCompletionModel::InvocationType invocationType = KTextEditor::CodeCompletionModel::ManualInvocation) = 0;
1219 
1220     /**
1221      * Obtain the list of registered code completion models.
1222      * \returns a list of a models that are currently registered
1223      * \see registerCompletionModel(CodeCompletionModel*)
1224      */
1225     virtual QList<CodeCompletionModel *> codeCompletionModels() const = 0;
1226 
1227 public:
1228     /**
1229      * Get the current active theme of this view.
1230      * Might change during runtime, configChanged() will be emitted in that cases.
1231      *
1232      * \return current active theme
1233      *
1234      * \since 5.79
1235      */
1236     KSyntaxHighlighting::Theme theme() const;
1237 
1238 private:
1239     /**
1240      * private d-pointer, pointing to the internal implementation
1241      */
1242     ViewPrivate *const d;
1243 };
1244 
1245 }
1246 
1247 #endif