xlsxcellrange.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /****************************************************************************
  2. ** Copyright (c) 2013-2014 Debao Zhang <hello@debao.me>
  3. ** All right reserved.
  4. **
  5. ** Permission is hereby granted, free of charge, to any person obtaining
  6. ** a copy of this software and associated documentation files (the
  7. ** "Software"), to deal in the Software without restriction, including
  8. ** without limitation the rights to use, copy, modify, merge, publish,
  9. ** distribute, sublicense, and/or sell copies of the Software, and to
  10. ** permit persons to whom the Software is furnished to do so, subject to
  11. ** the following conditions:
  12. **
  13. ** The above copyright notice and this permission notice shall be
  14. ** included in all copies or substantial portions of the Software.
  15. **
  16. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. **
  24. ****************************************************************************/
  25. #include "xlsxcellrange.h"
  26. #include "xlsxcellreference.h"
  27. #include <QString>
  28. #include <QPoint>
  29. #include <QStringList>
  30. QT_BEGIN_NAMESPACE_XLSX
  31. /*!
  32. \class CellRange
  33. \brief For a range "A1:B2" or single cell "A1"
  34. \inmodule QtXlsx
  35. The CellRange class stores the top left and bottom
  36. right rows and columns of a range in a worksheet.
  37. */
  38. /*!
  39. Constructs an range, i.e. a range
  40. whose rowCount() and columnCount() are 0.
  41. */
  42. CellRange::CellRange()
  43. : top(-1), left(-1), bottom(-2), right(-2)
  44. {
  45. }
  46. /*!
  47. Constructs the range from the given \a top, \a
  48. left, \a bottom and \a right rows and columns.
  49. \sa topRow(), leftColumn(), bottomRow(), rightColumn()
  50. */
  51. CellRange::CellRange(int top, int left, int bottom, int right)
  52. : top(top), left(left), bottom(bottom), right(right)
  53. {
  54. }
  55. CellRange::CellRange(const CellReference &topLeft, const CellReference &bottomRight)
  56. : top(topLeft.row()), left(topLeft.column())
  57. , bottom(bottomRight.row()), right(bottomRight.column())
  58. {
  59. }
  60. /*!
  61. \overload
  62. Constructs the range form the given \a range string.
  63. */
  64. CellRange::CellRange(const QString &range)
  65. {
  66. init(range);
  67. }
  68. /*!
  69. \overload
  70. Constructs the range form the given \a range string.
  71. */
  72. CellRange::CellRange(const char *range)
  73. {
  74. init(QString::fromLatin1(range));
  75. }
  76. void CellRange::init(const QString &range)
  77. {
  78. QStringList rs = range.split(QLatin1Char(':'));
  79. if (rs.size() == 2) {
  80. CellReference start(rs[0]);
  81. CellReference end(rs[1]);
  82. top = start.row();
  83. left = start.column();
  84. bottom = end.row();
  85. right = end.column();
  86. } else {
  87. CellReference p(rs[0]);
  88. top = p.row();
  89. left = p.column();
  90. bottom = p.row();
  91. right = p.column();
  92. }
  93. }
  94. /*!
  95. Constructs a the range by copying the given \a
  96. other range.
  97. */
  98. CellRange::CellRange(const CellRange &other)
  99. : top(other.top), left(other.left), bottom(other.bottom), right(other.right)
  100. {
  101. }
  102. /*!
  103. Destroys the range.
  104. */
  105. CellRange::~CellRange()
  106. {
  107. }
  108. /*!
  109. Convert the range to string notation, such as "A1:B5".
  110. */
  111. QString CellRange::toString(bool row_abs, bool col_abs) const
  112. {
  113. if (!isValid())
  114. return QString();
  115. if (left == right && top == bottom) {
  116. //Single cell
  117. return CellReference(top, left).toString(row_abs, col_abs);
  118. }
  119. QString cell_1 = CellReference(top, left).toString(row_abs, col_abs);
  120. QString cell_2 = CellReference(bottom, right).toString(row_abs, col_abs);
  121. return cell_1 + QLatin1String(":") + cell_2;
  122. }
  123. /*!
  124. * Returns true if the Range is valid.
  125. */
  126. bool CellRange::isValid() const
  127. {
  128. return left <= right && top <= bottom;
  129. }
  130. QT_END_NAMESPACE_XLSX