summaryrefslogtreecommitdiffstats
path: root/bibletime/frontend/keychooser/ckeychooser.cpp
blob: aad247f60bc87eaaa8d9f3baf9727a1811fa7ff5 (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
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2006 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/



#include "ckeychooser.h"
#include "backend/cswordmoduleinfo.h"
#include "backend/cswordbiblemoduleinfo.h"
#include "backend/cswordcommentarymoduleinfo.h"
#include "backend/cswordlexiconmoduleinfo.h"

#include "clexiconkeychooser.h"
#include "cbiblekeychooser.h"
#include "cbookkeychooser.h"

CKeyChooser::CKeyChooser(ListCSwordModuleInfo, CSwordKey *, TQWidget *parent, const char *name )
: TQWidget(parent, name),
m_inHistoryFunction(false) {}

CKeyChooser::~CKeyChooser() {}

CKeyChooser* CKeyChooser::createInstance(ListCSwordModuleInfo modules, CSwordKey *key, TQWidget *parent) {
	if (!modules.count()) {
		return 0;
	}

	CKeyChooser* ck = 0;
	switch ( modules.first()->type() ) {
		case CSwordModuleInfo::Commentary:  //Bibles and commentaries uise the same key chooser
		case CSwordModuleInfo::Bible:
		ck = new CBibleKeyChooser(modules,key,parent);
		break;
		case CSwordModuleInfo::Lexicon:
		ck = new CLexiconKeyChooser(modules,key,parent);
		break;
		case CSwordModuleInfo::GenericBook:
		ck = new CBookKeyChooser(modules,key,parent);
		break;
		default:
		return 0;
	}
	return ck;
}

void CKeyChooser::backInHistory() {
	backInHistory(1);
}

void CKeyChooser::backInHistory(int count) {
	m_inHistoryFunction = true;
	//  tqWarning("go back %d items in history", count);

	Q_ASSERT(m_prevKeyHistoryList.size());

	TQStringList::iterator it = m_prevKeyHistoryList.begin();

	//pop_front count items, the top item is then the new current key
	int index = count;
	while ((index > 0) && (it != m_prevKeyHistoryList.end())) {
		//    tqWarning("pop_front");

		m_nextKeyHistoryList.prepend(*it);
		it = m_prevKeyHistoryList.remove(it);
		--index;
	}

	//the first item is now the item which should be set as key
	if (it != m_nextKeyHistoryList.end() && key()) {
		CSwordKey* k = key();
		k->key(*it);
		setKey(k);
	}

	m_inHistoryFunction = false;
}

void CKeyChooser::forwardInHistory() {
	forwardInHistory(1);
}

void CKeyChooser::forwardInHistory(int count) {
	m_inHistoryFunction = true;
	//  tqWarning("go forward %d items in history", count);

	Q_ASSERT(m_nextKeyHistoryList.size());

	TQStringList::iterator it = m_nextKeyHistoryList.begin();
	//pop_front count-1 items, the top item is then the new current key
	int index = count;
	while (index > 0 && it != m_nextKeyHistoryList.end()) {
		//    tqWarning("pop_front");

		m_prevKeyHistoryList.prepend(*it);
		it = m_nextKeyHistoryList.remove(it);
		--index;
	}

	//the first item of the back list is now the new key
	it = m_prevKeyHistoryList.begin();
	if (it != m_prevKeyHistoryList.end() && key()) {
		CSwordKey* k = key();
		k->key(*it);
		setKey(k);
	}

	m_inHistoryFunction = false;
}

void CKeyChooser::addToHistory(CSwordKey* k) {
	//  tqWarning("addToHistory");

	Q_ASSERT(!m_inHistoryFunction);
	if (k && !m_inHistoryFunction) {
		Q_ASSERT(k->key() == key()->key());
		m_prevKeyHistoryList.prepend(k->key());
	}

	emit historyChanged();
}

const TQStringList CKeyChooser::getPreviousKeys() const {
	TQStringList ret = m_prevKeyHistoryList;
	if (ret.size() >= 1) {
	ret.pop_front(); //the first item always is equal to the current key
	}

	return ret;
}

const TQStringList CKeyChooser::getNextKeys() const {
	return m_nextKeyHistoryList;
}


/*!
    \fn CKeyChooser::polish()
 */
void CKeyChooser::polish() {
	TQWidget::polish();

	//connect the history calls just before we show, we want an empty history
	connect(this, TQT_SIGNAL(keyChanged(CSwordKey*)), TQT_SLOT(addToHistory(CSwordKey*)));
}