comparison src/xlsx/xlsxcellrange.cpp @ 1:93d3106bb9a4

Add qt xlsx library
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 22 Mar 2016 10:38:08 +0100
parents
children
comparison
equal deleted inserted replaced
0:49cd5cc0b072 1:93d3106bb9a4
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
31 QT_BEGIN_NAMESPACE_XLSX
32
33 /*!
34 \class CellRange
35 \brief For a range "A1:B2" or single cell "A1"
36 \inmodule QtXlsx
37
38 The CellRange class stores the top left and bottom
39 right rows and columns of a range in a worksheet.
40 */
41
42 /*!
43 Constructs an range, i.e. a range
44 whose rowCount() and columnCount() are 0.
45 */
46 CellRange::CellRange()
47 : top(-1), left(-1), bottom(-2), right(-2)
48 {
49 }
50
51 /*!
52 Constructs the range from the given \a top, \a
53 left, \a bottom and \a right rows and columns.
54
55 \sa topRow(), leftColumn(), bottomRow(), rightColumn()
56 */
57 CellRange::CellRange(int top, int left, int bottom, int right)
58 : top(top), left(left), bottom(bottom), right(right)
59 {
60 }
61
62 CellRange::CellRange(const CellReference &topLeft, const CellReference &bottomRight)
63 : top(topLeft.row()), left(topLeft.column())
64 , bottom(bottomRight.row()), right(bottomRight.column())
65 {
66 }
67
68 /*!
69 \overload
70 Constructs the range form the given \a range string.
71 */
72 CellRange::CellRange(const QString &range)
73 {
74 init(range);
75 }
76
77 /*!
78 \overload
79 Constructs the range form the given \a range string.
80 */
81 CellRange::CellRange(const char *range)
82 {
83 init(QString::fromLatin1(range));
84 }
85
86 void CellRange::init(const QString &range)
87 {
88 QStringList rs = range.split(QLatin1Char(':'));
89 if (rs.size() == 2) {
90 CellReference start(rs[0]);
91 CellReference end(rs[1]);
92 top = start.row();
93 left = start.column();
94 bottom = end.row();
95 right = end.column();
96 } else {
97 CellReference p(rs[0]);
98 top = p.row();
99 left = p.column();
100 bottom = p.row();
101 right = p.column();
102 }
103 }
104
105 /*!
106 Constructs a the range by copying the given \a
107 other range.
108 */
109 CellRange::CellRange(const CellRange &other)
110 : top(other.top), left(other.left), bottom(other.bottom), right(other.right)
111 {
112 }
113
114 /*!
115 Destroys the range.
116 */
117 CellRange::~CellRange()
118 {
119 }
120
121 /*!
122 Convert the range to string notation, such as "A1:B5".
123 */
124 QString CellRange::toString(bool row_abs, bool col_abs) const
125 {
126 if (!isValid())
127 return QString();
128
129 if (left == right && top == bottom) {
130 //Single cell
131 return CellReference(top, left).toString(row_abs, col_abs);
132 }
133
134 QString cell_1 = CellReference(top, left).toString(row_abs, col_abs);
135 QString cell_2 = CellReference(bottom, right).toString(row_abs, col_abs);
136 return cell_1 + QLatin1String(":") + cell_2;
137 }
138
139 /*!
140 * Returns true if the Range is valid.
141 */
142 bool CellRange::isValid() const
143 {
144 return left <= right && top <= bottom;
145 }
146
147 QT_END_NAMESPACE_XLSX
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)