summaryrefslogtreecommitdiffstats
path: root/bibletime/frontend/keychooser/cbooktreechooser.cpp
blob: fc9363008630a9d311370fb02feca0083b6a812d (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*********
*
* 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 "cbooktreechooser.h"
#include "backend/cswordtreekey.h"
#include "backend/cswordbookmoduleinfo.h"
#include "frontend/cbtconfig.h"

//TQt includes
#include <tqlayout.h>
#include <tqheader.h>
#include <tqlistview.h>
#include <tqcursor.h>

//KDE includes
#include <tdeapplication.h>

////////////

CBookTreeChooser::TreeItem::TreeItem(TQListViewItem* parent, TQListViewItem* after, CSwordTreeKey* key, const TQString keyName)
: TDEListViewItem(parent, after),
m_key(key),
m_keyName(keyName) {
	//tqDebug("first CBookTreeChooser::TreeItem ctor");
	const unsigned long offset = m_key->getOffset();

	m_key->key(m_keyName);
	//setText(0, TQString::fromLocal8Bit(m_key->getLocalName()) );
	setText(0, m_key->getLocalNameUnicode() );

	m_key->setOffset( offset );
};

CBookTreeChooser::TreeItem::TreeItem(TQListViewItem* parent,CSwordTreeKey* key, const TQString keyName)
: TDEListViewItem(parent),
m_key(key),
m_keyName(keyName) {
	//tqDebug("second CBookTreeChooser::TreeItem ctor");
	const unsigned int offset = m_key->getOffset();

	m_key->key(m_keyName);
	//setText(0, TQString::fromLocal8Bit(m_key->getLocalName()) );
	setText(0, m_key->getLocalNameUnicode() );

	m_key->setOffset( offset );
};

CBookTreeChooser::TreeItem::TreeItem(TQListView* view, TQListViewItem* after,CSwordTreeKey* key, const TQString keyName)
: TDEListViewItem(view,after),
m_key(key),
m_keyName(keyName) {
	//tqDebug("third CBookTreeChooser::TreeItem ctor");
	const unsigned int offset = m_key->getOffset();

	m_key->key(m_keyName);
	//setText(0, TQString::fromLocal8Bit(m_key->getLocalName()) );
	setText(0, m_key->getLocalNameUnicode() );

	m_key->setOffset( offset );
};

const TQString& CBookTreeChooser::TreeItem::key() const {
	return m_keyName;
};

/** Initializes this item with the correct caption. */
void CBookTreeChooser::TreeItem::setup() {
	const unsigned int offset = m_key->getOffset();

	m_key->key(m_keyName);
	setExpandable(m_key->hasChildren());

	m_key->setOffset( offset );

	TDEListViewItem::setup();
}

void CBookTreeChooser::TreeItem::createChilds() {
	//make sure that we don't change the status of the key!

	const unsigned long offset = m_key->getOffset();

	m_key->key(m_keyName);

	if (m_key->hasChildren()) {
		m_key->firstChild(); //go to the first child

		TQListViewItem* oldItem = 0;

		do {
			if (oldItem) {
				oldItem = new TreeItem(this, oldItem, m_key, m_key->key());
			}
			else {
				oldItem = new TreeItem(this, m_key, m_key->key());
			}

			//    oldItem->setExpandable( m_key->hasChildren() );
		}
		while (m_key->nextSibling());
	}

	m_key->setOffset( offset ); //restore the old state
}

void CBookTreeChooser::TreeItem::setOpen(bool o) {
	//setup the tree under this item
	if ((!isOpen() && o) && childCount() == 0) {
		listView()->viewport()->setCursor(WaitCursor);
		createChilds();
		listView()->viewport()->unsetCursor();
	}

	TDEListViewItem::setOpen(o);
}

//////////////////////////////////

CBookTreeChooser::CBookTreeChooser(ListCSwordModuleInfo modules, CSwordKey *key, TQWidget *parent, const char *name)
: CKeyChooser(modules, key, parent,name),
m_key( dynamic_cast<CSwordTreeKey*>(key) ) {
	setModules(modules, false);

	if (!modules.count()) {
		m_modules.clear();
		m_key = 0;
	}

	//now setup the keychooser widgets
	TQHBoxLayout* layout = new TQHBoxLayout(this);

	m_treeView = new TDEListView(this);
	layout->addWidget(m_treeView);
	m_treeView->addColumn("Tree");
	m_treeView->header()->hide();
	m_treeView->setSorting(-1);
	m_treeView->setRootIsDecorated(true);
	m_treeView->setFullWidth(true);
	connect(m_treeView, TQT_SIGNAL(executed(TQListViewItem*)), TQT_SLOT(itemActivated(TQListViewItem*)));

	setKey(key);
	adjustFont();
}

CBookTreeChooser::~CBookTreeChooser() {}

/** Sets a new key to this keychooser */
void CBookTreeChooser::setKey(CSwordKey* key/*newKey*/) {
	setKey(key, false);
}

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

	const TQString key = m_key->key();

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

	//find the right listview item
	const int count = siblings.count();
	int index = 0;
	TQString currentSibling = siblings[index];

	TQListViewItem* child = m_treeView->firstChild();
	while( child && index < count ) {
		if (child->text(0) == currentSibling) { //found a parent of our item
			//found right entry?
			TreeItem* i = dynamic_cast<TreeItem*>(child);
			if (!i || i->key() == key) {
				break;
			}
			i->setOpen(true); //automatically creates childs
			child = i->firstChild();
			currentSibling = siblings[++index];
		}
		else {
			child = child->nextSibling();
		}
	}

	m_treeView->setCurrentItem( child );
	m_treeView->setSelected( child, true );
	m_treeView->ensureItemVisible(child);

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

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

/** Sets another module to this keychooser */
void CBookTreeChooser::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 (CSwordBookModuleInfo* book = dynamic_cast<CSwordBookModuleInfo*>(*it)) {
			m_modules.append(book);
		}
	}

	if (refresh && m_modules.count() && m_key) {
		const uint offset = m_key->getOffset();
		setupTree();
		m_key->setOffset( offset );

		/*  m_key->root();
		  m_key->firstChild();
		  setKey(m_key, true);*/

		//  m_treeView->clear();
		//
		//    const TQString oldKey = m_key->key();
		//  m_key->root();
		//  m_key->firstChild();
		//  setupTree(0,0,m_key);
		//
		//    m_key->key(oldKey);
		//
		//  updateKey(m_key);
		adjustFont(); //only when refresh is set.
	}
}

/** No descriptions */
void CBookTreeChooser::adjustFont() {
	//Make sure the entries are displayed correctly.
	// if ( m_modules.first()->isUnicode() ){
	m_treeView->setFont( CBTConfig::get
							 (m_modules.first()->language()).second );
	//  tqWarning("Unicode Book detected");
	// }
	// else{
	//  m_treeView->setFont( CBTConfig::get(CBTConfig::standard) );
	//  tqWarning("Standard Book detected");
	// }
}


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

/** No descriptions */
void CBookTreeChooser::itemActivated( TQListViewItem* item ) {
	TreeItem* i = dynamic_cast<TreeItem*>(item);
	if (!i) {
		return;
	}

	m_key->key( i->key() );

	if (i->childCount() > 0 || i->isExpandable()) {
		i->setOpen(true);
	}

	emit keyChanged(m_key);
}

/** No descriptions */
void CBookTreeChooser::updateKey( CSwordKey* key ) {
	setKey(key, false);
}

/** Reimplementationm to handle tree creation on show. */
void CBookTreeChooser::show() {
	CKeyChooser::show();

	if (!m_treeView->childCount()) {
		TDEApplication::setOverrideCursor(WaitCursor);
		setupTree(); //create the first level of the tree structure
		m_treeView->resize(m_treeView->sizeHint());
		TDEApplication::restoreOverrideCursor();
	}
}

/** Creates the first level of the tree structure. */
void CBookTreeChooser::setupTree() {
	m_treeView->clear();

	const unsigned long offset = m_key->getOffset();
	m_key->root();

	if (m_key->hasChildren()) {
		TQListViewItem* item = 0;

		m_key->firstChild();
		do {
			//the TreeItem constructor doesn't change the state of the key
			item = new TreeItem(m_treeView, item, m_key, m_key->key());
		}
		while (m_key->nextSibling());
	}

	/*   m_key->root();
	   m_key->firstChild();*/
	m_key->setOffset( offset );
	setKey(m_key, false); //the module may have changed
}

#include "cbooktreechooser.moc"