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

0001 /* This file is part of the KDE project
0002    Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
0003    Copyright 2003,2004 Ariya Hidayat <ariya@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; only
0008    version 2 of the License.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #ifndef CALLIGRA_SHEETS_VALUE_H
0022 #define CALLIGRA_SHEETS_VALUE_H
0023 
0024 #include <complex>
0025 
0026 #include <QDateTime>
0027 #include <QSharedDataPointer>
0028 #include <QString>
0029 #include <QTextStream>
0030 #include <QVariant>
0031 
0032 #include "SheetsDebug.h"
0033 #include "Number.h"
0034 
0035 using namespace std;
0036 
0037 namespace Calligra
0038 {
0039 namespace Sheets
0040 {
0041 class CalculationSettings;
0042 class ValueStorage;
0043 
0044 /**
0045  * \ingroup Value
0046  * Provides a wrapper for cell value.
0047  *
0048  * Each cell in a worksheet must hold a value, either as entered by user
0049  * or as a result of formula evaluation. Default cell holds empty value.
0050  *
0051  * Value uses implicit data sharing to reduce memory usage.
0052  */
0053 class CALLIGRA_SHEETS_ODF_EXPORT Value
0054 {
0055 
0056 public:
0057 
0058     enum Type {
0059         Empty,
0060         Boolean,
0061         Integer,
0062         Float,
0063         Complex,
0064         String,
0065         Array,
0066         CellRange, // not used yet
0067         Error
0068     };
0069 
0070     enum Format {
0071         fmt_None,
0072         fmt_Boolean,
0073         fmt_Number,
0074         fmt_Percent,
0075         fmt_Money,
0076         fmt_DateTime,
0077         fmt_Date,
0078         fmt_Time,
0079         fmt_String
0080     };
0081     /**
0082      * Creates an empty value, i.e holds nothing.
0083      */
0084     Value();
0085 
0086     /**
0087      * Creates a value of certain type.
0088      */
0089     explicit Value(Type _type);
0090 
0091     /**
0092      * Destroys the value.
0093      */
0094     virtual ~Value();
0095 
0096     /**
0097      * Creates a copy from another value.
0098      */
0099     Value(const Value& _value);
0100 
0101     /**
0102      * Assigns from another value.
0103      *
0104      * Because the data is implicitly shared, such assignment is very fast and
0105      * doesn't consume additional memory.
0106      */
0107     Value& operator= (const Value& _value);
0108 
0109     /**
0110      * Creates a boolean value.
0111      */
0112     explicit Value(bool b);
0113 
0114     /**
0115      * Creates an integer value.
0116      */
0117     explicit Value(qint64 i);
0118 
0119     /**
0120      * Creates an integer value.
0121      */
0122     explicit Value(int i);
0123 
0124     /**
0125      * Creates a floating-point value.
0126      */
0127     explicit Value(double f);
0128 
0129     /**
0130      * Creates a floating-point value.
0131      */
0132     explicit Value(long double f);
0133 
0134 #ifdef CALLIGRA_SHEETS_HIGH_PRECISION_SUPPORT
0135     /**
0136      * Creates a floating-point value.
0137      */
0138     explicit Value(Number f);
0139 #endif // CALLIGRA_SHEETS_HIGH_PRECISION_SUPPORT
0140 
0141     /**
0142      * Creates a complex number value.
0143      */
0144     explicit Value(const complex<Number>& c);
0145 
0146     /**
0147      * Creates a string value.
0148      */
0149     explicit Value(const QString& s);
0150 
0151     /**
0152      * Creates a string value.
0153      */
0154     explicit Value(const char *s);
0155 
0156     /**
0157      * Creates an array value using the data from \p array.
0158      */
0159     explicit Value(const ValueStorage& array, const QSize& size);
0160 
0161     /**
0162      * Creates a floating-point value from date/time.
0163      *
0164      * Internally date/time is represented as serial-number, i.e number of
0165      * elapsed day since reference date. Day 61 is defined as March 1, 1900.
0166      */
0167     Value(const QDateTime& dt, const CalculationSettings* settings);
0168 
0169     /**
0170      * Creates a floating-point value from time.
0171      * See also note above.
0172      */
0173     explicit Value(const QTime& time);
0174 
0175     /**
0176      * Creates a floating-point value from date.
0177      * See also note above.
0178      */
0179     Value(const QDate& date, const CalculationSettings* settings);
0180 
0181     /**
0182      * Returns the type of the value.
0183      */
0184     Type type() const;
0185 
0186     /**
0187      * Returns true if null.
0188      *
0189      * A null value is equal to an empty value (and the other way around) in
0190      * every way, except for what isNull() returns.
0191      */
0192     bool isNull() const;
0193 
0194     /**
0195      * Returns the format of the value, i.e. how should it be interpreted.
0196      */
0197     Format format() const;
0198 
0199     /**
0200      * Sets format information for this value.
0201      */
0202     void setFormat(Format fmt);
0203 
0204     /**
0205      * Returns true if empty.
0206      */
0207     bool isEmpty() const {
0208         return type() == Empty;
0209     }
0210 
0211     /**
0212      * Returns true, if the type of this value is Boolean.
0213      */
0214     bool isBoolean() const {
0215         return type() == Boolean;
0216     }
0217 
0218     /**
0219      * Returns true, if the type of this value is integer.
0220      */
0221     bool isInteger() const {
0222         return type() == Integer;
0223     }
0224 
0225     /**
0226      * Returns true, if the type of this value is floating-point.
0227      */
0228     bool isFloat() const {
0229         return type() == Float;
0230     }
0231 
0232     /**
0233      * Returns true, if the type of this value is the complex number type.
0234      */
0235     bool isComplex() const {
0236         return type() == Complex;
0237     }
0238 
0239     /**
0240      * Returns true, if the type of this value is either
0241      * integer, floating-point or complex number.
0242      */
0243     bool isNumber() const {
0244         return (type() == Integer) || (type() == Float) || (type() == Complex);
0245     }
0246 
0247     /**
0248      * Returns true, if the type of this value is string.
0249      */
0250     bool isString() const {
0251         return type() == String;
0252     }
0253 
0254     /**
0255      * Returns true, if the type of this value is array.
0256      */
0257     bool isArray() const {
0258         return type() == Array;
0259     }
0260 
0261     /**
0262      * Returns true, if this value holds error information.
0263      */
0264     bool isError() const {
0265         return type() == Error;
0266     }
0267 
0268     /**
0269      * Sets this value to hold error message.
0270      */
0271     void setError(const QString& msg);
0272 
0273     /**
0274      * Returns the boolean value of this value.
0275      *
0276      * Call this function only if isBoolean() returns true.
0277      */
0278     bool asBoolean() const;
0279 
0280     /**
0281      * Returns the integer value of this value.
0282      *
0283      * Call this function only if isNumber() returns true.
0284      */
0285     qint64 asInteger() const;
0286 
0287     /**
0288      * Returns the floating-point value of this value.
0289      *
0290      * Call this function only if isNumber() returns true.
0291      */
0292     Number asFloat() const;
0293 
0294     /**
0295      * Returns the complex number value of this value.
0296      *
0297      * Call this function only if isNumber() returns true.
0298      */
0299     complex<Number> asComplex() const;
0300 
0301     /**
0302      * Returns the string value of this value.
0303      *
0304      * Call this function only if isString() returns true.
0305      */
0306     QString asString() const;
0307 
0308     /**
0309      * Returns the double quoted string value of this value.
0310      *
0311      * Same as \a asString but with double quotes around. This
0312      * is needed for example in Odf::saveConditionValue
0313      * to save Value strings with double quotes.
0314      */
0315     QString asStringWithDoubleQuotes() const;
0316 
0317     /**
0318      * Returns the data as a QVariant
0319      */
0320     QVariant asVariant() const;
0321 
0322     /**
0323      * Returns the date/time representation of this value.
0324      */
0325     QDateTime asDateTime(const CalculationSettings* settings) const;
0326 
0327     /**
0328      * Returns the date representation of this value.
0329      */
0330     QDate asDate(const CalculationSettings* settings) const;
0331 
0332     /**
0333      * Returns the time representation of this value.
0334      */
0335     QTime asTime() const;
0336 
0337     /**
0338      * Returns an element in the array value.
0339      */
0340     Value element(unsigned column, unsigned row) const;
0341 
0342     /**
0343      * Returns an array element given by its index denoting its position in the
0344      * row-wise sorted list of non-empty values.
0345      * Usable to iterate over the array.
0346      * \see count()
0347      */
0348     Value element(unsigned index) const;
0349 
0350     /**
0351      * Sets an element in the array value. Do not use if isArray() is false.
0352      */
0353     void setElement(unsigned column, unsigned row, const Value& value);
0354 
0355     /**
0356      * If this value is an array, return the number of columns.
0357      * Returns 1, if isArray() returns false.
0358      */
0359     unsigned columns() const;
0360 
0361     /**
0362      * If this value is an array, return the number of rows.
0363      * Returns 1, if isArray() returns false.
0364      */
0365     unsigned rows() const;
0366 
0367     /**
0368      * If this value is an array, return the number of non-empty elements.
0369      * Returns 1 if isArray() returns false.
0370      * Usable to iterate over the array.
0371      * \see element(unsigned)
0372      */
0373     unsigned count() const;
0374 
0375     /**
0376      * Returns error message associated with this value.
0377      *
0378      * Call this function only if isError() returns true.
0379      */
0380     QString errorMessage() const;
0381 
0382     /**
0383      * Returns constant reference to empty value.
0384      */
0385     static const Value& empty();
0386 
0387     /*
0388      * Returns a constant reference to a null value.
0389      *
0390      * A null value is equal to an empty value (and the other way around) in
0391      * every way, except for what isNull() returns.
0392      */
0393     static const Value& null();
0394 
0395     /**
0396      * Returns constant reference to '\#CIRCLE!' error.
0397      *
0398      * This is used to indicate circular cell references.
0399      */
0400     static const Value& errorCIRCLE();
0401 
0402     /**
0403      * Returns constant reference to '\#DEPEND!' error.
0404      *
0405      * This is used to indicate broken cell references.
0406      */
0407     static const Value& errorDEPEND();
0408 
0409     /**
0410      * Returns constant reference to '\#DIV/0!' error.
0411      *
0412      * This is used to indicate that a formula divides by 0 (zero).
0413      */
0414     static const Value& errorDIV0();
0415 
0416     /**
0417      * Returns constant reference to '\#N/A' error.
0418      *
0419      * This is to indicate that  a value is not available to a function.
0420      */
0421     static const Value& errorNA();
0422 
0423     /**
0424      * Returns constant reference to '\#NAME?' error.
0425      *
0426      * This is to indicate that certain text inside formula is not
0427      * recognized, possibly a misspelled name or name that
0428      * does not exist.
0429      */
0430     static const Value& errorNAME();
0431 
0432     /**
0433      * Returns constant reference to '\#NUM!' error.
0434      *
0435      * This is to indicate a problem with a number in a formula.
0436      */
0437     static const Value& errorNUM();
0438 
0439     /**
0440      * Returns constant reference to '\#NULL!' error.
0441      *
0442      * This is to indicate that two area do not intersect.
0443      */
0444     static const Value& errorNULL();
0445 
0446     /**
0447      * Returns constant reference to '\#PARSE!' error.
0448      *
0449      * This is used to indicate that a formula could not be parsed correctly.
0450      */
0451     static const Value& errorPARSE();
0452 
0453     /**
0454      * Returns constant reference to '\#REF!' error.
0455      *
0456      * This is used to indicate an invalid cell reference.
0457      */
0458     static const Value& errorREF();
0459 
0460     /**
0461      * Returns constant reference to '\#VALUE!' error.
0462      *
0463      * This is to indicate that wrong type of argument or operand
0464      * is used, usually within a function call, e.g SIN("some text").
0465      */
0466     static const Value& errorVALUE();
0467 
0468     /**
0469      * Returns true if it is OK to compare this value with v.
0470      * If this function returns false, then return value of compare is undefined.
0471      */
0472     bool allowComparison(const Value& v) const;
0473 
0474     /**
0475      * Returns -1, 0, 1, depends whether this value is less than, equal to, or
0476      * greater than v.
0477      */
0478     int compare(const Value& v, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0479 
0480     /**
0481      * Returns true if this value is equal to v.
0482      */
0483     bool equal(const Value& v, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0484 
0485     /**
0486      * Returns true if this value is less than v.
0487      */
0488     bool less(const Value& v, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0489 
0490     /**
0491      * Returns true if this value is greater than v.
0492      */
0493     bool greater(const Value& v, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0494 
0495     // comparison operator - returns true only if strictly identical, unlike equal()/compare()
0496     bool operator==(const Value& v) const;
0497     inline bool operator!=(const Value& other) const {
0498         return !operator==(other);
0499     }
0500 
0501     static int compare(Number v1, Number v2);
0502 
0503     bool isZero() const;
0504 
0505     static bool isZero(Number v);
0506 
0507 private:
0508     class Private;
0509     QSharedDataPointer<Private> d;
0510 };
0511 
0512 /***************************************************************************
0513   QHash/QSet support
0514 ****************************************************************************/
0515 
0516 uint qHash(const Value& value);
0517 
0518 } // namespace Sheets
0519 } // namespace Calligra
0520 
0521 Q_DECLARE_METATYPE(Calligra::Sheets::Value)
0522 Q_DECLARE_TYPEINFO(Calligra::Sheets::Value, Q_MOVABLE_TYPE);
0523 
0524 
0525 /***************************************************************************
0526   QTextStream support
0527 ****************************************************************************/
0528 
0529 CALLIGRA_SHEETS_ODF_EXPORT QTextStream& operator<<(QTextStream& ts, Calligra::Sheets::Value::Type type);
0530 CALLIGRA_SHEETS_ODF_EXPORT QTextStream& operator<<(QTextStream& ts, Calligra::Sheets::Value value);
0531 
0532 /***************************************************************************
0533   QDebug support
0534 ****************************************************************************/
0535 
0536 CALLIGRA_SHEETS_ODF_EXPORT QDebug operator<<(QDebug str, const Calligra::Sheets::Value& v);
0537 QDebug operator<<(QDebug stream, const Calligra::Sheets::Value::Format& f);
0538 
0539 #endif // CALLIGRA_SHEETS_VALUE_H