1 |
|
import sys
|
2 |
|
from PyQt5.QtCore import Qt
|
3 |
|
from PyQt5.QtWidgets import QWidget, QApplication, QGridLayout, QLabel
|
4 |
|
from PyQt5 import QtCore, QtGui, QtWidgets
|
5 |
|
from PyQt5.QtWidgets import *
|
6 |
|
from PyQt5.QtGui import *
|
7 |
|
|
8 |
|
CROP_AREA_MIN_SIZE = 10
|
9 |
|
|
10 |
|
class QcImageViewer(QGraphicsView):
|
11 |
|
def __init__(self, filePath, imgW, imgH):
|
12 |
|
super().__init__()
|
13 |
|
print("__init__")
|
14 |
|
self.filePath = filePath
|
15 |
|
self.selectedArea = [(0, 0),(imgW, imgH)]
|
16 |
|
self.imgW = imgW
|
17 |
|
self.imgH = imgH
|
18 |
|
self.zoom = 1.0
|
19 |
|
self.isPressCtrl = False
|
20 |
|
#PyQt5.QtCore.QRectF(3740.953125, 1164.765625, 1479.9375, 1891.03125)
|
21 |
|
self.initImage()
|
22 |
|
self.setMouseTracking(True)
|
23 |
|
#self.mousePressEvent = self.mousePressEvent
|
24 |
|
#self.mouseReleaseEvent = self.mouseReleaseEvent
|
25 |
|
#self.mouseDoubleClickEvent = self.mouseDoubleClickEvent
|
26 |
|
#self.wheelEvent = self.wheelEvent
|
27 |
|
#self.keyPressEvent = self.keyPressEvent
|
28 |
|
#self.keyReleaseEvent = self.keyReleaseEvent
|
29 |
|
|
30 |
|
def mousePressEvent(self, event):
|
31 |
|
if event.button() == QtCore.Qt.LeftButton:
|
32 |
|
print("Pressed : " + str(event.x()) + ", " + str(event.y()))
|
33 |
|
self.originPoint = event.pos()
|
34 |
|
self.currentRect = QtCore.QRect()
|
35 |
|
self.currentRect.setTopLeft(QtCore.QPoint(event.x(), event.y()))
|
36 |
|
|
37 |
|
def mouseReleaseEvent(self, event):
|
38 |
|
if event.button() == QtCore.Qt.LeftButton:
|
39 |
|
print("Released : " + str(event.x()) + ", " + str(event.y()))
|
40 |
|
self.currentRect.setBottomRight(QtCore.QPoint(event.x(), event.y()))
|
41 |
|
|
42 |
|
if self.currentRect.width() > CROP_AREA_MIN_SIZE and self.currentRect.height() > CROP_AREA_MIN_SIZE:
|
43 |
|
pixmap = self.pixmap().copy(self.currentRect)
|
44 |
|
pixmap.scaled(self.currentRect.width(), self.currentRect.height())
|
45 |
|
print("New Size : " + str(self.currentRect.width()) + "/" + str(self.currentRect.height()))
|
46 |
|
self.setPixmap(pixmap)
|
47 |
|
self.resize(self.currentRect.width(), self.currentRect.height())
|
48 |
|
self.parentWidget().resize(self.currentRect.width(), self.currentRect.height())
|
49 |
|
|
50 |
|
def mouseDoubleClickEvent(self, event):
|
51 |
|
if event.button() == QtCore.Qt.RightButton:
|
52 |
|
self.initImage()
|
53 |
|
|
54 |
|
def keyPressEvent(self, event):
|
55 |
|
if event.key() == QtCore.Qt.Key_Control:
|
56 |
|
self.isPressCtrl = True
|
57 |
|
print("Pressed Ctrl")
|
58 |
|
|
59 |
|
def keyReleaseEvent(self, event):
|
60 |
|
if event.key() == QtCore.Qt.Key_Control:
|
61 |
|
self.isPressCtrl = False
|
62 |
|
print("Released Ctrl")
|
63 |
|
|
64 |
|
def wheelEvent(self, event):
|
65 |
|
if self.isPressCtrl == True:
|
66 |
|
event.modifiers()
|
67 |
|
|
68 |
|
def initImage(self):
|
69 |
|
self.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
|
70 |
|
self.setScaledContents(True)
|
71 |
|
pixmap = QPixmap(self.filePath)
|
72 |
|
pixmap = pixmap.scaled(self.imgW, self.imgH, QtCore.Qt.KeepAspectRatioByExpanding)
|
73 |
|
self.setPixmap(pixmap)
|
74 |
|
self.resize(self.imgW, self.imgH)
|
75 |
|
if self.parentWidget() is not None:
|
76 |
|
self.parentWidget().resize(self.imgW, self.imgH)
|