Beispiel #1
0
    def __init__(self):
        self._gui = MainWindow(self)
        self._command_engine = CommandEngine(self)
        self._printer = CanvasPrinter(self._gui.canvas)
        self._shapes = ShapesStore(self)

        # import CliParser this late to avoid import loop
        from app.parsers.cli_parser import CliParser
        self._cli_parser = CliParser(self, RgbColorParser())
Beispiel #2
0
def canvas(qtbot) -> Canvas:
    controller = ControllerMockup()

    gui = MainWindow(controller)
    gui.show()
    qtbot.addWidget(gui)

    canvas = Canvas(controller)
    assert canvas.hasMouseTracking() is True
    return canvas
Beispiel #3
0
def test_print_lines_to_history(gui: MainWindow):
    assert gui._ui.history.toPlainText() == ''
    gui.print_lines_to_history(
        'new line printed to the history \n actualy it\'s two lines')
    assert gui._ui.history.toPlainText(
    ) == 'new line printed to the history \n actualy it\'s two lines'
    gui.print_lines_to_history('yet another line')
    assert (
        gui._ui.history.toPlainText() ==
        'new line printed to the history \n actualy it\'s two lines\nyet another line'
    )
Beispiel #4
0
def test_delete_from_history(gui: MainWindow):
    gui.print_lines_to_history('line1')
    gui.print_lines_to_history('line2')
    gui.print_lines_to_history('line3')
    gui.print_lines_to_history('line4')

    gui.delete_from_history(1)
    assert gui._ui.history.toPlainText() == 'line1\nline2\nline3'

    gui.delete_from_history(3)
    assert gui._ui.history.toPlainText() == ''

    with pytest.raises(ValueError):
        gui.delete_from_history(123)
Beispiel #5
0
    def interface(cli, quiet, ip, port):
        app = bundle_app()

        if cli:
            ip = ip or get_accessible_ips()[0][1]
            port = port or get_random_available_port(ip)
            app.config['LOCALADDR'] = ip

            click.echo(
                click.style(f'FQM {VERSION} is running on http://{ip}:{port}',
                            bold=True,
                            fg='green'))
            click.echo('')
            click.echo(
                click.style('Press Control-c to stop',
                            blink=True,
                            fg='black',
                            bg='white'))
            monkey.patch_socket()
            pywsgi.WSGIServer(
                (str(ip), int(port)), app,
                log=None if quiet else 'default').serve_forever()
        else:
            gui_process = QApplication(sys.argv)
            window = MainWindow(
                app)  # NOTE: has to be decleared in a var to work properly
            QCoreApplication.processEvents()
            gui_process.exec_()
Beispiel #6
0
def test_undo(gui: MainWindow):
    assert gui._ui.actionUndo.isEnabled() is False
    gui.enable_undo()
    assert gui._ui.actionUndo.isEnabled() is True
    gui.enable_undo()
    assert gui._ui.actionUndo.isEnabled() is True
    gui.disable_undo()
    assert gui._ui.actionUndo.isEnabled() is False
Beispiel #7
0
def test_clear_history(gui: MainWindow):
    gui.print_lines_to_history('line1')
    gui.print_lines_to_history('line2')
    gui.print_lines_to_history('line3')
    gui.print_lines_to_history('line4')

    gui.clear_history()
    assert gui._ui.history.toPlainText() == ''
Beispiel #8
0
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
Beispiel #9
0
def test_handle_user_input(gui: MainWindow):
    gui._ui.manualInput.setText('test input 123')
    gui._handle_user_input()
    assert gui._ui.manualInput.text() == ''
    assert gui._controller.parsed == 'test input 123'

    gui._ui.manualInput.setText('')
    gui._handle_user_input()
    assert gui._ui.manualInput.text() == ''
    assert gui._controller.parsed == 'test input 123'

    gui._ui.manualInput.setText('     \n     \t     ')
    gui._handle_user_input()
    assert gui._ui.manualInput.text() == ''
    assert gui._controller.parsed == 'test input 123'
Beispiel #10
0
class Controller:
    """
    Main class of this application. Holds all crucial pieces together.
    It represents an observer in the observer design pattern.
    """
    def __init__(self):
        self._gui = MainWindow(self)
        self._command_engine = CommandEngine(self)
        self._printer = CanvasPrinter(self._gui.canvas)
        self._shapes = ShapesStore(self)

        # import CliParser this late to avoid import loop
        from app.parsers.cli_parser import CliParser
        self._cli_parser = CliParser(self, RgbColorParser())

    def add_shapes(self, *shapes: Shape):
        for shape in shapes:
            self.print_to_history(str(shape))
        self._shapes.add_shapes(*shapes)

    def move_shapes(self,
                    move_from: Point,
                    move_to: Point,
                    divergence: bool = False) -> Dict[str, List[Shape]]:
        return self._shapes.move_shapes(move_from, move_to, divergence)

    def replace_shapes_store(self, shapes: List[Shape]):
        self._shapes = ShapesStore(self, shapes)
        self.update()

    def remove_last_shape(self):
        self._shapes.remove_last_shape()

    def remove_shapes_at(self,
                         point: Point,
                         divergence: bool = False) -> Dict[str, List[Shape]]:
        return self._shapes.remove_shapes_at(point, divergence)

    def preview_shape(self, shape: Shape):
        self._shapes.set_preview(shape)

    def end_preview(self):
        self._shapes.set_preview(None)

    def parse_command(self, command_text: str):
        command = self._cli_parser.parse_input(command_text)
        self.execute_command(command, command_text=command_text)

    def execute_command(self,
                        command: Command,
                        from_redo: bool = False,
                        command_text: str = None):
        history_line = ' > ' + (command_text or str(command))
        self._gui.print_lines_to_history(history_line)
        self._command_engine.execute_command(command, from_redo=from_redo)

    def remove_last_command(self):
        self._command_engine.remove_last_command()

    def print_to_history(self, lines: str):
        self._gui.print_lines_to_history(lines)

    def delete_from_history(self, number_of_lines: int = 1):
        self._gui.delete_from_history(number_of_lines)

    def shapes_at(self,
                  point: Point = None,
                  divergence: bool = False) -> List[Shape]:
        return self._shapes.shapes_at(point, divergence)

    def print_shapes_to_history(self, point: Point):
        for shape in self.shapes_at(point):
            self.print_to_history(str(shape))

    def print_all_shapes(self, printer: Printer = None) -> List[Shape]:
        return self._shapes.print_all(printer or self._printer)

    def update(self):
        self._printer.update(self)

    def undo(self):
        self._command_engine.undo()

    def redo(self):
        self._command_engine.redo()

    def enable_undo(self):
        self._gui.enable_undo()

    def enable_redo(self):
        self._gui.enable_redo()

    def disable_undo(self):
        self._gui.disable_undo()

    def disable_redo(self):
        self._gui.disable_redo()

    def save_dialog(self, path_to_file: str):
        self._gui.save_dialog(path_to_file)

    def load_dialog(self, path_to_file: str):
        self._gui.load_dialog(path_to_file)

    def save(self, file: str):
        commands = self._command_engine.get_all_commands()
        with open(file, 'w+', encoding='utf-8') as f:
            [f.write(str(c) + '\n') for c in commands['undos']]

        self._gui.set_status('File saved!')

    def load(self, file: str):
        with open(file, 'r', encoding='utf-8') as f:
            # Getting rid of the newline `\n` at the end of every line
            commands = [line[:-1] for line in f.readlines()]
            for command_text in commands:
                command = self._cli_parser.parse_input(command_text)
                self.execute_command(command, command_text=command_text)

        self._gui.set_status('File loaded!')

    def run_app(self):
        # Run the whole app
        self._gui.show()

    def clear_dialog(self) -> bool:
        return self._gui.clear_dialog()

    def restart(self):
        self._shapes.restart()

    def quit(self):
        self._gui.close()
Beispiel #11
0
def test_set_status(gui: MainWindow):
    assert gui.statusBar().currentMessage() == 'Move'
    gui.set_status('Rectangle')
    assert gui.statusBar().currentMessage() == 'Rectangle'
    gui.set_status()
    assert gui.statusBar().currentMessage() == 'Move'
Beispiel #12
0
def test_handle_action_quit(gui: MainWindow):
    gui._handle_action_quit()
    assert gui._controller.command == QuitCommand(gui._controller)
Beispiel #13
0
def test_handle_load_file(gui: MainWindow):
    gui._handle_load_file()
    assert gui._controller.command == LoadCommand(gui._controller)
Beispiel #14
0
def test_handle_save_file(gui: MainWindow):
    gui._handle_save_file()
    assert gui._controller.command == SaveCommand(gui._controller)
Beispiel #15
0
def test_handle_action_new(gui: MainWindow):
    gui._handle_action_new()
    assert gui._controller.command == ClearCommand(gui._controller)
Beispiel #16
0
def gui(qtbot: QtBot) -> MainWindow:
    controller = ControllerMockup()
    gui = MainWindow(controller)
    qtbot.addWidget(gui)
    return gui