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

KTextEditor

  • interfaces
  • ktexteditor
smartcursor.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 Copyright (C) 2003-2005 Hamish Rodda <rodda@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*/
18
19#include "smartcursor.h"
20
21#include "document.h"
22#include "smartrange.h"
23
24using namespace KTextEditor;
25
26SmartCursor::SmartCursor( const Cursor & position, Document * doc, InsertBehavior insertBehavior )
27 : Cursor(position)
28 , m_doc(doc)
29 , m_moveOnInsert(insertBehavior == MoveOnInsert)
30{
31 Q_ASSERT(m_doc);
32}
33
34SmartCursor::~ SmartCursor( )
35{
36}
37
38bool SmartCursor::isValid( ) const
39{
40 return m_doc->cursorInText(*this);
41}
42
43bool SmartCursor::atEndOfDocument( ) const
44{
45 return *this >= m_doc->documentEnd();
46}
47
48bool SmartCursor::isSmartCursor( ) const
49{
50 return true;
51}
52
53bool SmartCursor::insertText( const QStringList & text, bool block )
54{
55 return document()->insertText(*this, text, block);
56}
57
58QChar SmartCursor::character( ) const
59{
60 return document()->character(*this);
61}
62
63SmartRange * SmartCursor::smartRange( ) const
64{
65 return static_cast<SmartRange*>(m_range);
66}
67
68Document* SmartCursor::document() const
69{
70 return m_doc;
71}
72
73bool SmartCursor::atEndOfLine( ) const
74{
75 return column() == m_doc->lineLength(line());
76}
77
78SmartCursor::InsertBehavior SmartCursor::insertBehavior( ) const
79{
80 return m_moveOnInsert ? MoveOnInsert : StayOnInsert;
81}
82
83void SmartCursor::setInsertBehavior( InsertBehavior insertBehavior )
84{
85 m_moveOnInsert = insertBehavior == MoveOnInsert;
86}
87
88SmartCursor * SmartCursor::toSmartCursor( ) const
89{
90 return const_cast<SmartCursor*>(this);
91}
92
93bool SmartCursor::advance(int distance, AdvanceMode mode)
94{
95 Cursor c = *this;
96 if (mode == ByCharacter) {
97 while (distance) {
98 int lineLength = document()->lineLength(c.line());
99
100 if (distance > 0) {
101 int advance = qMin(lineLength - c.column(), distance);
102
103 if (distance > advance) {
104 if (c.line() + 1 >= document()->lines())
105 return false;
106
107 c.setPosition(c.line() + 1, 0);
108 // Account for end of line advancement
109 distance -= advance + 1;
110
111 } else {
112 c.setColumn(c.column() + distance);
113 distance = 0;
114 }
115
116 } else {
117 int back = qMin(c.column(), -distance);
118 if (-distance > back) {
119 if (c.line() - 1 < 0)
120 return false;
121
122 c.setPosition(c.line() - 1, document()->lineLength(c.line() - 1));
123 // Account for end of line advancement
124 distance += back + 1;
125
126 } else {
127 c.setColumn(c.column() + distance);
128 distance = 0;
129 }
130 }
131 }
132
133 } else {
134 // Not supported by the interface alone
135 return false;
136 }
137
138 setPosition(c);
139 return true;
140}
141
142// 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::Cursor::m_range
Range * m_range
Definition: cursor.h:352
KTextEditor::Document
A KParts derived class representing a text document.
Definition: document.h:112
KTextEditor::Document::character
virtual QChar character(const Cursor &position) const =0
Get the character at cursor.
KTextEditor::Document::lineLength
virtual int lineLength(int line) const =0
Get the length of a given line in characters.
KTextEditor::Document::cursorInText
virtual bool cursorInText(const Cursor &cursor)
Checks whether the cursor specifies a valid position in a document.
Definition: document.cpp:173
KTextEditor::Document::insertText
virtual bool insertText(const Cursor &position, const QString &text, bool block=false)=0
Insert text at position.
KTextEditor::Document::documentEnd
virtual Cursor documentEnd() const =0
End position of the document.
KTextEditor::SmartCursor
A Cursor which is bound to a specific Document, and maintains its position.
Definition: smartcursor.h:66
KTextEditor::SmartCursor::document
Document * document() const
Returns the document to which this cursor is attached.
Definition: smartcursor.cpp:68
KTextEditor::SmartCursor::InsertBehavior
InsertBehavior
Definition: smartcursor.h:187
KTextEditor::SmartCursor::MoveOnInsert
@ MoveOnInsert
Definition: smartcursor.h:189
KTextEditor::SmartCursor::StayOnInsert
@ StayOnInsert
Definition: smartcursor.h:188
KTextEditor::SmartCursor::insertBehavior
InsertBehavior insertBehavior() const
Returns how this cursor behaves when text is inserted at the cursor.
Definition: smartcursor.cpp:78
KTextEditor::SmartCursor::atEndOfDocument
virtual bool atEndOfDocument() const
Determine if this cursor is located at the end of the document.
Definition: smartcursor.cpp:43
KTextEditor::SmartCursor::setInsertBehavior
void setInsertBehavior(InsertBehavior insertBehavior)
Change the behavior of the cursor when text is inserted at the cursor.
Definition: smartcursor.cpp:83
KTextEditor::SmartCursor::SmartCursor
SmartCursor(const Cursor &position, Document *doc, InsertBehavior insertBehavior)
Definition: smartcursor.cpp:26
KTextEditor::SmartCursor::insertText
virtual bool insertText(const QStringList &text, bool block=false)
Insert text into the associated Document.
Definition: smartcursor.cpp:53
KTextEditor::SmartCursor::character
QChar character() const
Returns the character in the document immediately after this position, ie.
Definition: smartcursor.cpp:58
KTextEditor::SmartCursor::toSmartCursor
virtual SmartCursor * toSmartCursor() const
Returns this cursor as a SmartCursor.
Definition: smartcursor.cpp:88
KTextEditor::SmartCursor::isValid
virtual bool isValid() const
Definition: smartcursor.cpp:38
KTextEditor::SmartCursor::smartRange
SmartRange * smartRange() const
Returns the range that this cursor belongs to, if any.
Definition: smartcursor.cpp:63
KTextEditor::SmartCursor::isSmartCursor
virtual bool isSmartCursor() const
Returns that this cursor is a SmartCursor.
Definition: smartcursor.cpp:48
KTextEditor::SmartCursor::advance
virtual bool advance(int distance, AdvanceMode mode=ByCharacter)
Move cursor by specified distance along the document buffer.
Definition: smartcursor.cpp:93
KTextEditor::SmartCursor::atEndOfLine
virtual bool atEndOfLine() const
Determine if this cursor is located at the end of the current line.
Definition: smartcursor.cpp:73
KTextEditor::SmartCursor::AdvanceMode
AdvanceMode
Defines the ways in which the cursor can be advanced.
Definition: smartcursor.h:149
KTextEditor::SmartCursor::ByCharacter
@ ByCharacter
Movement is calculated on the basis of absolute numbers of characters.
Definition: smartcursor.h:151
KTextEditor::SmartRange
A Range which is bound to a specific Document, and maintains its position.
Definition: smartrange.h:95
document.h
back
KAction * back(const QObject *recvr, const char *slot, QObject *parent)
KTextEditor
Namespace for the KDE Text Editor Interfaces.
Definition: annotationinterface.h:31
smartcursor.h
smartrange.h
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