def defaults(self): return { 'x0': 300, 'y0': 300, 'grid_xy' : point(10, 10), 'translation': point(100, 100), 'window_size': point(450, 400), 'title': 'Editor', 'line_width': 10, **super().defaults() }
def uninvert(self): if self.height < 0: self.shape = None lower, upper = self.lower_right.y, self.upper_left.y self.upper_left = point(self.upper_left.x, lower) self.lower_right = point(self.lower_right.x, upper) if self.width < 0: self.shape = None left, right = self.lower_right.x, self.upper_left.x self.upper_left = point(left, self.upper_left.y) self.lower_right = point(right, self.lower_right.y) fill_in_the_blanks(self)
def move_if_held(self, held_edge, mouse_pos): if not held_edge: return if held_edge == 'corner_top_left': self.upper_left = mouse_pos elif held_edge == 'corner_top_right': self.upper_left = point(self.upper_left.x, mouse_pos.y) self.lower_right = point(mouse_pos.x, self.lower_right.y) elif held_edge == 'corner_bottom_left': self.upper_left = point(mouse_pos.x, self.upper_left.y) self.lower_right = point(self.lower_right.x, mouse_pos.y) elif held_edge == 'corner_bottom_right': self.lower_right = mouse_pos elif held_edge == 'left': self.upper_left = point(mouse_pos.x, self.upper_left.y) elif held_edge == 'right': self.lower_right = point(mouse_pos.x, self.lower_right.y) elif held_edge == 'top': self.upper_left = point(self.upper_left.x, mouse_pos.y) elif held_edge == 'bottom': self.lower_right = point(self.lower_right.x, mouse_pos.y) elif held_edge == 'body': if self.held_from is None: self.held_from = self.upper_left - mouse_pos else: self.upper_left = self.held_from + mouse_pos self.lower_right = self.upper_left + self.shape self.recalculate_height_and_width()
def __init__(self, app, **params): # Call superclasses QWidget.__init__(self, app.ui.centralwidget) Representable.__init__(self, **params) # Save the app self.app = app # Set geometry etc. self.initUI() # Init grid self.grid = Grid(self) self.show_grid = True # Init shapes self.shapes = {} self.shapes_to_create = [] self.shapes_to_delete = [] # Init state self.mouse_pos = point(0, 0) self.flags = { 'mouse_claimed': CountingSemaphore(), 'clicked': False, 'released': False, **{ key_name: False for key_name in MODIFIERS.values() } } self.held_keys = set() self.painting = False
def draw(self, painter, mouse_pos=point(0, 0), flags=None): if flags is None: flags = {} self.handle_movement(mouse_pos, flags) self.draw_main(painter) self.draw_edges(painter, mouse_pos, flags)
def draw_edge(self, painter, edge, color): x0, y0 = self.upper_left x1, y1 = self.lower_right corner = point(self.corner_size, self.corner_size) painter.setBrush(*color) if edge == 'corner_top_left': painter.drawRect(x0 - self.corner_size / 2, y0 - self.corner_size / 2, *corner) elif edge == 'corner_top_right': painter.drawRect(x1 - self.corner_size / 2, y0 - self.corner_size / 2, *corner) elif edge == 'corner_bottom_left': painter.drawRect(x0 - self.corner_size / 2, y1 - self.corner_size / 2, *corner) elif edge == 'corner_bottom_right': painter.drawRect(x1 - self.corner_size / 2, y1 - self.corner_size / 2, *corner) elif edge == 'left': painter.drawRect(x0 - self.hover_size / 2, y0, self.hover_size, self.height) elif edge == 'right': painter.drawRect(x1 - self.hover_size / 2, y0, self.hover_size, self.height) elif edge == 'top': painter.drawRect(x0, y0 - self.hover_size / 2, self.width, self.hover_size) elif edge == 'bottom': painter.drawRect(x0, y1 - self.hover_size / 2, self.width, self.hover_size) elif edge == 'body': painter.drawRect(*self.upper_left, *self.shape)
def fill_in_the_blanks(self): if self.shape is None: if self.lower_right is None or self.upper_left is None: raise KeyError( 'Must either specify both upper_left and lower_right or one shape' ) self.lower_right = point(*self.lower_right) self.upper_left = point(*self.upper_left) self.shape = self.lower_right - self.upper_left elif self.lower_right is None or self.upper_left is None: self.shape = point(*self.shape) if self.lower_right is None: if self.upper_left is None: raise KeyError( 'Either upper_left or lower_right must be specified') self.lower_right = point(*self.upper_left) + self.shape else: self.upper_left = point(*self.lower_right) - self.shape
def draw(self, painter): for x in range(0, self.canvas.window_size.x, self.canvas.grid_xy.x): painter.drawLine(point(x, 0), point(x, self.canvas.window_size.y)) for y in range(0, self.canvas.window_size.y, self.canvas.grid_xy.y): painter.drawLine(point(0, y), point(self.canvas.window_size.x, y))
def update_mouse_pos(self, event): xy = point(event.x(), event.y()) self.mouse_pos = xy - (xy % self.grid_xy)
def set_grid_size(self, x, y): self.grid_xy = point(x, y)