class AddItem(UsesQApplication): '''Tests for QGraphicsScene.add*''' qapplication = True def setUp(self): #Acquire resources super(AddItem, self).setUp() self.scene = QGraphicsScene() # While the scene does not inherits from QWidget, requires # an application to make the internals work. def tearDown(self): #Release resources del self.scene super(AddItem, self).tearDown() def testEllipse(self): #QGraphicsScene.addEllipse item = self.scene.addEllipse(100, 100, 100, 100) self.assertTrue(isinstance(item, QGraphicsEllipseItem)) def testLine(self): #QGraphicsScene.addLine item = self.scene.addLine(100, 100, 200, 200) self.assertTrue(isinstance(item, QGraphicsLineItem)) def testPath(self): #QGraphicsScene.addPath item = self.scene.addPath(QPainterPath()) self.assertTrue(isinstance(item, QGraphicsPathItem)) def testPixmap(self): #QGraphicsScene.addPixmap item = self.scene.addPixmap(QPixmap()) self.assertTrue(isinstance(item, QGraphicsPixmapItem)) def testPolygon(self): #QGraphicsScene.addPolygon points = [QPointF(0, 0), QPointF(100, 100), QPointF(0, 100)] item = self.scene.addPolygon(QPolygonF(points)) self.assertTrue(isinstance(item, QGraphicsPolygonItem)) def testRect(self): #QGraphicsScene.addRect item = self.scene.addRect(100, 100, 100, 100) self.assertTrue(isinstance(item, QGraphicsRectItem)) def testSimpleText(self): #QGraphicsScene.addSimpleText item = self.scene.addSimpleText('Monty Python 42') self.assertTrue(isinstance(item, QGraphicsSimpleTextItem)) def testText(self): #QGraphicsScene.addText item = self.scene.addText('Monty Python 42') self.assertTrue(isinstance(item, QGraphicsTextItem)) def testWidget(self): #QGraphicsScene.addWidget # XXX: printing some X11 error when using under PyQt4 item = self.scene.addWidget(QPushButton()) self.assertTrue(isinstance(item, QGraphicsProxyWidget))
class DrawableCameraView(QGraphicsView): overlay_changed = Signal() def __init__(self, camera=None, scene=None): super(DrawableCameraView, self).__init__() if scene is None: self.scene = QGraphicsScene() else: self.scene = scene self.setScene(self.scene) self.camera = camera self.pixmapitem = None # Overlay variables self.overlay_visible = False self.overlay_images = None def set_overlays(self, filenames): if not self.overlay_images: self.overlay_filenames = filenames self.overlay_images = [None] * len(filenames) self.overlay_index = len(filenames) # Start at live video else: old_fnames = self.overlay_filenames old_images = self.overlay_images new_fnames = filenames new_images = [None] * len(new_fnames) for old_fname in old_fnames: i = new_fnames.index(old_fname) new_images[i] = old_images[i] self.overlay_filenames = new_fnames self.overlay_images = new_images self.overlay_index = len(filenames) def hide_overlay(self): if self.overlay_index < len(self.overlay_images): self.overlay_images[self.overlay_index].hide() self.overlay_visible = False def show_overlay(self): if self.overlay_index < len(self.overlay_images): self.overlay_images[self.overlay_index].show() self.overlay_visible = True def keyPressEvent(self, event): key = event.key() if self.overlay_visible and self.overlay_images is not None: # We have overlays active and visible if key == Qt.Key_Left: self.overlay_index -= 1 if self.overlay_index < 0: self.overlay_index = 0 # No wraparound else: self.overlay_changed.emit() if self.overlay_images[self.overlay_index] is None: self._add_overlay_image() self.overlay_images[self.overlay_index].show() if self.overlay_index + 1 < len(self.overlay_images): self.overlay_images[self.overlay_index + 1].hide() elif key == Qt.Key_Right: self.overlay_index += 1 if self.overlay_index > len(self.overlay_images): self.overlay_index = len(self.overlay_images) else: if self.overlay_index < len(self.overlay_images): self.overlay_images[self.overlay_index].show() self.overlay_changed.emit() self.overlay_images[self.overlay_index - 1].hide() else: # Pass event through to base class QGraphicsView.keyPressEvent(self, event) def _add_overlay_image(self): fname = self.overlay_filenames[self.overlay_index] self.overlay_images[self.overlay_index] = self.scene.addPixmap( QPixmap(fname)) self.overlay_images[self.overlay_index].setZValue(10000000) def start_video(self, framerate=None): timer = QTimer() timer.timeout.connect(self._wait_for_frame) self.camera.start_live_video(framerate) timer.start(int(1000 / self.camera.framerate)) self.timer = timer self.centerOn(self.camera.width / 2, self.camera.height / 2) def stop_video(self): self.camera.stop_live_video() self.timer.stop() def _wait_for_frame(self): if self.camera.wait_for_frame(0): data = self.camera.get_image_buffer() bpl = self.camera.bytes_per_line format = QImage.Format_RGB32 image = QImage(data, self.camera.width, self.camera.height, bpl, format) if self.pixmapitem is None: self.pixmapitem = self.scene.addPixmap( QPixmap.fromImage(image)) else: self.pixmapitem.setPixmap(QPixmap.fromImage(image))
class DrawableCameraView(QGraphicsView): overlay_changed = Signal() def __init__(self, camera=None, scene=None): super(DrawableCameraView, self).__init__() if scene is None: self.scene = QGraphicsScene() else: self.scene = scene self.setScene(self.scene) self.camera = camera self.pixmapitem = None # Overlay variables self.overlay_visible = False self.overlay_images = None def set_overlays(self, filenames): if not self.overlay_images: self.overlay_filenames = filenames self.overlay_images = [None] * len(filenames) self.overlay_index = len(filenames) # Start at live video else: old_fnames = self.overlay_filenames old_images = self.overlay_images new_fnames = filenames new_images = [None] * len(new_fnames) for old_fname in old_fnames: i = new_fnames.index(old_fname) new_images[i] = old_images[i] self.overlay_filenames = new_fnames self.overlay_images = new_images self.overlay_index = len(filenames) def hide_overlay(self): if self.overlay_index < len(self.overlay_images): self.overlay_images[self.overlay_index].hide() self.overlay_visible = False def show_overlay(self): if self.overlay_index < len(self.overlay_images): self.overlay_images[self.overlay_index].show() self.overlay_visible = True def keyPressEvent(self, event): key = event.key() if self.overlay_visible and self.overlay_images is not None: # We have overlays active and visible if key == Qt.Key_Left: self.overlay_index -= 1 if self.overlay_index < 0: self.overlay_index = 0 # No wraparound else: self.overlay_changed.emit() if self.overlay_images[self.overlay_index] is None: self._add_overlay_image() self.overlay_images[self.overlay_index].show() if self.overlay_index+1 < len(self.overlay_images): self.overlay_images[self.overlay_index+1].hide() elif key == Qt.Key_Right: self.overlay_index += 1 if self.overlay_index > len(self.overlay_images): self.overlay_index = len(self.overlay_images) else: if self.overlay_index < len(self.overlay_images): self.overlay_images[self.overlay_index].show() self.overlay_changed.emit() self.overlay_images[self.overlay_index-1].hide() else: # Pass event through to base class QGraphicsView.keyPressEvent(self, event) def _add_overlay_image(self): fname = self.overlay_filenames[self.overlay_index] self.overlay_images[self.overlay_index] = self.scene.addPixmap(QPixmap(fname)) self.overlay_images[self.overlay_index].setZValue(10000000) def start_video(self, framerate=None): timer = QTimer() timer.timeout.connect(self._wait_for_frame) self.camera.start_live_video(framerate) timer.start(int(1000/self.camera.framerate)) self.timer = timer self.centerOn(self.camera.width/2, self.camera.height/2) def stop_video(self): self.camera.stop_live_video() self.timer.stop() def _wait_for_frame(self): if self.camera.wait_for_frame(0): data = self.camera.image_buffer() bpl = self.camera.bytes_per_line format = QImage.Format_RGB32 image = QImage(data, self.camera.width, self.camera.height, bpl, format) if self.pixmapitem is None: self.pixmapitem = self.scene.addPixmap(QPixmap.fromImage(image)) else: self.pixmapitem.setPixmap(QPixmap.fromImage(image))
from PySide.QtGui import (QApplication, QGraphicsView, QGraphicsScene, QMainWindow, QPixmap, QPainter) from PySide.QtCore import Qt import sys if __name__ == '__main__': app = QApplication(sys.argv) scene = QGraphicsScene() view = QGraphicsView(scene) view.scale(15, 15) pix = QPixmap(200, 50) pix.fill(Qt.white) p = QPainter(pix) p.setPen(Qt.black) #p.setBrush(Qt.red) for i in range(1, 10): p.drawEllipse(0, 0, i, i) p.translate(i+5, 0) p.end() pixItem = scene.addPixmap(pix) win = QMainWindow() win.setCentralWidget(view) win.resize(700, 200) win.show() sys.exit(app.exec_())
class AddItem(UsesQApplication): """Tests for QGraphicsScene.add*""" qapplication = True def setUp(self): # Acquire resources super(AddItem, self).setUp() self.scene = QGraphicsScene() # While the scene does not inherits from QWidget, requires # an application to make the internals work. def tearDown(self): # Release resources del self.scene super(AddItem, self).tearDown() def testEllipse(self): # QGraphicsScene.addEllipse item = self.scene.addEllipse(100, 100, 100, 100) self.assertTrue(isinstance(item, QGraphicsEllipseItem)) def testLine(self): # QGraphicsScene.addLine item = self.scene.addLine(100, 100, 200, 200) self.assertTrue(isinstance(item, QGraphicsLineItem)) def testPath(self): # QGraphicsScene.addPath item = self.scene.addPath(QPainterPath()) self.assertTrue(isinstance(item, QGraphicsPathItem)) def testPixmap(self): # QGraphicsScene.addPixmap item = self.scene.addPixmap(QPixmap()) self.assertTrue(isinstance(item, QGraphicsPixmapItem)) def testPolygon(self): # QGraphicsScene.addPolygon points = [QPointF(0, 0), QPointF(100, 100), QPointF(0, 100)] item = self.scene.addPolygon(QPolygonF(points)) self.assertTrue(isinstance(item, QGraphicsPolygonItem)) def testRect(self): # QGraphicsScene.addRect item = self.scene.addRect(100, 100, 100, 100) self.assertTrue(isinstance(item, QGraphicsRectItem)) def testSimpleText(self): # QGraphicsScene.addSimpleText item = self.scene.addSimpleText("Monty Python 42") self.assertTrue(isinstance(item, QGraphicsSimpleTextItem)) def testText(self): # QGraphicsScene.addText item = self.scene.addText("Monty Python 42") self.assertTrue(isinstance(item, QGraphicsTextItem)) def testWidget(self): # QGraphicsScene.addWidget # XXX: printing some X11 error when using under PyQt4 item = self.scene.addWidget(QPushButton()) self.assertTrue(isinstance(item, QGraphicsProxyWidget))