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 QString
KwBibleModuleSword::renderText(const KwBibleModule::Key
& key
)
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
;
124 int startChapter
= key
.start
.chapter
;
125 int endChapter
= key
.end
.chapter
;
126 if (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
.end
.verse
;
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();
162 sword::VerseKey last
= vkey
.UpperBound();
163 Q_ASSERT(verse
.isTraversable());
165 // verse must be before last
166 if (verse
.compare(last
) <= 0)
171 m_module
->setKey(&verse
);
172 const char* text
= m_module
->RenderText();
173 const char* preverse
= m_module
->getEntryAttributes()["Heading"]["Preverse"]["0"];
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
))
196 * Protected virtual interface
199 void KwBibleModuleSword::obtainBooks()
203 sword::VerseKey key
= m_module
->getKey();
204 for (int bibleIndex
= m_firstBibleBookIndex
; bibleIndex
<= m_lastBibleBookIndex
; ++bibleIndex
)
207 int testament
= bibleToTestamentBook(bibleIndex
, &testamentBook
);
208 Q_ASSERT(testament
>= 0);
209 books
<< QString::fromUtf8(key
.books
[testament
][testamentBook
].name
);
218 /// Convert a local book index to a bible book index.
219 int KwBibleModuleSword::localToBible(int localIndex
) const
222 if (localIndex
>= 0 && m_firstBibleBookIndex
+ localIndex
<= m_lastBibleBookIndex
)
224 result
= m_firstBibleBookIndex
+ localIndex
;
229 /// Convert a bible book index to a local book index.
230 int KwBibleModuleSword::bibleToLocal(int bibleIndex
) const
233 if (bibleIndex
>= m_firstBibleBookIndex
&& bibleIndex
<= m_lastBibleBookIndex
)
235 result
= bibleIndex
- m_firstBibleBookIndex
;
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
])
252 else if (testament
== 1)
254 return key
.BMAX
[0] + book
;
261 /// Convert a bible book index into a testament and book number.
262 int KwBibleModuleSword::bibleToTestamentBook(int bibleIndex
, int* book
) const
266 sword::VerseKey key
= m_module
->getKey();
268 if (bibleIndex
< key
.BMAX
[0])
274 else if (bibleIndex
< key
.BMAX
[0] + key
.BMAX
[1])
276 *book
= bibleIndex
- key
.BMAX
[0];
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
);