1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
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. *
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. *
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 ***************************************************************************/
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>
35 #include <KMessageBox>
37 #include <QMainWindow>
38 #include <QDockWidget>
39 #include <QHBoxLayout>
40 #include <QVBoxLayout>
48 * Constructors + destructor
51 /// Default constructor.
52 KwBiblePlugin::KwBiblePlugin()
55 i18n("The bible plugin allows for the navigation and display of "
56 "bible extracts from SWORD and BibleGateway.com."))
68 KwBiblePlugin::~KwBiblePlugin()
70 // Ensure all GUI stuff is cleaned up
71 KwBiblePlugin::_unload();
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
88 if (mgr
->comboBibles
->count() == 0)
90 KMessageBox::information(m_docker
, i18n("No bibles found"));
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();
111 KwBibleModule
* module
= 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();
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)
136 m_comboBook
->setCurrentIndex(index
);
137 if (canPreserveBook
&& chapter
>= 0)
139 // If we can restore book, also restore chapter
140 m_comboChapter
->setCurrentIndex(chapter
);
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();
168 KwBibleModule
* module
= 0;
171 QString bible
= mgr
.comboBibles
->itemData(bibleInd
).toString();
172 module
= mgr
.manager
->module(bible
);
176 // Is a book selected?
177 int index
= m_comboBook
->currentIndex();
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
));
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();
204 KwBibleModule
* module
= 0;
207 QString bible
= mgr
.comboBibles
->itemData(bibleInd
).toString();
208 module
= mgr
.manager
->module(bible
);
212 // Is a book selected?
213 int bookIndex
= m_comboBook
->currentIndex();
214 int chapterIndex
= -1;
217 // Is a chapter selected?
218 chapterIndex
= m_comboChapter
->currentIndex();
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
;
234 changedPal
.setColor( QPalette::Normal
, QPalette::Base
, QColor(255, 127, 127) );
236 m_editRange
->setPalette(changedPal
);
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
);
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();
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
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
);
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
);
357 void KwBiblePlugin::_unload()
359 // Clean up the bible managers.
360 /// @todo IMPLEMENT!!!
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
));