KTextEditor

katelinelayout.cpp
1/*
2 SPDX-FileCopyrightText: 2002-2005 Hamish Rodda <rodda@kde.org>
3 SPDX-FileCopyrightText: 2003 Anakim Border <aborder@sources.sourceforge.net>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "katelinelayout.h"
9#include "katetextfolding.h"
10#include "katetextlayout.h"
11
12#include <QTextLine>
13
14#include "katepartdebug.h"
15
16#include "katedocument.h"
17#include "katerenderer.h"
18
19KateLineLayout::KateLineLayout(KateRenderer &renderer)
20 : m_renderer(renderer)
21 , m_line(-1)
22 , m_virtualLine(-1)
23{
24}
25
26void KateLineLayout::clear()
27{
28 m_textLine.reset();
29 m_line = -1;
30 m_virtualLine = -1;
31 shiftX = 0;
32 // not touching dirty
33 m_layout.clearLayout();
34 // not touching layout dirty
35}
36
37bool KateLineLayout::includesCursor(const KTextEditor::Cursor realCursor) const
38{
39 return realCursor.line() == line();
40}
41
42const Kate::TextLine &KateLineLayout::textLine(bool reloadForce) const
43{
44 if (reloadForce || !m_textLine) {
45 m_textLine.reset();
46 if (m_line >= 0 && m_line < m_renderer.doc()->lines()) {
47 m_textLine = usePlainTextLine ? m_renderer.doc()->plainKateTextLine(m_line) : m_renderer.doc()->kateTextLine(m_line);
48 }
49 }
50
51 Q_ASSERT(m_textLine);
52
53 return *m_textLine;
54}
55
56int KateLineLayout::line() const
57{
58 return m_line;
59}
60
61void KateLineLayout::setLine(int line, int virtualLine)
62{
63 m_line = line;
64 m_virtualLine = (virtualLine == -1) ? m_renderer.folding().lineToVisibleLine(line) : virtualLine;
65 m_textLine.reset();
66}
67
68int KateLineLayout::virtualLine() const
69{
70 return m_virtualLine;
71}
72
73void KateLineLayout::setVirtualLine(int virtualLine)
74{
75 m_virtualLine = virtualLine;
76}
77
78bool KateLineLayout::startsInvisibleBlock() const
79{
80 if (!isValid()) {
81 return false;
82 }
83
84 return (virtualLine() + 1) != m_renderer.folding().lineToVisibleLine(line() + 1);
85}
86
87bool KateLineLayout::isValid() const
88{
89 return line() != -1 && layout().lineCount() > 0 && (textLine(), m_textLine);
90}
91
92void KateLineLayout::endLayout()
93{
94 m_layout.endLayout();
95 layoutDirty = m_layout.lineCount() <= 0;
96 m_dirtyList.clear();
97 if (m_layout.lineCount() > 0) {
98 for (int i = 0; i < qMax(1, m_layout.lineCount()); ++i) {
99 m_dirtyList.append(true);
100 }
101 }
102}
103
104void KateLineLayout::invalidateLayout()
105{
106 layoutDirty = true;
107 m_dirtyList.clear();
108}
109
110bool KateLineLayout::isDirty(int viewLine) const
111{
112 Q_ASSERT(isValid() && viewLine >= 0 && viewLine < viewLineCount());
113 return m_dirtyList[viewLine];
114}
115
116bool KateLineLayout::setDirty(int viewLine, bool dirty)
117{
118 Q_ASSERT(isValid() && viewLine >= 0 && viewLine < viewLineCount());
119 m_dirtyList[viewLine] = dirty;
120 return dirty;
121}
122
123KTextEditor::Cursor KateLineLayout::start() const
124{
125 return KTextEditor::Cursor(line(), 0);
126}
127
128int KateLineLayout::length() const
129{
130 return textLine().length();
131}
132
133int KateLineLayout::viewLineCount() const
134{
135 return m_layout.lineCount();
136}
137
138KateTextLayout KateLineLayout::viewLine(int viewLine)
139{
140 if (viewLine < 0) {
141 viewLine += viewLineCount();
142 }
143 Q_ASSERT(isValid());
144 Q_ASSERT(viewLine >= 0 && viewLine < viewLineCount());
145 return KateTextLayout(this, viewLine);
146}
147
148int KateLineLayout::width() const
149{
150 int width = 0;
151
152 for (int i = 0; i < m_layout.lineCount(); ++i) {
153 width = qMax((int)m_layout.lineAt(i).naturalTextWidth(), width);
154 }
155
156 return width;
157}
158
159int KateLineLayout::widthOfLastLine()
160{
161 const KateTextLayout &lastLine = viewLine(viewLineCount() - 1);
162 return lastLine.width() + lastLine.xOffset();
163}
164
165bool KateLineLayout::isOutsideDocument() const
166{
167 return line() < 0 || line() >= m_renderer.doc()->lines();
168}
169
170void KateLineLayout::debugOutput() const
171{
172 qCDebug(LOG_KTE) << "KateLineLayout: " << this << " valid " << isValid() << " line " << line() << " length " << length() << " width " << width()
173 << " viewLineCount " << viewLineCount();
174}
175
176int KateLineLayout::viewLineForColumn(int column) const
177{
178 int len = 0;
179 int i = 0;
180 for (; i < m_layout.lineCount() - 1; ++i) {
181 len += m_layout.lineAt(i).textLength();
182 if (column < len) {
183 return i;
184 }
185 }
186 return i;
187}
188
189bool KateLineLayout::isRightToLeft() const
190{
191 return m_layout.textOption().textDirection() == Qt::RightToLeft;
192}
The Cursor represents a position in a Document.
Definition cursor.h:75
constexpr int line() const noexcept
Retrieve the line on which this cursor is situated.
Definition cursor.h:174
int lines() const override
Get the count of lines of the document.
Kate::TextLine plainKateTextLine(int i)
Return line lineno.
Kate::TextLine kateTextLine(int i)
Same as plainKateTextLine(), except that it is made sure the line is highlighted.
Handles all of the work of rendering the text (used for the views and printing)
Kate::TextFolding & folding() const
Returns the folding info to which this renderer is bound.
KTextEditor::DocumentPrivate * doc() const
Returns the document to which this renderer is bound.
This class represents one visible line of text; with dynamic wrapping, many KateTextLayouts can be ne...
int lineToVisibleLine(int line) const
Convert a text buffer line to a visible line number.
Class representing a single text line.
int length() const
Returns the line's length.
void append(QList< T > &&value)
void clear()
RightToLeft
void clearLayout()
void endLayout()
QTextLine lineAt(int i) const const
int lineCount() const const
const QTextOption & textOption() const const
qreal naturalTextWidth() const const
int textLength() const const
Qt::LayoutDirection textDirection() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.