Simple plugin system and added bibles docker to it
[kworship.git] / kworship / KwBiblePlugin.cpp
blobcdeab9d0cb6e11149b86ecbdc69f09d46884efc4
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20 /**
21 * @file KwBiblePlugin.cpp
22 * @brief Bibles plugin.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwBiblePlugin.h"
28 #include <KwBibleManager.h>
29 #include <KwBibleManagerSword.h>
30 #include <KwBibleManagerBibleGateway.h>
31 #include <KwBibleModule.h>
33 #include <KLocale>
34 #include <KAction>
35 #include <KMessageBox>
37 #include <QMainWindow>
38 #include <QDockWidget>
39 #include <QHBoxLayout>
40 #include <QVBoxLayout>
41 #include <QComboBox>
42 #include <QLabel>
43 #include <QLineEdit>
44 #include <QTextEdit>
45 #include <QToolBar>
48 * Constructors + destructor
51 /// Default constructor.
52 KwBiblePlugin::KwBiblePlugin()
53 : KwPlugin("bible",
54 i18n("Bible"),
55 i18n("The bible plugin allows for the navigation and display of "
56 "bible extracts from SWORD and BibleGateway.com."))
57 , m_managers()
58 , m_docker(0)
59 , m_managerTabs(0)
60 , m_comboBook(0)
61 , m_comboChapter(0)
62 , m_editRange(0)
63 , m_textPassage(0)
67 /// Destructor.
68 KwBiblePlugin::~KwBiblePlugin()
70 // Ensure all GUI stuff is cleaned up
71 KwBiblePlugin::_unload();
75 * Private slots
78 /// Fired when the connect to bible button is pressed.
79 void KwBiblePlugin::slotConnect()
81 // Get the current bible manager
82 int tab = m_managerTabs->currentIndex();
83 Q_ASSERT(tab >= 0 && tab < m_managers.size());
84 BibleManager* mgr = &m_managers[tab];
86 // This will force the connection
87 fillBiblesList(mgr);
88 if (mgr->comboBibles->count() == 0)
90 KMessageBox::information(m_docker, i18n("No bibles found"));
92 else
94 mgr->comboBibles->setEnabled(true);
98 /// Fired when the bible is changed.
99 void KwBiblePlugin::slotBibleChanged()
101 // Get the current bible manager
102 int tab = m_managerTabs->currentIndex();
103 if (tab >= 0 && tab < m_managers.size())
105 BibleManager& mgr = m_managers[tab];
106 bool enabled = mgr.comboBibles->isEnabled();
108 // Is a bible selected?
109 int bibleInd = mgr.comboBibles->currentIndex();
110 QString bible;
111 KwBibleModule* module = 0;
112 if (bibleInd >= 0)
114 QString bible = mgr.comboBibles->itemData(bibleInd).toString();
115 module = mgr.manager->module(bible);
118 // Update the list of books
119 QString book = m_comboBook->currentText();
120 int chapter = m_comboChapter->currentIndex();
121 m_comboBook->clear();
122 if (0 != module)
124 QStringList bookNames = module->books();
125 for (int i = 0; i < bookNames.size(); ++i)
127 const QString& bookName = bookNames[i];
128 m_comboBook->addItem(bookName, QVariant(i));
130 int index = m_comboBook->findText(book);
131 bool canPreserveBook = (index >= 0);
132 if (!canPreserveBook && bookNames.size() > 0)
134 index = 0;
136 m_comboBook->setCurrentIndex(index);
137 if (canPreserveBook && chapter >= 0)
139 // If we can restore book, also restore chapter
140 m_comboChapter->setCurrentIndex(chapter);
143 else
145 enabled = false;
148 m_comboBook->setEnabled(enabled);
149 m_comboChapter->setEnabled(enabled);
150 m_editRange->setEnabled(enabled);
154 /// Fired when the bible book is changed.
155 void KwBiblePlugin::slotBookChanged()
157 m_comboChapter->clear();
159 // Get the current bible manager
160 int tab = m_managerTabs->currentIndex();
161 if (tab >= 0 && tab < m_managers.size())
163 BibleManager& mgr = m_managers[tab];
165 // Is a bible selected?
166 int bibleInd = mgr.comboBibles->currentIndex();
167 QString bible;
168 KwBibleModule* module = 0;
169 if (bibleInd >= 0)
171 QString bible = mgr.comboBibles->itemData(bibleInd).toString();
172 module = mgr.manager->module(bible);
174 if (0 != module)
176 // Is a book selected?
177 int index = m_comboBook->currentIndex();
178 if (index >= 0)
180 int bookIndex = m_comboBook->itemData(index).toInt();
181 int numChapters = module->numChapters(bookIndex);
182 for (int i = 0; i < numChapters; ++i)
184 m_comboChapter->addItem(QString("%1").arg(i+1), QVariant(i));
189 slotVerseRange();
192 /// Fired when the bible text needs to be retrieved.
193 void KwBiblePlugin::slotVerseRange()
195 // Get the current bible manager
196 int tab = m_managerTabs->currentIndex();
197 if (tab >= 0 && tab < m_managers.size())
199 BibleManager& mgr = m_managers[tab];
201 // Is a bible selected?
202 int bibleInd = mgr.comboBibles->currentIndex();
203 QString bible;
204 KwBibleModule* module = 0;
205 if (bibleInd >= 0)
207 QString bible = mgr.comboBibles->itemData(bibleInd).toString();
208 module = mgr.manager->module(bible);
210 if (0 != module)
212 // Is a book selected?
213 int bookIndex = m_comboBook->currentIndex();
214 int chapterIndex = -1;
215 if (bookIndex >= 0)
217 // Is a chapter selected?
218 chapterIndex = m_comboChapter->currentIndex();
221 bool valid;
222 KwBibleModule::Key relativeKey = module->createKey(bookIndex, chapterIndex);
223 KwBibleModule::Key key = module->createKey(relativeKey, m_editRange->text(), &valid);
224 // Update book and chapter
225 m_comboBook->setCurrentIndex(key.start.book);
226 m_comboChapter->setCurrentIndex(key.start.chapter);
227 m_textPassage->document()->setHtml(module->renderText(key));
229 // Update color of search box
230 static QPalette p = m_editRange->palette();
231 QPalette changedPal = p;
232 if (!valid)
234 changedPal.setColor( QPalette::Normal, QPalette::Base, QColor(255, 127, 127) );
236 m_editRange->setPalette(changedPal);
238 return;
241 m_textPassage->document()->setPlainText(QString());
245 * Loading and unloading virtual interface
248 void KwBiblePlugin::_load(QMainWindow* mainWindow)
250 // Construct the bible managers.
251 QList<KwBibleManager*> managers;
252 managers.push_back(new KwBibleManagerSword);
253 managers.push_back(new KwBibleManagerBibleGateway);
255 // Set up the docker
256 m_docker = new QDockWidget(i18n("Bible"));
257 m_docker->setObjectName("dockBible");
258 QWidget* mainDockWidget = new QWidget(/*m_docker*/);
259 QVBoxLayout* dockLayout = new QVBoxLayout(mainDockWidget);
260 m_docker->setWidget(mainDockWidget);
262 // The tab bar of bible managers
263 m_managerTabs = new QTabWidget(m_docker);
264 m_managerTabs->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
265 connect(m_managerTabs, SIGNAL(currentChanged(int)),
266 this, SLOT(slotBibleChanged()));
267 dockLayout->addWidget(m_managerTabs);
269 // Bible navigation combo boxes
270 QWidget* bibleNavigationWidget = new QWidget(mainDockWidget);
271 QHBoxLayout* bibleNavigationLayout = new QHBoxLayout(bibleNavigationWidget);
272 dockLayout->addWidget(bibleNavigationWidget);
274 m_comboBook = new QComboBox(bibleNavigationWidget);
275 m_comboChapter = new QComboBox(bibleNavigationWidget);
276 bibleNavigationLayout->addWidget(m_comboBook);
277 bibleNavigationLayout->addWidget(m_comboChapter);
279 // Verse range text box and label
280 QWidget* verseRangeWidget = new QWidget(mainDockWidget);
281 QHBoxLayout* verseRangeLayout = new QHBoxLayout(verseRangeWidget);
282 dockLayout->addWidget(verseRangeWidget);
284 QLabel* labelRange = new QLabel(i18n("&Verses"), verseRangeWidget);
285 m_editRange = new QLineEdit(verseRangeWidget);
286 labelRange->setBuddy(m_editRange);
287 verseRangeLayout->addWidget(labelRange);
288 verseRangeLayout->addWidget(m_editRange);
290 m_textPassage = new QTextEdit(mainDockWidget);
291 m_textPassage->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
292 dockLayout->addWidget(m_textPassage);
295 // Fill out tabs and manager data
296 foreach (KwBibleManager* manager, managers)
298 QString name = manager->name();
299 BibleManager mgr;
300 mgr.manager = manager;
302 QWidget* tabWidget = new QWidget();
303 QHBoxLayout* layout = new QHBoxLayout(tabWidget);
305 mgr.comboBibles = new QComboBox();
306 connect(mgr.comboBibles, SIGNAL(currentIndexChanged(int)),
307 this, SLOT(slotBibleChanged()));
309 // Only fill the module list if the manager is local
310 // Otherwise we should wait until the user requests it
311 if (manager->isRemote())
313 mgr.comboBibles->setEnabled(false);
315 mgr.toolBar = new QToolBar();
316 KAction* connectToBible = new KAction(KIcon("network-connect"), i18n("Connect to %1", name), mgr.toolBar);
317 connect(connectToBible, SIGNAL(triggered(bool)),
318 this, SLOT(slotConnect()));
319 mgr.toolBar->addAction(connectToBible);
320 layout->addWidget(mgr.toolBar);
322 // Toolbar should be as small as possible
323 QSizePolicy policy;
325 policy = mgr.toolBar->sizePolicy();
326 policy.setHorizontalStretch(1);
327 mgr.toolBar->setSizePolicy(policy);
329 policy = mgr.comboBibles->sizePolicy();
330 policy.setHorizontalStretch(10);
331 mgr.comboBibles->setSizePolicy(policy);
333 else
335 mgr.toolBar = 0;
336 fillBiblesList(&mgr);
339 layout->addWidget(mgr.comboBibles);
341 m_managerTabs->addTab(tabWidget, name);
342 m_managers.push_back(mgr);
344 // Ensure widgets are apropriately modified
345 connect(m_comboBook, SIGNAL(currentIndexChanged(int)),
346 this, SLOT(slotBookChanged()));
347 connect(m_comboChapter, SIGNAL(currentIndexChanged(int)),
348 this, SLOT(slotVerseRange()));
349 connect(m_editRange, SIGNAL(textChanged(const QString&)),
350 this, SLOT(slotVerseRange()));
352 mainWindow->addDockWidget(Qt::LeftDockWidgetArea, m_docker);
354 slotBibleChanged();
357 void KwBiblePlugin::_unload()
359 // Clean up the bible managers.
360 /// @todo IMPLEMENT!!!
361 delete m_docker;
365 * Private functions
368 /// Fill up the bibles list for a manager.
369 void KwBiblePlugin::fillBiblesList(BibleManager* mgr)
371 QStringList languages = mgr->manager->languages();
372 mgr->comboBibles->clear();
373 mgr->comboBibles->addItem(i18n("-- select a translation --"));
374 foreach (QString language, languages)
376 QStringList modules = mgr->manager->moduleNamesInLanguage(language);
377 if (!modules.isEmpty())
379 mgr->comboBibles->addItem(language, QVariant(modules.first()));
380 foreach (QString module, modules)
382 mgr->comboBibles->addItem(" " + module, QVariant(module));