class PolygonPlugin(CtxPlugin): name = 'PolygonPlugin' update_flag = False def __init__(self, app, parent): CtxPlugin.__init__(self, app, parent) events.connect(events.DOC_CHANGED, self.update) events.connect(events.SELECTION_CHANGED, self.update) def build(self): bmp = get_bmp(self, icons.CTX_POLYGON_NUM, _('Number of polygon angles')) self.add(bmp, 0, LEFT | CENTER, 2) self.num_spin = FloatSpin(self, 5, (3.0, 1000.0), 1.0, 0, onchange=self.changes) self.add(self.num_spin, 0, LEFT | CENTER, 2) def changes(self, *args): if self.update_flag: return val = self.num_spin.get_value() if self.insp.is_selection(): selection = self.app.current_doc.selection if self.insp.is_obj_polygon(selection.objs[0]): self.app.current_doc.api.set_polygon_corners_num(val) def update(self, *args): if self.insp.is_selection(): selection = self.app.current_doc.selection if self.insp.is_obj_polygon(selection.objs[0]): self.update_flag = True self.num_spin.set_value(selection.objs[0].corners_num) self.update_flag = False
class PolygonPlugin(CtxPlugin): name = "PolygonPlugin" update_flag = False def __init__(self, app, parent): CtxPlugin.__init__(self, app, parent) events.connect(events.DOC_CHANGED, self.update) events.connect(events.SELECTION_CHANGED, self.update) def build(self): bmp = get_bmp(self, icons.CTX_POLYGON_NUM, _("Number of polygon angles")) self.add(bmp, 0, LEFT | CENTER, 2) self.num_spin = FloatSpin( self, 5, (3.0, 1000.0), 1.0, 0, width=3, onchange=self.changes, spin_overlay=config.spin_overlay ) self.add(self.num_spin, 0, LEFT | CENTER, 2) def changes(self, *args): if self.update_flag: return val = self.num_spin.get_value() if self.insp.is_selection(): selection = self.app.current_doc.selection if self.insp.is_obj_polygon(selection.objs[0]): self.app.current_doc.api.set_polygon_corners_num(val) def update(self, *args): if self.insp.is_selection(): selection = self.app.current_doc.selection if self.insp.is_obj_polygon(selection.objs[0]): self.update_flag = True self.num_spin.set_value(selection.objs[0].corners_num) self.update_flag = False
class RectanglePlugin(CtxPlugin): name = 'RectanglePlugin' corners = [0, 0, 0, 0] orig_corners = [0, 0, 0, 0] update_flag = False active_corner = 0 def __init__(self, app, parent): CtxPlugin.__init__(self, app, parent) events.connect(events.DOC_CHANGED, self.update) events.connect(events.SELECTION_CHANGED, self.update) def build(self): bmp = get_bmp(self, icons.CTX_ROUNDED_RECT, _('Rounded rectangle')) self.add(bmp, 0, LEFT | CENTER, 2) self.slider = Slider(self, 0, (0, 100), onchange=self.slider_changes, on_final_change=self.slider_final_changes) self.add(self.slider, 0, LEFT | CENTER, 2) self.num_spin = FloatSpin(self, 0, (0.0, 100.0), 1.0, 0, width=3, onchange=self.changes, spin_overlay=config.spin_overlay) self.add(self.num_spin, 0, LEFT | CENTER, 2) self.switch = RectAngleSwitch(self, onchange=self.switch_changed) self.add(self.switch, 0, LEFT | CENTER, 3) self.switch.hide() self.keep_ratio = RatioToggle(self, onchange=self.lock_changed) self.add(self.keep_ratio, 0, LEFT | CENTER, 3) def lock_changed(self, *args): self.switch.set_visible(not self.keep_ratio.get_active()) self.parent.Layout() if self.keep_ratio.get_active(): val = self.corners[self.active_corner] self.active_corner = 0 self.switch.set_index(0) if not self.update_flag: self.apply_changes(val) self.update_vals() def switch_changed(self): self.active_corner = self.switch.get_index() self.update_vals() def slider_changes(self): if self.update_flag: return self.apply_changes(self.slider.get_value() / 100.0) def slider_final_changes(self): if self.update_flag: return self.apply_changes(self.slider.get_value() / 100.0, True) def changes(self, *args): if self.update_flag: return self.apply_changes(self.num_spin.get_value() / 100.0) def apply_changes(self, val, final=False): if self.insp.is_selection(): selection = self.app.current_doc.selection if self.insp.is_obj_rect(selection.objs[0]): if self.keep_ratio.get_active(): self.corners = [val, val, val, val] else: self.corners[self.active_corner] = val api = self.app.current_doc.api if final: api.set_rect_corners_final(self.corners, self.orig_corners) self.orig_corners = [] + self.corners else: api.set_rect_corners(self.corners) def update_vals(self): self.update_flag = True self.slider.set_value(int(self.corners[self.active_corner] * 100)) self.num_spin.set_value(self.corners[self.active_corner] * 100.0) self.update_flag = False def update(self, *args): if self.insp.is_selection(): selection = self.app.current_doc.selection if self.insp.is_obj_rect(selection.objs[0]): corners = [] + selection.objs[0].corners if self.corners == corners: self.update_vals() else: self.corners = corners self.orig_corners = [] + corners self.update_flag = True state = (corners[0] == corners[1] == corners[2] == corners[3]) self.keep_ratio.set_active(state) self.lock_changed()