| 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 | KateLineLayout::KateLineLayout() |
| 17 | : m_line(-1) |
| 18 | , m_virtualLine(-1) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | void KateLineLayout::clear() |
| 23 | { |
| 24 | m_line = -1; |
| 25 | m_virtualLine = -1; |
| 26 | shiftX = 0; |
| 27 | // not touching dirty |
| 28 | m_layout.clearLayout(); |
| 29 | // not touching layout dirty |
| 30 | } |
| 31 | |
| 32 | bool KateLineLayout::includesCursor(const KTextEditor::Cursor realCursor) const |
| 33 | { |
| 34 | return realCursor.line() == line(); |
| 35 | } |
| 36 | |
| 37 | int KateLineLayout::line() const |
| 38 | { |
| 39 | return m_line; |
| 40 | } |
| 41 | |
| 42 | void KateLineLayout::setLine(Kate::TextFolding &folding, int line, int virtualLine) |
| 43 | { |
| 44 | m_line = line; |
| 45 | m_virtualLine = (virtualLine == -1) ? folding.lineToVisibleLine(line) : virtualLine; |
| 46 | } |
| 47 | |
| 48 | int KateLineLayout::virtualLine() const |
| 49 | { |
| 50 | return m_virtualLine; |
| 51 | } |
| 52 | |
| 53 | void KateLineLayout::setVirtualLine(int virtualLine) |
| 54 | { |
| 55 | m_virtualLine = virtualLine; |
| 56 | } |
| 57 | |
| 58 | bool KateLineLayout::startsInvisibleBlock(Kate::TextFolding &folding) const |
| 59 | { |
| 60 | if (!isValid()) { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | return (virtualLine() + 1) != folding.lineToVisibleLine(line: line() + 1); |
| 65 | } |
| 66 | |
| 67 | bool KateLineLayout::isValid() const |
| 68 | { |
| 69 | return line() != -1 && layout().lineCount() > 0; |
| 70 | } |
| 71 | |
| 72 | void KateLineLayout::endLayout() |
| 73 | { |
| 74 | m_layout.endLayout(); |
| 75 | layoutDirty = m_layout.lineCount() <= 0; |
| 76 | m_dirtyList.clear(); |
| 77 | if (m_layout.lineCount() > 0) { |
| 78 | for (int i = 0; i < qMax(a: 1, b: m_layout.lineCount()); ++i) { |
| 79 | m_dirtyList.append(t: true); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void KateLineLayout::invalidateLayout() |
| 85 | { |
| 86 | layoutDirty = true; |
| 87 | m_dirtyList.clear(); |
| 88 | } |
| 89 | |
| 90 | bool KateLineLayout::isDirty(int viewLine) const |
| 91 | { |
| 92 | Q_ASSERT(isValid() && viewLine >= 0 && viewLine < viewLineCount()); |
| 93 | return m_dirtyList[viewLine]; |
| 94 | } |
| 95 | |
| 96 | bool KateLineLayout::setDirty(int viewLine, bool dirty) |
| 97 | { |
| 98 | Q_ASSERT(isValid() && viewLine >= 0 && viewLine < viewLineCount()); |
| 99 | m_dirtyList[viewLine] = dirty; |
| 100 | return dirty; |
| 101 | } |
| 102 | |
| 103 | KTextEditor::Cursor KateLineLayout::start() const |
| 104 | { |
| 105 | return KTextEditor::Cursor(line(), 0); |
| 106 | } |
| 107 | |
| 108 | int KateLineLayout::viewLineCount() const |
| 109 | { |
| 110 | return m_layout.lineCount(); |
| 111 | } |
| 112 | |
| 113 | KateTextLayout KateLineLayout::viewLine(int viewLine) |
| 114 | { |
| 115 | if (viewLine < 0) { |
| 116 | viewLine += viewLineCount(); |
| 117 | } |
| 118 | Q_ASSERT(isValid()); |
| 119 | Q_ASSERT(viewLine >= 0 && viewLine < viewLineCount()); |
| 120 | return KateTextLayout(this, viewLine); |
| 121 | } |
| 122 | |
| 123 | int KateLineLayout::width() const |
| 124 | { |
| 125 | int width = 0; |
| 126 | |
| 127 | for (int i = 0; i < m_layout.lineCount(); ++i) { |
| 128 | width = qMax(a: (int)m_layout.lineAt(i).naturalTextWidth(), b: width); |
| 129 | } |
| 130 | |
| 131 | return width; |
| 132 | } |
| 133 | |
| 134 | int KateLineLayout::widthOfLastLine() |
| 135 | { |
| 136 | const KateTextLayout &lastLine = viewLine(viewLine: viewLineCount() - 1); |
| 137 | return lastLine.width() + lastLine.xOffset(); |
| 138 | } |
| 139 | |
| 140 | void KateLineLayout::debugOutput() const |
| 141 | { |
| 142 | qCDebug(LOG_KTE) << "KateLineLayout: "<< this << " valid "<< isValid() << " line "<< line() << " width "<< width() << " viewLineCount " |
| 143 | << viewLineCount(); |
| 144 | } |
| 145 | |
| 146 | int KateLineLayout::viewLineForColumn(int column) const |
| 147 | { |
| 148 | int len = 0; |
| 149 | int i = 0; |
| 150 | for (; i < m_layout.lineCount() - 1; ++i) { |
| 151 | len += m_layout.lineAt(i).textLength(); |
| 152 | if (column < len) { |
| 153 | return i; |
| 154 | } |
| 155 | } |
| 156 | return i; |
| 157 | } |
| 158 | |
| 159 | bool KateLineLayout::isRightToLeft() const |
| 160 | { |
| 161 | return m_layout.textOption().textDirection() == Qt::RightToLeft; |
| 162 | } |
| 163 |
