KTextTemplate

context.cpp
1/*
2 This file is part of the KTextTemplate library
3
4 SPDX-FileCopyrightText: 2009, 2010 Stephen Kelly <steveire@gmail.com>
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7
8*/
9
10#include "context.h"
11
12#include "nulllocalizer_p.h"
13#include "rendercontext.h"
14#include "util.h"
15
16#include <QStringList>
17
18using namespace KTextTemplate;
19
20namespace KTextTemplate
21{
22class ContextPrivate
23{
24 ContextPrivate(Context *context, const QVariantHash &variantHash)
25 : q_ptr(context)
26 , m_renderContext(new RenderContext)
27 , m_localizer(new NullLocalizer)
28 {
29 m_variantHashStack.append(variantHash);
30 }
31
32 ~ContextPrivate()
33 {
34 delete m_renderContext;
35 }
36
37 Q_DECLARE_PUBLIC(Context)
38 Context *const q_ptr;
39
40 QList<QVariantHash> m_variantHashStack;
41 bool m_autoescape = true;
42 bool m_mutating = false;
43 QList<std::pair<QString, QString>> m_externalMedia;
45 QString m_relativeMediaPath;
46 RenderContext *const m_renderContext;
48};
49}
50
52 : d_ptr(new ContextPrivate(this, QVariantHash()))
53{
54}
55
56Context::Context(const QVariantHash &variantHash)
57 : d_ptr(new ContextPrivate(this, variantHash))
58{
59}
60
62 : d_ptr(new ContextPrivate(this, QVariantHash()))
63{
64 *this = other;
65}
66
68{
69 if (&other == this)
70 return *this;
71 d_ptr->m_autoescape = other.d_ptr->m_autoescape;
72 d_ptr->m_externalMedia = other.d_ptr->m_externalMedia;
73 d_ptr->m_mutating = other.d_ptr->m_mutating;
74 d_ptr->m_variantHashStack = other.d_ptr->m_variantHashStack;
75 d_ptr->m_urlType = other.d_ptr->m_urlType;
76 d_ptr->m_relativeMediaPath = other.d_ptr->m_relativeMediaPath;
77 return *this;
78}
79
81{
82 delete d_ptr;
83}
84
85bool Context::autoEscape() const
86{
87 Q_D(const Context);
88 return d->m_autoescape;
89}
90
91void Context::setAutoEscape(bool autoescape)
92{
93 Q_D(Context);
94 d->m_autoescape = autoescape;
95}
96
98{
99 Q_D(const Context);
100
101 // return a variant from the stack.
102 for (const auto &h : d->m_variantHashStack) {
103 auto it = h.constFind(str);
104 if (it != h.constEnd()) {
105 auto var = it.value();
106 // If the user passed a string into the context, turn it into a
107 // KTextTemplate::SafeString.
108 if (var.userType() == qMetaTypeId<QString>()) {
110 }
111 return var;
112 }
113 }
114
115 return {};
116}
117
119{
120 Q_D(Context);
121
122 const QHash<QString, QVariant> hash;
123 d->m_variantHashStack.prepend(hash);
124}
125
127{
128 Q_D(Context);
129
130 d->m_variantHashStack.removeFirst();
131}
132
133void Context::insert(const QString &name, const QVariant &variant)
134{
135 Q_D(Context);
136
137 d->m_variantHashStack[0].insert(name, variant);
138}
139
140void Context::insert(const QString &name, QObject *object)
141{
142 Q_D(Context);
143
144 d->m_variantHashStack[0].insert(name, QVariant::fromValue(object));
145}
146
147QHash<QString, QVariant> Context::stackHash(int depth) const
148{
149 Q_D(const Context);
150
151 return d->m_variantHashStack.value(depth);
152}
153
154bool Context::isMutating() const
155{
156 Q_D(const Context);
157 return d->m_mutating;
158}
159
160void Context::setMutating(bool mutating)
161{
162 Q_D(Context);
163 d->m_mutating = mutating;
164}
165
166void Context::addExternalMedia(const QString &absolutePart, const QString &relativePart)
167{
168 Q_D(Context);
169 d->m_externalMedia.append(std::make_pair(absolutePart, relativePart));
170}
171
173{
174 Q_D(const Context);
175 return d->m_externalMedia;
176}
177
178void Context::clearExternalMedia()
179{
180 Q_D(Context);
181 d->m_externalMedia.clear();
182}
183
185{
186 Q_D(Context);
187 d->m_urlType = type;
188}
189
191{
192 Q_D(const Context);
193 return d->m_urlType;
194}
195
197{
198 Q_D(Context);
199 d->m_relativeMediaPath = path;
200}
201
203{
204 Q_D(const Context);
205 return d->m_relativeMediaPath;
206}
207
209{
210 Q_D(const Context);
211 return d->m_renderContext;
212}
213
215{
216 Q_D(Context);
217 if (!localizer) {
218 d->m_localizer = QSharedPointer<AbstractLocalizer>(new NullLocalizer);
219 return;
220 }
221 d->m_localizer = localizer;
222}
223
225{
226 Q_D(const Context);
227 return d->m_localizer;
228}
The Context class holds the context to render a Template with.
Definition context.h:107
void pop()
Pops the context.
Definition context.cpp:126
void push()
Pushes a new context.
Definition context.cpp:118
UrlType
The type of urls to external media that should be put in the template.
Definition context.h:229
@ AbsoluteUrls
Absolute URLs should be put in the template.
Definition context.h:230
UrlType urlType() const
The type of URL used in the template.
Definition context.cpp:190
~Context()
Destructor.
Definition context.cpp:80
RenderContext * renderContext() const
Returns a modifiable RenderContext.
Definition context.cpp:208
QString relativeMediaPath() const
The relative path to external media to be used in templates.
Definition context.cpp:202
Context()
Creates an empty context.
Definition context.cpp:51
QSharedPointer< AbstractLocalizer > localizer() const
Returns the localizer currently in use.
Definition context.cpp:224
void setUrlType(UrlType type)
Sets the type of external media URL to be used in the template to type.
Definition context.cpp:184
void setLocalizer(QSharedPointer< AbstractLocalizer > localizer)
Sets the localizer to be used.
Definition context.cpp:214
void insert(const QString &name, QObject *object)
Insert the context object object identified by name into the Context.
Definition context.cpp:140
Context & operator=(const Context &other)
Assignmant operator.
Definition context.cpp:67
QVariant lookup(const QString &str) const
Returns the context object identified by the key str.
Definition context.cpp:97
void setRelativeMediaPath(const QString &relativePath)
Sets the relative path to external media to be used in templates to relativePath.
Definition context.cpp:196
QList< std::pair< QString, QString > > externalMedia() const
Returns the external media encountered in the Template while rendering.
Definition context.cpp:172
Provides storage facility for state while rendering a template.
The KTextTemplate namespace holds all public KTextTemplate API.
Definition Mainpage.dox:8
KTextTemplate::SafeString getSafeString(const QVariant &input)
Retrieves and returns a SafeString from the input.
Definition util.cpp:91
void append(QList< T > &&value)
QVariant fromValue(T &&value)
Q_D(Todo)
Utility functions used throughout KTextTemplate.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:17 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.