File indexing completed on 2024-04-28 11:20:39
0001 /* 0002 SPDX-FileCopyrightText: 2009 Milian Wolff <mail@milianw.de> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "qalculatesyntaxhelpobject.h" 0008 #include "settings.h" 0009 #include "qalculatesession.h" 0010 0011 #include <KLocalizedString> 0012 0013 #include <libqalculate/Calculator.h> 0014 #include <libqalculate/ExpressionItem.h> 0015 #include <libqalculate/Unit.h> 0016 #include <libqalculate/Prefix.h> 0017 #include <libqalculate/Variable.h> 0018 #include <libqalculate/Function.h> 0019 0020 QalculateSyntaxHelpObject::QalculateSyntaxHelpObject(const QString& command, QalculateSession* session) 0021 : SyntaxHelpObject(command, session), m_answer(QString()) 0022 { 0023 } 0024 0025 void QalculateSyntaxHelpObject::fetchInformation() 0026 { 0027 std::string cmd = command().remove(QLatin1String("help")).simplified().toLatin1().data(); 0028 qDebug() << "HELP CALLED FOR:" << QLatin1String(cmd.c_str()); 0029 0030 if (cmd == "plot") { 0031 setPlotInformation(); 0032 return; 0033 } 0034 if (cmd == "saveVariables") { 0035 setSaveVariablesInformation(); 0036 return; 0037 } 0038 if (cmd == "loadVariables") { 0039 setLoadVariablesInformation(); 0040 return; 0041 } 0042 0043 ExpressionItem *item = CALCULATOR->getActiveExpressionItem(cmd); 0044 0045 if (!item) { 0046 m_answer = i18n("No function, variable or unit with specified name exist."); 0047 return; 0048 } 0049 0050 switch(item->type()) { 0051 case TYPE_FUNCTION: 0052 MathFunction *f = (MathFunction*) item; 0053 QString title = i18n("Function: %1", QLatin1String(item->title().c_str())); 0054 const ExpressionName *ename = &f->preferredName(false); 0055 int iargs = f->maxargs(); 0056 if(iargs < 0) { 0057 iargs = f->minargs() + 1; 0058 } 0059 QString str,str2,syntax; 0060 str += QLatin1String(ename->name.c_str()); 0061 str += QLatin1String("("); 0062 if(iargs != 0) { 0063 Argument *arg; 0064 Argument default_arg; 0065 for(int i2 = 1; i2 <= iargs; i2++) { 0066 if(i2 > f->minargs()) { 0067 str += QLatin1String("["); 0068 } 0069 if(i2 > 1) { 0070 str += QLatin1String(CALCULATOR->getComma().c_str()); 0071 str += QLatin1String(" "); 0072 } 0073 arg = f->getArgumentDefinition(i2); 0074 if(arg && !arg->name().empty()) { 0075 str2 = QLatin1String(arg->name().c_str()); 0076 } else { 0077 str2 = QLatin1String("argument"); 0078 str2 += QLatin1String(" "); 0079 str2 += QString::number(i2); 0080 } 0081 str += str2; 0082 if(i2 > f->minargs()) { 0083 str += QLatin1String("]"); 0084 } 0085 } 0086 if(f->maxargs() < 0) { 0087 str += QLatin1String(CALCULATOR->getComma().c_str()); 0088 str += QLatin1String(" ..."); 0089 } 0090 } 0091 str += QLatin1String(")"); 0092 syntax = QString::fromLatin1("<p>%1</p>").arg(str); 0093 0094 QString arguments = QLatin1String(""); 0095 if(iargs != 0) { 0096 Argument *arg; 0097 Argument default_arg; 0098 for(int i2 = 1; i2 <= iargs; i2++) { 0099 arg = f->getArgumentDefinition(i2); 0100 if(arg && !arg->name().empty()) { 0101 str = QLatin1String(arg->name().c_str()); 0102 } else { 0103 str = QString::number(i2); 0104 } 0105 str += QLatin1String(": "); 0106 if(arg) { 0107 str2 = QLatin1String(arg->printlong().c_str()); 0108 } else { 0109 str2 = QLatin1String(default_arg.printlong().c_str()); 0110 } 0111 if(i2 > f->minargs()) { 0112 str2 += QLatin1String(" ("); 0113 //optional argument, in description 0114 str2 += QLatin1String("optional"); 0115 if(!f->getDefaultValue(i2).empty()) { 0116 str2 += QLatin1String(", "); 0117 //argument default, in description 0118 str2 += QLatin1String("default: "); 0119 str2 += QLatin1String(f->getDefaultValue(i2).c_str()); 0120 } 0121 str2 += QLatin1String(")"); 0122 } 0123 str += str2; 0124 arguments += QString::fromLatin1("<p>%1</p>").arg(str); 0125 } 0126 } 0127 0128 QString desc = QString::fromLatin1("<p>%1</p>").arg(QLatin1String(item->description().c_str())); 0129 0130 m_answer = title + desc + syntax + arguments; 0131 setHtml(QLatin1String("<p style='white-space:pre'>") + syntax + QLatin1String("</p>")); 0132 emit done(); 0133 } 0134 } 0135 0136 void QalculateSyntaxHelpObject::setPlotInformation() 0137 { 0138 QString title = QLatin1String("<p>") + i18n("Plotting interface") + QLatin1String("</p>"); 0139 QString desc = QLatin1String("<p>") + i18n("Plots one or more functions either inline or in a separate window.") + QLatin1String("</p>"); 0140 QString expression = i18n("expression"); 0141 QString option = i18n("option"); 0142 QString value = i18n("value"); 0143 QString syntax = QLatin1String("<p>plot %1 [%2=%3 ...] [, %4 [%5=%6 ...]] ...</p>"); 0144 syntax = syntax.arg(expression, option, value, expression, option, value); 0145 0146 QString integer = i18n("integer"); 0147 QString boolean = i18n("boolean"); 0148 QString number = i18n("number"); 0149 QString defaultValue = i18n("default: %1"); 0150 QString noDefault = QLatin1String(""); 0151 QString optionFormat2 = QLatin1String("<p>%1: %2</p>"); 0152 QString optionFormat3 = QLatin1String("<p>%1: %2 (%3)</p>"); 0153 QString optionFormat4 = QLatin1String("<p>%1: %2 (%3, %4)</p>"); 0154 0155 QStringList boolList; 0156 boolList << QLatin1String("false") << QLatin1String("true"); 0157 0158 QString legendDefault; 0159 QString styleDefault; 0160 QString smoothingDefault; 0161 switch (QalculateSettings::plotLegend()) { 0162 case QalculateSettings::LEGEND_NONE: 0163 legendDefault = QLatin1String("none"); break; 0164 case QalculateSettings::LEGEND_TOP_LEFT: 0165 legendDefault = QLatin1String("top_left"); break; 0166 case QalculateSettings::LEGEND_TOP_RIGHT: 0167 legendDefault = QLatin1String("top_right"); break; 0168 case QalculateSettings::LEGEND_BOTTOM_LEFT: 0169 legendDefault = QLatin1String("bottom_left"); break; 0170 case QalculateSettings::LEGEND_BOTTOM_RIGHT: 0171 legendDefault = QLatin1String("bottom_right"); break; 0172 case QalculateSettings::LEGEND_BELOW: 0173 legendDefault = QLatin1String("below"); break; 0174 case QalculateSettings::LEGEND_OUTSIDE: 0175 legendDefault = QLatin1String("outside"); break; 0176 } 0177 switch(QalculateSettings::plotSmoothing()) { 0178 case QalculateSettings::SMOOTHING_NONE: 0179 smoothingDefault = QLatin1String("none"); break; 0180 case QalculateSettings::SMOOTHING_UNIQUE: 0181 smoothingDefault = QLatin1String("monotonic"); break; 0182 case QalculateSettings::SMOOTHING_CSPLINES: 0183 smoothingDefault = QLatin1String("csplines"); break; 0184 case QalculateSettings::SMOOTHING_BEZIER: 0185 smoothingDefault = QLatin1String("bezier"); break; 0186 case QalculateSettings::SMOOTHING_SBEZIER: 0187 smoothingDefault = QLatin1String("sbezier"); break; 0188 } 0189 switch(QalculateSettings::plotStyle()) { 0190 case QalculateSettings::STYLE_LINES: 0191 styleDefault = QLatin1String("lines"); break; 0192 case QalculateSettings::STYLE_POINTS: 0193 styleDefault = QLatin1String("points"); break; 0194 case QalculateSettings::STYLE_LINES_POINTS: 0195 styleDefault = QLatin1String("points_lines"); break; 0196 case QalculateSettings::STYLE_BOXES: 0197 styleDefault = QLatin1String("boxes"); break; 0198 case QalculateSettings::STYLE_HISTOGRAM: 0199 styleDefault = QLatin1String("histogram"); break; 0200 case QalculateSettings::STYLE_STEPS: 0201 styleDefault = QLatin1String("steps"); break; 0202 case QalculateSettings::STYLE_CANDLESTICKS: 0203 styleDefault = QLatin1String("candlesticks"); break; 0204 case QalculateSettings::STYLE_DOTS: 0205 styleDefault = QLatin1String("dots"); break; 0206 } 0207 0208 QString arguments = QLatin1String(""); 0209 arguments += optionFormat3.arg(QLatin1String("title"), i18n("The function's name"), 0210 defaultValue.arg(QLatin1String("expression"))); 0211 arguments += optionFormat2.arg(QLatin1String("plottitle"), i18n("Title label")); 0212 arguments += optionFormat2.arg(QLatin1String("xlabel"), i18n("x-axis label")); 0213 arguments += optionFormat2.arg(QLatin1String("ylabel"), i18n("y-axis label")); 0214 arguments += optionFormat2.arg(QLatin1String("filename"), i18n("Image to save plot to. If empty shows plot in a window on the screen. If inline=true the image is shown regardless of this option.")); 0215 arguments += optionFormat3.arg(QLatin1String("filetype"), i18n("The image type to save as. One of auto, png, ps, eps, latex, svg, fig."), defaultValue.arg(QLatin1String("auto"))); 0216 arguments += optionFormat4.arg(QLatin1String("color"), i18n("Set to true for colored plot, false for monochrome."), boolean, defaultValue.arg(boolList[QalculateSettings::coloredPlot()])); 0217 arguments += optionFormat3.arg(QLatin1String("xmin"), i18n("Minimum x-axis value."), number); 0218 arguments += optionFormat3.arg(QLatin1String("xmax"), i18n("Maximum x-axis value."), number); 0219 arguments += optionFormat4.arg(QLatin1String("xlog"), i18n("If a logarithmic scale shall be used for the x-axis."), boolean, defaultValue.arg(QLatin1String("false"))); 0220 arguments += optionFormat4.arg(QLatin1String("ylog"), i18n("If a logarithmic scale shall be used for the y-axis."), boolean, defaultValue.arg(QLatin1String("false"))); 0221 arguments += optionFormat4.arg(QLatin1String("xlogbase"), i18n("Logarithmic base for the x-axis."), number, defaultValue.arg(QLatin1String("10"))); 0222 arguments += optionFormat4.arg(QLatin1String("ylogbase"), i18n("Logarithmic base for the y-axis."), boolean, defaultValue.arg(QLatin1String("10"))); 0223 arguments += optionFormat4.arg(QLatin1String("grid"), i18n("If a grid shall be shown in the plot."), boolean, defaultValue.arg(boolList[QalculateSettings::plotGrid()])); 0224 arguments += optionFormat4.arg(QLatin1String("border"), i18n("If the plot shall be surrounded by borders on all sides (not just axis)."), boolean, defaultValue.arg(boolList[QalculateSettings::plotBorder()])); 0225 arguments += optionFormat4.arg(QLatin1String("linewidth"), i18n("Width of lines."), integer, defaultValue.arg(QString::number(QalculateSettings::plotLineWidth()))); 0226 arguments += optionFormat3.arg(QLatin1String("legend"), i18n("Where the plot legend shall be placed. One of none, top_left, top_right, bottom_left, bottom_right, below, outside"), defaultValue.arg(legendDefault)); 0227 arguments += optionFormat3.arg(QLatin1String("smoothing"), i18n("Plot smoothing. One of none, unique, csplines, bezier, sbezier"), defaultValue.arg(smoothingDefault)); 0228 arguments += optionFormat3.arg(QLatin1String("style"), i18n("Plot style. One of lines, points, points_lines, boxes, histogram, steps, candlesticks, dots"), defaultValue.arg(styleDefault)); 0229 arguments += optionFormat4.arg(QLatin1String("xaxis2"), i18n("Use scale on second x-axis."), boolean, defaultValue.arg(QLatin1String("false"))); 0230 arguments += optionFormat4.arg(QLatin1String("yaxis2"), i18n("Use scale on second y-axis."), boolean, defaultValue.arg(QLatin1String("false"))); 0231 arguments += optionFormat4.arg(QLatin1String("inline"), i18n("If the plot is to be drawn inline, instead of in a new window."), boolean, defaultValue.arg(boolList[QalculateSettings::inlinePlot()])); 0232 arguments += optionFormat3.arg(QLatin1String("step"), i18n("Distance between two interpolation points. See also steps."), number); 0233 arguments += optionFormat4.arg(QLatin1String("steps"), i18n("Number of interpolation points. See also step."), integer, defaultValue.arg(QString::number(QalculateSettings::plotSteps()))); 0234 arguments += optionFormat3.arg(QLatin1String("xvar"), i18n("The name of the x variable. This must be an unknown variable"), defaultValue.arg(QLatin1String("x"))); 0235 0236 m_answer = title + desc + syntax + arguments; 0237 0238 0239 } 0240 0241 void QalculateSyntaxHelpObject::setSaveVariablesInformation() 0242 { 0243 QString title = QLatin1String("<p>") + i18n("Save variables to a file") + QLatin1String("</p>"); 0244 QString desc = QLatin1String("<p>") + i18n("Save all currently defined variables to a file. They can be reloaded with %1.", QLatin1String("loadVariables")) + QLatin1String("</p>"); 0245 QString syntax = QLatin1String("<p>saveVariables ") + i18n("file") + QLatin1String("</p>"); 0246 QString arguments = QLatin1String("<p>") + i18n("file: the file to save to") + QLatin1String("</p>"); 0247 m_answer = title + desc + syntax + arguments; 0248 } 0249 0250 void QalculateSyntaxHelpObject::setLoadVariablesInformation() 0251 { 0252 QString title = QLatin1String("<p>") + i18n("Load variables from a file") + QLatin1String("</p>"); 0253 QString desc = QLatin1String("<p>") + i18n("Load variables from a file that has previously been created by %1.", QLatin1String("saveVariables")) + QLatin1String("</p>"); 0254 QString syntax = QLatin1String("<p>loadVariables ") + i18n("file") + QLatin1String("</p>"); 0255 QString arguments = QLatin1String("<p>") + i18n("file: the file to load") + QLatin1String("</p>"); 0256 m_answer = title + desc + syntax + arguments; 0257 } 0258 0259 0260 QString QalculateSyntaxHelpObject::answer() 0261 { 0262 fetchInformation(); 0263 return m_answer; 0264 } 0265