1/* This file is part of the KDE project
2 Copyright 2006,2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
3 Copyright 1998,1999 Torben Weis <weis@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#ifndef CALLIGRA_SHEETS_UTIL
22#define CALLIGRA_SHEETS_UTIL
23
24#include <QString>
25#include <QRect>
26
27#include "calligra_sheets_export.h"
28#include <KoXmlReader.h>
29
30#include "Global.h"
31#include "Value.h"
32
33class QFont;
34class QPen;
35class QDomElement;
36class QDomDocument;
37
38class KLocale;
39
40bool util_isPointValid(const QPoint& point);
41bool util_isRectValid(const QRect& rect);
42
43namespace Calligra
44{
45namespace Sheets
46{
47class Cell;
48class Map;
49class Sheet;
50
51namespace Util
52{
53/**
54 * Call this function to decode the text of a column label to an integer,
55 * e.g. 1 for A and 27 for AA.
56 * Converted are all characters matching [A-Za-z]+ regular expression, the rest is ignored.
57 * 0 is returned if no characters match.
58 */
59CALLIGRA_SHEETS_ODF_EXPORT int decodeColumnLabelText(const QString &labelText);
60
61/**
62 * Call this function to decode the text of a row label to an integer,
63 * e.g. B7 is translated to 7.
64 */
65CALLIGRA_SHEETS_ODF_EXPORT int decodeRowLabelText(const QString &labelText);
66
67/**
68 * Call this function to encode an integer to the text of the column label
69 * i.e. 27->AA
70 */
71CALLIGRA_SHEETS_ODF_EXPORT QString encodeColumnLabelText(int column);
72
73/**
74 * Returns true if the given text is a cell-reference.
75 *
76 * This is an optimized version of QRegExp("^(\\$?)([a-zA-Z]+)(\\$?)([0-9]+)$")
77 * to check if the given string is a cell-reference like $A$1 or D17. Note
78 * that this will return false for cell-ranges like A1:B2 or cell-references
79 * given with sheet-name like Sheet1:A1.
80 *
81 * @param text The text to check
82 * @param startPos The position in the string where we should start to check
83 */
84CALLIGRA_SHEETS_ODF_EXPORT bool isCellReference(const QString &text, int startPos = 0);
85
86/**
87 * Generate and return the ODF formula for this cell (\p thisRow, \p thisColumn) based on the formula in the
88 * defined cell (\p referencedRow, \p referencedColumn ).
89 */
90CALLIGRA_SHEETS_ODF_EXPORT QString adjustFormulaReference(const QString& formula, int referencedRow, int referencedColumn, int thisRow, int thisColumn);
91
92//Return true when it's a reference to cell from sheet.
93CALLIGRA_SHEETS_ODF_EXPORT bool localReferenceAnchor(const QString &_ref);
94
95// TODO Stefan: used nowhere
96int penCompare(QPen const & pen1, QPen const & pen2);
97}
98
99/**
100 * \ingroup NativeFormat
101 * This namespace collects methods related to KSpread's old native file format
102 * encoding/decoding.
103 */
104namespace NativeFormat
105{
106/**
107 * \ingroup NativeFormat
108 */
109QDomElement createElement(const QString & tagName, const QFont & font, QDomDocument & doc);
110
111/**
112 * \ingroup NativeFormat
113 */
114QDomElement createElement(const QString & tagname, const QPen & pen, QDomDocument & doc);
115
116/**
117 * \ingroup NativeFormat
118 */
119QFont toFont(KoXmlElement & element);
120
121/**
122 * \ingroup NativeFormat
123 */
124QPen toPen(KoXmlElement & element);
125}
126
127/**
128 * \ingroup OpenDocument
129 * This namespace collects methods related to OpenDocument
130 * encoding/decoding.
131 */
132namespace Odf
133{
134/**
135 * \ingroup OpenDocument
136 * Creates OpenDocument pen attributes of the QPen \p pen .
137 * \return the OpenDocument pen attributes
138 */
139QString encodePen(const QPen& pen);
140
141/**
142 * \ingroup OpenDocument
143 * Creates a QPen of OpenDocument pen attributes \p str .
144 * \return the created QPen
145 */
146QPen decodePen(const QString &str);
147
148/**
149 * \ingroup OpenDocument
150 * Converts an OpenDocument representation of a formula to a localized formula.
151 * @param expr The expression to convert from OpenDocument format.
152 * @param locale The locale to which the expression should be converted.
153 * \note Use Region::loadOdf() for plain cell references.
154 */
155// TODO check visibility
156CALLIGRA_SHEETS_ODF_EXPORT QString decodeFormula(const QString& expression_, const KLocale *locale = 0, const QString &namespacePrefix = QString());
157
158/**
159 * \ingroup OpenDocument
160 * Converts a localized formula to an OpenDocument representation of a formula.
161 * @param expr The expression to convert to OpenDocument format.
162 * @param locale The locale from which the expression should be converted.
163 * \note Use Region::saveOdf() for plain cell references.
164 */
165CALLIGRA_SHEETS_ODF_EXPORT QString encodeFormula(const QString& expr, const KLocale* locale = 0);
166
167/**
168 * \ingroup OpenDocument
169 */
170CALLIGRA_SHEETS_ODF_EXPORT QString convertRefToRange(const QString & sheet, const QRect & rect);
171
172/**
173 * \ingroup OpenDocument
174 */
175CALLIGRA_SHEETS_ODF_EXPORT QString convertRefToBase(const QString & sheet, const QRect & rect);
176
177/**
178 * \ingroup OpenDocument
179 */
180CALLIGRA_SHEETS_ODF_EXPORT QString convertRangeToRef(const QString & sheetName, const QRect & _area);
181}
182
183namespace MSOOXML
184{
185/**
186 * Convert the MSOOXML \p formula into a ODF formula and return that ODF formula.
187 */
188CALLIGRA_SHEETS_ODF_EXPORT QString convertFormula(const QString& formula);
189}
190
191} // namespace Sheets
192} // namespace Calligra
193
194#endif // CALLIGRA_SHEETS_UTIL
195