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 <QMap>
8#include <QString>
9#include <QFile>
10#include <QRegExp>
11
12#include "MarbleDebug.h"
13
14namespace Marble
15{
16
17class TemplateDocumentPrivate
18{
19public:
20 TemplateDocumentPrivate()
21 {
22 }
23 QString templateText;
24 QMap<QString, QString> templateEntries;
25 static void processTemplateIncludes(QString &input);
26};
27
28void TemplateDocumentPrivate::processTemplateIncludes(QString &input)
29{
30 QRegExp rx("%!\\{([^}]*)\\}%");
31
32 QStringList includes;
33 int pos = 0;
34
35 while ((pos = rx.indexIn(input, pos)) != -1) {
36 includes << rx.cap(1);
37 pos += rx.matchedLength();
38 }
39
40 for (const QString &include: includes) {
41 QFile includeFile(QLatin1String(":/htmlfeatures/includes/") + include + QLatin1String(".inc"));
42 if (includeFile.open(QIODevice::ReadOnly)) {
43 input.replace(QLatin1String("%!{") + include + QLatin1String("}%"), includeFile.readAll());
44 } else {
45 mDebug() << "[WARNING] Can't process template include" << include;
46 }
47 }
48}
49
50TemplateDocument::TemplateDocument() :
51 d(new TemplateDocumentPrivate)
52{
53}
54
55TemplateDocument::TemplateDocument(const QString &templateText) :
56 d(new TemplateDocumentPrivate)
57{
58 setTemplate(templateText);
59}
60
61TemplateDocument::~TemplateDocument()
62{
63 delete d;
64}
65
66void TemplateDocument::setTemplate(const QString &newTemplateText)
67{
68 d->templateText = newTemplateText;
69}
70
71QString TemplateDocument::value(const QString &key) const
72{
73 return d->templateEntries[key];
74}
75
76void TemplateDocument::setValue(const QString &key, const QString &value)
77{
78 d->templateEntries[key] = value;
79}
80
81QString& TemplateDocument::operator[](const QString &key)
82{
83 return d->templateEntries[key];
84}
85
86QString TemplateDocument::finalText() const
87{
88 QString ready = d->templateText;
89 typedef QMap<QString, QString>::ConstIterator ConstIterator;
90 ConstIterator end = d->templateEntries.constEnd();
91 for (ConstIterator i = d->templateEntries.constBegin(); i != end; i++) {
92 ready.replace(QLatin1Char('%') + i.key() + QLatin1Char('%'), i.value());
93 }
94 d->processTemplateIncludes(ready);
95 return ready;
96}
97
98} // namespace Marble
Binds a QML item to a specific geodetic location in screen coordinates.
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 Fri Jun 21 2024 12:00:07 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.