def __init__(self, app_handler: UI, builder: Gtk.Builder): super().__init__(app_handler, builder) self.app_handler: UI = app_handler self._step = 10.0 self.viewport = Viewport(hpt(10., 10.), hpt(864.0, 438.0)) self.name_rt = Gtk.CellRendererText() self.type_rt = Gtk.CellRendererText() self.name_col = Gtk.TreeViewColumn("Name", self.name_rt, text=0) self.type_col = Gtk.TreeViewColumn("Type", self.type_rt, text=1) self.tree_objects.set_model(app_handler.lst_store_objects) self.tree_objects.append_column(self.name_col) self.tree_objects.append_column(self.type_col) self.model.subscribe("draw", self.update_screen) self.model.subscribe("add", self.on_add_object) self.no_shift = { Gdk.KEY_Up: self.on_btn_up_clicked, Gdk.KEY_Down: self.on_btn_down_clicked, Gdk.KEY_Left: self.on_btn_left_clicked, Gdk.KEY_Right: self.on_btn_right_clicked, } self.with_ctrl = { Gdk.KEY_Up: self.on_btn_zoom_in_clicked, Gdk.KEY_Down: self.on_btn_zoom_out_clicked, Gdk.KEY_Left: self.on_btn_left_rotate_clicked, Gdk.KEY_Right: self.on_btn_right_rotate_clicked, Gdk.KEY_plus: self.on_btn_zoom_in_clicked, Gdk.KEY_minus: self.on_btn_zoom_out_clicked, }
def on_btn_apply_scale_clicked(self, _: Gtk.Button) -> None: selected = self.model.selected center = selected.center m = scale(hpt(float(self.entry_scalex.get_text()), float(self.entry_scaley.get_text()))) m = np.matmul(np.matmul(translate(center * hpt(-1, -1)), m), translate(center)) selected.points = np.matmul(selected.points, m) self.win.hide() self.model.update()
def on_btn_apply_rotate_clicked(self, sender: Gtk.Button) -> None: selected = self.model.selected rel_to = self.id_comboxBox_rotate.get_active_id() if rel_to == 0: center = selected.center elif rel_to == 1: center = hpt(0.0, 0.0) else: center = hpt(float(self.entry_rotatex.get_text()), float(self.entry_rotatey.get_text())) m = rel_transform(center, rotate2D(rad(float(self.entry_radian.get_text())))) selected.points = selected.points @ m self.win.hide() self.model.update()
def on_btn_apply_translation_clicked(self, _: Gtk.Button) -> None: selected = self.model.selected x, y = float(self.entry_translatex.get_text()), float(self.entry_translatey.get_text()) m = translate(hpt(x, y)) selected.points = selected.points @ m self.win.hide() self.model.update()
def objects(self, clipper=...): ppc_matrix = rotate2D(-self.window.angle) bl = self.window.origin @ ppc_matrix tr = hpt(*(bl + self.window.size)[:-1]) self.window._ppc = np.vstack((bl, tr)) if clipper is ...: clipper = lambda x: x for obj in self.display_file.values(): obj._ppc = obj.points @ ppc_matrix tobj = clipper(obj) if tobj is not None: yield tobj
def snap(self, direction, v, point): x, y, *_ = point xv, yv, *_ = v win_p1, win_p2 = self.window._ppc m = 0. if xv == 0. else yv / xv if direction & Direction.LEFT: new_x = win_p1[0] new_y = m * (win_p1[0] - x) + y elif direction & Direction.RIGHT: new_x = win_p2[0] new_y = m * (new_x - x) + y elif direction & Direction.UP: new_y = win_p2[1] m = 1. / m if m else 0.0 new_x = x + m * (new_y - y) elif direction & Direction.DOWN: new_y = win_p1[1] m = 1. / m if m else 0.0 new_x = x + m * (new_y - y) else: new_x, new_y = x, y return hpt(new_x, new_y)
def viewport_transform(self, p): x, y, _ = (p - self.win._ppc[0]) / self.win.size return hpt(x, 1 - y) @ self.viewport.matrix()
def rect(name: str, tl, size) -> Polygon: br = tl + size x0, y0 = tl[:-1] x1, y1 = br[:-1] return Polygon(name, hpt(x0, y0), hpt(x1, y0), hpt(x1, y1), hpt(x0, y1))
def on_btn_add_curve_clicked(self, sender: Gtk.Button): points = [hpt(*p[:-1]) for p in self.__points] self.model.add_obj(Bezier(self.entry_curve_name.get_text(), *points)) self.win.hide()
def origin(self): sz = (.5 * self.size) * (self.x + self.y) return hpt(*(self.center - sz)[:-1])
def __init__(self): from .window import Window self.display_file: tp.Dict[str, GraphicalObject] = {} self.subscriptions = {} self.window = Window(hpt(447., 234.), hpt(894.0, 468.0)) self.selected_name = None
def resize(self, width, height): size = hpt(width, height) self.size = size