config.cpp 5.5 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
Philipp Erhardt's avatar
Philipp Erhardt committed
25
26
27
28
29
30
31
32
33
34
35
36
37
	// 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;
38
	// viewer options
39
	defaults["quit_on_init_fail"] = false;
40
	defaults["single_instance_per_file"] = false;
41
	defaults["icon_theme"] = "";
42
43
44
45
46

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

49
50
51
	tmp_values["start_page"] = 0;
	tmp_values["fullscreen"] = false;

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

137
138
139
140
141
142
143
144
145
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
146
147
148
149
150
151
152
153
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);
}

154
155
156
157
158
159
160
161
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;
}

162
163
164
165
bool CFG::has_tmp_value(const char *key) const {
	return tmp_values.contains(key);
}

166
167
168
169
170
171
172
173
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
174
175
176
177
QStringList CFG::get_keys(const char *action) const {
	return settings.value(QString("Keys/") + action, keys[action]).toStringList();
}