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 QString
KwBibleModuleSword::managerId()
87 int KwBibleModuleSword::numChapters(int book
)
90 int testament
= localToTestamentBook(book
, &testamentBook
);
93 sword::VerseKey key
= m_module
->getKey();
94 key
.Testament(1+testament
);
95 key
.Book(1+testamentBook
);
96 return key
.getChapterMax();
101 int KwBibleModuleSword::numVerses(int book
, int chapter
)
103 if (book
>= 0 && chapter
>= 0)
106 int testament
= localToTestamentBook(book
, &testamentBook
);
109 sword::VerseKey key
= m_module
->getKey();
110 key
.Testament(1+testament
);
111 key
.Book(1+testamentBook
);
112 if (chapter
< key
.getChapterMax())
114 key
.Chapter(1+chapter
);
115 return key
.getVerseMax();
122 bool KwBibleModuleSword::fillPassageVerse(int bookIndex
, int chapterIndex
, int verseIndex
, KwBiblePassage
* outPassage
)
124 int bookInTestament
= -1;
125 int testament
= localToTestamentBook(bookIndex
, &bookInTestament
);
128 sword::VerseKey verse
;
130 verse
.Testament(1+testament
);
131 verse
.Book(1+bookInTestament
);
132 verse
.Chapter(1+chapterIndex
);
133 verse
.Verse(1+verseIndex
);
135 m_module
->setKey(&verse
);
136 const char* text
= m_module
->RenderText();
137 const char* preverse
= m_module
->getEntryAttributes()["Heading"]["Preverse"]["0"];
139 if (preverse
[0] != '\0')
141 headings
= QString("<h4>%1</h4>").arg(QString::fromUtf8(preverse
));
143 outPassage
->initVerse(bookIndex
, 1+chapterIndex
, 1+verseIndex
,
145 QString::fromUtf8(text
));
152 * Protected virtual interface
155 void KwBibleModuleSword::obtainBooks()
159 sword::VerseKey key
= m_module
->getKey();
160 for (int bibleIndex
= m_firstBibleBookIndex
; bibleIndex
<= m_lastBibleBookIndex
; ++bibleIndex
)
163 int testament
= bibleToTestamentBook(bibleIndex
, &testamentBook
);
164 Q_ASSERT(testament
>= 0);
165 key
.Testament(1+testament
);
166 key
.Book(1+testamentBook
);
167 books
<< QString::fromUtf8(key
.getBookName());
176 /// Convert a local book index to a bible book index.
177 int KwBibleModuleSword::localToBible(int localIndex
) const
180 if (localIndex
>= 0 && m_firstBibleBookIndex
+ localIndex
<= m_lastBibleBookIndex
)
182 result
= m_firstBibleBookIndex
+ localIndex
;
187 /// Convert a bible book index to a local book index.
188 int KwBibleModuleSword::bibleToLocal(int bibleIndex
) const
191 if (bibleIndex
>= m_firstBibleBookIndex
&& bibleIndex
<= m_lastBibleBookIndex
)
193 result
= bibleIndex
- m_firstBibleBookIndex
;
198 /// Convert a testament & book number to a bible book index.
199 int KwBibleModuleSword::testamentBookToBible(int testament
, int book
) const
201 if (testament
>= 0 && book
>= 0)
203 sword::VerseKey key
= m_module
->getKey();
204 if (book
< key
.BMAX
[testament
])
210 else if (testament
== 1)
212 return key
.BMAX
[0] + book
;
219 /// Convert a bible book index into a testament and book number.
220 int KwBibleModuleSword::bibleToTestamentBook(int bibleIndex
, int* book
) const
224 sword::VerseKey key
= m_module
->getKey();
226 if (bibleIndex
< key
.BMAX
[0])
232 else if (bibleIndex
< key
.BMAX
[0] + key
.BMAX
[1])
234 *book
= bibleIndex
- key
.BMAX
[0];
241 /// Convert a testament & book number to a local book index.
242 int KwBibleModuleSword::testamentBookToLocal(int testament
, int book
) const
244 return bibleToLocal(testamentBookToBible(testament
, book
));
247 /// Convert a local book index into a testament and book number.
248 int KwBibleModuleSword::localToTestamentBook(int localIndex
, int* book
) const
250 return bibleToTestamentBook(localToBible(localIndex
), book
);