File indexing completed on 2024-12-01 06:32:00

0001 /*************************************************************************************
0002  *  Copyright (C) 2007-2009 by Aleix Pol <aleixpol@kde.org>                          *
0003  *  Copyright (C) 2010-2012 by Percy Camilo T. Aucahuasi <percy.camilo.ta@gmail.com> *
0004  *                                                                                   *
0005  *  This program is free software; you can redistribute it and/or                    *
0006  *  modify it under the terms of the GNU General Public License                      *
0007  *  as published by the Free Software Foundation; either version 2                   *
0008  *  of the License, or (at your option) any later version.                           *
0009  *                                                                                   *
0010  *  This program 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                    *
0013  *  GNU General Public License for more details.                                     *
0014  *                                                                                   *
0015  *  You should have received a copy of the GNU General Public License                *
0016  *  along with this program; if not, write to the Free Software                      *
0017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0018  *************************************************************************************/
0019 
0020 
0021 
0022 #ifndef ABSTRACTFUNCTIONGRAPHFACTORY_H
0023 #define ABSTRACTFUNCTIONGRAPHFACTORY_H
0024 
0025 #include "analitzaplotexport.h"
0026 #include <plottingenums.h>
0027 
0028 #include <QPair>
0029 #include <QMap>
0030 #include <QSharedPointer>
0031 
0032 #include "planecurve.h"
0033 #include "spacecurve.h"
0034 #include "surface.h"
0035 
0036 #define REGISTER_FUNCTIONGRAPH_DIM(dim, constructor, name) \
0037 static AbstractFunctionGraph * vcreate##name(const Analitza::Expression &exp, const QSharedPointer<Analitza::Variables>& v) { return new name (exp, v); } \
0038         namespace { bool _##name=FunctionGraphFactory::self()->registerFunctionGraph(dim, constructor, vcreate##name, \
0039         name ::TypeName(), name ::ExpressionType, name ::CoordSystem(), name ::Parameters(), \
0040         name ::IconName(), name ::Examples); }
0041 
0042 #define REGISTER_PLANECURVE(...) REGISTER_FUNCTIONGRAPH_DIM(Dim2D, FunctionGraphFactory::createPlotItem<PlaneCurve>, __VA_ARGS__)
0043 #define REGISTER_SPACECURVE(...) REGISTER_FUNCTIONGRAPH_DIM(Dim3D, FunctionGraphFactory::createPlotItem<SpaceCurve>, __VA_ARGS__)
0044 #define REGISTER_SURFACE(...) REGISTER_FUNCTIONGRAPH_DIM(Dim3D, FunctionGraphFactory::createPlotItem<Surface>, __VA_ARGS__)
0045 
0046 namespace Analitza {
0047 class Variables;
0048 class Expression;
0049 class ExpressionType;
0050 class AbstractFunctionGraph;
0051 class PlotItem;
0052 
0053 class FunctionGraphFactory
0054 {
0055 public:
0056     typedef AbstractFunctionGraph* (*BuilderFunctionWithVars)(const Analitza::Expression&, const QSharedPointer<Analitza::Variables>& );
0057     typedef FunctionGraph* (*PlotItemConstuctor)(AbstractFunctionGraph*);
0058     typedef Analitza::ExpressionType (*ExpressionTypeFunction)();
0059     typedef QStringList (*ExamplesFunction)();
0060     
0061     template <class T>
0062     static FunctionGraph* createPlotItem(AbstractFunctionGraph* g)
0063     { return new T(g); }
0064 
0065     QString typeName(const QString& id) const;
0066     Analitza::ExpressionType expressionType(const QString& id) const;
0067     //swap args 
0068     Dimension spaceDimension(const QString& id) const;
0069     // falla cuando hay multiples cassos ...mult expressio
0070 //     Dimension spaceDimension(const Analitza::ExpressionType& ftype, const QStringList &bvars) const;
0071     CoordinateSystem coordinateSystem(const QString& id) const;
0072     QString iconName(const QString& id) const;
0073     QStringList examples(const QString& id) const;
0074     QStringList examples(Dimension dim) const;
0075     
0076     static FunctionGraphFactory* self();
0077 
0078     bool registerFunctionGraph(Dimension dim, PlotItemConstuctor constructor, BuilderFunctionWithVars builderFunctionWithVars,
0079                   const char* typeNameFunction, ExpressionTypeFunction expressionTypeFunction, 
0080                          CoordinateSystem coordinateSystemFunction, const QStringList& argumentsFunction,
0081                          const QString& iconNameFunction, ExamplesFunction examplesFunction);
0082     QString trait(const Analitza::Expression& expr, const Analitza::ExpressionType& t, Dimension dim) const;
0083     bool contains(const QString &id) const;
0084     
0085     AbstractFunctionGraph * build(const QString& id, const Analitza::Expression& exp, const QSharedPointer<Analitza::Variables>& v) const;
0086     FunctionGraph* buildItem(const QString& id, const Analitza::Expression& exp, const QSharedPointer<Analitza::Variables>& v) const;
0087 
0088     QMap< QString, QPair< QStringList, Analitza::ExpressionType > > registeredFunctionGraphs() const;
0089 
0090 private:
0091     QMap<QString, const char *> typeNameFunctions;
0092     QMap<QString, ExpressionTypeFunction> expressionTypeFunctions;
0093     QMap<QString, Dimension> spaceDimensions; //internal use (without a "getter")
0094     QMap<QString, CoordinateSystem> coordinateSystemFunctions;
0095     QMap<QString, QStringList> argumentsFunctions; //internal use (without a "getter")
0096     QMap<QString, QString> iconNameFunctions;
0097     QMap<QString, ExamplesFunction> examplesFunctions;
0098     QMap<QString, PlotItemConstuctor> plotConstructor;
0099 
0100     static FunctionGraphFactory* m_self;
0101     FunctionGraphFactory() {
0102         Q_ASSERT(!m_self);
0103         m_self = this;
0104     }
0105 
0106     QMap<QString, BuilderFunctionWithVars> builderFunctionsWithVars;
0107 };
0108 
0109 }
0110 
0111 #endif // ABSTRACTFUNCTIONGRAPHFACTORY_H