Marble

TemplateDocument.cpp
1// SPDX-FileCopyrightText: 2012 Illya Kovalevskyy <illya.kovalevskyy@gmail.com>
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
5#include "TemplateDocument.h"
6
7#include <QFile>
8#include <QMap>
9#include <QRegExp>
10#include <QString>
11
12#include "MarbleDebug.h"
13
14namespace Marble
15{
16
17class TemplateDocumentPrivate
18{
19public:
20 TemplateDocumentPrivate() = default;
21 QString templateText;
22 QMap<QString, QString> templateEntries;
23 static void processTemplateIncludes(QString &input);
24};
25
26void TemplateDocumentPrivate::processTemplateIncludes(QString &input)
27{
28 QRegExp rx(QStringLiteral("%!\\{([^}]*)\\}%"));
29
30 QStringList includes;
31 int pos = 0;
32
33 while ((pos = rx.indexIn(input, pos)) != -1) {
34 includes << rx.cap(1);
35 pos += rx.matchedLength();
36 }
37
38 for (const QString &include : includes) {
39 QFile includeFile(QLatin1StringView(":/htmlfeatures/includes/") + include + QLatin1StringView(".inc"));
40 if (includeFile.open(QIODevice::ReadOnly)) {
41 input.replace(QLatin1StringView("%!{") + include + QLatin1StringView("}%"), QString::fromLatin1(includeFile.readAll()));
42 } else {
43 mDebug() << "[WARNING] Can't process template include" << include;
44 }
45 }
46}
47
48TemplateDocument::TemplateDocument()
49 : d(new TemplateDocumentPrivate)
50{
51}
52
53TemplateDocument::TemplateDocument(const QString &templateText)
54 : d(new TemplateDocumentPrivate)
55{
56 setTemplate(templateText);
57}
58
59TemplateDocument::~TemplateDocument()
60{
61 delete d;
62}
63
64void TemplateDocument::setTemplate(const QString &newTemplateText)
65{
66 d->templateText = newTemplateText;
67}
68
69QString TemplateDocument::value(const QString &key) const
70{
71 return d->templateEntries[key];
72}
73
74void TemplateDocument::setValue(const QString &key, const QString &value)
75{
76 d->templateEntries[key] = value;
77}
78
79QString &TemplateDocument::operator[](const QString &key)
80{
81 return d->templateEntries[key];
82}
83
84QString TemplateDocument::finalText() const
85{
86 QString ready = d->templateText;
87 typedef QMap<QString, QString>::ConstIterator ConstIterator;
88 ConstIterator end = d->templateEntries.constEnd();
89 for (ConstIterator i = d->templateEntries.constBegin(); i != end; i++) {
90 ready.replace(QLatin1Char('%') + i.key() + QLatin1Char('%'), i.value());
91 }
92 d->processTemplateIncludes(ready);
93 return ready;
94}
95
96} // namespace Marble
Binds a QML item to a specific geodetic location in screen coordinates.
ConstIterator
QString fromLatin1(QByteArrayView str)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:15:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.