KTextEditor

movingcursor.h
1 /*
2  SPDX-FileCopyrightText: 2010 Christoph Cullmann <[email protected]>
3 
4  Based on code of the SmartCursor/Range by:
5  SPDX-FileCopyrightText: 2003-2005 Hamish Rodda <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #ifndef KTEXTEDITOR_MOVINGCURSOR_H
11 #define KTEXTEDITOR_MOVINGCURSOR_H
12 
13 #include <ktexteditor/cursor.h>
14 #include <ktexteditor/document.h>
15 #include <ktexteditor_export.h>
16 
17 #include <QDebug>
18 
19 namespace KTextEditor
20 {
21 class MovingRange;
22 
54 class KTEXTEDITOR_EXPORT MovingCursor
55 {
56  //
57  // sub types
58  //
59 public:
65  StayOnInsert = 0x0,
66  MoveOnInsert = 0x1
67  };
68 
72  enum WrapBehavior {
73  Wrap = 0x0,
74  NoWrap = 0x1
75  };
76 
77  //
78  // stuff that needs to be implemented by editor part cursors
79  //
80 public:
85  virtual void setInsertBehavior(InsertBehavior insertBehavior) = 0;
86 
91  virtual InsertBehavior insertBehavior() const = 0;
92 
97  virtual Document *document() const = 0;
98 
103  virtual MovingRange *range() const = 0;
104 
110  virtual void setPosition(const KTextEditor::Cursor &position) = 0;
111 
116  virtual int line() const = 0;
117 
122  virtual int column() const = 0;
123 
127  virtual ~MovingCursor();
128 
129  //
130  // forbidden stuff
131  //
132 protected:
136  MovingCursor();
137 
138 private:
142  MovingCursor(const MovingCursor &);
143 
147  MovingCursor &operator=(const MovingCursor &);
148 
149  //
150  // convenience API
151  //
152 public:
159  inline bool isValid() const
160  {
161  return line() >= 0 && column() >= 0;
162  }
163 
177  inline bool isValidTextPosition() const
178  {
179  return document()->isValidTextPosition(toCursor());
180  }
181 
190  void setPosition(int line, int column);
191 
196  void setLine(int line);
197 
202  void setColumn(int column);
203 
209  bool atStartOfLine() const;
210 
216  bool atEndOfLine() const;
217 
223  bool atStartOfDocument() const;
224 
231  bool atEndOfDocument() const;
232 
240  bool gotoNextLine();
241 
249  bool gotoPreviousLine();
250 
262  bool move(int chars, WrapBehavior wrapBehavior = Wrap);
263 
269  const Cursor toCursor() const
270  {
271  return Cursor(line(), column());
272  }
273 
279  operator Cursor() const
280  {
281  return Cursor(line(), column());
282  }
283 
284  //
285  // operators for: MovingCursor <-> MovingCursor
286  //
297  inline friend bool operator==(const MovingCursor &c1, const MovingCursor &c2)
298  {
299  return c1.line() == c2.line() && c1.column() == c2.column();
300  }
301 
308  inline friend bool operator!=(const MovingCursor &c1, const MovingCursor &c2)
309  {
310  return !(c1 == c2);
311  }
312 
320  inline friend bool operator>(const MovingCursor &c1, const MovingCursor &c2)
321  {
322  return c1.line() > c2.line() || (c1.line() == c2.line() && c1.column() > c2.column());
323  }
324 
332  inline friend bool operator>=(const MovingCursor &c1, const MovingCursor &c2)
333  {
334  return c1.line() > c2.line() || (c1.line() == c2.line() && c1.column() >= c2.column());
335  }
336 
344  inline friend bool operator<(const MovingCursor &c1, const MovingCursor &c2)
345  {
346  return !(c1 >= c2);
347  }
348 
356  inline friend bool operator<=(const MovingCursor &c1, const MovingCursor &c2)
357  {
358  return !(c1 > c2);
359  }
360 
367  inline friend QDebug operator<<(QDebug s, const MovingCursor *cursor)
368  {
369  if (cursor) {
370  s.nospace() << "(" << cursor->line() << ", " << cursor->column() << ")";
371  } else {
372  s.nospace() << "(null cursor)";
373  }
374  return s.space();
375  }
376 
383  inline friend QDebug operator<<(QDebug s, const MovingCursor &cursor)
384  {
385  return s << &cursor;
386  }
387 };
388 
389 }
390 
391 #endif
friend bool operator<=(const MovingCursor &c1, const MovingCursor &c2)
Less than or equal to operator.
Definition: movingcursor.h:356
friend bool operator<(const MovingCursor &c1, const MovingCursor &c2)
Less than operator.
Definition: movingcursor.h:344
A Cursor which is bound to a specific Document, and maintains its position.
Definition: movingcursor.h:54
QDebug & nospace()
A range that is bound to a specific Document, and maintains its position.
Definition: movingrange.h:144
The Cursor represents a position in a Document.
Definition: cursor.h:71
InsertBehavior
Insert behavior of this cursor, should it stay if text is insert at its position or should it move...
Definition: movingcursor.h:64
A KParts derived class representing a text document.
Definition: document.h:185
bool isValidTextPosition() const
Check whether this MovingCursor is located at a valid text position.
Definition: movingcursor.h:177
virtual int column() const =0
Retrieve the column on which this cursor is situated.
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
Definition: katetextblock.h:21
const Cursor toCursor() const
Convert this clever cursor into a dumb one.
Definition: movingcursor.h:269
WrapBehavior
Wrap behavior for end of line treatement used in move().
Definition: movingcursor.h:72
friend QDebug operator<<(QDebug s, const MovingCursor &cursor)
qDebug() stream operator.
Definition: movingcursor.h:383
QDebug & space()
bool isValid() const
Returns whether the current position of this cursor is a valid position, i.e.
Definition: movingcursor.h:159
friend bool operator!=(const MovingCursor &c1, const MovingCursor &c2)
Inequality operator.
Definition: movingcursor.h:308
friend QDebug operator<<(QDebug s, const MovingCursor *cursor)
qDebug() stream operator.
Definition: movingcursor.h:367
friend bool operator>(const MovingCursor &c1, const MovingCursor &c2)
Greater than operator.
Definition: movingcursor.h:320
friend bool operator>=(const MovingCursor &c1, const MovingCursor &c2)
Greater than or equal to operator.
Definition: movingcursor.h:332
friend bool operator==(const MovingCursor &c1, const MovingCursor &c2)
Equality operator.
Definition: movingcursor.h:297
virtual int line() const =0
Retrieve the line on which this cursor is situated.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jan 27 2021 22:56:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.