-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcontext.py
311 lines (206 loc) · 9.29 KB
/
context.py
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python
#coding: utf-8
import os
from configparser import ConfigParser, NoSectionError, NoOptionError
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import Qt
from image import Image
from translation import Language, TDatabase
from brushes import *
class Context:
def __init__(self, signals):
self.signals = signals
self.palette = []
self.defaultPalette = [[14, 53, 75], [0, 76, 115], [18, 121, 174], [49, 162, 238], [136, 199, 234], [27, 52, 43],
[30, 85, 55], [69, 145, 26], [121, 191, 29], [190, 222, 44], [69, 18, 18], [113, 31, 31],
[184, 37, 53], [220, 81, 115], [255, 159, 182], [39, 20, 67], [105, 28, 99], [173, 81, 185],
[184, 152, 208], [53, 48, 36], [89, 66, 40], [140, 92, 77], [208, 128, 112], [229, 145, 49],
[247, 176, 114], [252, 215, 142], [0, 0, 0], [33, 33, 33], [79, 79, 79], [179, 179, 179],
[255, 255, 255], [37, 42, 46], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
self.imagePos = -1
self.images = []
self.theme = "algae"
self.currentTool = 0
self.clipboard = QtWidgets.QApplication.clipboard()
self.pencilCur = QtGui.QCursor(QtGui.QPixmap("images/cursors/penicon.png"), 2, 17)
self.colorPickerCur = QtGui.QCursor(QtGui.QPixmap("images/cursors/droppericon.png"), 2, 17)
self.eraserCur = QtGui.QCursor(QtGui.QPixmap("images/cursors/erasericon.png"), 2, 17)
self.fillCur = QtGui.QCursor(QtGui.QPixmap("images/cursors/fillicon.png"), 1, 14)
self.circles, self.brushes = createBrushes()
self.loadDefaults()
def newImage(self, width, height, bg):
self.images.append(Image.newImage(width, height, bg, self))
self.image = len(self.images) - 1
print("newImage")
self.signals.newImage.emit()
def loadImage(self, fileName):
self.images.append(Image.fromFile(fileName, self))
self.image = len(self.images) - 1
self.signals.newImage.emit()
def setCurrentImagePos(self, index):
self.imagePos = index
self.signals.imageChanged.emit(index)
def getCurrentImagePos(self):
return self.imagePos
def currentImage(self):
if len(self.images) > 0:
return self.images[self.getCurrentImagePos()]
return None
def currentQImage(self):
if self.imagePos != -1:
return self.images[self.getCurrentImagePos()].image
return None
def getImagePos(self, index):
return self.images[index]
def removeImagePos(self, index):
del self.images[index]
#self.images = self.images[:index] + self.images[index+1:]
if self.imagePos > index:
self.imagePos -= 1
self.signals.imageChanged.emit(index-1)
self.signals.imageRemoved.emit(index)
def setTransparentSelection(self, x):
self.transparentSelection = x
self.signals.transparentSelection.emit(x)
self.signals.updateCanvas.emit()
def setPencilSize(self, size):
if size < 10 and size > 0:
self.pencilSize = size
self.signals.updatePencilSize.emit(self.pencilSize)
def setEraserSize(self, size):
if size < 10 and size > 0:
self.eraserSize = size
self.signals.updateEraserSize.emit(self.eraserSize)
def changePrimaryColor(self, c):
self.primaryColor = c
self.signals.updateColor.emit()
def changeSecondaryColor(self, c):
self.secondaryColor = c
self.signals.updateColor.emit()
def changeCurrentTool(self, index):
print("Emitting updateTool")
self.currentTool = index
self.signals.updateTool.emit(index)
def getText(self, sect, ident): # Get some text in the current language
return self.tdatabase.getText(self.lang, sect, ident).replace("\\n", "\n")
def getTextInLang(self, lang, sect, ident): # Get some text in a specific language
return self.tdatabase.getText(lang, sect, ident)
def setDefault(self, sect, ident, value):
try:
self.cp.set(sect, ident, value)
except NoSectionError:
print("Trying to set \"" + ident + "\" to \"" + str(value) + "\" on section \"" + sect + "\", but given section does not exist. Creating section.")
self.cp.add_section(sect)
self.cp.set(sect, ident, value)
f = open("defaults.cfg", "w")
self.cp.write(f)
f.close()
def getDefault(self, sect, ident, default):
try:
return self.cp.get(sect, ident)
except NoSectionError:
print("Trying to get value from option \"" + ident + "\" on section \"" + sect + "\", but no section with that name exists. Returning default value.")
return default
except NoOptionError:
print("Trying to get value from option \"" + ident + "\" on section \"" + sect + "\", but specified option does not exist within that section. Returning default value.")
return default
def getBoolDefault(self, sect, ident, default):
try:
return self.cp.getboolean(sect, ident)
except ValueError:
print("Trying to get boolean value from option \"" + ident + "\" on section \"" + sect + "\", but given option value is not boolean.")
except NoSectionError:
print("Trying to get boolean value from option \"" + ident + "\" on section \"" + sect + "\", but no section with that name exists. Returning default value.")
return default
except NoOptionError:
print("Trying to get boolean value from option \"" + ident + "\" on section \"" + sect + "\", but specified option does not exist within that section. Returning default value.")
return default
def getIntDefault(self, sect, ident, default):
try:
return self.cp.getint(sect, ident)
except ValueError:
print("Trying to get integer value from option \"" + ident + "\" on section \"" + sect + "\", but given option value is not an integer.")
except NoSectionError:
print("Trying to get integer value from option \"" + ident + "\" on section \"" + sect + "\", but no section with that name exists. Returning default value.")
return default
except NoOptionError:
print("Trying to get integer value from option \"" + ident + "\" on section \"" + sect + "\", but specified option does not exist within that section. Returning default value.")
return default
def getFloatDefault(self, sect, ident, default):
try:
return self.cp.getfloat(sect, ident)
except ValueError:
print("Trying to get float value from option \"" + ident + "\" on section \"" + sect + "\", but given option value is not a floating point number.")
except NoSectionError:
print("Trying to get float value from option \"" + ident + "\" on section \"" + sect + "\", but no section with that name exists. Returning default value.")
return default
except NoOptionError:
print("Trying to get float value from option \"" + ident + "\" on section \"" + sect + "\", but specified option does not exist within that section. Returning default value.")
return default
def loadDefaults(self):
self.cp = ConfigParser()
self.cp.read("defaults.cfg")
self.loadDefaultsPalette()
self.loadDefaultsLanguage()
self.loadDefaultsGrid()
self.loadDefaultsColor()
self.loadDefaultsTheme()
self.loadDefaultsSelection()
self.loadDefaultsPencil()
self.loadDefaultsEraser()
def loadDefaultsPalette(self):
try:
f = open("palette.cfg", 'r')
except IOError:
print("Cannot open palette.cfg, falling back to default palette.")
self.palette = self.defaultPalette
return
l = f.readlines()
for i in l:
colors = i[:-1].split(' ')
red = int(colors[0])
green = int(colors[1])
blue = int(colors[2])
self.palette.append([red, green, blue])
f.close()
def loadDefaultsLanguage(self):
self.tdatabase = TDatabase()
lang = self.getDefault("language", "lang", "en")
if lang in self.tdatabase.langAvailable:
self.lang = lang
else:
self.lang = "en"
def loadDefaultsGrid(self):
self.grid = self.getBoolDefault("grid", "grid", False)
self.matrixGrid = self.getBoolDefault("grid", "matrix_grid", False)
self.matrixGridWidth = self.getIntDefault("grid", "matrix_grid_width", 16)
self.matrixGridHeight = self.getIntDefault("grid", "matrix_grid_height", 16)
def loadDefaultsColor(self):
self.primaryColor = QtGui.QColor(self.getIntDefault("color", "primary_color", QtCore.Qt.color1))
self.secondaryColor = QtGui.QColor(self.getIntDefault("color", "secondary_color", QtCore.Qt.color0))
def loadDefaultsTheme(self):
self.theme = self.getDefault("theme", "theme", "algae")
def loadDefaultsSelection(self):
self.transparentSelection = self.getBoolDefault("selection", "transparent", True)
def loadDefaultsPencil(self):
self.pencilSize = self.getIntDefault("pencil", "size", 3)
self.secondaryColorEraser = self.getBoolDefault("pencil", "secondary_color_eraser", False)
def loadDefaultsEraser(self):
self.eraserSize = self.getIntDefault("eraser", "size", 1)
def saveDefaults(self):
self.setDefault("color", "primary_color", self.primaryColor.rgb())
self.setDefault("color", "secondary_color", self.secondaryColor.rgb())
self.setDefault("selection", "transparent", self.transparentSelection)
self.setDefault("pencil", "size", self.pencilSize)
self.setDefault("pencil", "secondary_color_eraser", self.secondaryColorEraser)
self.setDefault("eraser", "size", self.eraserSize)
self.savePalette()
def savePalette(self):
f = open("palette.cfg", 'w')
for i in self.palette:
f.write(str(i[0]) + " " + str(i[1]) + " " + str(i[2]) + "\n")
f.close()