• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KTextEditor

  • interfaces
  • ktexteditor
movingcursor.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) 2010 Dominik Haumann <dhaumann kde org>
5 *
6 * Based on code of the SmartCursor/Range by:
7 * Copyright (C) 2003-2005 Hamish Rodda <rodda@kde.org>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24
25#include "movingcursor.h"
26#include "document.h"
27
28using namespace KTextEditor;
29
30MovingCursor::MovingCursor ()
31{
32}
33
34MovingCursor::~MovingCursor ()
35{
36}
37
38void MovingCursor::setPosition(int line, int column)
39{
40 // just use setPosition
41 setPosition(Cursor(line, column));
42}
43
44void MovingCursor::setLine (int line)
45{
46 // just use setPosition
47 setPosition (line, column());
48}
49
50void MovingCursor::setColumn (int column)
51{
52 // just use setPosition
53 setPosition (line(), column);
54}
55
56bool MovingCursor::atStartOfLine() const {
57 return isValidTextPosition() && column() == 0;
58}
59
60bool MovingCursor::atEndOfLine() const {
61 return isValidTextPosition() && column() == document()->lineLength(line());
62}
63
64bool MovingCursor::atEndOfDocument() const {
65 return *this == document()->documentEnd();
66}
67
68bool MovingCursor::atStartOfDocument() const {
69 return line() == 0 && column() == 0;
70}
71
72bool MovingCursor::gotoNextLine()
73{
74 // only touch valid cursors
75 const bool ok = isValid() && (line() + 1 < document()->lines());
76
77 if (ok) {
78 setPosition(Cursor(line() + 1, 0));
79 }
80
81 return ok;
82}
83
84bool MovingCursor::gotoPreviousLine()
85{
86 // only touch valid cursors
87 bool ok = (line() > 0) && (column() >= 0);
88
89 if (ok) {
90 setPosition(Cursor(line() - 1, 0));
91 }
92
93 return ok;
94}
95
96bool MovingCursor::move(int chars, WrapBehavior wrapBehavior)
97{
98 if (!isValid()) {
99 return false;
100 }
101
102 Cursor c(toCursor());
103
104 // special case: cursor position is not in valid text, then the algo does
105 // not work for Wrap mode. Hence, catch this special case by setting
106 // c.column() to the lineLength()
107 if (chars > 0 && wrapBehavior == Wrap && c.column() > document()->lineLength(c.line())) {
108 c.setColumn(document()->lineLength(c.line()));
109 }
110
111 while (chars != 0) {
112 if (chars > 0) {
113 if (wrapBehavior == Wrap) {
114 int advance = qMin(document()->lineLength(c.line()) - c.column(), chars);
115
116 if (chars > advance) {
117 if (c.line() + 1 >= document()->lines()) {
118 return false;
119 }
120
121 c.setPosition(c.line() + 1, 0);
122 chars -= advance + 1; // +1 because of end-of-line wrap
123 } else {
124 c.setColumn(c.column() + chars);
125 chars = 0;
126 }
127 } else { // NoWrap
128 c.setColumn(c.column() + chars);
129 chars = 0;
130 }
131 } else {
132 int back = qMin(c.column(), -chars);
133 if (-chars > back) {
134 if (c.line() == 0)
135 return false;
136
137 c.setPosition(c.line() - 1, document()->lineLength(c.line() - 1));
138 chars += back + 1; // +1 because of wrap-around at start-of-line
139 } else {
140 c.setColumn(c.column() + chars);
141 chars = 0;
142 }
143 }
144 }
145
146 if (c != *this) {
147 setPosition(c);
148 }
149 return true;
150}
151
152// kate: space-indent on; indent-width 2; replace-tabs on;
KTextEditor::Cursor
An object which represents a position in a Document.
Definition: cursor.h:62
KTextEditor::Cursor::column
int column() const
Retrieve the column on which this cursor is situated.
Definition: cursor.cpp:79
KTextEditor::Cursor::setColumn
virtual void setColumn(int column)
Set the cursor column to column.
Definition: cursor.cpp:84
KTextEditor::Cursor::setPosition
virtual void setPosition(const Cursor &position)
Set the current cursor position to position.
Definition: cursor.cpp:96
KTextEditor::Cursor::line
virtual int line() const
Retrieve the line on which this cursor is situated.
Definition: cursor.cpp:62
KTextEditor::Document::lines
virtual int lines() const =0
Get the count of lines of the document.
KTextEditor::Document::lineLength
virtual int lineLength(int line) const =0
Get the length of a given line in characters.
KTextEditor::Document::documentEnd
virtual Cursor documentEnd() const =0
End position of the document.
KTextEditor::MovingCursor::atStartOfDocument
bool atStartOfDocument() const
Determine if this cursor is located at line 0 and column 0.
Definition: movingcursor.cpp:68
KTextEditor::MovingCursor::move
bool move(int chars, WrapBehavior wrapBehavior=Wrap)
Moves the cursor chars character forward or backwards.
Definition: movingcursor.cpp:96
KTextEditor::MovingCursor::toCursor
const Cursor toCursor() const
Convert this clever cursor into a dumb one.
Definition: movingcursor.h:271
KTextEditor::MovingCursor::isValid
bool isValid() const
Returns whether the current position of this cursor is a valid position, i.e.
Definition: movingcursor.h:171
KTextEditor::MovingCursor::WrapBehavior
WrapBehavior
Wrap behavior for end of line treatement used in move().
Definition: movingcursor.h:84
KTextEditor::MovingCursor::Wrap
@ Wrap
wrap at end of line
Definition: movingcursor.h:85
KTextEditor::MovingCursor::gotoNextLine
bool gotoNextLine()
Moves the cursor to the next line and sets the column to 0.
Definition: movingcursor.cpp:72
KTextEditor::MovingCursor::document
virtual Document * document() const =0
Gets the document to which this cursor is bound.
KTextEditor::MovingCursor::setPosition
virtual void setPosition(const KTextEditor::Cursor &position)=0
Set the current cursor position to position.
KTextEditor::MovingCursor::~MovingCursor
virtual ~MovingCursor()
Destruct the moving cursor.
Definition: movingcursor.cpp:34
KTextEditor::MovingCursor::gotoPreviousLine
bool gotoPreviousLine()
Moves the cursor to the previous line and sets the column to 0.
Definition: movingcursor.cpp:84
KTextEditor::MovingCursor::setLine
void setLine(int line)
Set the cursor line to line.
Definition: movingcursor.cpp:44
KTextEditor::MovingCursor::MovingCursor
MovingCursor()
For inherited class only.
Definition: movingcursor.cpp:30
KTextEditor::MovingCursor::atEndOfLine
bool atEndOfLine() const
Determine if this cursor is located at the end of the current line.
Definition: movingcursor.cpp:60
KTextEditor::MovingCursor::isValidTextPosition
bool isValidTextPosition() const
Check whether the current position of this cursor is a valid text position.
Definition: movingcursor.h:180
KTextEditor::MovingCursor::column
virtual int column() const =0
Retrieve the column on which this cursor is situated.
KTextEditor::MovingCursor::atEndOfDocument
bool atEndOfDocument() const
Determine if this cursor is located at the end of the last line in the document.
Definition: movingcursor.cpp:64
KTextEditor::MovingCursor::setColumn
void setColumn(int column)
Set the cursor column to column.
Definition: movingcursor.cpp:50
KTextEditor::MovingCursor::line
virtual int line() const =0
Retrieve the line on which this cursor is situated.
KTextEditor::MovingCursor::atStartOfLine
bool atStartOfLine() const
Determine if this cursor is located at column 0 of a valid text line.
Definition: movingcursor.cpp:56
document.h
movingcursor.h
back
KAction * back(const QObject *recvr, const char *slot, QObject *parent)
ok
KGuiItem ok()
KTextEditor
Namespace for the KDE Text Editor Interfaces.
Definition: annotationinterface.h:31
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KTextEditor

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

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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