Cutelee
6.1.0
templates
defaulttags
widthratio.cpp
1
/*
2
This file is part of the Cutelee template system.
3
4
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
5
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either version
9
2.1 of the Licence, or (at your option) any later version.
10
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Lesser General Public License for more details.
15
16
You should have received a copy of the GNU Lesser General Public
17
License along with this library. If not, see <http://www.gnu.org/licenses/>.
18
19
*/
20
21
#include "widthratio.h"
22
23
#include "../lib/exception.h"
24
#include "parser.h"
25
26
WidthRatioNodeFactory::WidthRatioNodeFactory() {}
27
28
Node
*
WidthRatioNodeFactory::getNode
(
const
QString
&tagContent,
Parser
*p)
const
29
{
30
auto
expr =
smartSplit
(tagContent);
31
32
if
(expr.size() != 4) {
33
throw
Cutelee::Exception
(
34
TagSyntaxError, QStringLiteral(
"widthratio takes three arguments"
));
35
}
36
FilterExpression
valExpr(expr.at(1), p);
37
FilterExpression
maxExpr(expr.at(2), p);
38
FilterExpression
maxWidth(expr.at(3), p);
39
40
return
new
WidthRatioNode
(valExpr, maxExpr, maxWidth, p);
41
}
42
43
WidthRatioNode::WidthRatioNode(
const
FilterExpression
&valExpr,
44
const
FilterExpression
&maxExpr,
45
const
FilterExpression
&maxWidth,
46
QObject *parent)
47
:
Node
(parent)
48
{
49
m_valExpr = valExpr;
50
m_maxExpr = maxExpr;
51
m_maxWidth = maxWidth;
52
}
53
54
int
WidthRatioNode::round(qreal number)
55
{
56
auto
intPart = (int)number;
57
if
(number < (intPart + 0.5))
58
return
intPart;
59
return
intPart + 1;
60
}
61
62
void
WidthRatioNode::render
(
OutputStream
*stream,
Context
*c)
const
63
{
64
auto
thisVal = m_valExpr.
resolve
(c);
65
auto
maxVal = m_maxExpr.
resolve
(c);
66
if
(!thisVal.isValid() || !maxVal.isValid())
67
return
;
68
69
auto
tv = thisVal.value<
double
>();
70
auto
mv = maxVal.value<
double
>();
71
72
if
(mv == 0)
73
return
;
74
75
auto
maxWidth = m_maxWidth.
resolve
(c).value<
int
>();
76
77
auto
result = (tv / mv) * maxWidth;
78
79
result = round(result);
80
81
// TODO put integral streamers in OutputStream?
82
(*stream) << QString::number(result);
83
}
Cutelee::AbstractNodeFactory::smartSplit
Q_INVOKABLE QStringList smartSplit(const QString &str) const
Definition
node.cpp:202
Cutelee::Context
The Context class holds the context to render a Template with.
Definition
context.h:119
Cutelee::Exception
An exception for use when implementing template tags.
Definition
exception.h:85
Cutelee::FilterExpression
A FilterExpression object represents a filter expression in a template.
Definition
filterexpression.h:120
Cutelee::FilterExpression::resolve
QVariant resolve(OutputStream *stream, Context *c) const
Definition
filterexpression.cpp:209
Cutelee::Node
Base class for all nodes.
Definition
node.h:78
Cutelee::OutputStream
The OutputStream class is used to render templates to a QTextStream.
Definition
outputstream.h:81
Cutelee::Parser
The Parser class processes a string template into a tree of nodes.
Definition
parser.h:49
QString
Definition
operatorcasttests.cpp:24
WidthRatioNodeFactory::getNode
Node * getNode(const QString &tagContent, Parser *p) const override
Definition
widthratio.cpp:28
WidthRatioNode
Definition
widthratio.h:38
WidthRatioNode::render
void render(OutputStream *stream, Context *c) const override
Definition
widthratio.cpp:62
Generated by
1.9.8