File indexing completed on 2024-10-06 12:07:41

0001 /*************************************************************************************
0002 *  Copyright (C) 2012 by Aleix Pol <aleixpol@kde.org>                               *
0003 *                                                                                   *
0004 *  This program is free software; you can redistribute it and/or                    *
0005 *  modify it under the terms of the GNU General Public License                      *
0006 *  as published by the Free Software Foundation; either version 2                   *
0007 *  of the License, or (at your option) any later version.                           *
0008 *                                                                                   *
0009 *  This program is distributed in the hope that it will be useful,                  *
0010 *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012 *  GNU General Public License for more details.                                     *
0013 *                                                                                   *
0014 *  You should have received a copy of the GNU General Public License                *
0015 *  along with this program; if not, write to the Free Software                      *
0016 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017 *************************************************************************************/
0018 
0019 #include "plotsfactory.h"
0020 #include <analitzaplot/functiongraph.h>
0021 #include "private/functiongraphfactory.h"
0022 #include <analitza/analyzer.h>
0023 #include <analitza/variables.h>
0024 #include <QStringList>
0025 #include <QCoreApplication>
0026 
0027 using namespace Analitza;
0028 
0029 Q_GLOBAL_STATIC(PlotsFactory, factoryInstance)
0030 
0031 PlotsFactory::PlotsFactory()
0032     : m_vars(new Variables)
0033 {}
0034 
0035 PlotsFactory::~PlotsFactory()
0036 {
0037 }
0038 
0039 PlotsFactory* PlotsFactory::self()
0040 {
0041     return factoryInstance;
0042 }
0043 
0044 PlotBuilder PlotsFactory::requestPlot(const Analitza::Expression& testexp, Dimension dim, const QSharedPointer<Variables> &vars) const
0045 {
0046     QStringList errs;
0047     
0048     if(!testexp.isCorrect() || testexp.toString().isEmpty()) {
0049         errs << QCoreApplication::tr("The expression is not correct");
0050         PlotBuilder b;
0051         b.m_errors = errs;
0052         return b;
0053     }
0054     
0055     Analitza::Expression exp(testexp);
0056     if(exp.isDeclaration())
0057         exp = exp.declarationValue();
0058     
0059     if(exp.isEquation())
0060         exp = exp.equationToFunction();
0061     
0062     Analitza::Analyzer a(vars ? vars : m_vars);
0063     a.setExpression(exp);
0064     a.setExpression(a.dependenciesToLambda());
0065 
0066     QString id;
0067     if(a.isCorrect()) {
0068         QString expectedid = FunctionGraphFactory::self()->trait(a.expression(), a.type(), dim);
0069         if(FunctionGraphFactory::self()->contains(expectedid)) {
0070             id = expectedid;
0071         } else if (!expectedid.isEmpty())
0072             errs << QCoreApplication::tr("Function type '%1' not recognized").arg(expectedid);
0073         else
0074             errs << QCoreApplication::tr("Function '%1' not recognized").arg(a.expression().toString());
0075     } else {
0076         errs << a.errors();
0077     }
0078     
0079     Q_ASSERT(!errs.isEmpty() || !id.isEmpty());
0080     
0081     PlotBuilder b;
0082     b.m_errors = errs;
0083     b.m_id = id;
0084     b.m_expression = a.expression();
0085     b.m_display = testexp.toString();
0086     b.m_vars = vars;
0087     return b;
0088 }
0089 
0090 QStringList PlotsFactory::examples(Dimensions s) const
0091 {
0092     QStringList examples;
0093     if(s & Dim1D) examples += FunctionGraphFactory::self()->examples(Dim1D);
0094     if(s & Dim2D) examples += FunctionGraphFactory::self()->examples(Dim2D);
0095     if(s & Dim3D) examples += FunctionGraphFactory::self()->examples(Dim3D);
0096     return examples;
0097 }
0098 
0099 //------------------------------------------ PlotBuilder
0100 
0101 PlotBuilder::PlotBuilder()
0102     : m_vars(nullptr)
0103 {}
0104 
0105 bool PlotBuilder::canDraw() const
0106 {
0107     return m_errors.isEmpty() && !m_id.isEmpty();
0108 }
0109 
0110 FunctionGraph* PlotBuilder::create(const QColor& color, const QString& name) const
0111 {
0112     FunctionGraph* it = FunctionGraphFactory::self()->buildItem(m_id, m_expression, m_vars);
0113     it->setColor(color);
0114     it->setName(name);
0115     it->setDisplay(m_display);
0116     return it;
0117 }
0118 
0119 Analitza::Expression PlotBuilder::expression() const
0120 {
0121     return m_expression;
0122 }
0123 
0124 QString PlotBuilder::display() const
0125 {
0126     return m_display;
0127 }