-
Notifications
You must be signed in to change notification settings - Fork 0
/
hieronymus.py
executable file
·252 lines (194 loc) · 5.73 KB
/
hieronymus.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
#!/usr/bin/env python3
# this file is under the WTFPLv2 license, see COPYING.WTFPL
import sys
from PyQt5.QtCore import (
pyqtSlot as Slot, QPointF, QRectF,
)
from PyQt5.QtGui import (
QPainter, QPixmap,
)
from PyQt5.QtWidgets import (
QAction, QActionGroup, QApplication, QMainWindow, QToolBar, QWidget,
)
MODE_PANSCAN = 0
MODE_CURTAIN = 1
MODE_FADE = 2
MODE_HORIZONTAL = 0
MODE_VERTICAL = 1
MODE_GRID = 2
# TODO add option for visual splitter?
# TODO option for tint images?
# TODO grid for more than 2 images
# TODO do a kind of diff?
class MainWindow(QMainWindow):
def __init__(self, viewer=None, **kwargs):
super(MainWindow, self).__init__(**kwargs)
self.viewer = viewer
self.setCentralWidget(viewer)
tb = QToolBar()
tb.addAction(viewer.panScanAction)
tb.addAction(viewer.curtainAction)
tb.addAction(viewer.lockAction)
self.addToolBar(tb)
class CurtainViewer(object):
def __init__(self, **kwargs):
super(CurtainViewer, self).__init__(**kwargs)
self.__mouse = QPointF()
def disable(self):
self.setMouseTracking(False)
def enable(self):
self.setMouseTracking(True)
def _rect(self, rect, image_index):
irect = QRectF(rect)
irect.translate(-self.rect().center())
irect.setTopLeft(irect.topLeft() * self.zoom[image_index])
irect.setBottomRight(irect.bottomRight() * self.zoom[image_index])
irect.translate(self.centers[image_index])
return irect
def paintEvent(self, ev):
painter = QPainter(self)
if len(self.images) == 2:
if self.orientation == MODE_HORIZONTAL:
wrects = [
QRectF(0, 0, self.__mouse.x(), self.height()),
QRectF(self.__mouse.x(), 0, self.width() - self.__mouse.x(), self.height()),
]
else:
wrects = [
QRectF(0, 0, self.width(), self.__mouse.y()),
QRectF(0, self.__mouse.y(), self.width(), self.height() - self.__mouse.y()),
]
else:
wrects = [QRectF(self.rect())]
irects = [self._rect(r, n) for n, r in enumerate(wrects)]
for img, wrect, irect in zip(self.images, wrects, irects):
painter.drawPixmap(wrect, img, irect)
def mousePressEvent(self, ev):
pass
def mouseMoveEvent(self, ev):
self.__mouse = ev.pos()
self.update()
def mouseReleaseEvent(self, ev):
pass
def wheelEvent(self, ev):
pass
class MultiViewer(object):
def __init__(self, **kwargs):
super(MultiViewer, self).__init__(**kwargs)
def disable(self):
pass
def enable(self):
pass
def paintEvent(self, ev):
painter = QPainter(self)
iw = self.width() / len(self.images)
sx = 0
for img, zoom, center in zip(self.images, self.zoom, self.centers):
wrect = QRectF(sx, 0, iw, self.height())
irect = QRectF(0, 0, iw, self.height())
irect.setSize(irect.size() * zoom)
irect.moveCenter(QPointF(center))
painter.drawPixmap(wrect, img, irect)
sx += iw
def mousePressEvent(self, ev):
self.__point = ev.pos()
self.__clicked = self.imageAt(ev.pos())
def mouseMoveEvent(self, ev):
mv = QPointF(ev.pos() - self.__point)
indexes = [self.__clicked]
if self.lock:
indexes = range(len(self.images))
for index in indexes:
img_mv = mv * self.zoom[index]
self.centers[index] -= img_mv
self.__point = ev.pos()
self.update()
def mouseReleaseEvent(self, ev):
pass
def imageAt(self, pos):
iw = self.width() / len(self.images)
return int(pos.x() / iw)
def wheelEvent(self, ev):
idx = self.imageAt(ev.pos())
if idx >= len(self.images):
return
d = ev.angleDelta().y()
if d:
d *= 0.2
d /= 120
d = 1 - d
if self.lock:
for idx in range(len(self.zoom)):
self.zoom[idx] *= d
self.update()
class Hieronymus(QWidget, MultiViewer, CurtainViewer):
def __init__(self, **kwargs):
super(Hieronymus, self).__init__(**kwargs)
self.images = []
self.zoom = []
self.centers = []
self.mode = MODE_PANSCAN
self.orientation = MODE_HORIZONTAL
self.lock = False
self.panScanAction = QAction('Move/Zoom', self)
self.panScanAction.setCheckable(True)
self.panScanAction.setChecked(True)
self.panScanAction.triggered.connect(self.setPanScan)
self.curtainAction = QAction('Curtain', self)
self.curtainAction.setCheckable(True)
self.curtainAction.triggered.connect(self.setCurtain)
self.actionGroup = QActionGroup(self)
self.actionGroup.addAction(self.panScanAction)
self.actionGroup.addAction(self.curtainAction)
self.lockAction = QAction('Sync move/zoom', self)
self.lockAction.setCheckable(True)
self.lockAction.triggered.connect(self.triggerLock)
def addImage(self, image):
# TODO avoid loading full QPixmap?
image = QPixmap(image)
self.images.append(image)
self.zoom.append(1.)
self.centers.append(QPointF(image.rect().center()))
@Slot()
def setPanScan(self):
self.modeClass().disable(self)
self.mode = MODE_PANSCAN
self.modeClass().enable(self)
self.update()
@Slot()
def setCurtain(self):
self.modeClass().disable(self)
self.mode = MODE_CURTAIN
self.modeClass().enable(self)
self.update()
@Slot(bool)
def triggerLock(self, b):
self.lock = b
def modeClass(self):
if self.mode == MODE_PANSCAN:
return MultiViewer
elif self.mode == MODE_CURTAIN:
return CurtainViewer
def paintEvent(self, ev):
self.modeClass().paintEvent(self, ev)
def mousePressEvent(self, ev):
self.modeClass().mousePressEvent(self, ev)
def mouseMoveEvent(self, ev):
self.modeClass().mouseMoveEvent(self, ev)
def mouseReleaseEvent(self, ev):
self.modeClass().mouseReleaseEvent(self, ev)
def wheelEvent(self, ev):
self.modeClass().wheelEvent(self, ev)
if __name__ == '__main__':
app = QApplication(sys.argv)
viewer = Hieronymus()
files = app.arguments()
del files[0] # first arg is executable
if not files:
print('usage: hieronymus FILE1 FILE2', file=sys.stderr)
sys.exit(1)
for img in files:
viewer.addImage(img)
win = MainWindow(viewer=viewer)
win.show()
sys.exit(app.exec_())