From c4fc29d6bf49fb917c93a2c25a4df5f13e6ab3c6 Mon Sep 17 00:00:00 2001 From: James Hogan Date: Mon, 17 Nov 2008 00:48:47 +0000 Subject: [PATCH] Added KwBiblePassage and label bible passage playlist item passage isn't loaded in yet so labelled empty, but it does work e.g. Genesis 1:3-2:4 (ESV) need to make some kind of verse iterator interface to structurally extract the text into the KwBiblePassage --- kworship/KwBiblePlugin.cpp | 129 +++++++++++++++++++---------- kworship/KwBiblePlugin.h | 9 ++ kworship/bible/CMakeLists.txt | 1 + kworship/bible/KwBiblePassage.cpp | 124 +++++++++++++++++++++++++++ kworship/bible/KwBiblePassage.h | 121 +++++++++++++++++++++++++++ kworship/bible/KwBiblePlaylistItem.cpp | 15 +++- kworship/bible/KwBiblePlaylistItem.h | 29 ++++++- kworship/bible/KwBiblePlaylistItemNode.cpp | 3 +- 8 files changed, 380 insertions(+), 51 deletions(-) create mode 100644 kworship/bible/KwBiblePassage.cpp create mode 100644 kworship/bible/KwBiblePassage.h diff --git a/kworship/KwBiblePlugin.cpp b/kworship/KwBiblePlugin.cpp index 22898c1..9901b5b 100644 --- a/kworship/KwBiblePlugin.cpp +++ b/kworship/KwBiblePlugin.cpp @@ -80,6 +80,59 @@ KwBiblePlugin::~KwBiblePlugin() } /* + * Accessors + */ + +/// Get the current bible passage information. +bool KwBiblePlugin::resolvePassage(KwBibleManager** manager, KwBibleModule** module, KwBibleModule::Key* key, bool* usedSearch) const +{ + // Get the current bible manager + int tab = m_managerTabs->currentIndex(); + if (tab >= 0 && tab < m_managers.size()) + { + const BibleManager& mgr = m_managers[tab]; + *manager = mgr.manager; + + // Is a bible selected? + int bibleInd = mgr.comboBibles->currentIndex(); + QString bible; + KwBibleModule* mod = 0; + if (bibleInd >= 0) + { + QString bible = mgr.comboBibles->itemData(bibleInd).toString(); + mod = mgr.manager->module(bible); + } + if (0 != mod) + { + *module = mod; + // Is a book selected? + int bookIndex = m_comboBook->currentIndex(); + int chapterIndex = -1; + if (bookIndex >= 0) + { + // Is a chapter selected? + chapterIndex = m_comboChapter->currentIndex(); + } + + bool valid; + KwBibleModule::Key relativeKey = mod->createKey(bookIndex, chapterIndex); + *key = mod->createKey(relativeKey, m_editRange->text(), &valid); + + if (0 != usedSearch) + { + *usedSearch = true; + } + return valid; + } + } + if (0 != usedSearch) + { + *usedSearch = false; + } + return false; +} + +/* * Private slots */ @@ -200,54 +253,30 @@ void KwBiblePlugin::slotBookChanged() /// Fired when the bible text needs to be retrieved. void KwBiblePlugin::slotVerseRange() { - // Get the current bible manager - int tab = m_managerTabs->currentIndex(); - if (tab >= 0 && tab < m_managers.size()) + KwBibleManager* manager; + KwBibleModule* module; + KwBibleModule::Key key; + bool usedSearch; + bool success = resolvePassage(&manager, &module, &key, &usedSearch); + if (usedSearch) { - BibleManager& mgr = m_managers[tab]; + m_comboBook->setCurrentIndex(key.start.book); + m_comboChapter->setCurrentIndex(key.start.chapter); + m_textPassage->document()->setHtml(module->renderText(key)); - // Is a bible selected? - int bibleInd = mgr.comboBibles->currentIndex(); - QString bible; - KwBibleModule* module = 0; - if (bibleInd >= 0) + m_insertIntoPlaylistAction->setEnabled(true); + m_showNowAction->setEnabled(true); + + // Update color of search box + static QPalette p = m_editRange->palette(); + QPalette changedPal = p; + if (!success) { - QString bible = mgr.comboBibles->itemData(bibleInd).toString(); - module = mgr.manager->module(bible); + changedPal.setColor( QPalette::Normal, QPalette::Base, QColor(255, 127, 127) ); } - if (0 != module) - { - // Is a book selected? - int bookIndex = m_comboBook->currentIndex(); - int chapterIndex = -1; - if (bookIndex >= 0) - { - // Is a chapter selected? - chapterIndex = m_comboChapter->currentIndex(); - } + m_editRange->setPalette(changedPal); - bool valid; - KwBibleModule::Key relativeKey = module->createKey(bookIndex, chapterIndex); - KwBibleModule::Key key = module->createKey(relativeKey, m_editRange->text(), &valid); - // Update book and chapter - m_comboBook->setCurrentIndex(key.start.book); - m_comboChapter->setCurrentIndex(key.start.chapter); - m_textPassage->document()->setHtml(module->renderText(key)); - - m_insertIntoPlaylistAction->setEnabled(true); - m_showNowAction->setEnabled(true); - - // Update color of search box - static QPalette p = m_editRange->palette(); - QPalette changedPal = p; - if (!valid) - { - changedPal.setColor( QPalette::Normal, QPalette::Base, QColor(255, 127, 127) ); - } - m_editRange->setPalette(changedPal); - - return; - } + return; } m_textPassage->document()->setPlainText(QString()); m_insertIntoPlaylistAction->setEnabled(false); @@ -257,9 +286,17 @@ void KwBiblePlugin::slotVerseRange() /// Fired by the insert into playlist action. void KwBiblePlugin::slotInsertIntoPlaylist() { - KwBiblePlaylistItem* item = new KwBiblePlaylistItem(); - KwPlaylistModel* model = KwApplication::self()->mainWindow()->playlistModel(); - model->addItem(QModelIndex(), item); + KwBibleManager* manager; + KwBibleModule* module; + KwBibleModule::Key key; + bool success = resolvePassage(&manager, &module, &key); + + if (success) + { + KwBiblePlaylistItem* item = new KwBiblePlaylistItem(manager->name(), module->name(), key); + KwPlaylistModel* model = KwApplication::self()->mainWindow()->playlistModel(); + model->addItem(QModelIndex(), item); + } } /// Fired by the show now action. diff --git a/kworship/KwBiblePlugin.h b/kworship/KwBiblePlugin.h index 0590c21..068fe63 100644 --- a/kworship/KwBiblePlugin.h +++ b/kworship/KwBiblePlugin.h @@ -28,6 +28,8 @@ #include "KwPlugin.h" +#include + class KwBibleManager; class KAction; @@ -56,6 +58,13 @@ class KwBiblePlugin : public KwPlugin /// Destructor. virtual ~KwBiblePlugin(); + /* + * Accessors + */ + + /// Get the current bible passage information. + bool resolvePassage(KwBibleManager** manager, KwBibleModule** module, KwBibleModule::Key* key, bool* usedSearch = 0) const; + private slots: /* diff --git a/kworship/bible/CMakeLists.txt b/kworship/bible/CMakeLists.txt index ecdf214..14af1e1 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 + KwBiblePassage.cpp KwBiblePlaylistItem.cpp KwBiblePlaylistItemNode.cpp # SWORD diff --git a/kworship/bible/KwBiblePassage.cpp b/kworship/bible/KwBiblePassage.cpp new file mode 100644 index 0000000..3470a70 --- /dev/null +++ b/kworship/bible/KwBiblePassage.cpp @@ -0,0 +1,124 @@ +/*************************************************************************** + * This file is part of KWorship. * + * Copyright 2008 James Hogan * + * * + * KWorship is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 2 of the License, or * + * (at your option) any later version. * + * * + * KWorship is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with KWorship. If not, write to the Free Software Foundation, * + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +/** + * @file KwBiblePassage.cpp + * @brief A passage of rendered bible text. + * @author James Hogan + */ + +#include "KwBiblePassage.h" + +#include + +/* + * Constructors + destructor + */ + +/// Default constructor. +KwBiblePassage::KwBiblePassage() +: m_managerId() +, m_moduleId() +, m_key() +, m_firstBook(0) +, m_numBooks(0) +, m_books(0) +{ +} + +/// Fill the passage from a given module. +KwBiblePassage::KwBiblePassage(const QString& managerId, const QString& moduleId, const KwBibleModule::Key& key) +: m_managerId(managerId) +, m_moduleId(moduleId) +, m_key(key) +, m_firstBook(0) +, m_numBooks(0) +, m_books(0) +{ + /// @todo Iterate through the verses getting the data. +} + +/// Destructor. +KwBiblePassage::~KwBiblePassage() +{ + // Deep cleanup operation + for (int bookIndex = 0; bookIndex < m_numBooks; ++bookIndex) + { + Book* book = &m_books[bookIndex]; + for (int chapterIndex = 0; chapterIndex < book->numChapters; ++chapterIndex) + { + Chapter* chapter = &book->chapters[chapterIndex]; + delete chapter->verses; + } + delete book->chapters; + } + delete m_books; +} + +/* + * Main interface + */ + +/// Get textual key of this passage. +QString KwBiblePassage::textualKey() const +{ + // Empty range + if (0 == m_numBooks) + { + return i18nc("empty bible passage", "empty"); + } + Q_ASSERT(m_books[0].numChapters > 0); + Q_ASSERT(m_books[0].chapters[0].numVerses > 0); + + QString result = i18nc("bible index (book, chapter:verse)", "%1 %2", + m_books[0].name, + i18nc("bible index (chapter:verse)", "%1:%2", + m_books[0].firstChapter, + m_books[0].chapters[0].firstVerse)); + Book* lastBook = &m_books[m_numBooks - 1]; + Chapter* lastChapter = &lastBook->chapters[lastBook->numChapters - 1]; + // Overlapping multiple books + if (m_numBooks > 1) + { + result = i18nc("bible passage range (book chapter:verse, book chapter:verse)", "%1 - %2", result, + i18nc("bible index (book, chapter:verse)", "%1 %2", + lastBook->name, + i18nc("bible index (chapter:verse)", "%1:%2", + lastBook->firstChapter + lastBook->numChapters - 1, + lastChapter->firstVerse + lastChapter->numVerses - 1))); + } + // Overlapping multiple chapters + else if (lastBook->numChapters > 1) + { + result = i18nc("bible passage range (book chapter:verse, chapter:verse)", "%1 - %2", result, + i18nc("bible index (chapter:verse)", "%1:%2", + lastBook->firstChapter + lastBook->numChapters - 1, + lastChapter->firstVerse + lastChapter->numVerses - 1)); + } + // Overlapping multiple verses + else if (lastChapter->numVerses > 1) + { + result = i18nc("bible passage range (book chapter:verse, verse)", "%1-%2", result, + lastChapter->firstVerse + lastChapter->numVerses - 1); + } + + // Module name is quite important too + return i18nc("bible index (range, version)", "%1 (%2)", result, m_moduleId); +} + diff --git a/kworship/bible/KwBiblePassage.h b/kworship/bible/KwBiblePassage.h new file mode 100644 index 0000000..0149482 --- /dev/null +++ b/kworship/bible/KwBiblePassage.h @@ -0,0 +1,121 @@ +/*************************************************************************** + * This file is part of KWorship. * + * Copyright 2008 James Hogan * + * * + * KWorship is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 2 of the License, or * + * (at your option) any later version. * + * * + * KWorship is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with KWorship. If not, write to the Free Software Foundation, * + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef _KwBiblePassage_h_ +#define _KwBiblePassage_h_ + +/** + * @file KwBiblePassage.h + * @brief A passage of rendered bible text. + * @author James Hogan + */ + +#include "KwBibleModule.h" + +#include + +/** A passage of rendered bible text. + * Stores enough information to adjust settings. + * Can be used as a cache. + */ +class KwBiblePassage +{ + public: + + /* + * Constructors + destructor + */ + + /// Default constructor. + KwBiblePassage(); + + /// Fill the passage from a given module. + KwBiblePassage(const QString& managerId, const QString& moduleId, const KwBibleModule::Key& key); + + /// Destructor. + virtual ~KwBiblePassage(); + + /* + * Main interface + */ + + /// Get textual key of this passage. + QString textualKey() const; + + private: + + /* + * Types + */ + + /// A verse in a chapter in a book. + struct Verse + { + /// The headings preceeding this verse. + QString headings; + /// The main HTML content of this verse. + QString content; + /// @todo Footnotes + }; + + /// A chapter in a book containing verses. + struct Chapter + { + /// The number of the first verse in this chapter. + int firstVerse; + /// The number of verses in this chapter. + int numVerses; + /// The array of verses. + Verse* verses; + }; + + /// A book containing chapters. + struct Book + { + /// The name of this book. + QString name; + /// The number of the first chapter in this book. + int firstChapter; + /// The number of chapters in this book. + int numChapters; + /// The array of chapters. + Chapter* chapters; + }; + + /* + * Variables + */ + + /// Bible manager id. + QString m_managerId; + /// Bible module id. + QString m_moduleId; + /// Original module key. + KwBibleModule::Key m_key; + + /// The number of the first book. + int m_firstBook; + /// The number of books. + int m_numBooks; + /// The array of books. + Book* m_books; +}; + +#endif // _KwBiblePassage_h_ + diff --git a/kworship/bible/KwBiblePlaylistItem.cpp b/kworship/bible/KwBiblePlaylistItem.cpp index 47e3789..58738ec 100644 --- a/kworship/bible/KwBiblePlaylistItem.cpp +++ b/kworship/bible/KwBiblePlaylistItem.cpp @@ -32,9 +32,10 @@ KW_REGISTER_PLAYLIST_ITEM(KwBiblePlaylistItem, "bible") * Constructors + destructor. */ -/// Default constructor. -KwBiblePlaylistItem::KwBiblePlaylistItem() +/// Construct from reference information. +KwBiblePlaylistItem::KwBiblePlaylistItem(const QString& managerName, const QString& moduleName, const KwBibleModule::Key& moduleKey) : KwPlaylistItem() +, m_passage(managerName, moduleName, moduleKey) { } @@ -73,3 +74,13 @@ KwPlaylistNode* KwBiblePlaylistItem::getNode(KwPlaylistNode* parent) return new KwBiblePlaylistItemNode(parent, this); } +/* + * Accessors + */ + +/// Get the bible passage. +const KwBiblePassage& KwBiblePlaylistItem::passage() const +{ + return m_passage; +} + diff --git a/kworship/bible/KwBiblePlaylistItem.h b/kworship/bible/KwBiblePlaylistItem.h index adba549..f79e063 100644 --- a/kworship/bible/KwBiblePlaylistItem.h +++ b/kworship/bible/KwBiblePlaylistItem.h @@ -26,8 +26,13 @@ * @author James Hogan */ +#include "KwBibleModule.h" +#include "KwBiblePassage.h" + #include +#include + /// A playlist item for bible passages. class KwBiblePlaylistItem : public KwPlaylistItem { @@ -39,8 +44,12 @@ class KwBiblePlaylistItem : public KwPlaylistItem * Constructors + destructor. */ - /// Default constructor. - KwBiblePlaylistItem(); + /** Construct from reference information. + * @param managerName Identifier of bible manager. + * @param moduleName Identifier of bible module. + * @param moduleKey Bible passage key. + */ + KwBiblePlaylistItem(const QString& managerName, const QString& moduleName, const KwBibleModule::Key& moduleKey); /// Construct from a DOM element. KwBiblePlaylistItem(const QDomElement& element, KwResourceManager* resourceManager); @@ -63,6 +72,22 @@ class KwBiblePlaylistItem : public KwPlaylistItem */ virtual KwPlaylistNode* getNode(KwPlaylistNode* parent); + + /* + * Accessors + */ + + /// Get the bible passage. + const KwBiblePassage& passage() const; + + private: + + /* + * Variables + */ + + /// The bible passage. + KwBiblePassage m_passage; }; #endif // _KwBiblePlaylistItem_h_ diff --git a/kworship/bible/KwBiblePlaylistItemNode.cpp b/kworship/bible/KwBiblePlaylistItemNode.cpp index 9bfc14e..b236d51 100644 --- a/kworship/bible/KwBiblePlaylistItemNode.cpp +++ b/kworship/bible/KwBiblePlaylistItemNode.cpp @@ -25,6 +25,7 @@ #include "KwBiblePlaylistItemNode.h" #include "KwBiblePlaylistItem.h" +#include "KwBiblePassage.h" #include @@ -54,7 +55,7 @@ QVariant KwBiblePlaylistItemNode::getData(int role, int column) { if (column == 0) { - return "Bible Passage"; + return m_item->passage().textualKey(); } } else if (role == Qt::DecorationRole) -- 2.11.4.GIT