FrontPage / Python / PySide / QPainter-skelton.py

http://personals-index.com/index1552.html http://personals-index.com/index1553.html http://personals-index.com/index1554.html http://personals-index.com/index1555.html http://personals-index.com/index1556.html http://personals-index.com/index1557.html http://personals-index.com/index1558.html http://personals-index.com/index1559.html http://personals-index.com/index1560.html http://personals-index.com/index1561.html http://personals-index.com/index1562.html http://personals-index.com/index1563.html http://personals-index.com/index1564.html http://personals-index.com/index1565.html http://personals-index.com/index1566.html http://personals-index.com/index1567.html http://personals-index.com/index1568.html http://personals-index.com/index1569.html http://personals-index.com/index1570.html http://personals-index.com/index1571.html http://personals-index.com/index1572.html http://personals-index.com/index1573.html http://personals-index.com/index1574.html http://personals-index.com/index1575.html http://personals-index.com/index1576.html http://personals-index.com/index1577.html http://personals-index.com/index1578.html http://personals-index.com/index1579.html http://personals-index.com/index1580.html http://personals-index.com/index1581.html http://personals-index.com/index1582.html http://personals-index.com/index1583.html http://personals-index.com/index1584.html http://personals-index.com/index1585.html http://personals-index.com/index1586.html http://personals-index.com/index1587.html http://personals-index.com/index1588.html http://personals-index.com/index1589.html http://personals-index.com/index1590.html http://personals-index.com/index1591.html http://personals-index.com/index1592.html http://personals-index.com/index1593.html http://personals-index.com/index1594.html http://personals-index.com/index1595.html http://personals-index.com/index1596.html http://personals-index.com/index1597.html http://personals-index.com/index1598.html http://personals-index.com/index1599.html http://personals-index.com/index1500.html http://personals-index.com/index1501.html http://personals-index.com/index1502.html http://personals-index.com/index1503.html http://personals-index.com/index1504.html http://personals-index.com/index1505.html http://personals-index.com/index1506.html http://personals-index.com/index1507.html http://personals-index.com/index1508.html http://personals-index.com/index1509.html http://personals-index.com/index1510.html http://personals-index.com/index1511.html http://personals-index.com/index1512.html http://personals-index.com/index1513.html http://personals-index.com/index1514.html http://personals-index.com/index1515.html http://personals-index.com/index1516.html http://personals-index.com/index1517.html http://personals-index.com/index1518.html http://personals-index.com/index1518.html http://personals-index.com/index1518.html http://personals-index.com/index1521.html http://personals-index.com/index1522.html http://personals-index.com/index1523.html http://personals-index.com/index1524.html http://personals-index.com/index1525.html http://personals-index.com/index1526.html http://personals-index.com/index1527.html http://personals-index.com/index1528.html http://personals-index.com/index1529.html http://personals-index.com/index1530.html http://personals-index.com/index1531.html http://personals-index.com/index1532.html http://personals-index.com/index1533.html http://personals-index.com/index1534.html http://personals-index.com/index1535.html http://personals-index.com/index1536.html http://personals-index.com/index1537.html http://personals-index.com/index1538.html http://personals-index.com/index1539.html http://personals-index.com/index1540.html http://personals-index.com/index1541.html http://personals-index.com/index1542.html http://personals-index.com/index1543.html http://personals-index.com/index1544.html http://personals-index.com/index1545.html http://personals-index.com/index1546.html http://personals-index.com/index1547.html http://personals-index.com/index1548.html http://personals-index.com/index1549.html http://personals-index.com/index1552.html http://personals-index.com/index1551.html http://personals-index.com/index1550.html http://personals-index.com/index1652.html http://personals-index.com/index1653.html http://personals-index.com/index1654.html http://personals-index.com/index1655.html http://personals-index.com/index1656.html http://personals-index.com/index1657.html http://personals-index.com/index1658.html http://personals-index.com/index1659.html http://personals-index.com/index1660.html http://personals-index.com/index1661.html http://personals-index.com/index1662.html http://personals-index.com/index1663.html http://personals-index.com/index1664.html http://personals-index.com/index1665.html http://personals-index.com/index1666.html http://personals-index.com/index1667.html http://personals-index.com/index1668.html http://personals-index.com/index1669.html http://personals-index.com/index1670.html http://personals-index.com/index1671.html http://personals-index.com/index1672.html http://personals-index.com/index1673.html http://personals-index.com/index1674.html http://personals-index.com/index1675.html http://personals-index.com/index1676.html http://personals-index.com/index1677.html http://personals-index.com/index1678.html http://personals-index.com/index1679.html http://personals-index.com/index1680.html http://personals-index.com/index1681.html http://personals-index.com/index1682.html http://personals-index.com/index1683.html http://personals-index.com/index1684.html http://personals-index.com/index1685.html http://personals-index.com/index1686.html http://personals-index.com/index1687.html http://personals-index.com/index1688.html http://personals-index.com/index1689.html http://personals-index.com/index1690.html http://personals-index.com/index1691.html http://personals-index.com/index1692.html http://personals-index.com/index1693.html http://personals-index.com/index1694.html http://personals-index.com/index1695.html http://personals-index.com/index1696.html http://personals-index.com/index1697.html http://personals-index.com/index1698.html

FrontPage/Python/PySide

https://gist.github.com/peace098beat/db002e83002cc16c7981

#sh(){

       #! coding:utf-8
	}"""
	pyside-painter-skelton.py
	pysideでグラフィックを描画するためのスケルトンコード
	Created by 0160929 on 2015/11/19 7:28
	"""
	__version__ = '0.3'
	import sys
	import os
	# PySide系モジュール
	from PySide.QtGui import *
	from PySide.QtCore import *
	# 演算系モジュール
	import numpy as np
	# logを保存
	from datetime import datetime
	import csv
	def resetlog(file):
	    if os.path.exists(file):
	        os.remove(file)
	# ログファイルを削除
	resetlog('log.cav')
	def debuglog(s=None, data=None):
	    d = datetime.now().isoformat()
	    with open('log.csv', 'a') as f:
	        writer = csv.writer(f, lineterminator='\n')
	        if not s is None:
	            writer.writerow([d, s])
	            print d, s
	        if not data is None:
	            writer.writerow([d, data])
	            print d, data
	    return
	# 描画用PySideクラス
	class GameWindow(QWidget):
	    # -- 定数(画面サイズ) -----------------
	    SCREEN_HEIGHT = 500
	    SCREEN_WIDTH = 700
	    MARGIN_HEIGHT = 50
	    MARGIN_WIDTH = 50
	    STAGE_HEIGHT = SCREEN_HEIGHT - 2 * MARGIN_HEIGHT
	    STAGE_WIDTH = SCREEN_WIDTH - 2 * MARGIN_WIDTH
	    # -- 定数(タイマー ms) -------------------
	    INTERVAL_TIME = 1
	    # -- 定数(その他) ---------------------
	    def __init__(self, parent=None):
	        QWidget.__init__(self, parent)
	        self.resize(self.SCREEN_WIDTH, self.SCREEN_HEIGHT)
	        # -- 定数 ----------------------------
	        self.step = 0
	        # -- 画面バッファ --------------------
	        self.pixmap = QPixmap(self.size())
	        # -- 解析用オブジェクト --------------
	        # -- 操作用変数 ----------------------
	        # -- 初期画面の準備 ------------------
	        self.refreshPixmap()  # 画面バッファの初期化
	        painter = QPainter(self.pixmap)
	        self.drawGrid(painter)  # グリッドの表示
	        self.update()
	        # メインループの準備と開始
	        # -------------------------
	        if False:
	            self.timer = QTimer()
	            self.timer.timeout.connect(self.mainloop)
	            self.timer.start(self.INTERVAL_TIME)
	    # ************************************************************* #
	    # メインループ
	    # ************************************************************* #
	    def mainloop(self):
	        """
	        アニメーションのメインループ
	        アルゴリズムの時間更新等はここで行う
	        """
	        self.step += 1
	        # -- アルゴリズム処理-----------
	        # -- 描画 ----------------------
	        painter = QPainter(self.pixmap)
	        self.drawGrid(painter)
	        self.drawGeoPoints(painter)
	        # -- 画面更新 (AppのタイミングでpaintEventが呼ばれる) ---
	        self.update()
	    def paintEvent(self, *args, **kwargs):
	        # -- おまじない ---------------------------
	        painter = QStylePainter(self)  # QPainterを生成
	        painter.setRenderHint(QPainter.Antialiasing, True)  # アンチエイリアス
	        painter.drawPixmap(0, 0, self.pixmap)  # QPainterでバッファに準備したデータを描画
	        # 描画用QPenのデフォルト
	        pen_default = QPen()
	        pen_default.setColor(Qt.black)
	        pen_default.setWidth(2)
	        painter.setPen(pen_default)
	        brush_default = QBrush(Qt.red, Qt.NoBrush)
	        painter.setBrush(brush_default)
	        # -- アクセス用定数 -----------------------
	        x, y = 0, 1
	        # -- 車両の位置情報を取得 -----------------
	        pos = self.locateXY([self.STAGE_WIDTH / 2., self.STAGE_HEIGHT / 2.])
	        # -- 車両位置のプロット -------------------
	        pen = QPen(Qt.red, 5)
	        painter.setPen(pen)
	        painter.drawPoint(pos[x], pos[y])
	        # -- 車両位置に 円を描画 -----------------------------
	        pen = QPen(Qt.black, 1, Qt.DotLine, Qt.RoundCap, Qt.RoundJoin)
	        painter.setPen(pen)
	        painter.setBrush(QBrush(QColor(0, 255, 0, 175)))
	        circle_size = 50
	        painter.drawEllipse(pos[x] - circle_size / 2, pos[y] - circle_size / 2, circle_size, circle_size)
	        # -- 車両位置に四角を描画 -----------------------------
	        pen = QPen(Qt.black, 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)
	        painter.setPen(pen)
	        painter.setBrush(QBrush(QColor(0, 255, 0, 255), Qt.NoBrush))
	        painter.drawRect(pos[x] - circle_size / 2, pos[y] - circle_size / 2, circle_size, circle_size)
	        # qpoints = [QPoint(*lv) for lv in v]
	        # polygon = QPolygon(qpoints)
	        # painter.drawPolygon(polygon)
	        # -- 進行方向(速度ベクトル)を描画 ---------
	        pen = QPen(Qt.gray, 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)
	        painter.setPen(pen)
	        painter.drawLine(pos[x], pos[y], (pos[x] + 50), (pos[y] + 50))
	        # -- 棒グラフを表示 -------------------------
	        r = QRect(0, 0, 20, 30)
	        r.setHeight(200)
	        p0 = self.locateXY([20, 20], 'qp')
	        r.moveBottomLeft(p0)
	        painter.setBrush(QBrush(Qt.green, Qt.Dense4Pattern))
	        painter.drawRect(r)
	        # ************************************************************* #
	        # 描画系補助関数
	        # ************************************************************* #
	    def drawDebugLog(self, painter):
	        pass
	    def drawGrid(self, painter):
	        """ マップを表示する関数
	        """
	        # -- 定数 ---------------------------------------
	        Nx = 10
	        Ny = 10
	        Lx = self.STAGE_WIDTH
	        Ly = self.STAGE_HEIGHT
	        # -- 線の色と種類の選定 --------------------------
	        painter.setPen(QPen(Qt.black, 0.5))
	        painter.setBrush(QBrush(QColor(200, 200, 200, 150), Qt.Dense1Pattern))
	        # -- 横線を描画 ----------------------------------
	        for xi in range(Nx + 1):
	            p1 = self.locateXY([xi * Lx / Nx, 0], 'qp')
	            p2 = self.locateXY([xi * Lx / Nx, Ly], 'qp')
	            painter.drawLine(p1, p2)
	        # -- 縦線を描画 ----------------------------------
	        for yi in range(Ny + 1):
	            p1 = self.locateXY([0, yi * Ly / Ny], 'qp')
	            p2 = self.locateXY([Lx, yi * Ly / Ny], 'qp')
	            painter.drawLine(p1, p2)
	    def locateXY(self, pos, type='np'):
	        """
	        オブジェクトのローカル座標を、スクリーン上の座標へ変換
	        """
	        _pos = np.asarray(pos)
	        local_x, local_y = _pos.copy()
	        srn_x = local_x + self.MARGIN_WIDTH
	        srn_y = (self.SCREEN_HEIGHT - local_y - self.MARGIN_HEIGHT)
	        if type is 'np':
	            return np.asarray([srn_x, srn_y])
	        elif type is 'qp':
	            return QPoint(srn_x, srn_y)
	        elif type is 'qpF':
	            return QPointF(srn_x, srn_y)
	        elif type is 'list':
	            return [srn_x, srn_y]
	        return 'ERROR:: check type option'
	    def locateVec(self, vec, type='np'):
	        """
	        オブジェクトのベクトルを、スクリーン上の座標空間へ変換
	        (※yを反転させるだけ)
	        """
	        srn_vec = [vec[0], -1 * vec[1]]
	        if type is 'np':
	            return np.asarray(srn_vec)
	        elif type is 'qp':
	            return QPoint(*srn_vec)
	        elif type is 'qpF':
	            return QPointF(*srn_vec)
	        elif type is 'list':
	            return srn_vec
	        return 'ERROR:: check type option'
	    # ************************************************************* #
	    # その他Qt関連補助関数
	    # ************************************************************* #
	    def refreshPixmap(self):
	        """
	        画面バッファの初期化関数
	        """
	        # 画面バッファの初期化
	        self.pixmap = QPixmap(self.size())
	        # 画面を塗りつぶし (おまじない)
	        self.pixmap.fill(self, 0, 0)
	        self.pixmap.fill(Qt.white)
	        # ぺインターの生成 (おまじない)
	        painter = QPainter(self.pixmap)
	        # ぺインターによる初期化 (おまじない)
	        painter.initFrom(self)
	        pass
	    def sizeHint(self):
	        return QSize(self.SCREEN_WIDTH, self.SCREEN_HEIGHT)
	    def keyPressEvent(self, event):
	        e = event.key()
	        if e == Qt.Key_Up:
	            pass
	        elif e == Qt.Key_Down:
	            pass
	        elif e == Qt.Key_Left:
	            pass
	        elif e == Qt.Key_Right:
	            pass
	        elif e == Qt.Key_Plus:
	            pass
	        elif e == Qt.Key_Minus:
	            pass
	        elif e == Qt.Key_Q:
	            self.close()
	        else:
	            pass
	        print 'Presskey', e
	        self.update()
	# *******************************************************
	# main関数
	# *******************************************************
	def mainGUI():
	    app = QApplication(sys.argv)
	    win = GameWindow()
	    win.show()
	    sys.exit(app.exec_())
	if __name__ == "__main__":
	    mainGUI()
トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2015-11-20 (金) 08:43:47 (3320d)