如何在 Wxpython PseudoDC 中 return IDs within a wx.Rect
How to return IDs within a wx.Rect in Wxpython PseudoDC
我正在为我在 Wxpython 中开发的程序制作一个 "Box Select" 工具。我正在使用 PseudoDC class 进行绘图。
用户应该能够通过 ID 在节点图上为 select 绘制的节点对象绘制一个框,但我无法找到获取框内 ID 的好方法 select离子。
到目前为止,我已经得出以下结论:
def OnLeftUp(self, event):
...
# This is in the mouse event method which calls the *BoxSelectHitTest* method below.
self._selectednodes = self.BoxSelectHitTest(
wx.Point(self._bboxRect[2]/2,
self._bboxRect[3]/2)
)
...
def BoxSelectHitTest(self, pt):
# self._bboxRect is the wx.Rect of the Box Select
average = (self._bboxRect[3] + self._bboxRect[2])/2
idxs = self._pdc.FindObjects(pt[0], pt[1], int(average))
hits = [
idx
for idx in idxs
if idx in self._nodes
]
# Return the node objects from the IDs
if hits != []:
nodes = []
for Id in hits:
nodes.append(self._nodes[Id])
return nodes
else:
return []
这显然不是一个真正的盒子select。它更像是一个糟糕版本的圆select。 (平均半径只是我的尝试"work"。)
我在 PseudoDC 中找不到 可以 return 给定 wx.Rect 中对象 ID 的方法。 是否有执行此操作的方法或应如何正确实施?
谢谢。
我通过查看 wx.Rect 上的文档弄明白了,所以我想我会 post 在这里。
使用 wx.Rect.Intersects
它检查 bboxrect 是否与每个节点的 rect 和 returns 它们相交:
def BoxSelectHitTest(self, bboxrect):
nodehits = []
for node in self._nodes.values():
if bboxrect.Intersects(node.GetRect()) == True:
nodehits.append(node)
if nodehits != []:
return nodehits
else:
# Make sure we deselect everything
for node in self._selectednodes:
node.SetSelected(False)
node.Draw(self._pdc)
self._selectednodes = []
return []
我正在为我在 Wxpython 中开发的程序制作一个 "Box Select" 工具。我正在使用 PseudoDC class 进行绘图。
用户应该能够通过 ID 在节点图上为 select 绘制的节点对象绘制一个框,但我无法找到获取框内 ID 的好方法 select离子。
到目前为止,我已经得出以下结论:
def OnLeftUp(self, event):
...
# This is in the mouse event method which calls the *BoxSelectHitTest* method below.
self._selectednodes = self.BoxSelectHitTest(
wx.Point(self._bboxRect[2]/2,
self._bboxRect[3]/2)
)
...
def BoxSelectHitTest(self, pt):
# self._bboxRect is the wx.Rect of the Box Select
average = (self._bboxRect[3] + self._bboxRect[2])/2
idxs = self._pdc.FindObjects(pt[0], pt[1], int(average))
hits = [
idx
for idx in idxs
if idx in self._nodes
]
# Return the node objects from the IDs
if hits != []:
nodes = []
for Id in hits:
nodes.append(self._nodes[Id])
return nodes
else:
return []
这显然不是一个真正的盒子select。它更像是一个糟糕版本的圆select。 (平均半径只是我的尝试"work"。)
我在 PseudoDC 中找不到 可以 return 给定 wx.Rect 中对象 ID 的方法。 是否有执行此操作的方法或应如何正确实施?
谢谢。
我通过查看 wx.Rect 上的文档弄明白了,所以我想我会 post 在这里。
使用 wx.Rect.Intersects
它检查 bboxrect 是否与每个节点的 rect 和 returns 它们相交:
def BoxSelectHitTest(self, bboxrect):
nodehits = []
for node in self._nodes.values():
if bboxrect.Intersects(node.GetRect()) == True:
nodehits.append(node)
if nodehits != []:
return nodehits
else:
# Make sure we deselect everything
for node in self._selectednodes:
node.SetSelected(False)
node.Draw(self._pdc)
self._selectednodes = []
return []