Example #1
0
def test_text(pixmap_differ: PixmapDiffer):
    actual: QPainter
    expected: QPainter
    with pixmap_differ.create_painters(
            300, 240, 'scaled_radio_button_text') as (actual, expected):
        ex_widget = QWidget()
        ex_layout = QVBoxLayout(ex_widget)
        ex_radio1 = QRadioButton('Lorem ipsum')
        ex_radio2 = QRadioButton('Lorem ipsum')
        ex_font = ex_radio1.font()
        if ex_font.family() == 'Sans Serif':
            # Fonts are different on Travis CI.
            big_font_size = 26
            small_font_size = 25
            ex_radio1.setStyleSheet('QRadioButton::indicator {width: 26} '
                                    'QRadioButton {spacing: 13}')
            ex_radio2.setStyleSheet('QRadioButton::indicator {width: 25} '
                                    'QRadioButton {spacing: 12}')
        else:
            big_font_size = 29
            small_font_size = 28
            ex_radio1.setStyleSheet('QRadioButton::indicator {width: 29} '
                                    'QRadioButton {spacing: 14}')
            ex_radio2.setStyleSheet('QRadioButton::indicator {width: 28} '
                                    'QRadioButton {spacing: 14}')
        ex_font.setPointSize(big_font_size)
        ex_radio1.setFont(ex_font)
        ex_font.setPointSize(small_font_size)
        ex_radio2.setFont(ex_font)
        ex_size_policy = QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
        ex_radio1.setSizePolicy(ex_size_policy)
        ex_radio2.setSizePolicy(ex_size_policy)
        ex_layout.addWidget(ex_radio1)
        ex_layout.addWidget(ex_radio2)
        ex_layout.setStretch(0, 4)
        ex_layout.setStretch(1, 1)

        ex_widget.resize(300, 240)
        expected.drawPixmap(0, 0, ex_widget.grab())

        widget = QWidget()
        layout = QVBoxLayout(widget)
        radio1 = ScaledRadioButton('Lorem ipsum')
        radio2 = ScaledRadioButton('Lorem ipsum')
        size_policy = QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
        radio1.setSizePolicy(size_policy)
        radio2.setSizePolicy(size_policy)
        layout.addWidget(radio1)
        layout.addWidget(radio2)
        layout.setStretch(0, 4)
        layout.setStretch(1, 1)

        widget.resize(300, 240)

        actual.drawPixmap(0, 0, widget.grab())
Example #2
0
def test_icon(pixmap_differ: PixmapDiffer):
    actual: QPainter
    expected: QPainter
    with pixmap_differ.create_painters(
            300, 240, 'scaled_radio_button_icon') as (actual, expected):
        display = TicTacToeDisplay()
        icon = display.player1_icon
        display.close()
        ex_widget = QWidget()
        ex_layout = QVBoxLayout(ex_widget)
        ex_radio1 = QRadioButton()
        ex_radio2 = QRadioButton('Lorem ipsum')
        ex_radio1.setIcon(icon)
        ex_radio2.setIcon(icon)
        ex_font = ex_radio1.font()
        if ex_font.family() == 'Sans Serif':
            # Fonts are different on Travis CI.
            big_font_size = 93
            small_font_size = 22
            ex_radio1.setStyleSheet('QRadioButton::indicator {width: 93} '
                                    'QRadioButton {spacing: 46}')
            ex_radio2.setStyleSheet('QRadioButton::indicator {width: 22} '
                                    'QRadioButton {spacing: 11}')
        else:
            big_font_size = 93
            small_font_size = 25
            ex_radio1.setStyleSheet('QRadioButton::indicator {width: 93} '
                                    'QRadioButton {spacing: 46}')
            ex_radio2.setStyleSheet('QRadioButton::indicator {width: 25} '
                                    'QRadioButton {spacing: 12}')
        ex_radio1.setIconSize(
            QSize(big_font_size * 3 // 2, big_font_size * 3 // 2))
        ex_radio2.setIconSize(
            QSize(small_font_size * 3 // 2, small_font_size * 3 // 2))
        ex_font.setPointSize(big_font_size)
        ex_radio1.setFont(ex_font)
        ex_font.setPointSize(small_font_size)
        ex_radio2.setFont(ex_font)
        ex_size_policy = QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
        ex_radio1.setSizePolicy(ex_size_policy)
        ex_radio2.setSizePolicy(ex_size_policy)
        ex_layout.addWidget(ex_radio1)
        ex_layout.addWidget(ex_radio2)
        ex_layout.setStretch(0, 4)
        ex_layout.setStretch(1, 1)

        ex_widget.resize(300, 240)
        expected.drawPixmap(0, 0, ex_widget.grab())

        widget = QWidget()
        layout = QVBoxLayout(widget)
        radio1 = ScaledRadioButton()
        radio2 = ScaledRadioButton('Lorem ipsum')
        radio1.setIcon(icon)
        radio2.setIcon(icon)
        size_policy = QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
        radio1.setSizePolicy(size_policy)
        radio2.setSizePolicy(size_policy)
        layout.addWidget(radio1)
        layout.addWidget(radio2)
        layout.setStretch(0, 4)
        layout.setStretch(1, 1)

        widget.resize(300, 240)

        actual.drawPixmap(0, 0, widget.grab())
class WriteW(QWidget):
    def __init__(self, parent, usbif):
        super().__init__(parent)

        self._usbif = usbif
        self._time_switches = {}
        self._pulse_switches = {}
        self._mode = WRITE_W_POSITIONS['ALL']

        self._setup_ui()

        usbif.send(um.WriteControlWriteW(mode=um.WriteWMode.ALL, s1_s2=0))

        z = (0, ) * len(TIME_SWITCHES)
        usbif.send(um.WriteControlTimeSwitches(*z))
        z = (0, ) * len(PULSE_SWITCHES)
        usbif.send(um.WriteControlPulseSwitches(*z))

    def _update_mode(self, mode):
        self._mode = mode
        self._send_mode(self._s2.isChecked())

    def _send_mode(self, s1_s2):
        self._usbif.send(um.WriteControlWriteW(mode=self._mode, s1_s2=s1_s2))

    def _send_times(self, state):
        time_states = {
            s: self._time_switches[s].isChecked()
            for s in self._time_switches.keys()
        }
        self._usbif.send(um.WriteControlTimeSwitches(**time_states))

    def _send_pulses(self, state):
        pulse_states = {
            s: self._pulse_switches[s].isChecked()
            for s in self._pulse_switches.keys()
        }
        self._usbif.send(um.WriteControlPulseSwitches(**pulse_states))

    def _setup_ui(self):
        layout = QVBoxLayout(self)
        self.setLayout(layout)
        layout.setMargin(1)
        layout.setSpacing(1)

        s1_s2 = QWidget(self)
        s1_s2_layout = QHBoxLayout(s1_s2)
        s1_s2.setLayout(s1_s2_layout)
        s1_s2_layout.setMargin(1)
        s1_s2_layout.setSpacing(0)
        self._s1 = QRadioButton('S1', s1_s2)
        font = self._s1.font()
        font.setPointSize(7)
        font.setBold(True)
        self._s1.setFont(font)
        self._s1.setLayoutDirection(Qt.RightToLeft)
        self._s1.setChecked(True)
        self._s2 = QRadioButton('S2', s1_s2)
        self._s2.setFont(font)
        self._s2.toggled.connect(lambda s: self._send_mode(s))
        s1_s2_layout.addWidget(self._s1)
        s1_s2_layout.setAlignment(self._s1, Qt.AlignLeft)
        s1_s2_layout.addWidget(self._s2)
        s1_s2_layout.setAlignment(self._s2, Qt.AlignLeft)

        layout.addWidget(s1_s2)
        layout.setAlignment(s1_s2, Qt.AlignRight)

        write_w_box = QGroupBox('WRITE W', self)
        write_w_layout = QGridLayout(write_w_box)
        write_w_box.setLayout(write_w_layout)
        write_w_layout.setMargin(1)
        write_w_layout.setSpacing(1)

        row = 0
        col = 0
        for label, mode in WRITE_W_POSITIONS.items():
            pos = QRadioButton(label, write_w_box)
            pos.setFont(font)
            if label == 'ALL':
                pos.setChecked(True)
            pos.pressed.connect(lambda m=mode: self._update_mode(m))
            write_w_layout.addWidget(pos, row, col)
            col += 1
            if row == 0 or col >= 3:
                col = 0
                row += 1

        layout.addWidget(write_w_box)

        switch_frame = QFrame(self)
        switch_frame.setFrameStyle(QFrame.StyledPanel | QFrame.Raised)
        switch_layout = QGridLayout(switch_frame)
        switch_layout.setMargin(1)
        switch_layout.setSpacing(1)
        switch_frame.setLayout(switch_layout)

        layout.addWidget(switch_frame)

        row = self._add_switches(switch_frame, switch_layout, TIME_SWITCHES,
                                 self._time_switches, self._send_times, row)
        sep = QFrame(switch_frame)
        sep.setFrameStyle(QFrame.HLine | QFrame.Raised)
        switch_layout.addWidget(sep, row, 0, 1, 6)
        row += 1

        self._add_switches(switch_frame, switch_layout, PULSE_SWITCHES,
                           self._pulse_switches, self._send_pulses, row)

    def _add_switches(self, switch_frame, switch_layout, switches, switch_dict,
                      switch_fn, row):
        col = 0
        for v, l in switches.items():
            check = QCheckBox(switch_frame)
            check.setFixedSize(20, 20)
            check.setStyleSheet(
                'QCheckBox::indicator{subcontrol-position:center;}')
            check.stateChanged.connect(switch_fn)
            switch_dict[v] = check
            switch_layout.addWidget(check, row, col)
            switch_layout.setAlignment(check, Qt.AlignCenter)

            label = QLabel(l, switch_frame)
            font = label.font()
            font.setPointSize(7)
            font.setBold(True)
            label.setFont(font)
            switch_layout.addWidget(label, row + 1, col)
            switch_layout.setAlignment(label, Qt.AlignCenter)

            col += 1
            if col >= 6:
                col = 0
                row += 2

        return row