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 // Local
0022 #include "FunctionDescription.h"
0023 
0024 #include <QDomElement>
0025 #include <QDomNode>
0026 
0027 #include <KLocalizedString>
0028 
0029 #include "SheetsDebug.h"
0030 
0031 using namespace Calligra::Sheets;
0032 
0033 static ParameterType toType(const QString& type)
0034 {
0035     if (type == "Boolean")
0036         return KSpread_Boolean;
0037     if (type == "Int")
0038         return KSpread_Int;
0039     if (type == "String")
0040         return KSpread_String;
0041     if (type == "Any")
0042         return KSpread_Any;
0043     if (type == "Date")
0044         return KSpread_Date;
0045 
0046     return KSpread_Float;
0047 }
0048 
0049 static QString toString(ParameterType type, bool range = false)
0050 {
0051     if (!range) {
0052         switch (type) {
0053         case KSpread_String:
0054             return i18n("Text");
0055         case KSpread_Int:
0056             return i18n("Whole number (like 1, 132, 2344)");
0057         case KSpread_Boolean:
0058             return i18n("A truth value (TRUE or FALSE)");
0059         case KSpread_Float:
0060             return i18n("A floating point value (like 1.3, 0.343, 253 )");
0061         case KSpread_Any:
0062             return i18n("Any kind of value");
0063         case KSpread_Date:
0064             return i18n("A string representing a date (like \"2/22/2012\")");
0065         }
0066     } else {
0067         switch (type) {
0068         case KSpread_String:
0069             return i18n("A range of strings");
0070         case KSpread_Int:
0071             return i18n("A range of whole numbers (like 1, 132, 2344)");
0072         case KSpread_Boolean:
0073             return i18n("A range of truth values (TRUE or FALSE)");
0074         case KSpread_Float:
0075             return i18n("A range of floating point values (like 1.3, 0.343, 253 )");
0076         case KSpread_Any:
0077             return i18n("A range of any kind of values");
0078         case KSpread_Date:
0079             return i18n("A string representing a range of dates (like \"2/22/2012\"-\"5/22/2012\")");
0080         }
0081     }
0082 
0083     return QString();
0084 }
0085 
0086 FunctionParameter::FunctionParameter()
0087 {
0088     m_type = KSpread_Float;
0089     m_range = false;
0090 }
0091 
0092 FunctionParameter::FunctionParameter(const FunctionParameter& param)
0093 {
0094     m_help = param.m_help;
0095     m_type = param.m_type;
0096     m_range = param.m_range;
0097 }
0098 
0099 FunctionParameter::FunctionParameter(const QDomElement& element)
0100 {
0101     m_type  = KSpread_Float;
0102     m_range = false;
0103 
0104     QDomNode n = element.firstChild();
0105     for (; !n.isNull(); n = n.nextSibling())
0106         if (n.isElement()) {
0107             QDomElement e = n.toElement();
0108             if (e.tagName() == "Comment")
0109                 m_help = i18n(e.text().toUtf8());
0110             else if (e.tagName() == "Type") {
0111                 m_type = toType(e.text());
0112                 if (e.hasAttribute("range")) {
0113                     if (e.attribute("range").toLower() == "true")
0114                         m_range = true;
0115                 }
0116             }
0117         }
0118 }
0119 
0120 FunctionDescription::FunctionDescription()
0121 {
0122     m_type = KSpread_Float;
0123 }
0124 
0125 FunctionDescription::FunctionDescription(const QDomElement& element)
0126 {
0127     QDomNode n = element.firstChild();
0128     for (; !n.isNull(); n = n.nextSibling()) {
0129         if (!n.isElement())
0130             continue;
0131         QDomElement e = n.toElement();
0132         if (e.tagName() == "Name")
0133             m_name = e.text();
0134         else if (e.tagName() == "Type")
0135             m_type = toType(e.text());
0136         else if (e.tagName() == "Parameter")
0137             m_params.append(FunctionParameter(e));
0138         else if (e.tagName() == "Help") {
0139             QDomNode n2 = e.firstChild();
0140             for (; !n2.isNull(); n2 = n2.nextSibling()) {
0141                 if (!n2.isElement())
0142                     continue;
0143                 QDomElement e2 = n2.toElement();
0144                 if (e2.tagName() == "Text")
0145                     m_help.append(i18n(e2.text().toUtf8()));
0146                 else if (e2.tagName() == "Syntax")
0147                     m_syntax.append(i18n(e2.text().toUtf8()));
0148                 else if (e2.tagName() == "Example")
0149                     m_examples.append(i18n(e2.text().toUtf8()));
0150                 else if (e2.tagName() == "Related")
0151                     m_related.append(i18n(e2.text().toUtf8()));
0152             }
0153         }
0154     }
0155 }
0156 
0157 FunctionDescription::FunctionDescription(const FunctionDescription& desc)
0158 {
0159     m_examples = desc.m_examples;
0160     m_related = desc.m_related;
0161     m_syntax = desc.m_syntax;
0162     m_help = desc.m_help;
0163     m_name = desc.m_name;
0164     m_type = desc.m_type;
0165 }
0166 
0167 QString FunctionDescription::toQML() const
0168 {
0169     QString text("<qt><h1>");
0170     text += name();
0171     text += "</h1>";
0172 
0173     if (!m_help.isEmpty()) {
0174         text += "<p>";
0175         QStringList::ConstIterator it = m_help.begin();
0176         for (; it != m_help.end(); ++it) {
0177             text += *it + "<p>";
0178         }
0179         text += "</p>";
0180     }
0181 
0182     text += i18n("<p><b>Return type:</b> %1</p>", toString(type()));
0183 
0184     if (!m_syntax.isEmpty()) {
0185         text += "<h2>" + i18n("Syntax") + "</h2><ul>";
0186         QStringList::ConstIterator it = m_syntax.begin();
0187         for (; it != m_syntax.end(); ++it) {
0188             text += "<li>" + *it + "</li>";
0189         }
0190         text += "</ul>";
0191     }
0192 
0193     if (!m_params.isEmpty()) {
0194         text += "<h2>" + i18n("Parameters") + "</h2><ul>";
0195         QList<FunctionParameter>::ConstIterator it = m_params.begin();
0196         for (; it != m_params.end(); ++it) {
0197             text += i18n("<li><b>Comment:</b> %1", (*it).helpText()) +
0198                     i18n("<br><b>Type:</b> %1", toString((*it).type(), (*it).hasRange()));
0199         }
0200         text += "</ul>";
0201     }
0202 
0203     if (!m_examples.isEmpty()) {
0204         text += "<h2>" + i18n("Examples") + "</h2><ul>";
0205         QStringList::ConstIterator it = m_examples.begin();
0206         for (; it != m_examples.end(); ++it) {
0207             text += "<li>" + *it + "</li>";
0208         }
0209         text += "</ul>";
0210     }
0211 
0212     if (!m_related.isEmpty()) {
0213         text += "<h2>" + i18n("Related Functions") + "</h2><ul>";
0214         QStringList::ConstIterator it = m_related.begin();
0215         for (; it != m_related.end(); ++it) {
0216             text += "<li>"
0217                     "<a href=\"" + *it + "\">" +
0218                     *it +
0219                     "</a>"
0220                     "</li>";
0221         }
0222         text += "</ul>";
0223     }
0224 
0225     text += "</qt>";
0226     return text;
0227 }