Skip to content

Instantly share code, notes, and snippets.

@mieki256
Last active November 14, 2016 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mieki256/7943de4fa112e9f5f03b773069706bd3 to your computer and use it in GitHub Desktop.
Save mieki256/7943de4fa112e9f5f03b773069706bd3 to your computer and use it in GitHub Desktop.
PySideを使って消しゴムツールを実現できそうかテスト
#!python
# -*- mode: python; Encoding: utf-8; coding: utf-8 -*-
# Last updated: <2016/11/14 22:41:03 +0900>
u"""
消しゴムツールのテスト.
QPixmapに対して働く消しゴムツールを作れそうか実験。
Author : mieki256
License : CC0 / Public Domain.
"""
import sys
from PySide.QtCore import * # NOQA
from PySide.QtGui import * # NOQA
class MyGView(QGraphicsView):
"""Custom GraphicsView."""
def __init__(self, *argv, **keywords):
"""init."""
super(MyGView, self).__init__(*argv, **keywords)
scene = QGraphicsScene(self)
self.setScene(scene)
scene.setBackgroundBrush(QBrush(self.get_bg_chip_image()))
self.canvas = QPixmap("./tmp_bg_night.png")
self.canvas_item = QGraphicsPixmapItem(self.canvas)
scene.addItem(self.canvas_item)
self.brush = QPixmap("./tmp_brush2.png")
self.draw_brush()
# self.erase_brush()
self.erase_brush_pixmap()
def draw_brush(self):
u"""ブラシ画像で描画."""
qp = QPainter()
qp.begin(self.canvas)
qp.setCompositionMode(QPainter.CompositionMode_SourceOver)
x, y = 0, 0
qp.drawPixmap(x, y, self.brush)
qp.end()
del qp
self.canvas_item.setPixmap(self.canvas)
def erase_brush_pixmap(self):
u"""ブラシ画像(QPixmap)を消しゴムとして使う."""
qp = QPainter()
qp.begin(self.canvas)
qp.setCompositionMode(QPainter.CompositionMode_DestinationOut)
x, y = self.brush.width() + 16, 0
qp.drawPixmap(x, y, self.brush)
qp.end()
del qp
self.canvas_item.setPixmap(self.canvas)
def erase_brush(self):
u"""ブラシ画像(QImage)を消しゴムとして使う."""
# QPixmap を Qimage に変換
bim = self.brush.toImage()
if bim.format() != QImage.Format_ARGB32:
bim = bim.convertToFormat(QImage.Format_ARGB32)
cim = self.canvas.toImage()
if cim.format() != QImage.Format_ARGB32:
cim = cim.convertToFormat(QImage.Format_ARGB32)
# QPainter を使って描画
qp = QPainter()
qp.begin(cim)
# 合成モードを指定して描画
qp.setCompositionMode(QPainter.CompositionMode_DestinationOut)
x, y = self.brush.width() + 16, 0
qp.drawImage(x, y, bim)
qp.end()
del qp
# キャンバス画像として再設定
self.canvas = QPixmap.fromImage(cim)
self.canvas_item.setPixmap(self.canvas)
def get_bg_chip_image(self):
u"""背景塗りつぶし用のパターンを作成して返す."""
im = QImage(16, 16, QImage.Format_ARGB32)
for y in range(16):
for x in range(16):
c = 248 - 48 * ((x / 8 + y / 8) % 2)
col = QColor(c, c, c, 255)
im.setPixel(x, y, col.rgba())
return im
class MyWidget(QWidget):
"""main widget."""
def __init__(self, *argv, **keywords):
"""init."""
super(MyWidget, self).__init__(*argv, **keywords)
l = QVBoxLayout()
self.gview = MyGView(self)
l.addWidget(self.gview)
self.setLayout(l)
def main():
"""Main."""
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment