-
Notifications
You must be signed in to change notification settings - Fork 0
/
mechascan_gui.py
684 lines (597 loc) · 26.8 KB
/
mechascan_gui.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
#!/usr/bin/env python3
from PyQt5.QtCore import Qt, QDir, QRect, QTimer
from PyQt5.QtGui import QImage, QPixmap, QTransform, QPainter
from PyQt5.QtWidgets import (QApplication, QMainWindow, QGraphicsScene, QGraphicsView, QGraphicsPixmapItem,
QMenu, QDialog, QFileDialog, QMessageBox, QFrame, QRubberBand, QLabel,
QProgressBar)
from PyQt5.QtPrintSupport import QPrinter, QPrintDialog
from functools import partial
import os
from random import random
import editimage
import preferences
from fileimage import read_list # write_list
import logging
import mechascan_process
from mechascan_process import ScanType
from queue import Queue
# ui generated code
from main_window import *
logging.basicConfig(level=logging.DEBUG,
format="%(asctime)s.%(msecs)d-%(name)s-%(threadName)s-%(levelname)s %(message)s",
datefmt='%M:%S')
log = logging.getLogger(__name__)
# shut up gphoto
logit = logging.getLogger('gphoto2')
logit.setLevel(logging.INFO)
# self.win.open_img(fname) #open fname
# self.win.open() #?
class MechascanSlideGUI(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MechascanSlideGUI, self).__init__()
self.fname = ''
self.lbl_led_status = ''
self.lbl_tpt_status = ''
self.lbl_cam_status = ''
self.lbl_slot_status = ''
self.lbl_tpt_slot_status = ''
self.auto_orient = True
self.slide_delay = 0
self.quality = 0
self.progressbar = None
self.popup = None
self.pixmap = None
self.ss_timer = None
self.filelist = []
self.img_index = 0
self.last_file = 0
self.slides_next = True
self.msg_queue = Queue() # process messages for GUI to display - if it cares...
self.file_queue = Queue() # process to gui - filename of images when they are ready for display
self.work_queue = Queue() # gui -> process pass lambda commands
self.msp = mechascan_process.Process(self.msg_queue, self.file_queue, self.work_queue)
self.work_queue.put(lambda: self.msp.tpt_connect())
self.work_queue.put(lambda: self.msp.led_connect())
self.work_queue.put(lambda: self.msp.cam_connect())
self.msp.work_threaded()
self.setupUi(self)
self.printer = QPrinter()
self.load_img = self.load_img_fit # default load type
# to make this work in qtdesigner add a widget to the central abd then use a horizontal layout
# then delete the widget
self.scene = QGraphicsScene()
# self.img_view = ImageView(self.centralwidget)
self.img_view = ImageView(self)
self.img_view.setScene(self.scene)
self.horizontalLayout.addWidget(self.img_view) # add widget to uic generated form
self.reload_img = self.reload_auto
# self.open_new = self.parent().open_win
# self.exit = self.parent().closeAllWindows
self.create_actions()
self.create_menu()
self.orient_dict = self.create_dict()
self.setContextMenuPolicy(Qt.CustomContextMenu)
# noinspection PyUnresolvedReferences
self.customContextMenuRequested.connect(self.show_menu)
self.read_prefs()
# noinspection PyTypeChecker,PyCallByClass
self.pics_dir = os.path.expanduser('~/Pictures') or QDir.currentPath(self)
# we use a timer to process status updates from the process module
# we could have used signals but I did not want to make the module dependant on qt
self.timer = QtCore.QTimer()
# noinspection PyUnresolvedReferences
self.timer.timeout.connect(self.update_gui)
self.timer.start(5)
self.resize(1000, 600)
def closeEvent(self, event):
# stop current action and drain work queue
self.scan_stop()
#end thread
self.work_queue.put(lambda: self.msp.end())
event.accept()
# scan functions
def scan(self):
if self.msp_update_from_gui():
self.work_queue.put(lambda: self.msp.scan(scan_type=ScanType.start_end,
start=self.sb_start_slot.value(),
end=self.sb_end_slot.value()))
def scan_next(self):
if self.msp_update_from_gui():
self.work_queue.put(lambda: self.msp.scan(scan_type=ScanType.next))
def scan_prev(self):
if self.msp_update_from_gui():
self.work_queue.put(lambda: self.msp.scan(scan_type=ScanType.prev))
def scan_current(self):
if self.msp_update_from_gui():
self.work_queue.put(lambda: self.msp.scan(scan_type=ScanType.current))
def scan_stop(self):
while not self.work_queue.empty():
log.debug("clearing task off queue")
self.work_queue.get_nowait()
self.work_queue.task_done() # mark as done
# do not use queue as it will not break long running process
self.msp.stop_scan()
def tpt_slot_current(self, x):
self.work_queue.put(lambda: self.msp.select_slot(x))
def lamp_on(self, checked):
if checked:
self.work_queue.put(lambda: self.msp.led_on())
else:
self.work_queue.put(lambda: self.msp.led_off())
def tpt_home(self):
self.work_queue.put(lambda: self.msp.select_slot(0))
def tpt_reset(self):
self.work_queue.put(lambda: self.msp.tpt_reset())
def update_gui(self):
# empty msg queue - just grab the last message for display as user will
# only be able to see the last message
msg = ""
while not self.msg_queue.empty():
try:
msg = self.msg_queue.get_nowait()
log.info(msg)
self.msg_queue.task_done()
except:
raise
if msg:
self.statusbar.showMessage(msg, 1000)
file = ""
while not self.file_queue.empty():
try:
file = self.file_queue.get_nowait()
log.info("File on disk: " + file)
self.file_queue.task_done()
except:
raise
if file:
self.fname = file
self.reload_img()
# noinspection PyUnresolvedReferences
self.lbl_led_status.setText("LED: " + self.msp.led_port)
# noinspection PyUnresolvedReferences
self.lbl_tpt_status.setText("Transport: " + self.msp.tpt_port)
# noinspection PyUnresolvedReferences
self.lbl_cam_status.setText("Camera: " + self.msp.cam_port)
# noinspection PyUnresolvedReferences
self.lbl_slot_status.setText("Slot: " + str(self.msp.get_slot()))
if self.msp.get_slide_in_gate():
# noinspection PyUnresolvedReferences
self.lbl_tpt_slot_status.setText(" Slide in slot ")
# noinspection PyUnresolvedReferences
self.lbl_tpt_slot_status.setStyleSheet("QLabel { background-color : green; color : white; }")
else:
# noinspection PyUnresolvedReferences
self.lbl_tpt_slot_status.setText("No slide in slot")
# noinspection PyUnresolvedReferences
self.lbl_tpt_slot_status.setStyleSheet("QLabel { background-color : red; color : black; }")
if self.sb_start_slot.maximum() != self.msp.get_tpt_tray_size():
self.sb_start_slot.setMaximum(self.msp.get_tpt_tray_size())
self.sb_start_slot.setValue(1)
self.sb_end_slot.setMaximum(self.msp.get_tpt_tray_size())
self.sb_end_slot.setValue(self.msp.get_tpt_tray_size())
if self.msp.get_slot() is None:
self.progressbar.setRange(0, 0)
else:
self.progressbar.setRange(0, self.msp.get_tpt_tray_size())
self.progressbar.setValue(self.msp.get_slot())
def msp_update_from_gui(self):
self.msp.led_enabled = self.check_led.isChecked()
self.msp.cam_enabled = self.check_cam.isChecked()
self.msp.tpt_enabled = self.check_tpt.isChecked()
self.msp.auto_home_enabled = self.cb_auto_home.isChecked()
self.msp.capture_settle_delay = self.sb_settle_delay.value()
# check if ok to start
msg = ""
if self.msp.led_enabled and not self.msp.led_connected:
msg = "Lamp not connected\n\n"
if self.msp.tpt_enabled and not self.msp.tpt_connected:
msg += "Transport not connected\n\n"
if self.msp.cam_enabled and not self.msp.cam_connected:
msg += "Camera not connected"
if msg:
# noinspection PyCallByClass,PyArgumentList,PyTypeChecker
QMessageBox.information(self, 'Can not start scanning', msg)
return False
else:
return True
def led_check_change(self, event):
if event == 2:
log.debug("led open")
self.work_queue.put(lambda: self.msp.led_connect())
elif event == 0:
log.debug("led close")
self.work_queue.put(lambda: self.msp.led_connect(open=False))
def cam_check_change(self, event):
if event == 2:
log.debug("cam open")
self.work_queue.put(lambda: self.msp.cam_connect())
elif event == 0:
log.debug("cam close")
self.work_queue.put(lambda: self.msp.cam_connect(open=False))
def tpt_check_change(self, event):
if event == 2:
log.debug("tpt open")
self.work_queue.put(lambda: self.msp.tpt_connect())
elif event == 0:
log.debug("tpt close")
self.work_queue.put(lambda: self.msp.tpt_connect(open=False))
# noinspection PyUnresolvedReferences
def create_actions(self):
self.check_tpt.stateChanged.connect(self.tpt_check_change)
self.check_led.stateChanged.connect(self.led_check_change)
self.check_cam.stateChanged.connect(self.cam_check_change)
# connect to uic generated objects
self.tb_play.clicked.connect(self.scan)
self.tb_stop.clicked.connect(self.scan_stop)
self.tb_next.clicked.connect(self.scan_next)
self.tb_prev.clicked.connect(self.scan_prev)
self.pushButton_capture.clicked.connect(self.scan_current)
self.pushButton_lamp.clicked.connect(self.lamp_on)
self.pushButton_tpt_home.clicked.connect(self.tpt_home)
self.pushButton_tpt_reset.clicked.connect(self.tpt_reset)
self.open_act.triggered.connect(self.open)
self.reload_act.triggered.connect(self.reload_img)
self.print_act.triggered.connect(self.print_img)
self.save_act.triggered.connect(self.save_img)
self.close_act.triggered.connect(self.close)
# self.exit_act.triggered.connect(self.exit_handler)
self.fulls_act.triggered.connect(self.toggle_fs)
self.ss_act.triggered.connect(self.toggle_slideshow)
self.ss_next_act.triggered.connect(self.set_slide_type)
self.next_act.triggered.connect(self.go_next_img)
self.prev_act.triggered.connect(self.go_prev_img)
self.rotleft_act.triggered.connect(partial(self.img_rotate, 270))
self.rotright_act.triggered.connect(partial(self.img_rotate, 90))
self.fliph_act.triggered.connect(partial(self.img_flip, -1, 1))
self.flipv_act.triggered.connect(partial(self.img_flip, 1, -1))
self.resize_act.triggered.connect(self.resize_image)
self.crop_act.triggered.connect(self.crop_image)
self.zin_act.triggered.connect(partial(self.img_view.zoom, 1.1))
self.zout_act.triggered.connect(partial(self.img_view.zoom, 1 / 1.1))
self.fit_win_act.triggered.connect(self.zoom_default)
self.fit_win_act.setChecked(True)
self.prefs_act.triggered.connect(self.set_prefs)
self.props_act.triggered.connect(self.get_props)
self.help_act.triggered.connect(self.help_page)
self.about_act.triggered.connect(self.about_cm)
self.sb_current_slot.valueChanged.connect(self.tpt_slot_current)
self.lbl_led_status = QLabel(self)
self.statusbar.addPermanentWidget(self.lbl_led_status)
self.lbl_led_status.setText("Led: Not connected")
self.lbl_tpt_status = QLabel(self)
self.statusbar.addPermanentWidget(self.lbl_tpt_status)
self.lbl_tpt_status.setText("Transport: Not connected")
self.lbl_cam_status = QLabel(self)
self.statusbar.addPermanentWidget(self.lbl_cam_status)
self.lbl_cam_status.setText("Camera: Not connected")
self.lbl_slot_status = QLabel(self)
self.statusbar.addPermanentWidget(self.lbl_slot_status)
self.lbl_slot_status.setText("Slot: N/A")
self.lbl_tpt_slot_status = QLabel(self)
self.statusbar.addPermanentWidget(self.lbl_tpt_slot_status)
self.lbl_tpt_slot_status.setText("No slide")
self.progressbar = QProgressBar(self)
self.progressbar.setMaximumSize(170, 19)
self.progressbar.setFormat("Slot:%v (%p%)")
self.progressbar.setRange(0, 0)
self.statusbar.addPermanentWidget(self.progressbar)
def create_menu(self):
self.popup = QMenu(self)
main_acts = [self.open_act, self.reload_act, self.print_act, self.save_act]
edit_acts1 = [self.rotleft_act, self.rotright_act, self.fliph_act, self.flipv_act]
edit_acts2 = [self.resize_act, self.crop_act]
view_acts = [self.next_act, self.prev_act, self.zin_act, self.zout_act, self.fit_win_act, self.fulls_act,
self.ss_act, self.ss_next_act]
help_acts = [self.help_act, self.about_act]
end_acts = [self.prefs_act, self.props_act, self.close_act, self.exit_act]
for act in main_acts:
self.popup.addAction(act)
edit_menu = QMenu(self.popup)
edit_menu.setTitle('&Edit')
for act in edit_acts1:
edit_menu.addAction(act)
edit_menu.addSeparator()
for act in edit_acts2:
edit_menu.addAction(act)
self.popup.addMenu(edit_menu)
view_menu = QMenu(self.popup)
view_menu.setTitle('&View')
for act in view_acts:
view_menu.addAction(act)
self.popup.addMenu(view_menu)
help_menu = QMenu(self.popup)
help_menu.setTitle('&Help')
for act in help_acts:
help_menu.addAction(act)
self.popup.addMenu(help_menu)
for act in end_acts:
self.popup.addAction(act)
# was self.
action_list = main_acts + edit_acts1 + edit_acts2 + view_acts + help_acts + end_acts
for act in action_list:
self.addAction(act)
def show_menu(self, pos):
self.popup.popup(self.mapToGlobal(pos))
def read_prefs(self):
"""Parse the preferences from the config file, or set default values."""
conf = preferences.Config(__name__ + ".ini")
try:
values = conf.read_config()
self.auto_orient = values[0]
self.slide_delay = values[1]
self.quality = values[2]
except KeyError:
self.auto_orient = True
self.slide_delay = 5
self.quality = 90
except:
log.error("Unexpected error:", sys.exc_info()[0])
raise
self.reload_img = self.reload_auto if self.auto_orient else self.reload_nonauto
def set_prefs(self):
"""Write preferences to the config file."""
dialog = preferences.PrefsDialog(self)
if dialog.exec_() == QDialog.Accepted:
self.auto_orient = dialog.auto_orient
self.slide_delay = dialog.delay_spinb.value()
self.quality = dialog.qual_spinb.value()
conf = preferences.Config(__name__ + '.ini')
conf.write_config(self.auto_orient, self.slide_delay, self.quality)
self.reload_img = self.reload_auto if self.auto_orient else self.reload_nonauto
# fname = QFileDialog.getExistingDirectory(self)
def open(self):
# noinspection PyArgumentList,PyCallByClass,PyTypeChecker
fname = QFileDialog.getOpenFileName(self, 'Open File', self.pics_dir)[0]
if fname:
if fname.lower().endswith(read_list()):
self.open_img(fname)
else:
# noinspection PyArgumentList,PyCallByClass,PyTypeChecker
QMessageBox.information(self, 'Error', 'Cannot load image.'.format(fname.rsplit('.', 1)[1]))
def open_img(self, fname):
self.fname = fname
self.reload_img()
dirname = os.path.dirname(self.fname)
self.set_img_list(dirname)
self.img_index = self.filelist.index(self.fname)
self.statusbar.showMessage(fname, 2000)
def set_img_list(self, dirname):
"""Create a list of readable images from the current directory."""
filelist = os.listdir(dirname)
self.filelist = [os.path.join(dirname, fname) for fname in filelist
if fname.lower().endswith(read_list())]
self.filelist.sort()
self.last_file = len(self.filelist) - 1
def get_img(self):
"""Get image from fname and create pixmap."""
# noinspection PyCallByClass,PyArgumentList,PyTypeChecker
self.pixmap = QPixmap.fromImage(QImage(self.fname))
self.setWindowTitle(self.fname.rsplit('/', 1)[1])
def create_dict(self):
"""Create a dictionary to handle auto-orientation."""
return {None: self.load_img,
'1': self.load_img,
'2': partial(self.img_flip, -1, 1),
'3': partial(self.img_rotate, 180),
'4': partial(self.img_flip, -1, 1),
'5': self.img_rotate_flip_horizontal,
'6': partial(self.img_rotate, 90),
'7': self.img_rotate_flip_vertical,
'8': partial(self.img_rotate, 270)}
def reload_auto(self):
"""Load a new image with auto-orientation."""
self.get_img()
# try:
# f = open(self.fname, 'rb')
# tags = exifread.process_file(f, stop_tag='Image Orientation', details=False)
#orient = tags.key('Image Orientation')
#tags = exifread.process_file(f, ')
#exif = GExiv2.Metadata(self.fname)
#exif.get_exif_tags()
#orient = GExiv2.Metadata()
#orient. self.fname)['Exif.Image.Orientation']
#self.orient_dict[orient]()
#except:
self.load_img()
def reload_nonauto(self):
"""Load a new image without auto-orientation."""
self.get_img()
self.load_img()
def load_img_fit(self):
"""Load the image to fit the window."""
self.scene.clear()
# todo
self.scene.addPixmap(self.pixmap)
self.scene.setSceneRect(0, 0, self.pixmap.width(), self.pixmap.height())
self.img_view.fitInView(self.scene.sceneRect(), Qt.KeepAspectRatio)
def load_img_1to1(self):
"""Load the image at its original size."""
self.scene.clear()
self.img_view.resetTransform()
self.scene.addPixmap(self.pixmap)
self.scene.setSceneRect(0, 0, self.pixmap.width(), self.pixmap.height())
pixitem = QGraphicsPixmapItem(self.pixmap)
self.img_view.centerOn(pixitem)
def go_next_img(self):
self.img_index = self.img_index + 1 if self.img_index < self.last_file else 0
self.fname = self.filelist[self.img_index]
self.reload_img()
def go_prev_img(self):
self.img_index = self.img_index - 1 if self.img_index else self.last_file
self.fname = self.filelist[self.img_index]
self.reload_img()
def zoom_default(self):
"""Toggle best fit / original size loading."""
if self.fit_win_act.isChecked():
self.load_img = self.load_img_fit
# self.create_dict()
self.load_img()
else:
self.load_img = self.load_img_1to1
# self.create_dict()
self.load_img()
def img_rotate(self, angle):
self.pixmap = self.pixmap.transformed(QTransform().rotate(angle))
self.load_img()
def img_flip(self, x, y):
self.pixmap = self.pixmap.transformed(QTransform().scale(x, y))
self.load_img()
def img_rotate_flip_horizontal(self):
self.img_rotate(90)
self.img_flip(-1, 1)
def img_rotate_flip_vertical(self):
self.img_rotate(90)
self.img_flip(1, -1)
def resize_image(self):
dialog = editimage.ResizeDialog(self, self.pixmap.width(), self.pixmap.height())
if dialog.exec_() == QDialog.Accepted:
width = dialog.get_width.value()
height = dialog.get_height.value()
self.pixmap = self.pixmap.scaled(width, height, Qt.IgnoreAspectRatio,
Qt.SmoothTransformation)
self.save_img()
def crop_image(self):
self.img_view.setup_crop(self.pixmap.width(), self.pixmap.height())
dialog = editimage.CropDialog(self, self.pixmap.width(), self.pixmap.height())
if dialog.exec_() == QDialog.Accepted:
coords = self.img_view.get_coords()
self.pixmap = self.pixmap.copy(*coords)
self.load_img()
self.img_view.rubber_band.hide()
def toggle_fs(self):
if self.fulls_act.isChecked():
self.showFullScreen()
else:
self.showNormal()
def toggle_slideshow(self):
if self.ss_act.isChecked():
self.showFullScreen()
self.start_ss()
else:
self.toggle_fs()
self.timer.stop()
self.ss_timer.stop()
def start_ss(self):
self.timer = QTimer()
# noinspection PyUnresolvedReferences
self.timer.timeout.connect(self.update_img)
self.timer.start(self.slide_delay * 1000)
self.ss_timer = QTimer()
# noinspection PyUnresolvedReferences
self.ss_timer.timeout.connect(self.update_img)
self.ss_timer.start(60000)
def update_img(self):
if self.slides_next:
self.go_next_img()
else:
self.fname = random.choice(self.filelist)
self.reload_img()
def set_slide_type(self):
self.slides_next = self.ss_next_act.isChecked()
def save_img(self):
pass
# # noinspection PyCallByClass,PyTypeChecker
# fname = QFileDialog.getSaveFileName(self, 'Save your image', self.fname)[0]
# if fname:
# if fname.lower().endswith(write_list()):
# # noinspection PyCallByClass,PyTypeChecker
# keep_exif = QMessageBox.question(self, 'Save exif data',
# 'Do you want to save the picture metadata?', QMessageBox.Yes |
# QMessageBox.No, QMessageBox.Yes)
# if keep_exif == QMessageBox.Yes:
# self.pixmap.save(fname, None, self.quality)
# exif = GExiv2.Metadata(self.fname)
# if exif:
# saved_exif = GExiv2.Metadata(fname)
# for tag in exif.get_exif_tags():
# saved_exif[tag] = exif[tag]
# saved_exif.set_orientation(GExiv2.Orientation.NORMAL)
# saved_exif.save_file()
# else:
# # noinspection PyCallByClass,PyArgumentList,PyTypeChecker
# QMessageBox.information(self, 'Error', 'Cannot save {} images.'.format(fname.rsplit('.', 1)[1]))
def print_img(self):
dialog = QPrintDialog(self.printer, self)
if dialog.exec_():
painter = QPainter(self.printer)
rect = painter.viewport()
if self.pixmap.width() > self.pixmap.height():
self.pixmap = self.pixmap.transformed(QTransform().rotate(90))
size = self.pixmap.size()
size.scale(rect.size(), Qt.KeepAspectRatio)
painter.setViewport(rect.x(), rect.y(), size.width(), size.height())
painter.setWindow(self.pixmap.rect())
painter.drawPixmap(0, 0, self.pixmap)
def resizeEvent(self, event=None):
# originally no check for valid image file resulted in errors
if self.fit_win_act.isChecked() and len(self.fname):
self.load_img()
def get_props(self):
"""Get the properties of the current image."""
image = QImage(self.fname)
preferences.PropsDialog(self, self.fname.rsplit('/', 1)[1], image.width(), image.height())
def help_page(self):
preferences.HelpDialog(self)
def about_cm(self):
about_message = 'Version: 0.0.0\nAuthor: Dan Tyrrell\nwww.breager.com'
# noinspection PyCallByClass,PyArgumentList,PyTypeChecker
QMessageBox.about(self, 'About Mechaslide', about_message)
class ImageView(QGraphicsView):
# todo: parent is named - why?
def __init__(self, parent=None):
QGraphicsView.__init__(self, parent)
self.rubber_band = None
self.go_prev_img = parent.go_prev_img
self.go_next_img = parent.go_next_img
pal = self.palette()
pal.setColor(self.backgroundRole(), Qt.black)
self.setPalette(pal)
self.setFrameShape(QFrame.NoFrame)
def mousePressEvent(self, event):
"""Go to the next / previous image, or be able to drag the image with a hand."""
if event.button() == Qt.LeftButton:
x = event.x()
if x < 100:
self.go_prev_img()
elif x > self.width() - 100:
self.go_next_img()
else:
self.setDragMode(QGraphicsView.ScrollHandDrag)
QGraphicsView.mousePressEvent(self, event)
def mouseReleaseEvent(self, event):
self.setDragMode(QGraphicsView.NoDrag)
QGraphicsView.mouseReleaseEvent(self, event)
def zoom(self, zoom_ratio):
self.scale(zoom_ratio, zoom_ratio)
def wheelEvent(self, event):
zoom_ratio = 1.1
if event.angleDelta().y() < 0:
zoom_ratio = 1.0 / zoom_ratio
self.scale(zoom_ratio, zoom_ratio)
def setup_crop(self, width, height):
self.rubber_band = QRubberBand(QRubberBand.Rectangle, self)
coords = self.mapFromScene(0, 0, width, height)
self.rubber_band.setGeometry(QRect(coords.boundingRect()))
self.rubber_band.show()
def crop_draw(self, x, y, width, height):
coords = self.mapFromScene(x, y, width, height)
self.rubber_band.setGeometry(QRect(coords.boundingRect()))
def get_coords(self):
rect = self.rubber_band.geometry()
size = self.mapToScene(rect).boundingRect()
x = int(size.x())
y = int(size.y())
width = int(size.width())
height = int(size.height())
return x, y, width, height
# supports more than one image - see Cheesemaker source
class ImageViewer(QApplication):
def __init__(self, args):
QApplication.__init__(self, args)
self.args = args
self.win = MechascanSlideGUI()
self.win.show()
if __name__ == '__main__':
import sys
app = ImageViewer(sys.argv)
sys.exit(app.exec_()) # without sys.exit does not exit...