File: Value.h

package info (click to toggle)
calligra 1%3A3.2.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 260,432 kB
  • sloc: cpp: 650,911; xml: 27,662; python: 6,044; perl: 2,724; yacc: 1,817; ansic: 1,325; sh: 1,277; lex: 1,107; ruby: 1,010; javascript: 495; makefile: 24
file content (539 lines) | stat: -rw-r--r-- 13,602 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/* This file is part of the KDE project
   Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
   Copyright 2003,2004 Ariya Hidayat <ariya@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; only
   version 2 of the License.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#ifndef CALLIGRA_SHEETS_VALUE_H
#define CALLIGRA_SHEETS_VALUE_H

#include <complex>

#include <QDateTime>
#include <QSharedDataPointer>
#include <QString>
#include <QTextStream>
#include <QVariant>

#include "SheetsDebug.h"
#include "Number.h"

using namespace std;

namespace Calligra
{
namespace Sheets
{
class CalculationSettings;
class ValueStorage;

/**
 * \ingroup Value
 * Provides a wrapper for cell value.
 *
 * Each cell in a worksheet must hold a value, either as entered by user
 * or as a result of formula evaluation. Default cell holds empty value.
 *
 * Value uses implicit data sharing to reduce memory usage.
 */
class CALLIGRA_SHEETS_ODF_EXPORT Value
{

public:

    enum Type {
        Empty,
        Boolean,
        Integer,
        Float,
        Complex,
        String,
        Array,
        CellRange, // not used yet
        Error
    };

    enum Format {
        fmt_None,
        fmt_Boolean,
        fmt_Number,
        fmt_Percent,
        fmt_Money,
        fmt_DateTime,
        fmt_Date,
        fmt_Time,
        fmt_String
    };
    /**
     * Creates an empty value, i.e holds nothing.
     */
    Value();

    /**
     * Creates a value of certain type.
     */
    explicit Value(Type _type);

    /**
     * Destroys the value.
     */
    virtual ~Value();

    /**
     * Creates a copy from another value.
     */
    Value(const Value& _value);

    /**
     * Assigns from another value.
     *
     * Because the data is implicitly shared, such assignment is very fast and
     * doesn't consume additional memory.
     */
    Value& operator= (const Value& _value);

    /**
     * Creates a boolean value.
     */
    explicit Value(bool b);

    /**
     * Creates an integer value.
     */
    explicit Value(qint64 i);

    /**
     * Creates an integer value.
     */
    explicit Value(int i);

    /**
     * Creates a floating-point value.
     */
    explicit Value(double f);

    /**
     * Creates a floating-point value.
     */
    explicit Value(long double f);

#ifdef CALLIGRA_SHEETS_HIGH_PRECISION_SUPPORT
    /**
     * Creates a floating-point value.
     */
    explicit Value(Number f);
#endif // CALLIGRA_SHEETS_HIGH_PRECISION_SUPPORT

    /**
     * Creates a complex number value.
     */
    explicit Value(const complex<Number>& c);

    /**
     * Creates a string value.
     */
    explicit Value(const QString& s);

    /**
     * Creates a string value.
     */
    explicit Value(const char *s);

    /**
     * Creates an array value using the data from \p array.
     */
    explicit Value(const ValueStorage& array, const QSize& size);

    /**
     * Creates a floating-point value from date/time.
     *
     * Internally date/time is represented as serial-number, i.e number of
     * elapsed day since reference date. Day 61 is defined as March 1, 1900.
     */
    Value(const QDateTime& dt, const CalculationSettings* settings);

    /**
     * Creates a floating-point value from time.
     * See also note above.
     */
    explicit Value(const QTime& time);

    /**
     * Creates a floating-point value from date.
     * See also note above.
     */
    Value(const QDate& date, const CalculationSettings* settings);

    /**
     * Returns the type of the value.
     */
    Type type() const;

    /**
     * Returns true if null.
     *
     * A null value is equal to an empty value (and the other way around) in
     * every way, except for what isNull() returns.
     */
    bool isNull() const;

    /**
     * Returns the format of the value, i.e. how should it be interpreted.
     */
    Format format() const;

    /**
     * Sets format information for this value.
     */
    void setFormat(Format fmt);

    /**
     * Returns true if empty.
     */
    bool isEmpty() const {
        return type() == Empty;
    }

    /**
     * Returns true, if the type of this value is Boolean.
     */
    bool isBoolean() const {
        return type() == Boolean;
    }

    /**
     * Returns true, if the type of this value is integer.
     */
    bool isInteger() const {
        return type() == Integer;
    }

    /**
     * Returns true, if the type of this value is floating-point.
     */
    bool isFloat() const {
        return type() == Float;
    }

    /**
     * Returns true, if the type of this value is the complex number type.
     */
    bool isComplex() const {
        return type() == Complex;
    }

    /**
     * Returns true, if the type of this value is either
     * integer, floating-point or complex number.
     */
    bool isNumber() const {
        return (type() == Integer) || (type() == Float) || (type() == Complex);
    }

    /**
     * Returns true, if the type of this value is string.
     */
    bool isString() const {
        return type() == String;
    }

    /**
     * Returns true, if the type of this value is array.
     */
    bool isArray() const {
        return type() == Array;
    }

    /**
     * Returns true, if this value holds error information.
     */
    bool isError() const {
        return type() == Error;
    }

    /**
     * Sets this value to hold error message.
     */
    void setError(const QString& msg);

    /**
     * Returns the boolean value of this value.
     *
     * Call this function only if isBoolean() returns true.
     */
    bool asBoolean() const;

    /**
     * Returns the integer value of this value.
     *
     * Call this function only if isNumber() returns true.
     */
    qint64 asInteger() const;

    /**
     * Returns the floating-point value of this value.
     *
     * Call this function only if isNumber() returns true.
     */
    Number asFloat() const;

    /**
     * Returns the complex number value of this value.
     *
     * Call this function only if isNumber() returns true.
     */
    complex<Number> asComplex() const;

    /**
     * Returns the string value of this value.
     *
     * Call this function only if isString() returns true.
     */
    QString asString() const;

    /**
     * Returns the double quoted string value of this value.
     *
     * Same as \a asString but with double quotes around. This
     * is needed for example in Odf::saveConditionValue
     * to save Value strings with double quotes.
     */
    QString asStringWithDoubleQuotes() const;

    /**
     * Returns the data as a QVariant
     */
    QVariant asVariant() const;

    /**
     * Returns the date/time representation of this value.
     */
    QDateTime asDateTime(const CalculationSettings* settings) const;

    /**
     * Returns the date representation of this value.
     */
    QDate asDate(const CalculationSettings* settings) const;

    /**
     * Returns the time representation of this value.
     */
    QTime asTime() const;

    /**
     * Returns an element in the array value.
     */
    Value element(unsigned column, unsigned row) const;

    /**
     * Returns an array element given by its index denoting its position in the
     * row-wise sorted list of non-empty values.
     * Usable to iterate over the array.
     * \see count()
     */
    Value element(unsigned index) const;

    /**
     * Sets an element in the array value. Do not use if isArray() is false.
     */
    void setElement(unsigned column, unsigned row, const Value& value);

    /**
     * If this value is an array, return the number of columns.
     * Returns 1, if isArray() returns false.
     */
    unsigned columns() const;

    /**
     * If this value is an array, return the number of rows.
     * Returns 1, if isArray() returns false.
     */
    unsigned rows() const;

    /**
     * If this value is an array, return the number of non-empty elements.
     * Returns 1 if isArray() returns false.
     * Usable to iterate over the array.
     * \see element(unsigned)
     */
    unsigned count() const;

    /**
     * Returns error message associated with this value.
     *
     * Call this function only if isError() returns true.
     */
    QString errorMessage() const;

    /**
     * Returns constant reference to empty value.
     */
    static const Value& empty();

    /*
     * Returns a constant reference to a null value.
     *
     * A null value is equal to an empty value (and the other way around) in
     * every way, except for what isNull() returns.
     */
    static const Value& null();

    /**
     * Returns constant reference to '\#CIRCLE!' error.
     *
     * This is used to indicate circular cell references.
     */
    static const Value& errorCIRCLE();

    /**
     * Returns constant reference to '\#DEPEND!' error.
     *
     * This is used to indicate broken cell references.
     */
    static const Value& errorDEPEND();

    /**
     * Returns constant reference to '\#DIV/0!' error.
     *
     * This is used to indicate that a formula divides by 0 (zero).
     */
    static const Value& errorDIV0();

    /**
     * Returns constant reference to '\#N/A' error.
     *
     * This is to indicate that  a value is not available to a function.
     */
    static const Value& errorNA();

    /**
     * Returns constant reference to '\#NAME?' error.
     *
     * This is to indicate that certain text inside formula is not
     * recognized, possibly a misspelled name or name that
     * does not exist.
     */
    static const Value& errorNAME();

    /**
     * Returns constant reference to '\#NUM!' error.
     *
     * This is to indicate a problem with a number in a formula.
     */
    static const Value& errorNUM();

    /**
     * Returns constant reference to '\#NULL!' error.
     *
     * This is to indicate that two area do not intersect.
     */
    static const Value& errorNULL();

    /**
     * Returns constant reference to '\#PARSE!' error.
     *
     * This is used to indicate that a formula could not be parsed correctly.
     */
    static const Value& errorPARSE();

    /**
     * Returns constant reference to '\#REF!' error.
     *
     * This is used to indicate an invalid cell reference.
     */
    static const Value& errorREF();

    /**
     * Returns constant reference to '\#VALUE!' error.
     *
     * This is to indicate that wrong type of argument or operand
     * is used, usually within a function call, e.g SIN("some text").
     */
    static const Value& errorVALUE();

    /**
     * Returns true if it is OK to compare this value with v.
     * If this function returns false, then return value of compare is undefined.
     */
    bool allowComparison(const Value& v) const;

    /**
     * Returns -1, 0, 1, depends whether this value is less than, equal to, or
     * greater than v.
     */
    int compare(const Value& v, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;

    /**
     * Returns true if this value is equal to v.
     */
    bool equal(const Value& v, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;

    /**
     * Returns true if this value is less than v.
     */
    bool less(const Value& v, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;

    /**
     * Returns true if this value is greater than v.
     */
    bool greater(const Value& v, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;

    // comparison operator - returns true only if strictly identical, unlike equal()/compare()
    bool operator==(const Value& v) const;
    inline bool operator!=(const Value& other) const {
        return !operator==(other);
    }

    static int compare(Number v1, Number v2);

    bool isZero() const;

    static bool isZero(Number v);

private:
    class Private;
    QSharedDataPointer<Private> d;
};

/***************************************************************************
  QHash/QSet support
****************************************************************************/

uint qHash(const Value& value);

} // namespace Sheets
} // namespace Calligra

Q_DECLARE_METATYPE(Calligra::Sheets::Value)
Q_DECLARE_TYPEINFO(Calligra::Sheets::Value, Q_MOVABLE_TYPE);


/***************************************************************************
  QTextStream support
****************************************************************************/

CALLIGRA_SHEETS_ODF_EXPORT QTextStream& operator<<(QTextStream& ts, Calligra::Sheets::Value::Type type);
CALLIGRA_SHEETS_ODF_EXPORT QTextStream& operator<<(QTextStream& ts, Calligra::Sheets::Value value);

/***************************************************************************
  QDebug support
****************************************************************************/

CALLIGRA_SHEETS_ODF_EXPORT QDebug operator<<(QDebug str, const Calligra::Sheets::Value& v);
QDebug operator<<(QDebug stream, const Calligra::Sheets::Value::Format& f);

#endif // CALLIGRA_SHEETS_VALUE_H