-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.py
More file actions
223 lines (182 loc) · 6.96 KB
/
Copy pathConfig.py
File metadata and controls
223 lines (182 loc) · 6.96 KB
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
from __future__ import with_statement
import pickle
from Data import DB
from QtUtil import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtCore import pyqtSignal as Signal
class AmphSetting(QObject):
change = Signal()
def __init__(self, name):
super(AmphSetting, self).__init__()
self.name = name
# Check database for setting value
value = DB.fetchall('''select value from settings where name = ?''', (name,))
if (len(value) > 0):
# Load value from database
self.value = pickle.loads(value[0][0])
self.in_db = True
elif name in AmphSettings.defaults:
# Use default value
self.value = AmphSettings.defaults[name]
self.in_db = False
else:
# Error: No such setting
raise KeyError(name)
def get(self):
return self.value
def set(self, value):
if self.value == value: return
self.value = value
if self.in_db:
DB.execute('''update settings set value = ? where name = ?''',
(pickle.dumps(value), self.name))
else:
DB.execute('''insert into settings (name,value) values (?,?)''',
(self.name, pickle.dumps(value)))
self.in_db = True
self.change.emit()
class AmphSettings(QObject):
defaults = {
"typer_font": str(QFont("Arial", 14).toString()),
"ignore_case": True,
"progressive": True,
"prog_times": 3,
"prog_min": 15,
"prog_avg": 20,
"history": 30.0,
"perf_group_by": 0,
"perf_items": 100,
"text_regex": r"",
"num_rand": 10,
"graph_what": 3,
"show_xaxis": False,
"chrono_x": False,
"dampen_graph": False,
"breaks": True,
"minutes_in_sitting": 60.0,
"dampen_average": 10,
"group_month": 365.0,
"group_week": 30.0,
"group_day": 7.0,
"ana_how": 1,
"ana_which": "wpm asc",
"ana_many": 30,
"ana_count": 1,
"gen_copies": 3,
"gen_take": 2,
"gen_mix": 'c',
#"gen_stats": False,
"str_clear": 's',
"str_extra": 10,
"str_what": 'e'
}
def __init__(self):
super(AmphSettings, self).__init__()
self.cache = {}
def __getitem__(self, k):
if k not in self.cache:
self.cache[k] = AmphSetting(k)
return self.cache[k]
def get(self, k):
return self[k].get()
def getFont(self, k):
qf = QFont()
qf.fromString(self.get(k))
return qf
def getColor(self, k):
return QColor(self.get(k))
def set(self, k, v):
p = self.get(k)
if p == v:
return
self[k].set(v)
Settings = AmphSettings()
class SettingsColor(AmphButton):
def __init__(self, key, text):
self.key_ = key
super(SettingsColor, self).__init__(Settings.get(key), self.pickColor)
self.updateIcon()
def pickColor(self):
color = QColorDialog.getColor(Settings.getColor(self.key_), self)
if not color.isValid():
return
Settings.set(self.key_, color.name())
self.updateIcon()
def updateIcon(self):
pix = QPixmap(32, 32)
c = Settings.getColor(self.key_)
pix.fill(c)
self.setText(Settings.get(self.key_))
self.setIcon(QIcon(pix))
class SettingsEdit(AmphEdit):
def __init__(self, setting):
sttg = Settings[setting]
val = sttg.get()
typ = type(val)
validator = None
if isinstance(val, float):
validator = QDoubleValidator
elif isinstance(val, int):
validator = QIntValidator
if validator is None:
self.fmt = lambda x: x
else:
self.fmt = lambda x: "%g" % x
super(SettingsEdit, self).__init__(
self.fmt(val),
lambda: Settings.set(setting, typ(self.text())),
validator=validator)
sttg.change.connect(lambda : self.setText(str(sttg.get())) )
class SettingsCombo(QComboBox):
def __init__(self, setting, lst, *args):
super(SettingsCombo, self).__init__(*args)
sttg = Settings[setting]
prev = sttg.get()
self.idx2item = []
for i in range(len(lst)):
if isinstance(lst[i], str):
# not a tuple, use index as key
k, v = i, lst[i]
else:
k, v = lst[i]
self.addItem(v)
self.idx2item.append(k)
if k == prev:
self.setCurrentIndex(i)
self.activated.connect(lambda x: sttg.set(self.idx2item[x]))
class SettingsCheckBox(QCheckBox):
def __init__(self, setting, *args):
super(SettingsCheckBox, self).__init__(*args)
sttg = Settings[setting]
self.setCheckState(Qt.Checked if sttg.get() else Qt.Unchecked)
self.stateChanged.connect(lambda x: sttg.set(x == Qt.Checked))
class PreferenceWidget(QWidget):
def __init__(self):
super(PreferenceWidget, self).__init__()
self.font_lbl = QLabel()
self.setLayout(AmphBoxLayout([
["Typer font is", self.font_lbl, AmphButton("Change...", self.setFont), None],
[SettingsCheckBox("breaks", "Take breaks"), "after",
SettingsEdit('minutes_in_sitting'), "minutes.", None],
[SettingsCheckBox("ignore_case", "Ignore capitalization"), None],
[SettingsCheckBox("progressive", "Activate new words progressively"), None],
["Activate words when you have typed words accurately",
SettingsEdit("prog_times"), "times, ", None ],
["with a minimum speed of",
SettingsEdit("prog_min"), "WPM and an average speed of",
SettingsEdit("prog_avg"), "WPM.", None ],
["Show", SettingsEdit('num_rand'), "words at a time.", None],
["Data is considered too old to be included in analysis after",
SettingsEdit("history"), "days.", None],
["When smoothing out the graph, display a running average of", SettingsEdit('dampen_average'), "values", None]
]))
self.updateFont()
def setFont(self):
font, ok = QFontDialog.getFont(Settings.getFont('typer_font'), self)
Settings.set("typer_font", font.toString())
self.updateFont()
def updateFont(self):
self.font_lbl.setText(Settings.get("typer_font"))
qf = Settings.getFont('typer_font')
self.font_lbl.setFont(qf)