summaryrefslogtreecommitdiffstats
path: root/bibletime/frontend/displaywindow/cmodulechooserbutton.cpp
blob: 69ec064306c51e65f109d2ab920f2ba9363028d7 (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
/*********
*
* 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 "cmodulechooserbutton.h"
#include "cmodulechooserbar.h"

#include "backend/cswordbackend.h"

#include "util/cresmgr.h"

//TQt includes
#include <tqstring.h>
#include <tqtooltip.h>
#include <tqdict.h>
#include <tqvaluelist.h>

//KDE includes
#include <tdelocale.h>
#include <tdeglobal.h>
#include <kiconloader.h>

CModuleChooserButton::CModuleChooserButton(CSwordModuleInfo* useModule,CSwordModuleInfo::ModuleType type, const int id, CModuleChooserBar *parent, const char *name )
: TDEToolBarButton(iconName(), id, parent, name),
m_id(id), m_popup(0), m_moduleChooserBar(parent) {
	m_moduleType = type;
	m_module = useModule;
	if (!m_module) {
		m_hasModule = false;
	}
	else {
		m_hasModule = true;
	}

	setIcon( iconName() );
	setPopupDelay(1);

	populateMenu();
}

CModuleChooserButton::~CModuleChooserButton() {
	m_submenus.setAutoDelete(true); //delete all submenus
	m_submenus.clear();

	delete m_popup;
}

/** Returns the icon used for the current status. */
const TQString CModuleChooserButton::iconName() {
	switch (m_moduleType) {
		case CSwordModuleInfo::Bible:
		if (m_hasModule)
			return CResMgr::modules::bible::icon_unlocked;
		else
			return CResMgr::modules::bible::icon_add;
		case CSwordModuleInfo::Commentary:
		if (m_hasModule)
			return CResMgr::modules::commentary::icon_unlocked;
		else
			return CResMgr::modules::commentary::icon_add;
		case CSwordModuleInfo::Lexicon:
		if (m_hasModule)
			return CResMgr::modules::lexicon::icon_unlocked;
		else
			return CResMgr::modules::lexicon::icon_add;
		case CSwordModuleInfo::GenericBook:
		if (m_hasModule)
			return CResMgr::modules::book::icon_unlocked;
		else
			return CResMgr::modules::book::icon_add;
		default: //return as default the bible icon
		return CResMgr::modules::bible::icon_unlocked;
	}
}

CSwordModuleInfo* CModuleChooserButton::module() {
	for ( TDEPopupMenu* popup = m_submenus.first(); popup; popup = m_submenus.next() ) {
		for (unsigned int i = 0; i < popup->count(); i++) {
			if ( m_popup->isItemChecked(popup->idAt(i)) ) {
				TQString mod = popup->text(popup->idAt(i)).remove('&');
				return backend()->findModuleByName( mod.left(mod.find(" ")) );
			}
		}

	}
	return 0; //"none" selected
}

/** Returns the id used for this button. */
int CModuleChooserButton::getId() const {
	return m_id;
}

/** Is called after a module was selected in the popup */
void CModuleChooserButton::moduleChosen( int ID ) {
	for ( TDEPopupMenu* popup = m_submenus.first(); popup; popup = m_submenus.next() ) {
		for (unsigned int i = 0; i < popup->count(); i++) {
			popup->setItemChecked(popup->idAt(i),false);
		}
		popup->setItemChecked(ID, true);
	}

	m_popup->setItemChecked(m_noneId, false); //uncheck the "none" item

	if (m_popup->text(ID).remove('&') == i18n("NONE")) { // note: this is for m_popup, the toplevel!
		if (m_hasModule) {
			emit sigRemoveButton(m_id);
			return;
		}
	}
	else {
		if (!m_hasModule) {
			emit sigAddButton();
		}

		m_hasModule = true;
		m_module = module();
		setIcon( iconName() );
		emit sigChanged();

		setText( i18n("Select a work") );
		m_popup->changeTitle(m_titleId, i18n("Select a work"));

		TQToolTip::remove
			(this);
		if (module()) {
			TQToolTip::add
				(this, module()->name());
		}
	}
}

/** No descriptions */
void CModuleChooserButton::populateMenu() {
	m_submenus.setAutoDelete(true); //delete all submenus
	m_submenus.clear();

	delete m_popup;

	//create a new, empty popup
	m_popup = new TDEPopupMenu(this);

	if (m_module) {
		m_titleId = m_popup->insertTitle( i18n("Select a work") );
	}
	else {
		m_titleId = m_popup->insertTitle( i18n("Select an additional work") );
	}

	m_popup->setCheckable(true);

	m_noneId = m_popup->insertItem(i18n("NONE"));
	if ( !m_module ) {
		m_popup->setItemChecked(m_noneId, true);
	}

	m_popup->insertSeparator();
	connect(m_popup, TQT_SIGNAL(activated(int)), this, TQT_SLOT(moduleChosen(int)));
	setPopup(m_popup, true);

	TQStringList languages;
	TQDict<TDEPopupMenu> langdict;

	//the modules list contains only the modules we can use, i.e. same type and same features
	ListCSwordModuleInfo modules;
	ListCSwordModuleInfo allMods = backend()->moduleList();

	//   for (allMods.first(); allMods.current(); allMods.next()) {
	ListCSwordModuleInfo::iterator end_it = allMods.end();
	for (ListCSwordModuleInfo::iterator it(allMods.begin()); it != end_it; ++it) {
		if ((*it)->type() != m_moduleType) {
			continue;
		}

		modules.append( *it );
	};

	//iterate through all found modules of the type we support
	//  for (modules.first(); modules.current(); modules.next()) {
	/*ListCSwordModuleInfo::iterator*/
	end_it = modules.end();
	for (ListCSwordModuleInfo::iterator it(modules.begin()); it != end_it; ++it) {
		TQString lang = (*it)->language()->translatedName();

		if (lang.isEmpty()) {
			//lang = TQString::fromLatin1("xx"); //unknown language -- do not use English as default!!
			lang = (*it)->language()->abbrev();
			if (lang.isEmpty()) {
				lang = "xx";
			}
		}

		if (languages.find( lang ) == languages.end() ) { //this lang was not yet added
			languages += lang;

			TDEPopupMenu* menu = new TDEPopupMenu;
			langdict.insert(lang, menu );
			m_submenus.append(menu);
			connect(menu, TQT_SIGNAL(activated(int)), this, TQT_SLOT(moduleChosen(int)));
		}
	}


	//Check the appropriate entry
	//  for (modules.first(); modules.current(); modules.next()) {
	/*ListCSwordModuleInfo::iterator*/ end_it = modules.end();
	for (ListCSwordModuleInfo::iterator it(modules.begin()); it != end_it; ++it) {
		TQString lang = (*it)->language()->translatedName();

		if (lang.isEmpty()) {
			lang = (*it)->language()->abbrev();
			if (lang.isEmpty()) {
				lang = "xx";
			}
		}

		TQString name((*it)->name());
		name.append(" ").append((*it)->isLocked() ? i18n("[locked]") : TQString());

		const int id = langdict[lang]->insertItem( name );
		if ( m_module && (*it)->name() == m_module->name()) {
			langdict[lang]->setItemChecked(id, true);
		}
	}

	languages.sort();
	for ( TQStringList::Iterator it = languages.begin(); it != languages.end(); ++it ) {
		m_popup->insertItem( *it, langdict[*it]);
	}

	if (module()) {
		TQToolTip::add
			(this, module()->name());
	}
	else {
		TQToolTip::add
			(this, i18n("No work selected"));
	}
}


/*!
    \fn CModuleChooserButton::updateMenuItems()
 */
void CModuleChooserButton::updateMenuItems() {
	TQString moduleName;
	CSwordModuleInfo* module = 0;
	ListCSwordModuleInfo chosenModules = m_moduleChooserBar->getModuleList();

	for ( TDEPopupMenu* popup = m_submenus.first(); popup; popup = m_submenus.next() ) {

		for (unsigned int i = 0; i < popup->count(); i++) {
			moduleName = popup->text(popup->idAt(i)).remove('&');
			module = backend()->findModuleByName( moduleName.left(moduleName.findRev(" ")) );

 			Q_ASSERT(module);
// 			if (!module) {
// 				tqWarning("Can't find module with name %s", moduleName.latin1());
// 			}

			bool alreadyChosen = chosenModules.contains( module );
			if (m_module) {
				alreadyChosen = alreadyChosen && (m_module->name() != moduleName);
			}

			popup->setItemEnabled(popup->idAt(i), !alreadyChosen); //grey it out, it was chosen already
		}
	}
}