~ubuntu-branches/ubuntu/oneiric/kalgebra/oneiric-updates

« back to all changes in this revision

Viewing changes to analitzagui/function.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-08 20:10:34 UTC
  • Revision ID: james.westby@ubuntu.com-20110708201034-0cqpagx7uz4pu82n
Tags: upstream-4.6.90+repack1
Import upstream version 4.6.90+repack1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************************************************************************
 
2
 *  Copyright (C) 2007 by Aleix Pol <aleixpol@kde.org>                               *
 
3
 *                                                                                   *
 
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.                           *
 
8
 *                                                                                   *
 
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.                                     *
 
13
 *                                                                                   *
 
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
 *************************************************************************************/
 
18
 
 
19
#include "function.h"
 
20
 
 
21
#include "analitza/variables.h"
 
22
#include "analitza/analyzer.h"
 
23
#include "analitza/expression.h"
 
24
#include "functionimpl.h"
 
25
#include "functionfactory.h"
 
26
 
 
27
#include <KLocale>
 
28
#include <cmath>
 
29
 
 
30
using Analitza::ExpressionType;
 
31
 
 
32
Function::Function()
 
33
        : m_function(0), m_show(true), m_pen(Qt::black)
 
34
{}
 
35
 
 
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)
 
39
{
 
40
        if(newFunc.isCorrect()) {
 
41
                Analitza::Analyzer a(v);
 
42
                a.setExpression(newFunc);
 
43
                
 
44
                m_expression=a.dependenciesToLambda();
 
45
        a.setExpression(m_expression);
 
46
                
 
47
                QStringList bvars=m_expression.bvarList();
 
48
                
 
49
                //TODO: turn into assertion
 
50
                if(!FunctionFactory::self()->contains(bvars))                                        
 
51
                        m_err << i18n("Function type not recognized");
 
52
                else if(!a.isCorrect())
 
53
                        m_err << a.errors();
 
54
                else {
 
55
                        bool correct=true;
 
56
                        ExpressionType expected=FunctionFactory::self()->type(bvars);
 
57
                        ExpressionType actual=a.type();
 
58
                        
 
59
                        correct &= actual.canReduceTo(expected);
 
60
                        
 
61
                        if(correct) {
 
62
                                m_function=FunctionFactory::self()->item(bvars, m_expression, v);
 
63
                                if(downlimit!=uplimit)
 
64
                                        m_function->setLimits(downlimit, uplimit);
 
65
                        } else
 
66
                                m_err << i18n("Function type not correct for functions depending on %1", bvars.join(i18n(", ")));
 
67
                }
 
68
        } else {
 
69
                m_err << i18n("The expression is not correct");
 
70
        }
 
71
}
 
72
 
 
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)
 
76
{
 
77
        if(f.m_function)
 
78
                m_function=f.m_function->copy();
 
79
}
 
80
 
 
81
Function::~Function()
 
82
{
 
83
        delete m_function;
 
84
}
 
85
 
 
86
Function Function::operator=(const Function& f)
 
87
{
 
88
        if(&f!=this) {
 
89
                delete m_function;
 
90
                
 
91
                if(f.m_function) {
 
92
                        m_function=f.m_function->copy();
 
93
//                      m_function=copy(f.m_function);
 
94
                        Q_ASSERT(m_function);
 
95
                } else
 
96
                        m_function=0;
 
97
                m_expression=f.m_expression;
 
98
                m_show=f.m_show;
 
99
                m_pen=f.m_pen;
 
100
                m_name=f.m_name;
 
101
                m_err=f.m_err;
 
102
        }
 
103
        return *this;
 
104
}
 
105
 
 
106
void Function::update_points(const QRect& viewport)
 
107
{
 
108
        Q_ASSERT(m_function);
 
109
        Q_ASSERT(resolution()>2);
 
110
        
 
111
        m_function->updatePoints(viewport);
 
112
        Q_ASSERT(!m_function->isCorrect() || m_function->points.size()>=2);
 
113
}
 
114
 
 
115
void Function::setResolution(unsigned int resolution)
 
116
{
 
117
        Q_ASSERT(m_function);
 
118
        m_function->setResolution(resolution);
 
119
}
 
120
 
 
121
uint Function::resolution() const
 
122
{
 
123
        return m_function->resolution();
 
124
}
 
125
 
 
126
Function::Axe Function::axeType() const
 
127
{
 
128
        return m_function->axeType();
 
129
}
 
130
 
 
131
bool Function::isShown() const
 
132
{
 
133
        return m_show && m_function && m_function->isCorrect();
 
134
}
 
135
 
 
136
QLineF Function::derivative(const QPointF & p) const
 
137
{
 
138
        Q_ASSERT(m_function);
 
139
        return m_function->derivative(p);
 
140
}
 
141
 
 
142
const QVector<QPointF>& Function::points() const
 
143
{
 
144
        Q_ASSERT(m_function);
 
145
        Q_ASSERT(m_function->points.size()>1);
 
146
        return m_function->points;
 
147
}
 
148
 
 
149
QPair< QPointF, QString > Function::calc(const QPointF & dp)
 
150
{
 
151
        Q_ASSERT(m_function);
 
152
        return m_function->calc(dp);
 
153
}
 
154
 
 
155
bool Function::isCorrect() const
 
156
{
 
157
        return m_function && m_err.isEmpty() && m_function->isCorrect();
 
158
}
 
159
 
 
160
QStringList Function::errors() const
 
161
{
 
162
        QStringList err(m_err);
 
163
        if(m_function) {
 
164
                err += m_function->m_err;
 
165
                err += m_function->func.errors();
 
166
        }
 
167
        return err;
 
168
}
 
169
 
 
170
const Analitza::Expression& Function::expression() const
 
171
{
 
172
        return m_expression;
 
173
}
 
174
 
 
175
QList<int> Function::jumps() const
 
176
{
 
177
        return m_function->m_jumps;
 
178
}
 
179
 
 
180
bool Function::allDisconnected() const
 
181
{
 
182
    return m_function->allDisconnected();
 
183
}
 
184
 
 
185
QString Function::icon() const
 
186
{
 
187
        return m_function->iconName();
 
188
}