File indexing completed on 2024-11-10 05:07:59
0001 // clang-format off 0002 /* 0003 * KDiff3 - Text Diff And Merge Tool 0004 * 0005 * SPDX-FileCopyrightText: 2002-2011 Joachim Eibl, joachim.eibl at gmx.de 0006 * SPDX-FileCopyrightText: 2018-2020 Michael Reeves reeves.87@gmail.com 0007 * SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 // clang-format on 0010 #ifndef SELECTION_H 0011 #define SELECTION_H 0012 0013 #include "LineRef.h" 0014 0015 #include <algorithm> // for max, min 0016 #include <QtGlobal> 0017 0018 class Selection 0019 { 0020 public: 0021 Selection() = default; 0022 0023 private: 0024 LineRef firstLine; 0025 LineRef lastLine; 0026 0027 QtSizeType firstPos = -1; 0028 QtSizeType lastPos = -1; 0029 0030 LineRef oldFirstLine; 0031 LineRef oldLastLine; 0032 0033 public: 0034 [[nodiscard]] inline LineRef getFirstLine() const { return firstLine; }; 0035 [[nodiscard]] inline LineRef getLastLine() const { return lastLine; }; 0036 0037 [[nodiscard]] inline QtSizeType getFirstPos() const { return firstPos; }; 0038 [[nodiscard]] inline QtSizeType getLastPos() const { return lastPos; }; 0039 0040 [[nodiscard]] inline bool isValidFirstLine() const { return firstLine.isValid(); } 0041 inline void clearOldSelection() { oldLastLine.invalidate(), oldFirstLine.invalidate(); }; 0042 0043 [[nodiscard]] inline LineRef getOldLastLine() const { return oldLastLine; }; 0044 [[nodiscard]] inline LineRef getOldFirstLine() const { return oldFirstLine; }; 0045 [[nodiscard]] inline bool isEmpty() const { return !firstLine.isValid() || (firstLine == lastLine && firstPos == lastPos); } 0046 0047 void reset() 0048 { 0049 oldLastLine = lastLine; 0050 oldFirstLine = firstLine; 0051 firstLine.invalidate(); 0052 lastLine.invalidate(); 0053 } 0054 0055 void start(LineRef l, QtSizeType p) 0056 { 0057 firstLine = l; 0058 firstPos = p; 0059 } 0060 0061 void end(LineRef l, QtSizeType p) 0062 { 0063 if(!oldLastLine.isValid()) 0064 oldLastLine = lastLine; 0065 lastLine = l; 0066 lastPos = p; 0067 } 0068 0069 [[nodiscard]] bool within(LineRef l, QtSizeType p) const; 0070 [[nodiscard]] bool lineWithin(LineRef l) const; 0071 0072 [[nodiscard]] QtSizeType firstPosInLine(LineRef l) const; 0073 [[nodiscard]] QtSizeType lastPosInLine(LineRef l) const; 0074 0075 [[nodiscard]] LineRef beginLine() const 0076 { 0077 if(!firstLine.isValid() && !lastLine.isValid()) return LineRef(); 0078 return std::max((LineRef)0, std::min(firstLine, lastLine)); 0079 } 0080 0081 [[nodiscard]] LineRef endLine() const 0082 { 0083 if(!firstLine.isValid() && !lastLine.isValid()) return LineRef(); 0084 return std::max(firstLine, lastLine); 0085 } 0086 0087 [[nodiscard]] QtSizeType beginPos() const { return firstLine == lastLine ? std::min(firstPos, lastPos) : 0088 firstLine < lastLine ? (!firstLine.isValid() ? 0 : firstPos) : 0089 (!lastLine.isValid() ? 0 : lastPos); } 0090 [[nodiscard]] QtSizeType endPos() const { return firstLine == lastLine ? std::max(firstPos, lastPos) : 0091 firstLine < lastLine ? lastPos : 0092 firstPos; } 0093 }; 0094 0095 #endif