3
* This file is part of BibleTime's source code, http://www.bibletime.info/.
5
* Copyright 1999-2006 by the BibleTime developers.
6
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
12
#include "cbookkeychooser.h"
13
#include "backend/cswordtreekey.h"
14
#include "backend/cswordbookmoduleinfo.h"
15
#include "frontend/cbtconfig.h"
21
QMap<QObject*, int> boxes;
23
CBookKeyChooser::CBookKeyChooser(ListCSwordModuleInfo modules, CSwordKey *key, QWidget *parent, const char *name)
24
: CKeyChooser(modules, key, parent,name), m_layout(0) {
26
setModules(modules, false);
27
m_key = dynamic_cast<CSwordTreeKey*>(key);
28
if (!m_modules.count()) {
32
setModules(modules, true);
38
CBookKeyChooser::~CBookKeyChooser() {}
40
void CBookKeyChooser::setKey(CSwordKey* newKey) {
44
/** Sets a new key to this keychooser */
45
void CBookKeyChooser::setKey(CSwordKey* newKey, const bool emitSignal) {
46
if (m_key != newKey) {
47
m_key = dynamic_cast<CSwordTreeKey*>(newKey);
50
/*const */QString oldKey = m_key->key();
52
if (oldKey.isEmpty()) { //don't set keys equal to "/", always use a key which may have content
54
oldKey = m_key->key();
56
const int oldOffset = m_key->getOffset();
59
if (m_key && !oldKey.isEmpty()) {
60
siblings = QStringList::split("/", oldKey, false);
68
while( m_key->firstChild() && (depth <= int(siblings.count())) ) {
69
const QString key = m_key->key();
70
index = (depth == 0) ? -1 : 0;
71
const QString sibling = siblings[depth];
73
if (!sibling.isEmpty()) { //found it
78
found = (m_key->getLocalName() == sibling);
80
while (!found && m_key->nextSibling());
87
setupCombo(key, depth, index);
91
//clear the combos which were not filled
92
for (; depth < m_modules.first()->depth(); ++depth) {
93
CKeyChooserWidget* chooser = m_chooserWidgets.at(depth);
95
chooser->reset(0,0,false);
99
if (oldKey.isEmpty()) {
103
//m_key->key(oldKey);
104
m_key->setOffset(oldOffset);
108
emit keyChanged(m_key);
112
/** Returns the key of this kechooser. */
113
CSwordKey* const CBookKeyChooser::key() {
117
/** Sets another module to this keychooser */
118
void CBookKeyChooser::setModules(const ListCSwordModuleInfo& modules, const bool refresh) {
121
// for (modules.first(); modules.current(); modules.next()) {
122
ListCSwordModuleInfo::const_iterator end_it = modules.end();
123
for (ListCSwordModuleInfo::const_iterator it(modules.begin()); it != end_it; ++it) {
124
if ( (*it)->type() == CSwordModuleInfo::GenericBook ) {
125
if (CSwordBookModuleInfo* book = dynamic_cast<CSwordBookModuleInfo*>(*it)) {
126
m_modules.append(book);
131
//refresh the number of combos
132
if (refresh && m_modules.count() && m_key) {
134
m_layout = new QHBoxLayout(this);
138
m_chooserWidgets.setAutoDelete(true);
139
m_chooserWidgets.clear();
140
m_chooserWidgets.setAutoDelete(false);
142
for (int i = 0; i < m_modules.first()->depth(); ++i) {
143
// Create an empty keychooser, don't handle next/prev signals
144
CKeyChooserWidget* w = new CKeyChooserWidget(0, false, this);
145
m_chooserWidgets.append( w );
147
//don't allow a too high width, try to keep as narrow as possible
148
//to aid users with smaller screen resolutions
149
int totalWidth = 200; //only 1 level
150
if (m_modules.first()->depth() > 1) {
151
if (m_modules.first()->depth() > 3)
152
totalWidth = 400; //4+ levels
154
totalWidth = 300; //2-3 levels
157
int maxWidth = (int) ((float) totalWidth / (float) m_modules.first()->depth());
159
w->comboBox()->setMaximumWidth(maxWidth);
160
w->comboBox()->setCurrentItem(0);
162
connect(w, SIGNAL(changed(int)), SLOT(keyChooserChanged(int)));
163
connect(w, SIGNAL(focusOut(int)), SLOT(keyChooserChanged(int)));
165
m_layout->addWidget(w);
171
//set the tab order of the key chooser widgets
173
CKeyChooserWidget* chooser = 0;
174
CKeyChooserWidget* chooser_prev = 0;
175
const int count = m_chooserWidgets.count();
176
for (int i = 0; i < count; ++i) {
177
chooser = m_chooserWidgets.at(i);
180
if (chooser && chooser_prev) {
181
QWidget::setTabOrder(chooser_prev, chooser);
184
chooser_prev = chooser;
186
QWidget::setTabOrder(chooser, 0);
189
adjustFont(); // only when refresh is set.
193
/** No descriptions */
194
void CBookKeyChooser::adjustFont() {
196
//Make sure the entries are displayed correctly.
197
for ( CKeyChooserWidget* idx = m_chooserWidgets.first(); idx; idx = m_chooserWidgets.next() ) {
198
idx->comboBox()->setFont( CBTConfig::get
199
( m_modules.first()->language() ).second );
203
/** Refreshes the content. */
204
void CBookKeyChooser::refreshContent() {
206
updateKey( m_key ); //refresh with current key
210
void CBookKeyChooser::setupCombo(const QString key, const int depth, const int currentItem) {
211
CKeyChooserWidget* chooserWidget = m_chooserWidgets.at(depth);
213
const unsigned long oldOffset = m_key->getOffset();
216
if ((depth == 0) && chooserWidget && chooserWidget->comboBox()->count()) { //has already items
217
//set now the right item
218
if (CKeyChooserWidget* chooserWidget = m_chooserWidgets.at(depth)) {
219
chooserWidget->setItem( chooserWidget->comboBox()->text(currentItem) );
222
m_key->setOffset(oldOffset);
227
//insert an empty item at the top
230
items << QString::null;
234
items << QString::fromLocal8Bit(m_key->getLocalName());
236
while (m_key->nextSibling());
239
chooserWidget->reset(items,currentItem,false);
243
// m_key->key(oldKey);
244
m_key->setOffset( oldOffset );
247
/** A keychooser changed. Update and emit a signal if necessary. */
248
void CBookKeyChooser::keyChooserChanged(int /*newIndex*/) {
249
const int activeID = boxes[const_cast<QObject*>(sender())]; //no so good code!
252
CKeyChooserWidget* chooser;
253
const int count = m_chooserWidgets.count();
255
for (int i = 0; i < count; ++i) {
256
chooser = m_chooserWidgets.at(i);
257
const QString currentText =
258
(chooser && chooser->comboBox())
259
? chooser->comboBox()->currentText()
262
if (currentText.isEmpty() || i > activeID) {
266
items << currentText;
270
newKey.append(items.join("/"));
271
if (newKey.length() > 1) {
272
newKey.remove(newKey.length(),1); //remove the traling slash
275
// qWarning("key changed: setting to %s", newKey.latin1());
280
/** Updates the keychoosers for the given key but emit no signal. */
281
void CBookKeyChooser::updateKey(CSwordKey* key) {