KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
4.5KB

  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. 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. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. * Boston, MA 02110-1301, USA.
  16. */
  17. #ifndef KSPREAD_FUNCTIONS
  18. #define KSPREAD_FUNCTIONS
  19. #include <tqstringlist.h>
  20. #include <tqvaluevector.h>
  21. class TQDomElement;
  22. namespace KSpread
  23. {
  24. class Sheet;
  25. class Value;
  26. class ValueCalc;
  27. typedef TQValueVector<Value> valVector;
  28. struct rangeInfo {
  29. int col1, col2, row1, row2;
  30. };
  31. struct FuncExtra {
  32. // here we'll add all the extras a function may need
  33. TQValueVector<rangeInfo> ranges;
  34. Sheet *sheet;
  35. int myrow, mycol;
  36. };
  37. typedef Value (*FunctionPtr)(valVector, ValueCalc *, FuncExtra *);
  38. class Function
  39. {
  40. public:
  41. Function (const TQString& name, FunctionPtr ptr);
  42. ~Function();
  43. /**
  44. setParamCount sets allowed parameter count for a function.
  45. if max=0, it means max=min. If max=-1, there is no upper limit.
  46. */
  47. void setParamCount (int min, int max = 0);
  48. /** is it okay for the function to receive this many parameters ? */
  49. bool paramCountOkay (int count);
  50. /** when set to true, the function can receive arrays. When set to
  51. false, the auto-array mechamism will be used for arrays (so the
  52. fuction will receive simple values, not arrays). */
  53. void setAcceptArray (bool accept = true);
  54. bool needsExtra ();
  55. void setNeedsExtra (bool extra);
  56. TQString name() const;
  57. TQString localizedName() const;
  58. TQString helpText() const;
  59. void setHelpText( const TQString& text );
  60. Value exec (valVector args, ValueCalc *calc, FuncExtra *extra = 0);
  61. private:
  62. class Private;
  63. Private* d;
  64. };
  65. enum ParameterType { KSpread_Int, KSpread_Float, KSpread_String, KSpread_Boolean, KSpread_Any };
  66. class FunctionParameter
  67. {
  68. public:
  69. FunctionParameter();
  70. FunctionParameter( const FunctionParameter& param );
  71. FunctionParameter( const TQDomElement& element );
  72. TQString helpText() const { return m_help; }
  73. ParameterType type() const { return m_type; }
  74. bool hasRange() const { return m_range; }
  75. private:
  76. TQString m_help;
  77. ParameterType m_type;
  78. bool m_range;
  79. };
  80. class FunctionDescription
  81. {
  82. public:
  83. FunctionDescription();
  84. FunctionDescription (const TQDomElement& element);
  85. FunctionDescription (const FunctionDescription& desc);
  86. const TQStringList& examples() { return m_examples; }
  87. const TQStringList& syntax() { return m_syntax; }
  88. const TQStringList& related() { return m_related; }
  89. const TQStringList& helpText() const { return m_help; }
  90. TQString name() const { return m_name; }
  91. ParameterType type() const { return m_type; }
  92. int params() const { return m_params.count(); }
  93. FunctionParameter& param( int i ) { return m_params[ i ]; }
  94. void setGroup( const TQString& g ) { m_group = g; }
  95. TQString group() const { return m_group; }
  96. TQString toTQML() const;
  97. private:
  98. TQString m_group;
  99. TQStringList m_examples;
  100. TQStringList m_syntax;
  101. TQStringList m_related;
  102. TQStringList m_help;
  103. TQString m_name;
  104. ParameterType m_type;
  105. TQValueList<FunctionParameter> m_params;
  106. };
  107. class FunctionRepository
  108. {
  109. public:
  110. FunctionRepository();
  111. ~FunctionRepository();
  112. static FunctionRepository *self();
  113. void add (Function *function);
  114. Function *function (const TQString& name);
  115. FunctionDescription *functionInfo (const TQString& name);
  116. /** return functions within a group, or all if no group given */
  117. TQStringList functionNames (const TQString& group = TQString());
  118. const TQStringList &groups () const { return m_groups; }
  119. private:
  120. class Private;
  121. Private* d;
  122. static FunctionRepository* s_self;
  123. /** loads function descriptions from a XML file */
  124. void loadFile (const TQString& filename);
  125. TQStringList m_groups;
  126. // no copy or assign
  127. FunctionRepository( const FunctionRepository& );
  128. FunctionRepository& operator=( const FunctionRepository& );
  129. };
  130. } // namespace KSpread
  131. #endif // KSPREAD_FUNCTIONS