Bibletime – a bible study tool
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cdisplaytemplatemgr.cpp 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // C++ Implementation: cdisplaytemplatemgr
  3. //
  4. // Description:
  5. //
  6. //
  7. // Author: The BibleTime team <info@bibletime.info>, (C) 2004
  8. //
  9. // Copyright: See COPYING file that comes with this distribution
  10. //
  11. //
  12. //BibleTime includes
  13. #include "cdisplaytemplatemgr.h"
  14. #include "cswordmoduleinfo.h"
  15. #include "clanguagemgr.h"
  16. #include "frontend/cbtconfig.h"
  17. #include "util/cpointers.h"
  18. #include "config.h"
  19. //Qt includes
  20. #include <tqstringlist.h>
  21. #include <tqfile.h>
  22. #include <tqfileinfo.h>
  23. //KDE includes
  24. #include <klocale.h>
  25. #include <kstandarddirs.h>
  26. #include <iostream>
  27. CDisplayTemplateMgr::CDisplayTemplateMgr() {
  28. init();
  29. loadUserTemplates();
  30. }
  31. CDisplayTemplateMgr::~CDisplayTemplateMgr() {}
  32. /*!
  33. \fn CDisplayTemplateMgr::fillTemplate( const TQString& name, const TQString& title, const TQString& content )
  34. */
  35. const TQString CDisplayTemplateMgr::fillTemplate( const TQString& name, const TQString& content, Settings& settings ) {
  36. const TQString templateName = m_templateMap.contains(name) ? name : defaultTemplate();
  37. TQString displayTypeString;
  38. if (!settings.pageCSS_ID.isEmpty()) {
  39. displayTypeString = settings.pageCSS_ID;
  40. }
  41. else {
  42. if (settings.modules.count()) {
  43. switch (settings.modules.first()->type()) {
  44. case CSwordModuleInfo::Bible:
  45. displayTypeString = "bible";
  46. break;
  47. case CSwordModuleInfo::GenericBook:
  48. displayTypeString = "book";
  49. break;
  50. case CSwordModuleInfo::Commentary:
  51. case CSwordModuleInfo::Lexicon:
  52. default:
  53. displayTypeString = "singleentry";
  54. break;
  55. };
  56. }
  57. else { //use bible as default type if no modules are set
  58. displayTypeString = "bible";
  59. };
  60. }
  61. TQString newContent = content;
  62. const int moduleCount = settings.modules.count();
  63. if (moduleCount >= 2) {
  64. //create header for the modules
  65. TQString header;
  66. ListCSwordModuleInfo::iterator end_it = settings.modules.end();
  67. for (ListCSwordModuleInfo::iterator it(settings.modules.begin()); it != end_it; ++it) {
  68. header.append("<th style=\"width:")
  69. .append(TQString::number(int( 100.0 / (float)moduleCount )))
  70. .append("%;\">")
  71. .append((*it)->name())
  72. .append("</th>");
  73. }
  74. newContent.setLatin1("<table><tr>")
  75. .append(header)
  76. .append("</tr>")
  77. .append(content)
  78. .append("</table>");
  79. }
  80. TQString langCSS;
  81. CLanguageMgr::LangMap langMap = CPointers::languageMgr()->availableLanguages();
  82. for ( CLanguageMgr::LangMapIterator it( langMap ); it.current(); ++it ) {
  83. const CLanguageMgr::Language* lang = it.current();
  84. //if (lang->isValid() && CBTConfig::get(lang).first) {
  85. if (!lang->abbrev().isEmpty() && CBTConfig::get(lang).first) {
  86. const TQFont f = CBTConfig::get(lang).second;
  87. //don't use important, because it would reset the title formatting, etc. to the setup font
  88. TQString css("{ ");
  89. css.append("font-family:").append(f.family())/*.append(" !important")*/;
  90. css.append("; font-size:").append(TQString::number(f.pointSize())).append("pt /*!important*/");
  91. css.append("; font-weight:").append(f.bold() ? "bold" : "normal /*!important*/");
  92. css.append("; font-style:").append(f.italic() ? "italic" : "normal /*!important*/");
  93. css.append("; }\n");
  94. langCSS +=
  95. TQString("\n*[lang=%1] %2")
  96. .arg(lang->abbrev())
  97. .arg(css);
  98. }
  99. }
  100. //at first append the font standard settings for all languages without configured font
  101. CLanguageMgr::LangMapIterator it( langMap );
  102. const CLanguageMgr::Language* lang = it.current();
  103. if (lang && !lang->abbrev().isEmpty()/*&& lang->isValid()*/) {
  104. const TQFont standardFont = CBTConfig::getDefault(lang); //we just need a dummy lang param
  105. langCSS.prepend(
  106. TQString("\n#content {font-family:%1; font-size:%2pt; font-weight:%3; font-style: %4;}\n")
  107. .arg(standardFont.family())
  108. .arg(standardFont.pointSize())
  109. .arg(standardFont.bold() ? "bold" : "normal")
  110. .arg(standardFont.italic() ? "italic" : "normal")
  111. );
  112. }
  113. // qWarning("Outputing unformated text");
  114. const TQString t = TQString(m_templateMap[ templateName ]) //don't change the map's content directly, use a copy
  115. .replace("#TITLE#", settings.title)
  116. .replace("#LANG_ABBREV#", settings.langAbbrev.isEmpty() ? TQString("en") : settings.langAbbrev)
  117. .replace("#DISPLAYTYPE#", displayTypeString)
  118. .replace("#LANG_CSS#", langCSS)
  119. .replace("#PAGE_DIRECTION#", settings.pageDirection)
  120. .replace("#CONTENT#", newContent);
  121. //printf("%s\n\n", t.latin1());
  122. return t;
  123. /* TQString(m_templateMap[ templateName ]) //don't change the map's content directly, use a copy
  124. .replace("#TITLE#", settings.title)
  125. .replace("#LANG_ABBREV#", settings.langAbbrev.isEmpty() ? TQString("en") : settings.langAbbrev)
  126. .replace("#DISPLAYTYPE#", displayTypeString)
  127. .replace("#LANG_CSS#", langCSS)
  128. .replace("#PAGE_DIRECTION#", settings.pageDirection)
  129. .replace("#CONTENT#", newContent);*/
  130. }
  131. /*!
  132. \fn CDisplayTemplateMgr::loadUserTemplates
  133. */
  134. void CDisplayTemplateMgr::loadUserTemplates() {
  135. qDebug("Loading user templates");
  136. TQStringList files = KGlobal::dirs()->findAllResources("BT_DisplayTemplates");
  137. for ( TQStringList::iterator it( files.begin() ); it != files.end(); ++it) {
  138. qDebug("Found user template %s", (*it).latin1());
  139. TQFile f( *it );
  140. Q_ASSERT( f.exists() );
  141. if (f.open( IO_ReadOnly )) {
  142. TQString fileContent = TQTextStream( &f ).read();
  143. if (!fileContent.isEmpty()) {
  144. m_templateMap[ TQFileInfo(*it).fileName() + TQString(" ") + i18n("(user template)")] = fileContent;
  145. }
  146. }
  147. }
  148. }
  149. //Include the HTML templates which were put into a cpp file by a Perl script
  150. #include "../display-templates/template-init.cpp"