void Image::drawPattern(GraphicsContext* ctxt, const FloatRect& tileRect, const AffineTransform& patternTransform, const FloatPoint& phase, ColorSpace, CompositeOperator op, const FloatRect& destRect) { QPixmap* framePixmap = nativeImageForCurrentFrame(); if (!framePixmap) // If it's too early we won't have an image yet. return; // Qt interprets 0 width/height as full width/height so just short circuit. QRectF dr = QRectF(destRect).normalized(); QRect tr = QRectF(tileRect).toRect().normalized(); if (!dr.width() || !dr.height() || !tr.width() || !tr.height()) return; QPixmap pixmap = *framePixmap; if (tr.x() || tr.y() || tr.width() != pixmap.width() || tr.height() != pixmap.height()) pixmap = pixmap.copy(tr); CompositeOperator previousOperator = ctxt->compositeOperation(); ctxt->setCompositeOperation(!pixmap.hasAlpha() && op == CompositeSourceOver ? CompositeCopy : op); QPainter* p = ctxt->platformContext(); QTransform transform(patternTransform); // If this would draw more than one scaled tile, we scale the pixmap first and then use the result to draw. if (transform.type() == QTransform::TxScale) { QRectF tileRectInTargetCoords = (transform * QTransform().translate(phase.x(), phase.y())).mapRect(tr); bool tileWillBePaintedOnlyOnce = tileRectInTargetCoords.contains(dr); if (!tileWillBePaintedOnlyOnce) { QSizeF scaledSize(float(pixmap.width()) * transform.m11(), float(pixmap.height()) * transform.m22()); QPixmap scaledPixmap(scaledSize.toSize()); if (pixmap.hasAlpha()) scaledPixmap.fill(Qt::transparent); { QPainter painter(&scaledPixmap); painter.setCompositionMode(QPainter::CompositionMode_Source); painter.setRenderHints(p->renderHints()); painter.drawPixmap(QRect(0, 0, scaledPixmap.width(), scaledPixmap.height()), pixmap); } pixmap = scaledPixmap; transform = QTransform::fromTranslate(transform.dx(), transform.dy()); } } /* Translate the coordinates as phase is not in world matrix coordinate space but the tile rect origin is. */ transform *= QTransform().translate(phase.x(), phase.y()); transform.translate(tr.x(), tr.y()); QBrush b(pixmap); b.setTransform(transform); p->fillRect(dr, b); ctxt->setCompositeOperation(previousOperator); if (imageObserver()) imageObserver()->didDraw(this); }
void QuizFrame::resizeImageBox() { imageBox->resize( imageBox->width(), topLeftPanel->height() ); if( movie ) { // Use the pixmap to compute the scaled size. int proportionalWidth = ( image->height() - 20 ) * pixmap->width() / pixmap->height(); movie->setScaledSize( QSize( proportionalWidth, image->height() ) ); image->setMovie( movie ); movie->start(); } else if( pixmap ) { QPixmap scaledPixmap( pixmap->scaledToHeight( image->height() - 20, Qt::SmoothTransformation ) ); image->setPixmap( scaledPixmap ); } imageStack->updateGeometry(); // To resize the imageButton. }
QPixmap MainWindow::scalePixmap(QPixmap *pixmapOriginal, float scale) { QSize scaledsize = QSize(pixmapOriginal->size()); scaledsize.setHeight(scaledsize.height()*scale); scaledsize.setWidth(scaledsize.width()*scale); QPixmap scaledPixmap(scaledsize); scaledPixmap.fill(Qt::transparent); QPainter p(&scaledPixmap); p.setRenderHint(QPainter::Antialiasing); p.setRenderHint(QPainter::SmoothPixmapTransform); p.setRenderHint(QPainter::HighQualityAntialiasing); p.scale(scale,scale); p.drawPixmap(0, 0, *pixmapOriginal); p.end(); return scaledPixmap; }
void MovieLabel::scalePixmap() { QLabel::setPixmap(scaledPixmap(orgPixmap)); }
int QgsComposerPictureWidget::addDirectoryToPreview( const QString& path ) { //go through all files of a directory QDir directory( path ); if ( !directory.exists() || !directory.isReadable() ) { return 1; //error } QFileInfoList fileList = directory.entryInfoList( QDir::Files ); QFileInfoList::const_iterator fileIt = fileList.constBegin(); QProgressDialog progress( "Adding Icons...", "Abort", 0, fileList.size() - 1, this ); //cancel button does not seem to work properly with modal dialog //progress.setWindowModality(Qt::WindowModal); int counter = 0; for ( ; fileIt != fileList.constEnd(); ++fileIt ) { progress.setLabelText( tr( "Creating icon for file %1" ).arg( fileIt->fileName() ) ); progress.setValue( counter ); QCoreApplication::processEvents(); if ( progress.wasCanceled() ) { break; } QString filePath = fileIt->absoluteFilePath(); //test if file is svg or pixel format bool fileIsPixel = false; bool fileIsSvg = testSvgFile( filePath ); if ( !fileIsSvg ) { fileIsPixel = testImageFile( filePath ); } //exclude files that are not svg or image if ( !fileIsSvg && !fileIsPixel ) { ++counter; continue; } QListWidgetItem * listItem = new QListWidgetItem( mPreviewListWidget ); listItem->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled ); if ( fileIsSvg ) { QIcon icon( filePath ); listItem->setIcon( icon ); } else //for pixel formats: create icon from scaled pixmap { QPixmap iconPixmap( filePath ); if ( iconPixmap.isNull() ) { ++counter; continue; //unknown file format or other problem } //set pixmap hardcoded to 30/30, same as icon size for mPreviewListWidget QPixmap scaledPixmap( iconPixmap.scaled( QSize( 30, 30 ), Qt::KeepAspectRatio ) ); QIcon icon( scaledPixmap ); listItem->setIcon( icon ); } listItem->setText( "" ); //store the absolute icon file path as user data listItem->setData( Qt::UserRole, fileIt->absoluteFilePath() ); ++counter; } return 0; }