Improve sword modules interface
[kworship.git] / kworship / bible / KwBibleModuleSword.cpp
blob09b625e3b4cbfefdaa3515c94d9aecb3af56b4a9
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 QString KwBibleModuleSword::renderText(const KwBibleModule::Key& key)
113 QString result;
114 int startBook, endBook;
115 int startTestament = localToTestamentBook(key.start.book, &startBook);
116 int endTestament = localToTestamentBook(key.end.book, &endBook);
117 if (startTestament >= 0)
119 if (endTestament == -1)
121 endTestament = startTestament;
122 endBook = startBook;
124 int startChapter = key.start.chapter;
125 int endChapter = key.start.chapter;
126 if (startChapter < 0)
128 startChapter = 0;
129 endChapter = numChapters(testamentBookToLocal(endTestament, endBook))-1;
131 else if (endChapter < 0)
133 endChapter = startChapter;
136 int startVerse = key.start.verse;
137 int endVerse = key.start.verse;
138 if (startVerse < 0)
140 startVerse = 0;
141 endVerse = numVerses(testamentBookToLocal(endTestament, endBook), endChapter)-1;
143 else if (endVerse < 0)
145 endVerse = startVerse;
148 sword::VerseKey vkey;
149 vkey.LowerBound().Testament(1+startTestament);
150 vkey.LowerBound().Book(1+startBook);
151 vkey.LowerBound().Chapter(1+startChapter);
152 vkey.LowerBound().Verse(1+startVerse);
153 vkey.UpperBound().Testament(1+endTestament);
154 vkey.UpperBound().Book(1+endBook);
155 vkey.UpperBound().Chapter(1+endChapter);
156 vkey.UpperBound().Verse(1+endVerse);
157 //std::cout << startTestament << " " << startBook << " " << startChapter << ":" << startVerse << std::endl;
158 //std::cout << endTestament << " " << endBook << " " << endChapter << ":" << endVerse << std::endl;
160 sword::VerseKey verse = vkey.LowerBound();
161 verse.Headings(1);
162 sword::VerseKey last = vkey.UpperBound();
163 Q_ASSERT(verse.isTraversable());
165 // verse must be before last
166 if (verse.compare(last) <= 0)
168 int limit = 500;
169 while (--limit > 0)
171 m_module->setKey(&verse);
172 const char* text = m_module->RenderText();
173 const char* preverse = m_module->getEntryAttributes()["Heading"]["Preverse"]["0"];
174 result += " ";
175 if (preverse[0] != '\0')
177 result += QString("<h1>%1</h1>").arg(QString::fromUtf8(preverse));
179 if (QLatin1String(text) != QLatin1String(""))
181 result += QString("<sup>%1</sup>").arg(verse.Verse()) + QString::fromUtf8(text);
183 if (verse.equals(last))
185 break;
187 verse.increment(1);
192 return result;
196 * Protected virtual interface
199 void KwBibleModuleSword::obtainBooks()
201 // List books
202 QStringList books;
203 sword::VerseKey key = m_module->getKey();
204 for (int bibleIndex = m_firstBibleBookIndex; bibleIndex <= m_lastBibleBookIndex; ++bibleIndex)
206 int testamentBook;
207 int testament = bibleToTestamentBook(bibleIndex, &testamentBook);
208 Q_ASSERT(testament >= 0);
209 books << QString::fromUtf8(key.books[testament][testamentBook].name);
211 setBooks(books);
215 * Internal functions
218 /// Convert a local book index to a bible book index.
219 int KwBibleModuleSword::localToBible(int localIndex) const
221 int result = -1;
222 if (localIndex >= 0 && m_firstBibleBookIndex + localIndex <= m_lastBibleBookIndex)
224 result = m_firstBibleBookIndex + localIndex;
226 return result;
229 /// Convert a bible book index to a local book index.
230 int KwBibleModuleSword::bibleToLocal(int bibleIndex) const
232 int result = -1;
233 if (bibleIndex >= m_firstBibleBookIndex && bibleIndex <= m_lastBibleBookIndex)
235 result = bibleIndex - m_firstBibleBookIndex;
237 return result;
240 /// Convert a testament & book number to a bible book index.
241 int KwBibleModuleSword::testamentBookToBible(int testament, int book) const
243 if (testament >= 0 && book >= 0)
245 sword::VerseKey key = m_module->getKey();
246 if (book < key.BMAX[testament])
248 if (testament == 0)
250 return book;
252 else if (testament == 1)
254 return key.BMAX[0] + book;
258 return -1;
261 /// Convert a bible book index into a testament and book number.
262 int KwBibleModuleSword::bibleToTestamentBook(int bibleIndex, int* book) const
264 if (bibleIndex >= 0)
266 sword::VerseKey key = m_module->getKey();
267 // Old testament?
268 if (bibleIndex < key.BMAX[0])
270 *book = bibleIndex;
271 return 0;
273 // New testament?
274 else if (bibleIndex < key.BMAX[0] + key.BMAX[1])
276 *book = bibleIndex - key.BMAX[0];
277 return 1;
280 return -1;
283 /// Convert a testament & book number to a local book index.
284 int KwBibleModuleSword::testamentBookToLocal(int testament, int book) const
286 return bibleToLocal(testamentBookToBible(testament, book));
289 /// Convert a local book index into a testament and book number.
290 int KwBibleModuleSword::localToTestamentBook(int localIndex, int* book) const
292 return bibleToTestamentBook(localToBible(localIndex), book);