From fb6657885d259903fec58f32865038873c83e9ab Mon Sep 17 00:00:00 2001 From: James Hogan Date: Sat, 1 Nov 2008 01:02:39 +0000 Subject: [PATCH] More definition of bible module structure --- kworship/bible/CMakeLists.txt | 1 + kworship/bible/KwBibleManager.cpp | 4 +- kworship/bible/KwBibleModule.cpp | 137 ++++++++++++++++++++++++++------------ kworship/bible/KwBibleModule.h | 99 +++++++++++++++++++++++---- kworship/kworship.cpp | 17 ++--- kworship/kworshipview_base.ui | 62 ++++++++++++++++- 6 files changed, 249 insertions(+), 71 deletions(-) diff --git a/kworship/bible/CMakeLists.txt b/kworship/bible/CMakeLists.txt index 6e6ae7b..90b44bb 100644 --- a/kworship/bible/CMakeLists.txt +++ b/kworship/bible/CMakeLists.txt @@ -7,6 +7,7 @@ find_package(Sword REQUIRED) set(kworshipbible_SRCS KwBibleManager.cpp KwBibleModule.cpp + KwBibleModuleSword.cpp ) include_directories(${SWORD_INCLUDE_DIR}) diff --git a/kworship/bible/KwBibleManager.cpp b/kworship/bible/KwBibleManager.cpp index b022eda..f0e6069 100644 --- a/kworship/bible/KwBibleManager.cpp +++ b/kworship/bible/KwBibleManager.cpp @@ -24,7 +24,7 @@ */ #include "KwBibleManager.h" -#include "KwBibleModule.h" +#include "KwBibleModuleSword.h" #include #include @@ -68,7 +68,7 @@ KwBibleManager::KwBibleManager() { sword::SWBuf modName = (*modIterator).first; sword::SWModule* module = (*modIterator).second; - m_modules[QLatin1String(modName)] = new KwBibleModule(module); + m_modules[QLatin1String(modName)] = new KwBibleModuleSword(module); } } diff --git a/kworship/bible/KwBibleModule.cpp b/kworship/bible/KwBibleModule.cpp index 9a8d121..35efa06 100644 --- a/kworship/bible/KwBibleModule.cpp +++ b/kworship/bible/KwBibleModule.cpp @@ -19,24 +19,19 @@ /** * @file KwBibleModule.cpp - * @brief A bible module (analagous to a SWORD module). + * @brief An abstract bible module (analagous to a SWORD module). * @author James Hogan */ #include "KwBibleModule.h" -#include - -#include -#include - /* * Constructors + destructor */ /// Default constructor. -KwBibleModule::KwBibleModule(sword::SWModule* module) -: m_module(module) +KwBibleModule::KwBibleModule() +: m_books() { } @@ -49,56 +44,110 @@ KwBibleModule::~KwBibleModule() * Main interface */ -/// Get the name of the module. -QString KwBibleModule::name() const +/// Create a key from a string. +KwBibleModule::Key KwBibleModule::createKey(const QString& text) +{ + // parse the key + //Key key = { { -1, -1, -1 }, + // { -1, -1, -1 } }; + Key key = { { 0, 1, 1 }, + { 0, 1, 6 } }; + return key; +} + +/// Create a key from individual values. +KwBibleModule::Key KwBibleModule::createKey(const QString& book, int chapter, int verse) { - return m_module->Name(); + int bookId = bookIndex(book); + Key key = { { bookId, chapter, verse }, + { -1, -1, -1 } }; + return key; } -/// Get the description of the module. -QString KwBibleModule::description() const +/// Create a range key between two verses in the same chapter. +KwBibleModule::Key KwBibleModule::createKey(const QString& book, int chapter, int verseStart, + int verseEnd) { - return m_module->Description(); + int bookId = bookIndex(book); + Key key = { { bookId, chapter, verseStart }, + { bookId, chapter, verseEnd } }; + return key; } -/// Get rendered text for a given passage. -QString KwBibleModule::renderText(const QString& key) const +/// Create a range key between two verses in different chapters. +KwBibleModule::Key KwBibleModule::createKey(const QString& book, int chapterStart, int verseStart, + int chapterEnd, int verseEnd) { - QStringList bits = key.split('-'); - sword::VerseKey vkey; - if (bits.size() >= 2) + int bookId = bookIndex(book); + Key key = { { bookId, chapterStart, verseStart }, + { bookId, chapterEnd, verseEnd } }; + return key; +} + +/// Create a range key between two verses in different books. +KwBibleModule::Key KwBibleModule::createKey(const QString& bookStart, int chapterStart, int verseStart, + const QString& bookEnd, int chapterEnd, int verseEnd) +{ + int bookIdStart = bookIndex(bookStart); + int bookIdEnd = bookIndex(bookEnd); + Key key = { { bookIdStart, chapterStart, verseStart }, + { bookIdEnd, chapterEnd, verseEnd } }; + return key; +} + +/// List the books in this module. +const QStringList& KwBibleModule::books() +{ + if (m_books.isEmpty()) { - vkey = sword::VerseKey(bits[0].toAscii(), bits[1].toAscii()); + obtainBooks(); } - else + return m_books; +} + +/// Get the index of a book from it's name. +int KwBibleModule::bookIndex(const QString& name) +{ + if (m_books.isEmpty()) { - vkey = sword::VerseKey(key.toAscii(), key.toAscii()); + obtainBooks(); } + return m_books.indexOf(name); +} - QString result; - sword::VerseKey verse = vkey.LowerBound(); - verse.Headings(1); - sword::VerseKey last = vkey.UpperBound(); - Q_ASSERT(verse.isTraversable()); - - int limit = 100; - for (; verse.compare(last) <= 0; verse.increment(1)) +/// Get the name of a book from it's index. +QString KwBibleModule::bookName(int book) +{ + if (m_books.isEmpty()) + { + obtainBooks(); + } + if (book >= 0 && book < m_books.size()) { - m_module->setKey(&verse); - m_module->RenderText(); - const char* preverse = m_module->getEntryAttributes()["Heading"]["Preverse"]["0"]; - result += " "; - if (preverse[0] != '\0') - { - result += QString("

%1

").arg(QString::fromUtf8(preverse)); - } - result += QString("%1").arg(verse.Verse()) + QString::fromUtf8(m_module->RenderText()); - if (0 == --limit) - { - break; - } + return m_books[book]; } + else + { + return QString(); + } +} - return result; +/* + * Protected virtual interface + */ + +/// Ensure that the book list is up to date. +void KwBibleModule::obtainBooks() +{ +} + +/* + * Protected interface + */ + +/// Update the list of books. +void KwBibleModule::setBooks(const QStringList& books) +{ + m_books = books; } diff --git a/kworship/bible/KwBibleModule.h b/kworship/bible/KwBibleModule.h index 381125e..7a8632f 100644 --- a/kworship/bible/KwBibleModule.h +++ b/kworship/bible/KwBibleModule.h @@ -22,28 +22,48 @@ /** * @file KwBibleModule.h - * @brief A bible module (analagous to a SWORD module). + * @brief An abstract bible module (analagous to a SWORD module). * @author James Hogan */ #include - -namespace sword -{ - class SWModule; -} +#include /// A bible module (analagous to a SWORD module). class KwBibleModule { public: + + /* + * Types + */ + + /// Verse key. + struct VerseKey + { + /// Book of the bible. + int book; + /// Chapter of the book (-1 for entire book). + int chapter; + /// Verse of the chapter (-1 for entire chapter). + int verse; + }; + + /// Range of verse keys. + struct Key + { + /// Start key of range. + VerseKey start; + /// End key of range. + VerseKey end; + }; /* * Constructors + destructor */ /// Default constructor. - KwBibleModule(sword::SWModule* module); + KwBibleModule(); /// Destructor. virtual ~KwBibleModule(); @@ -52,23 +72,76 @@ class KwBibleModule * Main interface */ + /// Create a key from a string. + Key createKey(const QString& text); + + /// Create a key from individual values. + Key createKey(const QString& book, int chapter, int verse = -1); + + /// Create a range key between two verses in the same chapter. + Key createKey(const QString& book, int chapter, int verseStart, + int verseEnd); + + /// Create a range key between two verses in different chapters. + Key createKey(const QString& book, int chapterStart, int verseStart, + int chapterEnd, int verseEnd); + + /// Create a range key between two verses in different books. + Key createKey(const QString& bookStart, int chapterStart, int verseStart, + const QString& bookEnd, int chapterEnd, int verseEnd); + /// Get the name of the module. - QString name() const; + virtual QString name() = 0; /// Get the description of the module. - QString description() const; + virtual QString description() = 0; + + /// List the books in this module. + const QStringList& books(); + + /** Get the index of a book from it's name. + * @param name Name of the book to look for. + * @return Index of the book in this module or -1 if it does not exist. + */ + int bookIndex(const QString& name); + + /// Get the name of a book from it's index. + QString bookName(int book); + + /// Get the number of chapters in this book. + virtual int numChapters(int book) = 0; + + /// Get the number of verses in a chapter of a book. + virtual int numVerses(int book, int chapter) = 0; /// Get rendered text for a given passage. - QString renderText(const QString& key) const; + virtual QString renderText(const Key& key) = 0; + + protected: + + /* + * Protected virtual interface + */ + + /// Ensure that the book list is up to date. + virtual void obtainBooks(); + + /* + * Protected interface + */ + + /// Update the list of books. + void setBooks(const QStringList& books); private: /* * Variables */ - - /// SWORD module object. - sword::SWModule* m_module; + + /// List of book names. + QStringList m_books; + }; #endif // _KwBibleModule_h_ diff --git a/kworship/kworship.cpp b/kworship/kworship.cpp index 9dcb1fd..d7b6adc 100644 --- a/kworship/kworship.cpp +++ b/kworship/kworship.cpp @@ -333,10 +333,10 @@ kworship::kworship() QList modules = manager->modules(); foreach (KwBibleModule* module, modules) { - m_view->comboBibles->addItem(i18n("%1 - %2", module->name(), module->description()), QVariant(module->name())); + m_view->comboSwordBibles->addItem(i18n("%1 - %2", module->name(), module->description()), QVariant(module->name())); } - connect(m_view->comboBibles, SIGNAL(currentIndexChanged(int)), + connect(m_view->comboSwordBibles, SIGNAL(currentIndexChanged(int)), this, SLOT(bibleSearch())); connect(m_view->searchBible, SIGNAL(textEdited(const QString&)), this, SLOT(bibleSearch())); @@ -1004,19 +1004,16 @@ void kworship::songdbEditSongBooks() void kworship::bibleSearch() { // Search using the key - int index = m_view->comboBibles->currentIndex(); + int index = m_view->comboSwordBibles->currentIndex(); if (index >= 0) { - QString modName = m_view->comboBibles->itemData(index).toString(); + QString modName = m_view->comboSwordBibles->itemData(index).toString(); KwBibleModule* module = KwBibleManager::self()->module(modName); if (0 != module) { - QString key = m_view->searchBible->text(); - if (!key.isEmpty()) - { - m_view->textBible->document()->setHtml(module->renderText(key)); - return; - } + KwBibleModule::Key key = module->createKey(m_view->searchBible->text()); + m_view->textBible->document()->setHtml(module->renderText(key)); + return; } } m_view->textBible->document()->setPlainText(QString()); diff --git a/kworship/kworshipview_base.ui b/kworship/kworshipview_base.ui index 5b0fe46..2e0a680 100644 --- a/kworship/kworshipview_base.ui +++ b/kworship/kworshipview_base.ui @@ -6,7 +6,7 @@ 0 0 584 - 596 + 667 @@ -172,7 +172,58 @@ - + + + 0 + + + + SWORD + + + + + + + + + + BibleGateway.com + + + + + + + 10 + 0 + + + + + + + + + 1 + 0 + + + + + + + + + + + + + + + + + @@ -192,6 +243,13 @@ + textBible + tabWidget_2 + searchBible + toolBox + horizontalLayoutWidget + tabWidget_2 + comboSwordBibles -- 2.11.4.GIT