-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToolSelection.py
117 lines (99 loc) · 4.69 KB
/
ToolSelection.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
from PyQt5 import QtCore, QtGui, QtWidgets, Qt
class ToolSelection():
"""
Basic "selection" tool
"""
def __init__(self, window, imageView, image):
self.window = window
self.imageView = imageView
self.image = image
self.selStartX = None
self.selStartY = None
def activate(self):
"""
Turn this tool on
"""
self.imageView.mousePressEvent = self.mousePress
self.imageView.mouseMoveEvent = self.mouseMoveRelease
self.imageView.mouseReleaseEvent = self.mouseMoveRelease
def mousePress(self, event):
"""
Mouse down on image -> Begin selection
"""
if event.button() == QtCore.Qt.LeftButton:
selStartX = event.x() // self.image.get_char_size()[0]
selStartY = event.y() // self.image.get_char_size()[1]
append = event.modifiers() & QtCore.Qt.ControlModifier == QtCore.Qt.ControlModifier
remove = event.modifiers() & QtCore.Qt.AltModifier == QtCore.Qt.AltModifier
self.image.move_cursor(selStartX, selStartY, False)
self.beginSelection(selStartX, selStartY, append, remove)
if event.button() == QtCore.Qt.RightButton:
new_pal_char = self.image.get_cell(event.x() // self.image.get_char_size()[0], event.y() // self.image.get_char_size()[1])
self.window.palette.set_char_idx(new_pal_char[0])
self.window.palette.set_fore(new_pal_char[1])
self.window.palette.set_back(new_pal_char[2])
self.window.redisplayPalette()
def mouseMoveRelease(self, event):
"""
Changes or commits selection
"""
if event.buttons() == QtCore.Qt.LeftButton or event.button() == QtCore.Qt.LeftButton:
selEndX = event.x() // self.image.get_char_size()[0]
selEndY = event.y() // self.image.get_char_size()[1]
preliminary = not (event.button() == QtCore.Qt.LeftButton)
append = (event.modifiers() & QtCore.Qt.ControlModifier == QtCore.Qt.ControlModifier)
remove = (event.modifiers() & QtCore.Qt.AltModifier == QtCore.Qt.AltModifier)
if selEndX == self.selStartX and selEndY == self.selStartY and not append:
self.updateSelection(selEndX, selEndY, preliminary, append, remove)
self.image.set_selection()
self.window.redisplayAnsi()
else:
self.updateSelection(selEndX, selEndY, preliminary, append, remove)
def beginSelection(self, selStartX, selStartY, append, remove):
"""
Start the selection process
"""
if self.selStartX != None or self.selStartY != None:
return
self.selStartX = selStartX
self.selStartY = selStartY
self.image.set_selection([(self.selStartX, self.selStartY)], append = append, remove = remove)
self.window.redisplayAnsi()
def updateSelection(self, selEndX, selEndY, preliminary, append, remove):
"""
Updates the ongoing selection process
"""
selDirX = 1
if selEndX - self.selStartX < 0:
selDirX = -1
selDirY = 1
if selEndY - self.selStartY < 0:
selDirY = -1
if selEndY - self.selStartY == 0 and selEndX - self.selStartX == 0:
selDirX = 1
selDirY = 1
if selEndX != self.selStartX or selEndY != self.selStartY:
selection = []
for x in range(self.selStartX, selEndX + selDirX, selDirX):
for y in range(self.selStartY, selEndY + selDirY, selDirY):
selection.append((x, y))
self.image.set_selection(selection, append = append, remove = remove, preliminary = preliminary)
self.window.redisplayAnsi()
else:
self.image.set_selection([(selEndX, selEndY)], append = append, remove = remove, preliminary = preliminary)
self.window.redisplayAnsi()
if preliminary:
self.window.updateCursorPositionLabel("Selecting: ({0}, {1}) to ({2}, {3}) = {4} x {5}".format(
self.selStartX,
self.selStartY,
selEndX,
selEndY,
abs(self.selStartX - selEndX - selDirX),
abs(self.selStartY - selEndY - selDirY)
))
else:
self.selStartX = None
self.selStartY = None
if selEndX == self.selStartX and selEndY == self.selStartY and not preliminary:
self.selStartX = None
self.selStartY = None