Ejemplo n.º 1
0
    def render(self, text, _, color):
        if self.font is None:
            return "", (0, 0)

        fm = QFontMetrics(self.font)
        rect = fm.boundingRect(text)
        im = QImage(
            rect.x() + rect.width(),
            rect.height(),
            QImage.Format_ARGB32_Premultiplied,
        )
        im.fill(QColor(0, 0, 0, 0))
        painter = QPainter()
        painter.begin(im)
        painter.setPen(QPen(QColor(*color)))
        painter.setFont(self.font)
        painter.drawText(QPoint(0 - rect.x(), 0 - rect.y()), text)
        painter.end()

        bits = im.bits()
        try:
            pixels = bits.tobytes()
        except AttributeError:
            bits.setsize(im.byteCount())
            pixels = bytes(bits)
        return pixels, (rect.x() + rect.width(), rect.height())
Ejemplo n.º 2
0
    def __init__(self, *args):
        QColor.__init__(self)

        if len(args) == 1:
            c = args[0]
            if isinstance(c, QColor):
                self.setRgb(c.red(), c.green(), c.blue(), c.alpha())
            elif isinstance(c, int):
                r = (c & 0xFF0000) >> 16
                g = (c & 0x00FF00) >> 8
                b = c & 0x0000FF
                self.setRgb(r, g, b)
            else:
                if len(c) == 3:
                    r, g, b = c
                    self.setRgb(r, g, b)
                else:
                    r, g, b, a = c
                    self.setRgba(r, g, b, a)
        elif len(args) == 3:
            self.setRgb(*args)
        elif len(args) == 4:
            self.setRgb(args[0], args[1], args[2])
            self.setAlpha(args[3])
        else:
            raise TypeError("Color object is not initialized")
Ejemplo n.º 3
0
def set_colors(
    palette: QPalette,
    role,
    normal: Union[QColor, str],
    disabled: Union[QColor, str] = None,
):
    palette.setColor(role, QColor(normal))
    palette.setColor(QPalette.Disabled, role, QColor(disabled))
Ejemplo n.º 4
0
        def pushbit(bit):
            parsedata.bits.append(bit)
            if parsedata.palette:
                if len(parsedata.bits) == 8:
                    # parsedata.color.append(bits_to_value(parsedata.bits[0:8]))
                    parsedata.color.append(bits_to_value(parsedata.bits))
                    # r = bits_to_value(parsedata.bits[0:8])
                    # g = bits_to_value(parsedata.bits[8:16])
                    # b = bits_to_value(parsedata.bits[16:24])
                    if len(parsedata.color) == 3:
                        # pylint: disable=unbalanced-tuple-unpacking
                        r, g, b = parsedata.color
                        a = 0xFF
                        print(
                            f"PALETTE{len(palette):02d} 0x{r:02x}{g:02x}{b:02x}"
                        )
                        if dark:
                            palette.append(
                                QColor(r * 0.67, g * 0.67, b * 0.67, a)
                            )
                        else:
                            palette.append(QColor(r, g, b, a))
                        parsedata.color = []
                    # FIXME: More efficient
                    # parsedata.bits = parsedata.bits[8:]
                    parsedata.bits.clear()

                    # if parsedata.palette and len(palette) >= colors:
                    #     parsedata.palette = False
            elif parsedata.done:
                pass
            else:
                if len(parsedata.bits) == bitdepth:
                    # palette_index = bits_to_value(parsedata.bits[0:bitdepth])
                    palette_index = bits_to_value(parsedata.bits)
                    # FIXME: Maybe only subtract 1 etc when transparency is True
                    # print("palette", palette_index)
                    # FIXME: Transparency... correct?
                    if palette_index == 0 and transparency:
                        color = QColor(0x00, 0x00, 0x00, 0x00)
                    elif palette_index >= len(palette):
                        color = QColor(0xFF, 0x00, 0x00, 0xFF)
                        print("WARNING, palette_index is", palette_index)
                    else:
                        color = palette[palette_index]
                    image.setPixelColor(
                        QPoint(parsedata.x, parsedata.y), color
                    )
                    # FIXME: More efficient
                    # parsedata.bits = parsedata.bits[bitdepth:]
                    parsedata.bits.clear()
                    parsedata.x += 1
                    if parsedata.x == width:
                        parsedata.y += 1
                        parsedata.x = 0
                        if parsedata.y == height:
                            parsedata.done = True
Ejemplo n.º 5
0
    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHints(QPainter.Antialiasing)
        pen = QPen(QColor(0x80, 0x80, 0x80))
        pen.setWidth(2)
        painter.setPen(pen)
        painter.setBrush(QBrush(Qt.white))
        x = 1
        y = 1
        w = self.width() - 2
        h = self.height() - 2

        rect = QRect(x, y, w, h)
        painter.drawEllipse(rect)

        cx = x + w / 2
        cy = y + h / 2
        a = w / 2 * 0.85
        b = h / 2 * 0.85

        pen.setWidth(0)
        painter.setPen(pen)
        painter.setBrush(QBrush(Qt.black))

        for i in range(12):
            px = cx + a * math.cos(2 * math.pi * i / 12)
            py = cy + b * math.sin(2 * math.pi * i / 12)
            painter.drawEllipse(px - 3, py - 3, 6, 6)

        hours, minutes, seconds = self.time[3:]
        minutes += seconds / 60.0
        hours += minutes / 60.0

        a = w / 2 * 0.6
        b = h / 2 * 0.6
        pen.setWidth(4)
        painter.setPen(pen)
        self.draw_hand_line(painter, w, h, cx, cy, a, b,
                            2.0 * math.pi * hours / 12)

        a = w / 2 * 0.8
        b = h / 2 * 0.8
        pen.setWidth(3)
        painter.setPen(pen)
        self.draw_hand_line(painter, w, h, cx, cy, a, b,
                            2.0 * math.pi * minutes / 60)

        pen = QPen(QColor(0xFF, 0x00, 0x00))
        pen.setWidth(2)
        painter.setPen(pen)
        self.draw_hand_line(painter, w, h, cx, cy, a, b,
                            2.0 * math.pi * seconds / 60)
Ejemplo n.º 6
0
    def _glowicon_image_argb(self, imagedata, width, height, dark):
        from fsui.qt import QColor, QImage, QPoint, QSize

        # print(repr(imagedata[:16]))
        # print(len(imagedata))

        zlibdata = imagedata[10:]
        data = zlib.decompress(zlibdata)
        offset = 0

        image = QImage(QSize(width, height), QImage.Format_RGBA8888)
        for y in range(height):
            for x in range(width):
                color = QColor(
                    data[offset + 1],
                    data[offset + 2],
                    data[offset + 3],
                    data[offset + 0],
                )
                image.setPixelColor(QPoint(x, y), color)
                offset += 4

        if dark:
            print("FIXME: Make dark version")

        return ShellIconImage(image)
Ejemplo n.º 7
0
 def draw_vertical_gradient(self, x, y, w, h, c1, c2):
     gradient = QLinearGradient(0, 0, 0, h)
     gradient.setColorAt(0.0, c1)
     gradient.setColorAt(1.0, c2)
     self.qpainter.setPen(QPen(QColor(0, 0, 0, 0)))
     self.qpainter.setBrush(QBrush(gradient))
     self.qpainter.drawRect(x, y, w, h)
Ejemplo n.º 8
0
    def __init__(self, *args):
        QColor.__init__(self)

        if len(args) == 1:
            c = args[0]
            if isinstance(c, QColor):
                self.setRgb(c.red(), c.green(), c.blue(), c.alpha())
            else:
                if len(c) == 3:
                    r, g, b = c
                    self.setRgb(r, g, b)
                else:
                    r, g, b, a = c
                    self.setRgba(r, g, b, a)
        elif len(args) == 3:
            self.setRgb(*args)
        elif len(args) == 4:
            self.setRgb(args[0], args[1], args[2])
            self.setAlpha(args[3])
        else:
            raise TypeError("Color object is not initialized")
Ejemplo n.º 9
0
 def pushentry(value):
     if parsedata.palette:
         parsedata.color.append(value)
         if len(parsedata.color) == 3:
             # pylint: disable=unbalanced-tuple-unpacking
             r, g, b = parsedata.color
             a = 0xFF
             print(f"PALETTE{len(palette):02d} 0x{r:02x}{g:02x}{b:02x}")
             if dark:
                 palette.append(QColor(r * 0.66, g * 0.66, b * 0.66, a))
             else:
                 palette.append(QColor(r, g, b, a))
             parsedata.color = []
         # FIXME: More efficient
         # parsedata.bits = parsedata.bits[8:]
         if parsedata.palette and len(palette) >= num_colors:
             parsedata.palette = False
     elif parsedata.done:
         pass
     else:
         palette_index = value
         # FIXME: Maybe only subtract 1 etc when transparency is True
         # print("palette", palette_index)
         # FIXME: Transparency... correct?
         if palette_index == transparent_color:
             color = QColor(0x00, 0x00, 0x00, 0x00)
         elif palette_index >= len(palette):
             color = QColor(0xFF, 0x00, 0x00, 0xFF)
             print("WARNING, palette_index is", palette_index)
         else:
             color = palette[palette_index]
         image.setPixelColor(QPoint(parsedata.x, parsedata.y), color)
         # FIXME: More efficient
         # parsedata.bits = parsedata.bits[bitdepth:]
         parsedata.x += 1
         if parsedata.x == width:
             parsedata.y += 1
             parsedata.x = 0
             if parsedata.y == height:
                 parsedata.done = True
Ejemplo n.º 10
0
 def rounded_rectangle(self,
                       x,
                       y,
                       w,
                       h,
                       c,
                       radius,
                       border_color=None,
                       border_width=1.0):
     if border_color is not None:
         self.qpainter.setPen(QPen(border_color, border_width))
     else:
         self.qpainter.setPen(QPen(QColor(0, 0, 0, 0)))
     self.qpainter.setBrush(QBrush(c))
     self.qpainter.drawRoundedRect(x, y, w, h, radius, radius)
Ejemplo n.º 11
0
    def _image(self, info):
        from fsui.qt import QColor, QImage, QPoint, QSize

        # width = self.uint16(offset + 4)
        # height = self.uint16(offset + 6)
        # bitdepth = self.uint16(offset + 8)
        # has_imagedata = self.uint32(offset + 10)

        if not "width" in info:
            return self._missing_icon()

        width = info["width"]
        height = info["height"]
        bitdepth = info["bitdepth"]
        data = info["data"]

        pitch = ((width + 15) >> 4) << 1
        planebytesize = pitch * height
        planes = []
        for i in range(bitdepth):
            planes.append(data[planebytesize * i : planebytesize * (i + 1)])

        height_factor = 1
        image = QImage(
            QSize(width, height * height_factor), QImage.Format_RGBA8888
        )

        # image.fill(0xFFFFFFFF)

        for y in range(height):
            for x in range(width):
                value = 0
                bytenum = y * pitch + x // 8
                bitnum = 7 - x % 8
                for plane in range(bitdepth):
                    value = (
                        value | (planes[plane][bytenum] >> bitnum & 1) << plane
                    )
                a = 0xFF
                if value == 0:
                    rgb = 0x959595
                    a = 0x00
                elif value == 1:
                    rgb = 0x000000
                elif value == 2:
                    rgb = 0xFFFFFF
                elif value == 3:
                    rgb = 0x3B67A2
                elif value == 4:
                    rgb = 0x7B7B7B
                elif value == 5:
                    rgb = 0xAFAFAF
                elif value == 6:
                    rgb = 0xAA907C
                elif value == 7:
                    rgb = 0xFFA997
                else:
                    # raise Exception("Unsupported color")
                    print(value)
                    color = 0xFF0000
                color = QColor(rgb >> 16, (rgb >> 8) & 0xFF, rgb & 0xFF, a)
                # if height_factor == 2:
                #     image.setPixelColor(QPoint(x, y * 2), color)
                #     image.setPixelColor(QPoint(x, y * 2 + 1), color)
                # else:
                image.setPixelColor(QPoint(x, y), color)

        return ShellIconImage(image)
Ejemplo n.º 12
0
 def draw_rectangle(self, x, y, w, h, c):
     self.qpainter.setPen(QPen(QColor(0, 0, 0, 0)))
     self.qpainter.setBrush(QBrush(c))
     self.qpainter.drawRect(x, y, w, h)
Ejemplo n.º 13
0
def initialize_qt_style(qapplication, theme):
    print("Available QT styles:", QStyleFactory.keys())
    qapplication.setStyle(QStyleFactory.create("Fusion"))

    pa = QPalette()
    # background = QColor("#f6f5f4")
    # color = fsui.Color(0xAEAEAE).lighten(0.065)

    # color = fsui.Color(0xAEAEAE).lighten(0.05)
    # background = QColor(color.to_hex())
    background = QColor("#C2C2C2")

    text = "#000000"
    text_disabled = "#777777"

    pa.setColor(QPalette.Window, background)
    pa.setColor(QPalette.AlternateBase, background)
    pa.setColor(QPalette.Button, background)

    pa.setColor(QPalette.Highlight, QColor(0x66, 0x88, 0xBB))

    set_colors(pa, QPalette.Base, "#E8E8E8", "#C0C0C0")

    set_colors(pa, QPalette.Text, text, text_disabled)
    set_colors(pa, QPalette.WindowText, text, text_disabled)
    set_colors(pa, QPalette.ButtonText, text, text_disabled)

    # pa.setColor(QPalette.Base, QColor(0xE8, 0xE8, 0xE8))
    # pa.setColor(QPalette.Disabled, QPalette.Base, QColor(0xC0, 0xC0, 0xC0))

    # pa.setColor(QPalette.Text, QColor(0xFF, 0x00, 0x00))
    # pa.setColor(QPalette.Disabled, QPalette.Text, QColor(0xFF, 0x00, 0x00))

    # Labels

    # pa.setColor(QPalette.WindowText, QColor(0x00, 0xFF, 0x00))
    # pa.setColor(
    #     QPalette.Disabled, QPalette.WindowText, QColor(0x00, 0x88, 0x00)
    # )

    # Buttons
    # pa.setColor(QPalette.ButtonText, QColor(0x00, 0xFF, 0x00))
    # pa.setColor(
    #     QPalette.Disabled, QPalette.ButtonText, QColor(0x00, 0x88, 0x00)
    # )

    # pa.setColor(QPalette.Base, QColor(0xEE, 0xEE, 0xEE))
    # pa.setColor(QPalette.Base, QColor(0xFF, 0xFF, 0xFF))
    # pa.setColor(QPalette.Disabled, QPalette.Text, QColor(0x66, 0x66, 0x66))

    # pa.setColor(QPalette.Mid, QColor(0xFF, 0x00, 0x00))

    # pa.setColor(QPalette.Window, QColor("#aeaeae"))
    # pa.setColor(QPalette.AlternateBase, QColor("#aeaeae"))
    # pa.setColor(QPalette.Button, QColor("#aeaeae"))

    qapplication.setPalette(pa)

    # pa.setColor(QPalette.Window, QColor(0x50, 0x50, 0x50))
    # pa.setColor(QPalette.WindowText, Qt.white)
    # pa.setColor(QPalette.Base, QColor(25, 25, 25))
    # pa.setColor(QPalette.AlternateBase, QColor(53, 53, 53))
    # pa.setColor(QPalette.ToolTipBase, Qt.white)
    # pa.setColor(QPalette.ToolTipText, Qt.white)
    # pa.setColor(QPalette.Text, Qt.white)
    # pa.setColor(QPalette.Button, QColor(0x58, 0x58, 0x58))
    # pa.setColor(QPalette.ButtonText, Qt.white)
    # pa.setColor(QPalette.BrightText, Qt.red)
    # pa.setColor(QPalette.Link, QColor(42, 130, 218))
    # pa.setColor(QPalette.Highlight, QColor(255, 0, 0))
    # pa.setColor(QPalette.HighlightedText, Qt.black)
    # qapplication.setPalette(pa)

    # Choice::
    #     QStandardItemModel::
    #     QComboBoxPrivateContainer::
    #         QBoxLayout::
    #         QComboBoxListView::
    #             QWidget::qt_scrollarea_viewport
    #             QWidget::qt_scrollarea_hcontainer
    #                 QScrollBar::
    #                 QBoxLayout::
    #             QWidget::qt_scrollarea_vcontainer
    #                 QScrollBar::
    #                 QBoxLayout::
    #             QItemSelectionModel::
    #             QComboMenuDelegate::
    #         QComboBoxPrivateScroller::
    #         QComboBoxPrivateScroller::

    # FIXME: Make stylesheet use scaling function for numbers

    qapplication.setStyleSheet(f"""
        QToolTip {{
            background-color: #efe4ab;
            border: 1px solid #958a62;
            padding-top: 4px;
            padding-right: 8px;
            padding-bottom: 4px;
            padding-left: 8px;
            padding: 0px;
            padding-top: 2px;
            padding-right: 4px;
            padding-bottom: 2px;
            padding-left: 4px;
/*
            color: #FFFFFF; background-color: #2A82DA;
            border: 1px solid white;
*/
        }}
        QPushButton {{
            padding-right: 12px;
            padding-left: 12px;
        }}
        QPushButton:disabled {{
            color: #777777;
        }}
        /*
        QLineEdit {{
            padding-top: {theme.textfield_padding().top}px;
            padding-right: {theme.textfield_padding().right}px;
            padding-left: {theme.textfield_padding().left}px;
            padding-bottom: {theme.textfield_padding().bottom}px;
        }}
        */
        /* Placeholder text. It will be made partially transparent by Qt */
        /* FIXME: This only works when initially showing the widget :( */
        /* TODO: Move color logic to fsui.TextField instead then... :( */
        /* DONE! */
        /*
        QLineEdit[text=""] {{
            color: #660000;
        }}
        QLineEdit[text!=""] {{
            color: #000000;
        }}
        */
        /*
        QSpinBox {{
            padding-top: 2px;
            padding-bottom: 2px;

            padding-right: 4px;
            padding-left: 4px;
        }}
        */
        QCheckBox {{
            spacing: 5px;
        }}
        QCheckBox::indicator {{
            width: 18px;
            height: 18px;
        }}
        """)

    # try:
    #     launcher_font_size = int(
    #         Settings.instance().get("launcher_font_size")
    #     )
    # except ValueError:
    #     if fusion_variant == "fws":
    #         launcher_font_size = 13
    #     else:
    #         launcher_font_size = 0
    # if launcher_font_size:
    #     print("FONT: Override size {}".format(launcher_font_size))

    # font.setPointSize(launcher_font_size)
    # if fusion_variant == "fws":
    #     font = QFont("Roboto")
    #     font.setPointSizeF(10.5)
    #     font.setHintingPreference(QFont.PreferNoHinting)
    # font.setHintingPreference(QFont.PreferNoHinting)

    # font = QFont("Saira Condensed", 16, QFont.Medium)
    font = QFont("Roboto", 15, QFont.Normal)
    font.setPixelSize(15)
    qapplication.setFont(font)