1
/*************************************************************************************
2
* Copyright (C) 2007 by Aleix Pol <aleixpol@kde.org> *
4
* This program is free software; you can redistribute it and/or *
5
* modify it under the terms of the GNU General Public License *
6
* as published by the Free Software Foundation; either version 2 *
7
* of the License, or (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the Free Software *
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
17
*************************************************************************************/
21
#include "analitza/variables.h"
22
#include "analitza/analyzer.h"
23
#include "analitza/expression.h"
24
#include "functionimpl.h"
25
#include "functionfactory.h"
30
using Analitza::ExpressionType;
33
: m_function(0), m_show(true), m_pen(Qt::black)
36
Function::Function(const QString &name, const Analitza::Expression& newFunc, Analitza::Variables* v,
37
const QPen& pen, double uplimit, double downlimit)
38
: m_function(0), m_expression(newFunc), m_show(true), m_pen(pen), m_name(name)
40
if(newFunc.isCorrect()) {
41
Analitza::Analyzer a(v);
42
a.setExpression(newFunc);
44
m_expression=a.dependenciesToLambda();
45
a.setExpression(m_expression);
47
QStringList bvars=m_expression.bvarList();
49
//TODO: turn into assertion
50
if(!FunctionFactory::self()->contains(bvars))
51
m_err << i18n("Function type not recognized");
52
else if(!a.isCorrect())
56
ExpressionType expected=FunctionFactory::self()->type(bvars);
57
ExpressionType actual=a.type();
59
correct &= actual.canReduceTo(expected);
62
m_function=FunctionFactory::self()->item(bvars, m_expression, v);
63
if(downlimit!=uplimit)
64
m_function->setLimits(downlimit, uplimit);
66
m_err << i18n("Function type not correct for functions depending on %1", bvars.join(i18n(", ")));
69
m_err << i18n("The expression is not correct");
73
Function::Function(const Function& f)
74
: m_function(0), m_expression(f.expression()), m_show(f.m_show), m_pen(f.m_pen)
75
, m_name(f.m_name), m_err(f.m_err)
78
m_function=f.m_function->copy();
86
Function Function::operator=(const Function& f)
92
m_function=f.m_function->copy();
93
// m_function=copy(f.m_function);
97
m_expression=f.m_expression;
106
void Function::update_points(const QRect& viewport)
108
Q_ASSERT(m_function);
109
Q_ASSERT(resolution()>2);
111
m_function->updatePoints(viewport);
112
Q_ASSERT(!m_function->isCorrect() || m_function->points.size()>=2);
115
void Function::setResolution(unsigned int resolution)
117
Q_ASSERT(m_function);
118
m_function->setResolution(resolution);
121
uint Function::resolution() const
123
return m_function->resolution();
126
Function::Axe Function::axeType() const
128
return m_function->axeType();
131
bool Function::isShown() const
133
return m_show && m_function && m_function->isCorrect();
136
QLineF Function::derivative(const QPointF & p) const
138
Q_ASSERT(m_function);
139
return m_function->derivative(p);
142
const QVector<QPointF>& Function::points() const
144
Q_ASSERT(m_function);
145
Q_ASSERT(m_function->points.size()>1);
146
return m_function->points;
149
QPair< QPointF, QString > Function::calc(const QPointF & dp)
151
Q_ASSERT(m_function);
152
return m_function->calc(dp);
155
bool Function::isCorrect() const
157
return m_function && m_err.isEmpty() && m_function->isCorrect();
160
QStringList Function::errors() const
162
QStringList err(m_err);
164
err += m_function->m_err;
165
err += m_function->func.errors();
170
const Analitza::Expression& Function::expression() const
175
QList<int> Function::jumps() const
177
return m_function->m_jumps;
180
bool Function::allDisconnected() const
182
return m_function->allDisconnected();
185
QString Function::icon() const
187
return m_function->iconName();