1/* This file is part of the KDE project
2 Copyright (C) 2003,2004 Ariya Hidayat <ariya@kde.org>
3 Copyright (C) 2005 Tomas Mecir <mecirt@gmail.com>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; only
8 version 2 of the License.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21
22#ifndef CALLIGRA_SHEETS_FUNCTIONS
23#define CALLIGRA_SHEETS_FUNCTIONS
24
25#include <QList>
26#include <QStringList>
27#include <QVector>
28
29#include "Region.h"
30#include "Value.h"
31
32#include "calligra_sheets_export.h"
33
34class QDomElement;
35
36namespace Calligra
37{
38namespace Sheets
39{
40class Sheet;
41class ValueCalc;
42class Function;
43
44typedef QVector<Value> valVector;
45
46struct rangeInfo {
47 int col1, col2, row1, row2;
48 int columns() {
49 return col2 - col1 + 1;
50 }
51 int rows() {
52 return row2 - row1 + 1;
53 }
54};
55struct FuncExtra {
56 // here we'll add all the extras a function may need
57 Function* function;
58 QVector<rangeInfo> ranges;
59 QVector<Region> regions;
60 Sheet *sheet;
61 int myrow, mycol;
62};
63
64typedef Value(*FunctionPtr)(valVector, ValueCalc *, FuncExtra *);
65
66/**
67 * \ingroup Value
68 * A function pointer and context.
69 */
70class CALLIGRA_SHEETS_ODF_EXPORT Function
71{
72public:
73 Function(const QString& name, FunctionPtr ptr);
74 virtual ~Function();
75 /**
76 setParamCount sets allowed parameter count for a function.
77 if max=0, it means max=min. If max=-1, there is no upper limit.
78 */
79 void setParamCount(int min, int max = 0);
80 /** is it okay for the function to receive this many parameters ? */
81 bool paramCountOkay(int count);
82 /** when set to true, the function can receive arrays. When set to
83 false, the auto-array mechamism will be used for arrays (so the
84 function will receive simple values, not arrays). */
85 void setAcceptArray(bool accept = true);
86 bool needsExtra();
87 void setNeedsExtra(bool extra);
88 QString name() const;
89 QString localizedName() const;
90 QString helpText() const;
91 void setHelpText(const QString& text);
92 Value exec(valVector args, ValueCalc *calc, FuncExtra *extra = 0);
93
94 QString alternateName() const;
95 void setAlternateName(const QString &name);
96
97private:
98 Q_DISABLE_COPY(Function)
99
100 class Private;
101 Private * const d;
102};
103
104/**
105 * \ingroup Value
106 * A helper-class to call a function.
107 */
108class CALLIGRA_SHEETS_ODF_EXPORT FunctionCaller {
109public:
110 FunctionPtr m_ptr;
111 valVector m_args;
112 ValueCalc *m_calc;
113 FuncExtra *m_extra;
114
115 FunctionCaller(FunctionPtr ptr, const valVector &args, ValueCalc *calc, FuncExtra *extra = 0);
116 Value exec();
117 Value exec(const valVector &args);
118};
119
120} // namespace Sheets
121} // namespace Calligra
122
123#endif // CALLIGRA_SHEETS_FUNCTIONS
124