hytos / DTI_PID / DTI_PID / Commands / CropCommand.py @ 87a92ef6
이력 | 보기 | 이력해설 | 다운로드 (2.04 KB)
1 |
import os.path |
---|---|
2 |
import AbstractCommand |
3 |
try:
|
4 |
from PyQt5.QtCore import Qt, QRectF, pyqtSignal, QT_VERSION_STR |
5 |
from PyQt5.QtGui import QImage, QPixmap, QPainterPath |
6 |
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QFileDialog |
7 |
except ImportError: |
8 |
try:
|
9 |
from PyQt4.QtCore import Qt, QRectF, pyqtSignal, QT_VERSION_STR |
10 |
from PyQt4.QtGui import QGraphicsView, QGraphicsScene, QImage, QPixmap, QPainterPath, QFileDialog |
11 |
except ImportError: |
12 |
raise ImportError("ImageViewerQt: Requires PyQt5 or PyQt4.") |
13 |
|
14 |
class CropCommand(AbstractCommand.AbstractCommand): |
15 |
def __init__(self, imageViewer): |
16 |
super(CropCommand, self).__init__(imageViewer) |
17 |
self.name = 'Crop' |
18 |
|
19 |
'''
|
20 |
@brief crop image by rectangle selected by user
|
21 |
'''
|
22 |
def execute(self, param): |
23 |
event = param[1]
|
24 |
scenePos = param[2]
|
25 |
if 'mousePressEvent' == param[0] and event.button() == Qt.LeftButton: |
26 |
self.imageViewer.setDragMode(QGraphicsView.RubberBandDrag)
|
27 |
self.imageViewer.leftMouseButtonPressed.emit(scenePos.x(), scenePos.y())
|
28 |
#QGraphicsView.mousePressEvent(self.imageViewer, event)
|
29 |
elif 'mouseReleaseEvent' == param[0] and event.button() == Qt.LeftButton: |
30 |
try:
|
31 |
viewBBox = self.imageViewer.zoomStack[-1] if len(self.imageViewer.zoomStack) else self.imageViewer.sceneRect() |
32 |
selectionBBox = self.imageViewer.scene.selectionArea().boundingRect().intersected(viewBBox)
|
33 |
if selectionBBox.isValid():
|
34 |
croppedImage = self.imageViewer.image().copy(selectionBBox.toAlignedRect())
|
35 |
self.imageViewer.setImage(croppedImage)
|
36 |
self.imageViewer.setDragMode(QGraphicsView.NoDrag)
|
37 |
finally:
|
38 |
self.imageViewer.leftMouseButtonReleased.emit(scenePos.x(), scenePos.y())
|
39 |
#QGraphicsView.mouseReleaseEvent(self.imageViewer, event)
|
40 |
pass
|
41 |
self.isTreated = False |
42 |
|
43 |
def undo(self): |
44 |
pass
|
45 |
|
46 |
def redo(self): |
47 |
pass
|