Finished (for now) filling of bible passage and now renders html from it
[kworship.git] / kworship / bible / KwBibleModuleSword.cpp
bloba147e918cbc5a9fc633de0c5033bc847c1892eae
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file KwBibleModuleSword.cpp
22 * @brief A SWORD bible module.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwBibleModuleSword.h"
27 #include "KwBiblePassage.h"
29 #include <QStringList>
31 #include <swmodule.h>
32 #include <swtext.h>
33 #include <versekey.h>
36 * Constructors + destructor
39 /// Default constructor.
40 KwBibleModuleSword::KwBibleModuleSword(sword::SWText* module)
41 : KwBibleModule()
42 , m_module(module)
44 // Get the range of books.
45 bool oldSCL = module->getSkipConsecutiveLinks();
46 module->setSkipConsecutiveLinks(true);
49 module->setPosition(sword::TOP);
50 sword::VerseKey key = module->KeyText();
51 m_firstBibleBookIndex = testamentBookToBible(key.Testament()-1, key.Book()-1);
55 module->setPosition(sword::BOTTOM);
56 sword::VerseKey key = module->KeyText();
57 m_lastBibleBookIndex = testamentBookToBible(key.Testament()-1, key.Book()-1);
60 module->setSkipConsecutiveLinks(oldSCL);
63 /// Destructor.
64 KwBibleModuleSword::~KwBibleModuleSword()
69 * Main interface
72 QString KwBibleModuleSword::name()
74 return m_module->Name();
77 QString KwBibleModuleSword::description()
79 return m_module->Description();
82 int KwBibleModuleSword::numChapters(int book)
84 int testamentBook;
85 int testament = localToTestamentBook(book, &testamentBook);
86 if (testament >= 0)
88 sword::VerseKey key = m_module->getKey();
89 return key.books[testament][testamentBook].chapmax;
91 return 0;
94 int KwBibleModuleSword::numVerses(int book, int chapter)
96 if (book >= 0 && chapter >= 0)
98 int testamentBook;
99 int testament = localToTestamentBook(book, &testamentBook);
100 if (testament >= 0)
102 sword::VerseKey key = m_module->getKey();
103 if (chapter < key.books[testament][testamentBook].chapmax)
105 return key.books[testament][testamentBook].versemax[chapter];
109 return 0;
112 bool KwBibleModuleSword::fillPassageVerse(int bookIndex, int chapterIndex, int verseIndex, KwBiblePassage* outPassage)
114 int bookInTestament = -1;
115 int testament = localToTestamentBook(bookIndex, &bookInTestament);
116 if (testament >= 0)
118 sword::VerseKey verse;
119 verse.Testament(1+testament);
120 verse.Book(1+bookInTestament);
121 verse.Chapter(1+chapterIndex);
122 verse.Verse(1+verseIndex);
124 m_module->setKey(&verse);
125 const char* text = m_module->RenderText();
126 const char* preverse = m_module->getEntryAttributes()["Heading"]["Preverse"]["0"];
127 outPassage->initVerse(bookIndex, 1+chapterIndex, 1+verseIndex,
128 QString::fromUtf8(preverse),
129 QString::fromUtf8(text));
130 return true;
132 return false;
135 QString KwBibleModuleSword::renderText(const KwBibleModule::Key& key)
137 QString result;
138 int startBook, endBook;
139 int startTestament = localToTestamentBook(key.start.book, &startBook);
140 int endTestament = localToTestamentBook(key.end.book, &endBook);
141 if (startTestament >= 0)
143 if (endTestament == -1)
145 endTestament = startTestament;
146 endBook = startBook;
148 int startChapter = key.start.chapter;
149 int endChapter = key.end.chapter;
150 if (startChapter < 0)
152 startChapter = 0;
153 endChapter = numChapters(testamentBookToLocal(endTestament, endBook))-1;
155 else if (endChapter < 0)
157 endChapter = startChapter;
160 int startVerse = key.start.verse;
161 int endVerse = key.end.verse;
162 if (startVerse < 0)
164 startVerse = 0;
165 endVerse = numVerses(testamentBookToLocal(endTestament, endBook), endChapter)-1;
167 else if (endVerse < 0)
169 endVerse = startVerse;
172 sword::VerseKey vkey;
173 vkey.LowerBound().Testament(1+startTestament);
174 vkey.LowerBound().Book(1+startBook);
175 vkey.LowerBound().Chapter(1+startChapter);
176 vkey.LowerBound().Verse(1+startVerse);
177 vkey.UpperBound().Testament(1+endTestament);
178 vkey.UpperBound().Book(1+endBook);
179 vkey.UpperBound().Chapter(1+endChapter);
180 vkey.UpperBound().Verse(1+endVerse);
181 //std::cout << startTestament << " " << startBook << " " << startChapter << ":" << startVerse << std::endl;
182 //std::cout << endTestament << " " << endBook << " " << endChapter << ":" << endVerse << std::endl;
184 sword::VerseKey verse = vkey.LowerBound();
185 verse.Headings(1);
186 sword::VerseKey last = vkey.UpperBound();
187 Q_ASSERT(verse.isTraversable());
189 // verse must be before last
190 if (verse.compare(last) <= 0)
192 int limit = 500;
193 while (--limit > 0)
195 m_module->setKey(&verse);
196 const char* text = m_module->RenderText();
197 const char* preverse = m_module->getEntryAttributes()["Heading"]["Preverse"]["0"];
198 result += " ";
199 if (preverse[0] != '\0')
201 result += QString("<h1>%1</h1>").arg(QString::fromUtf8(preverse));
203 if (QLatin1String(text) != QLatin1String(""))
205 result += QString("<sup>%1</sup>").arg(verse.Verse()) + QString::fromUtf8(text);
207 if (verse.equals(last))
209 break;
211 verse.increment(1);
216 return result;
220 * Protected virtual interface
223 void KwBibleModuleSword::obtainBooks()
225 // List books
226 QStringList books;
227 sword::VerseKey key = m_module->getKey();
228 for (int bibleIndex = m_firstBibleBookIndex; bibleIndex <= m_lastBibleBookIndex; ++bibleIndex)
230 int testamentBook;
231 int testament = bibleToTestamentBook(bibleIndex, &testamentBook);
232 Q_ASSERT(testament >= 0);
233 books << QString::fromUtf8(key.books[testament][testamentBook].name);
235 setBooks(books);
239 * Internal functions
242 /// Convert a local book index to a bible book index.
243 int KwBibleModuleSword::localToBible(int localIndex) const
245 int result = -1;
246 if (localIndex >= 0 && m_firstBibleBookIndex + localIndex <= m_lastBibleBookIndex)
248 result = m_firstBibleBookIndex + localIndex;
250 return result;
253 /// Convert a bible book index to a local book index.
254 int KwBibleModuleSword::bibleToLocal(int bibleIndex) const
256 int result = -1;
257 if (bibleIndex >= m_firstBibleBookIndex && bibleIndex <= m_lastBibleBookIndex)
259 result = bibleIndex - m_firstBibleBookIndex;
261 return result;
264 /// Convert a testament & book number to a bible book index.
265 int KwBibleModuleSword::testamentBookToBible(int testament, int book) const
267 if (testament >= 0 && book >= 0)
269 sword::VerseKey key = m_module->getKey();
270 if (book < key.BMAX[testament])
272 if (testament == 0)
274 return book;
276 else if (testament == 1)
278 return key.BMAX[0] + book;
282 return -1;
285 /// Convert a bible book index into a testament and book number.
286 int KwBibleModuleSword::bibleToTestamentBook(int bibleIndex, int* book) const
288 if (bibleIndex >= 0)
290 sword::VerseKey key = m_module->getKey();
291 // Old testament?
292 if (bibleIndex < key.BMAX[0])
294 *book = bibleIndex;
295 return 0;
297 // New testament?
298 else if (bibleIndex < key.BMAX[0] + key.BMAX[1])
300 *book = bibleIndex - key.BMAX[0];
301 return 1;
304 return -1;
307 /// Convert a testament & book number to a local book index.
308 int KwBibleModuleSword::testamentBookToLocal(int testament, int book) const
310 return bibleToLocal(testamentBookToBible(testament, book));
313 /// Convert a local book index into a testament and book number.
314 int KwBibleModuleSword::localToTestamentBook(int localIndex, int* book) const
316 return bibleToTestamentBook(localToBible(localIndex), book);