KTextEditor

katelayoutcache.h
1 /*
2  SPDX-FileCopyrightText: 2005 Hamish Rodda <[email protected]>
3  SPDX-FileCopyrightText: 2008-2018 Dominik Haumann <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #ifndef KATELAYOUTCACHE_H
9 #define KATELAYOUTCACHE_H
10 
11 #include <QPair>
12 
13 #include <ktexteditor/range.h>
14 
15 #include "katetextlayout.h"
16 
17 class KateRenderer;
18 
19 class KateLineLayoutMap
20 {
21 public:
22  inline void clear();
23 
24  inline bool contains(int i) const;
25 
26  inline void insert(int realLine, const KateLineLayoutPtr &lineLayoutPtr);
27 
28  inline void viewWidthIncreased();
29  inline void viewWidthDecreased(int newWidth);
30 
31  inline void relayoutLines(int startRealLine, int endRealLine);
32 
33  inline void slotEditDone(int fromLine, int toLine, int shiftAmount);
34 
35  KateLineLayoutPtr &operator[](int i);
36 
37  typedef QPair<int, KateLineLayoutPtr> LineLayoutPair;
38 
39 private:
40  typedef std::vector<LineLayoutPair> LineLayoutMap;
41  LineLayoutMap m_lineLayouts;
42 };
43 
44 /**
45  * This class handles Kate's caching of layouting information (in KateLineLayout
46  * and KateTextLayout). This information is used primarily by both the view and
47  * the renderer.
48  *
49  * We outsource the hardcore layouting logic to the renderer, but other than
50  * that, this class handles all manipulation of the layout objects.
51  *
52  * This is separate from the renderer 1) for clarity 2) so you can have separate
53  * caches for separate views of the same document, even for view and printer
54  * (if the renderer is made to support rendering onto different targets).
55  *
56  * @author Hamish Rodda <[email protected]>
57  */
58 
59 class KateLayoutCache : public QObject
60 {
61  Q_OBJECT
62 
63 public:
64  explicit KateLayoutCache(KateRenderer *renderer, QObject *parent);
65 
66  void clear();
67 
68  int viewWidth() const;
69  void setViewWidth(int width);
70 
71  bool wrap() const;
72  void setWrap(bool wrap);
73 
74  bool acceptDirtyLayouts() const;
75  void setAcceptDirtyLayouts(bool accept);
76 
77  // BEGIN generic methods to get/set layouts
78  /**
79  * Returns the KateLineLayout for the specified line.
80  *
81  * If one does not exist, it will be created and laid out.
82  * Layouts which are not directly part of the view will be kept until the
83  * cache is full or until they are invalidated by other means (eg. the text
84  * changes).
85  *
86  * \param realLine real line number of the layout to retrieve.
87  * \param virtualLine virtual line number. only needed if you think it may have changed
88  * (ie. basically internal to KateLayoutCache)
89  */
90  KateLineLayoutPtr line(int realLine, int virtualLine = -1);
91  /// \overload
92  KateLineLayoutPtr line(const KTextEditor::Cursor realCursor);
93 
94  /// Returns the layout describing the text line which is occupied by \p realCursor.
96 
97  /// Returns the layout of the specified realLine + viewLine.
98  /// if viewLine is -1, return the last.
99  KateTextLayout textLayout(uint realLine, int viewLine);
100  // END
101 
102  // BEGIN methods to do with the caching of lines visible within a view
103  /// Returns the layout of the corresponding line in the view
105 
106  /**
107  * Find the view line of the cursor, relative to the display (0 = top line of view, 1 = second line, etc.)
108  *
109  * If @p limitToVisible is true, the function can return -2 for lines below the view. The idea is to get extra
110  * information about where the line lies when its out of the view so the clients doesn't have to make second
111  * call of this function with limitToVisible = false and potentionaly rerendering the whole document.
112  *
113  * \param virtualCursor cursor position
114  * \param limitToVisible if true, limit the search to only visible lines
115  *
116  * @return line number relative to the display. If @p limitToVisible is true,
117  * then valid values are only positive, negative values are invalid cursors for -1 and -2 for cursor is
118  * below the view.
119  */
120  int displayViewLine(const KTextEditor::Cursor virtualCursor, bool limitToVisible = false);
121 
122  int viewCacheLineCount() const;
123  KTextEditor::Cursor viewCacheStart() const;
124  KTextEditor::Cursor viewCacheEnd() const;
125  void updateViewCache(const KTextEditor::Cursor startPos, int newViewLineCount = -1, int viewLinesScrolled = 0);
126 
127  void relayoutLines(int startRealLine, int endRealLine);
128 
129  // find the index of the last view line for a specific line
130  int lastViewLine(int realLine);
131  // find the view line of cursor c (0 = same line, 1 = down one, etc.)
132  int viewLine(const KTextEditor::Cursor realCursor);
133  int viewLineCount(int realLine);
134 
135  void viewCacheDebugOutput() const;
136  // END
137 
138 private Q_SLOTS:
139  void wrapLine(const KTextEditor::Cursor position);
140  void unwrapLine(int line);
141  void insertText(const KTextEditor::Cursor position, const QString &text);
142  void removeText(KTextEditor::Range range);
143 
144 private:
145  KateRenderer *m_renderer;
146 
147  /**
148  * The master cache of all line layouts.
149  *
150  * Layouts which are not within the current view cache and whose
151  * refcount == 1 are only known to the cache and can be safely deleted.
152  */
153  mutable KateLineLayoutMap m_lineLayouts;
154 
155  // Convenience vector for quick direct access to the specific text layout
156  KTextEditor::Cursor m_startPos;
157 
158  mutable std::vector<KateTextLayout> m_textLayouts;
159 
160  int m_viewWidth;
161  bool m_wrap;
162  bool m_acceptDirtyLayouts;
163 };
164 
165 #endif
Q_OBJECTQ_OBJECT
Q_SLOTSQ_SLOTS
int displayViewLine(const KTextEditor::Cursor virtualCursor, bool limitToVisible=false)
Find the view line of the cursor, relative to the display (0 = top line of view, 1 = second line,...
An object representing a section of text, from one Cursor to another.
KateLineLayoutPtr line(int realLine, int virtualLine=-1)
Returns the KateLineLayout for the specified line.
bool insert(Part *part, qint64 *insertId=nullptr)
This class represents one visible line of text; with dynamic wrapping, many KateTextLayouts can be ne...
This class handles Kate's caching of layouting information (in KateLineLayout and KateTextLayout).
The Cursor represents a position in a Document.
Definition: cursor.h:71
QAction * clear(const QObject *recvr, const char *slot, QObject *parent)
Handles all of the work of rendering the text (used for the views and printing)
Definition: katerenderer.h:48
KateTextLayout textLayout(const KTextEditor::Cursor realCursor)
Returns the layout describing the text line which is occupied by realCursor.
QObject * parent() const const
KateTextLayout & viewLine(int viewLine)
Returns the layout of the corresponding line in the view.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 03:48:54 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.