2013年04月27日 星期六 17:29
我查找《python和科学计算》中的chaco_enable_stars.py
做了一个基于多边形的绘制程序。添加,删除,修改,移动
现在已经模仿实现了多边形的拖拽.
删除比较好做,准备放到后面。
现在想实现,添加和修改,
之前使用的是LineSegmentTool 来实现,
但是发现在metadata中操纵不是很方便,并且例子中是在Component中的用事件完成的。
本来希望将LineSegmentTool,整体移动过来。但是发现牵连太多,信号量也不好改。
但是LineSegmentTool的样式和效果非常不错,
不知道,各位,有不有好的思路,或者是code提供借鉴。
主要是后面要出差,很想短时间搞定这个小玩意,请大家帮忙看看。
已有代码如下:
Inversion.py
#-*-coding:utf-8 -*-
import gc
__author__ = 'Administrator'
import numpy as np
from traits.api import (HasTraits, Instance, List, Enum,
Float, Color, Tuple, Range)
from traitsui.api import Item, View, HGroup
###1###
from enable.api import ComponentEditor, Component
from chaco.tools.api import DragTool,LineSegmentTool
import sys
sys.path.append("..")
from Model.Polygon import Polygon
def convert_color(c):
if c.__class__.__name__ == "QColor":
return (c.red() / 255.0, c.green() / 255.0, c.blue() / 255.0)
else:
return (c[0] / 255.0, c[1] / 255.0, c[2] / 255.0)
class PolygonComponent(Component):
polygons = List(Polygon)
moving_x = Float
moving_y = Float
moving_polygon = Instance(Polygon)
temp_polygon = Instance(Polygon)
polygon_color = Color((255, 255, 255))
event_state = Enum("Normal", "Drawing", "Move", "Moving", "Removeing", "Exchange")
def __init__(self):
self.event_state = "Move"
self.polygons = []
p = Polygon()
p.tempPolygon()
self.polygons.append(p)
#print type(self.polygons[0].density)
def Find_Polygon(self, x, y):
from kiva.agg import points_in_polygon
for polygon in self.polygons[::-1]:
if polygon is not None:
if points_in_polygon((x, y), polygon.get_points()):
print "find it"
return polygon
print "find Nothing"
return None
def Move_right_down(self, event):
polygon = self.Find_Polygon(event.x, event.y)
if polygon is not None:
self.moving_x = event.x
self.moving_y = event.y
self.moving_polygon = polygon
self.event_state = "Moving"
self.request_redraw()
def Moving_mouse_move(self, event):
print self.moving_y, self.moving_x
self.moving_polygon.update_x_y(event.x - self.moving_x, event.y - self.moving_y)
self.moving_x = event.x
self.moving_y = event.y
self.request_redraw()
def Moving_right_up(self, event):
"""
移动操作结束,回到normal状态
"""
self.event_state = "Move"
def draw_Polygon(self, gc, polygon):
gc.save_state()
gc.set_antialias(True)
gc.set_stroke_color(convert_color(self.polygon_color))
gc.set_fill_color(convert_color(self.polygon_color))
gc.lines(polygon.get_points())
gc.draw_path()
gc.restore_state()
def _draw_overlay(self, gc, view_bounds=None, mode="normal"):
gc.clear((0, 0, 0, 1))
gc.save_state()
for polygon in self.polygons:
if polygon is not None:
self.draw_Polygon(gc, polygon)
gc.draw_path()
gc.restore_state()
def Drawing_left_down(self):
class PolygonDesign(HasTraits):
box = Instance(PolygonComponent)
view = View(
Item("box", editor=ComponentEditor(), show_label=False),
resizable=True,
width=600,
height=400,
title=u"图形绘制"
)
def __init__(self):
self.box = PolygonComponent()
if __name__ == "__main__":
demo = PolygonDesign()
demo.configure_traits()
polygon.py
#-*- coding:utf-8 -*-
__author__ = 'Administrator'
from traits.api import (HasTraits, Instance, List, Enum,
Float, Color, Tuple, Range)
from traitsui.api import Item, View, HGroup
###1###
from enable.api import ComponentEditor, Component
import numpy as np
from Point import Point
class Polygon(HasTraits):
density = Float
points = List(Point)
gravityValues = Float
def __init__(self, density=0, points=[], gravityValues=0):
self.density = density
self.points = points
self.gravityValues = gravityValues
def get_points(self):
p= [[i.x, i.y] for i in self.points]
p=np.array(p)
a=p[:,0]
b=p[:,1]
p=np.vstack((a,b)).T
return p
def update_x_y(self, x, y):
for i in self.points:
i.x += x
i.y += y
def tempPolygon(self):
self.density = 2.67
self.points = [Point(1, 2), Point(20, 6), Point(18,19), Point(44, 4), Point(1, 2)]
self.gravityValues = 2.5
if __name__ == "__main__":
p = Polygon(density=2.26, points=[Point(1, 1)], gravityValues=10.0)
p.tempPolygon()
p.get_points()
#p.configure_traits()
Zeuux © 2024
京ICP备05028076号