kspread

formula.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003,2004 Ariya Hidayat <ariya@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #ifndef KSPREAD_FORMULA
00021 #define KSPREAD_FORMULA
00022 
00023 #include <qstring.h>
00024 #include <qvaluevector.h>
00025 
00026 class KLocale;
00027 
00028 namespace KSpread
00029 {
00030 class Cell;
00031 class Sheet;
00032 class Value;
00033 
00037 class Token
00038 {
00039   public:
00043     enum Type
00044     {
00045       Unknown = 0, 
00046       Boolean,     
00047       Integer,     
00048       Float,       
00049       String,      
00050       Operator,    
00051       Cell,        
00052       Range,       
00053       Identifier   
00054     };
00055 
00059     enum Op
00060     {
00061       InvalidOp = 0,  
00062       Plus,           
00063       Minus,          
00064       Asterisk,       
00065       Slash,          
00066       Caret,          
00067       LeftPar,        
00068       RightPar,       
00069       Comma,          
00070       Semicolon,      
00071       Ampersand,      
00072       Equal,          
00073       NotEqual,       
00074       Less,           
00075       Greater,        
00076       LessEqual,      
00077       GreaterEqual,   
00078       Percent         
00079     };
00080 
00084     Token( Type type = Unknown, const QString& text = QString::null, int pos = -1 );
00085 
00086     static const Token null;
00087 
00088     Token( const Token& );
00089     Token& operator=( const Token& );
00090 
00094     Type type() const { return m_type; }
00095 
00103     QString text() const { return m_text; }
00104 
00105     int pos() const { return m_pos; };
00106 
00110     bool isBoolean() const { return m_type == Boolean; }
00111 
00115     bool isInteger() const { return m_type == Integer; }
00116 
00120     bool isFloat() const { return m_type == Float; }
00121 
00125     bool isNumber() const { return (m_type == Integer) || (m_type == Float); }
00126 
00130     bool isString() const { return m_type == String; }
00131 
00135     bool isOperator() const { return m_type == Operator; }
00136 
00140     bool isCell() const { return m_type == Cell; }
00141 
00145     bool isRange() const { return m_type == Range; }
00146 
00150     bool isIdentifier() const { return m_type == Identifier; }
00151 
00156     bool asBoolean() const;
00157 
00162     int asInteger() const;
00163 
00168     double asFloat() const;
00169 
00179     QString asString() const;
00180 
00185     Op asOperator() const;
00186 
00199     QString sheetName() const;
00200 
00205     QString description() const;
00206 
00207   protected:
00208 
00209     Type m_type;
00210     QString m_text;
00211     int m_pos;
00212 
00213 };
00214 
00219 class Tokens: public QValueVector<Token>
00220 {
00221 public:
00222   Tokens(): QValueVector<Token>(), m_valid(true) {};
00223   bool valid() const { return m_valid; }
00224   void setValid( bool v ){ m_valid = v; }
00225 protected:
00226   bool m_valid;
00227 };
00228 
00229 
00238 class Formula
00239 {
00240   public:
00241 
00245     Formula (Sheet *sheet,  Cell *cell = 0);
00246 
00251     Formula();
00252 
00256     ~Formula();
00257 
00261     Sheet* sheet() const;
00265     Cell* cell() const;
00266 
00270     void setExpression( const QString& expr );
00271 
00275     QString expression() const;
00276 
00280     void clear();
00281 
00287     bool isValid() const;
00288 
00295     Tokens tokens() const;
00296 
00300     Value eval() const;
00301 
00307     Tokens scan( const QString& expr, KLocale* locale = 0 ) const;
00308 
00309     QString dump() const;
00310 
00311   protected:
00312 
00313     void compile( const Tokens& tokens ) const;
00314 
00315   private:
00316     class Private;
00317     Private *d;
00318     // no copy or assign
00319     Formula( const Formula& );
00320     Formula& operator=( const Formula& );
00321 };
00322 
00326 QTextStream& operator<<( QTextStream& ts, Formula formula );
00327 
00328 
00333 Token::Op matchOperator( const QString& text );
00334 
00338 bool isIdentifier( QChar ch );
00339 
00340 } // namespace KSpread
00341 
00342 
00343 
00344 #endif // KSPREAD_FORMULA
00345 
KDE Home | KDE Accessibility Home | Description of Access Keys