From f20995dd3bb6a5a92d173e7c0f47823206468263 Mon Sep 17 00:00:00 2001 From: James Hogan Date: Mon, 3 Nov 2008 10:43:36 +0000 Subject: [PATCH] implemented bible verse selection box --- kworship/bible/KwBibleModule.cpp | 139 ++++++++++++++++++++++++++++++++-- kworship/bible/KwBibleModule.h | 5 +- kworship/bible/KwBibleModuleSword.cpp | 4 +- kworship/kworship.cpp | 54 ++++++------- kworship/kworship.h | 1 - kworship/kworshipview_base.ui | 30 ++++++-- 6 files changed, 184 insertions(+), 49 deletions(-) diff --git a/kworship/bible/KwBibleModule.cpp b/kworship/bible/KwBibleModule.cpp index 3e4c6f8..09e846e 100644 --- a/kworship/bible/KwBibleModule.cpp +++ b/kworship/bible/KwBibleModule.cpp @@ -25,6 +25,8 @@ #include "KwBibleModule.h" +#include + /* * Constructors + destructor */ @@ -45,13 +47,138 @@ KwBibleModule::~KwBibleModule() */ /// Create a key from a string. -KwBibleModule::Key KwBibleModule::createKey(const QString& text) +KwBibleModule::Key KwBibleModule::createKey(const QString& text, bool* valid) +{ + // Find the key relative to the first chapter of the first book. + return createKey(createKey(0, 0), text); +} + +/// Create a key from a string relative to another key. +KwBibleModule::Key KwBibleModule::createKey(const Key& other, const QString& text, bool* valid) { - // parse the key - //Key key = { { -1, -1, -1 }, - // { -1, -1, -1 } }; - Key key = { { 0, 1, 1 }, - { 0, 1, 6 } }; + Key key = other; + static QRegExp re("^\\s*" // Must from beginning (ignore whitespace) + // 1( 2( 3() 4() ) ) + "(((\\S+)|\"([^\"]+)\")\\s+)?" // Optional book name followed by spaces + // 5( 6() ) + "((\\d+)[:.])?" // Optional chapter number followed by [:.] + // 7() + "(\\d+)?" // Optional verse number + // 8( + "(\\s*-\\s*" // Optionally start matching dash followed by end of the range + // 9( 10() ) + "((\\d+)[:.])?" // Optional chapter number followed by [:.] + // 11() + "(\\d+)?" // Optional verse number + // ) + ")?" // End of matching end of range + "\\s*$" // Must match to end (ignore whitespace) + ); + Q_ASSERT(re.isValid()); + bool isValid = true; + if (re.exactMatch(text)) + { + QStringList cap = re.capturedTexts(); + // Get book name + QString bookName; + if (!cap[3].isEmpty()) + { + bookName = cap[3]; + } + else if (!cap[4].isEmpty()) + { + bookName = cap[4]; + } + if (!bookName.isEmpty()) + { + int book = bookIndex(bookName); + if (book >= 0) + { + key.start.book = book; + } + else + { + isValid = false; + } + } + // Get chapter + QString chapterName = cap[6]; + if (!chapterName.isEmpty()) + { + int chapter = chapterName.toInt() - 1; + if (chapter >= 0 && chapter < numChapters(key.start.book)) + { + key.start.chapter = chapter; + } + else + { + isValid = false; + } + } + // Get verse + QString verseName = cap[7]; + if (!verseName.isEmpty()) + { + int verse = verseName.toInt() - 1; + if (verse >= 0 && verse < numVerses(key.start.book, key.start.chapter)) + { + key.start.verse = verse; + } + else + { + isValid = false; + } + } + // End of range + if (!cap[8].isEmpty()) + { + key.end.book = key.start.book; + key.end.chapter = key.start.chapter; + key.end.verse = -1; + // Get chapter + QString chapterName = cap[10]; + if (!chapterName.isEmpty()) + { + int chapter = chapterName.toInt() - 1; + if (chapter >= 0 && chapter < numChapters(key.start.book)) + { + key.end.chapter = chapter; + } + else + { + isValid = false; + } + } + // Get verse + QString verseName = cap[11]; + if (!verseName.isEmpty()) + { + int verse = verseName.toInt() - 1; + if (verse >= 0 && verse < numVerses(key.start.book, key.start.chapter)) + { + key.end.verse = verse; + } + else + { + isValid = false; + } + } + if (key.end.verse < 0) + { + key.end.verse = numVerses(key.end.book, key.end.chapter)-1; + } + } + } + else + { + isValid = false; + } + //std::cout << "result: " << key.start.book << "." << key.start.chapter << ":" << key.start.verse << " - " + // << key.end.book << "." << key.end.chapter << ":" << key.end.verse << std::endl; + if (0 != valid) + { + *valid = isValid; + } return key; } diff --git a/kworship/bible/KwBibleModule.h b/kworship/bible/KwBibleModule.h index 31b9aa1..5d17d6c 100644 --- a/kworship/bible/KwBibleModule.h +++ b/kworship/bible/KwBibleModule.h @@ -73,7 +73,10 @@ class KwBibleModule */ /// Create a key from a string. - Key createKey(const QString& text); + Key createKey(const QString& text, bool* valid = 0); + + /// Create a key from a string relative to another key. + Key createKey(const Key& other, const QString& text, bool* valid = 0); /// Create a key from individual values. Key createKey(int book, int chapter, int verse = -1); diff --git a/kworship/bible/KwBibleModuleSword.cpp b/kworship/bible/KwBibleModuleSword.cpp index 09b625e..01d7f42 100644 --- a/kworship/bible/KwBibleModuleSword.cpp +++ b/kworship/bible/KwBibleModuleSword.cpp @@ -122,7 +122,7 @@ QString KwBibleModuleSword::renderText(const KwBibleModule::Key& key) endBook = startBook; } int startChapter = key.start.chapter; - int endChapter = key.start.chapter; + int endChapter = key.end.chapter; if (startChapter < 0) { startChapter = 0; @@ -134,7 +134,7 @@ QString KwBibleModuleSword::renderText(const KwBibleModule::Key& key) } int startVerse = key.start.verse; - int endVerse = key.start.verse; + int endVerse = key.end.verse; if (startVerse < 0) { startVerse = 0; diff --git a/kworship/kworship.cpp b/kworship/kworship.cpp index eff8e8a..382bdb3 100644 --- a/kworship/kworship.cpp +++ b/kworship/kworship.cpp @@ -393,7 +393,9 @@ kworship::kworship() connect(m_view->comboBibleBook, SIGNAL(currentIndexChanged(int)), this, SLOT(bibleBookChanged())); connect(m_view->comboBibleChapter, SIGNAL(currentIndexChanged(int)), - this, SLOT(bibleChapterChanged())); + this, SLOT(bibleSearch())); + connect(m_view->searchBible, SIGNAL(textChanged(const QString&)), + this, SLOT(bibleSearch())); bibleChanged(); } @@ -1093,7 +1095,6 @@ void kworship::bibleConnect() } } -#include void kworship::bibleChanged() { // Get the current bible manager @@ -1143,14 +1144,10 @@ void kworship::bibleChanged() enabled = false; } - //bibleBookChanged(); m_view->comboBibleBook->setEnabled(enabled); m_view->comboBibleChapter->setEnabled(enabled); m_view->searchBible->setEnabled(enabled); m_view->textBible->setEnabled(enabled); - - m_view->searchBible->setText(QString()); - } } @@ -1188,9 +1185,10 @@ void kworship::bibleBookChanged() } } } + bibleSearch(); } -void kworship::bibleChapterChanged() +void kworship::bibleSearch() { // Get the current bible manager int tab = m_bibleTabs->currentIndex(); @@ -1211,40 +1209,34 @@ void kworship::bibleChapterChanged() { // Is a book selected? int bookIndex = m_view->comboBibleBook->currentIndex(); + int chapterIndex = -1; if (bookIndex >= 0) { // Is a chapter selected? - int chapterIndex = m_view->comboBibleChapter->currentIndex(); - if (chapterIndex >= 0) - { - KwBibleModule::Key key = module->createKey(bookIndex, chapterIndex); - m_view->textBible->document()->setHtml(module->renderText(key)); - return; - } + chapterIndex = m_view->comboBibleChapter->currentIndex(); } - } - } - m_view->textBible->document()->setPlainText(QString()); -} -void kworship::bibleSearch() -{ - /* - // Search using the key - int index = m_view->comboSwordBibles->currentIndex(); - if (index >= 0) - { - QString modName = m_view->comboSwordBibles->itemData(index).toString(); - KwBibleModule* module = KwBibleManager::self()->module(modName); - if (0 != module) - { - KwBibleModule::Key key = module->createKey(m_view->searchBible->text()); + bool valid; + KwBibleModule::Key relativeKey = module->createKey(bookIndex, chapterIndex); + KwBibleModule::Key key = module->createKey(relativeKey, m_view->searchBible->text(), &valid); + // Update book and chapter + m_view->comboBibleBook->setCurrentIndex(key.start.book); + m_view->comboBibleChapter->setCurrentIndex(key.start.chapter); m_view->textBible->document()->setHtml(module->renderText(key)); + + // Update color of search box + static QPalette p = m_view->searchBible->palette(); + QPalette changedPal = p; + if (!valid) + { + changedPal.setColor( QPalette::Normal, QPalette::Base, QColor(255, 127, 127) ); + } + m_view->searchBible->setPalette(changedPal); + return; } } m_view->textBible->document()->setPlainText(QString()); - */ } #include "kworship.moc" diff --git a/kworship/kworship.h b/kworship/kworship.h index eb1a155..fe2644e 100644 --- a/kworship/kworship.h +++ b/kworship/kworship.h @@ -133,7 +133,6 @@ private slots: void bibleConnect(); void bibleChanged(); void bibleBookChanged(); - void bibleChapterChanged(); void bibleSearch(); private: diff --git a/kworship/kworshipview_base.ui b/kworship/kworshipview_base.ui index 4faa279..6e4d731 100644 --- a/kworship/kworshipview_base.ui +++ b/kworship/kworshipview_base.ui @@ -185,14 +185,28 @@ - - - - 10 - 0 - - - + + + + + &Verses + + + searchBible + + + + + + + + 10 + 0 + + + + + -- 2.11.4.GIT