summaryrefslogtreecommitdiffstats
path: root/bibletime/frontend/keychooser/cbookkeychooser.cpp
blob: e0372908c579546bd7dc4f4862d29dced8f55083 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2007 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/



#include "cbookkeychooser.h"
#include "backend/cswordtreekey.h"
#include "backend/cswordbookmoduleinfo.h"
#include "frontend/cbtconfig.h"

//TQt includes
#include <tqlayout.h>
#include <tqmap.h>

TQMap<TQObject*, int> boxes;

CBookKeyChooser::CBookKeyChooser(ListCSwordModuleInfo modules, CSwordKey *key, TQWidget *parent, const char *name)
: CKeyChooser(modules, key, parent,name), m_layout(0) {

	setModules(modules, false);
	m_key = dynamic_cast<CSwordTreeKey*>(key);
	if (!m_modules.count()) {
		m_key = 0;
	}

	setModules(modules, true);
	setKey(key);

	adjustFont();
}

CBookKeyChooser::~CBookKeyChooser() {}

void CBookKeyChooser::setKey(CSwordKey* newKey) {
	setKey(newKey, true);
}

/** Sets a new key to this keychooser */
void CBookKeyChooser::setKey(CSwordKey* newKey, const bool emitSignal) {
	//tqDebug("CBookKeyChooser::setKey");
	if (m_key != newKey) {
		m_key = dynamic_cast<CSwordTreeKey*>(newKey);
	}

	/*const */TQString oldKey = m_key->key();

	if (oldKey.isEmpty()) { //don't set keys equal to "/", always use a key which may have content
		m_key->firstChild();
		oldKey = m_key->key();
	}
	const int oldOffset = m_key->getOffset();

	TQStringList siblings;
	if (m_key && !oldKey.isEmpty()) {
		siblings = TQStringList::split("/", oldKey, false);
	}

	int depth = 0;
	int index = 0;

	m_key->root();

	while( m_key->firstChild() && (depth <= int(siblings.count())) ) {
		const TQString key = m_key->key();
		index = (depth == 0) ? -1 : 0;
		const TQString sibling = siblings[depth];

		if (!sibling.isEmpty()) { //found it
			bool found = false;

			do {
				++index;
				//tqDebug("set 'found'");
				//found = (TQString::fromLocal8Bit(m_key->getLocalName()) == sibling);
				found = (m_key->getLocalNameUnicode() == sibling);
			}
			while (!found && m_key->nextSibling());

			if (!found) {
				m_key->key( key );
			}
		}

		setupCombo(key, depth, index);
		depth++;
	}

	//clear the combos which were not filled
	for (; depth < m_modules.first()->depth(); ++depth)  {
		CKeyChooserWidget* chooser = m_chooserWidgets.at(depth);
		if (chooser) {
			chooser->reset(0,0,false);
		}
	}

	if (oldKey.isEmpty()) {
		m_key->root();
	}
	else {
		//m_key->key(oldKey);
		m_key->setOffset(oldOffset);
	}

	if (emitSignal) {
		emit keyChanged(m_key);
	}
}

/** Returns the key of this kechooser. */
CSwordKey* const CBookKeyChooser::key() {
	return m_key;
}

/** Sets another module to this keychooser */
void CBookKeyChooser::setModules(const ListCSwordModuleInfo& modules, const bool refresh) {
	m_modules.clear();

	//   for (modules.first(); modules.current(); modules.next()) {
	ListCSwordModuleInfo::const_iterator end_it = modules.end();
	for (ListCSwordModuleInfo::const_iterator it(modules.begin()); it != end_it; ++it) {
		if ( (*it)->type() == CSwordModuleInfo::GenericBook ) {
			if (CSwordBookModuleInfo* book = dynamic_cast<CSwordBookModuleInfo*>(*it)) {
				m_modules.append(book);
			}
		}
	}

	//refresh the number of combos
	if (refresh && m_modules.count() && m_key) {
		if (!m_layout) {
			m_layout = new TQHBoxLayout(this);
		}

		//delete old widgets
		m_chooserWidgets.setAutoDelete(true);
		m_chooserWidgets.clear();
		m_chooserWidgets.setAutoDelete(false);

		for (int i = 0; i < m_modules.first()->depth(); ++i) {
			// Create an empty keychooser, don't handle next/prev signals
			CKeyChooserWidget* w = new CKeyChooserWidget(0, false, this);
			m_chooserWidgets.append( w );

			//don't allow a too high width, try to keep as narrow as possible
			//to aid users with smaller screen resolutions
			int totalWidth = 200; //only 1 level
			if (m_modules.first()->depth() > 1) {
				if (m_modules.first()->depth() > 3)
					totalWidth = 400; //4+ levels
				else
					totalWidth = 300; //2-3 levels
			}

			int maxWidth = (int) ((float) totalWidth / (float) m_modules.first()->depth());

			w->comboBox()->setMaximumWidth(maxWidth);
			w->comboBox()->setCurrentItem(0);

			connect(w, TQT_SIGNAL(changed(int)), TQT_SLOT(keyChooserChanged(int)));
			connect(w, TQT_SIGNAL(focusOut(int)), TQT_SLOT(keyChooserChanged(int)));

			m_layout->addWidget(w);
			boxes[TQT_TQOBJECT(w)] = i;

			w->show();
		}

		//set the tab order of the key chooser widgets

		CKeyChooserWidget* chooser = 0;
		CKeyChooserWidget* chooser_prev = 0;
		const int count = m_chooserWidgets.count();
		for (int i = 0; i < count; ++i) {
			chooser = m_chooserWidgets.at(i);
			Q_ASSERT(chooser);

			if (chooser && chooser_prev) {
				TQWidget::setTabOrder(chooser_prev, chooser);
			}

			chooser_prev = chooser;
		}
		TQWidget::setTabOrder(chooser, 0);

		updateKey(m_key);
		adjustFont(); // only when refresh is set.
	}
}

/** No descriptions */
void CBookKeyChooser::adjustFont() {

	//Make sure the entries are displayed correctly.
	for ( CKeyChooserWidget* idx = m_chooserWidgets.first(); idx; idx = m_chooserWidgets.next() ) {
		idx->comboBox()->setFont( CBTConfig::get
									  ( m_modules.first()->language() ).second );
	}
}

/** Refreshes the content. */
void CBookKeyChooser::refreshContent() {
	if (m_key) {
		updateKey( m_key ); //refresh with current key
	}
}

void CBookKeyChooser::setupCombo(const TQString key, const int depth, const int currentItem) {
	CKeyChooserWidget* chooserWidget = m_chooserWidgets.at(depth);

	const unsigned long oldOffset = m_key->getOffset();
	m_key->key(key);

	if ((depth == 0) && chooserWidget && chooserWidget->comboBox()->count()) { //has already items
		//set now the right item
		if (CKeyChooserWidget* chooserWidget = m_chooserWidgets.at(depth)) {
			chooserWidget->setItem( chooserWidget->comboBox()->text(currentItem) );
		}

		m_key->setOffset(oldOffset);
		return;
	}


	//insert an empty item at the top
	TQStringList items;
	if (depth > 0) {
		items << TQString();
	}

	do {
		//items << TQString::fromLocal8Bit(m_key->getLocalName());
		items << m_key->getLocalNameUnicode();
	}
	while (m_key->nextSibling());

	if (chooserWidget) {
		chooserWidget->reset(items,currentItem,false);
	}

	//restore old key
	//  m_key->key(oldKey);
	m_key->setOffset( oldOffset );
}

/** A keychooser changed. Update and emit a signal if necessary. */
void CBookKeyChooser::keyChooserChanged(int /*newIndex*/) {
	const int activeID = boxes[TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))]; //no so good code!

	TQStringList items;
	CKeyChooserWidget* chooser;
	const int count = m_chooserWidgets.count();

	for (int i = 0; i < count; ++i) {
		chooser = m_chooserWidgets.at(i);
		const TQString currentText =
			(chooser && chooser->comboBox())
			? chooser->comboBox()->currentText()
			: TQString();

		if (currentText.isEmpty() || i > activeID) {
			break;
		}

		items << currentText;
	}

	TQString newKey("/");
	newKey.append(items.join("/"));
	if (newKey.length() > 1) {
		newKey.remove(newKey.length(),1); //remove the traling slash
	}

	//  tqWarning("key changed: setting to %s", newKey.latin1());
	m_key->key(newKey);
	setKey(m_key);
}

/** Updates the keychoosers for the given key but emit no signal. */
void CBookKeyChooser::updateKey(CSwordKey* key) {
	setKey(key, false);
}