kspread

formula.h
1/* This file is part of the KDE project
2 Copyright (C) 2003,2004 Ariya Hidayat <ariya@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#ifndef KSPREAD_FORMULA
21#define KSPREAD_FORMULA
22
23#include <tqstring.h>
24#include <tqvaluevector.h>
25
26class TDELocale;
27
28namespace KSpread
29{
30class Cell;
31class Sheet;
32class Value;
33
37class Token
38{
39 public:
55
80
84 Token( Type type = Unknown, const TQString& text = TQString(), int pos = -1 );
85
86 static const Token null;
87
88 Token( const Token& );
89 Token& operator=( const Token& );
90
94 Type type() const { return m_type; }
95
103 TQString text() const { return m_text; }
104
105 int pos() const { return m_pos; };
106
110 bool isBoolean() const { return m_type == Boolean; }
111
115 bool isInteger() const { return m_type == Integer; }
116
120 bool isFloat() const { return m_type == Float; }
121
125 bool isNumber() const { return (m_type == Integer) || (m_type == Float); }
126
130 bool isString() const { return m_type == String; }
131
135 bool isOperator() const { return m_type == Operator; }
136
140 bool isCell() const { return m_type == Cell; }
141
145 bool isRange() const { return m_type == Range; }
146
150 bool isIdentifier() const { return m_type == Identifier; }
151
156 bool asBoolean() const;
157
162 long asInteger() const;
163
168 double asFloat() const;
169
179 TQString asString() const;
180
185 Op asOperator() const;
186
199 TQString sheetName() const;
200
205 TQString description() const;
206
207 protected:
208
209 Type m_type;
210 TQString m_text;
211 int m_pos;
212
213};
214
219class Tokens: public TQValueVector<Token>
220{
221public:
222 Tokens(): TQValueVector<Token>(), m_valid(true) {};
223 bool valid() const { return m_valid; }
224 void setValid( bool v ){ m_valid = v; }
225protected:
226 bool m_valid;
227};
228
229
239{
240 public:
241
245 Formula (Sheet *sheet, Cell *cell = 0);
246
251 Formula();
252
256 ~Formula();
257
261 Sheet* sheet() const;
265 Cell* cell() const;
266
270 void setExpression( const TQString& expr );
271
275 TQString expression() const;
276
280 void clear();
281
287 bool isValid() const;
288
295 Tokens tokens() const;
296
300 Value eval() const;
301
307 Tokens scan( const TQString& expr, TDELocale* locale = 0 ) const;
308
309 TQString dump() const;
310
311 protected:
312
313 void compile( const Tokens& tokens ) const;
314
318 bool isNamedArea( const TQString& expr ) const;
319
320 private:
321 class Private;
322 Private *d;
323 // no copy or assign
324 Formula( const Formula& );
325 Formula& operator=( const Formula& );
326};
327
331TQTextStream& operator<<( TQTextStream& ts, Formula formula );
332
333
338Token::Op matchOperator( const TQString& text );
339
343bool isIdentifier( TQChar ch );
344
345} // namespace KSpread
346
347
348
349#endif // KSPREAD_FORMULA
350
For every cell in the spread sheet there is a Cell object.
Class Formula encapsulates a formula for a cell.
Definition formula.h:239
Tokens tokens() const
Returns list of tokens associated with this formula.
Definition formula.cpp:456
void setExpression(const TQString &expr)
Sets the expression for this formula.
Definition formula.cpp:407
Formula()
Creates a formula that is not owned by any sheet.
Definition formula.cpp:378
Sheet * sheet() const
Returns the cell which owns this formula.
Definition formula.cpp:398
~Formula()
Destroys the formula.
Definition formula.cpp:388
Cell * cell() const
Returns the cell which owns this formula.
Definition formula.cpp:393
Value eval() const
Evaluates the formula and returns the result.
Definition formula.cpp:1172
void clear()
Clears everything, makes as like a newly constructed formula.
Definition formula.cpp:442
TQString expression() const
Gets the expression of this formula.
Definition formula.cpp:416
bool isValid() const
Returns true if the specified expression is valid, i.e.
Definition formula.cpp:424
Tokens scan(const TQString &expr, TDELocale *locale=0) const
Given an expression, this function separates it into tokens.
Definition formula.cpp:464
bool isNamedArea(const TQString &expr) const
helper function: return true for valid named area
Definition formula.cpp:1146
Formula(Sheet *sheet, Cell *cell=0)
Creates a formula.
Definition formula.cpp:370
TQString description() const
Returns a short description of the token.
Definition formula.cpp:276
long asInteger() const
Returns integer value for an integer token.
Definition formula.cpp:243
bool isOperator() const
Returns true if token is an operator token.
Definition formula.h:135
double asFloat() const
Returns floating-point value for a floating-point token.
Definition formula.cpp:249
TQString sheetName() const
Returns sheet name in a cell reference token.
Definition formula.cpp:267
bool asBoolean() const
Returns boolean value for an boolean token.
Definition formula.cpp:236
bool isRange() const
Returns true if token is a range reference token.
Definition formula.h:145
Op
operator types
Definition formula.h:60
@ GreaterEqual
>=
Definition formula.h:77
@ Slash
/ (division)
Definition formula.h:65
@ InvalidOp
invalid operator
Definition formula.h:61
@ Ampersand
& (string concat)
Definition formula.h:71
@ Semicolon
; (argument separator)
Definition formula.h:70
@ Caret
^ (power)
Definition formula.h:66
bool isString() const
Returns true if token is a string token.
Definition formula.h:130
bool isInteger() const
Returns true if token is a integer token.
Definition formula.h:115
TQString text() const
Returns text associated with the token.
Definition formula.h:103
Type type() const
Returns type of the token.
Definition formula.h:94
bool isIdentifier() const
Returns true if token is an identifier.
Definition formula.h:150
bool isNumber() const
Returns true if token is either integer or floating-point token.
Definition formula.h:125
Token(Type type=Unknown, const TQString &text=TQString(), int pos=-1)
Creates a token.
Definition formula.cpp:212
bool isCell() const
Returns true if token is a cell reference token.
Definition formula.h:140
bool isBoolean() const
Returns true if token is a boolean token.
Definition formula.h:110
TQString asString() const
Returns string value for a string token.
Definition formula.cpp:255
Type
token types
Definition formula.h:44
@ Identifier
function name or named area
Definition formula.h:53
@ Integer
14, 3, 1977
Definition formula.h:47
@ Cell
$A$1, F4, Sheet2!B5, 'Sales Forecast'!Sum
Definition formula.h:51
@ Boolean
True, False (also i18n-ized).
Definition formula.h:46
@ Float
3.141592, 1e10, 5.9e-7
Definition formula.h:48
@ Range
C1:C100.
Definition formula.h:52
@ Operator
+, *, /, -
Definition formula.h:50
@ String
"KOffice", "The quick brown fox..."
Definition formula.h:49
@ Unknown
unknown type
Definition formula.h:45
Op asOperator() const
Returns operator value for an operator token.
Definition formula.cpp:261
bool isFloat() const
Returns true if token is a floating-point token.
Definition formula.h:120
Class Tokens represents array of tokens.
Definition formula.h:220
Provides a wrapper for cell value.
The KSpread namespace.
Definition commands.h:39
bool isIdentifier(TQChar ch)
helper function: return true for valid identifier character
Definition formula.cpp:356
Token::Op matchOperator(const TQString &text)
helper function: return operator of given token text e.g.
Definition formula.cpp:130
TQTextStream & operator<<(TQTextStream &ts, Formula formula)
Dumps the formula, should be used only to assist debugging.