summaryrefslogtreecommitdiff
path: root/src/frontend/cprinter.cpp
blob: 0a05a559c306ceb45ee6bddede53c4273c04a7fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2009 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "cprinter.h"

#include "backend/managers/cdisplaytemplatemgr.h"
#include "backend/keys/cswordversekey.h"
#include "util/cpointers.h"

#include <QWebPage>
#include <QWebFrame>
#include <QPrinter>
#include <QPrintDialog>

namespace Printing {

CPrinter::CPrinter(QObject*, CSwordBackend::DisplayOptions displayOptions, CSwordBackend::FilterOptions filterOptions)
        :	QObject(0),
        CDisplayRendering(displayOptions, filterOptions),
        m_htmlPage(new QWebPage()) {
    m_htmlPage->setParent(this);

    //override the filteroptions set in the c-tor of CDisplayRendering
    m_filterOptions.footnotes = false;
    m_filterOptions.scriptureReferences = false;
    m_filterOptions.strongNumbers = false;
    m_filterOptions.morphTags = false;
    m_filterOptions.headings = false;
}

CPrinter::~CPrinter() {
    delete m_htmlPage;
    m_htmlPage = 0;
}

void CPrinter::printKeyTree( KeyTree& tree ) {
    m_htmlPage->mainFrame()->setHtml(renderKeyTree(tree));

    QPrinter printer;
    QPrintDialog printDialog(&printer);
    if (printDialog.exec() == QDialog::Accepted) {
        m_htmlPage->mainFrame()->print(&printer);
    }
}

const QString CPrinter::entryLink(const KeyTreeItem& item, CSwordModuleInfo* module) {
    Q_ASSERT(module);
    if (module->type() == CSwordModuleInfo::Bible) {
        CSwordVerseKey vk(module);
        vk.key(item.key());
        switch (item.settings().keyRenderingFace) {
            case KeyTreeItem::Settings::CompleteShort:
                return QString::fromUtf8(vk.getShortText());

            case KeyTreeItem::Settings::CompleteLong:
                return vk.key();

            case KeyTreeItem::Settings::NoKey:
                return QString::null;

            case KeyTreeItem::Settings::SimpleKey: //fall through
            default:
                return QString::number(vk.Verse());
        }
    }
    return item.key();
}

const QString CPrinter::renderEntry( const KeyTreeItem& i, CSwordKey* ) {
    const CPrinter::KeyTreeItem* printItem = dynamic_cast<const CPrinter::KeyTreeItem*>(&i);
    Q_ASSERT(printItem);

    if (printItem && printItem->hasAlternativeContent()) {
        QString ret = QString::fromLatin1("<div class=\"entry\"><div class=\"rangeheading\">%1</div>").arg(printItem->getAlternativeContent());

        if (!i.childList()->isEmpty()) {
            KeyTree const * tree = i.childList();

            foreach ( KeyTreeItem* c, (*tree)) {
                ret.append( CDisplayRendering::renderEntry( *c ) );
            }
        }

        ret.append("</div>");
        return ret;
    }
    return CDisplayRendering::renderEntry(i);
}

const QString CPrinter::finishText(const QString& text, KeyTree& tree) {
    QList<CSwordModuleInfo*> modules = collectModules(&tree);
    Q_ASSERT(modules.count() > 0);

    const CLanguageMgr::Language* const lang = modules.first()->language();
    Q_ASSERT(lang);

    CDisplayTemplateMgr::Settings settings;
    //settings.modules = modules;
    settings.pageCSS_ID = "printer";
    settings.langAbbrev = ( lang && (modules.count() == 1) && lang->isValid() ) ? lang->abbrev() : "unknown";

    //the previous version gave compiler error for some strange reason
    //(well, I don't like ?: anyway, let alone nested)
    if (modules.count() != 1) {
        settings.pageDirection = QString::null;
    }
    else {
        settings.pageDirection = ( modules.first()->textDirection() == CSwordModuleInfo::LeftToRight ) ? "ltr" : "rtl";
    }

    CDisplayTemplateMgr* tMgr = CPointers::displayTemplateManager();
    return tMgr->fillTemplate(CBTConfig::get(CBTConfig::displayStyle), text, settings);
}

} //end of namespace