QPixmap QPixmapIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) { QPixmap pm; QPixmapIconEngineEntry *pe = bestMatch(size, mode, state, false); if (pe) pm = pe->pixmap; if (pm.isNull()) { int idx = pixmaps.count(); while (--idx >= 0) { if (pe == &pixmaps[idx]) { pixmaps.remove(idx); break; } } if (pixmaps.isEmpty()) return pm; else return pixmap(size, mode, state); } QSize actualSize = pm.size(); if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height())) actualSize.scale(size, Qt::KeepAspectRatio); QString key = QLatin1String("$qt_icon_") + QString::number(pm.cacheKey()) + QString::number(pe->mode) + QString::number(actualSize.width()) + QLatin1Char('_') + QString::number(actualSize.height()) + QLatin1Char('_'); if (mode == QIcon::Active) { if (QPixmapCache::find(key + QString::number(mode), pm)) return pm; // horray if (QPixmapCache::find(key + QString::number(QIcon::Normal), pm)) { QStyleOption opt(0); opt.palette = QApplication::palette(); QPixmap active = QApplication::style()->generatedIconPixmap(QIcon::Active, pm, &opt); if (pm.cacheKey() == active.cacheKey()) return pm; } } if (!QPixmapCache::find(key + QString::number(mode), pm)) { if (pm.size() != actualSize) pm = pm.scaled(actualSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); if (pe->mode != mode && mode != QIcon::Normal) { QStyleOption opt(0); opt.palette = QApplication::palette(); QPixmap generated = QApplication::style()->generatedIconPixmap(mode, pm, &opt); if (!generated.isNull()) pm = generated; } QPixmapCache::insert(key + QString::number(mode), pm); } return pm; }
QPixmap QPixmapIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) { QPixmap pm; QPixmapIconEngineEntry *pe = bestMatch(size, mode, state, false); if (pe) pm = pe->pixmap; if (pm.isNull()) { int idx = pixmaps.count(); while (--idx >= 0) { if (pe == &pixmaps[idx]) { pixmaps.remove(idx); break; } } if (pixmaps.isEmpty()) return pm; else return pixmap(size, mode, state); } QSize actualSize = pm.size(); if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height())) actualSize.scale(size, Qt::KeepAspectRatio); QString key = QLatin1String("qt_") % HexString<quint64>(pm.cacheKey()) % HexString<uint>(pe->mode) % HexString<quint64>(QGuiApplication::palette().cacheKey()) % HexString<uint>(actualSize.width()) % HexString<uint>(actualSize.height()); if (mode == QIcon::Active) { if (QPixmapCache::find(key % HexString<uint>(mode), pm)) return pm; // horray if (QPixmapCache::find(key % HexString<uint>(QIcon::Normal), pm)) { QPixmap active = pm; if (QGuiApplication *guiApp = qobject_cast<QGuiApplication *>(qApp)) active = static_cast<QGuiApplicationPrivate*>(QObjectPrivate::get(guiApp))->applyQIconStyleHelper(QIcon::Active, pm); if (pm.cacheKey() == active.cacheKey()) return pm; } } if (!QPixmapCache::find(key % HexString<uint>(mode), pm)) { if (pm.size() != actualSize) pm = pm.scaled(actualSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); if (pe->mode != mode && mode != QIcon::Normal) { QPixmap generated = pm; if (QGuiApplication *guiApp = qobject_cast<QGuiApplication *>(qApp)) generated = static_cast<QGuiApplicationPrivate*>(QObjectPrivate::get(guiApp))->applyQIconStyleHelper(mode, pm); if (!generated.isNull()) pm = generated; } QPixmapCache::insert(key % HexString<uint>(mode), pm); } return pm; }
const QPixmap & CImgButton::scaleImage( const QString & name ) const { QPixmap * scaled = new QPixmap(); *scaled = m_picsOrig[ name ].scaled( size().width(), size().height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation ); m_picsSz.insert( scaled->cacheKey(), scaled ); return *m_picsSz[ scaled->cacheKey() ]; }
void testUnknownIconNotCached() { // This is a test to ensure that "unknown" icons do not pin themselves // in the icon loader. Or in other words, if an "unknown" icon is // returned, but the appropriate icon is subsequently installed // properly, the next request for that icon should return the new icon // instead of the unknown icon. // Since we'll need to create an icon we'll need a temporary directory, // and we want that established before creating the icon loader. QString tempRoot = QDir::tempPath() + QLatin1String("/kiconloader_unittest"); QString temporaryDir = tempRoot + QLatin1String("/hicolor/22x22/actions"); QVERIFY(QDir::root().mkpath(temporaryDir)); QVERIFY(KGlobal::dirs()->addResourceDir("icon", tempRoot, false)); KIconLoader iconLoader; // First find an existing icon. The only ones installed for sure by // kdelibs are the kimproxy ones. QString loadedIconPath = iconLoader.iconPath( QLatin1String("presence_online"), KIconLoader::DefaultState, false /* Ensure "unknown" icon can't be returned */ ); QVERIFY(!loadedIconPath.isEmpty()); QString nonExistingIconName = QLatin1String("fhqwhgads_homsar"); // Find a non-existent icon, allowing unknown icon to be returned QPixmap nonExistingIcon = iconLoader.loadIcon( nonExistingIconName, KIconLoader::Toolbar); QCOMPARE(nonExistingIcon.isNull(), false); // Install the existing icon by copying. QFileInfo existingIconInfo(loadedIconPath); QString newIconPath = temporaryDir + QLatin1String("/") + nonExistingIconName + QLatin1String(".png"); QVERIFY(QFile::copy(loadedIconPath, newIconPath)); // Verify the icon can now be found. QPixmap nowExistingIcon = iconLoader.loadIcon( nonExistingIconName, KIconLoader::Toolbar); QVERIFY(nowExistingIcon.cacheKey() != nonExistingIcon.cacheKey()); QCOMPARE(iconLoader.iconPath(nonExistingIconName, KIconLoader::Toolbar), newIconPath); // Cleanup QFile::remove(newIconPath); QStringList entries(QDir(tempRoot).entryList( QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name | QDir::Reversed)); Q_FOREACH(const QString &dirName, entries) { QDir::root().rmdir(dirName); }
int64_t LayerTreeHostQt::adoptImageBackingStore(Image* image) { if (!image) return InvalidWebLayerID; QPixmap* pixmap = image->nativeImageForCurrentFrame(); if (!pixmap) return InvalidWebLayerID; int64_t key = pixmap->cacheKey(); HashMap<int64_t, int>::iterator it = m_directlyCompositedImageRefCounts.find(key); if (it != m_directlyCompositedImageRefCounts.end()) { ++(it->second); return key; } RefPtr<ShareableBitmap> bitmap = ShareableBitmap::createShareable(image->size(), image->currentFrameHasAlpha() ? ShareableBitmap::SupportsAlpha : 0); { OwnPtr<WebCore::GraphicsContext> graphicsContext = bitmap->createGraphicsContext(); graphicsContext->drawImage(image, ColorSpaceDeviceRGB, IntPoint::zero()); } ShareableBitmap::Handle handle; bitmap->createHandle(handle); m_webPage->send(Messages::LayerTreeHostProxy::CreateDirectlyCompositedImage(key, handle)); m_directlyCompositedImageRefCounts.add(key, 1); return key; }
void QPdfEngine::drawPixmap (const QRectF &rectangle, const QPixmap &pixmap, const QRectF &sr) { if (sr.isEmpty() || rectangle.isEmpty() || pixmap.isNull()) return; Q_D(QPdfEngine); QBrush b = d->brush; QRect sourceRect = sr.toRect(); QPixmap pm = sourceRect != pixmap.rect() ? pixmap.copy(sourceRect) : pixmap; QImage image = pm.toImage(); bool bitmap = true; const int object = d->addImage(image, &bitmap, pm.cacheKey()); if (object < 0) return; *d->currentPage << "q\n/GSa gs\n"; *d->currentPage << QPdf::generateMatrix(QTransform(rectangle.width() / sr.width(), 0, 0, rectangle.height() / sr.height(), rectangle.x(), rectangle.y()) * (d->simplePen ? QTransform() : d->stroker.matrix)); if (bitmap) { // set current pen as d->brush d->brush = d->pen.brush(); } setBrush(); d->currentPage->streamImage(image.width(), image.height(), object); *d->currentPage << "Q\n"; d->brush = b; }
void PixmapDelegateFader::setPixmap( const QPixmap& pixmap ) { if ( pixmap.isNull() ) return; m_defaultImage = false; const qint64 newImageMd5 = pixmap.cacheKey(); if ( m_oldImageMd5 == newImageMd5 ) return; m_oldImageMd5 = newImageMd5; if ( m_connectedToStl ) { m_pixmapQueue.enqueue( pixmap ); return; } m_oldReference = m_currentReference; m_currentReference = pixmap; stlInstance().data()->setUpdateInterval( 20 ); m_startFrame = stlInstance().data()->currentFrame(); m_connectedToStl = true; m_fadePct = 0; connect( stlInstance().data(), SIGNAL( frameChanged( int ) ), this, SLOT( onAnimationStep( int ) ) ); }
void ComplexVideoWidget::setPicture(const QPixmap& pxmp) { //if (!d->pixmap || d->pixmap->cacheKey() != pixmap.cacheKey()) { // d->clearContents(); // d->pixmap = new QPixmap(pixmap); //} //if (d->pixmap->depth() == 1 && !d->pixmap->mask()) // d->pixmap->setMask(*((QBitmap *)d->pixmap)); //d->updateLabel(); if(_noSignal) _noSignal = false; //static int k=0; //k++; //if(k > 50) // return; if(!_pPixmap || _pPixmap->cacheKey() != pxmp.cacheKey()) { if(_pPixmap != NULL) { delete _pPixmap; _pPixmap = NULL; } _pPixmap = new QPixmap(pxmp); update(rect()); } }
bool QWinThumbnailToolBarPrivate::IconicPixmapCache::setPixmap(const QPixmap &pixmap) { if (pixmap.cacheKey() == m_pixmap.cacheKey()) return false; deleteBitmap(); m_pixmap = pixmap; return true; }
void QWindowsDragCursorWindow::setPixmap(const QPixmap &p) { if (p.cacheKey() == m_pixmap.cacheKey()) return; const QSize oldSize = m_pixmap.size(); QSize newSize = p.size(); qCDebug(lcQpaMime) << __FUNCTION__ << p.cacheKey() << newSize; m_pixmap = p; if (oldSize != newSize) { const qreal pixDevicePixelRatio = p.devicePixelRatio(); if (pixDevicePixelRatio > 1.0 && qFuzzyCompare(pixDevicePixelRatio, devicePixelRatio())) newSize /= qRound(pixDevicePixelRatio); resize(newSize); } if (isVisible()) update(); }
void TestGuiPixmaps::testEntryIcons() { Database* db = new Database(); Entry* entry = new Entry(); entry->setGroup(db->rootGroup()); QImage icon; QImage image; QPixmap pixmap; QPixmap pixmapCached1; QPixmap pixmapCached2; icon = databaseIcons()->icon(10); entry->setIcon(10); image = entry->icon(); pixmap = entry->iconPixmap(); QCOMPARE(image, icon); compareImages(pixmap, icon); pixmapCached1 = entry->iconPixmap(); pixmapCached2 = databaseIcons()->iconPixmap(10); compareImages(pixmapCached1, icon); compareImages(pixmapCached2, icon); QCOMPARE(pixmapCached1.cacheKey(), pixmap.cacheKey()); QCOMPARE(pixmapCached2.cacheKey(), pixmap.cacheKey()); Uuid iconUuid = Uuid::random(); icon = QImage(2, 1, QImage::Format_RGB32); icon.setPixel(0, 0, qRgb(0, 0, 0)); icon.setPixel(1, 0, qRgb(0, 0, 50)); db->metadata()->addCustomIcon(iconUuid, icon); entry->setIcon(iconUuid); image = entry->icon(); pixmap = entry->iconPixmap(); QCOMPARE(image, icon); compareImages(pixmap, icon); pixmapCached1 = entry->iconPixmap(); compareImages(pixmapCached1, icon); QCOMPARE(pixmapCached1.cacheKey(), pixmap.cacheKey()); delete db; }
void TestGuiPixmaps::testDatabaseIcons() { QImage image; QPixmap pixmap; QPixmap pixmapCached; image = databaseIcons()->icon(0); pixmap = databaseIcons()->iconPixmap(0); compareImages(pixmap, image); // check if the cache works correctly pixmapCached = databaseIcons()->iconPixmap(0); compareImages(pixmapCached, image); QCOMPARE(pixmapCached.cacheKey(), pixmap.cacheKey()); pixmap = databaseIcons()->iconPixmap(1); image = databaseIcons()->icon(1); compareImages(pixmap, image); pixmapCached = databaseIcons()->iconPixmap(1); compareImages(pixmapCached, image); QCOMPARE(pixmapCached.cacheKey(), pixmap.cacheKey()); }
/*! \property QLabel::pixmap \brief the label's pixmap If no pixmap has been set this will return 0. Setting the pixmap clears any previous content. The buddy shortcut, if any, is disabled. */ void QLabel::setPixmap(const QPixmap &pixmap) { Q_D(QLabel); if (!d->pixmap || d->pixmap->cacheKey() != pixmap.cacheKey()) { d->clearContents(); d->pixmap = new QPixmap(pixmap); } if (d->pixmap->depth() == 1 && !d->pixmap->mask()) d->pixmap->setMask(*((QBitmap *)d->pixmap)); d->updateLabel(); }
int64_t LayerTreeCoordinator::adoptImageBackingStore(Image* image) { if (!image) return InvalidWebLayerID; int64_t key = 0; #if PLATFORM(QT) QPixmap* nativeImage = image->nativeImageForCurrentFrame(); if (!nativeImage) return InvalidWebLayerID; key = nativeImage->cacheKey(); #elif USE(CAIRO) NativeImageCairo* nativeImage = image->nativeImageForCurrentFrame(); if (!nativeImage) return InvalidWebLayerID; // This can be safely done since we own the reference. // A corresponding cairo_surface_destroy() is ensured in releaseImageBackingStore(). cairo_surface_t* cairoSurface = cairo_surface_reference(nativeImage->surface()); key = reinterpret_cast<int64_t>(cairoSurface); #endif HashMap<int64_t, int>::iterator it = m_directlyCompositedImageRefCounts.find(key); if (it != m_directlyCompositedImageRefCounts.end()) { ++(it->value); return key; } // Check if we were going to release this image during the next flush. size_t releasedIndex = m_releasedDirectlyCompositedImages.find(key); if (releasedIndex == notFound) { RefPtr<ShareableBitmap> bitmap = ShareableBitmap::createShareable(image->size(), (image->currentFrameHasAlpha() ? ShareableBitmap::SupportsAlpha : 0)); { OwnPtr<WebCore::GraphicsContext> graphicsContext = bitmap->createGraphicsContext(); graphicsContext->drawImage(image, ColorSpaceDeviceRGB, IntPoint::zero()); } ShareableBitmap::Handle handle; bitmap->createHandle(handle); m_webPage->send(Messages::LayerTreeCoordinatorProxy::CreateDirectlyCompositedImage(key, handle)); } else m_releasedDirectlyCompositedImages.remove(releasedIndex); m_directlyCompositedImageRefCounts.add(key, 1); return key; }
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); }
bool QPMCache::insert(const QString& key, const QPixmap &pixmap, int cost) { qint64 cacheKey = pixmap.cacheKey(); if (QCache<qint64, QDetachedPixmap>::object(cacheKey)) { cacheKeys.insert(key, cacheKey); return true; } bool success = QCache<qint64, QDetachedPixmap>::insert(cacheKey, new QDetachedPixmap(pixmap), cost); if (success) { cacheKeys.insert(key, cacheKey); if (!id) { id = startTimer(30000); t = false; } } return success; }
GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, const QPixmap &pixmap, BindOptions options) { if (pixmap.isNull()) return 0; QMutexLocker locker(&m_mutex); qint64 key = pixmap.cacheKey(); // A QPainter is active on the image - take the safe route and replace the texture. if (!pixmap.paintingActive()) { QOpenGLCachedTexture *entry = m_cache.object(key); if (entry && entry->options() == options) { context->functions()->glBindTexture(GL_TEXTURE_2D, entry->id()); return entry->id(); } } GLuint id = bindTexture(context, key, pixmap.toImage(), options); if (id > 0) QImagePixmapCleanupHooks::enableCleanupHooks(pixmap); return id; }
void VideoFrame::setPixmap(const QPixmap &APixmap) { if (FVideoFrame.cacheKey() != APixmap.cacheKey()) { if (FVideoFrame.isNull() != APixmap.isNull()) { if (APixmap.isNull()) FWaitMovie->start(); else FWaitMovie->stop(); } if (FVideoFrame.size() != APixmap.size()) { FVideoFrame = APixmap; updateGeometry(); } else { FVideoFrame = APixmap; } update(); } }
void QBasicDrag::updateCursor(Qt::DropAction action) { #ifndef QT_NO_CURSOR Qt::CursorShape cursorShape = Qt::ForbiddenCursor; if (canDrop()) { switch (action) { case Qt::CopyAction: cursorShape = Qt::DragCopyCursor; break; case Qt::LinkAction: cursorShape = Qt::DragLinkCursor; break; default: cursorShape = Qt::DragMoveCursor; break; } } QCursor *cursor = QGuiApplication::overrideCursor(); QPixmap pixmap = m_drag->dragCursor(action); if (!cursor) { QGuiApplication::changeOverrideCursor((pixmap.isNull()) ? QCursor(cursorShape) : QCursor(pixmap)); } else { if (!pixmap.isNull()) { if ((cursor->pixmap().cacheKey() != pixmap.cacheKey())) { QGuiApplication::changeOverrideCursor(QCursor(pixmap)); } } else { if (cursorShape != cursor->shape()) { QGuiApplication::changeOverrideCursor(QCursor(cursorShape)); } } } #endif updateAction(action); }
Vehicule::Vehicule(qint64 id,QString marque,QString modele,QString couleur,QString description,QPixmap photo, QObject *parent) : QObject(parent), id(id), marque(marque), modele(modele), couleur(couleur), description(description), photo(photo), photoCache(photo.cacheKey()) { }
void PlaylistItemDelegate::paintShort( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index, bool useAvatars ) const { TrackModelItem* item = m_model->itemFromIndex( m_model->mapToSource( index ) ); Q_ASSERT( item ); QStyleOptionViewItemV4 opt = option; prepareStyleOption( &opt, index, item ); opt.text.clear(); qApp->style()->drawControl( QStyle::CE_ItemViewItem, &opt, painter ); if ( m_view->header()->visualIndex( index.column() ) > 0 ) return; QPixmap pixmap; QString artist, track, upperText, lowerText; source_ptr source = item->query()->playedBy().first; if ( item->query()->results().count() ) { artist = item->query()->results().first()->artist()->name(); track = item->query()->results().first()->track(); } else { artist = item->query()->artist(); track = item->query()->track(); } if ( source.isNull() ) { upperText = artist; lowerText = track; } else { upperText = QString( "%1 - %2" ).arg( artist ).arg( track ); QString playtime = TomahawkUtils::ageToString( QDateTime::fromTime_t( item->query()->playedBy().second ) ); if ( source == SourceList::instance()->getLocal() ) lowerText = QString( "played %1 ago by you" ).arg( playtime ); else lowerText = QString( "played %1 ago by %2" ).arg( playtime ).arg( source->friendlyName() ); if ( useAvatars ) pixmap = source->avatar( Source::FancyStyle ); } if ( pixmap.isNull() && !useAvatars ) pixmap = QPixmap( RESPATH "images/track-placeholder.png" ); else if ( pixmap.isNull() && useAvatars ) pixmap = m_defaultAvatar; painter->save(); { QRect r = opt.rect.adjusted( 3, 6, 0, -6 ); // Paint Now Playing Speaker Icon if ( item->isPlaying() ) { r.adjust( 0, 0, 0, 0 ); QRect npr = r.adjusted( 3, r.height() / 2 - m_nowPlayingIcon.height() / 2, 18 - r.width(), -r.height() / 2 + m_nowPlayingIcon.height() / 2 ); painter->drawPixmap( npr, m_nowPlayingIcon ); r.adjust( 22, 0, 0, 0 ); } painter->setPen( opt.palette.text().color() ); QRect ir = r.adjusted( 4, 0, -option.rect.width() + option.rect.height() - 8 + r.left(), 0 ); QPixmap scover; if ( m_cache.contains( pixmap.cacheKey() ) ) { scover = m_cache.value( pixmap.cacheKey() ); } else { scover = pixmap.scaled( ir.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation ); m_cache.insert( pixmap.cacheKey(), scover ); } painter->drawPixmap( ir, scover ); QFont boldFont = opt.font; boldFont.setBold( true ); r.adjust( ir.width() + 12, 0, -12, 0 ); painter->setFont( boldFont ); QString text = painter->fontMetrics().elidedText( upperText, Qt::ElideRight, r.width() ); painter->drawText( r.adjusted( 0, 1, 0, 0 ), text, m_topOption ); painter->setFont( opt.font); text = painter->fontMetrics().elidedText( lowerText, Qt::ElideRight, r.width() ); painter->drawText( r.adjusted( 0, 1, 0, 0 ), text, m_bottomOption ); } painter->restore(); }
void AlbumItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const { AlbumItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) ); if ( !item ) return; QStyleOptionViewItemV4 opt = option; initStyleOption( &opt, QModelIndex() ); qApp->style()->drawControl( QStyle::CE_ItemViewItem, &opt, painter ); painter->save(); painter->setRenderHint( QPainter::Antialiasing ); if ( !( option.state & QStyle::State_Selected ) ) { QRect shadowRect = option.rect.adjusted( 5, 4, -5, -40 ); painter->setPen( QColor( 90, 90, 90 ) ); painter->drawRoundedRect( shadowRect, 0.5, 0.5 ); QPen shadowPen( QColor( 30, 30, 30 ) ); shadowPen.setWidth( 0.4 ); painter->drawLine( shadowRect.bottomLeft() + QPoint( -1, 2 ), shadowRect.bottomRight() + QPoint( 1, 2 ) ); shadowPen.setColor( QColor( 160, 160, 160 ) ); painter->setPen( shadowPen ); painter->drawLine( shadowRect.topLeft() + QPoint( -1, 2 ), shadowRect.bottomLeft() + QPoint( -1, 2 ) ); painter->drawLine( shadowRect.topRight() + QPoint( 2, 2 ), shadowRect.bottomRight() + QPoint( 2, 2 ) ); painter->drawLine( shadowRect.bottomLeft() + QPoint( 0, 3 ), shadowRect.bottomRight() + QPoint( 0, 3 ) ); shadowPen.setColor( QColor( 180, 180, 180 ) ); painter->setPen( shadowPen ); painter->drawLine( shadowRect.topLeft() + QPoint( -2, 3 ), shadowRect.bottomLeft() + QPoint( -2, 1 ) ); painter->drawLine( shadowRect.topRight() + QPoint( 3, 3 ), shadowRect.bottomRight() + QPoint( 3, 1 ) ); painter->drawLine( shadowRect.bottomLeft() + QPoint( 0, 4 ), shadowRect.bottomRight() + QPoint( 0, 4 ) ); } QPixmap cover; if ( !item->album().isNull() ) { cover.loadFromData( item->album()->cover() ); } else if ( !item->artist().isNull() ) { cover.loadFromData( item->artist()->cover() ); } if ( cover.isNull() ) cover = m_defaultCover; QRect r = option.rect.adjusted( 6, 5, -6, -41 ); if ( option.state & QStyle::State_Selected ) { #if defined(Q_WS_MAC) || defined(Q_WS_WIN) painter->save(); painter->setRenderHint( QPainter::Antialiasing ); QPainterPath border; border.addRoundedRect( r.adjusted( -2, -2, 2, 2 ), 3, 3 ); QPen borderPen( QColor( 86, 170, 243 ) ); borderPen.setWidth( 5 ); painter->setPen( borderPen ); painter->drawPath( border ); painter->restore(); #else opt.palette.setColor( QPalette::Text, opt.palette.color( QPalette::HighlightedText ) ); #endif } QPixmap scover; if ( m_cache.contains( cover.cacheKey() ) ) { scover = m_cache.value( cover.cacheKey() ); } else { scover = cover.scaled( r.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation ); m_cache.insert( cover.cacheKey(), scover ); } painter->drawPixmap( r, scover ); painter->setPen( opt.palette.color( QPalette::Text ) ); QTextOption to; to.setWrapMode( QTextOption::NoWrap ); QString text; QFont font = opt.font; font.setPixelSize( 11 ); QFont boldFont = font; boldFont.setBold( true ); QRect textRect = option.rect.adjusted( 0, option.rect.height() - 32, 0, -2 ); QString name; if ( !item->album().isNull() ) name = item->album()->name(); else if ( !item->artist().isNull() ) name = item->artist()->name(); painter->setFont( boldFont ); bool oneLiner = false; if ( item->album().isNull() || item->album()->artist().isNull() ) oneLiner = true; else oneLiner = ( textRect.height() / 2 < painter->fontMetrics().boundingRect( item->album()->name() ).height() || textRect.height() / 2 < painter->fontMetrics().boundingRect( item->album()->artist()->name() ).height() ); if ( oneLiner ) { to.setAlignment( Qt::AlignHCenter | Qt::AlignVCenter ); text = painter->fontMetrics().elidedText( name, Qt::ElideRight, textRect.width() - 3 ); painter->drawText( textRect, text, to ); } else { to.setAlignment( Qt::AlignHCenter | Qt::AlignTop ); text = painter->fontMetrics().elidedText( item->album()->name(), Qt::ElideRight, textRect.width() - 3 ); painter->drawText( textRect, text, to ); // If the user is hovering over an artist rect, draw a background so she knows it's clickable QRect r = textRect; r.setTop( r.bottom() - painter->fontMetrics().height() ); r.adjust( 4, 0, -4, -1 ); if ( m_hoveringOver == index ) { TomahawkUtils::drawQueryBackground( painter, opt.palette, r, 1.1 ); painter->setPen( opt.palette.color( QPalette::HighlightedText ) ); } else { if ( !( option.state & QStyle::State_Selected ) ) #ifdef Q_WS_MAC painter->setPen( opt.palette.color( QPalette::Dark ).darker( 200 ) ); #else painter->setPen( opt.palette.color( QPalette::Dark ) ); #endif } to.setAlignment( Qt::AlignHCenter | Qt::AlignBottom ); text = painter->fontMetrics().elidedText( item->album()->artist()->name(), Qt::ElideRight, textRect.width() - 10 ); painter->drawText( textRect.adjusted( 5, -1, -5, -1 ), text, to ); // Calculate rect of artist on-hover button click area m_artistNameRects[ index ] = r; } painter->restore(); }
void QWindowsOleDropSource::createCursors() { const QDrag *drag = m_drag->currentDrag(); const QPixmap pixmap = drag->pixmap(); const bool hasPixmap = !pixmap.isNull(); // Find screen for drag. Could be obtained from QDrag::source(), but that might be a QWidget. const QPlatformScreen *platformScreen = QWindowsContext::instance()->screenManager().screenAtDp(QWindowsCursor::mousePosition()); if (!platformScreen) { if (const QScreen *primaryScreen = QGuiApplication::primaryScreen()) platformScreen = primaryScreen->handle(); } Q_ASSERT(platformScreen); QPlatformCursor *platformCursor = platformScreen->cursor(); qreal pixmapScaleFactor = 1; qreal hotSpotScaleFactor = 1; if (m_mode != TouchDrag) { // Touch drag: pixmap is shown in a separate QWindow, which will be scaled.) hotSpotScaleFactor = QHighDpiScaling::factor(platformScreen); pixmapScaleFactor = hotSpotScaleFactor / pixmap.devicePixelRatio(); } QPixmap scaledPixmap = qFuzzyCompare(pixmapScaleFactor, 1.0) ? pixmap : pixmap.scaled((QSizeF(pixmap.size()) * pixmapScaleFactor).toSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation); scaledPixmap.setDevicePixelRatio(1); Qt::DropAction actions[] = { Qt::MoveAction, Qt::CopyAction, Qt::LinkAction, Qt::IgnoreAction }; int actionCount = int(sizeof(actions) / sizeof(actions[0])); if (!hasPixmap) --actionCount; // No Qt::IgnoreAction unless pixmap const QPoint hotSpot = qFuzzyCompare(hotSpotScaleFactor, 1.0) ? drag->hotSpot() : (QPointF(drag->hotSpot()) * hotSpotScaleFactor).toPoint(); for (int cnum = 0; cnum < actionCount; ++cnum) { const Qt::DropAction action = actions[cnum]; QPixmap cursorPixmap = drag->dragCursor(action); if (cursorPixmap.isNull() && platformCursor) cursorPixmap = static_cast<QWindowsCursor *>(platformCursor)->dragDefaultCursor(action); const qint64 cacheKey = cursorPixmap.cacheKey(); const auto it = m_cursors.find(action); if (it != m_cursors.end() && it.value().cacheKey == cacheKey) continue; if (cursorPixmap.isNull()) { qWarning("%s: Unable to obtain drag cursor for %d.", __FUNCTION__, action); continue; } QPoint newHotSpot(0, 0); QPixmap newPixmap = cursorPixmap; if (hasPixmap) { const int x1 = qMin(-hotSpot.x(), 0); const int x2 = qMax(scaledPixmap.width() - hotSpot.x(), cursorPixmap.width()); const int y1 = qMin(-hotSpot.y(), 0); const int y2 = qMax(scaledPixmap.height() - hotSpot.y(), cursorPixmap.height()); QPixmap newCursor(x2 - x1 + 1, y2 - y1 + 1); newCursor.fill(Qt::transparent); QPainter p(&newCursor); const QPoint pmDest = QPoint(qMax(0, -hotSpot.x()), qMax(0, -hotSpot.y())); p.drawPixmap(pmDest, scaledPixmap); p.drawPixmap(qMax(0, hotSpot.x()),qMax(0, hotSpot.y()), cursorPixmap); newPixmap = newCursor; newHotSpot = QPoint(qMax(0, hotSpot.x()), qMax(0, hotSpot.y())); } if (const HCURSOR sysCursor = QWindowsCursor::createPixmapCursor(newPixmap, newHotSpot)) { const CursorEntry entry(newPixmap, cacheKey, CursorHandlePtr(new CursorHandle(sysCursor)), newHotSpot); if (it == m_cursors.end()) m_cursors.insert(action, entry); else it.value() = entry; } } #ifndef QT_NO_DEBUG_OUTPUT if (lcQpaMime().isDebugEnabled()) qCDebug(lcQpaMime) << __FUNCTION__ << "pixmap" << pixmap.size() << m_cursors.size() << "cursors:\n" << m_cursors; #endif // !QT_NO_DEBUG_OUTPUT }
void AlbumItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const { AlbumItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) ); if ( !item ) return; QStyleOptionViewItemV4 opt = option; initStyleOption( &opt, QModelIndex() ); qApp->style()->drawControl( QStyle::CE_ItemViewItem, &opt, painter ); painter->save(); // painter->setRenderHint( QPainter::Antialiasing ); // painter->drawPixmap( option.rect.adjusted( 4, 4, -4, -38 ), m_shadowPixmap ); QPixmap cover = item->cover.isNull() ? m_defaultCover : item->cover; QRect r = option.rect.adjusted( 6, 5, -6, -41 ); if ( option.state & QStyle::State_Selected ) { #if defined(Q_WS_MAC) || defined(Q_WS_WIN) painter->save(); painter->setRenderHint( QPainter::Antialiasing ); QPainterPath border; border.addRoundedRect( r.adjusted( -2, -2, 2, 2 ), 3, 3 ); QPen borderPen( QColor( 86, 170, 243 ) ); borderPen.setWidth( 5 ); painter->setPen( borderPen ); painter->drawPath( border ); painter->restore(); #else opt.palette.setColor( QPalette::Text, opt.palette.color( QPalette::HighlightedText ) ); #endif } QPixmap scover; if ( m_cache.contains( cover.cacheKey() ) ) { scover = m_cache.value( cover.cacheKey() ); } else { scover = cover.scaled( r.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation ); m_cache.insert( cover.cacheKey(), scover ); } painter->drawPixmap( r, scover ); painter->setPen( opt.palette.color( QPalette::Text ) ); QTextOption to; to.setWrapMode( QTextOption::NoWrap ); QString text; QFont font = opt.font; QFont boldFont = opt.font; boldFont.setBold( true ); QRect textRect = option.rect.adjusted( 0, option.rect.height() - 32, 0, -2 ); bool oneLiner = ( textRect.height() / 2 < painter->fontMetrics().boundingRect( item->album()->name() ).height() || textRect.height() / 2 < painter->fontMetrics().boundingRect( item->album()->artist()->name() ).height() ); if ( oneLiner ) { to.setAlignment( Qt::AlignHCenter | Qt::AlignVCenter ); text = painter->fontMetrics().elidedText( item->album()->name(), Qt::ElideRight, textRect.width() - 3 ); painter->drawText( textRect, text, to ); } else { to.setAlignment( Qt::AlignHCenter | Qt::AlignTop ); text = painter->fontMetrics().elidedText( item->album()->name(), Qt::ElideRight, textRect.width() - 3 ); painter->drawText( textRect, text, to ); painter->setFont( boldFont ); to.setAlignment( Qt::AlignHCenter | Qt::AlignBottom ); text = painter->fontMetrics().elidedText( item->album()->artist()->name(), Qt::ElideRight, textRect.width() - 3 ); painter->drawText( textRect, text, to ); } painter->restore(); }