~ubuntu-branches/ubuntu/precise/kalgebra/precise-updates

« back to all changes in this revision

Viewing changes to analitza/htmlexpressionwriter.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-16 16:41:53 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20111216164153-ym1s7jhbqwn2ndz6
Tags: 4:4.7.90-0ubuntu2
PPA rebuild, no changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*************************************************************************************
2
 
 *  Copyright (C) 2008 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 "htmlexpressionwriter.h"
20
 
#include "value.h"
21
 
#include "operator.h"
22
 
#include "container.h"
23
 
#include "vector.h"
24
 
#include <QStringList>
25
 
#include <KLocale>
26
 
#include "list.h"
27
 
#include "variable.h"
28
 
#include "stringexpressionwriter.h"
29
 
#include "apply.h"
30
 
#include "analitzautils.h"
31
 
 
32
 
using namespace Analitza;
33
 
 
34
 
//we use the one in string*
35
 
QMap<Operator::OperatorType, QString> initOperators();
36
 
 
37
 
template <class T>
38
 
QStringList HtmlExpressionWriter::allValues(T it, const T& itEnd, ExpressionWriter* writer)
39
 
{
40
 
        QStringList elements;
41
 
        for(; it!=itEnd; ++it)
42
 
                elements += (*it)->visit(writer);
43
 
        
44
 
        return elements;
45
 
}
46
 
 
47
 
 
48
 
static const QMap<Operator::OperatorType, QString> s_operators=initOperators();
49
 
 
50
 
QString oper(const QString& op) { return i18nc("html representation of an operator", "<span class='op'>%1</span>", op); }
51
 
QString oper(const QChar& op) { return i18nc("html representation of an operator", "<span class='op'>%1</span>", op); }
52
 
QString keyword(const QString& op) { return i18nc("html representation of an operator", "<span class='keyword'>%1</span>", op); }
53
 
 
54
 
HtmlExpressionWriter::HtmlExpressionWriter(const Object* o)
55
 
{
56
 
        m_result=o->visit(this);
57
 
}
58
 
 
59
 
QString HtmlExpressionWriter::accept(const Vector* vec)
60
 
{
61
 
        return keyword("vector ")+oper("{ ")+allValues<Vector::const_iterator>(vec->constBegin(), vec->constEnd(), this).join(QString(oper(", ")))+oper(" }");
62
 
}
63
 
 
64
 
QString HtmlExpressionWriter::accept(const List* vec)
65
 
{
66
 
        if(!vec->isEmpty() && vec->at(0)->type()==Object::value && static_cast<Cn*>(vec->at(0))->format()==Cn::Char) {
67
 
                return "<span class='string'>&quot;"+AnalitzaUtils::listToString(vec)+ "&quot;</span>";
68
 
        } else
69
 
                return keyword("list ")+oper("{ ")+allValues<List::const_iterator>(vec->constBegin(), vec->constEnd(), this).join(QString(oper(", ")))+oper(" }");
70
 
}
71
 
 
72
 
QString HtmlExpressionWriter::accept(const Cn* var)
73
 
{
74
 
        if(var->isBoolean())
75
 
                return "<span class='var'>"+QString(var->isTrue() ? "true" : "false")+"</span>";
76
 
        else
77
 
                return "<span class='num'>"+QString::number(var->value(), 'g', 12)+"</span>";
78
 
}
79
 
 
80
 
QString HtmlExpressionWriter::accept(const Analitza::Ci* var)
81
 
{
82
 
        return var->toHtml();
83
 
}
84
 
 
85
 
QString HtmlExpressionWriter::accept(const Analitza::Operator* o)
86
 
{
87
 
        return "<span class='func'>"+o->toString()+"</span>";
88
 
}
89
 
 
90
 
QString HtmlExpressionWriter::accept ( const Analitza::Apply* a )
91
 
{
92
 
        Operator op=a->firstOperator();
93
 
        QStringList ret;
94
 
        QString toret;
95
 
        QString bounds;
96
 
        
97
 
        if(a->ulimit() || a->dlimit()) {
98
 
                bounds += oper('=');
99
 
                if(a->dlimit())
100
 
                        bounds += a->dlimit()->visit(this);
101
 
                bounds += oper("..");
102
 
                if(a->ulimit())
103
 
                        bounds += a->ulimit()->visit(this);
104
 
        }
105
 
        else if(a->domain())
106
 
                bounds += oper('@')+a->domain()->visit(this);
107
 
        
108
 
        foreach(Object* o, a->m_params) {
109
 
                Object::ObjectType type=o->type();
110
 
                switch(type) {
111
 
                        if(type == Object::oper)
112
 
                                Q_ASSERT(false);
113
 
                                break;
114
 
                        case Object::variable:
115
 
                                ret << static_cast<const Ci*>(o)->visit(this);
116
 
                                break;
117
 
                        case Object::apply: {
118
 
                                Apply *c = (Apply*) o;
119
 
                                QString s = c->visit(this);
120
 
                                if(s_operators.contains(op.operatorType())) {
121
 
                                        Operator child_op = c->firstOperator();
122
 
                                        
123
 
                                        if(child_op.operatorType() && 
124
 
                                                        StringExpressionWriter::weight(&op, c->countValues())>=StringExpressionWriter::weight(&child_op, c->countValues()))
125
 
                                                s=oper('(')+s+oper(')');
126
 
                                }
127
 
                                ret << s;
128
 
                        }       break;
129
 
                        default:
130
 
                                ret << o->visit(this);
131
 
                                break;
132
 
                }
133
 
        }
134
 
        
135
 
        bool func=op.operatorType()==Operator::function;
136
 
        if(func) {
137
 
                QString n = ret.takeFirst();
138
 
                if(a->m_params.first()->type()!=Object::variable)
139
 
                        n=oper('(')+n+oper(')');
140
 
                
141
 
                toret += n+oper('(') + ret.join(oper(", ")) + oper(')');
142
 
        } else if(ret.count()>1 && s_operators.contains(op.operatorType())) {
143
 
                toret += ret.join(oper(s_operators.value(op.operatorType())));
144
 
        } else if(ret.count()==1 && op.operatorType()==Operator::minus)
145
 
                toret += oper('-')+ret[0];
146
 
        else if(op.operatorType()==Operator::selector) {
147
 
                QString value = ret.takeLast();
148
 
                if(a->m_params.last()->isApply())
149
 
                        value = oper('(')+value+oper(')');
150
 
                toret += value + oper('[')+ret.join(oper(", "))+oper(']');
151
 
        }else {
152
 
                QString bounding;
153
 
                QStringList bvars;
154
 
                foreach(const Ci* bvar, a->bvarCi())
155
 
                        bvars += bvar->visit(this);
156
 
                
157
 
                if(!bounds.isEmpty() || !bvars.isEmpty()) {
158
 
                        if(bvars.count()!=1) bounding +=oper('(');
159
 
                        bounding += bvars.join(oper(", "));
160
 
                        if(bvars.count()!=1) bounding +=oper(')');
161
 
                        
162
 
                        bounding = ':'+bounding +bounds;
163
 
                }
164
 
                
165
 
                toret += op.visit(this)+oper('(')+ret.join(oper(", "))+bounding+oper(')');
166
 
        }
167
 
        
168
 
        return toret;
169
 
}
170
 
 
171
 
QString HtmlExpressionWriter::accept(const Container* var)
172
 
{
173
 
        QStringList ret = allValues(var->constBegin(), var->constEnd(), this);
174
 
        
175
 
        
176
 
        QString toret;
177
 
        switch(var->containerType()) {
178
 
                case Container::declare:
179
 
                        toret += ret.join(oper(":="));
180
 
                        break;
181
 
                case Container::lambda: {
182
 
                        QString last=ret.takeLast();
183
 
                        QStringList bvars;
184
 
                        foreach(const Ci* bvar, var->bvarCi())
185
 
                                bvars += bvar->visit(this);
186
 
                        
187
 
                        if(bvars.count()!=1) toret +=oper('(');
188
 
                        toret += bvars.join(", ");
189
 
                        if(bvars.count()!=1) toret +=oper(')');
190
 
                        toret += oper("->") + last;
191
 
                }       break;
192
 
                case Container::math:
193
 
                        toret += ret.join(oper("; "));
194
 
                        break;
195
 
                case Container::uplimit: //x->(n1..n2) is put at the same time
196
 
                case Container::downlimit:
197
 
                        break;
198
 
                case Container::bvar:
199
 
                        if(ret.count()>1) toret += oper('(');
200
 
                        toret += ret.join(", ");
201
 
                        if(ret.count()>1) toret += oper(')');
202
 
                        break;
203
 
                case Container::piece:
204
 
                        toret += ret[1]+oper(" ? ")+ret[0];
205
 
                        break;
206
 
                case Container::otherwise:
207
 
                        toret += oper("? ")+ret[0];
208
 
                        break;
209
 
                default:
210
 
                        toret += var->tagName()+oper(" { ")+ret.join(oper(", "))+oper(" }");
211
 
                        break;
212
 
        }
213
 
        return toret;
214
 
}
215
 
 
216
 
QString HtmlExpressionWriter::accept(const CustomObject*)
217
 
{
218
 
        return "Custom Object";
219
 
}