Moved responsibility of getting bible passage out of KwBiblePassage into KwBibleModul...
[kworship.git] / kworship / bible / KwBibleModuleSword.cpp
blob3d15785dca471f00bae9778dbb818c7c5d80615c
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"
28 #include <QStringList>
30 #include <swmodule.h>
31 #include <swtext.h>
32 #include <versekey.h>
35 * Constructors + destructor
38 /// Default constructor.
39 KwBibleModuleSword::KwBibleModuleSword(sword::SWText* module)
40 : KwBibleModule()
41 , m_module(module)
43 // Get the range of books.
44 bool oldSCL = module->getSkipConsecutiveLinks();
45 module->setSkipConsecutiveLinks(true);
48 module->setPosition(sword::TOP);
49 sword::VerseKey key = module->KeyText();
50 m_firstBibleBookIndex = testamentBookToBible(key.Testament()-1, key.Book()-1);
54 module->setPosition(sword::BOTTOM);
55 sword::VerseKey key = module->KeyText();
56 m_lastBibleBookIndex = testamentBookToBible(key.Testament()-1, key.Book()-1);
59 module->setSkipConsecutiveLinks(oldSCL);
62 /// Destructor.
63 KwBibleModuleSword::~KwBibleModuleSword()
68 * Main interface
71 QString KwBibleModuleSword::name()
73 return m_module->Name();
76 QString KwBibleModuleSword::description()
78 return m_module->Description();
81 int KwBibleModuleSword::numChapters(int book)
83 int testamentBook;
84 int testament = localToTestamentBook(book, &testamentBook);
85 if (testament >= 0)
87 sword::VerseKey key = m_module->getKey();
88 return key.books[testament][testamentBook].chapmax;
90 return 0;
93 int KwBibleModuleSword::numVerses(int book, int chapter)
95 if (book >= 0 && chapter >= 0)
97 int testamentBook;
98 int testament = localToTestamentBook(book, &testamentBook);
99 if (testament >= 0)
101 sword::VerseKey key = m_module->getKey();
102 if (chapter < key.books[testament][testamentBook].chapmax)
104 return key.books[testament][testamentBook].versemax[chapter];
108 return 0;
111 bool KwBibleModuleSword::fillPassage(const Key& key, KwBiblePassage* outPassage) const
113 return false;
116 QString KwBibleModuleSword::renderText(const KwBibleModule::Key& key)
118 QString result;
119 int startBook, endBook;
120 int startTestament = localToTestamentBook(key.start.book, &startBook);
121 int endTestament = localToTestamentBook(key.end.book, &endBook);
122 if (startTestament >= 0)
124 if (endTestament == -1)
126 endTestament = startTestament;
127 endBook = startBook;
129 int startChapter = key.start.chapter;
130 int endChapter = key.end.chapter;
131 if (startChapter < 0)
133 startChapter = 0;
134 endChapter = numChapters(testamentBookToLocal(endTestament, endBook))-1;
136 else if (endChapter < 0)
138 endChapter = startChapter;
141 int startVerse = key.start.verse;
142 int endVerse = key.end.verse;
143 if (startVerse < 0)
145 startVerse = 0;
146 endVerse = numVerses(testamentBookToLocal(endTestament, endBook), endChapter)-1;
148 else if (endVerse < 0)
150 endVerse = startVerse;
153 sword::VerseKey vkey;
154 vkey.LowerBound().Testament(1+startTestament);
155 vkey.LowerBound().Book(1+startBook);
156 vkey.LowerBound().Chapter(1+startChapter);
157 vkey.LowerBound().Verse(1+startVerse);
158 vkey.UpperBound().Testament(1+endTestament);
159 vkey.UpperBound().Book(1+endBook);
160 vkey.UpperBound().Chapter(1+endChapter);
161 vkey.UpperBound().Verse(1+endVerse);
162 //std::cout << startTestament << " " << startBook << " " << startChapter << ":" << startVerse << std::endl;
163 //std::cout << endTestament << " " << endBook << " " << endChapter << ":" << endVerse << std::endl;
165 sword::VerseKey verse = vkey.LowerBound();
166 verse.Headings(1);
167 sword::VerseKey last = vkey.UpperBound();
168 Q_ASSERT(verse.isTraversable());
170 // verse must be before last
171 if (verse.compare(last) <= 0)
173 int limit = 500;
174 while (--limit > 0)
176 m_module->setKey(&verse);
177 const char* text = m_module->RenderText();
178 const char* preverse = m_module->getEntryAttributes()["Heading"]["Preverse"]["0"];
179 result += " ";
180 if (preverse[0] != '\0')
182 result += QString("<h1>%1</h1>").arg(QString::fromUtf8(preverse));
184 if (QLatin1String(text) != QLatin1String(""))
186 result += QString("<sup>%1</sup>").arg(verse.Verse()) + QString::fromUtf8(text);
188 if (verse.equals(last))
190 break;
192 verse.increment(1);
197 return result;
201 * Protected virtual interface
204 void KwBibleModuleSword::obtainBooks()
206 // List books
207 QStringList books;
208 sword::VerseKey key = m_module->getKey();
209 for (int bibleIndex = m_firstBibleBookIndex; bibleIndex <= m_lastBibleBookIndex; ++bibleIndex)
211 int testamentBook;
212 int testament = bibleToTestamentBook(bibleIndex, &testamentBook);
213 Q_ASSERT(testament >= 0);
214 books << QString::fromUtf8(key.books[testament][testamentBook].name);
216 setBooks(books);
220 * Internal functions
223 /// Convert a local book index to a bible book index.
224 int KwBibleModuleSword::localToBible(int localIndex) const
226 int result = -1;
227 if (localIndex >= 0 && m_firstBibleBookIndex + localIndex <= m_lastBibleBookIndex)
229 result = m_firstBibleBookIndex + localIndex;
231 return result;
234 /// Convert a bible book index to a local book index.
235 int KwBibleModuleSword::bibleToLocal(int bibleIndex) const
237 int result = -1;
238 if (bibleIndex >= m_firstBibleBookIndex && bibleIndex <= m_lastBibleBookIndex)
240 result = bibleIndex - m_firstBibleBookIndex;
242 return result;
245 /// Convert a testament & book number to a bible book index.
246 int KwBibleModuleSword::testamentBookToBible(int testament, int book) const
248 if (testament >= 0 && book >= 0)
250 sword::VerseKey key = m_module->getKey();
251 if (book < key.BMAX[testament])
253 if (testament == 0)
255 return book;
257 else if (testament == 1)
259 return key.BMAX[0] + book;
263 return -1;
266 /// Convert a bible book index into a testament and book number.
267 int KwBibleModuleSword::bibleToTestamentBook(int bibleIndex, int* book) const
269 if (bibleIndex >= 0)
271 sword::VerseKey key = m_module->getKey();
272 // Old testament?
273 if (bibleIndex < key.BMAX[0])
275 *book = bibleIndex;
276 return 0;
278 // New testament?
279 else if (bibleIndex < key.BMAX[0] + key.BMAX[1])
281 *book = bibleIndex - key.BMAX[0];
282 return 1;
285 return -1;
288 /// Convert a testament & book number to a local book index.
289 int KwBibleModuleSword::testamentBookToLocal(int testament, int book) const
291 return bibleToLocal(testamentBookToBible(testament, book));
294 /// Convert a local book index into a testament and book number.
295 int KwBibleModuleSword::localToTestamentBook(int localIndex, int* book) const
297 return bibleToTestamentBook(localToBible(localIndex), book);