~ubuntu-branches/ubuntu/jaunty/bibletime/jaunty

« back to all changes in this revision

Viewing changes to bibletime/backend/cdisplaytemplatemgr.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ralph Janke
  • Date: 2008-05-10 15:18:16 UTC
  • mfrom: (1.1.6 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080510151816-bqp8y1to705zd0fm
Tags: 1.6.5.1-1
* New upstream version (Closes: #441161, #271502)
* fixes for new autotools and gcc 4.3 (Closes: #407291)
* added poxml to Build-Depends
* No DFSG necessary anymore since biblestudy howto has 
  now Commons Licence 
* Added libclucene-dev to dev-depends (Closes: #436677)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
13
//BibleTime includes
 
14
#include "cdisplaytemplatemgr.h"
 
15
#include "cswordmoduleinfo.h"
 
16
#include "clanguagemgr.h"
 
17
 
 
18
#include "frontend/cbtconfig.h"
 
19
 
 
20
#include "util/cpointers.h"
 
21
 
 
22
#include "config.h"
 
23
 
 
24
//Qt includes
 
25
#include <qstringlist.h>
 
26
#include <qfile.h>
 
27
#include <qfileinfo.h>
 
28
 
 
29
//KDE includes
 
30
#include <klocale.h>
 
31
#include <kstandarddirs.h>
 
32
 
 
33
#include <iostream>
 
34
 
 
35
CDisplayTemplateMgr::CDisplayTemplateMgr() {
 
36
        init();
 
37
        loadUserTemplates();
 
38
}
 
39
 
 
40
CDisplayTemplateMgr::~CDisplayTemplateMgr() {}
 
41
 
 
42
 
 
43
/*!
 
44
    \fn CDisplayTemplateMgr::fillTemplate( const QString& name, const QString& title, const QString& content )
 
45
 */
 
46
const QString CDisplayTemplateMgr::fillTemplate( const QString& name, const QString& content, Settings& settings ) {
 
47
        const QString templateName = m_templateMap.contains(name) ? name : defaultTemplate();
 
48
 
 
49
        QString displayTypeString;
 
50
 
 
51
        if (!settings.pageCSS_ID.isEmpty()) {
 
52
                displayTypeString = settings.pageCSS_ID;
 
53
        }
 
54
        else {
 
55
                if (settings.modules.count()) {
 
56
                        switch (settings.modules.first()->type()) {
 
57
 
 
58
                                case CSwordModuleInfo::Bible:
 
59
                                displayTypeString = "bible";
 
60
                                break;
 
61
 
 
62
                                case CSwordModuleInfo::GenericBook:
 
63
                                displayTypeString = "book";
 
64
                                break;
 
65
 
 
66
                                case CSwordModuleInfo::Commentary:
 
67
 
 
68
                                case CSwordModuleInfo::Lexicon:
 
69
 
 
70
                                default:
 
71
                                displayTypeString = "singleentry";
 
72
                                break;
 
73
                        };
 
74
                }
 
75
                else { //use bible as default type if no modules are set
 
76
                        displayTypeString = "bible";
 
77
                };
 
78
        }
 
79
 
 
80
        QString newContent = content;
 
81
        const int moduleCount = settings.modules.count();
 
82
 
 
83
        if (moduleCount >= 2) {
 
84
                //create header for the modules
 
85
                QString header;
 
86
 
 
87
                ListCSwordModuleInfo::iterator end_it = settings.modules.end();
 
88
 
 
89
                for (ListCSwordModuleInfo::iterator it(settings.modules.begin()); it != end_it; ++it) {
 
90
                        header.append("<th style=\"width:")
 
91
                        .append(QString::number(int( 100.0 / (float)moduleCount )))
 
92
                        .append("%;\">")
 
93
                        .append((*it)->name())
 
94
                        .append("</th>");
 
95
                }
 
96
 
 
97
                newContent.setLatin1("<table><tr>")
 
98
                .append(header)
 
99
                .append("</tr>")
 
100
                .append(content)
 
101
                .append("</table>");
 
102
        }
 
103
 
 
104
        QString langCSS;
 
105
        CLanguageMgr::LangMap langMap = CPointers::languageMgr()->availableLanguages();
 
106
 
 
107
        for ( CLanguageMgr::LangMapIterator it( langMap ); it.current(); ++it ) {
 
108
                const CLanguageMgr::Language* lang = it.current();
 
109
 
 
110
 
 
111
                //if (lang->isValid() && CBTConfig::get(lang).first) {
 
112
                if (!lang->abbrev().isEmpty() && CBTConfig::get(lang).first) {
 
113
                        const QFont f = CBTConfig::get(lang).second;
 
114
 
 
115
                        //don't use important, because it would reset the title formatting, etc. to the setup font
 
116
                        QString css("{ ");
 
117
                        css.append("font-family:").append(f.family())/*.append(" !important")*/;
 
118
                        css.append("; font-size:").append(QString::number(f.pointSize())).append("pt /*!important*/");
 
119
                        css.append("; font-weight:").append(f.bold() ? "bold" : "normal /*!important*/");
 
120
                        css.append("; font-style:").append(f.italic() ? "italic" : "normal /*!important*/");
 
121
                        css.append("; }\n");
 
122
 
 
123
                        langCSS +=
 
124
                                QString("\n*[lang=%1] %2")
 
125
                                .arg(lang->abbrev())
 
126
                                .arg(css);
 
127
                }
 
128
        }
 
129
 
 
130
        //at first append the font standard settings for all languages without configured font
 
131
        CLanguageMgr::LangMapIterator it( langMap );
 
132
 
 
133
        const CLanguageMgr::Language* lang = it.current();
 
134
 
 
135
        if (lang && !lang->abbrev().isEmpty()/*&& lang->isValid()*/) {
 
136
                const QFont standardFont = CBTConfig::getDefault(lang); //we just need a dummy lang param
 
137
                langCSS.prepend(
 
138
                        QString("\n#content {font-family:%1; font-size:%2pt; font-weight:%3; font-style: %4;}\n")
 
139
                        .arg(standardFont.family())
 
140
                        .arg(standardFont.pointSize())
 
141
                        .arg(standardFont.bold() ? "bold" : "normal")
 
142
                        .arg(standardFont.italic() ? "italic" : "normal")
 
143
                );
 
144
        }
 
145
 
 
146
//      qWarning("Outputing unformated text");
 
147
        const QString t = QString(m_templateMap[ templateName ]) //don't change the map's content directly, use  a copy
 
148
                        .replace("#TITLE#", settings.title)
 
149
                        .replace("#LANG_ABBREV#", settings.langAbbrev.isEmpty() ? QString("en") : settings.langAbbrev)
 
150
                        .replace("#DISPLAYTYPE#", displayTypeString)
 
151
                        .replace("#LANG_CSS#", langCSS)
 
152
                        .replace("#PAGE_DIRECTION#", settings.pageDirection)
 
153
                        .replace("#CONTENT#", newContent);
 
154
         //printf("%s\n\n", t.latin1());
 
155
 
 
156
        return t;
 
157
/*                      QString(m_templateMap[ templateName ]) //don't change the map's content directly, use  a copy
 
158
                   .replace("#TITLE#", settings.title)
 
159
                   .replace("#LANG_ABBREV#", settings.langAbbrev.isEmpty() ? QString("en") : settings.langAbbrev)
 
160
                   .replace("#DISPLAYTYPE#", displayTypeString)
 
161
                   .replace("#LANG_CSS#", langCSS)
 
162
                   .replace("#PAGE_DIRECTION#", settings.pageDirection)
 
163
                   .replace("#CONTENT#", newContent);*/
 
164
}
 
165
 
 
166
 
 
167
/*!
 
168
    \fn CDisplayTemplateMgr::loadUserTemplates
 
169
 */
 
170
void CDisplayTemplateMgr::loadUserTemplates() {
 
171
        qDebug("Loading user templates");
 
172
        QStringList files = KGlobal::dirs()->findAllResources("BT_DisplayTemplates");
 
173
 
 
174
        for ( QStringList::iterator it( files.begin() ); it != files.end(); ++it) {
 
175
                qDebug("Found user template %s", (*it).latin1());
 
176
 
 
177
                QFile f( *it );
 
178
                Q_ASSERT( f.exists() );
 
179
 
 
180
                if (f.open( IO_ReadOnly )) {
 
181
                        QString fileContent = QTextStream( &f ).read();
 
182
 
 
183
                        if (!fileContent.isEmpty()) {
 
184
                                m_templateMap[ QFileInfo(*it).fileName() + QString(" ") + i18n("(user template)")] = fileContent;
 
185
                        }
 
186
                }
 
187
        }
 
188
}
 
189
 
 
190
//Include the HTML templates which were put into a cpp file by a Perl script
 
191
#include "../display-templates/template-init.cpp"