File indexing completed on 2025-03-23 03:32:46
0001 // xlsxcellrange.cpp 0002 0003 #include <QtGlobal> 0004 #include <QString> 0005 #include <QPoint> 0006 #include <QStringList> 0007 0008 #include "xlsxcellrange.h" 0009 #include "xlsxcellreference.h" 0010 0011 QT_BEGIN_NAMESPACE_XLSX 0012 0013 /*! 0014 \class CellRange 0015 \brief For a range "A1:B2" or single cell "A1" 0016 \inmodule QtXlsx 0017 0018 The CellRange class stores the top left and bottom 0019 right rows and columns of a range in a worksheet. 0020 */ 0021 0022 /*! 0023 Constructs an range, i.e. a range 0024 whose rowCount() and columnCount() are 0. 0025 */ 0026 CellRange::CellRange() 0027 : top(-1), left(-1), bottom(-2), right(-2) 0028 { 0029 } 0030 0031 /*! 0032 Constructs the range from the given \a top, \a 0033 left, \a bottom and \a right rows and columns. 0034 0035 \sa topRow(), leftColumn(), bottomRow(), rightColumn() 0036 */ 0037 CellRange::CellRange(int top, int left, int bottom, int right) 0038 : top(top), left(left), bottom(bottom), right(right) 0039 { 0040 } 0041 0042 CellRange::CellRange(const CellReference &topLeft, const CellReference &bottomRight) 0043 : top(topLeft.row()), left(topLeft.column()) 0044 , bottom(bottomRight.row()), right(bottomRight.column()) 0045 { 0046 } 0047 0048 /*! 0049 \overload 0050 Constructs the range form the given \a range string. 0051 */ 0052 CellRange::CellRange(const QString &range) 0053 { 0054 init(range); 0055 } 0056 0057 /*! 0058 \overload 0059 Constructs the range form the given \a range string. 0060 */ 0061 CellRange::CellRange(const char *range) 0062 { 0063 init(QString::fromLatin1(range)); 0064 } 0065 0066 void CellRange::init(const QString &range) 0067 { 0068 QStringList rs = range.split(QLatin1Char(':')); 0069 if (rs.size() == 2) { 0070 CellReference start(rs[0]); 0071 CellReference end(rs[1]); 0072 top = start.row(); 0073 left = start.column(); 0074 bottom = end.row(); 0075 right = end.column(); 0076 } else { 0077 CellReference p(rs[0]); 0078 top = p.row(); 0079 left = p.column(); 0080 bottom = p.row(); 0081 right = p.column(); 0082 } 0083 } 0084 0085 /*! 0086 Constructs a the range by copying the given \a 0087 other range. 0088 */ 0089 CellRange::CellRange(const CellRange &other) 0090 : top(other.top), left(other.left), bottom(other.bottom), right(other.right) 0091 { 0092 } 0093 0094 /*! 0095 Destroys the range. 0096 */ 0097 CellRange::~CellRange() 0098 { 0099 } 0100 0101 /*! 0102 Convert the range to string notation, such as "A1:B5". 0103 */ 0104 QString CellRange::toString(bool row_abs, bool col_abs) const 0105 { 0106 if (!isValid()) 0107 return QString(); 0108 0109 if (left == right && top == bottom) { 0110 //Single cell 0111 return CellReference(top, left).toString(row_abs, col_abs); 0112 } 0113 0114 QString cell_1 = CellReference(top, left).toString(row_abs, col_abs); 0115 QString cell_2 = CellReference(bottom, right).toString(row_abs, col_abs); 0116 return cell_1 + QLatin1String(":") + cell_2; 0117 } 0118 0119 /*! 0120 * Returns true if the Range is valid. 0121 */ 0122 bool CellRange::isValid() const 0123 { 0124 return left <= right && top <= bottom; 0125 } 0126 0127 QT_END_NAMESPACE_XLSX