def testDeselectsWhenClickingElsewhere(self): document = Document() document.new_shape() document.current_shape.append_point((0, 0)) tool = SelectTool(document) _perform_click(tool, 0, 0) _perform_click(tool, 1000, 0) # Click far away self.assertTrue(document.selected_point_index is None)
def testSelectsWhenMouseClicked(self): document = Document() document.new_shape() document.current_shape.append_point((0, 0)) tool = SelectTool(document) self.assertTrue(document.selected_point_index is None) _perform_click(tool, 0, 0) self.assertTrue(document.selected_point_index == 0)
class Canvas(QGraphicsView): def __init__(self): self._scene = QGraphicsScene() QGraphicsView.__init__(self, self._scene) self._document = Document() self._document.changed.connect(self._update_scene) self.use_tool(PenTool) self.setMouseTracking(True) def use_tool(self, tool_class): """Instantiates tool_class and uses it as the current tool.""" self._tool = tool_class(self._document) self._tool.needs_repaint.connect(self._update) def new_shape(self): self._document.new_shape() def delete_selection(self): self._document.delete_selection() if not self._document.current_shape: self._document.delete_current_shape() def _update(self): self.update() def _update_scene(self): self._scene.clear() for shape in self._document.shapes: self._scene.addPath(shape.make_painter_path()) def _call_tool(self, method_name, *args): method = getattr(self._tool, method_name, None) if method: method(*args) def _map_event(self, qt_event): """Take a QMouseEvent and return a MouseEvent in scene space.""" point = self.mapToScene(qt_event.x(), qt_event.y()) return MouseEvent((_round_to_half(point.x()), _round_to_half(point.y()))) def mouseMoveEvent(self, event): self._call_tool("mouse_move_event", self._map_event(event)) def mousePressEvent(self, event): self._call_tool("mouse_press_event", self._map_event(event)) def mouseReleaseEvent(self, event): self._call_tool("mouse_release_event", self._map_event(event)) def paintEvent(self, event): QGraphicsView.paintEvent(self, event) self._call_tool("paint_event", self)
def testRedundantNewShapeCreatesOnlyOneShape(self): document = Document() document.new_shape() document.new_shape() self.assertEqual(document.shape_count, 1)
def testNewShape(self): document = Document() shape = document.new_shape() self.assertTrue(shape is not None) self.assertEqual(document.shape_count, 1)