void AbstractConfig::populateFrameList() { m_commonUi->framesList->clear(); // add frame items to the listview foreach (quint32 frameClass, FrameFactory::classes()) { // make icon from frame preview QIcon icon; Frame * frame = FrameFactory::createFrame(frameClass); if (!frame) { // generate the 'empty frame' preview QPixmap emptyPixmap(32, 32); emptyPixmap.fill(Qt::transparent); QPainter pixPainter(&emptyPixmap); pixPainter.drawLine(4, 4, 27, 27); pixPainter.drawLine(4, 27, 27, 4); pixPainter.end(); icon = emptyPixmap; } else { icon = frame->preview(32, 32); delete frame; } // add the item to the list (and attach it the class) QListWidgetItem * item = new QListWidgetItem(icon, QString(), m_commonUi->framesList); item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); item->setData(Qt::UserRole, frameClass); }
void BoardView::paintEvent(QPaintEvent *event) { const QRect& rectange = event->rect(); const float cellSize = calculateCellSize(rectange, m_height, m_width); const float tableWidth = cellSize * m_width; const float tableHeight = cellSize * m_height; QPixmap pixmap(QSize(tableWidth + 1, tableHeight + 1)); pixmap.fill(Qt::white); QPainter pixPainter(&pixmap); pixPainter.setRenderHint(QPainter::Antialiasing); pixPainter.setPen(QPen(Qt::black, 1)); for (int i = cellSize, k = m_width * cellSize; i < k; i += cellSize) pixPainter.drawLine(i, 0, i, pixmap.height()); for (int i = cellSize, k = m_height * cellSize; i < k; i += cellSize) pixPainter.drawLine(0, i, pixmap.width(), i); pixPainter.setPen(QPen(Qt::black, 3)); pixPainter.drawRect(0, 0, pixmap.width(), pixmap.height()); fillPolygon(pixPainter, cellSize); drawLastPoint(pixPainter, cellSize); drawPolygons(pixPainter, cellSize); drawUnderMousePoint(pixPainter, cellSize); QPainter painter(this); const int dx =(rectange.width() - tableWidth) / 2; const int dy =(rectange.height() - tableHeight) / 2; painter.drawPixmap(dx, dy, pixmap); }
QPixmap Frame::preview(int width, int height) { QPixmap pixmap(128, 128); pixmap.fill(Qt::transparent); QPainter pixPainter(&pixmap); paint(&pixPainter, QRect(0, 0, 128, 128), false, false); pixPainter.end(); return pixmap.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); }
QPixmap EmptyFrame::preview(int width, int height) { QPixmap pixmap(width, height); pixmap.fill(Qt::transparent); QPainter pixPainter(&pixmap); pixPainter.drawText(pixmap.rect(), Qt::AlignCenter, "X"); pixPainter.end(); return pixmap; }
void PictureWidget::setImage(const QPixmap& _pixmap, int radius) { pixmapToDraw_ = _pixmap; if (radius != -1) { auto borderPixmap = QPixmap(pixmapToDraw_.size()); borderPixmap.fill(Qt::transparent); QPainter pixPainter(&borderPixmap); pixPainter.setBrush(pixmapToDraw_); pixPainter.setPen(Qt::transparent); pixPainter.setRenderHint(QPainter::Antialiasing); pixPainter.drawRoundedRect(0, 0, pixmapToDraw_.width(), pixmapToDraw_.height(), radius, radius); Utils::check_pixel_ratio(pixmapToDraw_); borderPixmap.setDevicePixelRatio(pixmapToDraw_.devicePixelRatio()); pixmapToDraw_ = std::move(borderPixmap); } update(); }
QPixmap TextContent::renderAsBackground(const QSize & size, bool keepAspect) const { // get the base empty pixmap QPixmap pix = AbstractContent::renderAsBackground(size, keepAspect); QSize textSize = boundingRect().size().toSize(); const float w = size.width(), h = size.height(), tw = textSize.width(), th = textSize.height(); if (w < 2 || h < 2 || tw < 2 || th < 2) return pix; // draw text (centered, maximized keeping aspect ratio) float scale = qMin(w / (tw + 16), h / (th + 16)); QPainter pixPainter(&pix); pixPainter.translate((w - (int)((float)tw * scale)) / 2, (h - (int)((float)th * scale)) / 2); pixPainter.scale(scale, scale); m_text->drawContents(&pixPainter); pixPainter.end(); return pix; }
void BrushCombo::paintEvent(QPaintEvent*) { QStylePainter painter(this); QStyleOptionComboBox opt; initStyleOption(&opt); painter.drawComplexControl(QStyle::CC_ComboBox, opt); const QRect& rect = painter.style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField, this); QPixmap pixmap(rect.size() - QSize(4, 4)); QPainter pixPainter(&pixmap); pixPainter.setRenderHint(QStylePainter::Antialiasing); pixPainter.fillRect(pixmap.rect(), Qt::white); pixPainter.fillRect(pixmap.rect(), BrushComboDelegate::brushes()[currentIndex()]); pixPainter.drawRect(pixmap.rect()); QPainter genPainter(this); genPainter.drawPixmap(rect.x() + 2, rect.y() + 2, pixmap); }
QPixmap TextBoxContent::renderContent(const QSize & size, Qt::AspectRatioMode /*ratio*/) const { // get the base empty pixmap QSize textSize = boundingRect().size().toSize(); const float w = size.width(), h = size.height(), tw = textSize.width(), th = textSize.height(); if (w < 2 || h < 2 || tw < 2 || th < 2) return QPixmap(); // draw text (centered, maximized keeping aspect ratio) float scale = qMin(w / (tw + 16), h / (th + 16)); QPixmap pix(size); pix.fill(Qt::transparent); QPainter pixPainter(&pix); pixPainter.translate((w - (int)((float)tw * scale)) / 2, (h - (int)((float)th * scale)) / 2); pixPainter.scale(scale, scale); m_text->drawContents(&pixPainter); pixPainter.end(); return pix; }
void QcCanvas::paintEvent( QPaintEvent *e ) { if( _paint && _repaintNeeded ) { if( _resize ) { _pixmap = QPixmap( size() ); _resize = false; _clearOnce = true; } if( _clearOnRefresh || _clearOnce ) { _pixmap.fill( QColor(0,0,0,0) ); _clearOnce = false; } bool opaque_before = testAttribute(Qt::WA_OpaquePaintEvent); QPainter pixPainter( &_pixmap ); Q_EMIT( painting(&pixPainter) ); _repaintNeeded = false; bool opaque_after = testAttribute(Qt::WA_OpaquePaintEvent); if( opaque_before && !opaque_after ) { repaint(); return; } } QPainter p(this); QPalette plt(palette()); if( _bkg.isValid() ) p.fillRect( e->rect(), _bkg ); if( _paint ) p.drawPixmap( e->rect(), _pixmap, e->rect() ); }
QPixmap * KxQtHelper::createPixmap(const QString & path) { QString ext = path.mid(path.lastIndexOf(".")); QPixmap * pixmap = NULL; // svg file must be renderered onto a pixmap if(ext == "svg"){ QSvgRenderer svgRender(path); const QRectF box = svgRender.viewBoxF(); if(svgRender.isValid() && box.isValid()){ pixmap = new QPixmap(box.size().toSize()); pixmap->fill(Qt::transparent); QPainter pixPainter(pixmap); svgRender.render(&pixPainter); } } if(NULL == pixmap){ pixmap = new QPixmap(path); } if(pixmap->isNull()){ delete pixmap; return NULL; } return pixmap; }
//! [8] void RenderArea::paintEvent(QPaintEvent * /* event */) { //! [9] QPainter painter(this); painter.setPen(pen); painter.setBrush(brush); painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing, antialiased); //! [9] if (doRunTest) { QPoint ptCenter = this->rect().center(); if (transformed) { painter.translate(ptCenter); //painter.rotate(60.0); painter.scale(0.99, 0.99); painter.translate(-ptCenter ); } if (this->deviceType4Draw == QInternal::Widget) { qDebug() << "RenderArea: painter.device(): " << g_paintDeviceType[painter.device()->devType()] << ", painter.paintEngine(): " << g_paintEngineType[painter.paintEngine()->type()]; painter.fillRect(this->rect(), Qt::white); KQtTester tester(&painter); tester.runTest(); } else if (this->deviceType4Draw == QInternal::Pixmap) { QPixmap pix(this->width(), this->height()); pix.fill(Qt::white); QPainter pixPainter(&pix); pixPainter.setPen(pen); pixPainter.setBrush(brush); pixPainter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing, antialiased); qDebug() << "Pixmap: pixPainter.device(): " << g_paintDeviceType[pixPainter.device()->devType()] << ", pixPainter.paintEngine(): " << g_paintEngineType[pixPainter.paintEngine()->type()]; KQtTester tester(&pixPainter); tester.runTest(); pixPainter.end(); painter.fillRect(0, 0, 2000, 2000, Qt::white); painter.drawPixmap(0, 0, pix); } else if (this->deviceType4Draw == QInternal::Image) { QImage img(this->width(), this->height(), QImage::Format_ARGB32_Premultiplied); img.fill(0xffffffff); QPainter imgPainter(&img); imgPainter.setPen(pen); imgPainter.setBrush(brush); imgPainter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing, antialiased); qDebug() << "Image: imgPainter.device(): " << g_paintDeviceType[imgPainter.device()->devType()] << ", imgPainter.paintEngine(): " << g_paintEngineType[imgPainter.paintEngine()->type()]; KQtTester tester(&imgPainter); tester.runTest(); imgPainter.end(); painter.fillRect(0, 0, 2000, 2000, Qt::white); painter.drawImage(0, 0, img); } else Q_ASSERT(0); return; } static const QPoint points[4] = { QPoint(10, 80), QPoint(20, 10), QPoint(80, 30), QPoint(90, 70) }; QRect rect(10, 20, 80, 60); QPainterPath path; path.moveTo(20, 80); path.lineTo(20, 30); path.cubicTo(80, 0, 50, 50, 80, 80); int startAngle = 20 * 16; int arcLength = 120 * 16; //! [8] //! [10] for (int x = 0; x < width(); x += 100) { for (int y = 0; y < height(); y += 100) { painter.save(); painter.translate(x, y); //! [10] //! [11] if (transformed) { painter.translate(50, 50); painter.rotate(60.0); painter.scale(0.6, 0.9); painter.translate(-50, -50); } //! [11] //! [12] switch (shape) { case Line: painter.drawLine(rect.bottomLeft(), rect.topRight()); break; case Points: painter.drawPoints(points, 4); break; case Polyline: painter.drawPolyline(points, 4); break; case Polygon: painter.drawPolygon(points, 4); break; case Rect: painter.drawRect(rect); break; case RoundedRect: painter.drawRoundedRect(rect, 25, 25, Qt::RelativeSize); break; case Ellipse: painter.drawEllipse(rect); break; case Arc: painter.drawArc(rect, startAngle, arcLength); break; case Chord: painter.drawChord(rect, startAngle, arcLength); break; case Pie: painter.drawPie(rect, startAngle, arcLength); break; case Path: painter.drawPath(path); break; case Text: painter.drawText(rect, Qt::AlignCenter, tr("hello,oken! ")); break; case Pixmap: painter.drawPixmap(10, 10, pixmap); } //! [12] //! [13] painter.restore(); } } painter.setRenderHint(QPainter::Antialiasing, false); painter.setPen(palette().dark().color()); painter.setBrush(Qt::NoBrush); painter.drawRect(QRect(0, 0, width() - 1, height() - 1)); }
QPixmap ImageRegistry::pixmap( const QString& image, const QSize& size, TomahawkUtils::ImageMode mode, float opacity, QColor tint ) { if ( size.width() < 0 || size.height() < 0 ) { Q_ASSERT( false ); return QPixmap(); } QHash< qint64, QPixmap > subsubcache; QHash< int, QHash< qint64, QPixmap > > subcache; if ( s_cache.contains( image ) ) { subcache = s_cache.value( image ); if ( subcache.contains( mode ) ) { subsubcache = subcache.value( mode ); const qint64 ck = cacheKey( size, opacity, tint ); if ( subsubcache.contains( ck ) ) { return subsubcache.value( ck ); } } } // Image not found in cache. Let's load it. QPixmap pixmap; if ( image.toLower().endsWith( ".svg" ) ) { QSvgRenderer svgRenderer( image ); QPixmap p( size.isNull() || size.height() == 0 || size.width() == 0 ? svgRenderer.defaultSize() : size ); p.fill( Qt::transparent ); QPainter pixPainter( &p ); pixPainter.setOpacity( opacity ); svgRenderer.render( &pixPainter ); pixPainter.end(); if ( tint.alpha() > 0 ) p = TomahawkUtils::tinted( p, tint ); pixmap = p; } else pixmap = QPixmap( image ); if ( !pixmap.isNull() ) { switch ( mode ) { case TomahawkUtils::RoundedCorners: pixmap = TomahawkUtils::createRoundedImage( pixmap, size ); break; default: break; } if ( !size.isNull() && pixmap.size() != size ) { if ( size.width() == 0 ) { pixmap = pixmap.scaledToHeight( size.height(), Qt::SmoothTransformation ); } else if ( size.height() == 0 ) { pixmap = pixmap.scaledToWidth( size.width(), Qt::SmoothTransformation ); } else pixmap = pixmap.scaled( size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ); } putInCache( image, size, mode, opacity, pixmap, tint ); } return pixmap; }