ベクタパス作成ツール 3 version 7

2019/07/18 16:52 by yamasyuh68
  :追加された部分   :削除された部分
(差分が大きい場合、文字単位では表示しません)
hogeベクタパス作成ツール 2
hoge# オブジェクトのアイテムを確認してみる
```
def init
        self.path=None      # globalize
def showelem(self):
    for i in range(self.path.elementCount() ):
        print('format: {} x: {} y: {}'.format(
            self.elementAt(i).type , self.elementAt(i).x , self.elementAt(i).y ) )

    def custommenu(self):

        menu.addSeparator()

        action = QAction('Point Info', self)
        action.triggered.connect(self.showelem)
        menu.addAction(action)
```

# 次は保存
```
# QDataStream Class Reference [QtCore module
# QIODevice Class Reference [QtCore module]
# QDataStream Class Reference [QtCore

    def pathsave(self):
        fname=os.path.join(os.path.dirname(__file__) , 'qtsave')
        f = QFile(fname)
        f.open( QIODevice.WriteOnly ):
        ds = QDataStream(f)
        ds.__lshift__(self.path)
        f.close() 
    def pathload(self):
        fname=os.path.join(os.path.dirname(__file__) , 'qtsave')
        f = QFile(fname)
        f.open( QIODevice.ReadOnly):
        ds = QDataStream(f)
        ds.__rshift__(self.path)
        f.close() 
```

- c++ 参考

```
QPainterPath path;
// do sth
{
  QFile file("file.dat");
  file.open(QIODevice::WriteOnly);
  QDataStream out(&file);   // we will serialize the data into the file
  out << path;   // serialize a path, fortunately there is apriopriate functionality
}  #  left shift

QPainterPath path;
{
  QFile file("file.dat");
  file.open(QIODevice::ReadOnly);
  QDataStream in(&file);   // we will deserialize the data from the file
  in >> path;
}
//do sth
```



# サブクラス化

まずこのコードで代わりになるか確認
含まれているだけ--->全体赤、全ポイント赤表示
ポイントの上--->ポイントのみ青、関係線とポイント描画
ポイントクリック---->ポイントのみ青塗り、関係線とポイント描画

```
class mypath(QPainterPath):

def __init__(self,parent,p):
    super().__init__(self,parent,p)
    self.pointrect=[]
    self.a = QPointF(4,4) # Tolerance in detecting point


def makepointrect(self): # ポイント変更の都度呼びたいけど
    self.pointrect.clear()
    for i in range(self.elementCount() ):
        if not self.elementAt(i).__eq__(QPainterPath.CurveToDataElement):
            self.pointrect.append( [i ,
                QRectF(QPointF( path.elementAt(i).x -self.a ) , QPointF( path.elementAt(i).y + self.a) ) )] )

def iscontains_m(self,  p ) : QPointF p
    if not self.iscontains(p):
        return -2 #                   not contain
    else : 
        for l in self.pointrect :
            if l[1].contains(p) :
                return l[0] #        hit point
        return -1 #                  contain but point

def getpointrect(self):
    return self.pointrect
def getselectedpointrect(self, i) : # int i
```

---
--> [PyQt でベクターオブジェクト作成ツールを作る # もくじ ](https://mimemo.io/m/3Rx1XoR367le95E)

      

オブジェクトのアイテムを確認してみる

def init
        self.path=None      # globalize
def showelem(self):
    for i in range(self.path.elementCount() ):
        print('format: {} x: {} y: {}'.format(
            self.elementAt(i).type , self.elementAt(i).x , self.elementAt(i).y ) )

    def custommenu(self):

        menu.addSeparator()

        action = QAction('Point Info', self)
        action.triggered.connect(self.showelem)
        menu.addAction(action)

次は保存

# QDataStream Class Reference [QtCore module
# QIODevice Class Reference [QtCore module]
# QDataStream Class Reference [QtCore

    def pathsave(self):
        fname=os.path.join(os.path.dirname(__file__) , 'qtsave')
        f = QFile(fname)
        f.open( QIODevice.WriteOnly ):
        ds = QDataStream(f)
        ds.__lshift__(self.path)
        f.close() 
    def pathload(self):
        fname=os.path.join(os.path.dirname(__file__) , 'qtsave')
        f = QFile(fname)
        f.open( QIODevice.ReadOnly):
        ds = QDataStream(f)
        ds.__rshift__(self.path)
        f.close() 
  • c++ 参考
QPainterPath path;
// do sth
{
  QFile file("file.dat");
  file.open(QIODevice::WriteOnly);
  QDataStream out(&file);   // we will serialize the data into the file
  out << path;   // serialize a path, fortunately there is apriopriate functionality
}  #  left shift

QPainterPath path;
{
  QFile file("file.dat");
  file.open(QIODevice::ReadOnly);
  QDataStream in(&file);   // we will deserialize the data from the file
  in >> path;
}
//do sth

サブクラス化

まずこのコードで代わりになるか確認
含まれているだけ--->全体赤、全ポイント赤表示
ポイントの上--->ポイントのみ青、関係線とポイント描画
ポイントクリック---->ポイントのみ青塗り、関係線とポイント描画

class mypath(QPainterPath):

def __init__(self,parent,p):
    super().__init__(self,parent,p)
    self.pointrect=[]
    self.a = QPointF(4,4) # Tolerance in detecting point


def makepointrect(self): # ポイント変更の都度呼びたいけど
    self.pointrect.clear()
    for i in range(self.elementCount() ):
        if not self.elementAt(i).__eq__(QPainterPath.CurveToDataElement):
            self.pointrect.append( [i ,
                QRectF(QPointF( path.elementAt(i).x -self.a ) , QPointF( path.elementAt(i).y + self.a) ) )] )

def iscontains_m(self,  p ) : QPointF p
    if not self.iscontains(p):
        return -2 #                   not contain
    else : 
        for l in self.pointrect :
            if l[1].contains(p) :
                return l[0] #        hit point
        return -1 #                  contain but point

def getpointrect(self):
    return self.pointrect
def getselectedpointrect(self, i) : # int i

--> PyQt でベクターオブジェクト作成ツールを作る # もくじ