File indexing completed on 2024-04-28 16:21:39

0001 /* This file is part of the KDE project
0002    Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
0003    Copyright 2004 Tomas Mecir <mecirt@gmail.com>
0004    Copyright 1998,1999 Torben Weis <weis@kde.org>
0005 
0006    This library is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU Library General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 
0011    This library is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014    Library General Public License for more details.
0015 
0016    You should have received a copy of the GNU Library General Public License
0017    along with this library; see the file COPYING.LIB.  If not, write to
0018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019    Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #ifndef CALLIGRA_SHEETS_VALUE_PARSER
0023 #define CALLIGRA_SHEETS_VALUE_PARSER
0024 
0025 #include <QDateTime>
0026 
0027 #include "Format.h"
0028 #include "Number.h"
0029 
0030 #include "sheets_odf_export.h"
0031 
0032 namespace Calligra
0033 {
0034 namespace Sheets
0035 {
0036 class CalculationSettings;
0037 class Value;
0038 
0039 /**
0040  * \ingroup Value
0041  * Generates a Value by parsing a user input text.
0042  * Determines the most probable Value type, e.g. integer or date.
0043  */
0044 class CALLIGRA_SHEETS_ODF_EXPORT ValueParser
0045 {
0046 public:
0047     /**
0048      * Constructor.
0049      */
0050     explicit ValueParser(const CalculationSettings* settings);
0051 
0052     /**
0053      * Returns the calculation settings this ValueFormatter uses.
0054      */
0055     const CalculationSettings* settings() const;
0056 
0057     /**
0058      * Parses the user input text \p str and tries to determine the correct
0059      * value type for it.
0060      */
0061     Value parse(const QString& str) const;
0062 
0063     /**
0064      * Tries for boolean type. If \p str can be interpreted as this
0065      * type, \p ok is set to \c true and the corresponding value will
0066      * be returned.
0067      */
0068     Value tryParseBool(const QString& str, bool *ok = 0) const;
0069 
0070     /**
0071      * Tries for floating point, integer, complex (and percentage) type.
0072      * If \p str can be interpreted as one of these types, \p ok is set to
0073      * \c true and the corresponding value will be returned.
0074      */
0075     Value tryParseNumber(const QString& str, bool *ok = 0) const;
0076 
0077     /**
0078      * Tries for date type. If \p str can be interpreted as this
0079      * type, \p ok is set to \c true and the corresponding value will
0080      * be returned.
0081      */
0082     Value tryParseDate(const QString& str, bool *ok = 0) const;
0083 
0084     /**
0085      * Tries for time type. If \p str can be interpreted as this
0086      * type, \p ok is set to \c true and the corresponding value will
0087      * be returned.
0088      */
0089     Value tryParseTime(const QString& str, bool *ok = 0) const;
0090 
0091 protected:
0092     /**
0093      * Converts \p str to a date/time value.
0094      */
0095     QDateTime readTime(const QString& str, bool withSeconds, bool *ok) const;
0096 
0097     /**
0098      * A helper function to read numbers and distinguish integers and FPs.
0099      */
0100     Value readNumber(const QString &_str, bool* ok) const;
0101 
0102     /**
0103      * A helper function to read the imaginary part of a complex number.
0104      */
0105     Number readImaginary(const QString& str, bool* ok) const;
0106 
0107     /**
0108      * A helper function to read integers.
0109      * Used in the parsing process for date and time values.
0110      */
0111     int readInt(const QString& str, uint& pos) const;
0112 
0113 private:
0114     const CalculationSettings* m_settings;
0115 };
0116 
0117 } // namespace Sheets
0118 } // namespace Calligra
0119 
0120 #endif  //CALLIGRA_SHEETS_VALUE_PARSER