• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • kte5
documentcursor.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2  *
3  * Copyright (C) 2010 Christoph Cullmann <cullmann@kde.org>
4  * Copyright (C) 2012 Dominik Haumann <dhaumann@kde.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #include "documentcursor.h"
23 
24 namespace KTextEditor {
25 
26 DocumentCursor::DocumentCursor(KTextEditor::Document* document)
27  : m_document(document)
28  , m_cursor(KTextEditor::Cursor::invalid())
29 {
30  // we require a valid document
31  Q_ASSERT(m_document);
32 }
33 
34 DocumentCursor::DocumentCursor(KTextEditor::Document* document, const KTextEditor::Cursor& position)
35  : m_document(document)
36  , m_cursor(position)
37 {
38  // we require a valid document
39  Q_ASSERT(m_document);
40 }
41 
42 DocumentCursor::DocumentCursor(KTextEditor::Document* document, int line, int column)
43  : m_document(document)
44  , m_cursor(line, column)
45 {
46  // we require a valid document
47  Q_ASSERT(m_document);
48 }
49 
50 DocumentCursor::DocumentCursor (const DocumentCursor &other)
51 : m_document(other.m_document)
52 , m_cursor(other.m_cursor)
53 {
54 }
55 
56 DocumentCursor& DocumentCursor::operator= (const DocumentCursor &other)
57 {
58  m_document = other.m_document;
59  m_cursor = other.m_cursor;
60  return *this;
61 }
62 
63 KTextEditor::Document *DocumentCursor::document () const
64 {
65  return m_document;
66 }
67 
68 void DocumentCursor::setPosition (const KTextEditor::Cursor& position)
69 {
70  if (position.isValid()) {
71  m_cursor = position;
72  } else {
73  m_cursor = KTextEditor::Cursor::invalid();
74  }
75 }
76 
77 int DocumentCursor::line() const
78 {
79  return m_cursor.line();
80 }
81 
82 int DocumentCursor::column() const
83 {
84  return m_cursor.column();
85 }
86 
87 DocumentCursor::~DocumentCursor ()
88 {
89 }
90 
91 void DocumentCursor::makeValid()
92 {
93  const int line = m_cursor.line();
94  const int col = m_cursor.line();
95 
96  if (line < 0) {
97  m_cursor.setPosition(0, 0);
98  } else if (line >= m_document->lines()) {
99  m_cursor = m_document->documentEnd();
100  } else if (col > m_document->lineLength(line)) {
101  m_cursor.setColumn(m_document->lineLength(line));
102  }
103 // TODO KDE5 if QChar::isLowSurrogate() -> move one to the left.
104 // } else if (m_document->character(m_cursor).isLowSurrogate()) {
105 // Q_ASSERT(col > 0);
106 // m_cursor.setColumn(col - 1);
107 // }
108 
109  Q_ASSERT(isValidTextPosition());
110 }
111 
112 void DocumentCursor::setPosition (int line, int column)
113 {
114  m_cursor.setPosition(line, column);
115 }
116 
117 void DocumentCursor::setLine(int line)
118 {
119  setPosition(line, column());
120 }
121 
122 void DocumentCursor::setColumn(int column)
123 {
124  setPosition(line(), column);
125 }
126 
127 bool DocumentCursor::atStartOfLine() const
128 {
129  return isValidTextPosition() && column() == 0;
130 }
131 
132 bool DocumentCursor::atEndOfLine() const
133 {
134  return isValidTextPosition() && column() == document()->lineLength(line());
135 }
136 
137 bool DocumentCursor::atStartOfDocument() const
138 {
139  return line() == 0 && column() == 0;
140 }
141 
142 bool DocumentCursor::atEndOfDocument() const
143 {
144  return m_cursor == document()->documentEnd();
145 }
146 
147 bool DocumentCursor::gotoNextLine()
148 {
149  // only allow valid cursors
150  const bool ok = isValid() && (line() + 1 < document()->lines());
151 
152  if (ok) {
153  setPosition(Cursor(line() + 1, 0));
154  }
155 
156  return ok;
157 }
158 
159 bool DocumentCursor::gotoPreviousLine()
160 {
161  // only allow valid cursors
162  bool ok = (line() > 0) && (column() >= 0);
163 
164  if (ok) {
165  setPosition(Cursor(line() - 1, 0));
166  }
167 
168  return ok;
169 }
170 
171 bool DocumentCursor::move(int chars, WrapBehavior wrapBehavior)
172 {
173  if (!isValid()) {
174  return false;
175  }
176 
177  Cursor c(m_cursor);
178 
179  // cache lineLength to minimize calls of KateDocument::lineLength(), as
180  // results in locating the correct block in the text buffer every time,
181  // which is relatively slow
182  int lineLength = document()->lineLength(c.line());
183 
184  // special case: cursor position is not in valid text, then the algo does
185  // not work for Wrap mode. Hence, catch this special case by setting
186  // c.column() to the lineLength()
187  if (chars > 0 && wrapBehavior == Wrap && c.column() > lineLength) {
188  c.setColumn(lineLength);
189  }
190 
191  while (chars != 0) {
192  if (chars > 0) {
193  if (wrapBehavior == Wrap) {
194  int advance = qMin(lineLength - c.column(), chars);
195 
196  if (chars > advance) {
197  if (c.line() + 1 >= document()->lines()) {
198  return false;
199  }
200 
201  c.setPosition(c.line() + 1, 0);
202  chars -= advance + 1; // +1 because of end-of-line wrap
203 
204  // advanced one line, so cache correct line length again
205  lineLength = document()->lineLength(c.line());
206  } else {
207  c.setColumn(c.column() + chars);
208  chars = 0;
209  }
210  } else { // NoWrap
211  c.setColumn(c.column() + chars);
212  chars = 0;
213  }
214  } else {
215  int back = qMin(c.column(), -chars);
216  if (-chars > back) {
217  if (c.line() == 0)
218  return false;
219 
220  c.setPosition(c.line() - 1, document()->lineLength(c.line() - 1));
221  chars += back + 1; // +1 because of wrap-around at start-of-line
222 
223  // advanced one line, so cache correct line length again
224  lineLength = document()->lineLength(c.line());
225  } else {
226  c.setColumn(c.column() + chars);
227  chars = 0;
228  }
229  }
230  }
231 
232  if (c != m_cursor) {
233  setPosition(c);
234  }
235  return true;
236 }
237 
238 const Cursor& DocumentCursor::toCursor () const
239 {
240  return m_cursor;
241 }
242 
243 DocumentCursor::operator const Cursor& () const
244 {
245  return m_cursor;
246 }
247 
248 }
249 
250 // kate: space-indent on; indent-width 2; replace-tabs on;
KTextEditor::DocumentCursor::operator=
DocumentCursor & operator=(const DocumentCursor &other)
Assignment operator.
Definition: documentcursor.cpp:56
documentcursor.h
KTextEditor::DocumentCursor::Wrap
wrap at end of line
Definition: documentcursor.h:79
KTextEditor::DocumentCursor::makeValid
void makeValid()
Make sure the cursor position is at a valid text position according to the following rules...
Definition: documentcursor.cpp:91
KTextEditor::DocumentCursor::isValidTextPosition
bool isValidTextPosition() const
Check whether the current position of this cursor is a valid text position.
Definition: documentcursor.h:176
KTextEditor::DocumentCursor::atStartOfDocument
bool atStartOfDocument() const
Determine if this cursor is located at line 0 and column 0.
Definition: documentcursor.cpp:137
KTextEditor::DocumentCursor
A Cursor which is bound to a specific Document.
Definition: documentcursor.h:69
KTextEditor::DocumentCursor::move
bool move(int chars, WrapBehavior wrapBehavior=Wrap)
Moves the cursor chars character forward or backwards.
Definition: documentcursor.cpp:171
KTextEditor::DocumentCursor::atEndOfDocument
bool atEndOfDocument() const
Determine if this cursor is located at the end of the last line in the document.
Definition: documentcursor.cpp:142
KTextEditor::DocumentCursor::gotoNextLine
bool gotoNextLine()
Moves the cursor to the next line and sets the column to 0.
Definition: documentcursor.cpp:147
KTextEditor::DocumentCursor::gotoPreviousLine
bool gotoPreviousLine()
Moves the cursor to the previous line and sets the column to 0.
Definition: documentcursor.cpp:159
KTextEditor::DocumentCursor::atStartOfLine
bool atStartOfLine() const
Determine if this cursor is located at column 0 of a valid text line.
Definition: documentcursor.cpp:127
KTextEditor::DocumentCursor::toCursor
const Cursor & toCursor() const
Convert this clever cursor into a dumb one.
Definition: documentcursor.cpp:238
KTextEditor::DocumentCursor::isValid
bool isValid() const
Returns whether the current position of this cursor is a valid position, i.e.
Definition: documentcursor.h:166
KTextEditor::DocumentCursor::line
int line() const
Retrieve the line on which this cursor is situated.
Definition: documentcursor.cpp:77
KTextEditor::DocumentCursor::setLine
void setLine(int line)
Set the cursor line to line.
Definition: documentcursor.cpp:117
KTextEditor::DocumentCursor::column
int column() const
Retrieve the column on which this cursor is situated.
Definition: documentcursor.cpp:82
KTextEditor::DocumentCursor::WrapBehavior
WrapBehavior
Wrap behavior for end of line treatement used in move().
Definition: documentcursor.h:78
KTextEditor::DocumentCursor::atEndOfLine
bool atEndOfLine() const
Determine if this cursor is located at the end of the current line.
Definition: documentcursor.cpp:132
KTextEditor::DocumentCursor::setColumn
void setColumn(int column)
Set the cursor column to column.
Definition: documentcursor.cpp:122
KTextEditor::DocumentCursor::document
Document * document() const
Gets the document to which this cursor is bound.
Definition: documentcursor.cpp:63
KTextEditor::DocumentCursor::setPosition
void setPosition(const KTextEditor::Cursor &position)
Set the current cursor position to position.
Definition: documentcursor.cpp:68
KTextEditor::DocumentCursor::~DocumentCursor
~DocumentCursor()
Destruct the moving cursor.
Definition: documentcursor.cpp:87
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:57 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal