andre@1: /****************************************************************************
andre@1: ** Copyright (c) 2013-2014 Debao Zhang <hello@debao.me>
andre@1: ** All right reserved.
andre@1: **
andre@1: ** Permission is hereby granted, free of charge, to any person obtaining
andre@1: ** a copy of this software and associated documentation files (the
andre@1: ** "Software"), to deal in the Software without restriction, including
andre@1: ** without limitation the rights to use, copy, modify, merge, publish,
andre@1: ** distribute, sublicense, and/or sell copies of the Software, and to
andre@1: ** permit persons to whom the Software is furnished to do so, subject to
andre@1: ** the following conditions:
andre@1: **
andre@1: ** The above copyright notice and this permission notice shall be
andre@1: ** included in all copies or substantial portions of the Software.
andre@1: **
andre@1: ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
andre@1: ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
andre@1: ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
andre@1: ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
andre@1: ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
andre@1: ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
andre@1: ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
andre@1: **
andre@1: ****************************************************************************/
andre@1: #include "xlsxcellrange.h"
andre@1: #include "xlsxcellreference.h"
andre@1: #include <QString>
andre@1: #include <QPoint>
andre@1: #include <QStringList>
andre@1: 
andre@1: QT_BEGIN_NAMESPACE_XLSX
andre@1: 
andre@1: /*!
andre@1:     \class CellRange
andre@1:     \brief For a range "A1:B2" or single cell "A1"
andre@1:     \inmodule QtXlsx
andre@1: 
andre@1:     The CellRange class stores the top left and bottom
andre@1:     right rows and columns of a range in a worksheet.
andre@1: */
andre@1: 
andre@1: /*!
andre@1:     Constructs an range, i.e. a range
andre@1:     whose rowCount() and columnCount() are 0.
andre@1: */
andre@1: CellRange::CellRange()
andre@1:     : top(-1), left(-1), bottom(-2), right(-2)
andre@1: {
andre@1: }
andre@1: 
andre@1: /*!
andre@1:     Constructs the range from the given \a top, \a
andre@1:     left, \a bottom and \a right rows and columns.
andre@1: 
andre@1:     \sa topRow(), leftColumn(), bottomRow(), rightColumn()
andre@1: */
andre@1: CellRange::CellRange(int top, int left, int bottom, int right)
andre@1:     : top(top), left(left), bottom(bottom), right(right)
andre@1: {
andre@1: }
andre@1: 
andre@1: CellRange::CellRange(const CellReference &topLeft, const CellReference &bottomRight)
andre@1:     : top(topLeft.row()), left(topLeft.column())
andre@1:     , bottom(bottomRight.row()), right(bottomRight.column())
andre@1: {
andre@1: }
andre@1: 
andre@1: /*!
andre@1:     \overload
andre@1:     Constructs the range form the given \a range string.
andre@1: */
andre@1: CellRange::CellRange(const QString &range)
andre@1: {
andre@1:     init(range);
andre@1: }
andre@1: 
andre@1: /*!
andre@1:     \overload
andre@1:     Constructs the range form the given \a range string.
andre@1: */
andre@1: CellRange::CellRange(const char *range)
andre@1: {
andre@1:     init(QString::fromLatin1(range));
andre@1: }
andre@1: 
andre@1: void CellRange::init(const QString &range)
andre@1: {
andre@1:     QStringList rs = range.split(QLatin1Char(':'));
andre@1:     if (rs.size() == 2) {
andre@1:         CellReference start(rs[0]);
andre@1:         CellReference end(rs[1]);
andre@1:         top = start.row();
andre@1:         left = start.column();
andre@1:         bottom = end.row();
andre@1:         right = end.column();
andre@1:     } else {
andre@1:         CellReference p(rs[0]);
andre@1:         top = p.row();
andre@1:         left = p.column();
andre@1:         bottom = p.row();
andre@1:         right = p.column();
andre@1:     }
andre@1: }
andre@1: 
andre@1: /*!
andre@1:     Constructs a the range by copying the given \a
andre@1:     other range.
andre@1: */
andre@1: CellRange::CellRange(const CellRange &other)
andre@1:     : top(other.top), left(other.left), bottom(other.bottom), right(other.right)
andre@1: {
andre@1: }
andre@1: 
andre@1: /*!
andre@1:     Destroys the range.
andre@1: */
andre@1: CellRange::~CellRange()
andre@1: {
andre@1: }
andre@1: 
andre@1: /*!
andre@1:      Convert the range to string notation, such as "A1:B5".
andre@1: */
andre@1: QString CellRange::toString(bool row_abs, bool col_abs) const
andre@1: {
andre@1:     if (!isValid())
andre@1:         return QString();
andre@1: 
andre@1:     if (left == right && top == bottom) {
andre@1:         //Single cell
andre@1:         return CellReference(top, left).toString(row_abs, col_abs);
andre@1:     }
andre@1: 
andre@1:     QString cell_1 = CellReference(top, left).toString(row_abs, col_abs);
andre@1:     QString cell_2 = CellReference(bottom, right).toString(row_abs, col_abs);
andre@1:     return cell_1 + QLatin1String(":") + cell_2;
andre@1: }
andre@1: 
andre@1: /*!
andre@1:  * Returns true if the Range is valid.
andre@1:  */
andre@1: bool CellRange::isValid() const
andre@1: {
andre@1:     return left <= right && top <= bottom;
andre@1: }
andre@1: 
andre@1: QT_END_NAMESPACE_XLSX