Adapt read/write functions
This commit is contained in:
parent
e68c671fc5
commit
92f9561e81
49
painter.py
49
painter.py
@ -23,7 +23,7 @@ class Painter(Widget):
|
|||||||
# list of selected particles
|
# list of selected particles
|
||||||
self.selected=[]
|
self.selected=[]
|
||||||
# complement
|
# complement
|
||||||
self.unselected=self.particles
|
self.unselected=[]
|
||||||
|
|
||||||
# relative position of mouse when moving
|
# relative position of mouse when moving
|
||||||
self.offset=Point(0,0)
|
self.offset=Point(0,0)
|
||||||
@ -250,15 +250,15 @@ class Painter(Widget):
|
|||||||
return self.adjust_move_element(newdelta,element,recursion_depth+1)
|
return self.adjust_move_element(newdelta,element,recursion_depth+1)
|
||||||
|
|
||||||
|
|
||||||
# TODO adapt
|
|
||||||
# write configuration to file
|
# write configuration to file
|
||||||
def write(self,file):
|
def write(self,file):
|
||||||
ff=open(file,"w")
|
ff=open(file,"w")
|
||||||
for particle in self.particles:
|
for particle in self.particles:
|
||||||
ff.write("{:05.2f},{:05.2f};{:3.1f},{:3.1f},{:3.1f}\n".format(particle.pos.x,particle.pos.y,particle.color[0],particle.color[1],particle.color[2]))
|
if type(particle)==Cross:
|
||||||
|
ff.write("{:d};".format(CROSS_INDEX))
|
||||||
|
ff.write("{:05.2f},{:05.2f};{:3.1f},{:3.1f},{:3.1f}\n".format(particle.squares[0].pos.x,particle.squares[0].pos.y,particle.color[0],particle.color[1],particle.color[2]))
|
||||||
ff.close()
|
ff.close()
|
||||||
|
|
||||||
# TODO adapt
|
|
||||||
# read configuration from file
|
# read configuration from file
|
||||||
def read(self,file):
|
def read(self,file):
|
||||||
self.reset()
|
self.reset()
|
||||||
@ -292,40 +292,55 @@ class Painter(Widget):
|
|||||||
|
|
||||||
entries=line.split(";")
|
entries=line.split(";")
|
||||||
# skip line if improperly formatted
|
# skip line if improperly formatted
|
||||||
if len(entries)>2:
|
if len(entries)>3:
|
||||||
print("warning: ignoring line "+str(i)+" in file '"+file+"': more than two ';' spearated entries in '"+line+"'",file=sys.stderr)
|
print("warning: ignoring line "+str(i)+" in file '"+file+"': more than three ';' spearated entries in '"+line+"'",file=sys.stderr)
|
||||||
|
if len(entries)<2:
|
||||||
|
print("warning: ignoring line "+str(i)+" in file '"+file+"': fewer than two ';' spearated entries in '"+line+"'",file=sys.stderr)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# position
|
# position
|
||||||
pos_str=entries[0].split(",")
|
pos_str=entries[1].split(",")
|
||||||
# skip line if improperly formatted
|
# skip line if improperly formatted
|
||||||
if len(pos_str)!=2:
|
if len(pos_str)!=2:
|
||||||
print("warning: ignoring line "+str(i)+" in file '"+file+"': position '"+entries[0]+"' does not have two components",file=sys.stderr)
|
print("warning: ignoring line "+str(i)+" in file '"+file+"': position '"+entries[1]+"' does not have two components",file=sys.stderr)
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
pos=Point(float(pos_str[0]),float(pos_str[1]))
|
pos=Point(float(pos_str[0]),float(pos_str[1]))
|
||||||
except:
|
except:
|
||||||
print("warning: ignoring line "+str(i)+" in file '"+file+"': position '"+entries[0]+"' cannot be read",file=sys.stderr)
|
print("warning: ignoring line "+str(i)+" in file '"+file+"': position '"+entries[1]+"' cannot be read",file=sys.stderr)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
||||||
# color
|
# color
|
||||||
color=(0,0,1)
|
color=(0,0,1)
|
||||||
if len(entries)==2:
|
if len(entries)==3:
|
||||||
color_str=entries[1].split(",")
|
color_str=entries[2].split(",")
|
||||||
# skip line if improperly formatted
|
# skip line if improperly formatted
|
||||||
if len(color_str)!=3:
|
if len(color_str)!=3:
|
||||||
print("warning: ignoring line "+str(i)+" in file '"+file+"': color '"+entries[1]+"' does not have three components",file=sys.stderr)
|
print("warning: ignoring line "+str(i)+" in file '"+file+"': color '"+entries[2]+"' does not have three components",file=sys.stderr)
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
color=(float(color_str[0]),float(color_str[1]),float(color_str[2]))
|
color=(float(color_str[0]),float(color_str[1]),float(color_str[2]))
|
||||||
except:
|
except:
|
||||||
print("warning: ignoring line "+str(i)+" in file '"+file+"': color '"+entries[1]+"' cannot be read",file=sys.stderr)
|
print("warning: ignoring line "+str(i)+" in file '"+file+"': color '"+entries[2]+"' cannot be read",file=sys.stderr)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if self.check_interaction_any(pos,None):
|
# candidate particle
|
||||||
|
try:
|
||||||
|
particle_type=int(entries[0])
|
||||||
|
except:
|
||||||
|
print("warning: ignoring line "+str(i)+" in file '"+file+"': particle type '"+entries[0]+"' is not an integer",file=sys.stderr)
|
||||||
|
continue
|
||||||
|
if particle_type==CROSS_INDEX:
|
||||||
|
candidate=Cross(pos.x,pos.y,color=color)
|
||||||
|
else:
|
||||||
|
print("warning: ignoring line "+str(i)+" in file '"+file+"': unrecognized particle type: '"+entries[0]+"'",file=sys.stderr)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not self.check_interaction_any(candidate,Point(0,0)):
|
||||||
# add to list
|
# add to list
|
||||||
self.particles.append(Cross(pos.x,pos.y,color=color))
|
self.particles.append(candidate)
|
||||||
|
self.unselected.append(candidate)
|
||||||
else:
|
else:
|
||||||
print("warning: ignoring line "+str(i)+" in file '"+file+"': particle overlaps with existing particles",file=sys.stderr)
|
print("warning: ignoring line "+str(i)+" in file '"+file+"': particle overlaps with existing particles",file=sys.stderr)
|
||||||
|
|
||||||
@ -333,3 +348,7 @@ class Painter(Widget):
|
|||||||
self.draw()
|
self.draw()
|
||||||
|
|
||||||
|
|
||||||
|
# global variables (used like precompiler variables)
|
||||||
|
CROSS_INDEX=1
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user