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 | // Local |
22 | #include "FunctionDescription.h" |
23 | |
24 | #include <QDomElement> |
25 | #include <QDomNode> |
26 | |
27 | #include <kdebug.h> |
28 | #include <klocale.h> |
29 | |
30 | using namespace Calligra::Sheets; |
31 | |
32 | static ParameterType toType(const QString& type) |
33 | { |
34 | if (type == "Boolean" ) |
35 | return KSpread_Boolean; |
36 | if (type == "Int" ) |
37 | return KSpread_Int; |
38 | if (type == "String" ) |
39 | return KSpread_String; |
40 | if (type == "Any" ) |
41 | return KSpread_Any; |
42 | if (type == "Date" ) |
43 | return KSpread_Date; |
44 | |
45 | return KSpread_Float; |
46 | } |
47 | |
48 | static QString toString(ParameterType type, bool range = false) |
49 | { |
50 | if (!range) { |
51 | switch (type) { |
52 | case KSpread_String: |
53 | return i18n("Text" ); |
54 | case KSpread_Int: |
55 | return i18n("Whole number (like 1, 132, 2344)" ); |
56 | case KSpread_Boolean: |
57 | return i18n("A truth value (TRUE or FALSE)" ); |
58 | case KSpread_Float: |
59 | return i18n("A floating point value (like 1.3, 0.343, 253 )" ); |
60 | case KSpread_Any: |
61 | return i18n("Any kind of value" ); |
62 | case KSpread_Date: |
63 | return i18n("A string representing a date (like \"2/22/2012\")" ); |
64 | } |
65 | } else { |
66 | switch (type) { |
67 | case KSpread_String: |
68 | return i18n("A range of strings" ); |
69 | case KSpread_Int: |
70 | return i18n("A range of whole numbers (like 1, 132, 2344)" ); |
71 | case KSpread_Boolean: |
72 | return i18n("A range of truth values (TRUE or FALSE)" ); |
73 | case KSpread_Float: |
74 | return i18n("A range of floating point values (like 1.3, 0.343, 253 )" ); |
75 | case KSpread_Any: |
76 | return i18n("A range of any kind of values" ); |
77 | case KSpread_Date: |
78 | return i18n("A string representing a range of dates (like \"2/22/2012\"-\"5/22/2012\")" ); |
79 | } |
80 | } |
81 | |
82 | return QString(); |
83 | } |
84 | |
85 | FunctionParameter::FunctionParameter() |
86 | { |
87 | m_type = KSpread_Float; |
88 | m_range = false; |
89 | } |
90 | |
91 | FunctionParameter::FunctionParameter(const FunctionParameter& param) |
92 | { |
93 | m_help = param.m_help; |
94 | m_type = param.m_type; |
95 | m_range = param.m_range; |
96 | } |
97 | |
98 | FunctionParameter::FunctionParameter(const QDomElement& element) |
99 | { |
100 | m_type = KSpread_Float; |
101 | m_range = false; |
102 | |
103 | QDomNode n = element.firstChild(); |
104 | for (; !n.isNull(); n = n.nextSibling()) |
105 | if (n.isElement()) { |
106 | QDomElement e = n.toElement(); |
107 | if (e.tagName() == "Comment" ) |
108 | m_help = i18n(e.text().toUtf8()); |
109 | else if (e.tagName() == "Type" ) { |
110 | m_type = toType(e.text()); |
111 | if (e.hasAttribute("range" )) { |
112 | if (e.attribute("range" ).toLower() == "true" ) |
113 | m_range = true; |
114 | } |
115 | } |
116 | } |
117 | } |
118 | |
119 | FunctionDescription::FunctionDescription() |
120 | { |
121 | m_type = KSpread_Float; |
122 | } |
123 | |
124 | FunctionDescription::FunctionDescription(const QDomElement& element) |
125 | { |
126 | QDomNode n = element.firstChild(); |
127 | for (; !n.isNull(); n = n.nextSibling()) { |
128 | if (!n.isElement()) |
129 | continue; |
130 | QDomElement e = n.toElement(); |
131 | if (e.tagName() == "Name" ) |
132 | m_name = e.text(); |
133 | else if (e.tagName() == "Type" ) |
134 | m_type = toType(e.text()); |
135 | else if (e.tagName() == "Parameter" ) |
136 | m_params.append(FunctionParameter(e)); |
137 | else if (e.tagName() == "Help" ) { |
138 | QDomNode n2 = e.firstChild(); |
139 | for (; !n2.isNull(); n2 = n2.nextSibling()) { |
140 | if (!n2.isElement()) |
141 | continue; |
142 | QDomElement e2 = n2.toElement(); |
143 | if (e2.tagName() == "Text" ) |
144 | m_help.append(i18n(e2.text().toUtf8())); |
145 | else if (e2.tagName() == "Syntax" ) |
146 | m_syntax.append(i18n(e2.text().toUtf8())); |
147 | else if (e2.tagName() == "Example" ) |
148 | m_examples.append(i18n(e2.text().toUtf8())); |
149 | else if (e2.tagName() == "Related" ) |
150 | m_related.append(i18n(e2.text().toUtf8())); |
151 | } |
152 | } |
153 | } |
154 | } |
155 | |
156 | FunctionDescription::FunctionDescription(const FunctionDescription& desc) |
157 | { |
158 | m_examples = desc.m_examples; |
159 | m_related = desc.m_related; |
160 | m_syntax = desc.m_syntax; |
161 | m_help = desc.m_help; |
162 | m_name = desc.m_name; |
163 | m_type = desc.m_type; |
164 | } |
165 | |
166 | QString FunctionDescription::toQML() const |
167 | { |
168 | QString text("<qt><h1>" ); |
169 | text += name(); |
170 | text += "</h1>" ; |
171 | |
172 | if (!m_help.isEmpty()) { |
173 | text += "<p>" ; |
174 | QStringList::ConstIterator it = m_help.begin(); |
175 | for (; it != m_help.end(); ++it) { |
176 | text += *it + "<p>" ; |
177 | } |
178 | text += "</p>" ; |
179 | } |
180 | |
181 | text += i18n("<p><b>Return type:</b> %1</p>" , toString(type())); |
182 | |
183 | if (!m_syntax.isEmpty()) { |
184 | text += "<h2>" + i18n("Syntax" ) + "</h2><ul>" ; |
185 | QStringList::ConstIterator it = m_syntax.begin(); |
186 | for (; it != m_syntax.end(); ++it) { |
187 | text += "<li>" + *it + "</li>" ; |
188 | } |
189 | text += "</ul>" ; |
190 | } |
191 | |
192 | if (!m_params.isEmpty()) { |
193 | text += "<h2>" + i18n("Parameters" ) + "</h2><ul>" ; |
194 | QList<FunctionParameter>::ConstIterator it = m_params.begin(); |
195 | for (; it != m_params.end(); ++it) { |
196 | text += i18n("<li><b>Comment:</b> %1" , (*it).helpText()) + |
197 | i18n("<br><b>Type:</b> %1" , toString((*it).type(), (*it).hasRange())); |
198 | } |
199 | text += "</ul>" ; |
200 | } |
201 | |
202 | if (!m_examples.isEmpty()) { |
203 | text += "<h2>" + i18n("Examples" ) + "</h2><ul>" ; |
204 | QStringList::ConstIterator it = m_examples.begin(); |
205 | for (; it != m_examples.end(); ++it) { |
206 | text += "<li>" + *it + "</li>" ; |
207 | } |
208 | text += "</ul>" ; |
209 | } |
210 | |
211 | if (!m_related.isEmpty()) { |
212 | text += "<h2>" + i18n("Related Functions" ) + "</h2><ul>" ; |
213 | QStringList::ConstIterator it = m_related.begin(); |
214 | for (; it != m_related.end(); ++it) { |
215 | text += "<li>" |
216 | "<a href=\"" + *it + "\">" + |
217 | *it + |
218 | "</a>" |
219 | "</li>" ; |
220 | } |
221 | text += "</ul>" ; |
222 | } |
223 | |
224 | text += "</qt>" ; |
225 | return text; |
226 | } |
227 | |