1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
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. *
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. *
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 ***************************************************************************/
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>
36 * Constructors + destructor
39 /// Default constructor.
40 KwBibleModuleSword::KwBibleModuleSword(sword::SWText
* 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
);
64 KwBibleModuleSword::~KwBibleModuleSword()
72 QString
KwBibleModuleSword::name()
74 return m_module
->Name();
77 QString
KwBibleModuleSword::description()
79 return m_module
->Description();
82 int KwBibleModuleSword::numChapters(int book
)
85 int testament
= localToTestamentBook(book
, &testamentBook
);
88 sword::VerseKey key
= m_module
->getKey();
89 return key
.books
[testament
][testamentBook
].chapmax
;
94 int KwBibleModuleSword::numVerses(int book
, int chapter
)
96 if (book
>= 0 && chapter
>= 0)
99 int testament
= localToTestamentBook(book
, &testamentBook
);
102 sword::VerseKey key
= m_module
->getKey();
103 if (chapter
< key
.books
[testament
][testamentBook
].chapmax
)
105 return key
.books
[testament
][testamentBook
].versemax
[chapter
];
112 bool KwBibleModuleSword::fillPassageVerse(int bookIndex
, int chapterIndex
, int verseIndex
, KwBiblePassage
* outPassage
)
114 int bookInTestament
= -1;
115 int testament
= localToTestamentBook(bookIndex
, &bookInTestament
);
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
));
135 QString
KwBibleModuleSword::renderText(const KwBibleModule::Key
& key
)
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
;
148 int startChapter
= key
.start
.chapter
;
149 int endChapter
= key
.end
.chapter
;
150 if (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
;
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();
186 sword::VerseKey last
= vkey
.UpperBound();
187 Q_ASSERT(verse
.isTraversable());
189 // verse must be before last
190 if (verse
.compare(last
) <= 0)
195 m_module
->setKey(&verse
);
196 const char* text
= m_module
->RenderText();
197 const char* preverse
= m_module
->getEntryAttributes()["Heading"]["Preverse"]["0"];
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
))
220 * Protected virtual interface
223 void KwBibleModuleSword::obtainBooks()
227 sword::VerseKey key
= m_module
->getKey();
228 for (int bibleIndex
= m_firstBibleBookIndex
; bibleIndex
<= m_lastBibleBookIndex
; ++bibleIndex
)
231 int testament
= bibleToTestamentBook(bibleIndex
, &testamentBook
);
232 Q_ASSERT(testament
>= 0);
233 books
<< QString::fromUtf8(key
.books
[testament
][testamentBook
].name
);
242 /// Convert a local book index to a bible book index.
243 int KwBibleModuleSword::localToBible(int localIndex
) const
246 if (localIndex
>= 0 && m_firstBibleBookIndex
+ localIndex
<= m_lastBibleBookIndex
)
248 result
= m_firstBibleBookIndex
+ localIndex
;
253 /// Convert a bible book index to a local book index.
254 int KwBibleModuleSword::bibleToLocal(int bibleIndex
) const
257 if (bibleIndex
>= m_firstBibleBookIndex
&& bibleIndex
<= m_lastBibleBookIndex
)
259 result
= bibleIndex
- m_firstBibleBookIndex
;
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
])
276 else if (testament
== 1)
278 return key
.BMAX
[0] + book
;
285 /// Convert a bible book index into a testament and book number.
286 int KwBibleModuleSword::bibleToTestamentBook(int bibleIndex
, int* book
) const
290 sword::VerseKey key
= m_module
->getKey();
292 if (bibleIndex
< key
.BMAX
[0])
298 else if (bibleIndex
< key
.BMAX
[0] + key
.BMAX
[1])
300 *book
= bibleIndex
- key
.BMAX
[0];
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
);