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"
28 #include <QStringList>
35 * Constructors + destructor
38 /// Default constructor.
39 KwBibleModuleSword::KwBibleModuleSword(sword::SWText
* 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
);
63 KwBibleModuleSword::~KwBibleModuleSword()
71 QString
KwBibleModuleSword::name()
73 return m_module
->Name();
76 QString
KwBibleModuleSword::description()
78 return m_module
->Description();
81 int KwBibleModuleSword::numChapters(int book
)
84 int testament
= localToTestamentBook(book
, &testamentBook
);
87 sword::VerseKey key
= m_module
->getKey();
88 return key
.books
[testament
][testamentBook
].chapmax
;
93 int KwBibleModuleSword::numVerses(int book
, int chapter
)
95 if (book
>= 0 && chapter
>= 0)
98 int testament
= localToTestamentBook(book
, &testamentBook
);
101 sword::VerseKey key
= m_module
->getKey();
102 if (chapter
< key
.books
[testament
][testamentBook
].chapmax
)
104 return key
.books
[testament
][testamentBook
].versemax
[chapter
];
111 bool KwBibleModuleSword::fillPassage(const Key
& key
, KwBiblePassage
* outPassage
) const
116 QString
KwBibleModuleSword::renderText(const KwBibleModule::Key
& key
)
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
;
129 int startChapter
= key
.start
.chapter
;
130 int endChapter
= key
.end
.chapter
;
131 if (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
;
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();
167 sword::VerseKey last
= vkey
.UpperBound();
168 Q_ASSERT(verse
.isTraversable());
170 // verse must be before last
171 if (verse
.compare(last
) <= 0)
176 m_module
->setKey(&verse
);
177 const char* text
= m_module
->RenderText();
178 const char* preverse
= m_module
->getEntryAttributes()["Heading"]["Preverse"]["0"];
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
))
201 * Protected virtual interface
204 void KwBibleModuleSword::obtainBooks()
208 sword::VerseKey key
= m_module
->getKey();
209 for (int bibleIndex
= m_firstBibleBookIndex
; bibleIndex
<= m_lastBibleBookIndex
; ++bibleIndex
)
212 int testament
= bibleToTestamentBook(bibleIndex
, &testamentBook
);
213 Q_ASSERT(testament
>= 0);
214 books
<< QString::fromUtf8(key
.books
[testament
][testamentBook
].name
);
223 /// Convert a local book index to a bible book index.
224 int KwBibleModuleSword::localToBible(int localIndex
) const
227 if (localIndex
>= 0 && m_firstBibleBookIndex
+ localIndex
<= m_lastBibleBookIndex
)
229 result
= m_firstBibleBookIndex
+ localIndex
;
234 /// Convert a bible book index to a local book index.
235 int KwBibleModuleSword::bibleToLocal(int bibleIndex
) const
238 if (bibleIndex
>= m_firstBibleBookIndex
&& bibleIndex
<= m_lastBibleBookIndex
)
240 result
= bibleIndex
- m_firstBibleBookIndex
;
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
])
257 else if (testament
== 1)
259 return key
.BMAX
[0] + book
;
266 /// Convert a bible book index into a testament and book number.
267 int KwBibleModuleSword::bibleToTestamentBook(int bibleIndex
, int* book
) const
271 sword::VerseKey key
= m_module
->getKey();
273 if (bibleIndex
< key
.BMAX
[0])
279 else if (bibleIndex
< key
.BMAX
[0] + key
.BMAX
[1])
281 *book
= bibleIndex
- key
.BMAX
[0];
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
);