class ResizePlugin(CtxPlugin): name = 'ResizePlugin' update_flag = False width_spin = None height_spin = None keep_ratio = None 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_OBJECT_RESIZE, _('Selection size')) self.add(bmp, 0, LEFT | CENTER, 2) self.add((2, 2)) self.width_spin = UnitSpin(self.app, self, onchange=self.w_changes) self.add(self.width_spin, 0, LEFT | CENTER, 2) self.add(get_bmp(self, icons.CTX_W_ON_H), 0, LEFT | CENTER, 1) self.height_spin = UnitSpin(self.app, self, onchange=self.h_changes) self.add(self.height_spin, 0, LEFT | CENTER, 2) self.add((2, 2)) self.keep_ratio = RatioToggle(self) self.add(self.keep_ratio, 0, LEFT | CENTER, 2) def update(self, *args): if self.insp.is_selection(): bbox = self.app.current_doc.selection.bbox w = bbox[2] - bbox[0] h = bbox[3] - bbox[1] self.update_flag = True self.width_spin.set_point_value(w) self.height_spin.set_point_value(h) self.update_flag = False def w_changes(self, *args): if self.update_flag: return self.user_changes(True) def h_changes(self, *args): if self.update_flag: return self.user_changes(False) def user_changes(self, hr=True): if self.update_flag: return if self.insp.is_selection(): doc = self.app.current_doc bbox = doc.selection.bbox w = bbox[2] - bbox[0] h = bbox[3] - bbox[1] center_x = bbox[0] + w / 2.0 center_y = bbox[1] + h / 2.0 new_w = self.width_spin.get_point_value() new_h = self.height_spin.get_point_value() if not w == new_w or not h == new_h: if not new_w: self.width_spin.set_point_value(w) return if not new_h: self.height_spin.set_point_value(h) return m11 = new_w / w m22 = new_h / h if m11 == m22 == 1.0: return if self.keep_ratio.get_active(): if hr: dx = center_x * (1 - m11) dy = center_y * (1 - m11) trafo = [m11, 0.0, 0.0, m11, dx, dy] else: dx = center_x * (1 - m22) dy = center_y * (1 - m22) trafo = [m22, 0.0, 0.0, m22, dx, dy] else: dx = center_x * (1 - m11) dy = center_y * (1 - m22) trafo = [m11, 0.0, 0.0, m22, dx, dy] if trafo: doc.api.transform_selected(trafo)
class ResizePlugin(CtxPlugin): name = 'ResizePlugin' 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_OBJECT_RESIZE, _('Selection size')) self.add(bmp, 0, LEFT | CENTER, 2) self.add((2, 2)) self.width_spin = UnitSpin(self.app, self, onchange=self.w_changes) self.add(self.width_spin, 0, LEFT | CENTER, 2) self.add(get_bmp(self, icons.CTX_W_ON_H), 0, LEFT | CENTER, 1) self.height_spin = UnitSpin(self.app, self, onchange=self.h_changes) self.add(self.height_spin, 0, LEFT | CENTER, 2) self.add((2, 2)) self.keep_ratio = RatioToggle(self) self.add(self.keep_ratio, 0, LEFT | CENTER, 2) def update(self, *args): if self.insp.is_selection(): bbox = self.app.current_doc.selection.bbox w = bbox[2] - bbox[0] h = bbox[3] - bbox[1] self.update_flag = True self.width_spin.set_point_value(w) self.height_spin.set_point_value(h) self.update_flag = False def w_changes(self, *args): if self.update_flag: return self.user_changes(True) def h_changes(self, *args): if self.update_flag: return self.user_changes(False) def user_changes(self, hr=True): if self.update_flag: return if self.insp.is_selection(): doc = self.app.current_doc bbox = doc.selection.bbox w = bbox[2] - bbox[0] h = bbox[3] - bbox[1] center_x = bbox[0] + w / 2.0 center_y = bbox[1] + h / 2.0 new_w = self.width_spin.get_point_value() new_h = self.height_spin.get_point_value() if not w == new_w or not h == new_h: if not new_w: self.width_spin.set_point_value(w);return if not new_h: self.height_spin.set_point_value(h);return m11 = new_w / w m22 = new_h / h if m11 == m22 == 1.0:return trafo = [] if self.keep_ratio.get_active(): if hr: dx = center_x * (1 - m11) dy = center_y * (1 - m11) trafo = [m11, 0.0, 0.0, m11, dx, dy] else: dx = center_x * (1 - m22) dy = center_y * (1 - m22) trafo = [m22, 0.0, 0.0, m22, dx, dy] else: dx = center_x * (1 - m11) dy = center_y * (1 - m22) trafo = [m11, 0.0, 0.0, m22, dx, dy] if trafo: doc.api.transform_selected(trafo)
class RectanglePlugin(CtxPlugin): name = 'RectanglePlugin' corners = [0, 0, 0, 0] orig_corners = [0, 0, 0, 0] update_flag = False active_corner = 0 slider = None num_spin = None switch = None keep_ratio = None 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.pack(bmp, padding=2) self.slider = wal.Slider(self, 0, (0, 100), onchange=self.slider_changes, on_final_change=self.slider_final_changes) self.pack(self.slider, padding=2) self.num_spin = wal.FloatSpin(self, 0, (0.0, 100.0), 1.0, 0, onchange=self.changes) self.pack(self.num_spin, padding=2) self.switch = RectAngleSwitch(self, onchange=self.switch_changed) self.pack(self.switch, padding=3) self.switch.hide() self.keep_ratio = RatioToggle(self, onchange=self.lock_changed) self.pack(self.keep_ratio, padding=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()
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()