File indexing completed on 2024-03-24 17:26:49

0001 /*
0002     This file is part of the Okteta Gui library, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2003, 2009 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #ifndef OKTETA_COORDRANGE_HPP
0010 #define OKTETA_COORDRANGE_HPP
0011 
0012 // lib
0013 #include "coord.hpp"
0014 #include "linepositionrange.hpp"
0015 #include "linerange.hpp"
0016 // Okteta core
0017 #include <Okteta/Range>
0018 
0019 namespace Okteta {
0020 
0021 using BaseCoordRange = Range<Coord>;
0022 
0023 template <>
0024 inline const Coord BaseCoordRange::null() const
0025 { return Coord(-1, -1); }
0026 
0027 
0028 /** describes a range in the buffercoord
0029  * @author Friedrich W. H.  Kossebau
0030  */
0031 class CoordRange : public BaseCoordRange
0032 {
0033 public:
0034     /**
0035      * @param start start coord
0036      * @param end end coord
0037      */
0038     CoordRange(const Coord& start, const Coord& end);
0039     /**
0040      * @param posRange start and end pos
0041      * @param lineRange start and end line
0042      */
0043     CoordRange(const LinePositionRange& posRange, const LineRange& lineRange);
0044     CoordRange(const CoordRange& other);
0045     CoordRange();
0046     ~CoordRange();
0047 
0048 public:
0049     CoordRange& operator=(const CoordRange& other);
0050 
0051 public:
0052     bool operator==(const CoordRange& other) const;
0053 
0054 public:
0055     /** calculates the number of coords that are covered if a line has the given length.
0056      * If the range is invalid the behaviour is undefined.
0057      * @param lineWidth
0058      * @return the number of points covered if a line has a length of LineLength.
0059      */
0060     Size width(LinePositionSize lineWidth) const;
0061     /** calculates the number of lines that are covered by the range.
0062      * If the range is invalid the behaviour is undefined.
0063      * @return number of lines covered
0064      */
0065     LineSize lines() const;
0066     /** tests if the given line is included by the range.
0067      * If the range is invalid or the line < 0 the behaviour is undefined.
0068      * @param line index of line
0069      * @return @c true if Line is included, otherwise @c false
0070      */
0071     bool includesLine(Line line) const;
0072 };
0073 
0074 inline CoordRange::CoordRange(const Coord& start, const Coord& end) : BaseCoordRange(start, end) {}
0075 inline CoordRange::CoordRange(const LinePositionRange& posRange, const LineRange& lineRange)
0076     : BaseCoordRange(Coord(posRange.start(), lineRange.start()), Coord(posRange.end(), lineRange.end()))
0077 {}
0078 inline CoordRange::CoordRange(const CoordRange& other) = default;
0079 inline CoordRange::CoordRange() = default;
0080 
0081 inline CoordRange::~CoordRange() = default;
0082 
0083 inline CoordRange& CoordRange::operator=(const CoordRange& other) = default;
0084 
0085 inline bool CoordRange::operator==(const CoordRange& other) const { return BaseCoordRange::operator==(other); }
0086 
0087 inline Size CoordRange::width(LinePositionSize lineWidth)   const { return lineWidth * (lines() - 1) + end().pos() - start().pos() + 1; }
0088 inline LineSize CoordRange::lines()                         const { return end().line() - start().line() + 1; }
0089 inline bool CoordRange::includesLine(Line line)             const { return (start().line() <= line && line <= end().line()); }
0090 
0091 }
0092 
0093 #endif