Skip to content

Commit

Permalink
Implement fixed ID assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Feb 11, 2024
1 parent f64eff7 commit 50a5011
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 53 deletions.
71 changes: 38 additions & 33 deletions Tools/ged/ged.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import copy
from enum import Enum, IntEnum
import random
from random import randrange, randint
import dataclasses
from dataclasses import dataclass
Expand Down Expand Up @@ -489,9 +490,13 @@ def redraw(self):

def canvas_key(self, evt):
# print(evt)
def add_item(cls):
def add_item(itemtype):
x, y = self.canvas_point(evt.x, evt.y)
item = cls(*self.grid_align(x, y), *self.grid_align(50, 50))
x, y, w, h = *self.grid_align(x, y), *self.grid_align(50, 50)
item = GItem.create(itemtype, x=x, y=y, w=w, h=h)
item.assign_unique_id(self.display_list)
if hasattr(item, 'text'):
item.text = item.id.replace('_', ' ')
self.add_item(item)
self.select([item])

Expand All @@ -504,16 +509,16 @@ def add_item(cls):
if len(evt.keysym) != 1 or (mod & EVS_CONTROL):
return
c = evt.keysym.upper() if (mod & EVS_SHIFT) else evt.keysym.lower()
cls = {
'r': GRect,
'e': GEllipse,
'R': GFilledRect,
'E': GFilledEllipse,
't': GText,
'b': GButton,
itemtype = {
'r': 'Rect',
'R': 'FilledRect',
'e': 'Ellipse',
'E': 'FilledEllipse',
't': 'Text',
'b': 'Button',
}.get(c)
if cls is not None:
add_item(cls)
if itemtype is not None:
add_item(itemtype)


class Editor:
Expand Down Expand Up @@ -671,7 +676,6 @@ def dl_serialise(display_list):
for item in display_list:
d = {
'type': item.typename,
'tag': item.id,
}
for name, value in item.__dict__.items():
if type(value) in [int, float, str]:
Expand All @@ -684,9 +688,7 @@ def dl_serialise(display_list):
def dl_deserialise(data):
display_list = []
for d in data:
typename = d.pop('type')
tag = d.pop('tag')
item = GItem.create(typename)
item = GItem.create(d.pop('type'), id=d.pop('id'))
for a, v in d.items():
ac = item.fieldtype(a)
setattr(item, a, ac(v))
Expand All @@ -701,31 +703,32 @@ def fileClear():

def fileAddRandom(count=10):
display_list = []
id_list = set(x.id for x in handler.display_list)
for i in range(count):
w_min, w_max = 10, 200
h_min, h_max = 10, 100
w = randint(w_min, w_max)
h = randint(h_min, h_max)
x = randrange(handler.width - w)
y = randrange(handler.height - h)
r = Rect(*handler.grid_align(x, y, w, h))
line_width = randint(1, 5)
radius = randint(0, min(r.w, r.h) // 2)
kind = randrange(6)
if kind == 0:
item = GEllipse(line_width=line_width)
elif kind == 1:
item = GFilledEllipse()
elif kind == 2:
item = GText()
elif kind == 3:
item = GButton()
elif kind == 4:
item = GRect(line_width=line_width, radius=radius)
elif kind == 5:
item = GFilledRect(radius=radius)
item.bounds = r
item.color = GColor(randrange(0xffffff))
x, y, w, h = handler.grid_align(x, y, w, h)
itemtype = random.choice([
GRect,
GFilledRect,
GEllipse,
GFilledEllipse,
GText,
GButton,
])
item = itemtype(x=x, y=y, w=w, h=h, color = GColor(randrange(0xffffff)))
item.assign_unique_id(id_list)
id_list.add(item.id)
if hasattr(item, 'line_width'):
item.line_width = randint(1, 5)
if hasattr(item, 'radius'):
item.radius = randint(0, min(w, h) // 2)
if hasattr(item, 'text'):
item.text = item.id.replace('_', ' ')
display_list.append(item)
handler.add_items(display_list)

Expand Down Expand Up @@ -804,6 +807,8 @@ def changeScale():

def value_changed(name, value):
for item in handler.sel_items:
if not hasattr(item, name):
continue
try:
cls = item.fieldtype(name)
# print(f'setattr({item}, {name}, {cls(value)})')
Expand Down
44 changes: 24 additions & 20 deletions Tools/ged/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,33 @@ def __repr__(self):

@dataclass
class GItem(Rect):
id: str = None
color: GColor = GColor('orange')

@staticmethod
def create(typename: str):
return getattr(sys.modules[__name__], f'G{typename}')()
def create(itemtype, **field_values):
if isinstance(itemtype, str):
itemtype = getattr(sys.modules[__name__], f'G{itemtype}')
return itemtype(**field_values)

@classmethod
@property
def typename(self):
return self.__class__.__name__[1:]
def typename(cls):
return cls.__name__[1:]

def assign_unique_id(self, existing_ids_or_list):
if isinstance(existing_ids_or_list, set):
existing_ids = existing_ids_or_list
else:
existing_ids = set(item.id for item in existing_ids_or_list)
n = 1
typename = self.typename
while True:
id = f'{typename}_{n}'
if id not in existing_ids:
self.id = id
break
n += 1

def fieldtype(self, field_name: str):
return self.__dataclass_fields__[field_name].type
Expand All @@ -90,10 +108,6 @@ def get_min_size(self, offset=0):
def get_bounds(self):
return Rect(self.x, self.y, self.w, self.h)

@property
def id(self):
return 'G%08x' % id(self)


@dataclass
class GRect(GItem):
Expand Down Expand Up @@ -152,12 +166,7 @@ def draw(self, c):
@dataclass
class GText(GItem):
font: str = 'default'
text: str = None

def __post_init__(self):
super().__post_init__()
if self.text is None:
self.text = f'Text {self.id}'
text: str = ''

def draw(self, c):
c.color = str(self.color)
Expand All @@ -171,14 +180,9 @@ def draw(self, c):
@dataclass
class GButton(GItem):
font: str = 'default'
text: str = None
text: str = ''
text_color: GColor = GColor('white')

def __post_init__(self):
super().__post_init__()
if self.text is None:
self.text = f'button {self.id}'

def draw(self, c):
radius = min(self.w, self.h) // 8
M = radius // 2
Expand Down

0 comments on commit 50a5011

Please sign in to comment.