• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KTextEditor

range.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2007 Mirko Stocker <me@misto.ch>
00003    Copyright (C) 2003-2005 Hamish Rodda <rodda@kde.org>
00004    Copyright (C) 2002 Christian Couder <christian@kdevelop.org>
00005    Copyright (C) 2001, 2003 Christoph Cullmann <cullmann@kde.org>
00006    Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
00007    Copyright (C) 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>
00008 
00009    This library is free software; you can redistribute it and/or
00010    modify it under the terms of the GNU Library General Public
00011    License version 2 as published by the Free Software Foundation.
00012 
00013    This library is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Library General Public License for more details.
00017 
00018    You should have received a copy of the GNU Library General Public License
00019    along with this library; see the file COPYING.LIB.  If not, write to
00020    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021    Boston, MA 02110-1301, USA.
00022 */
00023 
00024 #include "range.h"
00025 
00026 using namespace KTextEditor;
00027 
00028 Range::Range()
00029   : m_start(new Cursor())
00030   , m_end(new Cursor())
00031 {
00032   m_start->setRange(this);
00033   m_end->setRange(this);
00034 }
00035 
00036 Range::Range(const Cursor& start, const Cursor& end)
00037 {
00038   if (start <= end) {
00039     m_start = new Cursor(start);
00040     m_end = new Cursor(end);
00041 
00042   } else {
00043     m_start = new Cursor(end);
00044     m_end = new Cursor(start);
00045   }
00046 
00047   m_start->setRange(this);
00048   m_end->setRange(this);
00049 }
00050 
00051 Range::Range(const Cursor& start, int width)
00052   : m_start(new Cursor(start))
00053   , m_end(new Cursor(start.line(), start.column() + width))
00054 {
00055   m_start->setRange(this);
00056   m_end->setRange(this);
00057 }
00058 
00059 Range::Range(const Cursor& start, int endLine, int endColumn)
00060   : m_start(new Cursor(start))
00061   , m_end(new Cursor(endLine, endColumn))
00062 {
00063   if (*m_end < *m_start) {
00064     Cursor* temp = m_end;
00065     m_end = m_start;
00066     m_start = temp;
00067   }
00068 
00069   m_start->setRange(this);
00070   m_end->setRange(this);
00071 }
00072 
00073 Range::Range(int startLine, int startColumn, int endLine, int endColumn)
00074   : m_start(new Cursor(startLine, startColumn))
00075   , m_end(new Cursor(endLine, endColumn))
00076 {
00077   if (*m_end < *m_start) {
00078     Cursor* temp = m_end;
00079     m_end = m_start;
00080     m_start = temp;
00081   }
00082 
00083   m_start->setRange(this);
00084   m_end->setRange(this);
00085 }
00086 
00087 Range::Range(Cursor* start, Cursor* end)
00088   : m_start(start)
00089   , m_end(end)
00090 {
00091   if (*m_end < *m_start) {
00092     Cursor temp = *m_end;
00093     *m_end = *m_start;
00094     *m_start = temp;
00095   }
00096 
00097   m_start->setRange(this);
00098   m_end->setRange(this);
00099 }
00100 
00101 Range::Range(const Range& copy)
00102   : m_start(new Cursor(copy.start()))
00103   , m_end(new Cursor(copy.end()))
00104 {
00105   m_start->setRange(this);
00106   m_end->setRange(this);
00107 }
00108 
00109 Range::~Range()
00110 {
00111   delete m_start;
00112   delete m_end;
00113 }
00114 
00115 bool Range::isValid( ) const
00116 {
00117   return start().isValid() && end().isValid();
00118 }
00119 
00120 Range Range::invalid()
00121 {
00122   return Range (Cursor(-1, -1), Cursor(-1, -1));
00123 }
00124 
00125 void Range::setRange(const Range& range)
00126 {
00127   *m_start = range.start();
00128   *m_end = range.end();
00129 }
00130 
00131 void Range::setRange( const Cursor & start, const Cursor & end )
00132 {
00133   if (start > end)
00134     setRange(Range(end, start));
00135   else
00136     setRange(Range(start, end));
00137 }
00138 
00139 bool Range::containsLine(int line) const
00140 {
00141   return (line > start().line() || line == start().line() && !start().column()) && line < end().line();
00142 }
00143 
00144 bool Range::overlapsLine(int line) const
00145 {
00146   return line >= start().line() && line <= end().line();
00147 }
00148 
00149 bool Range::overlapsColumn( int col ) const
00150 {
00151   return start().column() <= col && end().column() > col;
00152 }
00153 
00154 bool Range::contains( const Cursor& cursor ) const
00155 {
00156   return cursor >= start() && cursor < end();
00157 }
00158 
00159 bool Range::contains( const Range& range ) const
00160 {
00161   return range.start() >= start() && range.end() <= end();
00162 }
00163 
00164 bool Range::containsColumn( int column ) const
00165 {
00166   return column >= start().column() && column < end().column();
00167 }
00168 
00169 bool Range::overlaps( const Range& range ) const
00170 {
00171   if (range.start() <= start())
00172     return range.end() > start();
00173 
00174   else if (range.end() >= end())
00175     return range.start() < end();
00176 
00177   else
00178     return contains(range);
00179 }
00180 
00181 bool Range::boundaryAtCursor(const Cursor& cursor) const
00182 {
00183   return cursor == start() || cursor == end();
00184 }
00185 
00186 bool Range::boundaryOnLine(int line) const
00187 {
00188   return start().line() == line || end().line() == line;
00189 }
00190 
00191 bool Range::confineToRange(const Range& range)
00192 {
00193   if (start() < range.start())
00194     if (end() > range.end())
00195       setRange(range);
00196     else
00197       start() = range.start();
00198   else if (end() > range.end())
00199     end() = range.end();
00200   else
00201     return false;
00202 
00203   return true;
00204 }
00205 
00206 bool Range::expandToRange(const Range& range)
00207 {
00208   if (start() > range.start())
00209     if (end() < range.end())
00210       setRange(range);
00211     else
00212       start() = range.start();
00213   else if (end() < range.end())
00214     end() = range.end();
00215   else
00216     return false;
00217 
00218   return true;
00219 }
00220 
00221 void Range::rangeChanged( Cursor * c, const Range& )
00222 {
00223   if (c == m_start) {
00224     if (*c > *m_end)
00225       *m_end = *c;
00226 
00227   } else if (c == m_end) {
00228     if (*c < *m_start)
00229       *m_start = *c;
00230   }
00231 }
00232 
00233 void Range::setBothLines( int line )
00234 {
00235   setRange(Range(line, start().column(), line, end().column()));
00236 }
00237 
00238 bool KTextEditor::Range::onSingleLine( ) const
00239 {
00240   return start().line() == end().line();
00241 }
00242 
00243 int KTextEditor::Range::columnWidth( ) const
00244 {
00245   return end().column() - start().column();
00246 }
00247 
00248 int KTextEditor::Range::numberOfLines( ) const
00249 {
00250   return end().line() - start().line();
00251 }
00252 
00253 bool KTextEditor::Range::isEmpty( ) const
00254 {
00255   return start() == end();
00256 }
00257 
00258 int Range::positionRelativeToCursor( const Cursor & cursor ) const
00259 {
00260   if (end() <= cursor)
00261     return -1;
00262 
00263   if (start() > cursor)
00264     return +1;
00265 
00266   return 0;
00267 }
00268 
00269 int Range::positionRelativeToLine( int line ) const
00270 {
00271   if (end().line() < line)
00272     return -1;
00273 
00274   if (start().line() > line)
00275     return +1;
00276 
00277   return 0;
00278 }
00279 
00280 void KTextEditor::Range::setBothColumns( int column )
00281 {
00282   setRange(Range(start().line(), column, end().line(), column));
00283 }
00284 
00285 bool KTextEditor::Range::isSmartRange( ) const
00286 {
00287   return false;
00288 }
00289 
00290 SmartRange* KTextEditor::Range::toSmartRange( ) const
00291 {
00292   return 0L;
00293 }
00294 
00295 Cursor& KTextEditor::Range::start()
00296 {
00297   return *m_start;
00298 }
00299 
00300 const Cursor& KTextEditor::Range::start() const
00301 {
00302   return *m_start;
00303 }
00304 
00305 Cursor& KTextEditor::Range::end()
00306 {
00307   return *m_end;
00308 }
00309 
00310 const Cursor& KTextEditor::Range::end() const
00311 {
00312   return *m_end;
00313 }
00314 
00315 Range KTextEditor::Range::intersect( const Range & range ) const
00316 {
00317   if (!isValid() || !range.isValid() || *this > range || *this < range)
00318     return invalid();
00319 
00320   return Range(qMax(start(), range.start()), qMin(end(), range.end()));
00321 }
00322 
00323 Range KTextEditor::Range::encompass( const Range & range ) const
00324 {
00325   if (!isValid())
00326     if (range.isValid())
00327       return range;
00328     else
00329       return invalid();
00330   else if (!range.isValid())
00331     return *this;
00332   else
00333     return Range(qMin(start(), range.start()), qMax(end(), range.end()));
00334 }
00335 
00336 // kate: space-indent on; indent-width 2; replace-tabs on;

KTextEditor

Skip menu "KTextEditor"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal