From bcd57be38461cddd2da140bdcb040e6e7d8b0b6b Mon Sep 17 00:00:00 2001 From: James Hogan Date: Tue, 23 Dec 2008 13:47:47 +0000 Subject: [PATCH] Bible passage playlist item can save / load with DOM Mostly implemented in KwBiblePassage --- design/bible.txt | 2 +- kworship/bible/KwBiblePassage.cpp | 267 +++++++++++++++++++++++++++++++-- kworship/bible/KwBiblePassage.h | 15 +- kworship/bible/KwBiblePlaylistItem.cpp | 14 +- 4 files changed, 280 insertions(+), 18 deletions(-) diff --git a/design/bible.txt b/design/bible.txt index e7e53a0..938b5c9 100644 --- a/design/bible.txt +++ b/design/bible.txt @@ -8,7 +8,7 @@ features [ ] go to selected bible passage in playlist [ ] extend passage [ ] quick access to bibletime bookmarks - [ ] save/load bible passages in playlist + [X] save/load bible passages in playlist [ ] "show now" button [ ] different style during display [ ] footnote extraction diff --git a/kworship/bible/KwBiblePassage.cpp b/kworship/bible/KwBiblePassage.cpp index 9d978d4..996b679 100644 --- a/kworship/bible/KwBiblePassage.cpp +++ b/kworship/bible/KwBiblePassage.cpp @@ -27,6 +27,9 @@ #include +#include +#include + /* * Constructors + destructor */ @@ -35,7 +38,6 @@ KwBiblePassage::KwBiblePassage() : m_managerId() , m_moduleId() -, m_key() , m_rightToLeft(false) , m_firstBook(0) , m_numBooks(0) @@ -49,6 +51,238 @@ KwBiblePassage::~KwBiblePassage() } /* + * DOM filters + */ + +/// Import the passage in a DOM. +void KwBiblePassage::importFromDom(const QDomElement& element) +{ + // Manager id element + QDomElement managerElem = element.firstChildElement("manager"); + if (!managerElem.isNull()) + { + m_managerId = managerElem.text(); + } + else + { + m_managerId = QString(); + } + + // Module id element + QDomElement moduleElem = element.firstChildElement("module"); + if (!moduleElem.isNull()) + { + m_moduleId = moduleElem.text(); + } + else + { + m_moduleId = QString(); + } + + // Text direction element + QDomElement directionElem = element.firstChildElement("direction"); + if (!directionElem.isNull()) + { + m_rightToLeft = (directionElem.text().toLower() == "rtl"); + } + else + { + m_rightToLeft = false; + } + + bool success; + + // Books element + QDomElement booksElem = element.firstChildElement("books"); + if (!booksElem.isNull()) + { + int firstBook = booksElem.attribute("first").toInt(&success); + if (!success) + { + firstBook = 0; + } + int numBooks = booksElem.attribute("count").toInt(&success); + if (!success) + { + numBooks = 0; + } + initBooks(firstBook, numBooks); + + // Book elements + QDomNodeList books = booksElem.elementsByTagName("book"); + for (int book = 0; book < books.count(); ++book) + { + QDomElement bookElem = books.at(book).toElement(); + + int bookId = bookElem.attribute("num").toInt(&success); + if (success && bookId >= m_firstBook && bookId < m_firstBook+m_numBooks) + { + // Chapters element + QDomElement chaptersElem = bookElem.firstChildElement("chapters"); + if (!chaptersElem.isNull()) + { + int firstChapter = chaptersElem.attribute("first").toInt(&success); + if (!success) + { + firstChapter = 0; + } + int numChapters = chaptersElem.attribute("count").toInt(&success); + if (!success) + { + numChapters = 0; + } + initBook(bookId, bookElem.attribute("name", "unknown"), firstChapter, numChapters); + + // Chapter elements + QDomNodeList chapters = chaptersElem.elementsByTagName("chapter"); + for (int chapter = 0; chapter < chapters.count(); ++chapter) + { + QDomElement chapterElem = chapters.at(chapter).toElement(); + + int chapterId = chapterElem.attribute("num").toInt(&success); + if (success && chapterId >= firstChapter && chapterId < firstChapter+numChapters) + { + // Verses element + QDomElement versesElem = chapterElem.firstChildElement("verses"); + if (!versesElem.isNull()) + { + int firstVerse = versesElem.attribute("first").toInt(&success); + if (!success) + { + firstVerse = 0; + } + int numVerses = versesElem.attribute("count").toInt(&success); + if (!success) + { + numVerses = 0; + } + initChapter(bookId, chapterId, firstVerse, numVerses); + + // Verse elements + QDomNodeList verses = versesElem.elementsByTagName("verse"); + for (int verse = 0; verse < verses.count(); ++verse) + { + QDomElement verseElem = verses.at(verse).toElement(); + + int verseId = verseElem.attribute("num").toInt(&success); + if (success && verseId >= firstVerse && verseId < firstVerse+numVerses) + { + QString headings, content; + + // Headings element + QDomElement headingsElem = verseElem.firstChildElement("headings"); + if (!headingsElem.isNull()) + { + headings = headingsElem.text(); + } + + // Content element + QDomElement contentElem = verseElem.firstChildElement("content"); + if (!contentElem.isNull()) + { + content = contentElem.text(); + } + + initVerse(bookId, chapterId, verseId, headings, content); + } + } + } + } + } + } + } + } + } +} + +/// Export the passage using DOM. +void KwBiblePassage::exportToDom(QDomDocument& document, QDomElement& element) const +{ + // Manager id element + QDomElement managerElem = document.createElement("manager"); + element.appendChild(managerElem); + managerElem.appendChild(document.createTextNode(m_managerId)); + + // Module id element + QDomElement moduleElem = document.createElement("module"); + element.appendChild(moduleElem); + moduleElem.appendChild(document.createTextNode(m_moduleId)); + + // Text direction element + QDomElement directionElem = document.createElement("direction"); + element.appendChild(directionElem); + directionElem.appendChild(document.createTextNode(m_rightToLeft ? "rtl" : "ltr")); + + // Books element + QDomElement booksElem = document.createElement("books"); + element.appendChild(booksElem); + booksElem.setAttribute("first", m_firstBook); + booksElem.setAttribute("count", m_numBooks); + + Book* book = m_books; + for (int bookInd = 0; bookInd < m_numBooks; ++bookInd) + { + // Book element + QDomElement bookElem = document.createElement("book"); + booksElem.appendChild(bookElem); + bookElem.setAttribute("num", m_firstBook + bookInd); + bookElem.setAttribute("name", book->name); + + // Chapters element + QDomElement chaptersElem = document.createElement("chapters"); + bookElem.appendChild(chaptersElem); + chaptersElem.setAttribute("first", book->firstChapter); + chaptersElem.setAttribute("count", book->numChapters); + + Chapter* chapter = book->chapters; + for (int chapterInd = 0; chapterInd < book->numChapters; ++chapterInd) + { + // Chapter element + QDomElement chapterElem = document.createElement("chapter"); + chaptersElem.appendChild(chapterElem); + chapterElem.setAttribute("num", book->firstChapter + chapterInd); + + // Verses element + QDomElement versesElem = document.createElement("verses"); + chapterElem.appendChild(versesElem); + versesElem.setAttribute("first", chapter->firstVerse); + versesElem.setAttribute("count", chapter->numVerses); + + Verse* verse = chapter->verses; + for (int verseInd = 0; verseInd < chapter->numVerses; ++verseInd) + { + // Verse element + QDomElement verseElem = document.createElement("verse"); + versesElem.appendChild(verseElem); + verseElem.setAttribute("num", chapter->firstVerse + verseInd); + + // Headings + if (!verse->headings.isEmpty()) + { + QDomElement headingsElem = document.createElement("headings"); + verseElem.appendChild(headingsElem); + headingsElem.appendChild(document.createTextNode(verse->headings)); + } + + // Content + if (!verse->content.isEmpty()) + { + QDomElement contentElem = document.createElement("content"); + verseElem.appendChild(contentElem); + contentElem.appendChild(document.createTextNode(verse->content)); + } + + ++verse; + } + + ++chapter; + } + + ++book; + } +} + +/* * Main interface */ @@ -114,12 +348,15 @@ void KwBiblePassage::initBooks(int firstBook, int numBooks) clearBooks(); m_firstBook = firstBook; m_numBooks = numBooks; - m_books = new Book[numBooks]; - for (int i = 0; i < numBooks; ++i) + if (m_numBooks > 0) { - m_books[i].firstChapter = -1; - m_books[i].numChapters = 0; - m_books[i].chapters = 0; + m_books = new Book[numBooks]; + for (int i = 0; i < numBooks; ++i) + { + m_books[i].firstChapter = -1; + m_books[i].numChapters = 0; + m_books[i].chapters = 0; + } } } @@ -133,12 +370,15 @@ void KwBiblePassage::initBook(int bookNumber, book->name = name; book->firstChapter = firstChapter; book->numChapters = numChapters; - book->chapters = new Chapter[numChapters]; - for (int i = 0; i < numChapters; ++i) + if (numChapters > 0) { - book->chapters[i].firstVerse = -1; - book->chapters[i].numVerses = 0; - book->chapters[i].verses = 0; + book->chapters = new Chapter[numChapters]; + for (int i = 0; i < numChapters; ++i) + { + book->chapters[i].firstVerse = -1; + book->chapters[i].numVerses = 0; + book->chapters[i].verses = 0; + } } } @@ -152,7 +392,10 @@ void KwBiblePassage::initChapter(int bookNumber, int chapterNumber, Chapter* chapter = &m_books[bookNumber].chapters[chapterNumber]; chapter->firstVerse = firstVerse; chapter->numVerses = numVerses; - chapter->verses = new Verse[numVerses]; + if (numVerses > 0) + { + chapter->verses = new Verse[numVerses]; + } } /// Initialise a verse in the passage. diff --git a/kworship/bible/KwBiblePassage.h b/kworship/bible/KwBiblePassage.h index 365272c..218e728 100644 --- a/kworship/bible/KwBiblePassage.h +++ b/kworship/bible/KwBiblePassage.h @@ -30,6 +30,9 @@ #include +class QDomDocument; +class QDomElement; + /** A passage of rendered bible text. * Stores enough information to adjust settings. * Can be used as a cache. @@ -49,6 +52,16 @@ class KwBiblePassage virtual ~KwBiblePassage(); /* + * DOM filters + */ + + /// Import the passage in a DOM. + void importFromDom(const QDomElement& element); + + /// Export the passage using DOM. + void exportToDom(QDomDocument& document, QDomElement& element) const; + + /* * Main interface */ @@ -218,8 +231,6 @@ class KwBiblePassage QString m_managerId; /// Bible module id. QString m_moduleId; - /// Original module key. - KwBibleModule::Key m_key; /// Whether the text is right-to-left. bool m_rightToLeft; diff --git a/kworship/bible/KwBiblePlaylistItem.cpp b/kworship/bible/KwBiblePlaylistItem.cpp index e9aacdc..6e3bd6e 100644 --- a/kworship/bible/KwBiblePlaylistItem.cpp +++ b/kworship/bible/KwBiblePlaylistItem.cpp @@ -42,9 +42,15 @@ KwBiblePlaylistItem::KwBiblePlaylistItem(KwBibleModule* module, const KwBibleMod /// Construct from a DOM element. KwBiblePlaylistItem::KwBiblePlaylistItem(const QDomElement& element, KwResourceManager* resourceManager) -: KwPlaylistItem() +: KwPlaylistItem(element, resourceManager) +, m_passage() { - /// @todo Implement bible passage import. + QDomElement passageElem = element.firstChildElement("passage"); + if (!passageElem.isNull()) + { + elementHandled("passage"); + m_passage.importFromDom(passageElem); + } } /// Destructor. @@ -63,7 +69,9 @@ QString KwBiblePlaylistItem::itemType() const void KwBiblePlaylistItem::exportDetailsToDom(QDomDocument& document, QDomElement& element, KwResourceManager* resourceManager) const { - /// @todo Implement bible passage export. + QDomElement passageElem = document.createElement("passage"); + element.appendChild(passageElem); + m_passage.exportToDom(document, passageElem); } /* -- 2.11.4.GIT