summaryrefslogtreecommitdiff
path: root/src/frontend/searchdialog/analysis/csearchanalysisscene.cpp
blob: 6d486a816c9997135cd264158ffb8b6e5c18d213 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2008 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "frontend/searchdialog/analysis/csearchanalysisscene.h"

#include <QApplication>
#include <QFileDialog>
#include <QHashIterator>
#include <QTextCodec>
#include "backend/keys/cswordversekey.h"
#include "frontend/searchdialog/analysis/csearchanalysisitem.h"
#include "frontend/searchdialog/analysis/csearchanalysislegenditem.h"
#include "frontend/searchdialog/csearchdialog.h"
#include "util/tool.h"


namespace Search {

const int SPACE_BETWEEN_PARTS = 5;
const int RIGHT_BORDER = 15;
const int LEFT_BORDER = 15;
const int LOWER_BORDER = 10;
const int UPPER_BORDER = 10;

const int ITEM_TEXT_SIZE = 8;
const int LABEL_TEXT_SIZE = 6;

//used for the shift between the bars
const int BAR_DELTAX = 4;
const int BAR_DELTAY = 2;
const int BAR_WIDTH  = 2 + (2*BAR_DELTAX);  //should be equal or bigger than the label font size
// Used for the text below the bars
const int BAR_LOWER_BORDER = 90;

const int LEGEND_INNER_BORDER = 5;
const int LEGEND_DELTAY = 4;
const int LEGEND_WIDTH = 85;


CSearchAnalysisScene::CSearchAnalysisScene(QObject *parent )
        : QGraphicsScene(parent),
        m_scaleFactor(0.0),
        m_legend(0) {
    setBackgroundBrush(QBrush(Qt::white));
    setSceneRect(0, 0, 1, 1);
}


QHash<QString, CSearchAnalysisItem*>* CSearchAnalysisScene::getSearchAnalysisItemList() {
    // Returns pointer to the search analysis items
    return &m_itemList;
}

/** Starts the analysis of the search result. This should be called only once because QCanvas handles the updates automatically. */
void CSearchAnalysisScene::analyse(QList<CSwordModuleInfo*> modules) {
    /**
    * Steps of analysing our search result;
    * -Create the items for all available books ("Genesis" - "Revelation")
    * -Iterate through all modules we analyse
    *  -Go through all books of this module
    *   -Find out how many times we found the book
    *   -Set the count to the items which belongs to the book
    */
    setModules(modules);

    m_lastPosList.clear();
    const int numberOfModules = m_moduleList.count();
    if (!numberOfModules)
        return;
    m_legend = new CSearchAnalysisLegendItem(&m_moduleList);
    addItem(m_legend);
    m_legend->setRect(LEFT_BORDER, UPPER_BORDER,
                      LEGEND_WIDTH, LEGEND_INNER_BORDER*2 + ITEM_TEXT_SIZE*numberOfModules + LEGEND_DELTAY*(numberOfModules - 1) );
    m_legend->show();

    int xPos = (int)(LEFT_BORDER + m_legend->rect().width() + SPACE_BETWEEN_PARTS);
    int moduleIndex = 0;
    m_maxCount = 0;
    int count = 0;
    CSwordVerseKey key(0);
    key.key("Genesis 1:1");

    CSearchAnalysisItem* analysisItem = m_itemList[key.book()];
    bool ok = true;
    while (ok && analysisItem) {
        moduleIndex = 0;
        QList<CSwordModuleInfo*>::iterator end_it = m_moduleList.end();
        for (QList<CSwordModuleInfo*>::iterator it(m_moduleList.begin()); it != end_it; ++it) {
            qApp->processEvents( QEventLoop::AllEvents );
            if (!m_lastPosList.contains(*it)) {
                m_lastPosList.insert(*it, 0);
            }

            analysisItem->setCountForModule(moduleIndex, (count = getCount(key.book(), *it)));
            m_maxCount = (count > m_maxCount) ? count : m_maxCount;

            ++moduleIndex;
        }
        if (analysisItem->hasHitsInAnyModule()) {
            analysisItem->setRect(xPos, UPPER_BORDER, analysisItem->rect().width(), analysisItem->rect().height());
            QString tip = analysisItem->getToolTip();
            analysisItem->setToolTip(tip);
            analysisItem->show();
            xPos += (int)analysisItem->width() + SPACE_BETWEEN_PARTS;
        }
        ok = key.next(CSwordVerseKey::UseBook);
        analysisItem = m_itemList[key.book()];
    }
    setSceneRect(0, 0, xPos + BAR_WIDTH + (m_moduleList.count() - 1)*BAR_DELTAX + RIGHT_BORDER, height() );
    slotResized();
}

/** Sets the module list used for the analysis. */
void CSearchAnalysisScene::setModules(QList<CSwordModuleInfo*> modules) {
    m_moduleList.clear();
    foreach (CSwordModuleInfo * mod, modules) {
        if ( (mod->type() == CSwordModuleInfo::Bible) || (mod->type() == CSwordModuleInfo::Commentary) ) { //a Bible or an commentary
            m_moduleList.append(mod);
        }
    }

    m_itemList.clear();
    CSearchAnalysisItem* analysisItem = 0;
    CSwordVerseKey key(0);
    key.key("Genesis 1:1");
    do {
        analysisItem = new CSearchAnalysisItem(m_moduleList.count(), key.book(), &m_scaleFactor, &m_moduleList);
        addItem(analysisItem);
        analysisItem->hide();
        m_itemList.insert(key.book(), analysisItem);
    }
    while (key.next(CSwordVerseKey::UseBook));
    update();
}

/** Sets back the items and deletes things to cleanup */
void CSearchAnalysisScene::reset() {
    m_scaleFactor = 0.0;

    QHashIterator<QString, CSearchAnalysisItem*> it( m_itemList ); // iterator for items
    while ( it.hasNext() ) {
        it.next();
        if (it.value()) it.value()->hide();
    }
    m_lastPosList.clear();

    if (m_legend) m_legend->hide();

    delete m_legend;
    m_legend = 0;

    update();
}

/** No descriptions */
void CSearchAnalysisScene::slotResized() {
    m_scaleFactor = (double)( (double)(height() - UPPER_BORDER - LOWER_BORDER - BAR_LOWER_BORDER - 100 - (m_moduleList.count() - 1) * BAR_DELTAY)
                              / (double)m_maxCount);
    QHashIterator<QString, CSearchAnalysisItem*> it( m_itemList );
    while ( it.hasNext() ) {
        it.next();
        if (it.value()) {
            it.value()->setRect(it.value()->rect().x(), UPPER_BORDER, BAR_WIDTH + (m_moduleList.count() - 1)*BAR_DELTAX, height() - LOWER_BORDER - BAR_LOWER_BORDER);
        }
    }
    update();
}

/** This function returns a color for each module */
QColor CSearchAnalysisScene::getColor(int index) {
    switch (index) {
        case  0:
            return Qt::red;
        case  1:
            return Qt::darkGreen;
        case  2:
            return Qt::blue;
        case  3:
            return Qt::cyan;
        case  4:
            return Qt::magenta;
        case  5:
            return Qt::darkRed;
        case  6:
            return Qt::darkGray;
        case  7:
            return Qt::black;
        case  8:
            return Qt::darkCyan;
        case  9:
            return Qt::darkMagenta;
        default:
            return Qt::red;
    }
}

/** Returns the count of the book in the module */
unsigned int CSearchAnalysisScene::getCount( const QString book, CSwordModuleInfo* module ) {
    sword::ListKey& result = module->searchResult();
    const int length = book.length();
    unsigned int i = m_lastPosList[module];
    unsigned int count = 0;
    const unsigned int resultCount = result.Count();
    while (i < resultCount) {
        if ( strncmp(book.toUtf8(), (const char*)*result.GetElement(i), length) )
            break;
        i++;
        ++count;
    }
    m_lastPosList.insert(module, i);
    return count;
}

void CSearchAnalysisScene::saveAsHTML() {
    const QString fileName = QFileDialog::getSaveFileName(0, tr("Save Search Analysis"), QString::null, tr("HTML files (*.html;*.HTML;*.HTM;*.htm)") );
    if (fileName.isEmpty()) return;

    int count = 0;
    QString countStr = "";
    QString m_searchAnalysisHTML = "";
    QString tableTitle = "";
    QString tableTotals = "";
    QString VerseRange = "";
    const QString txtCSS = QString("<style type=\"text/css\">\ntd {border:1px solid black;}\nth {font-size: 130%; text-align:left; vertical-align:top;}\n</style>\n");
    const QString metaEncoding = QString("<META http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
    CSwordVerseKey key(0);
    sword::ListKey searchResult;

    key.key("Genesis 1:1");

    CSearchAnalysisItem* analysisItem = m_itemList.value( key.book() );

    QString text = "<html>\n<head>\n<title>" + tr("BibleTime Search Analysis") + "</title>\n" + txtCSS + metaEncoding + "</head>\n<body>\n";
    text += "<table>\n<tr><th>" + tr("Search text :") + "</th><th>" + CSearchDialog::getSearchDialog()->searchText() + "</th></tr>\n";

    tableTitle = "<tr><th align=\"left\">" + tr("Book") + "</th>";
    tableTotals = "<tr><td align=\"left\">" + tr("Total hits") + "</td>";

    foreach (CSwordModuleInfo* mod, m_moduleList) {
        tableTitle += QString("<th align=\"left\">") + mod->name() + QString("</th>");
        searchResult = mod->searchResult();
        countStr.setNum(searchResult.Count());

        tableTotals += QString("<td align=\"right\">") + countStr + QString("</td>");
    }
    tableTitle += QString("</tr>\n");
    tableTotals += QString("</tr>\n");

    m_searchAnalysisHTML = "";
    bool ok = true;
    while (ok) {
        m_searchAnalysisHTML += QString("<tr><td>") + key.book() + QString("</td>");
        analysisItem = m_itemList.value( key.book() );

        int moduleIndex = 0;
        QList<CSwordModuleInfo*>::iterator end_it = m_moduleList.end();
        for (QList<CSwordModuleInfo*>::iterator it(m_moduleList.begin()); it != end_it; ++it) {
            count = analysisItem->getCountForModule(moduleIndex);
            countStr.setNum(count);
            m_searchAnalysisHTML += QString("<td align=\"right\">") + countStr + QString("</td>");

            ++moduleIndex;
        }
        m_searchAnalysisHTML += QString("</tr>\n");
        ok = key.next(CSwordVerseKey::UseBook);
    }

    text += QString("<table>\n") + tableTitle + tableTotals + m_searchAnalysisHTML + QString("</table>\n");
    text += QString("<center>") + tr("Created by <a href=\"http://www.bibletime.info/\">BibleTime</a>") + QString("</center>");
    text += QString("</body></html>");

    util::tool::savePlainFile(fileName, text, false, QTextCodec::codecForName("UTF8"));
}

void CSearchAnalysisScene::resizeHeight(int height) {
    setSceneRect(0, 0, sceneRect().width(), height);
    slotResized();
}

}