File indexing completed on 2022-09-27 12:21:23

0001 /*************************************************************************************
0002  *  Copyright (C) 2010 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 #ifndef ANALITZAWRAPPER_H
0020 #define ANALITZAWRAPPER_H
0021 
0022 #include <QScopedPointer>
0023 #include <QObject>
0024 #include <QVariant>
0025 #include <analitza/expression.h>
0026 #include <analitza/variables.h>
0027 
0028 namespace Analitza {
0029     class Analyzer;
0030 }
0031 
0032 class ExpressionWrapper : public QObject
0033 {
0034     Q_OBJECT
0035     Q_PROPERTY(QString expression READ toString CONSTANT)
0036     Q_PROPERTY(bool isCorrect READ isCorrect CONSTANT)
0037     public:
0038         explicit ExpressionWrapper(QObject* parent=nullptr);
0039         explicit ExpressionWrapper(const Analitza::Expression & e, QObject* parent = nullptr);
0040         
0041         bool isCorrect() const;
0042         QString toString() const;
0043 
0044     public Q_SLOTS:
0045         QVariant value() const;
0046         QStringList errors() const;
0047         
0048     private:
0049         Analitza::Expression m_exp;
0050 };
0051 
0052 class AnalitzaWrapper : public QObject
0053 {
0054     Q_OBJECT
0055     Q_PROPERTY(bool calculate READ isCalculate WRITE setCalculate NOTIFY isCalculateChanged)
0056     Q_PROPERTY(bool isCorrect READ isCorrect)
0057     Q_PROPERTY(QStringList errors READ errors)
0058     Q_PROPERTY(QSharedPointer<Analitza::Variables> variables READ variables WRITE setVariables)
0059     public:
0060         explicit AnalitzaWrapper(QObject* parent = nullptr);
0061         ~AnalitzaWrapper() override;
0062         
0063         void setCalculate(bool calc);
0064         bool isCalculate() const { return m_calc; } 
0065         
0066         Analitza::Analyzer* wrapped() const { return m_wrapped.data(); }
0067         
0068         Q_SCRIPTABLE QVariant simplify(const QString& expression);
0069 
0070         Q_SCRIPTABLE QVariant execute(const QString& expression);
0071         Q_SCRIPTABLE QVariant executeFunc(const QString& name, const QVariantList& args);
0072         Q_SCRIPTABLE QString unusedVariableName() const;
0073         Q_SCRIPTABLE QString dependenciesToLambda(const QString& expression) const;
0074         Q_SCRIPTABLE void insertVariable(const QString& name, const QString& expression) const;
0075         Q_SCRIPTABLE void removeVariable(const QString& name);
0076         
0077         QStringList errors() const;
0078         bool isCorrect() const;
0079         
0080         QSharedPointer<Analitza::Variables> variables() const { return m_vars; }
0081         void setVariables(const QSharedPointer<Analitza::Variables> &v);
0082 
0083     Q_SIGNALS:
0084         void isCalculateChanged(bool isCalculate);
0085         
0086     private:
0087         void initWrapped();
0088         
0089         QScopedPointer<Analitza::Analyzer> m_wrapped;
0090         QSharedPointer<Analitza::Variables> m_vars;
0091         bool m_calc;
0092 };
0093 
0094 #endif // ANALITZAWRAPPER_H