KTextEditor

documentcursor.h
1 /* SPDX-License-Identifier: LGPL-2.0-or-later
2 
3  Copyright (C) 2010 Christoph Cullmann <[email protected]>
4  Copyright (C) 2012-2013 Dominik Haumann <[email protected]>
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 #ifndef KTEXTEDITOR_DOCUMENT_CURSOR_H
23 #define KTEXTEDITOR_DOCUMENT_CURSOR_H
24 
25 #include <ktexteditor/cursor.h>
26 #include <ktexteditor/document.h>
27 #include <ktexteditor_export.h>
28 
29 #include <QDebug>
30 
31 namespace KTextEditor
32 {
69 class KTEXTEDITOR_EXPORT DocumentCursor
70 {
71  //
72  // sub types
73  //
74 public:
78  enum WrapBehavior {
79  Wrap = 0x0,
80  NoWrap = 0x1
81  };
82 
83  //
84  // Constructor
85  //
86 public:
93 
97  DocumentCursor(KTextEditor::Document *document, const KTextEditor::Cursor &position);
98 
102  DocumentCursor(KTextEditor::Document *document, int line, int column);
103 
108  DocumentCursor(const DocumentCursor &other);
109 
110  //
111  // stuff that needs to be implemented by editor part cursors
112  //
113 public:
118  inline Document *document() const
119  {
120  return m_document;
121  }
122 
130  void setPosition(const KTextEditor::Cursor &position);
131 
136  inline int line() const
137  {
138  return m_cursor.line();
139  }
140 
145  inline int column() const
146  {
147  return m_cursor.column();
148  }
149 
154  {
155  }
156 
157  //
158  // forbidden stuff
159  //
160 private:
164  DocumentCursor();
165 
166  //
167  // convenience API
168  //
169 public:
176  inline bool isValid() const
177  {
178  return m_cursor.isValid();
179  }
180 
192  inline bool isValidTextPosition() const
193  {
194  return document()->isValidTextPosition(m_cursor);
195  }
196 
213  void makeValid();
214 
223  void setPosition(int line, int column);
224 
229  void setLine(int line);
230 
235  void setColumn(int column);
236 
242  bool atStartOfLine() const;
243 
249  bool atEndOfLine() const;
250 
256  bool atStartOfDocument() const;
257 
264  bool atEndOfDocument() const;
265 
273  bool gotoNextLine();
274 
282  bool gotoPreviousLine();
283 
295  bool move(int chars, WrapBehavior wrapBehavior = Wrap);
296 
301  inline Cursor toCursor() const
302  {
303  return m_cursor;
304  }
305 
310  inline operator Cursor() const
311  {
312  return m_cursor;
313  }
314 
315  //
316  // operators for: DocumentCursor <-> DocumentCursor
317  //
323  {
324  m_document = other.m_document;
325  m_cursor = other.m_cursor;
326  return *this;
327  }
328 
339  inline friend bool operator==(const DocumentCursor &c1, const DocumentCursor &c2)
340  {
341  return c1.document() == c2.document() && c1.line() == c2.line() && c1.column() == c2.column();
342  }
343 
350  inline friend bool operator!=(const DocumentCursor &c1, const DocumentCursor &c2)
351  {
352  return !(c1 == c2);
353  }
354 
362  inline friend bool operator>(const DocumentCursor &c1, const DocumentCursor &c2)
363  {
364  return c1.line() > c2.line() || (c1.line() == c2.line() && c1.column() > c2.column());
365  }
366 
374  inline friend bool operator>=(const DocumentCursor &c1, const DocumentCursor &c2)
375  {
376  return c1.line() > c2.line() || (c1.line() == c2.line() && c1.column() >= c2.column());
377  }
378 
386  inline friend bool operator<(const DocumentCursor &c1, const DocumentCursor &c2)
387  {
388  return !(c1 >= c2);
389  }
390 
398  inline friend bool operator<=(const DocumentCursor &c1, const DocumentCursor &c2)
399  {
400  return !(c1 > c2);
401  }
402 
409  inline friend QDebug operator<<(QDebug s, const DocumentCursor *cursor)
410  {
411  if (cursor) {
412  s.nospace() << "(" << cursor->document() << ": " << cursor->line() << ", " << cursor->column() << ")";
413  } else {
414  s.nospace() << "(null document cursor)";
415  }
416  return s.space();
417  }
418 
425  inline friend QDebug operator<<(QDebug s, const DocumentCursor &cursor)
426  {
427  return s << &cursor;
428  }
429 
430 private:
431  KTextEditor::Document *m_document;
432  KTextEditor::Cursor m_cursor;
433 };
434 
435 }
436 
437 Q_DECLARE_TYPEINFO(KTextEditor::DocumentCursor, Q_MOVABLE_TYPE);
438 
439 #endif
Document * document() const
Gets the document to which this cursor is bound.
friend bool operator>=(const DocumentCursor &c1, const DocumentCursor &c2)
Greater than or equal to operator.
DocumentCursor & operator=(const DocumentCursor &other)
Assignment operator.
friend bool operator!=(const DocumentCursor &c1, const DocumentCursor &c2)
Inequality operator.
bool isValidTextPosition() const
Check if this cursor is currently at a valid text position.
A Cursor which is bound to a specific Document.
QDebug & nospace()
The Cursor represents a position in a Document.
Definition: cursor.h:85
friend bool operator<=(const DocumentCursor &c1, const DocumentCursor &c2)
Less than or equal to operator.
friend bool operator==(const DocumentCursor &c1, const DocumentCursor &c2)
Equality operator.
A KParts derived class representing a text document.
Definition: document.h:199
bool isValid() const
Check if the current position of this cursor is a valid position, i.e.
int line() const
Retrieve the line on which this cursor is situated.
friend QDebug operator<<(QDebug s, const DocumentCursor *cursor)
qDebug() stream operator.
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
QDebug & space()
friend QDebug operator<<(QDebug s, const DocumentCursor &cursor)
qDebug() stream operator.
friend bool operator>(const DocumentCursor &c1, const DocumentCursor &c2)
Greater than operator.
friend bool operator<(const DocumentCursor &c1, const DocumentCursor &c2)
Less than operator.
Cursor toCursor() const
Convert this clever cursor into a dumb one.
int column() const
Retrieve the column on which this cursor is situated.
WrapBehavior
Wrap behavior for end of line treatement used in move().
~DocumentCursor()
Destruct the moving cursor.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Jul 14 2020 22:57:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.