KTextEditor

view.h
1 /*
2  SPDX-FileCopyrightText: 2001 Christoph Cullmann <[email protected]>
3 
4  Documentation:
5  SPDX-FileCopyrightText: 2005 Dominik Haumann <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #ifndef KTEXTEDITOR_VIEW_H
11 #define KTEXTEDITOR_VIEW_H
12 
13 #include <ktexteditor_export.h>
14 
15 // gui merging
16 #include <KSyntaxHighlighting/Theme>
17 #include <KXMLGUIClient>
18 #include <ktexteditor/codecompletionmodel.h>
19 
20 // widget
21 #include <QSet>
22 #include <QWidget>
23 
24 class QMenu;
25 
26 class KConfigGroup;
27 
28 namespace KSyntaxHighlighting
29 {
30 class Theme;
31 }
32 
33 namespace KTextEditor
34 {
35 class Document;
36 class MainWindow;
37 class ViewPrivate;
38 class Attribute;
39 class AttributeBlock;
40 class Range;
41 class Cursor;
42 class AnnotationModel;
43 class AbstractAnnotationItemDelegate;
44 class InlineNoteProvider;
45 class TextHintProvider;
46 class CodeCompletionModel;
47 
48 /**
49  * \class View view.h <KTextEditor/View>
50  *
51  * \brief A text widget with KXMLGUIClient that represents a Document.
52  *
53  * Topics:
54  * - \ref view_intro
55  * - \ref view_hook_into_gui
56  * - \ref view_selection
57  * - \ref view_cursors
58  * - \ref view_mouse_tracking
59  * - \ref view_modes
60  * - \ref view_config
61  * - \ref view_annoview
62  * - \ref view_inlinenote
63  * - \ref view_texthint
64  * - \ref view_compiface
65  *
66  * \section view_intro Introduction
67  *
68  * The View class represents a single view of a KTextEditor::Document,
69  * get the document on which the view operates with document().
70  * A view provides both the graphical representation of the text and the
71  * KXMLGUIClient for the actions. The view itself does not provide
72  * text manipulation, use the methods from the Document instead. The only
73  * method to insert text is insertText(), which inserts the given text
74  * at the current cursor position and emits the signal textInserted().
75  *
76  * Usually a view is created by using Document::createView().
77  * Furthermore a view can have a context menu. Set it with setContextMenu()
78  * and get it with contextMenu().
79  *
80  * \section view_hook_into_gui Merging the View's GUI
81  *
82  * A View is derived from the class KXMLGUIClient, so its GUI elements (like
83  * menu entries and toolbar items) can be merged into the application's GUI
84  * (or into a KXMLGUIFactory) by calling
85  * \code
86  * // view is of type KTextEditor::View*
87  * mainWindow()->guiFactory()->addClient( view );
88  * \endcode
89  * You can add only one view as client, so if you have several views, you first
90  * have to remove the current view, and then add the new one, like this
91  * \code
92  * mainWindow()->guiFactory()->removeClient( currentView );
93  * mainWindow()->guiFactory()->addClient( newView );
94  * \endcode
95  *
96  * \section view_selection Text Selection
97  *
98  * As the view is a graphical text editor it provides \e normal and \e block
99  * text selection. You can check with selection() whether a selection exists.
100  * removeSelection() will remove the selection without removing the text,
101  * whereas removeSelectionText() also removes both, the selection and the
102  * selected text. Use selectionText() to get the selected text and
103  * setSelection() to specify the selected text range. The signal
104  * selectionChanged() is emitted whenever the selection changed.
105  *
106  * \section view_cursors Cursor Positions
107  *
108  * A view has one Cursor which represents a line/column tuple. Two different
109  * kinds of cursor positions are supported: first is the \e real cursor
110  * position where a \e tab character only counts one character. Second is the
111  * \e virtual cursor position, where a \e tab character counts as many
112  * spaces as defined. Get the real position with cursorPosition() and the
113  * virtual position with cursorPositionVirtual(). Set the real cursor
114  * position with setCursorPosition(). The signal cursorPositionChanged() is
115  * emitted whenever the cursor position changed.
116  *
117  * Screen coordinates of the current text cursor position in pixels are obtained
118  * through cursorPositionCoordinates(). Further conversion of screen pixel
119  * coordinates and text cursor positions are provided by cursorToCoordinate()
120  * and coordinatesToCursor().
121  *
122  * \section view_mouse_tracking Mouse Tracking
123  *
124  * It is possible to get notified via the signal mousePositionChanged() for
125  * mouse move events, if mouseTrackingEnabled() returns \e true. Mouse tracking
126  * can be turned on/off by calling setMouseTrackingEnabled(). If an editor
127  * implementation does not support mouse tracking, mouseTrackingEnabled() will
128  * always return \e false.
129  *
130  * \section view_modes Input/View Modes
131  *
132  * A view supports several input modes. Common input modes are
133  * \e NormalInputMode and \e ViInputMode. Which input modes the editor supports depends on the
134  * implementation. The getter viewInputMode() returns enum \InputMode representing the current mode.
135  *
136  * Input modes can have their own view modes. In case of default \e NormalInputMode those are
137  * \e NormalModeInsert and \e NormalModeOverwrite. You can use viewMode() getter to obtain those.
138  *
139  * For viewMode() and viewInputMode() there are also variants with \e Human suffix, which
140  * returns the human readable representation (i18n) usable for displaying in user interface.
141  *
142  * Whenever the input/view mode changes the signals
143  * viewInputModeChanged()/viewModeChanged() are emitted.
144  *
145  * \section view_config View Config
146  * Config provides methods to access and modify the low level config information for a given
147  * View
148  * KTextEditor::View has support for the following keys:
149  * - line-numbers [bool], show/hide line numbers
150  * - icon-bar [bool], show/hide icon bar
151  * - folding-bar [bool], show/hide the folding bar
152  * - folding-preview [bool], enable/disable folding preview when mouse hovers
153  * on folded region
154  * - dynamic-word-wrap [bool], enable/disable dynamic word wrap
155  * - background-color [QColor], read/set the default background color
156  * - selection-color [QColor], read/set the default color for selections
157  * - search-highlight-color [QColor], read/set the background color for search
158  * - replace-highlight-color [QColor], read/set the background color for replaces
159  * - default-mark-type [uint], read/set the default mark type
160  * - allow-mark-menu [bool], enable/disable the menu shown when right clicking
161  * on the left gutter. When disabled, click on the gutter will always set
162  * or clear the mark of default type.
163  * - icon-border-color [QColor] read/set the icon border color (on the left,
164  * with the line numbers)
165  * - folding-marker-color [QColor] read/set folding marker colors (in the icon border)
166  * - line-number-color [QColor] read/set line number colors (in the icon border)
167  * - current-line-number-color [QColor] read/set current line number color (in the icon border)
168  * - modification-markers [bool] read/set whether the modification markers are shown
169  * - word-count [bool] enable/disable the counting of words and characters in the statusbar
170  * - line-count [bool] show/hide the total number of lines in the status bar (@since 5.66)
171  * - scrollbar-minimap [bool] enable/disable scrollbar minimap
172  * - scrollbar-preview [bool] enable/disable scrollbar text preview on hover
173  * - font [QFont] change the font
174  * - theme [QString] change the theme.
175  *
176  * You can retrieve the value of a config key using configValue() and set the value
177  * for a config key using setConfigValue().
178  *
179  * \section view_annoview Annotation Interface
180  *
181  * The Annotation Interface allows to do these things:
182  * - (1) show/hide the annotation border along with the possibility to add actions
183  * into its context menu.
184  * - (2) set a separate AnnotationModel for the View: Note that this interface
185  * inherits the AnnotationInterface.
186  * - (3) set a custom AbstractAnnotationItemDelegate for the View.
187  *
188  * For a more detailed explanation about whether you want to set a custom
189  * delegate for rendering the annotations, read the detailed documentation about the
190  * AbstractAnnotationItemDelegate.
191  *
192  * \section view_inlinenote Inline Notes
193  *
194  * The inline notes interface provides a way to render arbitrary things in
195  * the text. The text layout of the line is adapted to create space for the
196  * note. Possible applications include showing a name of a function parameter
197  * in a function call or rendering a square with a color preview next to CSS
198  * color property.
199  *
200  * \image html inlinenote.png "Inline note showing a CSS color preview"
201  *
202  * To register as inline note provider, call registerInlineNoteProvider() with
203  * an instance that inherits InlineNoteProvider. Finally, make sure you remove
204  * your inline note provider by calling unregisterInlineNoteProvider().
205  *
206  * \section view_texthint Introduction
207  *
208  * The text hint interface provides a way to show tool tips for text located
209  * under the mouse. Possible applications include showing a value of a variable
210  * when debugging an application, or showing a complete path of an include
211  * directive.
212  *
213  * \image html texthint.png "Text hint showing the contents of a variable"
214  *
215  * To register as text hint provider, call registerTextHintProvider() with an
216  * instance that inherits TextHintProvider. Finally, make sure you remove your
217  * text hint provider by calling unregisterTextHintProvider().
218  *
219  * Text hints are shown after the user hovers with the mouse for a delay of
220  * textHintDelay() milliseconds over the same word. To change the delay, call
221  * setTextHintDelay().
222  *
223  * \section view_compiface Completion Interface
224  *
225  * The Completion Interface is designed to provide code completion
226  * functionality for a KTextEditor::View. This interface provides the basic
227  * mechanisms to display a list of completions, update this list according
228  * to user input, and allow the user to select a completion.
229  *
230  * Essentially, this provides an item view for the available completions. In
231  * order to use this interface, you will need to implement a
232  * CodeCompletionModel that generates the relevant completions given the
233  * current input.
234  *
235  * More information about interfaces for the view can be found in
236  * \ref kte_group_view_extensions.
237  *
238  * \see KTextEditor::Document, KXMLGUIClient
239  * \author Christoph Cullmann <[email protected]>
240  */
241 class KTEXTEDITOR_EXPORT View : public QWidget, public KXMLGUIClient
242 {
243  Q_OBJECT
244 
245 protected:
246  /**
247  * Constructor.
248  *
249  * Create a view attached to the widget \p parent.
250  *
251  * Pass it the internal implementation to store a d-pointer.
252  *
253  * \param impl d-pointer to use
254  * \param parent parent widget
255  * \see Document::createView()
256  */
257  View(ViewPrivate *impl, QWidget *parent);
258 
259 public:
260  /**
261  * Virtual destructor.
262  */
263  ~View() override;
264 
265  /*
266  * Accessor for the document
267  */
268 public:
269  /**
270  * Get the view's \e document, that means the view is a view of the
271  * returned document.
272  * \return the view's document
273  */
274  virtual Document *document() const = 0;
275 
276  /*
277  * General information about this view
278  */
279 public:
280  /**
281  * Possible input modes.
282  * These correspond to various modes the text editor might be in.
283  */
284  enum InputMode {
285  NormalInputMode = 0, /**< Normal Mode. */
286  ViInputMode = 1 /**< Vi mode. The view will behave like the editor vi(m) */
287  };
288 
289  /**
290  * Possible view modes
291  * These correspond to various modes the text editor might be in.
292  */
293  enum ViewMode {
294  NormalModeInsert = 0, /**< Insert mode. Characters will be added. */
295  NormalModeOverwrite = 1, /**< Overwrite mode. Characters will be replaced. */
296 
297  ViModeNormal = 10,
298  ViModeInsert = 11,
299  ViModeVisual = 12,
300  ViModeVisualLine = 13,
301  ViModeVisualBlock = 14,
302  ViModeReplace = 15
303  };
304 
305  /**
306  * Possible line types
307  * \since 5.33
308  */
309  enum LineType {
310  RealLine = 0, /**< Real line */
311  VisibleLine = 1 /**< Visible line. Line that is not folded. */
312  };
313  /**
314  * Get the current view mode/state.
315  * This can be used to detect the view's current mode. For
316  * example \NormalInputMode, \ViInputMode or whatever other input modes are
317  * supported. \see viewModeHuman() for translated version.
318  * \return current view mode/state
319  * \see viewModeChanged()
320  */
321  virtual ViewMode viewMode() const = 0;
322 
323  /**
324  * Get the current view mode state.
325  * This can be used to visually indicate the view's current mode, for
326  * example \e INSERT mode, \e OVERWRITE mode or \e COMMAND mode - or
327  * whatever other edit modes are supported. The string should be
328  * translated (i18n), as this is a user aimed representation of the view
329  * state, which should be shown in the GUI, for example in the status bar.
330  * This string may be rich-text.
331  * \return Human-readable version of the view mode state
332  * \see viewModeChanged()
333  */
334  virtual QString viewModeHuman() const = 0;
335 
336  /**
337  * Set the view's new input mode.
338  * \param inputMode new InputMode value
339  * \see viewInputMode()
340  * @since 5.54
341  * KF6: make virtual
342  */
343  void setViewInputMode(InputMode inputMode);
344 
345  /**
346  * Get the view's current input mode.
347  * The current mode can be \NormalInputMode and \ViInputMode.
348  * For human translated version \see viewInputModeHuman.
349  *
350  * \return the current input mode of this view
351  * \see viewInputModeChanged()
352  */
353  virtual InputMode viewInputMode() const = 0;
354 
355  /**
356  * Get the view's current input mode in human readable form.
357  * The string should be translated (i18n). For id like version \see viewInputMode
358  *
359  * \return the current input mode of this view in human readable form
360  */
361  virtual QString viewInputModeHuman() const = 0;
362 
363  /**
364  * Get the view's main window, if any
365  * \return the view's main window, will always return at least some non-nullptr dummy interface
366  */
367  virtual KTextEditor::MainWindow *mainWindow() const = 0;
368 
369  /*
370  * SIGNALS
371  * following signals should be emitted by the editor view
372  */
373 Q_SIGNALS:
374  /**
375  * This signal is emitted whenever the \p view gets the focus.
376  * \param view view which gets focus
377  * \see focusOut()
378  */
379  void focusIn(KTextEditor::View *view);
380 
381  /**
382  * This signal is emitted whenever the \p view loses the focus.
383  * \param view view which lost focus
384  * \see focusIn()
385  */
386  void focusOut(KTextEditor::View *view);
387 
388  /**
389  * This signal is emitted whenever the view mode of \p view changes.
390  * \param view the view which changed its mode
391  * \param mode new view mode
392  * \see viewMode()
393  */
394  void viewModeChanged(KTextEditor::View *view, KTextEditor::View::ViewMode mode);
395 
396  /**
397  * This signal is emitted whenever the \p view's input \p mode changes.
398  * \param view view which changed its input mode
399  * \param mode new input mode
400  * \see viewInputMode()
401  */
402  void viewInputModeChanged(KTextEditor::View *view, KTextEditor::View::InputMode mode);
403 
404  /**
405  * This signal is emitted from \p view whenever the users inserts \p text
406  * at \p position, that means the user typed/pasted text.
407  * \param view view in which the text was inserted
408  * \param position position where the text was inserted
409  * \param text the text the user has typed into the editor
410  * \see insertText()
411  */
412  void textInserted(KTextEditor::View *view, KTextEditor::Cursor position, const QString &text);
413 
414  /*
415  * Context menu handling
416  */
417 public:
418  /**
419  * Set a context menu for this view to \p menu.
420  *
421  * \note any previously assigned menu is not deleted. If you are finished
422  * with the previous menu, you may delete it.
423  *
424  * \warning Use this with care! Plugin xml gui clients are not merged
425  * into this menu!
426  * \warning !!!!!! DON'T USE THIS FUNCTION, UNLESS YOU ARE SURE YOU DON'T WANT PLUGINS TO WORK !!!!!!
427  *
428  * \param menu new context menu object for this view
429  * \see contextMenu()
430  */
431  virtual void setContextMenu(QMenu *menu) = 0;
432 
433  /**
434  * Get the context menu for this view. The return value can be NULL
435  * if no context menu object was set and kxmlgui is not initialized yet.
436  * If there is no user set menu, the kxmlgui menu is returned. Do not delete this menu, if
437  * if it is the xmlgui menu.
438  * \return context menu object
439  * \see setContextMenu()
440  */
441  virtual QMenu *contextMenu() const = 0;
442 
443  /**
444  * Populate \a menu with default text editor actions. If \a menu is
445  * null, a menu will be created with the view as its parent.
446  *
447  * \note to use this menu, you will next need to call setContextMenu(),
448  * as this does not assign the new context menu.
449  *
450  * \warning This contains only basic options from the editor component
451  * (katepart). Plugins are \p not merged/integrated into it!
452  * If you want to be a better citizen and take full advantage
453  * of KTextEditor plugins do something like:
454  * \code
455  * KXMLGUIClient* client = view;
456  * // search parent XmlGuiClient
457  * while (client->parentClient()) {
458  * client = client->parentClient();
459  * }
460  *
461  * if (client->factory()) {
462  * const QList<QWidget*> menuContainers = client->factory()->containers("menu");
463  * for (QWidget *w : menuContainers) {
464  * if (w->objectName() == "ktexteditor_popup") {
465  * // do something with the menu (ie adding an onshow handler)
466  * break;
467  * }
468  * }
469  * }
470  * \endcode
471  * \warning or simply use the aboutToShow, aboutToHide signals !!!!!
472  *
473  * \param menu the menu to be populated, or null to create a new menu.
474  * \return the menu, whether created or passed initially
475  */
476  virtual QMenu *defaultContextMenu(QMenu *menu = nullptr) const = 0;
477 
478 Q_SIGNALS:
479  /**
480  * Signal which is emitted immediately prior to showing the current
481  * context \a menu.
482  */
483  void contextMenuAboutToShow(KTextEditor::View *view, QMenu *menu);
484 
485  /*
486  * Cursor handling
487  */
488 public:
489  /**
490  * Set the view's new cursor to \p position. A \e TAB character
491  * is handled as only on character.
492  * \param position new cursor position
493  * \return \e true on success, otherwise \e false
494  * \see cursorPosition()
495  */
496  virtual bool setCursorPosition(Cursor position) = 0;
497 
498  /**
499  * Set the view's new cursors to \p positions. A \e TAB character
500  * is handled as only on character.
501  *
502  * This allows to create multiple cursors in this view.
503  *
504  * The first passed position will be used for the primary cursor
505  * just like if you would call \ref setCursorPosition.
506  *
507  * \param positions new cursor positions
508  * \see cursorPositions()
509  *
510  * @since 5.95
511  */
512  void setCursorPositions(const QVector<KTextEditor::Cursor> &positions);
513 
514  /**
515  * Get the view's current cursor position. A \e TAB character is
516  * handled as only one character.
517  * \return current cursor position
518  * \see setCursorPosition()
519  */
520  virtual Cursor cursorPosition() const = 0;
521 
522  /**
523  * Get the view's current cursor positions. A \e TAB character is
524  * handled as only one character.
525  *
526  * The returned vector contains the primary cursor as first element.
527  *
528  * @since 5.95
529  *
530  * \return all currently existing cursors
531  */
532  QVector<KTextEditor::Cursor> cursorPositions() const;
533 
534  /**
535  * Get the current \e virtual cursor position, \e virtual means the
536  * tabulator character (TAB) counts \e multiple characters, as configured
537  * by the user (e.g. one TAB is 8 spaces). The virtual cursor
538  * position provides access to the user visible values of the current
539  * cursor position.
540  *
541  * \return virtual cursor position
542  * \see cursorPosition()
543  */
544  virtual Cursor cursorPositionVirtual() const = 0;
545 
546  /**
547  * Get the screen coordinates (x, y) of the supplied \a cursor relative
548  * to the view widget in pixels. Thus, (0, 0) represents the top left hand
549  * of the view widget.
550  *
551  * To map pixel coordinates to a Cursor position (the reverse transformation)
552  * use coordinatesToCursor().
553  *
554  * \param cursor cursor to determine coordinate for.
555  * \return cursor screen coordinates relative to the view widget
556  *
557  * \see cursorPositionCoordinates(), coordinatesToCursor()
558  */
559  virtual QPoint cursorToCoordinate(KTextEditor::Cursor cursor) const = 0;
560 
561  /**
562  * Get the screen coordinates (x, y) of the cursor position in pixels.
563  * The returned coordinates are relative to the View such that (0, 0)
564  * represents tht top-left corner of the View.
565  *
566  * If global screen coordinates are required, e.g. for showing a QToolTip,
567  * convert the view coordinates to global screen coordinates as follows:
568  * \code
569  * QPoint viewCoordinates = view->cursorPositionCoordinates();
570  * QPoint globalCoorinates = view->mapToGlobal(viewCoordinates);
571  * \endcode
572  * \return cursor screen coordinates
573  * \see cursorToCoordinate(), coordinatesToCursor()
574  */
575  virtual QPoint cursorPositionCoordinates() const = 0;
576 
577  /**
578  * Get the text-cursor in the document from the screen coordinates,
579  * relative to the view widget.
580  *
581  * To map a cursor to pixel coordinates (the reverse transformation)
582  * use cursorToCoordinate().
583  *
584  * \param coord coordinates relative to the view widget
585  * \return cursor in the View, that points onto the character under
586  * the given coordinate. May be KTextEditor::Cursor::invalid().
587  */
588  virtual KTextEditor::Cursor coordinatesToCursor(const QPoint &coord) const = 0;
589 
590  /*
591  * SIGNALS
592  * following signals should be emitted by the editor view
593  * if the cursor position changes
594  */
595 Q_SIGNALS:
596  /**
597  * This signal is emitted whenever the \p view's cursor position changed.
598  * \param view view which emitted the signal
599  * \param newPosition new position of the cursor (Kate will pass the real
600  * cursor position, not the virtual)
601  * \see cursorPosition(), cursorPositionVirtual()
602  */
603  void cursorPositionChanged(KTextEditor::View *view, KTextEditor::Cursor newPosition);
604 
605  /**
606  * This signal should be emitted whenever the \p view is scrolled vertically.
607  * \param view view which emitted the signal
608  * \param newPos the new scroll position
609  */
610  void verticalScrollPositionChanged(KTextEditor::View *view, KTextEditor::Cursor newPos);
611 
612  /**
613  * This signal should be emitted whenever the \p view is scrolled horizontally.
614  * \param view view which emitted the signal
615  */
616  void horizontalScrollPositionChanged(KTextEditor::View *view);
617  /*
618  * Mouse position
619  */
620 public:
621  /**
622  * Check, whether mouse tracking is enabled.
623  *
624  * Mouse tracking is required to have the signal mousePositionChanged()
625  * emitted.
626  * \return \e true, if mouse tracking is enabled, otherwise \e false
627  * \see setMouseTrackingEnabled(), mousePositionChanged()
628  */
629  virtual bool mouseTrackingEnabled() const = 0;
630 
631  /**
632  * Try to enable or disable mouse tracking according to \p enable.
633  * The return value contains the state of mouse tracking \e after the
634  * request. Mouse tracking is required to have the mousePositionChanged()
635  * signal emitted.
636  *
637  * \note Implementation Notes: An implementation is not forced to support
638  * this, and should always return \e false if it does not have
639  * support.
640  *
641  * \param enable if \e true, try to enable mouse tracking, otherwise disable
642  * it.
643  * \return the current state of mouse tracking
644  * \see mouseTrackingEnabled(), mousePositionChanged()
645  */
646  virtual bool setMouseTrackingEnabled(bool enable) = 0;
647 
648 Q_SIGNALS:
649  /**
650  * This signal is emitted whenever the position of the mouse changes over
651  * this \a view. If the mouse moves off the view, an invalid cursor position
652  * should be emitted, i.e. Cursor::invalid().
653  * \note If mouseTrackingEnabled() returns \e false, this signal is never
654  * emitted.
655  * \param view view which emitted the signal
656  * \param newPosition new position of the mouse or Cursor::invalid(), if the
657  * mouse moved out of the \p view.
658  * \see mouseTrackingEnabled()
659  */
660  void mousePositionChanged(KTextEditor::View *view, KTextEditor::Cursor newPosition);
661 
662  /*
663  * Selection methods.
664  * This deals with text selection and copy&paste
665  */
666 public:
667  /**
668  * Set the view's selection to the range \p selection.
669  * The old selection will be discarded.
670  * \param range the range of the new selection
671  * \return \e true on success, otherwise \e false (e.g. when the cursor
672  * range is invalid)
673  * \see selectionRange(), selection()
674  */
675  virtual bool setSelection(Range range) = 0;
676 
677  /**
678  * Set the view's selection to the range \p selection.
679  * The old selection will be discarded.
680  * \param ranges the ranges of the new selections
681  * \see selectionRanges(), selection()
682  *
683  * @since 5.95
684  */
685  void setSelections(const QVector<KTextEditor::Range> &ranges);
686 
687  /**
688  * Query the view whether it has selected text, i.e. whether a selection
689  * exists.
690  * \return \e true if a text selection exists, otherwise \e false
691  * \see setSelection(), selectionRange()
692  */
693  virtual bool selection() const = 0;
694 
695  /**
696  * Get the range occupied by the current selection.
697  * \return selection range, valid only if a selection currently exists.
698  * \see setSelection()
699  */
700  virtual Range selectionRange() const = 0;
701 
702  /**
703  * Get the ranges occupied by the current selections.
704  * \return selection ranges, valid only if a selection currently exists.
705  * \see setSelections()
706  *
707  * @since 5.95
708  */
709  QVector<KTextEditor::Range> selectionRanges() const;
710 
711  /**
712  * Get the view's selected text.
713  * \return the selected text
714  * \see setSelection()
715  */
716  virtual QString selectionText() const = 0;
717 
718  /**
719  * Remove the view's current selection, \e without deleting the selected
720  * text.
721  * \return \e true on success, otherwise \e false
722  * \see removeSelectionText()
723  */
724  virtual bool removeSelection() = 0;
725 
726  /**
727  * Remove the view's current selection \e including the selected text.
728  * \return \e true on success, otherwise \e false
729  * \see removeSelection()
730  */
731  virtual bool removeSelectionText() = 0;
732 
733  /*
734  * Blockselection stuff
735  */
736 public:
737  /**
738  * Set block selection mode to state \p on.
739  * \param on if \e true, block selection mode is turned on, otherwise off
740  * \return \e true on success, otherwise \e false
741  * \see blockSelection()
742  */
743  virtual bool setBlockSelection(bool on) = 0;
744 
745  /**
746  * Get the status of the selection mode. \e true indicates that block
747  * selection mode is on. If this is \e true, selections applied via the
748  * SelectionInterface are handled as block selections and the Copy&Paste
749  * functions work on rectangular blocks of text rather than normal.
750  * \return \e true, if block selection mode is enabled, otherwise \e false
751  * \see setBlockSelection()
752  */
753  virtual bool blockSelection() const = 0;
754 
755  /*
756  * SIGNALS
757  * following signals should be emitted by the editor view for selection
758  * handling.
759  */
760 Q_SIGNALS:
761  /**
762  * This signal is emitted whenever the \p view's selection changes.
763  * \note If the mode switches from block selection to normal selection
764  * or vice versa this signal should also be emitted.
765  * \param view view in which the selection changed
766  * \see selection(), selectionRange(), selectionText()
767  */
768  void selectionChanged(KTextEditor::View *view);
769 
770 public:
771  /**
772  * This is a convenience function which inserts \p text at the view's
773  * current cursor position. You do not necessarily need to reimplement
774  * it, except you want to do some special things.
775  * \param text Text to be inserted
776  * \return \e true on success of insertion, otherwise \e false
777  * \see textInserted()
778  */
779  virtual bool insertText(const QString &text);
780 
781  /**
782  * Insert a template into the document. The template can have editable fields
783  * which can be filled by the user. You can create editable fields
784  * with ${fieldname}; multiple fields with the same name will have their
785  * contents synchronized automatically, and only the first one is editable
786  * in this case.
787  * Fields can have a default value specified by writing ${fieldname=default}.
788  * Note that `default' is a JavaScript expression and strings need to be quoted.
789  * You can also provide a piece of JavaScript for more complex logic.
790  * To create a field which provides text based on a JS function call and the values
791  * of the other, editable fields, use the ${func()} syntax. func() must be a callable
792  * object defined in @p script. You can pass arguments to the function by just
793  * writing any constant expression or a field name.
794  * \param insertPosition where to insert the template
795  * \param templateString template to insert using the above syntax
796  * \param script script with functions which can be used in @p templateScript
797  * \return true on success, false if insertion failed (e.g. read-only mode)
798  */
799  bool insertTemplate(KTextEditor::Cursor insertPosition, const QString &templateString, const QString &script = QString());
800 
801  /**
802  * Scroll view to cursor.
803  *
804  * \param cursor the cursor position to scroll to.
805  *
806  * \since 5.33
807  */
808  void setScrollPosition(KTextEditor::Cursor cursor);
809 
810  /**
811  * Horizontally scroll view to position.
812  *
813  * \param x the pixel position to scroll to.
814  *
815  * \since 5.33
816  */
817  void setHorizontalScrollPosition(int x);
818 
819  /**
820  * Get the cursor corresponding to the maximum position
821  * the view can vertically scroll to.
822  *
823  * \return cursor position of the maximum vertical scroll position.
824  *
825  * \since 5.33
826  */
827  KTextEditor::Cursor maxScrollPosition() const;
828 
829  /**
830  * Get the first displayed line in the view.
831  *
832  * \note If code is folded, many hundred lines can be
833  * between firstDisplayedLine() and lastDisplayedLine().
834  *
835  * \param lineType if RealLine (the default), it returns the real line number
836  * accounting for folded regions. In that case it walks over all folded
837  * regions
838  * O(n) for n == number of folded ranges
839  * \return the first displayed line
840  *
841  * \see lastDisplayedLine()
842  * \since 5.33
843  */
844  int firstDisplayedLine(LineType lineType = RealLine) const;
845 
846  /**
847  * Get the last displayed line in the view.
848  *
849  * \note If code is folded, many hundred lines can be
850  * between firstDisplayedLine() and lastDisplayedLine().
851  *
852  * \param lineType if RealLine (the default), it returns the real line number
853  * accounting for folded regions. In that case it walks over all folded
854  * regions
855  * O(n) for n == number of folded ranges
856  * \return the last displayed line
857  *
858  * \see firstDisplayedLine()
859  * \since 5.33
860  */
861  int lastDisplayedLine(LineType lineType = RealLine) const;
862 
863  /**
864  * Get the view's text area rectangle excluding border, scrollbars, etc.
865  *
866  * \return the view's text area rectangle
867  *
868  * \since 5.33
869  */
870  QRect textAreaRect() const;
871 
872 public:
873  /**
874  * Print the document. This should result in showing the print dialog.
875  *
876  * @returns true if document was printed
877  */
878  virtual bool print() = 0;
879 
880  /**
881  * Shows the print preview dialog/
882  */
883  virtual void printPreview() = 0;
884 
885  /**
886  * Is the status bar enabled?
887  *
888  * @return status bar enabled?
889  */
890  bool isStatusBarEnabled() const;
891 
892  /**
893  * Show/hide the status bar of the view.
894  * Per default, the status bar is enabled.
895  *
896  * @param enable should the status bar be enabled?
897  */
898  void setStatusBarEnabled(bool enable);
899 
900 Q_SIGNALS:
901  /**
902  * This signal is emitted whenever the status bar of \p view is toggled.
903  *
904  * @param enabled Whether the status bar is currently enabled or not
905  */
906  void statusBarEnabledChanged(KTextEditor::View *view, bool enabled);
907 
908 public:
909  /**
910  * Read session settings from the given \p config.
911  *
912  * Known flags:
913  * none atm
914  *
915  * \param config read the session settings from this KConfigGroup
916  * \param flags additional flags
917  * \see writeSessionConfig()
918  */
919  virtual void readSessionConfig(const KConfigGroup &config, const QSet<QString> &flags = QSet<QString>()) = 0;
920 
921  /**
922  * Write session settings to the \p config.
923  * See readSessionConfig() for more details.
924  *
925  * \param config write the session settings to this KConfigGroup
926  * \param flags additional flags
927  * \see readSessionConfig()
928  */
929  virtual void writeSessionConfig(KConfigGroup &config, const QSet<QString> &flags = QSet<QString>()) = 0;
930 
931 public:
932  /**
933  * Returns the attribute for the default style \p defaultStyle.
934  * @param defaultStyle default style to get the attribute for
935  * @see KTextEditor::Attribute
936  */
937  virtual QExplicitlySharedDataPointer<KTextEditor::Attribute> defaultStyleAttribute(KSyntaxHighlighting::Theme::TextStyle defaultStyle) const = 0;
938 
939  /**
940  * Get the list of AttributeBlocks for a given \p line in the document.
941  *
942  * \return list of AttributeBlocks for given \p line.
943  */
944  virtual QList<KTextEditor::AttributeBlock> lineAttributes(int line) = 0;
945 
946 Q_SIGNALS:
947  /**
948  * This signal is emitted whenever the current view configuration is changed.
949  *
950  * \param view the view which's config has changed
951  *
952  * \since 5.79
953  */
954  void configChanged(KTextEditor::View *view);
955 
956  /**
957  * View Config
958  */
959 public:
960  /**
961  * Get a list of all available keys.
962  */
963  virtual QStringList configKeys() const = 0;
964  /**
965  * Get a value for the \p key.
966  */
967  virtual QVariant configValue(const QString &key) = 0;
968  /**
969  * Set a the \p key's value to \p value.
970  */
971  virtual void setConfigValue(const QString &key, const QVariant &value) = 0;
972 
973  /**
974  * View Annotation Interface
975  */
976 public:
977  /**
978  * Sets a new \ref AnnotationModel for this document to provide
979  * annotation information for each line.
980  *
981  * \param model the new AnnotationModel
982  */
983  virtual void setAnnotationModel(AnnotationModel *model) = 0;
984 
985  /**
986  * returns the currently set \ref AnnotationModel or 0 if there's none
987  * set
988  * @returns the current \ref AnnotationModel
989  */
990  virtual AnnotationModel *annotationModel() const = 0;
991 
992  /**
993  * This function can be used to show or hide the annotation border
994  * The annotation border is hidden by default.
995  *
996  * @param visible if \e true the annotation border is shown, otherwise hidden
997  */
998  virtual void setAnnotationBorderVisible(bool visible) = 0;
999 
1000  /**
1001  * Checks whether the View's annotation border is visible.
1002  */
1003  virtual bool isAnnotationBorderVisible() const = 0;
1004 
1005  /**
1006  * Sets the AbstractAnnotationItemDelegate for this view and the model
1007  * to provide custom rendering of annotation information for each line.
1008  * Ownership is not transferred.
1009  *
1010  * \param delegate the new AbstractAnnotationItemDelegate, or \c nullptr to reset to the default delegate
1011  *
1012  * @since 6.0
1013  */
1014  virtual void setAnnotationItemDelegate(KTextEditor::AbstractAnnotationItemDelegate *delegate) = 0;
1015 
1016  /**
1017  * Returns the currently used AbstractAnnotationItemDelegate
1018  *
1019  * @returns the current AbstractAnnotationItemDelegate
1020  *
1021  * @since 6.0
1022  */
1023  virtual KTextEditor::AbstractAnnotationItemDelegate *annotationItemDelegate() const = 0;
1024 
1025  /**
1026  * This function can be used to declare whether it is known that the annotation items
1027  * rendered by the set delegate all have the same size.
1028  * This enables the view to do some optimizations for performance purposes.
1029  *
1030  * By default the value of this property is \c false .
1031  *
1032  * @param uniformItemSizes if \c true the annotation items are considered to all have the same size
1033  *
1034  * @since 6.0
1035  */
1036  virtual void setAnnotationUniformItemSizes(bool uniformItemSizes) = 0;
1037 
1038  /**
1039  * Checks whether the annotation items all have the same size.
1040  *
1041  * @since 6.0
1042  */
1043  virtual bool uniformAnnotationItemSizes() const = 0;
1044 
1045 Q_SIGNALS:
1046  /**
1047  * This signal is emitted before a context menu is shown on the annotation
1048  * border for the given line and view.
1049  *
1050  * \note Kate Part implementation detail: In Kate Part, the menu has an
1051  * entry to hide the annotation border.
1052  *
1053  * \param view the view that the annotation border belongs to
1054  * \param menu the context menu that will be shown
1055  * \param line the annotated line for which the context menu is shown
1056  */
1057  void annotationContextMenuAboutToShow(KTextEditor::View *view, QMenu *menu, int line);
1058 
1059  /**
1060  * This signal is emitted when an entry on the annotation border was activated,
1061  * for example by clicking or double-clicking it. This follows the KDE wide
1062  * setting for activation via click or double-clcik
1063  *
1064  * \param view the view to which the activated border belongs to
1065  * \param line the document line that the activated position belongs to
1066  */
1067  void annotationActivated(KTextEditor::View *view, int line);
1068 
1069  /**
1070  * This signal is emitted when the annotation border is shown or hidden.
1071  *
1072  * \param view the view to which the border belongs to
1073  * \param visible the current visibility state
1074  */
1075  void annotationBorderVisibilityChanged(KTextEditor::View *view, bool visible);
1076 
1077  /**
1078  * Inline Note
1079  */
1080 public:
1081  /**
1082  * Register the inline note provider @p provider.
1083  *
1084  * Whenever a line is painted, the @p provider will be queried for notes
1085  * that should be painted in it. When the provider is about to be
1086  * destroyed, make sure to call unregisterInlineNoteProvider() to avoid a
1087  * dangling pointer.
1088  *
1089  * @param provider inline note provider
1090  * @see unregisterInlineNoteProvider(), InlineNoteProvider
1091  */
1092  virtual void registerInlineNoteProvider(KTextEditor::InlineNoteProvider *provider) = 0;
1093 
1094  /**
1095  * Unregister the inline note provider @p provider.
1096  *
1097  * @param provider inline note provider to unregister
1098  * @see registerInlineNoteProvider(), InlineNoteProvider
1099  */
1100  virtual void unregisterInlineNoteProvider(KTextEditor::InlineNoteProvider *provider) = 0;
1101 
1102  /**
1103  * Text Hint
1104  */
1105 public:
1106  /**
1107  * Register the text hint provider \p provider.
1108  *
1109  * Whenever the user hovers over text, \p provider will be asked for
1110  * a text hint. When the provider is about to be destroyed, make
1111  * sure to call unregisterTextHintProvider() to avoid a dangling pointer.
1112  *
1113  * @param provider text hint provider
1114  * @see unregisterTextHintProvider(), TextHintProvider
1115  */
1116  virtual void registerTextHintProvider(KTextEditor::TextHintProvider *provider) = 0;
1117 
1118  /**
1119  * Unregister the text hint provider \p provider.
1120  *
1121  * @param provider text hint provider to unregister
1122  * @see registerTextHintProvider(), TextHintProvider
1123  */
1124  virtual void unregisterTextHintProvider(KTextEditor::TextHintProvider *provider) = 0;
1125 
1126  /**
1127  * Set the text hint delay to \p delay milliseconds.
1128  *
1129  * The delay specifies the time the user needs to hover over the text
1130  * before the tool tip is shown. Therefore, \p delay should not be
1131  * too large, a value of 500 milliseconds is recommended and set by
1132  * default.
1133  *
1134  * If \p delay is <= 0, the default delay will be set.
1135  *
1136  * \param delay tool tip delay in milliseconds
1137  */
1138  virtual void setTextHintDelay(int delay) = 0;
1139 
1140  /**
1141  * Get the text hint delay in milliseconds.
1142  * By default, the text hint delay is set to 500 milliseconds.
1143  * It can be changed by calling \p setTextHintDelay().
1144  */
1145  virtual int textHintDelay() const = 0;
1146 
1147  /**
1148  * Completion
1149  */
1150 public:
1151  /**
1152  * Query whether the code completion box is currently displayed.
1153  */
1154  virtual bool isCompletionActive() const = 0;
1155 
1156  /**
1157  * Invoke code completion over a given range, with a specific \a model.
1158  */
1159  virtual void startCompletion(Range word, CodeCompletionModel *model) = 0;
1160 
1161  /**
1162  * Abort the currently displayed code completion without executing any currently
1163  * selected completion. This is safe, even when the completion box is not currently
1164  * active.
1165  * \see isCompletionActive()
1166  */
1167  virtual void abortCompletion() = 0;
1168 
1169  /**
1170  * Force execution of the currently selected completion, and hide the code completion
1171  * box.
1172  */
1173  virtual void forceCompletion() = 0;
1174 
1175  /**
1176  * Register a new code completion \p model.
1177  * \param model new completion model
1178  * \see unregisterCompletionModel()
1179  */
1180  virtual void registerCompletionModel(CodeCompletionModel *model) = 0;
1181 
1182  /**
1183  * Unregister a code completion \p model.
1184  * \param model the model that should be unregistered
1185  * \see registerCompletionModel()
1186  */
1187  virtual void unregisterCompletionModel(CodeCompletionModel *model) = 0;
1188 
1189  /**
1190  * Determine the status of automatic code completion invocation.
1191  */
1192  virtual bool isAutomaticInvocationEnabled() const = 0;
1193 
1194  /**
1195  * Enable or disable automatic code completion invocation.
1196  */
1197  virtual void setAutomaticInvocationEnabled(bool enabled = true) = 0;
1198 
1199  /**
1200  * Invoke code completion over a given range, with specific models and invocation type.
1201  * \param models list of models to start. If this is an empty list, all registered models are started.
1202  */
1203  virtual void startCompletion(const Range &word,
1205  KTextEditor::CodeCompletionModel::InvocationType invocationType = KTextEditor::CodeCompletionModel::ManualInvocation) = 0;
1206 
1207  /**
1208  * Obtain the list of registered code completion models.
1209  * \returns a list of a models that are currently registered
1210  * \see registerCompletionModel(CodeCompletionModel*)
1211  */
1212  virtual QList<CodeCompletionModel *> codeCompletionModels() const = 0;
1213 
1214 public:
1215  /**
1216  * Get the current active theme of this view.
1217  * Might change during runtime, configChanged() will be emitted in that cases.
1218  *
1219  * \return current active theme
1220  *
1221  * \since 5.79
1222  */
1223  KSyntaxHighlighting::Theme theme() const;
1224 
1225 private:
1226  /**
1227  * private d-pointer, pointing to the internal implementation
1228  */
1229  ViewPrivate *const d;
1230 };
1231 
1232 }
1233 
1234 #endif
A delegate for rendering line annotation information and handling events.
An item model for providing code completion, and meta information for enhanced presentation.
InputMode
Possible input modes.
Definition: view.h:284
An model for providing line annotation information.
An object representing a section of text, from one Cursor to another.
QString mode() const override
Return the name of the currently used mode.
void writeSessionConfig(KConfigGroup &config, const QSet< QString > &flags=QSet< QString >()) override
Write session settings to the config.
ViewMode
Possible view modes These correspond to various modes the text editor might be in.
Definition: view.h:293
The Cursor represents a position in a Document.
Definition: cursor.h:74
Class to provide text hints for a View.
A text widget with KXMLGUIClient that represents a Document.
Definition: view.h:241
LineType
Possible line types.
Definition: view.h:309
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
Definition: katetextblock.h:22
Q_SCRIPTABLE bool focusIn(int ms=-1)
void readSessionConfig(const KConfigGroup &config, const QSet< QString > &flags=QSet< QString >()) override
Read session settings from the given config.
KateDocumentConfig * config()
Configuration.
Q_SCRIPTABLE bool focusOut(int ms=-1)
A source of inline notes for a document.
A KParts derived class representing a text document.
Definition: document.h:284
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Jun 6 2023 03:50:08 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.