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

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003,2004 Ariya Hidayat <ariya@kde.org>
0003    Copyright (C) 2005 Tomas Mecir <mecirt@gmail.com>
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 
0022 #ifndef CALLIGRA_SHEETS_FUNCTIONS
0023 #define CALLIGRA_SHEETS_FUNCTIONS
0024 
0025 #include <QList>
0026 #include <QStringList>
0027 #include <QVector>
0028 
0029 #include "Region.h"
0030 #include "Value.h"
0031 
0032 #include "sheets_odf_export.h"
0033 
0034 
0035 namespace Calligra
0036 {
0037 namespace Sheets
0038 {
0039 class Sheet;
0040 class ValueCalc;
0041 class Function;
0042 
0043 typedef QVector<Value> valVector;
0044 
0045 struct rangeInfo {
0046     int col1, col2, row1, row2;
0047     int columns() {
0048         return col2 - col1 + 1;
0049     }
0050     int rows() {
0051         return row2 - row1 + 1;
0052     }
0053 };
0054 struct FuncExtra {
0055     // here we'll add all the extras a function may need
0056     Function* function;
0057     QVector<rangeInfo> ranges;
0058     QVector<Region> regions;
0059     Sheet *sheet;
0060     int myrow, mycol;
0061 };
0062 
0063 typedef Value(*FunctionPtr)(valVector, ValueCalc *, FuncExtra *);
0064 
0065 /**
0066  * \ingroup Value
0067  * A function pointer and context.
0068  */
0069 class CALLIGRA_SHEETS_ODF_EXPORT Function
0070 {
0071 public:
0072     Function(const QString& name, FunctionPtr ptr);
0073     virtual ~Function();
0074     /**
0075     setParamCount sets allowed parameter count for a function.
0076     if max=0, it means max=min. If max=-1, there is no upper limit.
0077     */
0078     void setParamCount(int min, int max = 0);
0079     /** is it okay for the function to receive this many parameters ? */
0080     bool paramCountOkay(int count);
0081     /** when set to true, the function can receive arrays. When set to
0082     false, the auto-array mechanism will be used for arrays (so the
0083     function will receive simple values, not arrays). */
0084     void setAcceptArray(bool accept = true);
0085     bool needsExtra();
0086     void setNeedsExtra(bool extra);
0087     QString name() const;
0088     QString localizedName() const;
0089     QString helpText() const;
0090     void setHelpText(const QString& text);
0091     Value exec(valVector args, ValueCalc *calc, FuncExtra *extra = 0);
0092 
0093     QString alternateName() const;
0094     void setAlternateName(const QString &name);
0095 
0096 private:
0097     Q_DISABLE_COPY(Function)
0098 
0099     class Private;
0100     Private * const d;
0101 };
0102 
0103 /**
0104  * \ingroup Value
0105  * A helper-class to call a function.
0106  */
0107 class CALLIGRA_SHEETS_ODF_EXPORT FunctionCaller {
0108 public:
0109     FunctionPtr m_ptr;
0110     valVector m_args;
0111     ValueCalc *m_calc;
0112     FuncExtra *m_extra;
0113 
0114     FunctionCaller(FunctionPtr ptr, const valVector &args, ValueCalc *calc, FuncExtra *extra = 0);
0115     Value exec();
0116     Value exec(const valVector &args);
0117 };
0118 
0119 } // namespace Sheets
0120 } // namespace Calligra
0121 
0122 #endif // CALLIGRA_SHEETS_FUNCTIONS