本文整理汇总了Python中PyQt5.QtWidgets.QGraphicsScene.update方法的典型用法代码示例。如果您正苦于以下问题:Python QGraphicsScene.update方法的具体用法?Python QGraphicsScene.update怎么用?Python QGraphicsScene.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtWidgets.QGraphicsScene
的用法示例。
在下文中一共展示了QGraphicsScene.update方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Demo
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsScene [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsScene import update [as 别名]
class Demo(QGraphicsView):
def __init__(self, parent=None):
super(Demo, self).__init__(parent)
# Setup scene
self.m_scene = QGraphicsScene()
self.setScene(self.m_scene)
self.setup_scene()
self.setFixedSize(WINDOW_WIDTH, WINDOW_HEIGHT)
# Set up entities (not attached to this term, but basically things like light sources, level, npcs)
self.m_lightSource = None
self.level = Level(self, fp='assets/level_test.txt')
self.player = Player(self, MAP_WIDTH/2, MAP_HEIGHT/2, Link(parent=self))
# Setup animation timer
self.timer = QTimer(self)
self.timer.timeout.connect(self.animate)
self.timer.setInterval(30)
self.timer.start()
# Render settings
self.setRenderHint(QPainter.Antialiasing)
self.setFrameStyle(QFrame.NoFrame)
# Input management
self.mouse_down = False
self.setMouseTracking(True)
self.key_pressed = False
def setup_scene(self):
self.m_scene.setSceneRect(0, 0, MAP_WIDTH, MAP_HEIGHT)
linear_grad = QLinearGradient(QPointF(-100, -100), QPointF(100, 100))
linear_grad.setColorAt(0, QColor(255, 255, 255))
linear_grad.setColorAt(1, QColor(192, 192, 255))
self.setBackgroundBrush(linear_grad)
def animate(self):
self.centerOn(self.player.pos.x(), self.player.pos.y())
self.m_scene.update()
def get_angle(self, event):
mouse_pos = Position(x=event.pos().x(), y=event.pos().y())
mouse_pos = mouse_pos + self.player.pos - Position(WINDOW_WIDTH/2, WINDOW_HEIGHT/2)
# print('Player : %s' % self.player.pos)
# print('Mouse : %s' % mouse_pos)
# print('Angle : %f' % (self.player.pos < mouse_pos))
return self.player.pos < mouse_pos
def keyPressEvent(self, event):
key = event.key()
if key == Qt.Key_Shift:
self.player.speed = 3
self.player.stamina_fade = -.1
if event.isAutoRepeat() or self.mouse_down:
return
self.key_pressed = True
if key == Qt.Key_Up or key == Qt.Key_W:
self.player.set_state('up')
self.player.vel = Position(0, -self.player.speed)
if key == Qt.Key_Down or key == Qt.Key_S:
self.player.set_state('down')
self.player.vel = Position(0, self.player.speed)
if key == Qt.Key_Left or key == Qt.Key_A:
self.player.set_state('left')
self.player.vel = Position(-self.player.speed, 0)
if key == Qt.Key_Right or key == Qt.Key_D:
self.player.set_state('right')
self.player.vel = Position(self.player.speed, 0)
if key == Qt.Key_Space:
if self.player.state() == 'static':
self.player.set_state('blink')
else:
self.player.set_state('static')
if key == Qt.Key_Escape:
exit()
# super(Demo, self).keyPressEvent(event)
def keyReleaseEvent(self, event):
key = event.key()
if key == Qt.Key_Shift:
self.player.speed = 1
self.player.stamina_fade = .1
if event.isAutoRepeat() or self.mouse_down:
return
self.player.vel = Position(0, 0)
if self.player.state() == 'left' and (key == Qt.Key_Left or key == Qt.Key_A):
self.key_pressed = False
self.player.set_state('left_static')
if self.player.state() == 'right' and (key == Qt.Key_Right or key == Qt.Key_D):
self.key_pressed = False
self.player.set_state('right_static')
if self.player.state() == 'down' and (key == Qt.Key_Down or key == Qt.Key_S):
self.key_pressed = False
self.player.set_state('static')
if self.player.state() == 'up' and (key == Qt.Key_Up or key == Qt.Key_W):
self.key_pressed = False
self.player.set_state('up_static')
def wheelEvent(self, event):
#.........这里部分代码省略.........
示例2: Lighting
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsScene [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsScene import update [as 别名]
class Lighting(QGraphicsView):
def __init__(self, parent=None):
super(Lighting, self).__init__(parent)
self.angle = 0.0
self.m_scene = QGraphicsScene()
self.m_lightSource = None
self.m_items = []
self.setScene(self.m_scene)
self.setupScene()
self.timer = QTimer(self)
self.timer.timeout.connect(self.animate)
self.timer.setInterval(30)
self.timer.start()
self.setRenderHint(QPainter.Antialiasing)
self.setFrameStyle(QFrame.NoFrame)
def setupScene(self):
self.m_scene.setSceneRect(-300, -200, 600, 460)
linearGrad = QLinearGradient(QPointF(-100, -100), QPointF(100, 100))
linearGrad.setColorAt(0, Qt.darkGreen)#QColor(255, 255, 255))
linearGrad.setColorAt(1, Qt.green)#QQColor(192, 192, 255))
self.setBackgroundBrush(linearGrad)
radialGrad = QRadialGradient(30, 30, 30)
radialGrad.setColorAt(0, Qt.yellow)
radialGrad.setColorAt(0.2, Qt.yellow)
radialGrad.setColorAt(1, Qt.transparent)
pixmap = QPixmap(60, 60)
pixmap.fill(Qt.transparent)
painter = QPainter(pixmap)
painter.setPen(Qt.NoPen)
painter.setBrush(radialGrad)
painter.drawEllipse(0, 0, 60, 60)
painter.end()
self.m_lightSource = self.m_scene.addPixmap(pixmap)
self.m_lightSource.setZValue(2)
self.proto = ProtoObj(0, 0, 50, 50, self)
self.proto.initObj()
#self.m_items.append(self.proto.getObj()[0])
self.m_scene.addItem(self.proto.getObj()[0])
#self.m_scene.addItem(self.proto.getObj()[1])
def animate(self):
self.angle += (math.pi / 30)
xs = 200 * math.sin(self.angle) - 40 + 25
ys = 200 * math.cos(self.angle) - 40 + 25
self.m_lightSource.setPos(xs, ys)
item = self.proto.getObj()[0]
effect = item.graphicsEffect()
delta = QPointF(item.x() - xs, item.y() - ys)
effect.setOffset(QPointF(delta.toPoint() / 30))
dd = math.hypot(delta.x(), delta.y())
color = effect.color()
color.setAlphaF(max(0.4, min(1 - dd / 200.0, 0.7)))
effect.setColor(color)
item.setPos(self.proto.posX, self.proto.posY)
#self.proto.animate(0)
#self.proto.pix.setPos(self.proto.posX, self.proto.posY)
self.m_scene.update()
def keyPressEvent(self, event):
key = event.key()
if key == Qt.Key_Up or key == Qt.Key_W:
#print('Pressed Up or W?')
self.proto.moveObj(0, -10)
#self.player.move(0, -10)
if key == Qt.Key_Down or key == Qt.Key_S:
#print('Pressed Down or S?')
self.proto.moveObj(0, 10)
#self.player.move(0, 10)
if key == Qt.Key_Left or key == Qt.Key_A:
#print('Pressed Left or A?')
self.proto.moveObj(-10, 0)
#self.player.move(-10, 0)
if key == Qt.Key_Right or key == Qt.Key_D:
#print('Pressed Right or D?')
self.proto.moveObj(10, 0)
#self.player.move(10, 0)
if key == Qt.Key_Escape:
exit()
super(Lighting, self).keyPressEvent(event)
示例3: TcamScreen
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsScene [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsScene import update [as 别名]
class TcamScreen(QtWidgets.QGraphicsView):
new_pixmap = pyqtSignal(QtGui.QPixmap)
new_pixel_under_mouse = pyqtSignal(bool, int, int, QtGui.QColor)
destroy_widget = pyqtSignal()
fit_in_view = pyqtSignal()
def __init__(self, parent=None):
super(TcamScreen, self).__init__(parent)
self.setMouseTracking(True)
self.setSizePolicy(QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Expanding)
self.setDragMode(QGraphicsView.ScrollHandDrag)
self.setFrameStyle(0)
self.scene = QGraphicsScene(self)
self.setScene(self.scene)
self.new_pixmap.connect(self.on_new_pixmap)
self.fit_in_view.connect(self.fit_view)
self.pix = ViewItem()
self.scene.addItem(self.pix)
self.scene.setSceneRect(self.pix.boundingRect())
self.is_fullscreen = False
# Flag to differentiate between actual images
# and 'fake images' i.e. color background + text while
# waiting for first trigger image
self.display_real_image = True
self.text_item = None
self.fit_in_view_called = False
self.mouse_position_x = -1
self.mouse_position_y = -1
self.zoom_factor = 1.0
self.first_image = True
self.image_counter = 0
self.capture_roi = False
self.roi_obj = None
self.roi_origin = None
self.roi_widgets = []
self.selection_area = None
self.capture_widget = None
self.origin = None
def fit_view(self):
"""
"""
self.reset_zoom()
self.scene.setSceneRect(self.pix.boundingRect())
self.scene.update()
self.fitInView(self.scene.sceneRect(), Qt.KeepAspectRatio)
def reset_zoom(self):
self.zoom_factor = 1.0
# this resets the view internal transformation matrix
self.setTransform(QtGui.QTransform())
def on_new_pixmap(self, pixmap):
self.image_counter += 1
self.pix.setPixmap(pixmap)
if not self.display_real_image:
self.text_item.hide()
self.scene.removeItem(self.text_item)
self.display_real_image = True
if self.image_counter == 1:
self.resize(self.size())
self.scene.setSceneRect(self.pix.boundingRect())
self.update()
self.reset_zoom()
self.first_image = False
# wait for the second image
# resizeEvents, etc appear before the scene has adjusted
# to the actual image size. By waiting for the 2. image
# we circumvent this by having the first image making all
# adjustments for us. The only scenario where this will
# cause problems is triggering.
if self.is_fullscreen and self.image_counter == 2:
self.fit_view()
self.send_mouse_pixel()
# don't call repaint here
# it causes problems once the screen goes blank due to screensavers, etc
# self.repaint()
def wait_for_first_image(self):
if not self.display_real_image:
return
#.........这里部分代码省略.........
示例4: qfi_ALT
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsScene [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsScene import update [as 别名]
class qfi_ALT (QGraphicsView):
viewUpdate = pyqtSignal()
def __init__(self,winParent):
QGraphicsView.__init__(self)
self.winParent=winParent
self.viewUpdate.connect(self.updateView)
self.m_altitude = 0
self.m_pressure = 28
self.m_originalHeight = 240
self.m_originalWidth = 240
self.m_originalAltCtr = QPointF(120,120)
self.m_face1Z = -50
self.m_face2Z = -40
self.m_face3Z = -30
self.m_hand1Z = -20
self.m_hand2Z = -10
self.m_caseZ = 10
self.setStyleSheet("background: transparent; border: none")
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setInteractive(False)
self.setEnabled(False)
self.m_scene = QGraphicsScene(self)
self.setScene(self.m_scene)
self.init()
def init (self):
self.m_scaleX = self.width() / self.m_originalWidth
self.m_scaleY = self.height() / self.m_originalHeight
self.m_itemFace_1 = QGraphicsSvgItem(":/qfi/images/alt/alt_face_1.svg")
self.m_itemFace_1.setCacheMode (QGraphicsItem.NoCache)
self.m_itemFace_1.setZValue( self.m_face1Z )
self.m_itemFace_1.setTransform( QTransform.fromScale( self.m_scaleX, self.m_scaleY ), True )
self.m_itemFace_1.setTransformOriginPoint( self.m_originalAltCtr )
self.m_scene.addItem (self.m_itemFace_1)
self.m_itemFace_2 = QGraphicsSvgItem(":/qfi/images/alt/alt_face_2.svg")
self.m_itemFace_2.setCacheMode (QGraphicsItem.NoCache)
self.m_itemFace_2.setZValue( self.m_face2Z )
self.m_itemFace_2.setTransform( QTransform.fromScale( self.m_scaleX, self.m_scaleY ), True )
self.m_itemFace_2.setTransformOriginPoint( self.m_originalAltCtr )
self.m_scene.addItem (self.m_itemFace_2)
self.m_itemFace_3 = QGraphicsSvgItem(":/qfi/images/alt/alt_face_3.svg")
self.m_itemFace_3.setCacheMode (QGraphicsItem.NoCache)
self.m_itemFace_3.setZValue( self.m_face3Z )
self.m_itemFace_3.setTransform( QTransform.fromScale( self.m_scaleX, self.m_scaleY ), True )
self.m_itemFace_3.setTransformOriginPoint( self.m_originalAltCtr )
self.m_scene.addItem (self.m_itemFace_3)
self.m_itemHand_1 = QGraphicsSvgItem(":/qfi/images/alt/alt_hand_1.svg")
self.m_itemHand_1.setCacheMode (QGraphicsItem.NoCache)
self.m_itemHand_1.setZValue( self.m_hand1Z )
self.m_itemHand_1.setTransform( QTransform.fromScale( self.m_scaleX, self.m_scaleY ), True )
self.m_itemHand_1.setTransformOriginPoint( self.m_originalAltCtr )
self.m_scene.addItem (self.m_itemHand_1)
self.m_itemHand_2 = QGraphicsSvgItem(":/qfi/images/alt/alt_hand_2.svg")
self.m_itemHand_2.setCacheMode (QGraphicsItem.NoCache)
self.m_itemHand_2.setZValue( self.m_hand2Z )
self.m_itemHand_2.setTransform( QTransform.fromScale( self.m_scaleX, self.m_scaleY ), True )
self.m_itemHand_2.setTransformOriginPoint( self.m_originalAltCtr )
self.m_scene.addItem (self.m_itemHand_2)
self.m_itemCase = QGraphicsSvgItem(":/qfi/images/alt/alt_case.svg")
self.m_itemCase.setCacheMode (QGraphicsItem.NoCache)
self.m_itemCase.setZValue( self.m_caseZ )
self.m_itemCase.setTransform( QTransform.fromScale( self.m_scaleX, self.m_scaleY ), True )
self.m_itemCase.setTransformOriginPoint( self.m_originalAltCtr )
self.m_scene.addItem (self.m_itemCase)
self.centerOn (self.width()/2, self.height()/2)
self.updateView()
def reinit(self):
if (self.m_scene):
self.m_scene.clear()
self.init()
def update(self):
self.updateView()
#.........这里部分代码省略.........
示例5: Lighting
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsScene [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsScene import update [as 别名]
class Lighting(QGraphicsView):
def __init__(self, parent=None):
super(Lighting, self).__init__(parent)
self.angle = 0.0
self.m_scene = QGraphicsScene()
self.m_lightSource = None
self.m_items = []
self.setScene(self.m_scene)
self.setupScene()
timer = QTimer(self)
timer.timeout.connect(self.animate)
timer.setInterval(30)
timer.start()
self.setRenderHint(QPainter.Antialiasing)
self.setFrameStyle(QFrame.NoFrame)
def setupScene(self):
self.m_scene.setSceneRect(-300, -200, 600, 460)
linearGrad = QLinearGradient(QPointF(-100, -100), QPointF(100, 100))
linearGrad.setColorAt(0, QColor(255, 255, 255))
linearGrad.setColorAt(1, QColor(192, 192, 255))
self.setBackgroundBrush(linearGrad)
radialGrad = QRadialGradient(30, 30, 30)
radialGrad.setColorAt(0, Qt.yellow)
radialGrad.setColorAt(0.2, Qt.yellow)
radialGrad.setColorAt(1, Qt.transparent)
pixmap = QPixmap(60, 60)
pixmap.fill(Qt.transparent)
painter = QPainter(pixmap)
painter.setPen(Qt.NoPen)
painter.setBrush(radialGrad)
painter.drawEllipse(0, 0, 60, 60)
painter.end()
self.m_lightSource = self.m_scene.addPixmap(pixmap)
self.m_lightSource.setZValue(2)
for i in range(-2, 3):
for j in range(-2, 3):
if (i + j) & 1:
item = QGraphicsEllipseItem(0, 0, 50, 50)
else:
item = QGraphicsRectItem(0, 0, 50, 50)
item.setPen(QPen(Qt.black, 1))
item.setBrush(QBrush(Qt.white))
effect = QGraphicsDropShadowEffect(self)
effect.setBlurRadius(8)
item.setGraphicsEffect(effect)
item.setZValue(1)
item.setPos(i * 80, j * 80)
self.m_scene.addItem(item)
self.m_items.append(item)
def animate(self):
self.angle += (math.pi / 30)
xs = 200 * math.sin(self.angle) - 40 + 25
ys = 200 * math.cos(self.angle) - 40 + 25
self.m_lightSource.setPos(xs, ys)
for item in self.m_items:
effect = item.graphicsEffect()
delta = QPointF(item.x() - xs, item.y() - ys)
effect.setOffset(QPointF(delta.toPoint() / 30))
dd = math.hypot(delta.x(), delta.y())
color = effect.color()
color.setAlphaF(max(0.4, min(1 - dd / 200.0, 0.7)))
effect.setColor(color)
self.m_scene.update()
示例6: StaticPhotos
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsScene [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsScene import update [as 别名]
#.........这里部分代码省略.........
self.showImage()
def getCurPhotoFilename(self):
return self.photoFileManager.getCurPhotoFilename()
def picChangeFn(self):
# pass
if self.userMovedBack:
# Skip this update
self.userMovedBack = False
else:
self.nextPicItem()
self.picChgTimer.setInterval(self.picChangeMs)
self.picChgTimer.start()
def loadImage(self):
self.newImg = QImage()
self.newImg.load(self.photoFileManager.getCurPhotoFilename())
self.newImgInfo = self.photoFileManager.getCurPhotoInfo()
transform = QTransform()
transform.rotate(self.newImgInfo.rotationAngle)
self.interImg = self.newImg.transformed(transform, Qt.SmoothTransformation)
# xReqdSize = self.cellSize.width() * xFactor + self.xBetweenPics * (xFactor-1)
# yReqdSize = self.cellSize.height() * yFactor + self.yBetweenPics * (yFactor-1)
self.inter2Img = self.interImg.scaled(QSize(self.width(),self.height()),
Qt.KeepAspectRatio, Qt.SmoothTransformation)
# finalImg = interImg.copy(0,0,xReqdSize,yReqdSize)
# print("XY Size", xFactor, yFactor, xReqdSize,yReqdSize)
return self.inter2Img, self.newImgInfo
def showImage(self):
(newImg, newImgInfo) = self.loadImage()
# return PicItem(Pixmap(QPixmap(newImg)), -1, -1, xFactor, yFactor, newImgInfo)
self.scene.clear()
imgSz = newImgInfo.imgSize
self.setSceneRect(QRectF(0,0,imgSz.width(), imgSz.height()))
pixMap = QPixmap.fromImage(newImg)
# # pixMap.setWidth(self.width())
pixMapItem = self.scene.addPixmap(pixMap)
# pixMapItem.setPos(50,50)
# self.fitInView(QRectF(0, 0, self.width(), self.height()), Qt.KeepAspectRatio)
# Add caption
caption = QGraphicsTextItem()
caption.setDefaultTextColor(QColor(255,255,255))
caption.setPos(0, self.height()*0.94)
caption.setFont(QFont("Segoe UI", 30))
caption.setTextWidth(self.width())
# caption.setPos(100, 100)
# caption.setTextWidth(1500)
# if newImgInfo.createDate is not None:
# caption.setPlainText(newImgInfo.createDate.format());
# else:
# caption.setPlainText("Image is called bananas");
# print("Tags", newImgInfo.tags)
# tagStr = ""
# for tag in newImgInfo.tags:
# if tag != "Duplicate":
# tagStr += (", " if len(tagStr) != 0 else "") + tag
# if tagStr == "":
# tagStr = "NO TAGS"
# captionStr = '<h1 style="text-align:center;width:100%">' + tagStr + '</h1>'
# if newImgInfo.createDate is not None:
# print(newImgInfo.createDate.format())
# captionStr += '<BR><h2>' + newImgInfo.createDate.format() + '</h2>'
captionStr = ""
try:
if newImgInfo.rating is not None:
for i in range(newImgInfo.rating):
captionStr += "★"
for i in range(5-newImgInfo.rating):
captionStr += "☆"
if newImgInfo.mainDate is not None:
if len(captionStr) != 0:
captionStr += " "
captionStr += newImgInfo.mainDate.strftime("%d %b %Y")
except Exception as excp:
print("StaticPhotos: Cannot set caption")
captionStr = '<div style="background-color:#000000;text-align: right;padding-right:10dp;">' + captionStr + "</div>"
print(captionStr)
caption.setHtml(captionStr)
self.scene.addItem(caption)
self.scene.update()
def prevPicItem(self):
if self.photoFileManager.getNumPhotos() == 0:
return None
self.photoFileManager.movePrev()
# print ("Loaded photo", self.sourcePhotoList[self.curPhotoIdx], " w", finalImg.width(), " h", finalImg.height(), " facs", xFactor, yFactor)
self.showImage()
def nextPicItem(self):
if self.photoFileManager.getNumPhotos() == 0:
return None
# print ("Loaded photo", self.sourcePhotoList[self.curPhotoIdx], " w", finalImg.width(), " h", finalImg.height(), " facs", xFactor, yFactor)
self.photoFileManager.moveNext()
self.showImage()
def keyPressEvent(self, event): #QKeyEvent
event.ignore()
示例7: qfi_VSI
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsScene [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsScene import update [as 别名]
class qfi_VSI (QGraphicsView):
viewUpdate = pyqtSignal()
def __init__(self,winParent):
QGraphicsView.__init__(self)
self.winParent=winParent
self.viewUpdate.connect(self.update)
self.m_climbRate = 0
self.m_scaleX = 0
self.m_scaleY = 0
self.m_originalHeight = 240
self.m_originalWidth = 240
self.m_originalVsiCtr = QPointF(120,120)
self.m_faceZ = -20
self.m_handZ = -10
self.m_caseZ = 10
self.m_itemHand = None
self.m_itemFace = None
self.m_itemCase = None
self.setStyleSheet("background: transparent; border: none");
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setInteractive(False)
self.setEnabled(False)
self.m_scene = QGraphicsScene(self)
self.setScene(self.m_scene)
self.init()
def init (self):
self.m_scaleX = self.width() / self.m_originalWidth
self.m_scaleY = self.height() / self.m_originalHeight
self.m_itemHand = QGraphicsSvgItem(":/qfi/images/vsi/vsi_hand.svg")
self.m_itemHand.setCacheMode (QGraphicsItem.NoCache)
self.m_itemHand.setZValue( self.m_handZ )
self.m_itemHand.setTransform( QTransform.fromScale( self.m_scaleX, self.m_scaleY ), True )
self.m_itemHand.setTransformOriginPoint( self.m_originalVsiCtr )
self.m_scene.addItem (self.m_itemHand)
self.m_itemFace = QGraphicsSvgItem(":/qfi/images/vsi/vsi_face.svg")
self.m_itemFace.setCacheMode (QGraphicsItem.NoCache)
self.m_itemFace.setZValue( self.m_faceZ )
self.m_itemFace.setTransform( QTransform.fromScale( self.m_scaleX, self.m_scaleY ), True )
self.m_itemFace.setTransformOriginPoint( self.m_originalVsiCtr )
self.m_scene.addItem (self.m_itemFace)
self.m_itemCase = QGraphicsSvgItem(":/qfi/images/vsi/vsi_case.svg")
self.m_itemCase.setCacheMode (QGraphicsItem.NoCache)
self.m_itemCase.setZValue( self.m_caseZ )
self.m_itemCase.setTransform( QTransform.fromScale( self.m_scaleX, self.m_scaleY ), True )
self.m_itemCase.setTransformOriginPoint( self.m_originalVsiCtr )
self.m_scene.addItem (self.m_itemCase)
self.centerOn (self.width()/2, self.height()/2)
self.updateView()
def reinit(self):
if (self.m_scene):
self.m_scene.clear()
self.init()
def update(self):
self.updateView()
def setClimbRate (self, climbRate):
self.m_climbRate = climbRate
if (self.m_climbRate < -2000):
self.m_climbRate = -2000
if (self.m_climbRate > 2000):
self.m_climbRate = 2000
def resizeEvent (self, event):
QGraphicsView.resizeEvent (self,event)
self.reinit()
#.........这里部分代码省略.........
示例8: CanvasModel
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsScene [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsScene import update [as 别名]
class CanvasModel(object):
def __init__(self):
self.scene = QGraphicsScene() # Canvas area
self.scale_factor = 1 # Scaling factor
self.original_image = None # Original image, used for scaling
self.image = None # Image viewable in our canvas
self.c_width = 0 # Container width
self.c_height = 0 # Container height
def set_image(self, image):
self.original_image = image
def setGeometry(self, container_width, container_height):
self.c_width = container_width
self.c_height = container_height
self.scene.setSceneRect(0, 0, self.c_width, self.c_height)
def update_image(self, selection):
# Check for container width to prevent behaviour when container is not initialized yet (ex. passing an image as argument)
if self.original_image is not None and self.c_width != 0:
# Selection defines the viewport behaviour
# 0: Ratio scaling (uses canvas/image-size)
# 1: Scale to container width
# 2: Scale to container height
# 3: Scale to original size
if selection == 0:
ratio = [self.original_image.width() / self.c_width, self.original_image.height() / self.c_height]
if ratio[0] > 1 and ratio[0] > ratio[1]:
self.image = self.original_image.scaledToWidth(self.c_width, Qt.SmoothTransformation)
elif ratio[1] > 1 and ratio[1] > ratio[0]:
self.image = self.original_image.scaledToHeight(self.c_height, Qt.SmoothTransformation)
else: # Small image, show in original size
self.image = self.original_image
elif selection == 1:
self.image = self.original_image.scaledToWidth(self.c_width, Qt.SmoothTransformation)
elif selection == 2:
self.image = self.original_image.scaledToHeight(self.c_height, Qt.SmoothTransformation)
elif selection == 3:
self.image = self.original_image
# Update scene
self.scene.clear()
self.scene.addItem(CustomQGraphicsPixmapItem(QPixmap.fromImage(self.image), self.c_width, self.c_height))
self.scene.update()
def scale_image(self, factor):
if self.original_image is not None:
self.scale_factor *= factor
self.image = self.original_image.scaled(self.image.width() * self.scale_factor, self.image.height() * self.scale_factor, Qt.KeepAspectRatio, Qt.SmoothTransformation)
# Reset scale factor
self.scale_factor = 1
self.scene.clear()
self.scene.addItem(CustomQGraphicsPixmapItem(QPixmap.fromImage(self.image), self.c_width, self.c_height))
self.scene.update()
def rotate_image(self, rotation):
if self.original_image is not None:
print(rotation)
# Rotate the original and viewable image to ensure a constant workflow when modifying from canvas
matrix = QTransform()
matrix.translate(self.image.width() / 2, self.image.height() / 2)
matrix.rotate(rotation)
matrix.translate(-self.image.width() / 2, -self.image.height() / 2)
# Save transformed image
self.image = self.image.transformed(matrix)
original_matrix = QTransform()
original_matrix.translate(self.original_image.width() / 2, self.original_image.height() / 2)
original_matrix.rotate(rotation)
original_matrix.translate(-self.original_image.width() / 2, -self.original_image.height() / 2)
# Save transformed image
self.original_image = self.original_image.transformed(original_matrix)
self.scene.clear()
self.scene.addItem(CustomQGraphicsPixmapItem(QPixmap.fromImage(self.image), self.c_width, self.c_height))
self.scene.update()
def flip_image(self, direction):
if self.original_image is not None:
if direction == 0:
self.image = self.image.mirrored(True, False)
self.original_image = self.original_image.mirrored(True, False)
else:
self.image = self.image.mirrored(False, True)
self.original_image = self.original_image.mirrored(False, True)
self.scene.clear()
self.scene.addItem(CustomQGraphicsPixmapItem(QPixmap.fromImage(self.image), self.c_width, self.c_height))
self.scene.update()
示例9: CharacterIdentifier
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsScene [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsScene import update [as 别名]
#.........这里部分代码省略.........
# Create the main item and fill it with the characters
self.mainItem = CharacterIdentifierItem(QRectF(0, 0, sceneWidth, sceneHeight))
self.graphicsScene.addItem(self.mainItem)
if self.retrieveCharacters():
return True, sceneWidth, sceneHeight
else:
return False, 0, 0
def retrieveCharacters(self) -> bool:
""" Retrieve the character set
The character set is a list of files with the extention chr
in the directory that will be selected by the user in this method
Returns:
bool : True is the user selected a source directory
"""
# Retrieve the directory
fileDirectory = os.path.join(dataDir, "Chapter 3 - Distance Metrics")
dir = QFileDialog.getExistingDirectory(self, "Open Directory", fileDirectory, QFileDialog.ShowDirsOnly
| QFileDialog.DontResolveSymlinks)
if dir == "":
return False
# Retrieve all the files in the directory
charFiles = FileUtiles.GetAllFilesInFolder(dir, False, "chr")
# Show the files
for charFile in charFiles:
charFile = dir + '/' + charFile
self.mainItem.addCharacter(charFile)
self.graphicsScene.update()
return True
def pushButton_add_clicked(self):
""" Slot : Add a new character to the character set
Activate the CharIdentifierInput dialog and
add the new character to the main item
"""
charIdentifierInput = CharIdentifierInput(self)
if (charIdentifierInput.exec_()):
if charIdentifierInput.filename:
self.mainItem.addCharacter(charIdentifierInput.filename)
self.graphicsScene.update()
def pushButton_remove_clicked(self):
""" Slot : Remove the selected character
-# If there is a selected character ask the user if he wants
to remove the file also.
-# Call the removeItem of the main item
"""
if self.mainItem.selectedItemIndex == -1:
QMessageBox.critical(None, "Char identifier window", "No item was selected")
return
deleteFile = QMessageBox.question(
None, "Char Identifier Input", "Do You want to delete the character file ?",
QMessageBox.StandardButtons(QMessageBox.Yes | QMessageBox.No)) == QMessageBox.Yes
self.mainItem.removeItem(deleteFile)
示例10: Demo
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsScene [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsScene import update [as 别名]
class Demo(QGraphicsView):
def __init__(self, parent=None):
super(Demo, self).__init__(parent)
self.setFixedSize(WINDOW_WIDTH, WINDOW_HEIGHT)#self.size())
self.m_scene = QGraphicsScene()
self.m_lightSource = None
self.m_items = []
self.m_sprites = []
self.setScene(self.m_scene)
self.setup_scene()
self.timer = QTimer(self)
self.timer.timeout.connect(self.animate)
self.timer.setInterval(30)
self.timer.start()
self.setRenderHint(QPainter.Antialiasing)
self.setFrameStyle(QFrame.NoFrame)
self.mouse_down = False
self.setMouseTracking(True)
self.key_pressed = False
def setup_scene(self):
self.m_scene.setSceneRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)
self.centerOn(WINDOW_WIDTH/2, WINDOW_HEIGHT/2)
linear_grad = QLinearGradient(QPointF(-100, -100), QPointF(100, 100))
linear_grad.setColorAt(0, QColor(255, 255, 255))
linear_grad.setColorAt(1, QColor(192, 192, 255))
self.setBackgroundBrush(linear_grad)
link = Link(parent=self)
link.move_sprite([WINDOW_WIDTH/2, WINDOW_HEIGHT/2])
self.m_sprites.append(link)
#self.m_items.append(QGraphicsEllipseItem(WINDOW_WIDTH/2, WINDOW_HEIGHT/2, 1, 1))
#self.m_items[0].setPen(QPen(Qt.black, 1))
#self.m_items[0].setBrush(QBrush(Qt.black))
#self.m_scene.addItem(self.m_items[0])
def animate(self):
#for sprite in self.m_sprites:
# sprite.update()
self.m_scene.update()
def get_angle(self, event):
mouse_pos = Position(x=event.pos().x(), y=event.pos().y())
print('sprite : %s' % self.m_sprites[0].pos)
print('mouse : %s' % mouse_pos)
print('angle : %s' % (self.m_sprites[0].pos < mouse_pos))
return self.m_sprites[0].pos < mouse_pos
def keyPressEvent(self, event):
key = event.key()
if event.isAutoRepeat() or self.mouse_down:
return
self.key_pressed = True
if key == Qt.Key_Up or key == Qt.Key_W:
#print('Pressed Up or W?')
self.m_sprites[0].set_state('up')
if key == Qt.Key_Down or key == Qt.Key_S:
#print('Pressed Down or S?')
self.m_sprites[0].set_state('down')
if key == Qt.Key_Left or key == Qt.Key_A:
#print('Pressed Left or A?')
self.m_sprites[0].set_state('left')
if key == Qt.Key_Right or key == Qt.Key_D:
#print('Pressed Right or D?')
self.m_sprites[0].set_state('right')
if key == Qt.Key_Space:
#print('Pressed Space?')
if self.m_sprites[0].state == 'static':
self.m_sprites[0].set_state('blink')
else:
self.m_sprites[0].set_state('static')
if key == Qt.Key_Escape:
exit()
super(Demo, self).keyPressEvent(event)
def keyReleaseEvent(self, event):
if event.isAutoRepeat() or self.mouse_down:
return
key = event.key()
#print('Keyboard released?')
if self.m_sprites[0].state == 'left' and (key == Qt.Key_Left or key == Qt.Key_A):
self.key_pressed = False
self.m_sprites[0].state = 'left_static'
if self.m_sprites[0].state == 'right' and (key == Qt.Key_Right or key == Qt.Key_D):
self.key_pressed = False
self.m_sprites[0].state = 'right_static'
if self.m_sprites[0].state == 'down' and (key == Qt.Key_Down or key == Qt.Key_S):
self.key_pressed = False
self.m_sprites[0].state = 'static'
if self.m_sprites[0].state == 'up' and (key == Qt.Key_Up or key == Qt.Key_W):
self.key_pressed = False
self.m_sprites[0].state = 'up_static'
#.........这里部分代码省略.........
示例11: qfi_ADI
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsScene [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsScene import update [as 别名]
class qfi_ADI (QGraphicsView):
viewUpdate = pyqtSignal()
def __init__(self,winParent):
QGraphicsView.__init__(self)
self.winParent=winParent
self.viewUpdate.connect(self.update)
self.m_roll = 0
self.m_pitch = 0
self.m_faceDeltaX_new = 0
self.m_faceDeltaX_old = 0
self.m_faceDeltaY_new = 0
self.m_faceDeltaY_old = 0
self.m_originalHeight = 240
self.m_originalWidth = 240
self.m_originalPixPerDeg = 1.7
self.m_originalAdiCtr = QPointF(120,120)
self.m_backZ = -30
self.m_faceZ = -20
self.m_ringZ = -10
self.m_caseZ = 10
self.setStyleSheet("background: transparent; border: none");
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setInteractive(False)
self.setEnabled(False)
self.m_scene = QGraphicsScene(self)
self.setScene(self.m_scene)
self.init()
def init (self):
self.m_scaleX = self.width() / self.m_originalWidth
self.m_scaleY = self.height() / self.m_originalHeight
self.m_itemBack = QGraphicsSvgItem(":/qfi/images/adi/adi_back.svg")
self.m_itemBack.setCacheMode (QGraphicsItem.NoCache)
self.m_itemBack.setZValue( self.m_backZ )
self.m_itemBack.setTransform( QTransform.fromScale( self.m_scaleX, self.m_scaleY ), True )
self.m_itemBack.setTransformOriginPoint( self.m_originalAdiCtr )
self.m_scene.addItem (self.m_itemBack)
self.m_itemFace = QGraphicsSvgItem(":/qfi/images/adi/adi_face.svg")
self.m_itemFace.setCacheMode (QGraphicsItem.NoCache)
self.m_itemFace.setZValue( self.m_faceZ )
self.m_itemFace.setTransform( QTransform.fromScale( self.m_scaleX, self.m_scaleY ), True )
self.m_itemFace.setTransformOriginPoint( self.m_originalAdiCtr )
self.m_scene.addItem (self.m_itemFace)
self.m_itemRing = QGraphicsSvgItem(":/qfi/images/adi/adi_ring.svg")
self.m_itemRing.setCacheMode (QGraphicsItem.NoCache)
self.m_itemRing.setZValue( self.m_ringZ )
self.m_itemRing.setTransform( QTransform.fromScale( self.m_scaleX, self.m_scaleY ), True )
self.m_itemRing.setTransformOriginPoint( self.m_originalAdiCtr )
self.m_scene.addItem (self.m_itemRing)
self.m_itemCase = QGraphicsSvgItem(":/qfi/images/alt/alt_case.svg")
self.m_itemCase.setCacheMode (QGraphicsItem.NoCache)
self.m_itemCase.setZValue( self.m_caseZ )
self.m_itemCase.setTransform( QTransform.fromScale( self.m_scaleX, self.m_scaleY ), True )
self.m_itemCase.setTransformOriginPoint( self.m_originalAdiCtr )
self.m_scene.addItem (self.m_itemCase)
self.centerOn (self.width()/2, self.height()/2)
self.updateView()
def reinit(self):
if (self.m_scene):
self.m_scene.clear()
self.init()
def update(self):
self.updateView()
self.m_faceDeltaX_old = self.m_faceDeltaX_new
self.m_faceDeltaY_old = self.m_faceDeltaY_new
def setRoll (self, roll):
self.m_roll = roll
#.........这里部分代码省略.........