xlsxcellrange.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // xlsxcellrange.cpp
  2. #include <QtGlobal>
  3. #include <QString>
  4. #include <QPoint>
  5. #include <QStringList>
  6. #include "xlsxcellrange.h"
  7. #include "xlsxcellreference.h"
  8. QT_BEGIN_NAMESPACE_XLSX
  9. /*!
  10. \class CellRange
  11. \brief For a range "A1:B2" or single cell "A1"
  12. \inmodule QtXlsx
  13. The CellRange class stores the top left and bottom
  14. right rows and columns of a range in a worksheet.
  15. */
  16. /*!
  17. Constructs an range, i.e. a range
  18. whose rowCount() and columnCount() are 0.
  19. */
  20. CellRange::CellRange()
  21. : top(-1), left(-1), bottom(-2), right(-2)
  22. {
  23. }
  24. /*!
  25. Constructs the range from the given \a top, \a
  26. left, \a bottom and \a right rows and columns.
  27. \sa topRow(), leftColumn(), bottomRow(), rightColumn()
  28. */
  29. CellRange::CellRange(int top, int left, int bottom, int right)
  30. : top(top), left(left), bottom(bottom), right(right)
  31. {
  32. }
  33. CellRange::CellRange(const CellReference &topLeft, const CellReference &bottomRight)
  34. : top(topLeft.row()), left(topLeft.column())
  35. , bottom(bottomRight.row()), right(bottomRight.column())
  36. {
  37. }
  38. /*!
  39. \overload
  40. Constructs the range form the given \a range string.
  41. */
  42. CellRange::CellRange(const QString &range)
  43. {
  44. init(range);
  45. }
  46. /*!
  47. \overload
  48. Constructs the range form the given \a range string.
  49. */
  50. CellRange::CellRange(const char *range)
  51. {
  52. init(QString::fromLatin1(range));
  53. }
  54. void CellRange::init(const QString &range)
  55. {
  56. QStringList rs = range.split(QLatin1Char(':'));
  57. if (rs.size() == 2) {
  58. CellReference start(rs[0]);
  59. CellReference end(rs[1]);
  60. top = start.row();
  61. left = start.column();
  62. bottom = end.row();
  63. right = end.column();
  64. } else {
  65. CellReference p(rs[0]);
  66. top = p.row();
  67. left = p.column();
  68. bottom = p.row();
  69. right = p.column();
  70. }
  71. }
  72. /*!
  73. Constructs a the range by copying the given \a
  74. other range.
  75. */
  76. CellRange::CellRange(const CellRange &other)
  77. : top(other.top), left(other.left), bottom(other.bottom), right(other.right)
  78. {
  79. }
  80. /*!
  81. Destroys the range.
  82. */
  83. CellRange::~CellRange()
  84. {
  85. }
  86. /*!
  87. Convert the range to string notation, such as "A1:B5".
  88. */
  89. QString CellRange::toString(bool row_abs, bool col_abs) const
  90. {
  91. if (!isValid())
  92. return QString();
  93. if (left == right && top == bottom) {
  94. //Single cell
  95. return CellReference(top, left).toString(row_abs, col_abs);
  96. }
  97. QString cell_1 = CellReference(top, left).toString(row_abs, col_abs);
  98. QString cell_2 = CellReference(bottom, right).toString(row_abs, col_abs);
  99. return cell_1 + QLatin1String(":") + cell_2;
  100. }
  101. /*!
  102. * Returns true if the Range is valid.
  103. */
  104. bool CellRange::isValid() const
  105. {
  106. return left <= right && top <= bottom;
  107. }
  108. QT_END_NAMESPACE_XLSX