PyQt でベジェ曲線を描画してマウスでグリグリする version 6
:追加された部分
:削除された部分
(差分が大きい場合、文字単位では表示しません)
下書きだがやベジェ描画の実験
hoge一から学ぶベジェ曲線
https://postd.cc/bezier-curves/
このサイト素晴らしい、動画を埋め込むのでは無い、動くページの素晴らしさだな
ペイントの基本
https://qiita.com/kenasman/items/87c12f3a8b63f5948153
・ウイジェットを一つ作って(500-500)そこに直接書く
・まずキューブを一つ書く、端点は四つ、これをマウスでグリグリする
・端点を中心に誤差aでマウスで選択してドラッグできるようにする
通常は黒い■、マウスオーバーで赤い■にしてマウスプレスで選択、ドラッグ
ドラッグ中はリアルタイムで描画
```
# =====================
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import QPointF ,QRectF
from PyQt5.QtGui import QPainterPath ,QPainter
class App(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('hogetta')
self.setGeometry( 500, 50 , 500 , 500 )
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
# =======================
def init
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(2,2)
self.pselected = -1 # selectedpoint
self.pmove = 0 # should move
self.draw()
# ========================
def keyPressEvent( self ,e):
if e. # not shift return
setPen(black);
for l in self.plist :
DrawRect(l._sub_(a) , l._add_( a) ) # <---- ???
# ========================
def mouseMoveEvent(self ,e):
setPen(red);
if self.pmove : # Dorag ing
self.plist[self.selected] = e.pos()
self.draw()
else: # non Dorag ing
for i, l in enumerate( self.plist ) :
p1 = e.pos() -= self.a # e.pos()._sub_( self.a )
p2 = e.pos()._add_( self.a )
if e.pos().x() > p1.x() and e.pos().x() < p2.x() and e.pos().y() > p1.y() and e.pos().y() < p2.y() :
self.pselected = i
paint = QPainter(self)
paint.drawRect( QRectF( p1 , p2) ) # <---- ???
else :
self.pselected = -1
self.update()
# ========================
def mousePressEvent(self ,e):
if self.pselected != -1 :
self.pmove = True
def mouseReleaseEvent(self ,e):
self.pmove = False
self.pselected = -1
# ========================
def draw(self):
# パスを設定した後でパスをドローする
# path = QPainterPath()
# path.moveTo( plist[0] );
path = QPainterPath( plist[0] )
path.cubicTo( plist[1] , plist[2] , plist[3] )
painter= QPainter(self) ;
painter.setPen(QPen(QColor(79, 106, 25), 1, Qt::SolidLine,
Qt::FlatCap, Qt::MiterJoin))
painter.drawPath(path)
# ===================
```
一から学ぶベジェ曲線
https://postd.cc/bezier-curves/
このサイト素晴らしい、動画を埋め込むのでは無い、動くページの素晴らしさだな
ペイントの基本
https://qiita.com/kenasman/items/87c12f3a8b63f5948153
・ウイジェットを一つ作って(500-500)そこに直接書く
・まずキューブを一つ書く、端点は四つ、これをマウスでグリグリする
・端点を中心に誤差aでマウスで選択してドラッグできるようにする
通常は黒い■、マウスオーバーで赤い■にしてマウスプレスで選択、ドラッグ
ドラッグ中はリアルタイムで描画
# =====================
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import QPointF ,QRectF
from PyQt5.QtGui import QPainterPath ,QPainter
class App(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('hogetta')
self.setGeometry( 500, 50 , 500 , 500 )
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
# =======================
def init
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(2,2)
self.pselected = -1 # selectedpoint
self.pmove = 0 # should move
self.draw()
# ========================
def keyPressEvent( self ,e):
if e. # not shift return
setPen(black);
for l in self.plist :
DrawRect(l._sub_(a) , l._add_( a) ) # <---- ???
# ========================
def mouseMoveEvent(self ,e):
setPen(red);
if self.pmove : # Dorag ing
self.plist[self.selected] = e.pos()
self.draw()
else: # non Dorag ing
for i, l in enumerate( self.plist ) :
p1 = e.pos() -= self.a # e.pos()._sub_( self.a )
p2 = e.pos()._add_( self.a )
if e.pos().x() > p1.x() and e.pos().x() < p2.x() and e.pos().y() > p1.y() and e.pos().y() < p2.y() :
self.pselected = i
paint = QPainter(self)
paint.drawRect( QRectF( p1 , p2) ) # <---- ???
else :
self.pselected = -1
self.update()
# ========================
def mousePressEvent(self ,e):
if self.pselected != -1 :
self.pmove = True
def mouseReleaseEvent(self ,e):
self.pmove = False
self.pselected = -1
# ========================
def draw(self):
# パスを設定した後でパスをドローする
# path = QPainterPath()
# path.moveTo( plist[0] );
path = QPainterPath( plist[0] )
path.cubicTo( plist[1] , plist[2] , plist[3] )
painter= QPainter(self) ;
painter.setPen(QPen(QColor(79, 106, 25), 1, Qt::SolidLine,
Qt::FlatCap, Qt::MiterJoin))
painter.drawPath(path)
# ===================