-
Notifications
You must be signed in to change notification settings - Fork 8
/
shufti.py
executable file
·420 lines (336 loc) · 14 KB
/
shufti.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/env python3
'''
shufti 2.4
The persistent image viewer
By Dan MacDonald 2017-2020
shufti is free software. You can use, redistribute it and/or modify it under the terms of the included LICENSE file which is based on the BSD-2-Clause license.
Usage:
$ shufti.py /path/to/image slideshow-delay-in-milliseconds
The slideshow delay value (milliseconds) is optional and defaults to 4000 ms ie a 4 second delay when slideshow mode is active.
You may want to associate shufti with image files in your file manager rather than use it from the terminal.
'''
import os, sys, glob
from functools import partial
from PyQt5 import QtCore, QtSql
from PyQt5.QtGui import QPixmap, QTransform
from os.path import expanduser, dirname
from PyQt5.QtWidgets import QMainWindow, QApplication, QGraphicsScene, QGraphicsView, QMenu, QLabel
class ShuftiView(QGraphicsView):
def wheelEvent(self, event):
moose = event.angleDelta().y()/120
if moose > 0:
shufti.zoomIn()
elif moose < 0:
shufti.zoomOut()
def contextMenuEvent(self, event):
menu = QMenu()
menu.addAction('Zoom in +, e', shufti.zoomIn)
menu.addAction('Zoom out -, d', shufti.zoomOut)
menu.addAction('Toggle fullscreen F11', shufti.toggleFullscreen)
menu.addAction('Vertically max. v', shufti.vertMax)
menu.addAction('HoriZontally max. z', shufti.horizMax)
menu.addAction('Rotate CCW r', partial(shufti.rotateImg, 1))
menu.addAction('Spin CW s', partial(shufti.rotateImg, -1))
menu.addAction('Next image SPACE', partial(shufti.dirBrowse, 1))
menu.addAction('Previous image BACKSPACE', partial(shufti.dirBrowse, -1))
menu.addAction('Fit image f', shufti.fitView)
menu.addAction('Reset zoom 1', shufti.zoomReset)
menu.addAction('Play slideshow p', shufti.toggleSlideshow)
menu.addAction('About shufti', shufti.about)
menu.addAction('Quit q', shufti.close)
menu.exec_(event.globalPos())
class ShuftiWindow(QMainWindow):
def resizeEvent(self,resizeEvent):
width = self.frameGeometry().width()
height = self.frameGeometry().height()
self.view.resize(width + 2, height + 2)
def closeEvent(self, event):
if self.slideshowTimer.isActive():
self.slideshowTimer.stop()
shufti.winState()
if self.inshuft == 0:
shufti.dbInsert()
self.db.close()
else:
shufti.dbUpdate()
self.db.close()
class AboutShufti(QLabel):
def __init__(self):
QLabel.__init__(self,"shufti 2.4\n\nBy Dan MacDonald")
self.setAlignment(QtCore.Qt.AlignCenter)
def initUI(self):
self.center()
def center(self):
qr = self.frameGeometry()
cp = app.desktop().availableGeometry().centre()
qr.moveCenter(cp)
self.move(qr.topLeft())
class Shufti(ShuftiWindow):
def __init__(self):
super(Shufti,self).__init__()
try:
self.key = sys.argv[1]
except IndexError:
print('\nshufti 2.4\n\nYou must specify the full path to an image for the first parameter and, optionally, a slideshow delay value in ms\n')
sys.exit(1)
self.dbSanitise()
self.formats = ('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.pbm', '.pgm', '.ppm',
'.xbm', '.xpm', '.dds', '.icns', '.jp2', '.mng', '.tga', '.tiff', '.wbmp', '.webp')
try:
open(self.key, 'r')
except IOError:
print('There was an error opening the file')
sys.exit(1)
if len(sys.argv) > 2:
try:
self.slideTime = int(sys.argv[2])
except ValueError:
print('Invalid slideshow delay value')
sys.exit(1)
else:
self.slideTime = 4000
if self.key.lower().endswith(self.formats):
# If inshuft = 0, the image is not in shufti's image database
self.inshuft = 0
self.rotval = 0
self.rotvals = (0,-90,-180,-270)
self.dbfile = expanduser("~/.config/shufti/shufti.db")
self.dbdir = os.path.dirname(self.dbfile)
if not os.path.isfile(self.dbfile):
self.createDB()
self.db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName(self.dbfile)
self.db.open()
self.query = QtSql.QSqlQuery()
self.dbSearch(self.dbkey)
# Set common window attributes
self.path, self.title = os.path.split(self.key)
self.setWindowTitle(str(self.title) + " - shufti")
self.img = QPixmap(self.key)
self.scene = QGraphicsScene()
self.scene.addPixmap(self.img)
self.view = ShuftiView(self.scene, self)
self.view.setDragMode(QGraphicsView.ScrollHandDrag)
self.view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
# Create array of images in current image dir
self.imgfiles = []
for filename in glob.glob(str(self.path) + '/*'):
base, ext = os.path.splitext(filename)
if ext.lower() in self.formats:
self.imgfiles.append(filename)
# Find location of current image in imgfiles array
self.dirpos = 0
while self.dirpos < len(self.imgfiles) and self.imgfiles[self.dirpos] != self.key:
self.dirpos += 1
# If we have no inshuftery, we use the defaults
if self.inshuft == 0:
self.newImage()
else:
self.oldImage()
else:
print("Unsupported file format")
sys.exit(1)
# Prepare automatic slideshow
self.slideshowTimer = QtCore.QTimer(self)
self.slideshowTimer.timeout.connect(self.slideshowNext)
self.slideshowTimer.setSingleShot(False)
# TODO: Parse command line to enable fullscreen and/or slideshow at startup
#self.toggleFullscreen()
#self.toggleSlideshow()
def slideshowNext(self):
self.dirBrowse(1)
def newImage(self):
self.getScreenRes()
self.imgw = self.img.width()
self.imgh = self.img.height()
self.zoom = 1
self.rotate = 0
if self.imgw > self.screenw or self.imgh > self.screenh:
self.resize(self.screenw, self.screenh)
self.show()
self.resetScroll()
self.fitView()
else:
self.resize(self.imgw + 2, self.imgh + 2)
self.show()
self.resetScroll()
def oldImage(self):
if self.rotate == -90:
self.rotval = 1
elif self.rotate == -180:
self.rotval = 2
elif self.rotate == -270:
self.rotval = 3
self.resize(self.img.size())
self.updateView()
self.show()
self.setGeometry(self.winposx, self.winposy, self.winsizex, self.winsizey)
self.view.verticalScrollBar().setValue(self.vscroll)
self.view.horizontalScrollBar().setValue(self.hscroll)
def toggleFullscreen(self):
if self.isFullScreen():
self.showNormal()
else:
self.showFullScreen()
def toggleSlideshow(self):
if self.slideshowTimer.isActive():
self.slideshowTimer.stop()
else:
self.slideshowTimer.start(self.slideTime)
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_F11:
self.toggleFullscreen()
elif event.key() == QtCore.Qt.Key_Equal or event.key() == QtCore.Qt.Key_E:
self.zoomIn()
elif event.key() == QtCore.Qt.Key_Minus or event.key() == QtCore.Qt.Key_D:
self.zoomOut()
elif event.key() == QtCore.Qt.Key_1:
self.zoomReset()
elif event.key() == QtCore.Qt.Key_S:
self.rotateImg(-1)
elif event.key() == QtCore.Qt.Key_R:
self.rotateImg(1)
elif event.key() == QtCore.Qt.Key_F:
self.fitView()
elif event.key() == QtCore.Qt.Key_Space:
self.dirBrowse(1)
elif event.key() == QtCore.Qt.Key_Backspace:
self.dirBrowse(-1)
elif event.key() == QtCore.Qt.Key_V:
self.vertMax()
elif event.key() == QtCore.Qt.Key_Z:
self.horizMax()
elif event.key() == QtCore.Qt.Key_P:
self.toggleSlideshow()
elif event.key() == QtCore.Qt.Key_Q:
self.close()
def mouseDoubleClickEvent(self, event):
self.toggleFullscreen()
def createDB(self):
if not os.path.exists(self.dbdir):
os.makedirs(self.dbdir)
self.db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName(self.dbfile)
self.query = QtSql.QSqlQuery()
self.db.open()
self.query.exec_("create table shuftery(filename text primary key, "
"zoom real, winposx int, winposy int, winsizex int, winsizey int, "
"hscroll int, vscroll int, rotate int)")
return True
def zoomIn(self):
self.zoom *= 1.05
self.updateView()
def zoomOut(self):
self.zoom /= 1.05
self.updateView()
def zoomReset(self):
self.zoom = 1
self.updateView()
def rotateImg(self, clock):
self.rotval += clock
if self.rotval == 4:
self.rotval = 0
elif self.rotval < 0:
self.rotval = 3
self.rotate = self.rotvals[self.rotval]
self.updateView()
def fitView(self):
self.view.fitInView(self.scene.sceneRect(), QtCore.Qt.KeepAspectRatio)
if self.rotate == 0:
self.zoom = self.view.transform().m11()
elif self.rotate == -90:
self.zoom = (self.view.transform().m12()) * -1
elif self.rotate == -180:
self.zoom = (self.view.transform().m11()) * -1
else:
self.zoom = self.view.transform().m12()
def updateView(self):
self.view.setTransform(QTransform().scale(self.zoom, self.zoom).rotate(self.rotate))
def winState(self):
self.winsizex = self.geometry().width()
self.winsizey = self.geometry().height()
self.vscroll = self.view.verticalScrollBar().value()
self.hscroll = self.view.horizontalScrollBar().value()
self.winposx = self.pos().x()
self.winposy = self.pos().y()
def dbInsert(self):
self.query.exec_("insert into shuftery values('%s" % self.dbkey +
"', " + str(self.zoom) + ", " + str(self.winposx) + ", " + str(self.winposy) +
", " + str(self.winsizex) + ", " + str(self.winsizey) + ", " + str(self.hscroll) +
", " + str(self.vscroll) + ", " + str(self.rotate) + ")")
def dbUpdate(self):
self.query.exec_("update shuftery set zoom=" + str(self.zoom) +
", winposx=" + str(self.winposx) + ", winposy=" + str(self.winposy) +
", winsizex=" + str(self.winsizex) + ", winsizey=" + str(self.winsizey) +
", hscroll=" + str(self.hscroll) + ", vscroll=" + str(self.vscroll) +
", rotate=" + str(self.rotate) + " where filename='%s'" % self.dbkey)
def dbSearch(self, field):
self.query.exec_("SELECT * FROM shuftery WHERE filename='%s'" % field)
# If the image is found in shufti.db, load the previous view settings
while self.query.next() and self.inshuft == 0:
self.zoom = self.query.value(1)
self.winposx = self.query.value(2)
self.winposy = self.query.value(3)
self.winsizex = self.query.value(4)
self.winsizey = self.query.value(5)
self.hscroll = self.query.value(6)
self.vscroll = self.query.value(7)
self.rotate = self.query.value(8)
self.inshuft = 1
def dbSanitise(self):
self.dbkey = self.key.replace("\"", "\"\"")
self.dbkey = self.dbkey.replace("\'", "\'\'")
self.dbkey = self.dbkey.replace("\\", "\\\\")
def dirBrowse(self, direc):
if len(self.imgfiles) > 1:
self.dirpos += direc
if self.dirpos > (len(self.imgfiles) - 1):
self.dirpos = 0
elif self.dirpos < 0:
self.dirpos = (len(self.imgfiles) - 1)
shufti.winState()
if self.inshuft == 0:
shufti.dbInsert()
else:
shufti.dbUpdate()
self.key = self.imgfiles[self.dirpos]
self.dbSanitise()
self.path, self.title = os.path.split(self.key)
self.setWindowTitle(str(self.title) + " - shufti")
self.inshuft = 0
self.dbSearch(self.dbkey)
self.scene.clear()
self.view.resetTransform()
self.img = QPixmap(self.key)
self.scene.addPixmap(self.img)
if self.inshuft == 0:
self.newImage()
else:
self.oldImage()
def vertMax(self):
self.getScreenRes()
self.winsizex = self.geometry().width()
self.winposx = self.pos().x()
self.setGeometry(self.winposx, 0, self.winsizex, self.screenh)
def horizMax(self):
self.getScreenRes()
self.winsizey = self.geometry().height()
self.winposy = self.pos().y()
self.setGeometry(0, self.winposy, self.screenw, self.winsizey)
def resetScroll(self):
self.view.verticalScrollBar().setValue(0)
self.view.horizontalScrollBar().setValue(0)
def getScreenRes(self):
self.screen_res = app.desktop().availableGeometry()
self.screenw = self.screen_res.width()
self.screenh = self.screen_res.height()
def about(self):
self.pop = AboutShufti()
self.pop.resize(555, 333)
self.pop.setWindowTitle("About shufti")
self.pop.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
shufti = Shufti()
sys.exit(app.exec_())