• 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
range.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  * Copyright (C) 2007 Mirko Stocker <me@misto.ch>
3  * Copyright (C) 2003-2005 Hamish Rodda <rodda@kde.org>
4  * Copyright (C) 2002 Christian Couder <christian@kdevelop.org>
5  * Copyright (C) 2001, 2003 Christoph Cullmann <cullmann@kde.org>
6  * Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
7  * Copyright (C) 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>
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 "range.h"
26 
27 using namespace KTextEditor;
28 
29 Range::Range()
30  : m_start(new Cursor())
31  , m_end(new Cursor())
32 {
33  m_start->setRange(this);
34  m_end->setRange(this);
35 }
36 
37 Range::Range(const Cursor& start, const Cursor& end)
38 {
39  if (start <= end) {
40  m_start = new Cursor(start);
41  m_end = new Cursor(end);
42 
43  } else {
44  m_start = new Cursor(end);
45  m_end = new Cursor(start);
46  }
47 
48  m_start->setRange(this);
49  m_end->setRange(this);
50 }
51 
52 Range::Range(const Cursor& start, int width)
53  : m_start(new Cursor(start))
54  , m_end(new Cursor(start.line(), start.column() + width))
55 {
56  m_start->setRange(this);
57  m_end->setRange(this);
58 }
59 
60 Range::Range(const Cursor& start, int endLine, int endColumn)
61  : m_start(new Cursor(start))
62  , m_end(new Cursor(endLine, endColumn))
63 {
64  if (*m_end < *m_start) {
65  Cursor* temp = m_end;
66  m_end = m_start;
67  m_start = temp;
68  }
69 
70  m_start->setRange(this);
71  m_end->setRange(this);
72 }
73 
74 Range::Range(int startLine, int startColumn, int endLine, int endColumn)
75  : m_start(new Cursor(startLine, startColumn))
76  , m_end(new Cursor(endLine, endColumn))
77 {
78  if (*m_end < *m_start) {
79  Cursor* temp = m_end;
80  m_end = m_start;
81  m_start = temp;
82  }
83 
84  m_start->setRange(this);
85  m_end->setRange(this);
86 }
87 
88 Range::Range(Cursor* start, Cursor* end)
89  : m_start(start)
90  , m_end(end)
91 {
92  if (*m_end < *m_start) {
93  Cursor temp = *m_end;
94  *m_end = *m_start;
95  *m_start = temp;
96  }
97 
98  m_start->setRange(this);
99  m_end->setRange(this);
100 }
101 
102 Range::Range(const Range& copy)
103  : m_start(new Cursor(copy.start()))
104  , m_end(new Cursor(copy.end()))
105 {
106  m_start->setRange(this);
107  m_end->setRange(this);
108 }
109 
110 Range::~Range()
111 {
112  delete m_start;
113  delete m_end;
114 }
115 
116 bool Range::isValid( ) const
117 {
118  return start().isValid() && end().isValid();
119 }
120 
121 Range Range::invalid()
122 {
123  return Range (Cursor(-1, -1), Cursor(-1, -1));
124 }
125 
126 void Range::setRange(const Range& range)
127 {
128  *m_start = range.start();
129  *m_end = range.end();
130 }
131 
132 void Range::setRange( const Cursor & start, const Cursor & end )
133 {
134  if (start > end)
135  setRange(Range(end, start));
136  else
137  setRange(Range(start, end));
138 }
139 
140 bool Range::containsLine(int line) const
141 {
142  return (line > start().line() || (line == start().line() && !start().column())) && line < end().line();
143 }
144 
145 bool Range::overlapsLine(int line) const
146 {
147  return line >= start().line() && line <= end().line();
148 }
149 
150 bool Range::overlapsColumn( int col ) const
151 {
152  return start().column() <= col && end().column() > col;
153 }
154 
155 bool Range::contains( const Cursor& cursor ) const
156 {
157  return cursor >= start() && cursor < end();
158 }
159 
160 bool Range::contains( const Range& range ) const
161 {
162  return range.start() >= start() && range.end() <= end();
163 }
164 
165 bool Range::containsColumn( int column ) const
166 {
167  return column >= start().column() && column < end().column();
168 }
169 
170 bool Range::overlaps( const Range& range ) const
171 {
172  if (range.start() <= start())
173  return range.end() > start();
174 
175  else if (range.end() >= end())
176  return range.start() < end();
177 
178  else
179  return contains(range);
180 }
181 
182 bool Range::boundaryAtCursor(const Cursor& cursor) const
183 {
184  return cursor == start() || cursor == end();
185 }
186 
187 bool Range::boundaryOnLine(int line) const
188 {
189  return start().line() == line || end().line() == line;
190 }
191 
192 bool Range::confineToRange(const Range& range)
193 {
194  if (start() < range.start())
195  if (end() > range.end())
196  setRange(range);
197  else
198  start() = range.start();
199  else if (end() > range.end())
200  end() = range.end();
201  else
202  return false;
203 
204  return true;
205 }
206 
207 bool Range::expandToRange(const Range& range)
208 {
209  if (start() > range.start())
210  if (end() < range.end())
211  setRange(range);
212  else
213  start() = range.start();
214  else if (end() < range.end())
215  end() = range.end();
216  else
217  return false;
218 
219  return true;
220 }
221 
222 void Range::rangeChanged( Cursor * c, const Range& )
223 {
224  if (c == m_start) {
225  if (*c > *m_end)
226  *m_end = *c;
227 
228  } else if (c == m_end) {
229  if (*c < *m_start)
230  *m_start = *c;
231  }
232 }
233 
234 void Range::setBothLines( int line )
235 {
236  setRange(Range(line, start().column(), line, end().column()));
237 }
238 
239 bool KTextEditor::Range::onSingleLine( ) const
240 {
241  return start().line() == end().line();
242 }
243 
244 int KTextEditor::Range::columnWidth( ) const
245 {
246  return end().column() - start().column();
247 }
248 
249 int KTextEditor::Range::numberOfLines( ) const
250 {
251  return end().line() - start().line();
252 }
253 
254 bool KTextEditor::Range::isEmpty( ) const
255 {
256  return start() == end();
257 }
258 
259 int Range::positionRelativeToCursor( const Cursor & cursor ) const
260 {
261  if (end() <= cursor)
262  return -1;
263 
264  if (start() > cursor)
265  return +1;
266 
267  return 0;
268 }
269 
270 int Range::positionRelativeToLine( int line ) const
271 {
272  if (end().line() < line)
273  return -1;
274 
275  if (start().line() > line)
276  return +1;
277 
278  return 0;
279 }
280 
281 void KTextEditor::Range::setBothColumns( int column )
282 {
283  setRange(Range(start().line(), column, end().line(), column));
284 }
285 
286 bool KTextEditor::Range::isSmartRange( ) const
287 {
288  return false;
289 }
290 
291 SmartRange* KTextEditor::Range::toSmartRange( ) const
292 {
293  return 0L;
294 }
295 
296 Cursor& KTextEditor::Range::start()
297 {
298  return *m_start;
299 }
300 
301 const Cursor& KTextEditor::Range::start() const
302 {
303  return *m_start;
304 }
305 
306 Cursor& KTextEditor::Range::end()
307 {
308  return *m_end;
309 }
310 
311 const Cursor& KTextEditor::Range::end() const
312 {
313  return *m_end;
314 }
315 
316 Range KTextEditor::Range::intersect( const Range & range ) const
317 {
318  if (!isValid() || !range.isValid() || *this > range || *this < range)
319  return invalid();
320 
321  return Range(qMax(start(), range.start()), qMin(end(), range.end()));
322 }
323 
324 Range KTextEditor::Range::encompass( const Range & range ) const
325 {
326  if (!isValid())
327  if (range.isValid())
328  return range;
329  else
330  return invalid();
331  else if (!range.isValid())
332  return *this;
333  else
334  return Range(qMin(start(), range.start()), qMax(end(), range.end()));
335 }
336 
337 // kate: space-indent on; indent-width 2; replace-tabs on;
KTextEditor::Range::start
Cursor & start()
Get the start position of this range.
Definition: range.cpp:296
KTextEditor::Range::m_start
Cursor * m_start
This range's start cursor pointer.
Definition: range.h:654
KTextEditor::Range::isValid
virtual bool isValid() const
Validity check.
Definition: range.cpp:116
KTextEditor::Range::toSmartRange
virtual SmartRange * toSmartRange() const
Returns this range as a SmartRange, if it is one.
Definition: range.cpp:291
KTextEditor::Range::positionRelativeToCursor
int positionRelativeToCursor(const Cursor &cursor) const
Determine where cursor is positioned in relationship to this range.
Definition: range.cpp:259
KTextEditor::SmartRange
A Range which is bound to a specific Document, and maintains its position.
Definition: smartrange.h:94
KTextEditor::Range::boundaryOnLine
bool boundaryOnLine(int line) const
Check whether line is on the same line as either of the start() or end() boundaries.
Definition: range.cpp:187
KTextEditor::Range::setRange
virtual void setRange(const Range &range)
Set the start and end cursors to range.start() and range.end() respectively.
Definition: range.cpp:126
KTextEditor::Cursor::setRange
virtual void setRange(Range *range)
Definition: cursor.cpp:133
KTextEditor::Cursor
An object which represents a position in a Document.
Definition: cursor.h:55
KTextEditor::Range::confineToRange
virtual bool confineToRange(const Range &range)
Confine this range if necessary to fit within range.
Definition: range.cpp:192
KTextEditor::Range::rangeChanged
virtual void rangeChanged(Cursor *cursor, const Range &from)
Notify this range that one or both of the cursors' position has changed directly. ...
Definition: range.cpp:222
KTextEditor::Range::columnWidth
int columnWidth() const
Returns the number of columns separating the start() and end() positions.
Definition: range.cpp:244
KTextEditor::Range::containsColumn
bool containsColumn(int column) const
Check whether the range contains column.
Definition: range.cpp:165
KTextEditor::Range::~Range
virtual ~Range()
Virtual destructor.
Definition: range.cpp:110
KTextEditor::Cursor::isValid
virtual bool isValid() const
Returns whether the current position of this cursor is a valid position (line + column must both be >...
Definition: cursor.cpp:47
range.h
KTextEditor::Range::m_end
Cursor * m_end
This range's end cursor pointer.
Definition: range.h:661
KTextEditor::Range::invalid
static Range invalid()
Returns an invalid range.
Definition: range.cpp:121
KTextEditor::Range::boundaryAtCursor
bool boundaryAtCursor(const Cursor &cursor) const
Check whether cursor is located at either of the start() or end() boundaries.
Definition: range.cpp:182
KTextEditor::Range
An object representing a section of text, from one Cursor to another.
Definition: range.h:54
KTextEditor::Range::encompass
Range encompass(const Range &range) const
Returns the smallest range which encompasses this range and the supplied range.
Definition: range.cpp:324
KTextEditor::Range::onSingleLine
bool onSingleLine() const
Check whether this range is wholly contained within one line, ie.
Definition: range.cpp:239
KTextEditor::Range::overlapsLine
bool overlapsLine(int line) const
Check whether the range overlaps at least part of line.
Definition: range.cpp:145
KTextEditor::Range::expandToRange
virtual bool expandToRange(const Range &range)
Expand this range if necessary to contain range.
Definition: range.cpp:207
KTextEditor::Range::Cursor
friend class Cursor
Definition: range.h:56
KTextEditor::Cursor::line
virtual int line() const
Retrieve the line on which this cursor is situated.
Definition: cursor.cpp:62
KTextEditor::Range::overlapsColumn
bool overlapsColumn(int column) const
Check to see if this range overlaps column; that is, if column is between start().column() and end().column().
Definition: range.cpp:150
KTextEditor::Range::Range
Range()
Default constructor.
Definition: range.cpp:29
KTextEditor::Range::end
Cursor & end()
Get the end position of this range.
Definition: range.cpp:306
KTextEditor::Range::containsLine
bool containsLine(int line) const
Returns true if this range wholly encompasses line.
Definition: range.cpp:140
KTextEditor::Range::isSmartRange
virtual bool isSmartRange() const
Returns whether this range is a SmartRange.
Definition: range.cpp:286
KTextEditor::Range::numberOfLines
int numberOfLines() const
Returns the number of lines separating the start() and end() positions.
Definition: range.cpp:249
KTextEditor::Range::intersect
Range intersect(const Range &range) const
Intersects this range with another, returning the shared area of the two ranges.
Definition: range.cpp:316
KTextEditor::Range::contains
bool contains(const Range &range) const
Check whether the this range wholly encompasses range.
Definition: range.cpp:160
KTextEditor::Range::isEmpty
bool isEmpty() const
Returns true if this range contains no characters, ie.
Definition: range.cpp:254
KTextEditor::Cursor::column
int column() const
Retrieve the column on which this cursor is situated.
Definition: cursor.cpp:79
KTextEditor::Range::setBothLines
void setBothLines(int line)
Convenience function.
Definition: range.cpp:234
KTextEditor::Range::positionRelativeToLine
int positionRelativeToLine(int line) const
Determine where line is positioned in relationship to this range.
Definition: range.cpp:270
KTextEditor::Range::overlaps
bool overlaps(const Range &range) const
Check whether the this range overlaps with range.
Definition: range.cpp:170
KTextEditor::Range::setBothColumns
void setBothColumns(int column)
Convenience function.
Definition: range.cpp:281
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