def paintEvent(self, event): # noinspection PyNoneFunctionAssignment images = self.images() size = self.size() w = size.width() h = size.height() painter = QPainter(self) painter.drawImage(QPoint(-10, -10), images.nw.qimage) painter.drawImage(QRect(30, -10, w - 60, -10), images.n.qimage) painter.drawImage(QPoint(w - 30, -10), images.ne.qimage) painter.drawImage(QRect(w - 10, 30, -10, h - 60), images.e.qimage) painter.drawImage(QPoint(w - 30, h - 30), images.se.qimage) painter.drawImage(QRect(30, h - 10, w - 60, -10), images.s.qimage) painter.drawImage(QPoint(-10, h - 30), images.sw.qimage) painter.drawImage(QRect(-10, 30, -10, h - 60), images.w.qimage)
def paintEvent(self, event): # noinspection PyNoneFunctionAssignment images = self.images() w = self.width() h = self.height() from fsui.qt import QPainter, QPoint, QRect painter = QPainter(self) painter.drawImage(QPoint(0, 0), images.nw.qimage) painter.drawImage(QRect(40, 0, w - 80, 20), images.n.qimage) painter.drawImage(QPoint(w - 40, 0), images.ne.qimage) painter.drawImage(QRect(w - 20, 40, 20, h - 80), images.e.qimage) painter.drawImage(QPoint(w - 40, h - 40), images.se.qimage) painter.drawImage(QRect(40, h - 20, w - 80, 20), images.s.qimage) painter.drawImage(QPoint(0, h - 40), images.sw.qimage) painter.drawImage(QRect(0, 40, 20, h - 80), images.w.qimage)
def _pngicon_image(self, index): print("_pngicon_image", index) from fsui.qt import QImage, QPoint dark = False if index == 1 and len(self.parser.png_images) == 1: data = self.parser.png_images[0] dark = True elif index < len(self.parser.png_images): data = self.parser.png_images[index] else: return self._missing_icon() image = QImage() # print("load from data", data) image.loadFromData(data, "PNG") print(image.size().width(), image.size().height()) # FIXME: dark if dark: for y in range(image.height()): for x in range(image.width()): point = QPoint(x, y) color = image.pixelColor(point) color.setRed(color.red() * 0.67) color.setGreen(color.green() * 0.67) color.setBlue(color.blue() * 0.67) image.setPixelColor(point, color) return ShellIconImage(image)
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)
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())
def is_mouse_over(self): # noinspection PyArgumentList c = QCursor.pos() # noinspection PyUnresolvedReferences p = self.mapToGlobal(QPoint(0, 0)) s = self.get_size() if p.x() <= c.x() < p.x() + s[0] and p.y() <= c.y() < p.y() + s[1]: return True return False
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
def popup_menu(self, menu, pos=(0, 0), blocking=True): # popup does not block, and if menu goes out of the scope of the # caller, it will disappear (unless we keep a reference here # FIXME: using exec now # self.__last_popup_menu = menu widget = getattr(self, "_widget", self) global_pos = widget.mapToGlobal(QPoint(pos[0], pos[1])) menu.set_parent(self) if blocking: menu.qmenu.exec_(global_pos) else: menu.qmenu.popup(global_pos)
def mousePressEvent(self, event): parent = self._parent() # noinspection PyArgumentList c = QCursor.pos() p = parent.widget().mapToGlobal(QPoint(0, 0)) s = parent.get_size() if p.x() <= c.x() < p.x() + s[0] and p.y() <= c.y() < p.y() + s[1]: # We want to close the menu (and not re-open it) if we click # on the widget used to open the menu. # parent._ignore_next_left_down_event = True pass super().mousePressEvent(event)
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
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)
def draw_image(self, image, x, y): # self.dc.DrawBitmap(image.bitmap, x, y, True) self.qpainter.drawImage(QPoint(x, y), image.qimage)