KTextEditor
smartcursor.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "smartcursor.h"
00020
00021 #include "document.h"
00022 #include "smartrange.h"
00023
00024 using namespace KTextEditor;
00025
00026 SmartCursor::SmartCursor( const Cursor & position, Document * doc, InsertBehavior insertBehavior )
00027 : Cursor(position)
00028 , m_doc(doc)
00029 , m_moveOnInsert(insertBehavior == MoveOnInsert)
00030 {
00031 Q_ASSERT(m_doc);
00032 }
00033
00034 SmartCursor::~ SmartCursor( )
00035 {
00036 }
00037
00038 bool SmartCursor::isValid( ) const
00039 {
00040 return m_doc->cursorInText(*this);
00041 }
00042
00043 bool SmartCursor::atEndOfDocument( ) const
00044 {
00045 return *this >= m_doc->documentEnd();
00046 }
00047
00048 bool SmartCursor::isSmartCursor( ) const
00049 {
00050 return true;
00051 }
00052
00053 bool SmartCursor::insertText( const QStringList & text, bool block )
00054 {
00055 return document()->insertText(*this, text, block);
00056 }
00057
00058 QChar SmartCursor::character( ) const
00059 {
00060 return document()->character(*this);
00061 }
00062
00063 SmartRange * SmartCursor::smartRange( ) const
00064 {
00065 return static_cast<SmartRange*>(m_range);
00066 }
00067
00068 Document* SmartCursor::document() const
00069 {
00070 return m_doc;
00071 }
00072
00073 bool SmartCursor::atEndOfLine( ) const
00074 {
00075 return column() == m_doc->lineLength(line());
00076 }
00077
00078 SmartCursor::InsertBehavior SmartCursor::insertBehavior( ) const
00079 {
00080 return m_moveOnInsert ? MoveOnInsert : StayOnInsert;
00081 }
00082
00083 void SmartCursor::setInsertBehavior( InsertBehavior insertBehavior )
00084 {
00085 m_moveOnInsert = insertBehavior == MoveOnInsert;
00086 }
00087
00088 SmartCursor * SmartCursor::toSmartCursor( ) const
00089 {
00090 return const_cast<SmartCursor*>(this);
00091 }
00092
00093 bool SmartCursor::advance(int distance, AdvanceMode mode)
00094 {
00095 Cursor c = *this;
00096 if (mode == ByCharacter) {
00097 while (distance) {
00098 int lineLength = document()->lineLength(c.line());
00099
00100 if (distance > 0) {
00101 int advance = qMin(lineLength - c.column(), distance);
00102
00103 if (distance > advance) {
00104 if (c.line() + 1 >= document()->lines())
00105 return false;
00106
00107 c.setPosition(c.line() + 1, 0);
00108
00109 distance -= advance + 1;
00110
00111 } else {
00112 c.setColumn(c.column() + distance);
00113 distance = 0;
00114 }
00115
00116 } else {
00117 int back = qMin(c.column(), -distance);
00118 if (-distance > back) {
00119 if (c.line() - 1 < 0)
00120 return false;
00121
00122 c.setPosition(c.line() - 1, document()->lineLength(c.line() - 1));
00123
00124 distance += back + 1;
00125
00126 } else {
00127 c.setColumn(c.column() + distance);
00128 distance = 0;
00129 }
00130 }
00131 }
00132
00133 } else {
00134
00135 return false;
00136 }
00137
00138 setPosition(c);
00139 return true;
00140 }
00141
00142