def __init__(self, controller): super().__init__() self.setStyleSheet('background-color: red') self._controller = controller self.brush = MoveShapeBrush() self.setCursor(self.brush.cursor) self.setMouseTracking(True) self.color = (255, 255, 255)
def test_set_brush(canvas: Canvas): assert canvas.brush == MoveShapeBrush() assert canvas.cursor() == Qt.ArrowCursor canvas.set_brush(LineShapeBrush()) assert canvas.brush.color == (255, 255, 255) assert canvas.brush == LineShapeBrush() assert canvas.cursor() == Qt.CrossCursor canvas.set_color((10, 20, 30)) canvas.set_brush(RectShapeBrush()) assert canvas.brush.color == (10, 20, 30) assert canvas.brush == RectShapeBrush() assert canvas.cursor() == Qt.CrossCursor canvas.set_brush() assert canvas.brush == MoveShapeBrush() assert canvas.cursor() == Qt.ArrowCursor
def _toggle_brush(self, brush: Brush): if self.canvas.brush != MoveShapeBrush(): # Un-checking the previous brush button previous_brush_button = self.brush_buttons[self.canvas.brush] previous_brush_button.setChecked(False) if self.canvas.brush != brush: self.canvas.set_brush(brush) self.set_status(str(brush)) else: self.canvas.set_brush() self.set_status()
def test_mouse_move_event(canvas: Canvas): assert canvas.brush == MoveShapeBrush() assert canvas.cursor() == Qt.ArrowCursor canvas.mouseMoveEvent(EventMockup) assert (canvas._controller.command is None) canvas.set_brush(DotShapeBrush()) canvas.mouseMoveEvent(EventMockup) assert (canvas._controller.command == PrintDotCommand( canvas._controller, EventMockup.x(), EventMockup.y(), (255, 255, 255))) assert canvas.cursor() == Qt.CrossCursor
class Canvas(QtWidgets.QWidget): """ A (GUI) drawing place for all the shapes. """ def __init__(self, controller): super().__init__() self.setStyleSheet('background-color: red') self._controller = controller self.brush = MoveShapeBrush() self.setCursor(self.brush.cursor) self.setMouseTracking(True) self.color = (255, 255, 255) def set_brush(self, brush: Brush = MoveShapeBrush()): self.brush = brush self.brush.color = self.color self.setCursor(self.brush.cursor) def set_color(self, color: Tuple[int, int, int]): self.color = color self.brush.color = color # -------------------------- QWidget overridden methods ---------------------------- # Overriding paintEvent method of QWidget to respond to QEvent.Paint. # This method should be the only place from where we draw with QPainter. This means, that the print_* methods # should never be called before executing this method! def paintEvent(self, event: QEvent.Paint): self._controller.print_all_shapes() # By default this event is emitted only when some mouse button is pressed and the mouse moves def mouseMoveEvent(self, event: QEvent.MouseMove): self.brush.mouse_move(self._controller, event.x(), event.y(), event.buttons()) self.setCursor(self.brush.cursor) def mousePressEvent(self, event: QEvent.MouseButtonPress): self.brush.mouse_press(self._controller, event.x(), event.y(), event.buttons()) self.setCursor(self.brush.cursor)
def test_toggle_brush(gui: MainWindow): assert gui.canvas.brush == MoveShapeBrush() assert gui.statusBar().currentMessage() == 'Move' for brush_button in gui.brush_buttons.values(): assert brush_button.isChecked() is False button = gui.brush_buttons[LineShapeBrush()] button.click() assert gui.canvas.brush is LineShapeBrush() assert gui.statusBar().currentMessage() == 'Line' assert button.isChecked() is True button = gui.brush_buttons[CircleShapeBrush()] button.click() assert gui.canvas.brush is CircleShapeBrush() assert gui.statusBar().currentMessage() == 'Circle' assert gui.brush_buttons[LineShapeBrush()].isChecked() is False assert button.isChecked() is True button.click() assert gui.canvas.brush == MoveShapeBrush() assert gui.statusBar().currentMessage() == 'Move' assert button.isChecked() is False
def test_mouse_press_event(canvas: Canvas): assert canvas.brush == MoveShapeBrush() assert canvas.cursor() == Qt.ArrowCursor canvas.mousePressEvent(EventMockup) assert canvas._controller.command is None canvas.set_brush(RectShapeBrush()) canvas.mousePressEvent(EventMockup) canvas.mousePressEvent(EventMockup) assert (canvas._controller.command == PrintRectCommand( receiver=canvas._controller, start_x=EventMockup.x(), start_y=EventMockup.y(), color=(255, 255, 255), rect_factory=PointsRectFactory, end_x=EventMockup.x(), end_y=EventMockup.y()))
def test_move_shape_brush(controller: ControllerMockup): b1 = MoveShapeBrush() b2 = MoveShapeBrush() assert b1 == b2 assert str(b1) == str(b2) == 'Move' b1.mouse_move(controller, -1, 0, None) assert b1.cursor == Qt.ArrowCursor b1.mouse_move(controller, 0, 5, None) assert b1.cursor == Qt.OpenHandCursor b1.mouse_press(controller, 0, 0, Qt.LeftButton) assert controller.command is None assert b1.cursor == Qt.ClosedHandCursor b1.mouse_press(controller, 10, 10, Qt.LeftButton) assert controller.command == MoveShapeCommand(receiver=controller, start_x=0, start_y=0, end_x=10, end_y=10) assert b1._start is None assert b1.cursor == Qt.OpenHandCursor
def set_brush(self, brush: Brush = MoveShapeBrush()): self.brush = brush self.brush.color = self.color self.setCursor(self.brush.cursor)
def set_status(self, message: str = str(MoveShapeBrush())): self.statusBar().showMessage(message)