Marble

TemplateDocument.cpp
1 // SPDX-FileCopyrightText: 2012 Illya Kovalevskyy <[email protected]>
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 
14 namespace Marble
15 {
16 
17 class TemplateDocumentPrivate
18 {
19 public:
20  TemplateDocumentPrivate()
21  {
22  }
23  QString templateText;
24  QMap<QString, QString> templateEntries;
25  static void processTemplateIncludes(QString &input);
26 };
27 
28 void 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 
50 TemplateDocument::TemplateDocument() :
51  d(new TemplateDocumentPrivate)
52 {
53 }
54 
55 TemplateDocument::TemplateDocument(const QString &templateText) :
56  d(new TemplateDocumentPrivate)
57 {
58  setTemplate(templateText);
59 }
60 
61 TemplateDocument::~TemplateDocument()
62 {
63  delete d;
64 }
65 
66 void TemplateDocument::setTemplate(const QString &newTemplateText)
67 {
68  d->templateText = newTemplateText;
69 }
70 
72 {
73  return d->templateEntries[key];
74 }
75 
76 void TemplateDocument::setValue(const QString &key, const QString &value)
77 {
78  d->templateEntries[key] = value;
79 }
80 
82 {
83  return d->templateEntries[key];
84 }
85 
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
QString value(const QString &key) const
Returns the current template value of key.
QString finalText() const
Final proceed text.
void setValue(const QString &key, const QString &value)
Change set template value into new one.
Binds a QML item to a specific geodetic location in screen coordinates.
QString & replace(int position, int n, QChar after)
void setTemplate(const QString &newTemplateText)
Set template text.
QString & operator[](const QString &key)
Indexator for template values.
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:31
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Oct 4 2023 04:09:43 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.