comparison src/xlsx/xlsxcellreference.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 "xlsxcellreference.h"
26 #include <QStringList>
27 #include <QMap>
28 #include <QRegularExpression>
29
30 QT_BEGIN_NAMESPACE_XLSX
31
32 namespace {
33
34 int intPow(int x, int p)
35 {
36 if (p == 0) return 1;
37 if (p == 1) return x;
38
39 int tmp = intPow(x, p/2);
40 if (p%2 == 0) return tmp * tmp;
41 else return x * tmp * tmp;
42 }
43
44 QString col_to_name(int col_num)
45 {
46 static QMap<int, QString> col_cache;
47
48 if (!col_cache.contains(col_num)) {
49 QString col_str;
50 int remainder;
51 while (col_num) {
52 remainder = col_num % 26;
53 if (remainder == 0)
54 remainder = 26;
55 col_str.prepend(QChar('A'+remainder-1));
56 col_num = (col_num - 1) / 26;
57 }
58 col_cache.insert(col_num, col_str);
59 }
60
61 return col_cache[col_num];
62 }
63
64 int col_from_name(const QString &col_str)
65 {
66 int col = 0;
67 int expn = 0;
68 for (int i=col_str.size()-1; i>-1; --i) {
69 col += (col_str[i].unicode() - 'A' + 1) * intPow(26, expn);
70 expn++;
71 }
72
73 return col;
74 }
75 } //namespace
76
77 /*!
78 \class CellReference
79 \brief For one single cell such as "A1"
80 \inmodule QtXlsx
81
82 The CellReference class stores the cell location in a worksheet.
83 */
84
85 /*!
86 Constructs an invalid Cell Reference
87 */
88 CellReference::CellReference()
89 : _row(-1), _column(-1)
90 {
91 }
92
93 /*!
94 Constructs the Reference from the given \a row, and \a column.
95 */
96 CellReference::CellReference(int row, int column)
97 : _row(row), _column(column)
98 {
99 }
100
101 /*!
102 \overload
103 Constructs the Reference form the given \a cell string.
104 */
105 CellReference::CellReference(const QString &cell)
106 {
107 init(cell);
108 }
109
110 /*!
111 \overload
112 Constructs the Reference form the given \a cell string.
113 */
114 CellReference::CellReference(const char *cell)
115 {
116 init(QString::fromLatin1(cell));
117 }
118
119 void CellReference::init(const QString &cell_str)
120 {
121 static QRegularExpression re(QStringLiteral("^\\$?([A-Z]{1,3})\\$?(\\d+)$"));
122 QRegularExpressionMatch match = re.match(cell_str);
123 if (match.hasMatch()) {
124 const QString col_str = match.captured(1);
125 const QString row_str = match.captured(2);
126 _row = row_str.toInt();
127 _column = col_from_name(col_str);
128 }
129 }
130
131 /*!
132 Constructs a Reference by copying the given \a
133 other Reference.
134 */
135 CellReference::CellReference(const CellReference &other)
136 : _row(other._row), _column(other._column)
137 {
138 }
139
140 /*!
141 Destroys the Reference.
142 */
143 CellReference::~CellReference()
144 {
145 }
146
147 /*!
148 Convert the Reference to string notation, such as "A1" or "$A$1".
149 If current object is invalid, an empty string will be returned.
150 */
151 QString CellReference::toString(bool row_abs, bool col_abs) const
152 {
153 if (!isValid())
154 return QString();
155
156 QString cell_str;
157 if (col_abs)
158 cell_str.append(QLatin1Char('$'));
159 cell_str.append(col_to_name(_column));
160 if (row_abs)
161 cell_str.append(QLatin1Char('$'));
162 cell_str.append(QString::number(_row));
163 return cell_str;
164 }
165
166 /*!
167 * Returns true if the Reference is valid.
168 */
169 bool CellReference::isValid() const
170 {
171 return _row > 0 && _column > 0;
172 }
173
174 QT_END_NAMESPACE_XLSX
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)