~ubuntu-branches/ubuntu/trusty/bibletime/trusty

« back to all changes in this revision

Viewing changes to src/backend/rendering/cbookdisplay.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Marsden
  • Date: 2009-11-18 17:30:00 UTC
  • mfrom: (1.3.4 upstream) (5.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20091118173000-endkhjz5qai88tvr
Tags: 2.4-1
* New upstream version 2.4
* debian/control: 
  - Replace incorrect bibletime-data Depends on lib4qt-gui
    with bibletime Depends on libqtgui4 (>= 4.4.0). (Closes: #556209).
  - Add Build-depends: on zlib1g-dev and libcurl4-gnutls-dev 
    (Closes: #556805).

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
*
8
8
**********/
9
9
 
10
 
//Backend
11
 
#include "cbookdisplay.h"
12
 
#include "cdisplayrendering.h"
 
10
#include "backend/rendering/cbookdisplay.h"
 
11
 
 
12
#include <boost/scoped_ptr.hpp>
 
13
#include <QtAlgorithms>
 
14
 
13
15
#include "backend/drivers/cswordbookmoduleinfo.h"
14
16
#include "backend/keys/cswordtreekey.h"
 
17
#include "backend/rendering/cdisplayrendering.h"
15
18
 
16
 
//Util
17
 
#include <boost/scoped_ptr.hpp>
18
19
 
19
20
/** Returns the rendered text using the modules in the list and using the key parameter. The displayoptions and filter options are used, too. */
20
21
const QString Rendering::CBookDisplay::text( const QList<CSwordModuleInfo*>& modules, const QString& keyName, const CSwordBackend::DisplayOptions displayOptions, const CSwordBackend::FilterOptions filterOptions ) {
21
 
        CSwordBookModuleInfo* book = dynamic_cast<CSwordBookModuleInfo*>(modules.first());
22
 
        Q_ASSERT(book);
23
 
 
24
 
        CSwordBackend::DisplayOptions dOpts = displayOptions;
25
 
        dOpts.lineBreaks = true; //books should render with blocks, not with inlined sections
26
 
 
27
 
        CDisplayRendering render(dOpts, filterOptions);
28
 
        CDisplayRendering::KeyTree tree;
29
 
        CDisplayRendering::KeyTreeItem::Settings itemSettings;
30
 
 
31
 
        // the number of levels which should be display together, 1 means display no entries together
32
 
        int displayLevel = book->config( CSwordModuleInfo::DisplayLevel ).toInt();
33
 
 
34
 
        boost::scoped_ptr<CSwordTreeKey> key (
35
 
                dynamic_cast<CSwordTreeKey*>( CSwordKey::createInstance(book) )
36
 
        );
37
 
        key->key(keyName); //set the key to position we'd like to get
38
 
 
39
 
        const unsigned long offset = key->getOffset();
40
 
 
41
 
        // standard of DisplayLevel, display nothing together
42
 
        // if the current key is the root entry don't display anything together!
43
 
 
44
 
        if ((displayLevel <= 1) || (key->key().isEmpty() || (key->key() == "/") )) {
45
 
                tree.append( new CDisplayRendering::KeyTreeItem( key->key(), modules, itemSettings ) );
46
 
 
47
 
                const QString renderedText = render.renderKeyTree(tree);
48
 
                key->setOffset( offset );
49
 
                return renderedText;
50
 
        };
51
 
 
52
 
        /**
53
 
        * Check whether displaying displayLevel levels together is possible.
54
 
        * For this count the childs and parents
55
 
        * of the required position
56
 
        */
57
 
 
58
 
        int possibleLevels = 1; //we start with the default value of displayLevel, which means no entries together
59
 
 
60
 
        while( key->parent() && (key->key() != "/") && !key->key().isEmpty() ) {//add parents
61
 
                ++possibleLevels;
62
 
        };
63
 
 
64
 
        //   key->key(keyName); //set the key to the start position
65
 
 
66
 
        key->setOffset( offset );
67
 
 
68
 
        while( key->firstChild( )) { //add childs
69
 
                ++possibleLevels;
70
 
        };
71
 
 
72
 
        if (possibleLevels < displayLevel) { //too few levels available!
73
 
                //display current level, we could also decide to display the available levels together
74
 
                tree.append( new CDisplayRendering::KeyTreeItem( key->key(), modules, itemSettings ) );
75
 
 
76
 
                const QString renderedText = render.renderKeyTree(tree);
77
 
                key->setOffset( offset );
78
 
                return renderedText;
79
 
        };
80
 
 
81
 
        if ((displayLevel > 2) && (displayLevel == possibleLevels)) { //fix not to diplay the whole module
82
 
                --displayLevel;
83
 
        }
84
 
 
85
 
        // at this point we're sure that we can display the required levels toogether
86
 
        // at the moment we're at the lowest level, so we only have to go up!
87
 
        for (int currentLevel = 1; currentLevel < displayLevel; ++currentLevel) { //we start again with 1 == standard of displayLevel
88
 
 
89
 
                if ( !key->parent() ) { //something went wrong although we checked before! Be safe and return entry's text
90
 
                        tree.append( new CDisplayRendering::KeyTreeItem( key->key(), modules, itemSettings ) );
91
 
 
92
 
                        const QString renderedText = render.renderKeyTree(tree);
93
 
                        key->setOffset( offset );
94
 
                        return renderedText;
95
 
                };
96
 
        };
97
 
 
98
 
        // no we can display all sub levels together! We checked before that this is possible!
99
 
        itemSettings.highlight = (key->key() == keyName);
100
 
 
101
 
        tree.append( new CDisplayRendering::KeyTreeItem( key->key(), modules, itemSettings ) );
102
 
 
103
 
        //const bool hasToplevelText = !key->strippedText().isEmpty();
104
 
        key->firstChild(); //go to the first sibling on the same level
105
 
 
106
 
        setupRenderTree(key.get(), &tree, keyName);
107
 
 
108
 
        const QString renderedText = render.renderKeyTree(tree);
109
 
 
110
 
        key->setOffset( offset ); //restore key
111
 
 
112
 
        return renderedText;
 
22
    CSwordBookModuleInfo* book = dynamic_cast<CSwordBookModuleInfo*>(modules.first());
 
23
    Q_ASSERT(book);
 
24
 
 
25
    CSwordBackend::DisplayOptions dOpts = displayOptions;
 
26
    dOpts.lineBreaks = true; //books should render with blocks, not with inlined sections
 
27
 
 
28
    CDisplayRendering render(dOpts, filterOptions);
 
29
    CDisplayRendering::KeyTree tree;
 
30
    CDisplayRendering::KeyTreeItem::Settings itemSettings;
 
31
 
 
32
    // the number of levels which should be display together, 1 means display no entries together
 
33
    int displayLevel = book->config( CSwordModuleInfo::DisplayLevel ).toInt();
 
34
 
 
35
    boost::scoped_ptr<CSwordTreeKey> key (
 
36
        dynamic_cast<CSwordTreeKey*>( CSwordKey::createInstance(book) )
 
37
    );
 
38
    key->key(keyName); //set the key to position we'd like to get
 
39
 
 
40
    const unsigned long offset = key->getOffset();
 
41
 
 
42
    // standard of DisplayLevel, display nothing together
 
43
    // if the current key is the root entry don't display anything together!
 
44
 
 
45
    if ((displayLevel <= 1) || (key->key().isEmpty() || (key->key() == "/") )) {
 
46
        tree.append( new CDisplayRendering::KeyTreeItem( key->key(), modules, itemSettings ) );
 
47
 
 
48
        const QString renderedText = render.renderKeyTree(tree);
 
49
        key->setOffset( offset );
 
50
 
 
51
        qDeleteAll(tree);      // Dispose of the heap allocated objects pointed to in tree.
 
52
        return renderedText;
 
53
    };
 
54
 
 
55
    /**
 
56
    * Check whether displaying displayLevel levels together is possible.
 
57
    * For this count the childs and parents
 
58
    * of the required position
 
59
    */
 
60
 
 
61
    int possibleLevels = 1; //we start with the default value of displayLevel, which means no entries together
 
62
 
 
63
    while ( key->parent() && (key->key() != "/") && !key->key().isEmpty() ) {//add parents
 
64
        ++possibleLevels;
 
65
    };
 
66
 
 
67
    //   key->key(keyName); //set the key to the start position
 
68
 
 
69
    key->setOffset( offset );
 
70
 
 
71
    while ( key->firstChild( )) { //add childs
 
72
        ++possibleLevels;
 
73
    };
 
74
 
 
75
    if (possibleLevels < displayLevel) { //too few levels available!
 
76
        //display current level, we could also decide to display the available levels together
 
77
        tree.append( new CDisplayRendering::KeyTreeItem( key->key(), modules, itemSettings ) );
 
78
 
 
79
        const QString renderedText = render.renderKeyTree(tree);
 
80
        key->setOffset( offset );
 
81
        qDeleteAll(tree);      // Dispose of the heap allocated objects pointed to in tree.
 
82
        return renderedText;
 
83
    };
 
84
 
 
85
    if ((displayLevel > 2) && (displayLevel == possibleLevels)) { //fix not to diplay the whole module
 
86
        --displayLevel;
 
87
    }
 
88
 
 
89
    // at this point we're sure that we can display the required levels toogether
 
90
    // at the moment we're at the lowest level, so we only have to go up!
 
91
    for (int currentLevel = 1; currentLevel < displayLevel; ++currentLevel) { //we start again with 1 == standard of displayLevel
 
92
 
 
93
        if ( !key->parent() ) { //something went wrong although we checked before! Be safe and return entry's text
 
94
            tree.append( new CDisplayRendering::KeyTreeItem( key->key(), modules, itemSettings ) );
 
95
 
 
96
            const QString renderedText = render.renderKeyTree(tree);
 
97
            key->setOffset( offset );
 
98
                     qDeleteAll(tree);      // Dispose of the heap allocated objects pointed to in tree.
 
99
            return renderedText;
 
100
        };
 
101
    };
 
102
 
 
103
    // no we can display all sub levels together! We checked before that this is possible!
 
104
    itemSettings.highlight = (key->key() == keyName);
 
105
 
 
106
    tree.append( new CDisplayRendering::KeyTreeItem( key->key(), modules, itemSettings ) );
 
107
 
 
108
    //const bool hasToplevelText = !key->strippedText().isEmpty();
 
109
    key->firstChild(); //go to the first sibling on the same level
 
110
 
 
111
    setupRenderTree(key.get(), &tree, keyName);
 
112
 
 
113
    const QString renderedText = render.renderKeyTree(tree);
 
114
 
 
115
    key->setOffset( offset ); //restore key
 
116
 
 
117
    qDeleteAll(tree);      // Dispose of the heap allocated objects pointed to in tree.
 
118
    return renderedText;
113
119
}
114
120
 
115
121
void Rendering::CBookDisplay::setupRenderTree(CSwordTreeKey * swordTree, CTextRendering::KeyTree * renderTree, const QString& highlightKey) {
116
122
 
117
 
        const QString key = swordTree->key();
118
 
        const unsigned long offset = swordTree->getOffset();
119
 
 
120
 
        CTextRendering::KeyTreeItem::Settings settings;
121
 
        settings.highlight = (key == highlightKey);
122
 
 
123
 
        CTextRendering::KeyTreeItem* item = new CTextRendering::KeyTreeItem(key, swordTree->module(0), settings );
124
 
        renderTree->append( item );
125
 
 
126
 
        if (swordTree->hasChildren()) { //print tree for the child items
127
 
                swordTree->firstChild();
128
 
                setupRenderTree(swordTree, item->childList(), highlightKey);
129
 
                swordTree->setOffset( offset ); //go back where we came from
130
 
        }
131
 
 
132
 
        if (swordTree->nextSibling()) { //print tree for next entry on the same depth
133
 
                setupRenderTree(swordTree, renderTree, highlightKey);
134
 
                swordTree->setOffset( offset ); //return to the value we had at the beginning of this block!
135
 
        }
 
123
    const QString key = swordTree->key();
 
124
    const unsigned long offset = swordTree->getOffset();
 
125
 
 
126
    CTextRendering::KeyTreeItem::Settings settings;
 
127
    settings.highlight = (key == highlightKey);
 
128
 
 
129
    CTextRendering::KeyTreeItem* item = new CTextRendering::KeyTreeItem(key, swordTree->module(0), settings );
 
130
    renderTree->append( item );
 
131
 
 
132
    if (swordTree->hasChildren()) { //print tree for the child items
 
133
        swordTree->firstChild();
 
134
        setupRenderTree(swordTree, item->childList(), highlightKey);
 
135
        swordTree->setOffset( offset ); //go back where we came from
 
136
    }
 
137
 
 
138
    if (swordTree->nextSibling()) { //print tree for next entry on the same depth
 
139
        setupRenderTree(swordTree, renderTree, highlightKey);
 
140
        swordTree->setOffset( offset ); //return to the value we had at the beginning of this block!
 
141
    }
136
142
}