• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

KTextEditor

  • kde-4.14
  • applications
  • kate
  • 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 
24 using namespace KTextEditor;
25 
26 SmartCursor::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 
34 SmartCursor::~ SmartCursor( )
35 {
36 }
37 
38 bool SmartCursor::isValid( ) const
39 {
40  return m_doc->cursorInText(*this);
41 }
42 
43 bool SmartCursor::atEndOfDocument( ) const
44 {
45  return *this >= m_doc->documentEnd();
46 }
47 
48 bool SmartCursor::isSmartCursor( ) const
49 {
50  return true;
51 }
52 
53 bool SmartCursor::insertText( const QStringList & text, bool block )
54 {
55  return document()->insertText(*this, text, block);
56 }
57 
58 QChar SmartCursor::character( ) const
59 {
60  return document()->character(*this);
61 }
62 
63 SmartRange * SmartCursor::smartRange( ) const
64 {
65  return static_cast<SmartRange*>(m_range);
66 }
67 
68 Document* SmartCursor::document() const
69 {
70  return m_doc;
71 }
72 
73 bool SmartCursor::atEndOfLine( ) const
74 {
75  return column() == m_doc->lineLength(line());
76 }
77 
78 SmartCursor::InsertBehavior SmartCursor::insertBehavior( ) const
79 {
80  return m_moveOnInsert ? MoveOnInsert : StayOnInsert;
81 }
82 
83 void SmartCursor::setInsertBehavior( InsertBehavior insertBehavior )
84 {
85  m_moveOnInsert = insertBehavior == MoveOnInsert;
86 }
87 
88 SmartCursor * SmartCursor::toSmartCursor( ) const
89 {
90  return const_cast<SmartCursor*>(this);
91 }
92 
93 bool 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::SmartCursor::StayOnInsert
Definition: smartcursor.h:188
KTextEditor::Document::insertText
virtual bool insertText(const Cursor &position, const QString &text, bool block=false)=0
Insert text at position.
KTextEditor::SmartCursor::~SmartCursor
virtual ~SmartCursor()
Virtual destructor.
Definition: smartcursor.cpp:34
KTextEditor::Cursor::m_range
Range * m_range
Definition: cursor.h:349
KTextEditor::SmartCursor::character
QChar character() const
Returns the character in the document immediately after this position, ie.
Definition: smartcursor.cpp:58
KTextEditor::SmartCursor::insertBehavior
InsertBehavior insertBehavior() const
Returns how this cursor behaves when text is inserted at the cursor.
Definition: smartcursor.cpp:78
KTextEditor::SmartCursor::InsertBehavior
InsertBehavior
Definition: smartcursor.h:187
QChar
KTextEditor::SmartCursor::insertText
virtual bool insertText(const QStringList &text, bool block=false)
Insert text into the associated Document.
Definition: smartcursor.cpp:53
KTextEditor::SmartCursor::atEndOfDocument
virtual bool atEndOfDocument() const
Determine if this cursor is located at the end of the document.
Definition: smartcursor.cpp:43
KTextEditor::Document::documentEnd
virtual Cursor documentEnd() const =0
End position of the document.
KTextEditor::SmartCursor::toSmartCursor
virtual SmartCursor * toSmartCursor() const
Returns this cursor as a SmartCursor.
Definition: smartcursor.cpp:88
KTextEditor::SmartRange
A Range which is bound to a specific Document, and maintains its position.
Definition: smartrange.h:94
KTextEditor::SmartCursor::SmartCursor
SmartCursor(const Cursor &position, Document *doc, InsertBehavior insertBehavior)
Definition: smartcursor.cpp:26
smartrange.h
KTextEditor::Cursor
An object which represents a position in a Document.
Definition: cursor.h:55
KTextEditor::Document
A KParts derived class representing a text document.
Definition: document.h:111
KTextEditor::SmartCursor::isValid
virtual bool isValid() const
Definition: smartcursor.cpp:38
KTextEditor::SmartCursor
A Cursor which is bound to a specific Document, and maintains its position.
Definition: smartcursor.h:65
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::atEndOfLine
virtual bool atEndOfLine() const
Determine if this cursor is located at the end of the current line.
Definition: smartcursor.cpp:73
document.h
KTextEditor::SmartCursor::AdvanceMode
AdvanceMode
Defines the ways in which the cursor can be advanced.
Definition: smartcursor.h:149
QStringList
KTextEditor::Document::cursorInText
virtual bool cursorInText(const Cursor &cursor)
Checks whether the cursor specifies a valid position in a document.
Definition: document.cpp:164
KTextEditor::SmartCursor::ByCharacter
Movement is calculated on the basis of absolute numbers of characters.
Definition: smartcursor.h:151
smartcursor.h
KTextEditor::Cursor::line
virtual int line() const
Retrieve the line on which this cursor is situated.
Definition: cursor.cpp:62
KTextEditor::Cursor::setPosition
virtual void setPosition(const Cursor &position)
Set the current cursor position to position.
Definition: cursor.cpp:96
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::SmartCursor::smartRange
SmartRange * smartRange() const
Returns the range that this cursor belongs to, if any.
Definition: smartcursor.cpp:63
KTextEditor::SmartCursor::MoveOnInsert
Definition: smartcursor.h:189
KTextEditor::SmartCursor::isSmartCursor
virtual bool isSmartCursor() const
Returns that this cursor is a SmartCursor.
Definition: smartcursor.cpp:48
KTextEditor::Cursor::setColumn
virtual void setColumn(int column)
Set the cursor column to column.
Definition: cursor.cpp:84
KTextEditor::Cursor::column
int column() const
Retrieve the column on which this cursor is situated.
Definition: cursor.cpp:79
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::document
Document * document() const
Returns the document to which this cursor is attached.
Definition: smartcursor.cpp:68
KTextEditor::Document::lines
virtual int lines() const =0
Get the count of lines of the document.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:48 by doxygen 1.8.7 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

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



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