KTextEditor

movingcursor.h
1 /* SPDX-License-Identifier: LGPL-2.0-or-later
2 
3  Copyright (C) 2010 Christoph Cullmann <[email protected]>
4 
5  Based on code of the SmartCursor/Range by:
6  Copyright (C) 2003-2005 Hamish Rodda <[email protected]>
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Library General Public
10  License as published by the Free Software Foundation; either
11  version 2 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU Library General Public License
19  along with this library; see the file COPYING.LIB. If not, write to
20  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  Boston, MA 02110-1301, USA.
22 */
23 
24 #ifndef KTEXTEDITOR_MOVINGCURSOR_H
25 #define KTEXTEDITOR_MOVINGCURSOR_H
26 
27 #include <ktexteditor/cursor.h>
28 #include <ktexteditor/document.h>
29 #include <ktexteditor_export.h>
30 
31 #include <QDebug>
32 
33 namespace KTextEditor
34 {
35 class MovingRange;
36 
68 class KTEXTEDITOR_EXPORT MovingCursor
69 {
70  //
71  // sub types
72  //
73 public:
79  StayOnInsert = 0x0,
80  MoveOnInsert = 0x1
81  };
82 
86  enum WrapBehavior {
87  Wrap = 0x0,
88  NoWrap = 0x1
89  };
90 
91  //
92  // stuff that needs to be implemented by editor part cursors
93  //
94 public:
99  virtual void setInsertBehavior(InsertBehavior insertBehavior) = 0;
100 
105  virtual InsertBehavior insertBehavior() const = 0;
106 
111  virtual Document *document() const = 0;
112 
117  virtual MovingRange *range() const = 0;
118 
124  virtual void setPosition(const KTextEditor::Cursor &position) = 0;
125 
130  virtual int line() const = 0;
131 
136  virtual int column() const = 0;
137 
141  virtual ~MovingCursor();
142 
143  //
144  // forbidden stuff
145  //
146 protected:
150  MovingCursor();
151 
152 private:
156  MovingCursor(const MovingCursor &);
157 
161  MovingCursor &operator=(const MovingCursor &);
162 
163  //
164  // convenience API
165  //
166 public:
173  inline bool isValid() const
174  {
175  return line() >= 0 && column() >= 0;
176  }
177 
191  inline bool isValidTextPosition() const
192  {
193  return document()->isValidTextPosition(toCursor());
194  }
195 
204  void setPosition(int line, int column);
205 
210  void setLine(int line);
211 
216  void setColumn(int column);
217 
223  bool atStartOfLine() const;
224 
230  bool atEndOfLine() const;
231 
237  bool atStartOfDocument() const;
238 
245  bool atEndOfDocument() const;
246 
254  bool gotoNextLine();
255 
263  bool gotoPreviousLine();
264 
276  bool move(int chars, WrapBehavior wrapBehavior = Wrap);
277 
283  const Cursor toCursor() const
284  {
285  return Cursor(line(), column());
286  }
287 
293  operator Cursor() const
294  {
295  return Cursor(line(), column());
296  }
297 
298  //
299  // operators for: MovingCursor <-> MovingCursor
300  //
311  inline friend bool operator==(const MovingCursor &c1, const MovingCursor &c2)
312  {
313  return c1.line() == c2.line() && c1.column() == c2.column();
314  }
315 
322  inline friend bool operator!=(const MovingCursor &c1, const MovingCursor &c2)
323  {
324  return !(c1 == c2);
325  }
326 
334  inline friend bool operator>(const MovingCursor &c1, const MovingCursor &c2)
335  {
336  return c1.line() > c2.line() || (c1.line() == c2.line() && c1.column() > c2.column());
337  }
338 
346  inline friend bool operator>=(const MovingCursor &c1, const MovingCursor &c2)
347  {
348  return c1.line() > c2.line() || (c1.line() == c2.line() && c1.column() >= c2.column());
349  }
350 
358  inline friend bool operator<(const MovingCursor &c1, const MovingCursor &c2)
359  {
360  return !(c1 >= c2);
361  }
362 
370  inline friend bool operator<=(const MovingCursor &c1, const MovingCursor &c2)
371  {
372  return !(c1 > c2);
373  }
374 
381  inline friend QDebug operator<<(QDebug s, const MovingCursor *cursor)
382  {
383  if (cursor) {
384  s.nospace() << "(" << cursor->line() << ", " << cursor->column() << ")";
385  } else {
386  s.nospace() << "(null cursor)";
387  }
388  return s.space();
389  }
390 
397  inline friend QDebug operator<<(QDebug s, const MovingCursor &cursor)
398  {
399  return s << &cursor;
400  }
401 };
402 
403 }
404 
405 #endif
friend bool operator<=(const MovingCursor &c1, const MovingCursor &c2)
Less than or equal to operator.
Definition: movingcursor.h:370
friend bool operator<(const MovingCursor &c1, const MovingCursor &c2)
Less than operator.
Definition: movingcursor.h:358
A Cursor which is bound to a specific Document, and maintains its position.
Definition: movingcursor.h:68
QDebug & nospace()
A range that is bound to a specific Document, and maintains its position.
Definition: movingrange.h:157
The Cursor represents a position in a Document.
Definition: cursor.h:85
InsertBehavior
Insert behavior of this cursor, should it stay if text is insert at its position or should it move...
Definition: movingcursor.h:78
A KParts derived class representing a text document.
Definition: document.h:199
bool isValidTextPosition() const
Check whether this MovingCursor is located at a valid text position.
Definition: movingcursor.h:191
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...
const Cursor toCursor() const
Convert this clever cursor into a dumb one.
Definition: movingcursor.h:283
WrapBehavior
Wrap behavior for end of line treatement used in move().
Definition: movingcursor.h:86
friend QDebug operator<<(QDebug s, const MovingCursor &cursor)
qDebug() stream operator.
Definition: movingcursor.h:397
QDebug & space()
bool isValid() const
Returns whether the current position of this cursor is a valid position, i.e.
Definition: movingcursor.h:173
friend bool operator!=(const MovingCursor &c1, const MovingCursor &c2)
Inequality operator.
Definition: movingcursor.h:322
friend QDebug operator<<(QDebug s, const MovingCursor *cursor)
qDebug() stream operator.
Definition: movingcursor.h:381
friend bool operator>(const MovingCursor &c1, const MovingCursor &c2)
Greater than operator.
Definition: movingcursor.h:334
friend bool operator>=(const MovingCursor &c1, const MovingCursor &c2)
Greater than or equal to operator.
Definition: movingcursor.h:346
friend bool operator==(const MovingCursor &c1, const MovingCursor &c2)
Equality operator.
Definition: movingcursor.h:311
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-2020 The KDE developers.
Generated on Thu Aug 6 2020 22:56:48 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.