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