Beispiel #1
0
def test_resizing(qtbot):
    """
    Simply test if resizing doesn't trigger any errors
    :param QtBot qtbot:
    :return: None
    """
    shot_widget = ShotWidget()
    qtbot.addWidget(shot_widget)

    event = QtGui.QResizeEvent(QtCore.QSize(200, 200), shot_widget.size())
    shot_widget.resizeEvent(event)
Beispiel #2
0
def test_image_save_failing(qtbot, tmpdir):
    """
    Test if image saving fails if no grab has been made
    :param QtBot qtbot:
    :param LocalPath tmpdir:
    :return: None
    """
    shot_widget = ShotWidget()
    qtbot.addWidget(shot_widget)

    test_path = os.path.join(str(tmpdir), '.png')

    with pytest.raises(ValueError):
        shot_widget.save_capture(test_path)
Beispiel #3
0
def test_assumed_integration(qtbot, monkeypatch, p1, p2, expected_height,
                             expected_width):
    """
    Test the assumed integration of the ShotWidget.
    :param QtBot qtbot:
    :param MonkeyPatch monkeypatch:
    :param QtCore.QPoint p1:
    :param QtCore.QPoint p2:
    :param int expected_height:
    :param int expected_width:
    :return: None
    """
    shot_widget = ShotWidget()
    # mock the coordinate grabbing dialog
    monkeypatch.setattr(shot_widget, 'get_coordinates', lambda: (p1, p2))

    qtbot.addWidget(shot_widget)

    btn_trigger = QtWidgets.QPushButton()
    btn_trigger.clicked.connect(shot_widget.capture_screen)
    qtbot.addWidget(btn_trigger)

    # test if no screenshot has been made
    assert shot_widget._pmp_screen_grab is None
    qtbot.mouseClick(btn_trigger, QtCore.Qt.LeftButton)

    # test if a screenshot was made
    assert isinstance(shot_widget._pmp_screen_grab, QtGui.QPixmap)

    # test if the screenshot has the expected size
    img_height = shot_widget._pmp_screen_grab.height()
    img_width = shot_widget._pmp_screen_grab.width()

    assert img_height == pytest.approx(expected_height, 1)
    assert img_width == pytest.approx(expected_width, 1)
Beispiel #4
0
    def _create_ui(self):
        """
        Create the widget's UI
        :return: None
        """
        layout = QtWidgets.QVBoxLayout()
        self.setLayout(layout)
        self._shot_widget = ShotWidget()

        # create and set a background image for the shot widget
        bg = QtGui.QPixmap(300, 200)
        bg.fill(QtGui.QColor(128, 128, 128))
        self._shot_widget.setPixmap(bg)
        layout.addWidget(self._shot_widget)

        # toggle the ratio constraint on and off
        self._chb_constrain_ratio = QtWidgets.QCheckBox(
            'Constrain Capture image ratio')
        self._chb_constrain_ratio.stateChanged[int].connect(self.toggle_ratio)
        layout.addWidget(self._chb_constrain_ratio)

        # control the ratio constraint amount
        ratio_layout = QtWidgets.QHBoxLayout()
        ratio_layout.addWidget(QtWidgets.QLabel('Ratio:'))
        self._spn_ratio = QtWidgets.QDoubleSpinBox()
        self._spn_ratio.setValue(1)
        self._spn_ratio.setSingleStep(0.01)
        self._spn_ratio.setEnabled(False)
        self._spn_ratio.valueChanged[float].connect(self.set_ratio_value)
        ratio_layout.addWidget(self._spn_ratio)

        layout.addLayout(ratio_layout)

        # capture button
        self.btn_capture = QtWidgets.QPushButton('Create Capture')
        layout.addWidget(self.btn_capture)
        self.btn_capture.clicked.connect(self._shot_widget.capture_screen)

        # save the capture button
        self.btn_save = QtWidgets.QPushButton('Save Capture')
        layout.addWidget(self.btn_save)
        self.btn_save.clicked.connect(self.save_capture)
Beispiel #5
0
def test_coordinate_grabbing_options(qtbot, monkeypatch):
    """
    Test if options are used as expected.
    :param QtBot qtbot:
    :param MonkeyPatch monkeypatch:
    :return: None
    """
    def mocked_coordinate_getting(enable_constraint, ratio):
        assert enable_constraint is True
        assert ratio == 5

    monkeypatch.setattr(CoordinateWidget, 'get_coordinates',
                        mocked_coordinate_getting)

    shot_widget = ShotWidget()
    qtbot.addWidget(shot_widget)

    shot_widget.enable_image_ratio_constraint()
    shot_widget.set_image_ratio(5)
    shot_widget.get_coordinates()
Beispiel #6
0
def test_default_coordinate_grabbing(qtbot, monkeypatch):
    """
    Test if no ratio constraining is enforced by default.
    :param QtBot qtbot:
    :param MonkeyPatch monkeypatch:
    :return: None
    """
    def mocked_coordinate_getting(enable_constraint, ratio):
        assert enable_constraint is False
        assert ratio == 1

    monkeypatch.setattr(CoordinateWidget, 'get_coordinates',
                        mocked_coordinate_getting)

    shot_widget = ShotWidget()
    qtbot.addWidget(shot_widget)

    shot_widget.get_coordinates()

    # disabling the ratio constraint shouldn't make a difference
    shot_widget.disable_image_ratio_constraint()
    shot_widget.get_coordinates()
Beispiel #7
0
def test_coordinate_canceling(qtbot, monkeypatch, p1, p2):
    """
    Test if failed coordinate grabbing actually cancels the screen grabbing.
    :param QtBot qtbot:
    :param MonkeyPatch monkeypatch:
    :param QtCore.QPoint p1:
    :param QtCore.QPoint p2:
    :return: None
    """
    shot_widget = ShotWidget()
    # mock the coordinate grabbing dialog
    monkeypatch.setattr(shot_widget, 'get_coordinates', lambda: (p1, p2))

    qtbot.addWidget(shot_widget)

    btn_trigger = QtWidgets.QPushButton()
    btn_trigger.clicked.connect(shot_widget.capture_screen)
    qtbot.addWidget(btn_trigger)

    # test if no screenshot has been made
    qtbot.mouseClick(btn_trigger, QtCore.Qt.LeftButton)
    assert shot_widget._pmp_screen_grab is None
Beispiel #8
0
def test_image_save(qtbot, tmpdir, monkeypatch):
    """
    Test image saving.
    :param QtBot qtbot:
    :param LocalPath tmpdir:
    :param MonkeyPatch monkeypatch:
    :return: None
    """
    shot_widget = ShotWidget()
    qtbot.addWidget(shot_widget)
    test_path = os.path.join(str(tmpdir), '.png')

    monkeypatch.setattr(
        shot_widget, 'get_coordinates', lambda:
        (QtCore.QPoint(100, 100), QtCore.QPoint(300, 200)))

    shot_widget.capture_screen()
    shot_widget.save_capture(test_path)

    assert os.path.isfile(test_path)
Beispiel #9
0
class SampleUi(QtWidgets.QWidget):
    """
    SampleUi, inherits from QWidget, demonstrates a possible use ot the
    ShotWidget
    """
    def __init__(self):
        super(SampleUi, self).__init__()
        self._create_ui()

    def _create_ui(self):
        """
        Create the widget's UI
        :return: None
        """
        layout = QtWidgets.QVBoxLayout()
        self.setLayout(layout)
        self._shot_widget = ShotWidget()

        # create and set a background image for the shot widget
        bg = QtGui.QPixmap(300, 200)
        bg.fill(QtGui.QColor(128, 128, 128))
        self._shot_widget.setPixmap(bg)
        layout.addWidget(self._shot_widget)

        # toggle the ratio constraint on and off
        self._chb_constrain_ratio = QtWidgets.QCheckBox(
            'Constrain Capture image ratio')
        self._chb_constrain_ratio.stateChanged[int].connect(self.toggle_ratio)
        layout.addWidget(self._chb_constrain_ratio)

        # control the ratio constraint amount
        ratio_layout = QtWidgets.QHBoxLayout()
        ratio_layout.addWidget(QtWidgets.QLabel('Ratio:'))
        self._spn_ratio = QtWidgets.QDoubleSpinBox()
        self._spn_ratio.setValue(1)
        self._spn_ratio.setSingleStep(0.01)
        self._spn_ratio.setEnabled(False)
        self._spn_ratio.valueChanged[float].connect(self.set_ratio_value)
        ratio_layout.addWidget(self._spn_ratio)

        layout.addLayout(ratio_layout)

        # capture button
        self.btn_capture = QtWidgets.QPushButton('Create Capture')
        layout.addWidget(self.btn_capture)
        self.btn_capture.clicked.connect(self._shot_widget.capture_screen)

        # save the capture button
        self.btn_save = QtWidgets.QPushButton('Save Capture')
        layout.addWidget(self.btn_save)
        self.btn_save.clicked.connect(self.save_capture)

    @QtCore.Slot(int)
    def toggle_ratio(self, state):
        """
        Set the ratio constraint to the given state
        :param int state:
        :return: None
        """
        state = bool(state)
        self._spn_ratio.setEnabled(state)
        if state:
            self._shot_widget.enable_image_ratio_constraint()
        else:
            self._shot_widget.disable_image_ratio_constraint()

    @QtCore.Slot(float)
    def set_ratio_value(self, value):
        """
        Set the ratio value
        :param float value:
        :return: None
        """
        self._shot_widget.set_image_ratio(value)

    @QtCore.Slot()
    def save_capture(self):
        """
        Save the screen grab to the selected file path.
        :return: None
        """
        file_path, file_filter = QtWidgets.QFileDialog.getSaveFileName(
            caption='Save the capture', filter='*.png')
        if file_path is None:
            return

        self._shot_widget.save_capture(file_path)