config.cpp 5.57 KB
Newer Older
Philipp Erhardt's avatar
Philipp Erhardt committed
1
2
3
4
5
6
7
#include "config.h"
#include <QHashIterator>


CFG::CFG() :
		settings(QSettings::IniFormat, QSettings::UserScope, "katarakt") {
	// TODO warn about invalid user input
8
9
10
11
12
13
14
	init_defaults();
}

CFG::CFG(const char* file) :
		settings(file, QSettings::IniFormat) {
	init_defaults();
}
Philipp Erhardt's avatar
Philipp Erhardt committed
15

16
void CFG::init_defaults() {
Philipp Erhardt's avatar
Philipp Erhardt committed
17
18
	settings.beginGroup("Settings");
	// canvas options
19
20
	defaults["background_color"] = "0xDF000000";
	defaults["background_color_fullscreen"] = "0xFF000000";
21
	defaults["default_layout"] = "presentation";
Philipp Erhardt's avatar
Philipp Erhardt committed
22
23
	defaults["mouse_wheel_factor"] = 120; // (qt-)delta for turning the mouse wheel 1 click
	defaults["smooth_scroll_delta"] = 30; // pixel scroll offset
Philipp Erhardt's avatar
Philipp Erhardt committed
24
	defaults["screen_scroll_factor"] = 0.9; // creates overlap for scrolling 1 screen down, should be <= 1
25
26
27

	defaults["page_overlay_text"] = "Page %1/%2";

Philipp Erhardt's avatar
Philipp Erhardt committed
28
29
30
31
32
33
34
35
36
37
38
39
40
	// layout options
	defaults["useless_gap"] = 2;
	defaults["min_page_width"] = 150;
	defaults["min_zoom"] = -14;
	defaults["max_zoom"] = 30;
	defaults["zoom_factor"] = 0.05;
	defaults["prefetch_count"] = 3;
	defaults["search_padding"] = 0.2; // must be <= 0.5
	// resourcemanager options
	defaults["smooth_downscaling"] = true; // filter when creating thumbnail image
	defaults["thumbnail_size"] = 32;
	// search options
	defaults["rect_expansion"] = 2;
41
	// viewer options
42
	defaults["quit_on_init_fail"] = false;
43
	defaults["single_instance_per_file"] = false;
44
	defaults["icon_theme"] = "";
45
	defaults["stylesheet"] = "";
46
47
48
49
50

	// mouse buttons
	defaults["click_link_button"] = 1;
	defaults["drag_view_button"] = 2;
	defaults["select_text_button"] = 1;
Philipp Erhardt's avatar
Philipp Erhardt committed
51
52
	settings.endGroup();

53
54
55
	tmp_values["start_page"] = 0;
	tmp_values["fullscreen"] = false;

56
	settings.beginGroup("Keys");
Philipp Erhardt's avatar
Philipp Erhardt committed
57
58
59
	// canvas keys
	keys["set_presentation_layout"] = QStringList() << "1";
	keys["set_grid_layout"] = QStringList() << "2";
60
	keys["set_presenter_layout"] = QStringList() << "3";
Philipp Erhardt's avatar
Philipp Erhardt committed
61
62
	keys["page_up"] = QStringList() << "PgUp";
	keys["page_down"] = QStringList() << "PgDown";
63
64
	keys["page_first"] = QStringList() << "Home" << "G";
	keys["page_last"] = QStringList() << "End" << "Shift+G";
Philipp Erhardt's avatar
Philipp Erhardt committed
65
66
67
68
69
70
71
72
	keys["half_screen_up"] = QStringList() << "Ctrl+U";
	keys["half_screen_down"] = QStringList() << "Ctrl+D";
	keys["screen_up"] = QStringList() << "Backspace" << "Ctrl+B";
	keys["screen_down"] = QStringList() << "Space" << "Ctrl+F";
	keys["smooth_up"] = QStringList() << "Up" << "K";
	keys["smooth_down"] = QStringList() << "Down" << "J";
	keys["smooth_left"] = QStringList() << "Left" << "H";
	keys["smooth_right"] = QStringList() << "Right" << "L";
Philipp Erhardt's avatar
Philipp Erhardt committed
73
74
75
76
77
	keys["zoom_in"] = QStringList() << "=" << "+";
	keys["zoom_out"] = QStringList() << "-";
	keys["reset_zoom"] = QStringList() << "Z";
	keys["columns_inc"] = QStringList() << "]";
	keys["columns_dec"] = QStringList() << "[";
Philipp Erhardt's avatar
Philipp Erhardt committed
78
79
	keys["offset_inc"] = QStringList() << "}";
	keys["offset_dec"] = QStringList() << "{";
Philipp Erhardt's avatar
Philipp Erhardt committed
80
81
82
	keys["toggle_overlay"] = QStringList() << "T";
	keys["quit"] = QStringList() << "Q" << "W,E,E,E";
	keys["search"] = QStringList() << "/";
83
	keys["search_backward"] = QStringList() << "?";
Philipp Erhardt's avatar
Philipp Erhardt committed
84
85
	keys["next_hit"] = QStringList() << "N";
	keys["previous_hit"] = QStringList() << "Shift+N";
86
87
	keys["next_invisible_hit"] = QStringList() << "Ctrl+N";
	keys["previous_invisible_hit"] = QStringList() << "Ctrl+Shift+N";
Philipp Erhardt's avatar
Philipp Erhardt committed
88
	keys["focus_goto"] = QStringList() << "Ctrl+G";
89
90
	keys["rotate_left"] = QStringList() << ",";
	keys["rotate_right"] = QStringList() << ".";
Philipp Erhardt's avatar
Philipp Erhardt committed
91
92
	keys["jump_back"] = QStringList() << "Ctrl+O" << "Alt+Left";
	keys["jump_forward"] = QStringList() << "Ctrl+I" << "Alt+Right";
93
94
	keys["mark_jump"] = QStringList() << "M";
	keys["toggle_invert_colors"] = QStringList() << "I";
95
	keys["copy_to_clipboard"] = QStringList() << "Ctrl+C";
96
	keys["swap_selection_and_panning_buttons"] = QStringList() << "V";
Philipp Erhardt's avatar
Philipp Erhardt committed
97
98
99
100
	// viewer keys
	keys["toggle_fullscreen"] = QStringList() << "F";
	keys["close_search"] = QStringList() << "Esc";
	keys["reload"] = QStringList() << "R";
101
	keys["open"] = QStringList() << "O";
102
	keys["save"] = QStringList() << "S";
Philipp Erhardt's avatar
Philipp Erhardt committed
103
	keys["toggle_toc"] = QStringList() << "F9";
Philipp Erhardt's avatar
Philipp Erhardt committed
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
	settings.endGroup();
}

CFG::CFG(const CFG &/*other*/) {
}

CFG &CFG::operator=(const CFG &/*other*/) {
	return *this;
}

CFG::~CFG() {
//	set_defaults(); // uncomment to create ini file with all the default settings
}

void CFG::set_defaults() {
	settings.beginGroup("Settings");
	QHashIterator<QString,QVariant> it(defaults);
	while (it.hasNext()) {
		it.next();
		settings.setValue(it.key(), it.value());
	}
	settings.endGroup();

	settings.beginGroup("Keys");
	QHashIterator<QString,QStringList> i2(keys);
	while (i2.hasNext()) {
		i2.next();
		settings.setValue(i2.key(), i2.value());
	}
	settings.endGroup();
}

CFG *CFG::get_instance() {
	static CFG instance;
	return &instance;
}

141
142
143
144
145
146
147
148
149
void CFG::write_defaults(const char *file) {
	CFG inst(file);
	inst.settings.clear();
	inst.settings.setFallbacksEnabled(false);
	inst.set_defaults();
	inst.settings.sync();
	get_instance();
}

Philipp Erhardt's avatar
Philipp Erhardt committed
150
151
152
153
154
155
156
157
QVariant CFG::get_value(const char *key) const {
	return settings.value(QString("Settings/") + key, defaults[key]);
}

void CFG::set_value(const char *key, QVariant value) {
	settings.setValue(QString("Settings/") + key, value);
}

158
159
160
161
162
163
164
165
QVariant CFG::get_tmp_value(const char *key) const {
	return tmp_values[key];
}

void CFG::set_tmp_value(const char *key, QVariant value) {
	tmp_values[key] = value;
}

166
167
168
169
bool CFG::has_tmp_value(const char *key) const {
	return tmp_values.contains(key);
}

170
171
172
173
174
175
176
177
QVariant CFG::get_most_current_value(const char *key) const {
	if (tmp_values.contains(key)) {
		return tmp_values[key];
	} else {
		return settings.value(QString("Settings/") + key, defaults[key]);
	}
}

Philipp Erhardt's avatar
Philipp Erhardt committed
178
179
180
181
QStringList CFG::get_keys(const char *action) const {
	return settings.value(QString("Keys/") + action, keys[action]).toStringList();
}