class Country(object): ''' ISO 3166 Country Class Holds information about a Country including its name, ISO 3166 code, and a Qt Image of the country flag, if the resource is available. The flag image should reside in the resource /flags/xx.png, where xx is the two- character lower-case country code. ''' def __init__(self, code): self.code = code.upper() self._flag = None self._icon = None def __unicode__(self): return unicode(self.code or u'') def __eq__(self, other): return unicode(self) == unicode(other) def __ne__(self, other): return not self.__eq__(other) def __cmp__(self, other): return cmp(unicode(self), unicode(other)) def __hash__(self): return hash(unicode(self)) def __repr__(self): return "%s(code=%r)" % (self.__class__.__name__, unicode(self)) def __nonzero__(self): return bool(self.code) def __len__(self): return len(unicode(self)) @property def name(self): # Local import so the countries aren't loaded unless they are needed. from countries import COUNTRIES for code, name in COUNTRIES: if self.code.upper() == code.upper(): return name return '' @property def flag(self): # Load flag image from resource if not self._flag: from PySide.QtGui import QImage self._flag = QImage(':/flags/%s.png' % self.code.lower()) return self._flag if not self._flag.isNull() else None @property def icon(self): # Load flag icon from resource from PySide.QtGui import QIcon return QIcon(':/flags/%s.png' % self.code.lower())
def requestImage(self, id, size, requestedSize): key = (id, requestedSize) if key in self._cache: return self._cache[key] cover_file, cover_url, podcast_url, podcast_title = ( urllib.unquote(x) for x in id.split('|')) def get_filename(): return self.downloader.get_cover(cover_file, cover_url, podcast_url, podcast_title, None, None, True) filename = get_filename() image = QImage() if not image.load(filename): if filename.startswith(cover_file): logger.info('Deleting broken cover art: %s', filename) util.delete_file(filename) image.load(get_filename()) if not image.isNull(): self._cache[key] = image.scaled(requestedSize, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation) return self._cache[key]
def requestImage(self, id, size, requestedSize): key = (id, requestedSize) if key in self._cache: return self._cache[key] cover_file, cover_url, podcast_url, podcast_title = id.split('|') def get_filename(): return self.downloader.get_cover(cover_file, cover_url, podcast_url, podcast_title, None, None, True) filename = get_filename() image = QImage() if not image.load(filename): if filename.startswith(cover_file): logger.info('Deleting broken cover art: %s', filename) util.delete_file(filename) image.load(get_filename()) if not image.isNull(): self._cache[key] = image.scaled(requestedSize, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation) return self._cache[key]
def requestImage(self, name, rsize, size): """@reimp @public @param[in] providerId unicode unused @param[out] rsize QSize @param[in] size QSize @return QImage not None virtual QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) """ ret = QImage(rc.image_path(name)) if ret.isNull(): derror("failed to load image: '%s'" % name) elif ret.size() != size: ret = ( ret.scaled(size, Qt.KeepAspectRatio, Qt.SmoothTransformation) if not size.isEmpty() else ret.scaledToWidth( size.width(), Qt.SmoothTransformation) if size.width() > 0 else ret.scaledToHeight(size.height(), Qt.SmoothTransformation) if size.height() > 0 else ret) rsize.setWidth(ret.width()) rsize.setHeight(ret.height()) return ret
def image_from_hbitmap(hdc, bitmap, width, height): bitmap_info = BITMAPINFO() memset(byref(bitmap_info), 0, sizeof(bitmap_info)) bitmap_info.bmiHeader.biSize = sizeof(bitmap_info.bmiHeader) bitmap_info.bmiHeader.biWidth = width bitmap_info.bmiHeader.biHeight = -height bitmap_info.bmiHeader.biPlanes = 1 bitmap_info.bmiHeader.biBitCount = 32 bitmap_info.bmiHeader.biCompression = BI_RGB bitmap_info.bmiHeader.biSizeImage = width * height * 4 image = QImage(width, height, QImage.Format_ARGB32_Premultiplied) if image.isNull(): return image data = (c_ubyte * bitmap_info.bmiHeader.biSizeImage)() if not GetDIBits(hdc, bitmap, 0, height, data, byref(bitmap_info), DIB_RGB_COLORS): raise WindowsError('call to GetDIBits failed') bytes_per_line = image.bytesPerLine() for y in xrange(0, height): offset = y * bytes_per_line line = image.scanLine(y) for x in xrange(0, bytes_per_line): line[x] = chr(data[offset + x]) return image
def qt_fromWinHBITMAP(hdc, h_bitmap, w, h): """ Original: static QImage qt_fromWinHBITMAP(HDC hdc, HBITMAP bitmap, int w, int h) { BITMAPINFO bmi; memset(&bmi, 0, sizeof(bmi)); bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = w; bmi.bmiHeader.biHeight = -h; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; bmi.bmiHeader.biCompression = BI_RGB; bmi.bmiHeader.biSizeImage = w * h * 4; QImage image(w, h, QImage::Format_ARGB32_Premultiplied); if (image.isNull()) return image; // Get bitmap bits uchar *data = (uchar *) qMalloc(bmi.bmiHeader.biSizeImage); if (GetDIBits(hdc, bitmap, 0, h, data, &bmi, DIB_RGB_COLORS)) { // Create image and copy data into image. for (int y=0; y<h; ++y) { void *dest = (void *) image.scanLine(y); void *src = data + y * image.bytesPerLine(); memcpy(dest, src, image.bytesPerLine()); } } else { qWarning("qt_fromWinHBITMAP(), failed to get bitmap bits"); } qFree(data); return image; } """ import ctypes GetDIBits = ctypes.windll.gdi32.GetDIBits DIB_RGB_COLORS = 0 BI_RGB = 0 bitmapInfo = BITMAPINFO() bitmapInfo.bmiHeader.biSize = ctypes.sizeof(BITMAPINFOHEADER) bitmapInfo.bmiHeader.biWidth = w bitmapInfo.bmiHeader.biHeight = -h bitmapInfo.bmiHeader.biPlanes = 1 bitmapInfo.bmiHeader.biBitCount = 32 bitmapInfo.bmiHeader.biCompression = BI_RGB bitmapInfo.bmiHeader.biSizeImage = w * h * 4 from PySide.QtGui import QImage image = QImage(w, h, QImage.Format_ARGB32_Premultiplied) if image.isNull(): return image # Get bitmap bits data = ctypes.create_string_buffer(bitmapInfo.bmiHeader.biSizeImage) if GetDIBits(hdc, h_bitmap, 0, h, ctypes.byref(data), ctypes.byref(bitmapInfo), DIB_RGB_COLORS): # Create image and copy data into image. for y in range(h): dest = image.scanLine(y) src = data[y * image.bytesPerLine():y * image.bytesPerLine() + image.bytesPerLine()] for i in range(image.bytesPerLine()): dest[i] = src[i] else: # qWarning("qt_fromWinHBITMAP(), failed to get bitmap bits"); print("qt_fromWinHBITMAP(), failed to get bitmap bits") return image