kspread

Value.h

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
00003    Copyright 2003,2004 Ariya Hidayat <ariya@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; only
00008    version 2 of the License.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #ifndef KSPREAD_VALUE_H
00022 #define KSPREAD_VALUE_H
00023 
00024 #include <complex>
00025 
00026 #include <QDateTime>
00027 #include <QSharedDataPointer>
00028 #include <QString>
00029 #include <QTextStream>
00030 
00031 #include <kdebug.h>
00032 
00033 #include "kspread_export.h"
00034 
00035 #include "Number.h"
00036 
00037 using namespace std;
00038 
00039 namespace KSpread
00040 {
00041 class CalculationSettings;
00042 class ValueStorage;
00043 
00052 class KSPREAD_EXPORT Value
00053 {
00054 
00055 public:
00056 
00057     enum Type {
00058         Empty,
00059         Boolean,
00060         Integer,
00061         Float,
00062         Complex,
00063         String,
00064         Array,
00065         CellRange, // not used yet
00066         Error
00067     };
00068 
00069     enum Format {
00070         fmt_None,
00071         fmt_Boolean,
00072         fmt_Number,
00073         fmt_Percent,
00074         fmt_Money,
00075         fmt_DateTime,
00076         fmt_Date,
00077         fmt_Time,
00078         fmt_String
00079     };
00083     Value();
00084 
00088     explicit Value(Type _type);
00089 
00093     virtual ~Value();
00094 
00098     Value(const Value& _value);
00099 
00106     Value& operator= (const Value& _value);
00107 
00111     explicit Value(bool b);
00112 
00116     explicit Value(qint64 i);
00117 
00121     explicit Value(int i);
00122 
00126     explicit Value(double f);
00127 
00131     explicit Value(long double f);
00132 
00133 #ifdef KSPREAD_HIGH_PRECISION_SUPPORT
00134 
00137     explicit Value(Number f);
00138 #endif // KSPREAD_HIGH_PRECISION_SUPPORT
00139 
00143     explicit Value(const complex<Number>& c);
00144 
00148     explicit Value(const QString& s);
00149 
00153     explicit Value(const char *s);
00154 
00158     explicit Value(const ValueStorage& array);
00159 
00166     Value(const QDateTime& dt, const CalculationSettings* settings);
00167 
00172     Value(const QTime& time, const CalculationSettings* settings);
00173 
00178     Value(const QDate& date, const CalculationSettings* settings);
00179 
00183     Type type() const;
00184 
00188     Format format() const;
00189 
00193     void setFormat(Format fmt);
00194 
00198     bool isEmpty() const {
00199         return type() == Empty;
00200     }
00201 
00205     bool isBoolean() const {
00206         return type() == Boolean;
00207     }
00208 
00212     bool isInteger() const {
00213         return type() == Integer;
00214     }
00215 
00219     bool isFloat() const {
00220         return type() == Float;
00221     }
00222 
00226     bool isComplex() const {
00227         return type() == Complex;
00228     }
00229 
00234     bool isNumber() const {
00235         return (type() == Integer) || (type() == Float) || (type() == Complex);
00236     }
00237 
00241     bool isString() const {
00242         return type() == String;
00243     }
00244 
00248     bool isArray() const {
00249         return type() == Array;
00250     }
00251 
00255     bool isError() const {
00256         return type() == Error;
00257     }
00258 
00262     void setError(const QString& msg);
00263 
00269     bool asBoolean() const;
00270 
00276     qint64 asInteger() const;
00277 
00283     Number asFloat() const;
00284 
00290     complex<Number> asComplex() const;
00291 
00297     QString asString() const;
00298 
00302     QVariant asVariant() const;
00303 
00307     QDateTime asDateTime(const CalculationSettings* settings) const;
00308 
00312     QDate asDate(const CalculationSettings* settings) const;
00313 
00317     QTime asTime(const CalculationSettings* settings) const;
00318 
00322     Value element(unsigned column, unsigned row) const;
00323 
00330     Value element(unsigned index) const;
00331 
00335     void setElement(unsigned column, unsigned row, const Value& value);
00336 
00341     unsigned columns() const;
00342 
00347     unsigned rows() const;
00348 
00355     unsigned count() const;
00356 
00362     QString errorMessage() const;
00363 
00367     static const Value& empty();
00368 
00374     static const Value& errorCIRCLE();
00375 
00381     static const Value& errorDEPEND();
00382 
00388     static const Value& errorDIV0();
00389 
00395     static const Value& errorNA();
00396 
00404     static const Value& errorNAME();
00405 
00411     static const Value& errorNUM();
00412 
00418     static const Value& errorNULL();
00419 
00425     static const Value& errorPARSE();
00426 
00432     static const Value& errorREF();
00433 
00440     static const Value& errorVALUE();
00441 
00446     bool allowComparison(const Value& v) const;
00447 
00452     int compare(const Value& v) const;
00453 
00457     bool equal(const Value& v) const;
00458 
00462     bool less(const Value& v) const;
00463 
00467     bool greater(const Value& v) const;
00468 
00469     // comparison operator - returns true only if strictly identical, unlike equal()/compare()
00470     bool operator==(const Value& v) const;
00471     inline bool operator!=(const Value& other) const {
00472         return !operator==(other);
00473     }
00474 
00475     static int compare(Number v1, Number v2);
00476 
00477     bool isZero() const;
00478 
00479     static bool isZero(Number v);
00480 
00481 private:
00482     class Private;
00483     QSharedDataPointer<Private> d;
00484 };
00485 
00486 /***************************************************************************
00487   QHash/QSet support
00488 ****************************************************************************/
00489 
00490 uint qHash(const Value& value);
00491 
00492 } // namespace KSpread
00493 
00494 Q_DECLARE_TYPEINFO(KSpread::Value, Q_MOVABLE_TYPE);
00495 
00496 
00497 /***************************************************************************
00498   QTextStream support
00499 ****************************************************************************/
00500 
00501 KSPREAD_EXPORT QTextStream& operator<<(QTextStream& ts, KSpread::Value::Type type);
00502 KSPREAD_EXPORT QTextStream& operator<<(QTextStream& ts, KSpread::Value value);
00503 
00504 /***************************************************************************
00505   kDebug support
00506 ****************************************************************************/
00507 
00508 KSPREAD_EXPORT kdbgstream operator<<(kdbgstream str, const KSpread::Value& v);
00509 kdbgstream operator<<(kdbgstream stream, const KSpread::Value::Format& f);
00510 
00511 #endif // KSPREAD_VALUE_H