23 #include <QStringList>
24 #include <QTextStream>
26 #include "../../util/btassert.h"
27 #include "../../util/directory.h"
28 #include "../config/btconfig.h"
29 #include "../drivers/btmodulelist.h"
30 #include "../drivers/cswordmoduleinfo.h"
31 #include "../language.h"
35 #define CSSTEMPLATEBASE "Basic.tmpl"
41 return f.open(QIODevice::ReadOnly) ? QTextStream(&f).readAll() : QString();
51 m_multiModuleHeaders(true) {
57 const QDir::Filters readableFileFilter(QDir::Files | QDir::Readable);
63 const QStringList
filter(QStringLiteral(
"*.tmpl"));
65 for (
auto const & file : td.entryList(
filter, readableFileFilter))
68 for (
auto const & file : utd.entryList(
filter, readableFileFilter))
73 errorMessage = QObject::tr(
"CSS base template not found!");
79 const QStringList cssfilter(QStringLiteral(
"*.css"));
81 for (
auto const & file : td.entryList(cssfilter, readableFileFilter))
84 for (
auto const & file : utd.entryList(cssfilter, readableFileFilter))
90 errorMessage = QObject::tr(
"Default template \"%1\" not found!")
103 errorMessage = QString();
107 const QString & content,
111 BT_ASSERT(name.endsWith(QStringLiteral(
".css"))
112 || name.endsWith(QStringLiteral(
".tmpl")));
113 BT_ASSERT(!name.endsWith(QStringLiteral(
".css"))
115 BT_ASSERT(!name.endsWith(QStringLiteral(
".tmpl"))
117 const bool templateIsCss = name.endsWith(QStringLiteral(
".css"));
119 QString displayTypeString;
126 if (settings.
modules.count()) {
127 switch (settings.
modules.first()->type()) {
130 displayTypeString = QStringLiteral(
"bible");
134 displayTypeString = QStringLiteral(
"book");
140 displayTypeString = QStringLiteral(
"singleentry");
143 moduleName = settings.
modules.first()->name();
146 displayTypeString = QStringLiteral(
"bible");
150 QString newContent = content;
151 const int moduleCount = settings.
modules.count();
158 for (
auto const *
const mi : settings.
modules)
160 QStringLiteral(
"<th style=\"width:%1%;\">%2</th>")
161 .arg(QString::number(
static_cast<int>(100.0 / moduleCount)),
164 newContent = QStringLiteral(
"<table><tr>%1</tr>%2</table>")
165 .arg(header, content);
172 QStringLiteral(
"#content{font-family:%1;font-size:%2pt;"
173 "font-weight:%3;font-style:%4}")
175 QString::number(f.pointSizeF(),
'f'),
177 ? QStringLiteral(
"bold")
178 : QStringLiteral(
"normal"),
180 ? QStringLiteral(
"italic")
181 : QStringLiteral(
"normal")));
184 auto const availableLanguages =
187 for (
auto const & lang : *availableLanguages) {
188 if (lang->abbrev().isEmpty())
193 const QFont & f = fp.second;
199 auto const fontWeight = 1.0 + (f.weight() * 999.0) / 99.0;
201 auto const fontStyleString =
203 switch ((
int) f.style()) {
204 case QFont::StyleItalic:
205 return QStringLiteral(
"italic");
206 case QFont::StyleOblique:
207 return QStringLiteral(
"oblique");
208 case QFont::StyleNormal:
210 return QStringLiteral(
"normal");
214 auto const textDecorationString =
217 ? QStringLiteral(
"underline line-through")
218 : QStringLiteral(
"underline"))
220 ? QStringLiteral(
"line-through")
221 : QStringLiteral(
"none"));
226 QStringLiteral(
"*[lang=%1]{font-family:%2;"
227 "font-size:%3pt;font-weight:%4;"
228 "font-style:%5;text-decoration:%6}")
231 QString::number(f.pointSizeF(),
'f'),
232 QString::number(fontWeight),
234 textDecorationString));
243 output.replace(QStringLiteral(
"#TITLE#"), settings.
title)
244 .replace(QStringLiteral(
"#LANG_ABBREV#"), settings.
langAbbrev)
245 .replace(QStringLiteral(
"#DISPLAYTYPE#"), displayTypeString)
246 .replace(QStringLiteral(
"#LANG_CSS#"), langCSS)
247 .replace(QStringLiteral(
"#PAGE_DIRECTION#"),
249 .replace(QStringLiteral(
"#CONTENT#"), newContent)
250 .replace(QStringLiteral(
"#BODY_CLASSES#"),
251 QStringLiteral(
"%1 %1_%2").arg(displayTypeString,
253 .replace(QStringLiteral(
"#DISPLAY_TEMPLATES_PATH#"),
257 output.replace(QStringLiteral(
"#THEME_STYLE#"),
m_cssMap[name]);
264 btConfig().
value<QString>(QStringLiteral(
"GUI/activeTemplateName"),
273 BT_ASSERT(filename.endsWith(QStringLiteral(
".tmpl")));
276 if (!templateString.isEmpty())
277 m_templateMap.insert(QFileInfo(filename).fileName(), templateString);
281 BT_ASSERT(filename.endsWith(QStringLiteral(
".css")));
282 const QFileInfo fi(filename);
BtConfig & btConfig()
This is a shortchand for BtConfig::getInstance().
T value(QString const &key, T const &defaultValue=T()) const
Returns the settings value for the given global key.
QPair< bool, QFont > FontSettingsPair
QFont const & getDefaultFont() const
FontSettingsPair getFontForLanguage(Language const &language)
Get font for a language.
CDisplayTemplateMgr(QString &errorMessage)
void setMultiModuleHeadersVisible(bool visible)
Turn on or off displaying module names during parallel text rendering.
QString fillTemplate(const QString &name, const QString &content, const Settings &settings) const
Fills the template.
static QString activeTemplateName()
QHash< QString, QString > m_templateMap
void loadCSSTemplate(const QString &filename)
bool m_multiModuleHeaders
QHash< QString, QString > m_cssMap
static CDisplayTemplateMgr * m_instance
static CDisplayTemplateMgr * instance()
void loadTemplate(const QString &filename)
QStringList m_availableTemplateNamesCache
std::shared_ptr< AvailableLanguagesCacheContainer const > availableLanguages() noexcept
static CSwordBackend & instance() noexcept
bool filter(WizardTaskType const taskType, QStringList const &languages, CSwordModuleInfo const *const mInfo)
auto const defaultTemplateName
QString readFileToString(const QString &filename)
const QDir & getDisplayTemplatesDir()
const QDir & getUserDisplayTemplatesDir()
BtConstModuleList modules
char const * textDirectionAsHtmlDirAttr() const noexcept