File: htmlexpressionwriter.cpp

package info (click to toggle)
analitza 4:17.08.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,824 kB
  • sloc: cpp: 26,899; perl: 63; sh: 16; makefile: 9
file content (274 lines) | stat: -rw-r--r-- 11,055 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*************************************************************************************
 *  Copyright (C) 2008 by Aleix Pol <aleixpol@kde.org>                               *
 *                                                                                   *
 *  This program is free software; you can redistribute it and/or                    *
 *  modify it under the terms of the GNU General Public License                      *
 *  as published by the Free Software Foundation; either version 2                   *
 *  of the License, or (at your option) any later version.                           *
 *                                                                                   *
 *  This program is distributed in the hope that it will be useful,                  *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
 *  GNU General Public License for more details.                                     *
 *                                                                                   *
 *  You should have received a copy of the GNU General Public License                *
 *  along with this program; if not, write to the Free Software                      *
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
 *************************************************************************************/

#include "htmlexpressionwriter.h"
#include "value.h"
#include "operator.h"
#include "container.h"
#include "vector.h"
#include <QStringList>
#include <QCoreApplication>

#include "list.h"
#include "variable.h"
#include "stringexpressionwriter.h"
#include "apply.h"
#include "analitzautils.h"
#include "matrix.h"

using namespace Analitza;

const double HtmlExpressionWriter::MIN_PRINTABLE_VALUE = 0.0000000000001; // since the precision we choose for 'G' is 12

//we use the one in string*
QMap<Operator::OperatorType, QString> initOperators();

template <class T>
QStringList HtmlExpressionWriter::allValues(T it, const T& itEnd, AbstractExpressionVisitor* writer)
{
    QStringList elements;
    for(; it!=itEnd; ++it)
        elements += (*it)->accept(writer).toString();
    
    return elements;
}


static const QMap<Operator::OperatorType, QString> s_operators=initOperators();

QString oper(const QString& op) { return QCoreApplication::translate("html representation of an operator", "<span class='op'>%1</span>").arg(op); }
QString oper(const QChar& op) { return QCoreApplication::translate("html representation of an operator", "<span class='op'>%1</span>").arg(op); }
QString keyword(const QString& op) { return QCoreApplication::translate("html representation of an operator", "<span class='keyword'>%1</span>").arg(op); }

HtmlExpressionWriter::HtmlExpressionWriter(const Object* o)
{
    if(o)
        m_result=o->accept(this);
}

QVariant HtmlExpressionWriter::visit(const Vector* vec)
{
    return QString(keyword(QStringLiteral("vector "))+oper(QStringLiteral("{ "))+allValues<Vector::const_iterator>(vec->constBegin(), vec->constEnd(), this).join(QString(oper(QStringLiteral(", "))))+oper(QStringLiteral(" }")));
}

QVariant HtmlExpressionWriter::visit(const Matrix* m)
{
    return QString(keyword(QStringLiteral("matrix "))+oper(QStringLiteral("{ "))+allValues(m->constBegin(), m->constEnd(), this).join(QString(oper(QStringLiteral(", "))))+oper(QStringLiteral(" }")));
}

QVariant HtmlExpressionWriter::visit(const MatrixRow* mr)
{
    return QString(keyword(QStringLiteral("matrixrow "))+oper(QStringLiteral("{ "))+allValues(mr->constBegin(), mr->constEnd(), this).join(QString(oper(QStringLiteral(", "))))+oper(QStringLiteral(" }")));
}

QVariant HtmlExpressionWriter::visit(const List* vec)
{
    if(!vec->isEmpty() && vec->at(0)->type()==Object::value && static_cast<Cn*>(vec->at(0))->format()==Cn::Char) {
        return QVariant::fromValue<QString>(QStringLiteral("<span class='string'>&quot;") + AnalitzaUtils::listToString(vec) + QStringLiteral("&quot;</span>"));
    } else
        return QVariant::fromValue<QString>(keyword(QStringLiteral("list "))+oper(QStringLiteral("{ "))+allValues<List::const_iterator>(vec->constBegin(), vec->constEnd(), this).join(QString(oper(QStringLiteral(", "))))+oper(QStringLiteral(" }")));
}

QVariant HtmlExpressionWriter::visit(const Cn* var)
{
    QString innerhtml;
    if(var->isBoolean())
        innerhtml = var->isTrue() ? QStringLiteral("true") : QStringLiteral("false");
    else if(var->isCharacter())
        innerhtml = QString(var->character());
    else if(var->isComplex()) {
        QString realpart;
        QString imagpart;
        bool realiszero = false;
        if (qAbs(var->complexValue().real()) > MIN_PRINTABLE_VALUE)
            realpart = QString::number(var->complexValue().real(), 'g', 12);
        else
            realiszero = true;
        
        if (var->complexValue().imag() != 1 && var->complexValue().imag() != -1) {
            if (qAbs(var->complexValue().imag()) > MIN_PRINTABLE_VALUE) {
                if (!realiszero && var->complexValue().imag()>0.)
                    realpart += QLatin1String("+");
                imagpart = QString::number(var->complexValue().imag(), 'g', 12);
                imagpart += QStringLiteral("*i");
            }
        } else  {
            if (var->complexValue().imag() == 1)
                imagpart = QStringLiteral("i");
            else if (var->complexValue().imag() == -1)
                imagpart = QStringLiteral("-i");
        }
        
        innerhtml = realpart+imagpart;
    }
    else
        innerhtml = QString::number(var->value(), 'g', 12);
    
    return QVariant::fromValue<QString>(QStringLiteral("<span class='num'>")+innerhtml+"</span>");
}

QVariant HtmlExpressionWriter::visit(const Analitza::Ci* var)
{
    return var->toHtml();
}

QVariant HtmlExpressionWriter::visit(const Analitza::Operator* o)
{
    return QVariant::fromValue<QString>(QStringLiteral("<span class='func'>") + o->toString() + QStringLiteral("</span>"));
}

QVariant HtmlExpressionWriter::visit ( const Analitza::Apply* a )
{
    Operator op=a->firstOperator();
    QStringList ret;
    QString toret;
    QString bounds;
    
    if(a->ulimit() || a->dlimit()) {
        bounds += oper('=');
        if(a->dlimit())
            bounds += a->dlimit()->accept(this).toString();
        bounds += oper(QStringLiteral(".."));
        if(a->ulimit())
            bounds += a->ulimit()->accept(this).toString();
    }
    else if(a->domain())
        bounds += oper('@')+a->domain()->accept(this).toString();
    
    int i = 0;
    foreach(Object* o, a->m_params) {
        Object::ObjectType type=o->type();
        switch(type) {
            if(type == Object::oper)
                Q_ASSERT(false);
                break;
            case Object::variable:
                ret << static_cast<const Ci*>(o)->accept(this).toString();
                break;
            case Object::apply: {
                Apply *c = (Apply*) o;
                QString s = c->accept(this).toString();
                if(s_operators.contains(op.operatorType())) {
                    Operator child_op = c->firstOperator();
                    
                    if(child_op.operatorType() && 
                            StringExpressionWriter::weight(&op, c->countValues(), -1)>StringExpressionWriter::weight(&child_op, c->countValues(), i))
                        s=oper('(')+s+oper(')');
                }
                ret << s;
            }    break;
            default:
                ret << o->accept(this).toString();
                break;
        }
        ++i;
    }
    
    bool func=op.operatorType()==Operator::function;
    if(func) {
        QString n = ret.takeFirst();
        if(a->m_params.first()->type()!=Object::variable)
            n=oper('(')+n+oper(')');
        
        toret += n+oper('(') + ret.join(oper(QStringLiteral(", "))) + oper(')');
    } else if(op.operatorType()==Operator::selector) {
        if(a->m_params.last()->isApply()) {
            const Apply* a1=static_cast<const Apply*>(a->m_params.last());
            if(s_operators.contains(a1->firstOperator().operatorType()))
                ret.last()=oper('(')+ret.last()+oper(')');
        }
        
        toret += ret.last() + oper('[') + ret.first() + oper(']');
    } else if(ret.count()>1 && s_operators.contains(op.operatorType())) {
        toret += ret.join(oper(s_operators.value(op.operatorType())));
    } else if(ret.count()==1 && op.operatorType()==Operator::minus)
        toret += oper('-')+ret[0];
    else {
        QString bounding;
        QStringList bvars;
        foreach(const Ci* bvar, a->bvarCi())
            bvars += bvar->accept(this).toString();
        
        if(!bounds.isEmpty() || !bvars.isEmpty()) {
            if(bvars.count()!=1) bounding +=oper('(');
            bounding += bvars.join(oper(QStringLiteral(", ")));
            if(bvars.count()!=1) bounding +=oper(')');
            
            bounding = ':'+bounding +bounds;
        }
        
        toret += op.accept(this).toString()+oper('(')+ret.join(oper(QStringLiteral(", ")))+bounding+oper(')');
    }
    
    return toret;
}

QVariant HtmlExpressionWriter::visit(const Container* var)
{
    QStringList ret = allValues(var->constBegin(), var->constEnd(), this);
    
    
    QString toret;
    switch(var->containerType()) {
        case Container::declare:
            toret += ret.join(oper(QStringLiteral(":=")));
            break;
        case Container::lambda: {
            QString last=ret.takeLast();
            QStringList bvars;
            foreach(const Ci* bvar, var->bvarCi())
                bvars += bvar->accept(this).toString();
            
            if(bvars.count()!=1) toret +=oper('(');
            toret += bvars.join(QStringLiteral(", "));
            if(bvars.count()!=1) toret +=oper(')');
            toret += oper(QStringLiteral("->")) + last;
        }    break;
        case Container::math:
            toret += ret.join(oper(QStringLiteral("; ")));
            break;
        case Container::uplimit: //x->(n1..n2) is put at the same time
        case Container::downlimit:
            break;
        case Container::bvar:
            if(ret.count()>1) toret += oper('(');
            toret += ret.join(QStringLiteral(", "));
            if(ret.count()>1) toret += oper(')');
            break;
        case Container::piece:
            toret += ret[1]+oper(QStringLiteral(" ? "))+ret[0];
            break;
        case Container::otherwise:
            toret += oper(QStringLiteral("? "))+ret[0];
            break;
        default:
            toret += var->tagName()+oper(QStringLiteral(" { "))+ret.join(oper(QStringLiteral(", ")))+oper(QStringLiteral(" }"));
            break;
    }
    return toret;
}

QVariant HtmlExpressionWriter::visit(const CustomObject*)
{
    return QStringLiteral("Custom Object");
}

QVariant HtmlExpressionWriter::visit(const None* )
{
    return QString();
}