ベクタパス作成ツール 1 version 1
:追加された部分
:削除された部分
(差分が大きい場合、文字単位では表示しません)
hogehoge
- ベクタパス作成ツール
```
import sys , os , pickle
from PyQt5.QtWidgets import QApplication, QWidget,QMessageBox
from PyQt5.QtCore import QPointF ,QRectF ,Qt
from PyQt5.QtGui import QPainterPath ,QPainter ,QPen
class makepath(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('hogetta')
self.setGeometry( 500, 50 , 500 , 500 )
self.setMouseTracking (True) # <----------------*****
self.plist=[]
self.plist.append( QPointF(10,250) ) # start point
self.plist.append( QPointF(100,100) ) #
self.plist.append( QPointF(300,200) ) #
self.plist.append( QPointF(490,250) ) #
self.a = QPointF(4,4) # Tolerance in detecting point
self.pselected = -1 # selected point
self.pmove = 0 # move flag
self.draw=0 # selected start point
self.isclosed=0 # closed flag
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect( self.custommenu )
self.show()
self custommenu(self):
menu=QMenu(self)
action = QAction('Add Point', self)
action.triggered.connect(self.addpoint)
menu.addAction(action)
action = QAction('Close Path', self)
action.triggered.connect(self.closepath)
menu.addAction(action)
action = QAction('Save Path', self)
action.triggered.connect(self.topickle)
menu.addAction(action)
menu.exec_(QCursor.pos ())
def addpoint(self,e): # ctrl + click
p = QPointF( (e.pos.x() - self.plist[-1].x())/3 , (e.posy() - self.plist[-1].y())/3 )
self.plist.append( self.plist[-1] + p )
self.plist.append( self.plist[-1] + p )
self.plist.append( self.plist[-1] + p ) # new start point
self.update()
def closepath(self):
reply = QMessageBox.question(self, 'Confirm'," Set End Point To Start Point",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
self.plist[-1] - self.plist[0]
self.isclosed=1 # set close flag
sel.update()
def openpath(self):
# message
pass
def topickle(self):
fname=os.path.join(os.path.dirname(__file__) , 'hoge')
with open( file, 'wb') as f :
pickle.dump( self.plist ,f)
def frompickle(self):
pass
def mouseMoveEvent(self ,e):
if self.pmove : # Dorag ing
self.plist[self.pselected] = e.pos()
self.update()
else: # non Dorag ing
self.pselected = -1
rc = QRectF ( e.pos() - self.a , e.pos() + self.a )
for i,l in enumerate( self.plist) :
if rc.contains( l ) :
self.selected = i
self.drawp = int( (i+1) / 3 ) # selected start point
self.update()
break
def mousePressEvent(self ,e):
if Qt.KeyboardModifiers == Qt.ControlModifier : # ctrl Qt.ShiftModifierQt.AltModifier
self.addpoint( e )
elif self.pselected != -1 :
self.pmove = True
def mouseReleaseEvent(self ,e):
self.pmove = False
self.pselected = -1
def paintEvent (self,e):
# 与えられたパラメータでパスを作成した後でパスをドローする→ mousemove に移したい
path = QPainterPath( self.plist[0] )
for i in range (1, len(self.plist) ,3):
path.cubicTo( self.plist[i] , self.plist[i+1] , self.plist[i+2] )
painter= QPainter()
painter.begin(self)
painter.setPen( QPen( Qt.black , 2, style=Qt.SolidLine ) )
painter.drawPath(path)
for i in range( 0 , len(self.plist)-3 , 3 ) # draw all start point
painter.drawRect( QRectF(QPointF(self.plist[i]-self.a) ,
QPointF(self.plist[i] + self.a) ))
# if p.selected != -1 :
# painter.drawRect( QRectF(QPointF(self.plist[p.selected]-self.a) ,
# QPointF(self.plist[p.selected] + self.a) ))
# draw selected points and sub-line
painter.setPen( QPen( Qt.red , 2, style=Qt.DotLine ) )
painter.drawRect( QRectF(QPointF(self.plist[self.draw]-self.a) ,
QPointF(self.plist[self.draw] + self.a) ))
if self.drawp==0:
painter.drawLine( self.plist[self.draw] , self.plist[self.draw+1] )
painter.drawRect( QRectF(QPointF(self.plist[self.draw+1]-self.a) ,
QPointF(self.plist[self.draw+1] + self.a) ))
painter.drawLine( self.plist[-1] , self.plist[-2] )
painter.drawRect( QRectF(QPointF(self.plist[self.draw-2]-self.a) ,
QPointF(self.plist[self.draw-2] + self.a) ))
elif self.drawp== len(self.plist)-1 :
painter.drawLine( self.plist[self.draw] , self.plist[self.draw-1] ) # sub
painter.drawRect( QRectF(QPointF(self.plist[self.draw-1]-self.a) ,
QPointF(self.plist[self.draw-1] + self.a) ))
else :
painter.drawLine( self.plist[self.draw] , self.plist[self.draw-1] )
painter.drawLine( self.plist[self.draw] , self.plist[self.draw+1] )
painter.drawRect( QRectF(QPointF(self.plist[self.draw+1]-self.a) ,
QPointF(self.plist[self.draw+1] + self.a) ))
painter.drawRect( QRectF(QPointF(self.plist[self.draw-1]-self.a) ,
QPointF(self.plist[self.draw-1] + self.a) ))
painter.end()
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = makepath()
sys.exit(app.exec_())
```
- ベクタパス作成ツール
import sys , os , pickle
from PyQt5.QtWidgets import QApplication, QWidget,QMessageBox
from PyQt5.QtCore import QPointF ,QRectF ,Qt
from PyQt5.QtGui import QPainterPath ,QPainter ,QPen
class makepath(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('hogetta')
self.setGeometry( 500, 50 , 500 , 500 )
self.setMouseTracking (True) # <----------------*****
self.plist=[]
self.plist.append( QPointF(10,250) ) # start point
self.plist.append( QPointF(100,100) ) #
self.plist.append( QPointF(300,200) ) #
self.plist.append( QPointF(490,250) ) #
self.a = QPointF(4,4) # Tolerance in detecting point
self.pselected = -1 # selected point
self.pmove = 0 # move flag
self.draw=0 # selected start point
self.isclosed=0 # closed flag
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect( self.custommenu )
self.show()
self custommenu(self):
menu=QMenu(self)
action = QAction('Add Point', self)
action.triggered.connect(self.addpoint)
menu.addAction(action)
action = QAction('Close Path', self)
action.triggered.connect(self.closepath)
menu.addAction(action)
action = QAction('Save Path', self)
action.triggered.connect(self.topickle)
menu.addAction(action)
menu.exec_(QCursor.pos ())
def addpoint(self,e): # ctrl + click
p = QPointF( (e.pos.x() - self.plist[-1].x())/3 , (e.posy() - self.plist[-1].y())/3 )
self.plist.append( self.plist[-1] + p )
self.plist.append( self.plist[-1] + p )
self.plist.append( self.plist[-1] + p ) # new start point
self.update()
def closepath(self):
reply = QMessageBox.question(self, 'Confirm'," Set End Point To Start Point",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
self.plist[-1] - self.plist[0]
self.isclosed=1 # set close flag
sel.update()
def openpath(self):
# message
pass
def topickle(self):
fname=os.path.join(os.path.dirname(__file__) , 'hoge')
with open( file, 'wb') as f :
pickle.dump( self.plist ,f)
def frompickle(self):
pass
def mouseMoveEvent(self ,e):
if self.pmove : # Dorag ing
self.plist[self.pselected] = e.pos()
self.update()
else: # non Dorag ing
self.pselected = -1
rc = QRectF ( e.pos() - self.a , e.pos() + self.a )
for i,l in enumerate( self.plist) :
if rc.contains( l ) :
self.selected = i
self.drawp = int( (i+1) / 3 ) # selected start point
self.update()
break
def mousePressEvent(self ,e):
if Qt.KeyboardModifiers == Qt.ControlModifier : # ctrl Qt.ShiftModifierQt.AltModifier
self.addpoint( e )
elif self.pselected != -1 :
self.pmove = True
def mouseReleaseEvent(self ,e):
self.pmove = False
self.pselected = -1
def paintEvent (self,e):
# 与えられたパラメータでパスを作成した後でパスをドローする→ mousemove に移したい
path = QPainterPath( self.plist[0] )
for i in range (1, len(self.plist) ,3):
path.cubicTo( self.plist[i] , self.plist[i+1] , self.plist[i+2] )
painter= QPainter()
painter.begin(self)
painter.setPen( QPen( Qt.black , 2, style=Qt.SolidLine ) )
painter.drawPath(path)
for i in range( 0 , len(self.plist)-3 , 3 ) # draw all start point
painter.drawRect( QRectF(QPointF(self.plist[i]-self.a) ,
QPointF(self.plist[i] + self.a) ))
# if p.selected != -1 :
# painter.drawRect( QRectF(QPointF(self.plist[p.selected]-self.a) ,
# QPointF(self.plist[p.selected] + self.a) ))
# draw selected points and sub-line
painter.setPen( QPen( Qt.red , 2, style=Qt.DotLine ) )
painter.drawRect( QRectF(QPointF(self.plist[self.draw]-self.a) ,
QPointF(self.plist[self.draw] + self.a) ))
if self.drawp==0:
painter.drawLine( self.plist[self.draw] , self.plist[self.draw+1] )
painter.drawRect( QRectF(QPointF(self.plist[self.draw+1]-self.a) ,
QPointF(self.plist[self.draw+1] + self.a) ))
painter.drawLine( self.plist[-1] , self.plist[-2] )
painter.drawRect( QRectF(QPointF(self.plist[self.draw-2]-self.a) ,
QPointF(self.plist[self.draw-2] + self.a) ))
elif self.drawp== len(self.plist)-1 :
painter.drawLine( self.plist[self.draw] , self.plist[self.draw-1] ) # sub
painter.drawRect( QRectF(QPointF(self.plist[self.draw-1]-self.a) ,
QPointF(self.plist[self.draw-1] + self.a) ))
else :
painter.drawLine( self.plist[self.draw] , self.plist[self.draw-1] )
painter.drawLine( self.plist[self.draw] , self.plist[self.draw+1] )
painter.drawRect( QRectF(QPointF(self.plist[self.draw+1]-self.a) ,
QPointF(self.plist[self.draw+1] + self.a) ))
painter.drawRect( QRectF(QPointF(self.plist[self.draw-1]-self.a) ,
QPointF(self.plist[self.draw-1] + self.a) ))
painter.end()
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = makepath()
sys.exit(app.exec_())