• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.5 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 
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;
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Mon Jul 15 2013 13:10:33 by doxygen 1.8.3.1 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.10.5 API Reference

Skip menu "kdelibs-4.10.5 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