1/* This file is part of the KDE project
2 Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
3 Copyright 2004 Tomas Mecir <mecirt@gmail.com>
4 Copyright 1998,1999 Torben Weis <weis@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22#ifndef CALLIGRA_SHEETS_VALUE_PARSER
23#define CALLIGRA_SHEETS_VALUE_PARSER
24
25#include <QDateTime>
26
27#include "Format.h"
28#include "Number.h"
29
30#include "calligra_sheets_export.h"
31
32namespace Calligra
33{
34namespace Sheets
35{
36class CalculationSettings;
37class Value;
38
39/**
40 * \ingroup Value
41 * Generates a Value by parsing an user input text.
42 * Determines the most probable Value type, e.g. integer or date.
43 */
44class CALLIGRA_SHEETS_ODF_EXPORT ValueParser
45{
46public:
47 /**
48 * Constructor.
49 */
50 explicit ValueParser(const CalculationSettings* settings);
51
52 /**
53 * Returns the calculation settings this ValueFormatter uses.
54 */
55 const CalculationSettings* settings() const;
56
57 /**
58 * Parses the user input text \p str and tries to determine the correct
59 * value type for it.
60 */
61 Value parse(const QString& str) const;
62
63 /**
64 * Tries for boolean type. If \p str can be interpreted as this
65 * type, \p ok is set to \c true and the corresponding value will
66 * be returned.
67 */
68 Value tryParseBool(const QString& str, bool *ok = 0) const;
69
70 /**
71 * Tries for floating point, integer, complex (and percentage) type.
72 * If \p str can be interpreted as one of these types, \p ok is set to
73 * \c true and the corresponding value will be returned.
74 */
75 Value tryParseNumber(const QString& str, bool *ok = 0) const;
76
77 /**
78 * Tries for date type. If \p str can be interpreted as this
79 * type, \p ok is set to \c true and the corresponding value will
80 * be returned.
81 */
82 Value tryParseDate(const QString& str, bool *ok = 0) const;
83
84 /**
85 * Tries for time type. If \p str can be interpreted as this
86 * type, \p ok is set to \c true and the corresponding value will
87 * be returned.
88 */
89 Value tryParseTime(const QString& str, bool *ok = 0) const;
90
91protected:
92 /**
93 * Converts \p str to a date/time value.
94 */
95 QDateTime readTime(const QString& str, bool withSeconds, bool *ok) const;
96
97 /**
98 * A helper function to read numbers and distinguish integers and FPs.
99 */
100 Value readNumber(const QString &_str, bool* ok) const;
101
102 /**
103 * A helper function to read the imaginary part of a complex number.
104 */
105 Number readImaginary(const QString& str, bool* ok) const;
106
107 /**
108 * A helper function to read integers.
109 * Used in the parsing process for date and time values.
110 */
111 int readInt(const QString& str, uint& pos) const;
112
113private:
114 const CalculationSettings* m_settings;
115};
116
117} // namespace Sheets
118} // namespace Calligra
119
120#endif //CALLIGRA_SHEETS_VALUE_PARSER
121