Cutelee 6.1.0
logic.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 "logic.h"
22
23#include "util.h"
24
26 const QVariant &argument,
27 bool autoescape) const
28{
29 Q_UNUSED(autoescape)
30 if (!input.isValid() || getSafeString(input).get().isEmpty())
31 return argument;
32 return getSafeString(input);
33}
34
36 const QVariant &argument,
37 bool autoescape) const
38{
39 Q_UNUSED(autoescape)
40 if (!input.isValid())
41 return argument;
42 return getSafeString(input);
43}
44
46 const QVariant &argument,
47 bool autoescape) const
48{
49 Q_UNUSED(autoescape)
50 return (getSafeString(input).get().toInt() % QVariant(argument).value<int>()
51 == 0)
52 ? QStringLiteral("true")
53 : QString();
54}
55
56QVariant YesNoFilter::doFilter(const QVariant &input, const QVariant &argument,
57 bool autoescape) const
58{
59 Q_UNUSED(autoescape)
60 auto arg = getSafeString(argument);
61 QString yes;
62 QString no;
63 QString maybe;
64 if (arg.get().isEmpty()) {
65 yes = QStringLiteral("yes");
66 no = QStringLiteral("no");
67 maybe = QStringLiteral("maybe");
68 } else {
69 auto argList = arg.get().split(QLatin1Char(','));
70 auto numArgs = argList.size();
71 if ((numArgs < 2) || (numArgs > 3)) {
72 return input.toString();
73 } else if (numArgs == 2) {
74 yes = argList.first();
75 no = argList.at(1);
76 maybe = argList.at(1);
77 } else if (numArgs == 3) {
78 yes = argList.first();
79 no = argList.at(1);
80 maybe = argList.at(2);
81 }
82 }
83 if (!input.isValid())
84 return maybe;
85 if (!getSafeString(input).get().isEmpty())
86 return yes;
87 return no;
88}
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
Definition logic.cpp:25
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
Definition logic.cpp:35
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
Definition logic.cpp:45
QVariant doFilter(const QVariant &input, const QVariant &argument={}, bool autoescape={}) const override
Definition logic.cpp:56
Cutelee::SafeString getSafeString(const QVariant &input)
Definition util.cpp:108
Utility functions used throughout Cutelee.