QIcon get_file_icon(const QString& content_type) { static QCache<QString, QIcon> cache; cache.setMaxCost(100); if (cache.contains(content_type)) return QIcon(*(cache.object(content_type))); QByteArray ba = content_type.toLocal8Bit(); GIcon* gicon = g_content_type_get_icon(ba.constData()); QStringList tried_names; if (G_IS_THEMED_ICON(gicon)) { const gchar * const * names = g_themed_icon_get_names(reinterpret_cast<GThemedIcon*>(gicon)); if (names != 0) { int i = 0; while(names[i] != 0) { QString name = QString::fromLocal8Bit(names[i]); QIcon r = QIcon::fromTheme(name); if (r.isNull()) { tried_names << name; } else { cache.insert(content_type, new QIcon(r)); g_object_unref(gicon); return r; } i++; } } else { qDebug() << "get_file_icon: empty or invalid result of g_themed_icon_get_names"; } } else { qDebug() << "get_file_icon: gicon is not themed icon"; } qDebug() << "get_file_icon(" << content_type << "): no valid names" << tried_names << "; returning null icon"; cache.insert(content_type, new QIcon()); g_object_unref(gicon); return QIcon(); }
void tst_QCache::axioms_on_key_type() { QCache<KeyType, ValueType> foo; foo.setMaxCost(1); foo.clear(); foo.insert(KeyType(123), new ValueType(123)); foo.object(KeyType(123)); foo.contains(KeyType(456)); foo[KeyType(456)]; foo.remove(KeyType(456)); foo.remove(KeyType(123)); foo.take(KeyType(789)); // If this fails, contact the maintaner QVERIFY(sizeof(QHash<int, int>) == sizeof(void *)); }
QPixmap * getPixmapForCountryCode( const QString & countryCode ) { if( countryCode.isEmpty() ) return NULL; QString sCode( countryCode.toLower() ); QPixmap * pm = NULL; pm = pPixmapCache->object(sCode); if( !pm ) { QString sFlag( KStandardDirs::locate("locale", sFlagTemplate.arg(sCode)) ); if( !sFlag.isEmpty() ) { pm = new QPixmap(sFlag); pPixmapCache->insert( sCode, pm ); } } return pm; }
void GtkStyle::drawSelection(const QStyleOptionViewItemV4 &opt, QPainter *painter, double opacity) { static const int constMaxDimension=32; static QCache<QString, QPixmap> cache(30000); if (opt.rect.width()<2 || opt.rect.height()<2) { return; } int width=qMin(constMaxDimension, opt.rect.width()); QString key=QString::number(width)+QChar(':')+QString::number(opt.rect.height()); QPixmap *pix=cache.object(key); if (!pix) { pix=new QPixmap(width, opt.rect.height()); QStyleOptionViewItemV4 styleOpt(opt); pix->fill(Qt::transparent); QPainter p(pix); styleOpt.state=opt.state; styleOpt.state&=~(QStyle::State_Selected|QStyle::State_MouseOver); styleOpt.state|=QStyle::State_Selected|QStyle::State_Enabled|QStyle::State_Active; styleOpt.viewItemPosition = QStyleOptionViewItemV4::OnlyOne; styleOpt.rect=QRect(0, 0, opt.rect.width(), opt.rect.height()); QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &styleOpt, &p, 0); p.end(); cache.insert(key, pix, pix->width()*pix->height()); } double opacityB4=painter->opacity(); painter->setOpacity(opacity); if (opt.rect.width()>pix->width()) { int half=qMin(opt.rect.width()>>1, pix->width()>>1); painter->drawPixmap(opt.rect.x(), opt.rect.y(), pix->copy(0, 0, half, pix->height())); if ((half*2)!=opt.rect.width()) { painter->drawTiledPixmap(opt.rect.x()+half, opt.rect.y(), (opt.rect.width()-((2*half))), opt.rect.height(), pix->copy(half-1, 0, 1, pix->height())); } painter->drawPixmap((opt.rect.x()+opt.rect.width())-half, opt.rect.y(), pix->copy(half, 0, half, pix->height())); } else {