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::insert() { QCache<QString, Foo> cache; Foo *f1 = new Foo; cache.insert("one", f1, 1); QVERIFY(cache.contains("one")); Foo *f2 = new Foo; cache.insert("two", f2, 2); QVERIFY(cache.contains("two")); QCOMPARE(cache.size(), 2); Foo *f3 = new Foo; cache.insert("two", f3, 2); QVERIFY(cache.contains("two")); QCOMPARE(cache.size(), 2); QVERIFY(cache["two"] == f3); QCOMPARE(Foo::count, 2); /* If the new item is too big, any item with the same name in the cache must still be removed, otherwise the application might get bad results. */ Foo *f4 = new Foo; cache.insert("two", f4, 10000); QVERIFY(!cache.contains("two")); QCOMPARE(cache.size(), 1); QCOMPARE(Foo::count, 1); }
void tst_QCache::take() { QCache<QString, Foo> cache; QCOMPARE(cache.take(QString()), (Foo*)0); QCOMPARE(cache.take("alpha"), (Foo*)0); QVERIFY(cache.isEmpty()); Foo *f1 = new Foo; cache.insert("alpha", f1, 10); QCOMPARE(cache.size(), 1); QVERIFY(cache["alpha"] == f1); cache.insert("beta", new Foo, 20); QCOMPARE(cache.size(), 2); QCOMPARE(cache.take("alpha"), f1); QCOMPARE(cache.size(), 1); QCOMPARE(cache.totalCost(), 20); QCOMPARE(Foo::count, 2); delete f1; QCOMPARE(Foo::count, 1); QCOMPARE(cache.take("alpha"), (Foo*)0); QCOMPARE(Foo::count, 1); QCOMPARE(cache.size(), 1); QCOMPARE(cache.totalCost(), 20); cache.setMaxCost(1); QCOMPARE(cache.size(), 0); QCOMPARE(cache.take("beta"), (Foo*)0); QCOMPARE(cache.size(), 0); }
void tst_QCache::setMaxCost() { QCache<int, Foo> cache; cache.setMaxCost(2); cache.insert(1, new Foo); cache.insert(2, new Foo); QCOMPARE(cache.totalCost(), 2); QCOMPARE(Foo::count, 2); cache.insert(3, new Foo); QCOMPARE(cache.totalCost(), 2); QCOMPARE(Foo::count, 2); cache.setMaxCost(3); QCOMPARE(cache.totalCost(), 2); QCOMPARE(Foo::count, 2); cache.setMaxCost(2); QCOMPARE(cache.totalCost(), 2); QCOMPARE(Foo::count, 2); cache.setMaxCost(1); QCOMPARE(cache.totalCost(), 1); QCOMPARE(Foo::count, 1); cache.setMaxCost(0); QCOMPARE(cache.totalCost(), 0); QCOMPARE(Foo::count, 0); cache.setMaxCost(-1); QCOMPARE(cache.totalCost(), 0); QCOMPARE(Foo::count, 0); }
void tst_QCache::totalCost() { QCache<QString, int> cache; QCOMPARE(cache.totalCost(), 0); cache.insert("one", new int(1), 0); QCOMPARE(cache.totalCost(), 0); cache.insert("two", new int(2), 1); QCOMPARE(cache.totalCost(), 1); cache.insert("three", new int(3), 2); QCOMPARE(cache.totalCost(), 3); cache.insert("four", new int(4), 10000); QCOMPARE(cache.totalCost(), 3); QVERIFY(!cache.contains("four")); cache.insert("five", new int(5), -5); QCOMPARE(cache.totalCost(), -2); cache.insert("six", new int(6), 101); QCOMPARE(cache.totalCost(), -2); cache.insert("seven", new int(7), 100); QCOMPARE(cache.totalCost(), 98); QCOMPARE(cache.size(), 5); cache.insert("eight", new int(8), 2); QCOMPARE(cache.totalCost(), 100); QCOMPARE(cache.size(), 6); }
void FileSystemItem::markIsDefaultFolderCover(bool isDefault) { g_isDefaultFolderCover.insert(coverCacheKey(), new bool(isDefault)); m_useDefaultFolderCover = isDefault; // Re-create render here to refresh "m_useDefaultFolderCover" createRenderer(); }
void FileSystemItem::coverThumbnailLoadFailed() { QImage *coverImage = new QImage(":/res/image.png"); g_coverCache.insert(coverCacheKey(), coverImage); setThumbnail(new QImage(*coverImage)); m_coverImage->deleteLater(); m_coverImage = 0; }
void Audio::play(const QString &filename){ Sound *sound = SoundCache[filename]; if(sound == NULL){ sound = new Sound(filename); SoundCache.insert(filename, sound); }else if(sound->isPlaying()) return; sound->play(); }
void tst_QCache::operator_bracket_bracket() { QCache<int, int> cache; cache.insert(1, new int(2)); QVERIFY(cache[0] == 0); QVERIFY(cache[1] != 0); QCOMPARE(*cache[1], 2); cache.insert(1, new int(4)); QVERIFY(cache[1] != 0); QCOMPARE(*cache[1], 4); // check that operator[] doesn't remove the item QVERIFY(cache[1] != 0); QCOMPARE(*cache[1], 4); cache.remove(1); QVERIFY(cache[1] == 0); }
void tst_QCache::contains() { QCache<int, int> cache; QVERIFY(!cache.contains(0)); QVERIFY(!cache.contains(1)); cache.insert(1, new int(1), 1); QVERIFY(!cache.contains(0)); QVERIFY(cache.contains(1)); cache.remove(0); cache.remove(1); QVERIFY(!cache.contains(0)); QVERIFY(!cache.contains(1)); cache.insert(1, new int(1), 1); QVERIFY(!cache.contains(0)); QVERIFY(cache.contains(1)); cache.clear(); QVERIFY(!cache.contains(0)); QVERIFY(!cache.contains(1)); }
void FileSystemItem::coverThumbnailLoaded() { QImage thumbnail = m_coverImage->thumbnail(); QImage *coverImage = 0; if (!thumbnail.isNull()) { coverImage = new QImage(m_coverImage->thumbnail()); } else { coverImage = new QImage(":/res/image.png"); } g_coverCache.insert(coverCacheKey(), coverImage); setThumbnail(new QImage(*coverImage)); m_coverImage->deleteLater(); m_coverImage = 0; }
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 *)); }
void tst_QCache::remove() { QCache<QString, Foo> cache; cache.remove(QString()); cache.remove("alpha"); QVERIFY(cache.isEmpty()); cache.insert("alpha", new Foo, 10); QCOMPARE(cache.size(), 1); cache.insert("beta", new Foo, 20); QCOMPARE(cache.size(), 2); for (int i = 0; i < 10; ++i) { cache.remove("alpha"); QCOMPARE(cache.size(), 1); QCOMPARE(cache.totalCost(), 20); } cache.setMaxCost(1); QCOMPARE(cache.size(), 0); cache.remove("beta"); QCOMPARE(cache.size(), 0); }
void QGLBlurTextureCache::insertBlurTextureInfo(const QPixmap &pixmap, QGLBlurTextureInfo *info) { static bool hookAdded = false; if (!hookAdded) { QImagePixmapCleanupHooks::instance()->addPixmapDataDestructionHook(pixmapDestroyed); QImagePixmapCleanupHooks::instance()->addPixmapDataModificationHook(pixmapDestroyed); hookAdded = true; } QImagePixmapCleanupHooks::enableCleanupHooks(pixmap); cache.insert(pixmap.cacheKey(), info, pixmap.width() * pixmap.height()); if (timerId) killTimer(timerId); timerId = startTimer(8000); }
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; }
QString DateStringBuilder::getShortDate(const KDateTime &dateTime) { if (!dateTime.isValid() || dateTime.isNull()) { return QString(); } QDate currentDate = QDateTime().currentDateTime().date(); if (currentDate.weekNumber() == dateTime.date().weekNumber() || currentDate.addDays(1) == dateTime.date()) { //this week or tomorrow (i.e. on sunday) return getDayName(dateTime); } if (currentDate.year() == dateTime.date().year()) { //this year //Micro optimization because this function showed up as hotspot static QCache<uint, QString> cache; uint hash = dateTime.date().month() ^ dateTime.date().day(); if (!cache.contains(hash)) { cache.insert(hash, new QString(dateTime.toString("%d.%m"))); } return *cache[hash]; } return dateTime.toString("%d.%m.%Y"); }
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 {
FileDef *findFileDef(const FileNameDict *fnDict,const char *n,bool &ambig) { ambig=FALSE; if (n==0) return 0; const int maxAddrSize = 20; char addr[maxAddrSize]; qsnprintf(addr,maxAddrSize,"%p:",fnDict); QCString key = addr; key+=n; g_findFileDefCache.setAutoDelete(TRUE); FindFileCacheElem *cachedResult = g_findFileDefCache.find(key); //printf("key=%s cachedResult=%p\n",key.data(),cachedResult); if (cachedResult) { ambig = cachedResult->isAmbig; //printf("cached: fileDef=%p\n",cachedResult->fileDef); return cachedResult->fileDef; } else { cachedResult = new FindFileCacheElem(0,FALSE); } QCString name=QDir::cleanDirPath(n).utf8(); QCString path; int slashPos; FileName *fn; if (name.isEmpty()) goto exit; slashPos=QMAX(name.findRev('/'),name.findRev('\\')); if (slashPos!=-1) { path=name.left(slashPos+1); name=name.right(name.length()-slashPos-1); //printf("path=%s name=%s\n",path.data(),name.data()); } if (name.isEmpty()) goto exit; if ((fn=(*fnDict)[name])) { //printf("fn->count()=%d\n",fn->count()); if (fn->count()==1) { FileDef *fd = fn->getFirst(); #if defined(_WIN32) || defined(__MACOSX__) // Windows or MacOSX bool isSamePath = fd->getPath().right(path.length()).lower()==path.lower(); #else // Unix bool isSamePath = fd->getPath().right(path.length())==path; #endif if (path.isEmpty() || isSamePath) { cachedResult->fileDef = fd; g_findFileDefCache.insert(key,cachedResult); //printf("=1 ===> add to cache %p\n",fd); return fd; } } else // file name alone is ambiguous { int count=0; FileNameIterator fni(*fn); FileDef *fd; FileDef *lastMatch=0; QCString pathStripped = path; for (fni.toFirst();(fd=fni.current());++fni) { QCString fdStripPath = fd->getPath(); if (path.isEmpty() || fdStripPath.right(pathStripped.length())==pathStripped) { count++; lastMatch=fd; } } //printf(">1 ===> add to cache %p\n",fd); ambig=(count>1); cachedResult->isAmbig = ambig; cachedResult->fileDef = lastMatch; g_findFileDefCache.insert(key,cachedResult); return lastMatch; } } else { //printf("not found!\n"); } exit: //printf("0 ===> add to cache %p: %s\n",cachedResult,n); g_findFileDefCache.insert(key,cachedResult); //delete cachedResult; return 0; }
void FileSystemItem::gotThumbnail(QString path) { QImage *coverImage = new QImage(path); g_coverCache.insert(coverCacheKey(), coverImage); setThumbnail(new QImage(*coverImage)); }