summaryrefslogtreecommitdiff
path: root/src/frontend/displaywindow/cbuttons.cpp
blob: 582e5def15f4230f0f974093878ca7f054ac0b98 (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
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2008 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

//BibleTime includes
#include "cbuttons.h"

#include "util/cresmgr.h"
#include "util/cpointers.h"
#include "util/directoryutil.h"

//Qt includes
#include <QString>
#include <QToolTip>
#include <QToolButton>
#include <QHash>
#include <QMenu>

//KDE includes




CDisplaySettingsButton::CDisplaySettingsButton(CSwordBackend::DisplayOptions *displaySettings, CSwordBackend::FilterOptions *moduleSettings, const QList<CSwordModuleInfo*>& useModules,QWidget *parent )
: QToolButton(parent) {
	//  qWarning("CDisplaySettingsButton::CDisplaySettingsButton");
	QToolButton::setIcon(util::filesystem::DirectoryUtil::getIcon(CResMgr::displaywindows::displaySettings::icon));

	m_displaySettings = displaySettings;
	m_moduleSettings = moduleSettings;
	m_modules = useModules;

	m_popup = new QMenu(this);
	setMenu(m_popup);
	setPopupMode(QToolButton::InstantPopup);
	setToolTip(tr("Display options"));

	connect(m_popup, SIGNAL(triggered(QAction*)), this, SLOT(optionToggled(QAction*)));
	populateMenu();
}

void CDisplaySettingsButton::reset(const QList<CSwordModuleInfo*>& useModules) {
	m_modules = useModules;
	populateMenu();
	//disable the settings button if no options are available
	if (!populateMenu()) {
		setEnabled(false);
		setToolTip(tr("Display settings: No options available"));
	}
	else {
		setEnabled(true);
		setToolTip(tr("Display settings"));
	}
}


void CDisplaySettingsButton::optionToggled(QAction* /*action*/) {
	qDebug("display settings option toggled");
	//Take each Action and set the corresponding setting.
	//Using QAction (QObject) property and OptionType enum is a dirty way to do this.
	//See populateMenu().
	foreach (QAction* act, m_popup->actions()) {
		int optionType = act->property("OptionType").toInt();
		bool checked = act->isChecked();
		switch(optionType) {
		case Linebreak:
			m_displaySettings->lineBreaks = checked;
			break;
		case Versenum:
			m_displaySettings->verseNumbers = checked;
			break;
		case Variant:
			m_moduleSettings->textualVariants = checked;
			break;
		case Vowel:
			m_moduleSettings->hebrewPoints = checked;
			break;
		case Accents:
			m_moduleSettings->greekAccents = checked;
			break;
		case Cantillation:
			m_moduleSettings->hebrewCantillation = checked;
			break;
		case Headings:
			m_moduleSettings->headings = checked;
			break;
		case Morphseg:
			m_moduleSettings->morphSegmentation = checked;
			break;
		case Xref:
			m_moduleSettings->scriptureReferences = checked;
			break;
		case WordsofJ:
			m_moduleSettings->redLetterWords = checked;
			break;
		}
	}

	emit sigChanged();
}

/** No descriptions */
int CDisplaySettingsButton::populateMenu() {
	int ret = 0;

	m_popup->clear();

	// See also optionToggled()

	ret += addMenuEntry(tr("Use linebreaks after each verse"), Linebreak, &m_displaySettings->lineBreaks, (m_modules.first()->type() == CSwordModuleInfo::Bible));

	//show the verse numbers option only for bible modules
	ret += addMenuEntry(tr("Show verse numbers"), Versenum, &m_displaySettings->verseNumbers, (m_modules.first()->type() == CSwordModuleInfo::Bible));

	ret += addMenuEntry(tr("Show headings"), Headings, &m_moduleSettings->headings,
						isOptionAvailable(CSwordModuleInfo::headings));

	ret += addMenuEntry(tr("Highlight words of Jesus"), WordsofJ, &m_moduleSettings->redLetterWords,
						isOptionAvailable(CSwordModuleInfo::redLetterWords ));

	ret += addMenuEntry(tr("Show Hebrew vowel points"), Vowel, &m_moduleSettings->hebrewPoints,
						isOptionAvailable(CSwordModuleInfo::hebrewPoints ));

	ret += addMenuEntry(tr("Show Hebrew cantillation marks"), Cantillation, &m_moduleSettings->hebrewCantillation,
						isOptionAvailable(CSwordModuleInfo::hebrewCantillation ));

	ret += addMenuEntry(tr("Show Greek accents"), Accents, &m_moduleSettings->greekAccents,
						isOptionAvailable(CSwordModuleInfo::greekAccents ));

	ret += addMenuEntry(tr("Use alternative textual variant"), Variant, &m_moduleSettings->textualVariants,
						isOptionAvailable(CSwordModuleInfo::textualVariants ));

	ret += addMenuEntry(tr("Show scripture cross-references"), Xref, &m_moduleSettings->scriptureReferences,
						isOptionAvailable(CSwordModuleInfo::scriptureReferences ));

	ret += addMenuEntry(tr("Show morph segmentation"), Morphseg, &m_moduleSettings->morphSegmentation,
						isOptionAvailable(CSwordModuleInfo::morphSegmentation ));

	return ret;
}

/** Adds an entry to m_popup. */
int CDisplaySettingsButton::addMenuEntry( const QString name, OptionType type, const int* option, const bool available) {
	int ret = 0;

	if (available) {
		QAction* a = m_popup->addAction(name);
		//see optionToggled()
		a->setProperty("OptionType", type);
		a->setCheckable(true);
		a->setChecked(*option);
		ret = 1;
	}

	return ret;
}

bool CDisplaySettingsButton::isOptionAvailable( const CSwordModuleInfo::FilterTypes option ) {
	bool ret = false;
	QList<CSwordModuleInfo*>::iterator end_it = m_modules.end();
	for (QList<CSwordModuleInfo*>::iterator it(m_modules.begin()); it != end_it; ++it) {
		ret = ret || (*it)->has(option);
	}
	return ret;
}

/** Returns the number of usable menu items in the settings menu. */
int CDisplaySettingsButton::menuItemCount() {
	return m_popup->actions().count();
}

/** Sets the item at position pos to the state given as 2nd paramter. */
void CDisplaySettingsButton::setItemStatus( const int index, const bool checked ) {
	QAction* action = m_popup->actions().at(index);
	action->setChecked(checked);
}

/** Returns the status of the item at position "index" */
bool CDisplaySettingsButton::itemStatus( const int index ) {
	return m_popup->actions().at(index)->isChecked();
}

/** Sets the status to changed. The signal changed will be emitted. */
void CDisplaySettingsButton::setChanged() {
	emit sigChanged();
}