Add toolbar to bible plugin with show now, insert into playlist
[kworship.git] / kworship / KwBiblePlugin.cpp
blobab5dc8a971b50dcbf83da3a803040d7056b0275a
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_insertIntoPlaylistAction(0)
59 , m_showNowAction(0)
60 , m_docker(0)
61 , m_managerTabs(0)
62 , m_comboBook(0)
63 , m_comboChapter(0)
64 , m_editRange(0)
65 , m_textPassage(0)
69 /// Destructor.
70 KwBiblePlugin::~KwBiblePlugin()
72 // Ensure all GUI stuff is cleaned up
73 KwBiblePlugin::_unload();
77 * Private slots
80 /// Fired when the connect to bible button is pressed.
81 void KwBiblePlugin::slotConnect()
83 // Get the current bible manager
84 int tab = m_managerTabs->currentIndex();
85 Q_ASSERT(tab >= 0 && tab < m_managers.size());
86 BibleManager* mgr = &m_managers[tab];
88 // This will force the connection
89 fillBiblesList(mgr);
90 if (mgr->comboBibles->count() == 0)
92 KMessageBox::information(m_docker, i18n("No bibles found"));
94 else
96 mgr->comboBibles->setEnabled(true);
100 /// Fired when the bible is changed.
101 void KwBiblePlugin::slotBibleChanged()
103 // Get the current bible manager
104 int tab = m_managerTabs->currentIndex();
105 if (tab >= 0 && tab < m_managers.size())
107 BibleManager& mgr = m_managers[tab];
108 bool enabled = mgr.comboBibles->isEnabled();
110 // Is a bible selected?
111 int bibleInd = mgr.comboBibles->currentIndex();
112 QString bible;
113 KwBibleModule* module = 0;
114 if (bibleInd >= 0)
116 QString bible = mgr.comboBibles->itemData(bibleInd).toString();
117 module = mgr.manager->module(bible);
120 // Update the list of books
121 QString book = m_comboBook->currentText();
122 int chapter = m_comboChapter->currentIndex();
123 m_comboBook->clear();
124 if (0 != module)
126 QStringList bookNames = module->books();
127 for (int i = 0; i < bookNames.size(); ++i)
129 const QString& bookName = bookNames[i];
130 m_comboBook->addItem(bookName, QVariant(i));
132 int index = m_comboBook->findText(book);
133 bool canPreserveBook = (index >= 0);
134 if (!canPreserveBook && bookNames.size() > 0)
136 index = 0;
138 m_comboBook->setCurrentIndex(index);
139 if (canPreserveBook && chapter >= 0)
141 // If we can restore book, also restore chapter
142 m_comboChapter->setCurrentIndex(chapter);
145 else
147 enabled = false;
150 m_comboBook->setEnabled(enabled);
151 m_comboChapter->setEnabled(enabled);
152 m_editRange->setEnabled(enabled);
156 /// Fired when the bible book is changed.
157 void KwBiblePlugin::slotBookChanged()
159 m_comboChapter->clear();
161 // Get the current bible manager
162 int tab = m_managerTabs->currentIndex();
163 if (tab >= 0 && tab < m_managers.size())
165 BibleManager& mgr = m_managers[tab];
167 // Is a bible selected?
168 int bibleInd = mgr.comboBibles->currentIndex();
169 QString bible;
170 KwBibleModule* module = 0;
171 if (bibleInd >= 0)
173 QString bible = mgr.comboBibles->itemData(bibleInd).toString();
174 module = mgr.manager->module(bible);
176 if (0 != module)
178 // Is a book selected?
179 int index = m_comboBook->currentIndex();
180 if (index >= 0)
182 int bookIndex = m_comboBook->itemData(index).toInt();
183 int numChapters = module->numChapters(bookIndex);
184 for (int i = 0; i < numChapters; ++i)
186 m_comboChapter->addItem(QString("%1").arg(i+1), QVariant(i));
191 slotVerseRange();
194 /// Fired when the bible text needs to be retrieved.
195 void KwBiblePlugin::slotVerseRange()
197 // Get the current bible manager
198 int tab = m_managerTabs->currentIndex();
199 if (tab >= 0 && tab < m_managers.size())
201 BibleManager& mgr = m_managers[tab];
203 // Is a bible selected?
204 int bibleInd = mgr.comboBibles->currentIndex();
205 QString bible;
206 KwBibleModule* module = 0;
207 if (bibleInd >= 0)
209 QString bible = mgr.comboBibles->itemData(bibleInd).toString();
210 module = mgr.manager->module(bible);
212 if (0 != module)
214 // Is a book selected?
215 int bookIndex = m_comboBook->currentIndex();
216 int chapterIndex = -1;
217 if (bookIndex >= 0)
219 // Is a chapter selected?
220 chapterIndex = m_comboChapter->currentIndex();
223 bool valid;
224 KwBibleModule::Key relativeKey = module->createKey(bookIndex, chapterIndex);
225 KwBibleModule::Key key = module->createKey(relativeKey, m_editRange->text(), &valid);
226 // Update book and chapter
227 m_comboBook->setCurrentIndex(key.start.book);
228 m_comboChapter->setCurrentIndex(key.start.chapter);
229 m_textPassage->document()->setHtml(module->renderText(key));
231 m_insertIntoPlaylistAction->setEnabled(true);
232 m_showNowAction->setEnabled(true);
234 // Update color of search box
235 static QPalette p = m_editRange->palette();
236 QPalette changedPal = p;
237 if (!valid)
239 changedPal.setColor( QPalette::Normal, QPalette::Base, QColor(255, 127, 127) );
241 m_editRange->setPalette(changedPal);
243 return;
246 m_textPassage->document()->setPlainText(QString());
247 m_insertIntoPlaylistAction->setEnabled(false);
248 m_showNowAction->setEnabled(false);
251 /// Fired by the insert into playlist action.
252 void KwBiblePlugin::slotInsertIntoPlaylist()
256 /// Fired by the show now action.
257 void KwBiblePlugin::slotShowNow()
262 * Loading and unloading virtual interface
265 void KwBiblePlugin::_load(QMainWindow* mainWindow)
267 // Construct the bible managers.
268 QList<KwBibleManager*> managers;
269 managers.push_back(new KwBibleManagerSword);
270 managers.push_back(new KwBibleManagerBibleGateway);
272 // Set up the docker
273 m_docker = new QDockWidget(i18n("Bible"));
274 m_docker->setObjectName("dockBible");
275 QWidget* mainDockWidget = new QWidget(/*m_docker*/);
276 QVBoxLayout* dockLayout = new QVBoxLayout(mainDockWidget);
277 m_docker->setWidget(mainDockWidget);
279 // The tab bar of bible managers
280 m_managerTabs = new QTabWidget(m_docker);
281 m_managerTabs->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
282 connect(m_managerTabs, SIGNAL(currentChanged(int)),
283 this, SLOT(slotBibleChanged()));
284 dockLayout->addWidget(m_managerTabs);
286 // Bible navigation combo boxes
287 QWidget* bibleNavigationWidget = new QWidget(mainDockWidget);
288 QHBoxLayout* bibleNavigationLayout = new QHBoxLayout(bibleNavigationWidget);
289 dockLayout->addWidget(bibleNavigationWidget);
291 m_comboBook = new QComboBox(bibleNavigationWidget);
292 m_comboChapter = new QComboBox(bibleNavigationWidget);
293 bibleNavigationLayout->addWidget(m_comboBook);
294 bibleNavigationLayout->addWidget(m_comboChapter);
296 // Verse range text box and label
297 QWidget* verseRangeWidget = new QWidget(mainDockWidget);
298 QHBoxLayout* verseRangeLayout = new QHBoxLayout(verseRangeWidget);
299 dockLayout->addWidget(verseRangeWidget);
301 QLabel* labelRange = new QLabel(i18n("&Verses"), verseRangeWidget);
302 m_editRange = new QLineEdit(verseRangeWidget);
303 labelRange->setBuddy(m_editRange);
304 verseRangeLayout->addWidget(labelRange);
305 verseRangeLayout->addWidget(m_editRange);
307 m_textPassage = new QTextEdit(mainDockWidget);
308 m_textPassage->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
309 dockLayout->addWidget(m_textPassage);
311 // Toolbar
312 QToolBar* bibleToolBar = new QToolBar("bibleToolBar");
313 bibleToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
314 dockLayout->addWidget(bibleToolBar);
316 m_insertIntoPlaylistAction = new KAction(KIcon("player_playlist"), i18n("Insert Into Playlist"), bibleToolBar);
317 m_insertIntoPlaylistAction->setEnabled(false);
318 connect(m_insertIntoPlaylistAction, SIGNAL(triggered(bool)),
319 this, SLOT(slotInsertIntoPlaylist()));
320 bibleToolBar->addAction(m_insertIntoPlaylistAction);
322 m_showNowAction = new KAction(KIcon("player_playlist"), i18n("Show Now"), bibleToolBar);
323 m_showNowAction->setEnabled(false);
324 connect(m_showNowAction, SIGNAL(triggered(bool)),
325 this, SLOT(slotShowNow()));
326 bibleToolBar->addAction(m_showNowAction);
328 // Fill out tabs and manager data
329 foreach (KwBibleManager* manager, managers)
331 QString name = manager->name();
332 BibleManager mgr;
333 mgr.manager = manager;
335 QWidget* tabWidget = new QWidget();
336 QHBoxLayout* layout = new QHBoxLayout(tabWidget);
338 mgr.comboBibles = new QComboBox();
339 connect(mgr.comboBibles, SIGNAL(currentIndexChanged(int)),
340 this, SLOT(slotBibleChanged()));
342 // Only fill the module list if the manager is local
343 // Otherwise we should wait until the user requests it
344 if (manager->isRemote())
346 mgr.comboBibles->setEnabled(false);
348 mgr.toolBar = new QToolBar();
349 KAction* connectToBible = new KAction(KIcon("network-connect"), i18n("Connect to %1", name), mgr.toolBar);
350 connect(connectToBible, SIGNAL(triggered(bool)),
351 this, SLOT(slotConnect()));
352 mgr.toolBar->addAction(connectToBible);
353 layout->addWidget(mgr.toolBar);
355 // Toolbar should be as small as possible
356 QSizePolicy policy;
358 policy = mgr.toolBar->sizePolicy();
359 policy.setHorizontalStretch(1);
360 mgr.toolBar->setSizePolicy(policy);
362 policy = mgr.comboBibles->sizePolicy();
363 policy.setHorizontalStretch(10);
364 mgr.comboBibles->setSizePolicy(policy);
366 else
368 mgr.toolBar = 0;
369 fillBiblesList(&mgr);
372 layout->addWidget(mgr.comboBibles);
374 m_managerTabs->addTab(tabWidget, name);
375 m_managers.push_back(mgr);
377 // Ensure widgets are apropriately modified
378 connect(m_comboBook, SIGNAL(currentIndexChanged(int)),
379 this, SLOT(slotBookChanged()));
380 connect(m_comboChapter, SIGNAL(currentIndexChanged(int)),
381 this, SLOT(slotVerseRange()));
382 connect(m_editRange, SIGNAL(textChanged(const QString&)),
383 this, SLOT(slotVerseRange()));
385 mainWindow->addDockWidget(Qt::LeftDockWidgetArea, m_docker);
387 slotBibleChanged();
390 void KwBiblePlugin::_unload()
392 // Clean up the bible managers.
393 /// @todo IMPLEMENT!!!
394 delete m_docker;
398 * Private functions
401 /// Fill up the bibles list for a manager.
402 void KwBiblePlugin::fillBiblesList(BibleManager* mgr)
404 QStringList languages = mgr->manager->languages();
405 mgr->comboBibles->clear();
406 mgr->comboBibles->addItem(i18n("-- select a translation --"));
407 foreach (QString language, languages)
409 QStringList modules = mgr->manager->moduleNamesInLanguage(language);
410 if (!modules.isEmpty())
412 mgr->comboBibles->addItem(language, QVariant(modules.first()));
413 foreach (QString module, modules)
415 mgr->comboBibles->addItem(" " + module, QVariant(module));