예제 #1
0
파일: test_button.py 프로젝트: MSLNZ/msl-qt
def test_icon():
    path = os.path.dirname(__file__) + '/gamma.png'
    gamma_size = QtCore.QSize(191, 291)

    int_val = QtWidgets.QStyle.SP_DriveNetIcon
    icon = convert.to_qicon(int_val)
    sizes = icon.availableSizes()
    if sys.platform == 'win32':
        assert len(sizes) > 1

    b = Button(icon=int_val)
    assert b.text() == ''
    assert not b.icon().isNull()
    assert b.iconSize() == sizes[0]
    assert b.toolButtonStyle() == Qt.ToolButtonIconOnly

    b = Button(icon=path)
    assert b.text() == ''
    assert not b.icon().isNull()
    assert b.iconSize() == gamma_size
    assert b.toolButtonStyle() == Qt.ToolButtonIconOnly

    b = Button(icon=convert.icon_to_base64(convert.to_qicon(path)))
    assert b.text() == ''
    assert not b.icon().isNull()
    assert b.iconSize() == gamma_size
    assert b.toolButtonStyle() == Qt.ToolButtonIconOnly
예제 #2
0
    def add_qt_tab(self, label, icons):
        """Add the Qt icons."""
        tab = QtWidgets.QWidget()
        self.tab_widget.addTab(tab, label)

        layout = QtWidgets.QGridLayout()

        self.update_message('Loading Qt icons...')

        count = 0
        num_cols = 4
        for i in icons:
            button = QtWidgets.QPushButton(i)
            ico = convert.to_qicon(getattr(QtWidgets.QStyle, i))
            button.setIcon(ico)
            button.clicked.connect(lambda *args, ic=ico, n=i: self.zoom(ic, n))

            layout.addWidget(button, count // num_cols, count % num_cols)
            count += 1
            self.num_icons += 1

        tab.setLayout(layout)

        self.file_index += 1
        self.progress_bar.setValue(self.file_index)
예제 #3
0
def test_icon_to_base64():
    # reading from a standard Qt icon does not raise any errors
    assert isinstance(
        convert.icon_to_base64(QtWidgets.QStyle.SP_TitleBarMenuButton),
        QtCore.QByteArray)

    # reading from a file does not raise any errors
    icon = convert.to_qicon('gamma.png')
    assert isinstance(convert.icon_to_base64(icon), QtCore.QByteArray)
    assert convert.icon_to_base64(icon).startsWith(b'iVBORw0KGgoAAAA')
    assert convert.icon_to_base64(icon,
                                  fmt='PNG').startsWith(b'iVBORw0KGgoAAAA')
    assert convert.icon_to_base64(icon,
                                  fmt='BMP').startsWith(b'Qk32jgIAAAAAADY')
    assert convert.icon_to_base64(icon,
                                  fmt='JPG').startsWith(b'/9j/4AAQSkZJRgA')
    assert convert.icon_to_base64(icon,
                                  fmt='JPEG').startsWith(b'/9j/4AAQSkZJRgA')

    # GIF is not supported
    with pytest.raises(ValueError):
        convert.icon_to_base64(icon, fmt='GIF')

    # the size of 'gamma.png'
    size = QtCore.QSize(191, 291)

    # convert back to a QIcon and check each pixel
    original = icon.pixmap(size).toImage()
    converted = convert.to_qicon(
        convert.icon_to_base64(icon)).pixmap(size).toImage()
    for x in range(0, size.width()):
        for y in range(0, size.height()):
            rgb_original = original.pixel(x, y)
            rgb_converted = converted.pixel(x, y)
            assert QtGui.QColor(rgb_original).getRgb() == QtGui.QColor(
                rgb_converted).getRgb()
예제 #4
0
    def add_windows_tab(self):
        """Add the icons from the Windows DLL and EXE files."""
        num_cols = 16
        filename = self.windows_files[self.windows_index]
        self.update_message('Loading icons from {}...'.format(filename))

        tab = QtWidgets.QWidget()
        self.tab_widget.addTab(tab, filename)

        layout = QtWidgets.QGridLayout()

        index = 0
        while True:
            button = QtWidgets.QPushButton(str(index))
            try:
                name = '{}|{}'.format(filename, str(index))
                ico = convert.to_qicon(name)
            except OSError:
                break

            button.setIcon(ico)
            button.clicked.connect(
                lambda *args, ic=ico, n=name: self.zoom(ic, n))
            layout.addWidget(button, index // num_cols, index % num_cols)
            index += 1
            self.num_icons += 1

        self.file_index += 1
        self.progress_bar.setValue(self.file_index)

        tab.setLayout(layout)

        self.windows_index += 1
        if self.windows_index == len(self.windows_files):
            self.timer.stop()
            self.update_message('Loaded {} icons.'.format(self.num_icons))
            self.progress_bar.hide()
예제 #5
0
파일: test_button.py 프로젝트: MSLNZ/msl-qt
def test_icon_size():
    int_val = QtWidgets.QStyle.SP_DriveNetIcon
    icon = convert.to_qicon(int_val)
    sizes = icon.availableSizes()
    if sys.platform == 'win32':
        assert len(sizes) > 1

    #
    # specify the size to the get_icon function
    #

    b = Button(icon=convert.to_qicon(int_val))
    assert b.text() == ''
    assert b.toolButtonStyle() == Qt.ToolButtonIconOnly
    assert b.iconSize() == sizes[0]

    b = Button(icon=convert.to_qicon(int_val, size=789))
    assert b.iconSize() == QtCore.QSize(789, 789)

    b = Button(icon=convert.to_qicon(int_val, size=3.0))
    # specifying a scale factor will use the largest available size
    assert b.iconSize() == QtCore.QSize(3 * sizes[-1].width(),
                                        3 * sizes[-1].height())

    b = Button(icon=convert.to_qicon(int_val, size=QtCore.QSize(50, 50)))
    assert b.iconSize() == QtCore.QSize(50, 50)

    for size in [(256, ), (256, 256, 256)]:
        with pytest.raises(ValueError, match='(width, height)'):
            Button(icon=convert.to_qicon(int_val, size=size))

    #
    # use the icon_size kwarg
    #

    b = Button(icon=convert.to_qicon(int_val), icon_size=1234)
    assert b.iconSize() == QtCore.QSize(1234, 1234)

    b = Button(icon=convert.to_qicon(int_val), icon_size=3.0)
    # specifying a scale factor will use the largest available size
    assert b.iconSize() == QtCore.QSize(3 * sizes[-1].width(),
                                        3 * sizes[-1].height())

    b = Button(icon=convert.to_qicon(int_val), icon_size=(312, 312))
    assert b.iconSize() == QtCore.QSize(312, 312)

    b = Button(icon=convert.to_qicon(int_val),
               icon_size=QtCore.QSize(500, 500))
    assert b.iconSize() == QtCore.QSize(500, 500)

    for size in [(256, ), (256, 256, 256)]:
        with pytest.raises(ValueError, match='(width, height)'):
            Button(icon=convert.to_qicon(int_val), icon_size=size)
예제 #6
0
def test_to_qicon_dll_exe():
    assert isinstance(convert.to_qicon('C:/Windows/System32/shell32.dll|0'),
                      QtGui.QIcon)
    assert isinstance(convert.to_qicon('shell32.dll|0'), QtGui.QIcon)
    assert isinstance(convert.to_qicon('shell32|0'), QtGui.QIcon)
    with pytest.raises(OSError):
        convert.to_qicon(
            '/shell32|0'
        )  # fails because it appears as though a full path is being specified
    assert isinstance(convert.to_qicon('C:/Windows/explorer.exe|0'),
                      QtGui.QIcon)
    assert isinstance(convert.to_qicon('explorer.exe|0'), QtGui.QIcon)
    assert isinstance(convert.to_qicon('explorer|0'), QtGui.QIcon)
    if sys.maxsize > 2**32:
        # the exe is located at 'C:/Windows/explorer.exe'
        with pytest.raises(OSError):
            convert.to_qicon('C:/Windows/System32/explorer.exe|0')
    else:
        assert isinstance(
            convert.to_qicon('C:/Windows/System32/explorer.exe|0'),
            QtGui.QIcon)
    with pytest.raises(OSError, match=r'Requested icon 9999'):
        convert.to_qicon(
            'shell32|9999'
        )  # the maximum icon index should be much less than 9999
예제 #7
0
def test_to_qicon():

    assert isinstance(convert.to_qicon(QtGui.QIcon()), QtGui.QIcon)
    assert isinstance(convert.to_qicon(QtGui.QPixmap()), QtGui.QIcon)
    assert isinstance(convert.to_qicon(QtGui.QImage()), QtGui.QIcon)
    assert isinstance(convert.to_qicon(QtWidgets.QStyle.SP_TitleBarMenuButton),
                      QtGui.QIcon)
    assert isinstance(convert.to_qicon(14), QtGui.QIcon)

    base64 = 'iVBORw0KGgoAAAANSUhEUgAAACgAAABCCAYAAAAlkZRRAAAACXBIWXMAAAsTAAALEwEAmpwYAAABpElEQVR' \
             'oge2ZT07CQBSHf29kLXKOsnApBEx7DAl3sT2BZzAx6C1ahYBLFzRxyQ1Q12SeiwoSQonjzMiLmW9Fh+nMlz' \
             'ft/HkFhEOuGnofRLz+3RyVztpVrhryhXjBhu8OlsN2DADQ/NYalS+m93sXVJpzACBGASAxvt+1kGuCoC3On' \
             'kGXc9824iMYBG0JgrZ4X0lMWe+KtKKkdTcvQgT3sRy2Y6URr6+bo3laV/cogpUcX28VpbV1vdtY4iyCYcsv' \
             'lSBoi7j94G474iMoXjDMg7YEQVvEC4rPDxq/xctBdH7CuAGA0/vSOBlkivk0o+iMNcfuVWq6+6uOfot4wdo' \
             'h/riKcqbqYOMrMfQTxEdQvKC4/eAu4iMYBG0RL9gAthd6yg4lco6B+AiKFzSfB1erBVQj8+CyF2PB1sPrAg' \
             'fyea4RP8TiBb+GmDIA0ArF5h/CLUCPx5AKuISAsJJYIV4w8O8hAOj2O9VbTJRNn6YpAHT6nZxQnYun49nmQ' \
             'NTrXcSaKN8t37RRU85AMRvPEgDoXnZT8Pe3un31FXMymTzL/xwrXlA8n2MHdwPYAbB5AAAAAElFTkSuQmCC'

    assert isinstance(convert.to_qicon(QtCore.QByteArray(base64.encode())),
                      QtGui.QIcon)
    assert isinstance(convert.to_qicon(bytearray(base64.encode())),
                      QtGui.QIcon)

    default_size = convert.to_qicon(
        QtWidgets.QStyle.SP_TitleBarMenuButton).availableSizes()[-1]
    assert isinstance(default_size, QtCore.QSize)

    with pytest.raises(TypeError):
        convert.to_qicon(None)

    with pytest.raises(OSError):
        convert.to_qicon(99999)  # not a valid QStyle.StandardPixmap enum value

    with pytest.raises(OSError):
        convert.to_qicon('this is not an icon')

    assert isinstance(
        convert.to_qicon(os.path.join(os.path.dirname(__file__), 'gamma.png')),
        QtGui.QIcon)

    # insert this directory into sys.path and check again
    sys.path.insert(0, os.path.dirname(__file__))
    assert isinstance(convert.to_qicon('gamma.png'), QtGui.QIcon)

    icon_1 = convert.to_qicon('gamma.png').availableSizes()[0]
    icon_2 = convert.to_qicon('gamma.png', size=0.5).availableSizes()[0]
    assert int(icon_1.width() * 0.5) == icon_2.width()
    assert int(icon_1.height() * 0.5) == icon_2.height()
예제 #8
0
def test_rescale_icon():
    # the actual size of 'gamma.png'
    size = QtCore.QSize(191, 291)

    icon = convert.to_qicon('gamma.png')
    sizes = icon.availableSizes()
    assert len(sizes) == 1
    assert sizes[0].width() == size.width()
    assert sizes[0].height() == size.height()

    new_size = convert.rescale_icon(icon, 2.6).size()
    assert new_size.width() == int(size.width() * 2.6)
    assert new_size.height() == int(size.height() * 2.6)

    new_size = convert.rescale_icon(icon, 150).size()
    assert new_size.width() == int(
        (150. / float(size.height())) * size.width())
    assert new_size.height() == 150

    new_size = convert.rescale_icon(
        icon, 300, aspect_mode=Qt.KeepAspectRatioByExpanding).size()
    assert new_size.width() == 300
    assert new_size.height() == int(
        (300. / float(size.width())) * size.height())

    size2 = (150, 200)
    new_size = convert.rescale_icon(icon,
                                    size2,
                                    aspect_mode=Qt.IgnoreAspectRatio).size()
    assert new_size.width() == size2[0]
    assert new_size.height() == size2[1]
    new_size = convert.rescale_icon(icon, size2).size()
    assert new_size.width() == int(size2[1] * size.width() /
                                   float(size.height()))
    assert new_size.height() == size2[1]

    # passing different objects does not raise an error
    if sys.platform == 'win32' and has_pythonnet():
        assert isinstance(convert.rescale_icon('explorer|0', 1.0),
                          QtGui.QPixmap)
    assert isinstance(
        convert.rescale_icon(QtWidgets.QStyle.SP_TitleBarMenuButton, 1.0),
        QtGui.QPixmap)
    assert isinstance(convert.rescale_icon(25, 1.0), QtGui.QPixmap)
    assert isinstance(convert.rescale_icon(icon.pixmap(size), 1.0),
                      QtGui.QPixmap)
    assert isinstance(convert.rescale_icon(icon.pixmap(size).toImage(), 1.0),
                      QtGui.QPixmap)

    if sys.platform == 'win32' and has_pythonnet():
        with pytest.raises(TypeError, match=r'Unsupported "size"'):
            convert.rescale_icon('explorer|0', None)

    # if a list/tuple then must contain 2 elements
    for item in [(), [], (256, ), [
            256,
    ], (256, 256, 256), [256, 256, 256]]:
        with pytest.raises(
                ValueError,
                match=r'The size must be in the form \(width, height\)'):
            convert.rescale_icon(icon, item)