void PreviewWidget::drawShadow(QPainter &painter, QRectF rect) { // Shadow width int width = qBound(4, int(qMax(rect.height(), rect.width())) / 100, 7); painter.save(); painter.setClipRect(rect.adjusted(0, 0, width, width)); QColor lightColor = this->palette().color(QPalette::Background); QColor darkColor = lightColor.darker(160); QRectF shadowRect = rect.adjusted(0, 0, width, width); QLinearGradient lg; lg.setColorAt(0.0, darkColor); lg.setColorAt(1.0, lightColor); QRadialGradient rg; rg.setColorAt(0, darkColor); rg.setColorAt(1, lightColor); rg.setRadius(width); // Right lg.setStart(QPointF(rect.right(), rect.center().y())); lg.setFinalStop(QPointF(shadowRect.right(), rect.center().y())); painter.fillRect(rect.right(), rect.top() + width, width, rect.height() - width, lg); // Bottom lg.setStart(rect.center().x(), rect.bottom()); lg.setFinalStop(rect.center().x(), rect.bottom() + width); painter.fillRect(rect.left() + width, rect.bottom(), rect.width() - width, width, lg); //TopRight QPointF p; p = rect.bottomRight(); rg.setCenter(p); rg.setFocalPoint(p); painter.fillRect(rect.right(), rect.bottom(), width, width, rg); // BottomRight p = rect.topRight(); p.ry() += width; rg.setCenter(p); rg.setFocalPoint(p); painter.fillRect(rect.right(), rect.top(), width, width, rg); //BottomLeft p = rect.bottomLeft(); p.rx() += width; rg.setCenter(p); rg.setFocalPoint(p); painter.fillRect(rect.left(), rect.bottom(), width, width, rg); painter.restore(); }
void EditorMagnifierItem::updateMask() { QRectF current = getRect(); QSize box = QSize( current.width(), current.width() ); // reupdate our mask if (m_imgMask.size() != box) { int radius = box.width() / 2; int ring = radius - 10; m_imgMask = QPixmap(box); m_imgMask.fill(Qt::transparent); QRadialGradient g; g.setCenter(radius, radius); g.setFocalPoint(radius, radius); g.setRadius(radius); g.setColorAt( 1.0, QColor(255, 255, 255, 0) ); g.setColorAt( 0.5, QColor(128, 128, 128, 255) ); QPainter mask(&m_imgMask); mask.setRenderHint(QPainter::Antialiasing); mask.setCompositionMode(QPainter::CompositionMode_Source); mask.setBrush(g); mask.setPen(Qt::NoPen); mask.drawRect( m_imgMask.rect() ); mask.setBrush( QColor(Qt::transparent) ); mask.drawEllipse(g.center(), ring, ring); mask.end(); } }
int MailTreeDelegate::DrawMessageActionIcons (QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index, int height) const { if (Mode_ != MailListMode::Normal) return 0; if (option.state & QStyle::State_MouseOver) return 0; const auto& actionsVar = index.data (MailModel::MailRole::MessageActions); if (actionsVar.isNull ()) return 0; auto actionInfos = actionsVar.value<QList<MessageListActionInfo>> (); if (actionInfos.isEmpty ()) return 0; std::reverse (actionInfos.begin (), actionInfos.end ()); if (ActionsHintsBalls_) height -= Padding * 2; painter->save (); painter->setRenderHint (QPainter::Antialiasing); painter->setPen (Qt::NoPen); auto rect = option.rect; rect.setLeft (rect.right () - height - Padding); rect.setSize ({ height, height }); rect.moveTop (rect.top () + Padding); for (const auto& item : actionInfos) { if (item.Flags_ & MessageListActionFlag::AlwaysPresent) continue; if (ActionsHintsBalls_) { QRadialGradient gradient; gradient.setCoordinateMode (QGradient::ObjectBoundingMode); gradient.setFocalPoint ({ 0.3, 0.3 }); gradient.setCenter ({ 0.5, 0.5 }); gradient.setRadius (0.5); gradient.setColorAt (0, item.ReprColor_.lighter (200)); gradient.setColorAt (1, item.ReprColor_.darker (120)); painter->setBrush (gradient); painter->drawEllipse (rect); } else item.Icon_.paint (painter, rect); rect.moveLeft (rect.left () - height - Padding); } painter->restore (); return option.rect.right () - rect.right (); }
void dot::paint ( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget ) { Q_UNUSED ( widget ); painter->setPen ( Qt::NoPen ); painter->setBrush ( Qt::darkGray ); painter->drawEllipse ( -1, -1, 5, 5 ); QRadialGradient gradient ( -3, -3, 10 ); if( this->isSelected() ) { if ( option->state & QStyle::State_Sunken ) { gradient.setCenter ( 3, 3 ); gradient.setFocalPoint ( 3, 3 ); gradient.setColorAt ( 1, QColor ( Qt::green ).light ( 100 ) ); gradient.setColorAt ( 0, QColor ( Qt::darkGreen ).light ( 100 ) ); } else { gradient.setColorAt ( 0, Qt::green ); gradient.setColorAt ( 1, Qt::darkGreen ); } } else { if ( option->state & QStyle::State_Sunken ) { gradient.setCenter ( 3, 3 ); gradient.setFocalPoint ( 3, 3 ); gradient.setColorAt ( 1, QColor ( Qt::yellow ).light ( 100 ) ); gradient.setColorAt ( 0, QColor ( Qt::darkYellow ).light ( 100 ) ); } else { gradient.setColorAt ( 0, Qt::yellow ); gradient.setColorAt ( 1, Qt::darkYellow ); } } painter->setBrush ( gradient ); painter->setPen ( QPen ( Qt::black, 0 ) ); painter->drawEllipse ( -2, -2, 6, 6 ); painter->scale ( 1.0,-1.0 ); painter->setFont(QFont("Arial", 14)); painter->drawText(0,10,message); }
void BoxStyle::unoptimizedPaint(QPainter* painter, int xOffset, int yOffset, int contentBoxWidth, int contentBoxHeight) const { qreal x = xOffset; qreal y = yOffset; int outlineWidth = outline_.style()!=Qt::NoPen ? outline_.width() : 0; // Move the figure when using antialiasing. The outline will start at a pixel boundary. This makes it sharper. if ( painter->testRenderHint(QPainter::Antialiasing) || painter->testRenderHint(QPainter::HighQualityAntialiasing) ) if ( outline().style() != Qt::NoPen) { x = xOffset + outlineWidth/2.0; y = yOffset + outlineWidth/2.0; } // Draw shadow if ( shadow() != Qt::NoBrush ) { painter->setPen(Qt::NoPen); painter->setBrush(shadow()); painter->drawPath(getRectanglePath(xOffset + xShadowOffset(), yOffset + yShadowOffset(), contentBoxWidth, contentBoxHeight)); } // Draw box. painter->setPen(outline()); // Set the brush and fix the gradient if needed. if ( background().style() == Qt::LinearGradientPattern && background().gradient()->coordinateMode() == QGradient::LogicalMode ) { QLinearGradient g = *(static_cast<const QLinearGradient*> (background().gradient())); g.setStart(x + g.start().x(), y + g.start().y()); g.setFinalStop(x + g.finalStop().x(), y + g.finalStop().y()); painter->setBrush(g); } else if ( background().style() == Qt::RadialGradientPattern && background().gradient()->coordinateMode() == QGradient::LogicalMode ) { QRadialGradient g = *(static_cast<const QRadialGradient*> (background().gradient())); g.setCenter(x + g.center().x(), y + g.center().y()); g.setFocalPoint(x + g.focalPoint().x(), y + g.focalPoint().y()); painter->setBrush(g); } else { painter->setBrush(background()); } painter->drawPath(getRectanglePath(x, y, contentBoxWidth - outlineWidth, contentBoxHeight - outlineWidth)); }
void Highlight::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QColor pen_color = pen.color(); painter->setPen(pen); if(time < total_time ){ //don't want to keep adding them past animation length int expected_num_rings = (int)ceil(frequency * time); if( expected_num_rings > rings.length()){ rings.append(time); } //velocity= 200.0 - (time*total_time*2.0);//pretty cool //velocity= 200.0*(1.0-done_ratio);//pretty cool, about the same //frequency = 3.0*(1.0-done_ratio);//pretty cool for(int i=0; i<rings.length(); i++){ //qreal dist = diameter + ( velocity * (time - rings.at(i))); qreal t = (time - rings.at(i)); qreal dist = diameter + ( velocity * t ) + (0.5 * -5.0 * t*t); //qDebug() << "dist:" << dist << " outerDiameter:" << outerDiameter; QRectF s(x()-dist/2.0, y()-dist/2.0,dist,dist); QRectF r = mapRectFromScene(x()-dist/2.0, y()-dist/2.0,dist,dist); pen.setWidth(20.0+50.0* dist/outerDiameter); QRadialGradient radialGrad; radialGrad.setCenter(r.center()); radialGrad.setFocalPoint(r.center()); radialGrad.setCenterRadius(r.width()/2.0+pen.widthF()/2.0); radialGrad.setFocalRadius(r.width()/2.0-pen.widthF()/2.0); /* not entirely sure I get it, but I think focal radius * needs to be either the center of the pen or its inner edge * while center radius is the outer edge. */ QColor green(0,255,0,255); QColor yellow(255,255,0,255); /* pen_color.setAlphaF(1.0-(dist/outerDiameter)); //surface waves don't inverse square */ green.setAlphaF(1.0-(dist/outerDiameter)); yellow.setAlphaF((1.0-(dist/outerDiameter))); radialGrad.setColorAt(.0, yellow ); radialGrad.setColorAt( .5, green ); radialGrad.setColorAt(1, yellow ); brush = QBrush(radialGrad); pen.setBrush(brush); painter->setPen(pen); painter->drawEllipse(r); } } }
void tst_QBrush::testQRadialGradientSetters() { QRadialGradient rg; QCOMPARE(rg.radius(), qreal(1.0)); QCOMPARE(rg.center(), QPointF(0, 0)); QCOMPARE(rg.focalPoint(), QPointF(0, 0)); rg.setRadius(100); QCOMPARE(rg.radius(), qreal(100.0)); rg.setCenter(101, 102); QCOMPARE(rg.center(), QPointF(101, 102)); rg.setCenter(QPointF(201, 202)); QCOMPARE(rg.center(), QPointF(201, 202)); rg.setFocalPoint(103, 104); QCOMPARE(rg.focalPoint(), QPointF(103, 104)); rg.setFocalPoint(QPointF(203, 204)); QCOMPARE(rg.focalPoint(), QPointF(203, 204)); }
void PainterWidget::updateBrush() { QRadialGradient g; g.setCenter(brush_size/2, brush_size/2); g.setFocalPoint(brush_size/2, brush_size/2); g.setRadius(brush_size/2); g.setColorAt(1.0, Qt::black); g.setColorAt(0, QColor(100,100,100)); QImage mask(brush_size, brush_size,QImage::Format_RGB32); mask.fill(qRgb(0,0,0)); QPainter painter2(&mask); painter2.fillRect(mask.rect(), g); painter2.end(); soft_brush = QImage(brush_size, brush_size,QImage::Format_RGB32); soft_brush.fill(brush_color.rgb()); soft_brush.setAlphaChannel(mask); }
static QGradient loadGradient(const QDomElement &elem) { if (elem.tagName() != QLatin1String("gradientData")) return QLinearGradient(); const QGradient::Type type = stringToGradientType(elem.attribute(QLatin1String("type"))); const QGradient::Spread spread = stringToGradientSpread(elem.attribute(QLatin1String("spread"))); const QGradient::CoordinateMode mode = stringToGradientCoordinateMode(elem.attribute(QLatin1String("coordinateMode"))); QGradient gradient = QLinearGradient(); if (type == QGradient::LinearGradient) { QLinearGradient g; g.setStart(elem.attribute(QLatin1String("startX")).toDouble(), elem.attribute(QLatin1String("startY")).toDouble()); g.setFinalStop(elem.attribute(QLatin1String("endX")).toDouble(), elem.attribute(QLatin1String("endY")).toDouble()); gradient = g; } else if (type == QGradient::RadialGradient) { QRadialGradient g; g.setCenter(elem.attribute(QLatin1String("centerX")).toDouble(), elem.attribute(QLatin1String("centerY")).toDouble()); g.setFocalPoint(elem.attribute(QLatin1String("focalX")).toDouble(), elem.attribute(QLatin1String("focalY")).toDouble()); g.setRadius(elem.attribute(QLatin1String("radius")).toDouble()); gradient = g; } else if (type == QGradient::ConicalGradient) { QConicalGradient g; g.setCenter(elem.attribute(QLatin1String("centerX")).toDouble(), elem.attribute(QLatin1String("centerY")).toDouble()); g.setAngle(elem.attribute(QLatin1String("angle")).toDouble()); gradient = g; } QDomElement stopElem = elem.firstChildElement(); while (!stopElem.isNull()) { QGradientStop stop = loadGradientStop(stopElem); gradient.setColorAt(stop.first, stop.second); stopElem = stopElem.nextSiblingElement(); } gradient.setSpread(spread); gradient.setCoordinateMode(mode); return gradient; }
void BitmapImage::drawEllipse( QRectF rectangle, QPen pen, QBrush brush, QPainter::CompositionMode cm, bool antialiasing) { int width = pen.width(); extend( rectangle.adjusted(-width,-width,width,width).toRect() ); if (brush.style() == Qt::RadialGradientPattern) { QRadialGradient* gradient = (QRadialGradient*)brush.gradient(); gradient->setCenter( gradient->center() - topLeft() ); gradient->setFocalPoint( gradient->focalPoint() - topLeft() ); } if (mImage != NULL && !mImage->isNull() ) { QPainter painter(mImage); painter.setCompositionMode(cm); painter.setRenderHint(QPainter::Antialiasing, antialiasing); painter.setPen(pen); painter.setBrush(brush); //if (brush == Qt::NoBrush) painter.drawEllipse( rectangle.translated(-topLeft()) ); painter.end(); } }
void BitmapImage::drawRect(QRectF rectangle, QPen pen, QBrush brush, QPainter::CompositionMode cm, bool antialiasing) { int width = pen.width(); setCompositionModeBounds(rectangle.adjusted(-width, -width, width, width).toRect(), true, cm); if (brush.style() == Qt::RadialGradientPattern) { QRadialGradient* gradient = (QRadialGradient*)brush.gradient(); gradient->setCenter(gradient->center() - mBounds.topLeft()); gradient->setFocalPoint(gradient->focalPoint() - mBounds.topLeft()); } if (!image()->isNull()) { QPainter painter(image()); painter.setCompositionMode(cm); painter.setRenderHint(QPainter::Antialiasing, antialiasing); painter.setPen(pen); painter.setBrush(brush); painter.drawRect(rectangle.translated(-mBounds.topLeft())); painter.end(); } modification(); }
SpaceObjectSun::SpaceObjectSun(int x, int y) : SpaceObject(x, y, SO_TYPE_SUN) { mPixmap->fill(Qt::transparent); QPainter p(mPixmap); QRadialGradient rd; rd.setCenter(40, 40); rd.setRadius(40); rd.setFocalPoint(40, 40); rd.setColorAt(0, QColor(0xFF, 0xFF, 0xDD, 0xFF)); rd.setColorAt(0.45, QColor(0xFF, 0xFF, 0xDD, 0xFF)); rd.setColorAt(0.55, QColor(0xFF, 0xFF, 0x33, 0xFF)); rd.setColorAt(0.6, QColor("#FF9900")); rd.setColorAt(1, QColor( 0, 0, 0, 0x00 )); p.setPen(Qt::NoPen); p.setBrush( rd ); p.drawEllipse(0, 0, 80, 80); }
/** * 重绘 * 实现只出现垂直滚动条的重载的画图事件处理方法。 * */ void TaskBallMapWidget::paintEvent(QPaintEvent * event ) { // qLogx()<< __FUNCTION__ <<this->mBallBit.size()<<this->mBallMap.size()<<this->mCurrentTaskId; QBrush brush(QColor(0, 0, 0)); //依靠currtaskid 判断是否需要重绘 if (this->mCurrentTaskId < 0) { return; } if (this->mBallBit.size() <= 0) { qLogx()<<__FUNCTION__<<QString("No bit set,"); // leave the widget raw clear return; } //计算图象应该具有的宽度,splitter是以线性变化,而图形则必须以非线性变化,需要对取到的窗口宽度进行圆整 int width = this->mp->size().width() - 20 ; if (width % (this->mBallRadius*2) != 0 ) { width = ( (width / (this->mBallRadius*2)) ) * ( this->mBallRadius * 2) ; } else { width = (width / (this->mBallRadius*2)) * ( this->mBallRadius * 2) ; } //窗口的高度。 int height = this->mBallMap.height() ; if (width != this->mBallMap.width()) { //qLogx()<< "change to width:" << width ; //需要改变图象大小。宽度 this->mBallMap = QImage(width , this->mBallMap.height() , QImage::Format_ARGB32_Premultiplied); //this->mBallMap = this->mBallMap.scaledToWidth( width ) ; //this->resize(width,height); this->setFixedWidth( width ); } if (this->mBallBit.size() != (width/(this->mBallRadius*2)) * (height/(this->mBallRadius*2))) { height = (this->mBallRadius*2) * (this->mBallBit.size())/((width/(this->mBallRadius*2))); if (height % (this->mBallRadius*2) != 0 ) { height = ( 1+ (height / (this->mBallRadius*2)) ) * ( this->mBallRadius * 2); } else { height = ( height / (this->mBallRadius*2) ) * ( this->mBallRadius * 2); } //qLogx()<< "change to height: " << height ; //需要改变图象大小。高度。 //this->mBallMap = this->mBallMap.scaledToHeight( (this->mCurrentLength / (this->mBlockSize))/((width/(this->mBallRadius*2)) ) * ( this->mBallRadius*2 )); this->mBallMap = QImage(width, height, QImage::Format_ARGB32_Premultiplied); //this->mBallMap = this->mBallMap.scaledToHeight(height); //this->resize(width,height); this->setFixedHeight(height); //设置本窗口构件的大小,使得外层的滚动构件能适当的添加或者删除滚动条。 } // ball ball 48 0 // 0 true 600 0 // qLogx()<<__FUNCTION__<<this->mBallBit.size()<<this->mBallMap.isNull()<<width<<height; if (this->mBallMap.isNull()) { /* if not check, show this error: QPainter::begin: Paint device returned engine == 0, type: 3 QPainter::setRenderHint: Painter must be active to set rendering hints QPainter::setBrush: Painter not active QPainter::drawRects: Painter not active QPainter::setBrush: Painter not active QPainter::setBrush: Painter not actiev */ // this->mBallMap = QImage(mp->width(), mp->height(), QImage::Format_ARGB32_Premultiplied); return; } QPainter imgp(&this->mBallMap) ; imgp.setRenderHint(QPainter::Antialiasing, true); imgp.setBrush(brush) ; imgp.drawRect(0, 0, this->mBallMap.width(), this->mBallMap.height()); //更改前景颜色。 brush.setColor(QColor(0,255,0)); imgp.setBrush(brush); ////////// QRadialGradient radgrad ; //梯度法 imgp.setBrush(radgrad) ; int row = 0 , col = 0 ; //当前行,列 int mrow , mcol ; //最大行,列 int srow , scol ; //当前球块所在行,列。 mcol = width / (this->mBallRadius*2) ; mrow = this->mBallBit.size() % mcol == 0 ? (this->mBallBit.size() / mcol) : (this->mBallBit.size() / mcol + 1); int mtb = this->mBallBit.size(); //所有球的个数。 // qLogx()<<"rows:"<<mrow<<"cols:"<<mcol; for (int row = 0; row < mrow; row++) { int sballno; for (int col=0; col < mcol ; col ++) { int bx, by, bw, bh; sballno = row * mcol + col; //本球块在整个文件块中的序号。 if (sballno >= this->mBallBit.size()) { break; } if (this->mBallBit.testBit(sballno) == false) { continue; } bx = col * this->mBallRadius * 2; //本求块的右上角X值。 by = row * this->mBallRadius * 2; //本求块的右上角Y值。 //qLogx()<<"x,y,w"<<bx<<by<<(this->mBallRadius*2); radgrad.setCenter(bx+this->mBallRadius, by + this->mBallRadius); //梯度参数设置 radgrad.setRadius(this->mBallRadius * 2 ); radgrad.setFocalPoint(bx+this->mBallRadius/2,by+this->mBallRadius/2); radgrad.setColorAt(0.0,QColor(255,255,255)); radgrad.setColorAt(0.5,QColor(0,255,0)); radgrad.setColorAt(1.0,QColor(0,0,0)); imgp.setBrush(radgrad); imgp.drawEllipse(bx,by,this->mBallRadius*2 , this->mBallRadius*2); } } //输出到屏幕。 QPainter scrp(this); scrp.drawImage(0, 0, this->mBallMap); }
void AbstractDiagram::paintMarker( QPainter* painter, const MarkerAttributes& markerAttributes, const QBrush& brush, const QPen& pen, const QPointF& pos, const QSizeF& maSize ) { const QPen oldPen( painter->pen() ); // Pen is used to paint 4Pixels - 1 Pixel - Ring and FastCross types. // make sure to use the brush color - see above in those cases. const bool isFourPixels = (markerAttributes.markerStyle() == MarkerAttributes::Marker4Pixels); if( isFourPixels || (markerAttributes.markerStyle() == MarkerAttributes::Marker1Pixel) ){ // for high-performance point charts with tiny point markers: painter->setPen( PrintingParameters::scalePen( QPen( brush.color().light() ) ) ); if( isFourPixels ){ const qreal x = pos.x(); const qreal y = pos.y(); painter->drawLine( QPointF(x-1.0,y-1.0), QPointF(x+1.0,y-1.0) ); painter->drawLine( QPointF(x-1.0,y), QPointF(x+1.0,y) ); painter->drawLine( QPointF(x-1.0,y+1.0), QPointF(x+1.0,y+1.0) ); } painter->drawPoint( pos ); }else{ const PainterSaver painterSaver( painter ); // we only a solid line surrounding the markers QPen painterPen( pen ); painterPen.setStyle( Qt::SolidLine ); painter->setPen( PrintingParameters::scalePen( painterPen ) ); painter->setBrush( brush ); painter->setRenderHint ( QPainter::Antialiasing ); painter->translate( pos ); switch ( markerAttributes.markerStyle() ) { case MarkerAttributes::MarkerCircle: { if ( markerAttributes.threeD() ) { QRadialGradient grad; grad.setCoordinateMode( QGradient::ObjectBoundingMode ); QColor drawColor = brush.color(); grad.setCenter( 0.5, 0.5 ); grad.setRadius( 1.0 ); grad.setFocalPoint( 0.35, 0.35 ); grad.setColorAt( 0.00, drawColor.lighter( 150 ) ); grad.setColorAt( 0.20, drawColor ); grad.setColorAt( 0.50, drawColor.darker( 150 ) ); grad.setColorAt( 0.75, drawColor.darker( 200 ) ); grad.setColorAt( 0.95, drawColor.darker( 250 ) ); grad.setColorAt( 1.00, drawColor.darker( 200 ) ); QBrush newBrush( grad ); newBrush.setMatrix( brush.matrix() ); painter->setBrush( newBrush ); } painter->drawEllipse( QRectF( 0 - maSize.height()/2, 0 - maSize.width()/2, maSize.height(), maSize.width()) ); } break; case MarkerAttributes::MarkerSquare: { QRectF rect( 0 - maSize.width()/2, 0 - maSize.height()/2, maSize.width(), maSize.height() ); painter->drawRect( rect ); break; } case MarkerAttributes::MarkerDiamond: { QVector <QPointF > diamondPoints; QPointF top, left, bottom, right; top = QPointF( 0, 0 - maSize.height()/2 ); left = QPointF( 0 - maSize.width()/2, 0 ); bottom = QPointF( 0, maSize.height()/2 ); right = QPointF( maSize.width()/2, 0 ); diamondPoints << top << left << bottom << right; painter->drawPolygon( diamondPoints ); break; } // both handled on top of the method: case MarkerAttributes::Marker1Pixel: case MarkerAttributes::Marker4Pixels: break; case MarkerAttributes::MarkerRing: { painter->setPen( PrintingParameters::scalePen( QPen( brush.color() ) ) ); painter->setBrush( Qt::NoBrush ); painter->drawEllipse( QRectF( 0 - maSize.height()/2, 0 - maSize.width()/2, maSize.height(), maSize.width()) ); break; } case MarkerAttributes::MarkerCross: { // Note: Markers can have outline, // so just drawing two rects is NOT the solution here! const qreal w02 = maSize.width() * 0.2; const qreal w05 = maSize.width() * 0.5; const qreal h02 = maSize.height()* 0.2; const qreal h05 = maSize.height()* 0.5; QVector <QPointF > crossPoints; QPointF p[12]; p[ 0] = QPointF( -w02, -h05 ); p[ 1] = QPointF( w02, -h05 ); p[ 2] = QPointF( w02, -h02 ); p[ 3] = QPointF( w05, -h02 ); p[ 4] = QPointF( w05, h02 ); p[ 5] = QPointF( w02, h02 ); p[ 6] = QPointF( w02, h05 ); p[ 7] = QPointF( -w02, h05 ); p[ 8] = QPointF( -w02, h02 ); p[ 9] = QPointF( -w05, h02 ); p[10] = QPointF( -w05, -h02 ); p[11] = QPointF( -w02, -h02 ); for( int i=0; i<12; ++i ) crossPoints << p[i]; crossPoints << p[0]; painter->drawPolygon( crossPoints ); break; } case MarkerAttributes::MarkerFastCross: { QPointF left, right, top, bottom; left = QPointF( -maSize.width()/2, 0 ); right = QPointF( maSize.width()/2, 0 ); top = QPointF( 0, -maSize.height()/2 ); bottom= QPointF( 0, maSize.height()/2 ); painter->setPen( PrintingParameters::scalePen( QPen( brush.color() ) ) ); painter->drawLine( left, right ); painter->drawLine( top, bottom ); break; } case MarkerAttributes::NoMarker: break; default: Q_ASSERT_X ( false, "paintMarkers()", "Type item does not match a defined Marker Type." ); } } painter->setPen( oldPen ); }
bool SvgParser::parseGradient(const KoXmlElement &e, const KoXmlElement &referencedBy) { // IMPROVEMENTS: // - Store the parsed colorstops in some sort of a cache so they don't need to be parsed again. // - A gradient inherits attributes it does not have from the referencing gradient. // - Gradients with no color stops have no fill or stroke. // - Gradients with one color stop have a solid color. SvgGraphicsContext *gc = m_context.currentGC(); if (!gc) return false; SvgGradientHelper gradhelper; if (e.hasAttribute("xlink:href")) { QString href = e.attribute("xlink:href").mid(1); if (! href.isEmpty()) { // copy the referenced gradient if found SvgGradientHelper *pGrad = findGradient(href); if (pGrad) gradhelper = *pGrad; } else { //gc->fillType = SvgGraphicsContext::None; // <--- TODO Fill OR Stroke are none return false; } } // Use the gradient that is referencing, or if there isn't one, the original gradient. KoXmlElement b; if (!referencedBy.isNull()) b = referencedBy; else b = e; QString gradientId = b.attribute("id"); if (! gradientId.isEmpty()) { // check if we have this gradient already parsed // copy existing gradient if it exists if (m_gradients.find(gradientId) != m_gradients.end()) gradhelper.copyGradient(m_gradients[ gradientId ].gradient()); } if (b.attribute("gradientUnits") == "userSpaceOnUse") gradhelper.setGradientUnits(SvgGradientHelper::UserSpaceOnUse); // parse color prop QColor c = gc->currentColor; if (!b.attribute("color").isEmpty()) { m_context.styleParser().parseColor(c, b.attribute("color")); } else { // try style attr QString style = b.attribute("style").simplified(); const QStringList substyles = style.split(';', QString::SkipEmptyParts); for (QStringList::ConstIterator it = substyles.begin(); it != substyles.end(); ++it) { QStringList substyle = it->split(':'); QString command = substyle[0].trimmed(); QString params = substyle[1].trimmed(); if (command == "color") m_context.styleParser().parseColor(c, params); } } gc->currentColor = c; if (b.tagName() == "linearGradient") { QLinearGradient *g = new QLinearGradient(); if (gradhelper.gradientUnits() == SvgGradientHelper::ObjectBoundingBox) { g->setCoordinateMode(QGradient::ObjectBoundingMode); g->setStart(QPointF(SvgUtil::fromPercentage(b.attribute("x1", "0%")), SvgUtil::fromPercentage(b.attribute("y1", "0%")))); g->setFinalStop(QPointF(SvgUtil::fromPercentage(b.attribute("x2", "100%")), SvgUtil::fromPercentage(b.attribute("y2", "0%")))); } else { g->setStart(QPointF(SvgUtil::fromUserSpace(b.attribute("x1").toDouble()), SvgUtil::fromUserSpace(b.attribute("y1").toDouble()))); g->setFinalStop(QPointF(SvgUtil::fromUserSpace(b.attribute("x2").toDouble()), SvgUtil::fromUserSpace(b.attribute("y2").toDouble()))); } // preserve color stops if (gradhelper.gradient()) g->setStops(gradhelper.gradient()->stops()); gradhelper.setGradient(g); } else if (b.tagName() == "radialGradient") { QRadialGradient *g = new QRadialGradient(); if (gradhelper.gradientUnits() == SvgGradientHelper::ObjectBoundingBox) { g->setCoordinateMode(QGradient::ObjectBoundingMode); g->setCenter(QPointF(SvgUtil::fromPercentage(b.attribute("cx", "50%")), SvgUtil::fromPercentage(b.attribute("cy", "50%")))); g->setRadius(SvgUtil::fromPercentage(b.attribute("r", "50%"))); g->setFocalPoint(QPointF(SvgUtil::fromPercentage(b.attribute("fx", "50%")), SvgUtil::fromPercentage(b.attribute("fy", "50%")))); } else { g->setCenter(QPointF(SvgUtil::fromUserSpace(b.attribute("cx").toDouble()), SvgUtil::fromUserSpace(b.attribute("cy").toDouble()))); g->setFocalPoint(QPointF(SvgUtil::fromUserSpace(b.attribute("fx").toDouble()), SvgUtil::fromUserSpace(b.attribute("fy").toDouble()))); g->setRadius(SvgUtil::fromUserSpace(b.attribute("r").toDouble())); } // preserve color stops if (gradhelper.gradient()) g->setStops(gradhelper.gradient()->stops()); gradhelper.setGradient(g); } else { return false; } // handle spread method QString spreadMethod = b.attribute("spreadMethod"); if (!spreadMethod.isEmpty()) { if (spreadMethod == "reflect") gradhelper.gradient()->setSpread(QGradient::ReflectSpread); else if (spreadMethod == "repeat") gradhelper.gradient()->setSpread(QGradient::RepeatSpread); else gradhelper.gradient()->setSpread(QGradient::PadSpread); } else gradhelper.gradient()->setSpread(QGradient::PadSpread); // Parse the color stops. The referencing gradient does not have colorstops, // so use the stops from the gradient it references to (e in this case and not b) m_context.styleParser().parseColorStops(gradhelper.gradient(), e); gradhelper.setTransform(SvgUtil::parseTransform(b.attribute("gradientTransform"))); m_gradients.insert(gradientId, gradhelper); return true; }
QBrush XMLParseBase::parseGradient(const QDomElement &element) { QBrush brush; QString gradientStart = element.attribute("start", ""); QString gradientEnd = element.attribute("end", ""); int gradientAlpha = element.attribute("alpha", "255").toInt(); QString direction = element.attribute("direction", "vertical"); QGradientStops stops; if (!gradientStart.isEmpty()) { QColor startColor = QColor(gradientStart); startColor.setAlpha(gradientAlpha); QGradientStop stop(0.0, startColor); stops.append(stop); } for (QDomNode child = element.firstChild(); !child.isNull(); child = child.nextSibling()) { QDomElement childElem = child.toElement(); if (childElem.tagName() == "stop") { float position = childElem.attribute("position", "0").toFloat(); QString color = childElem.attribute("color", ""); int alpha = childElem.attribute("alpha", "-1").toInt(); if (alpha < 0) alpha = gradientAlpha; QColor stopColor = QColor(color); stopColor.setAlpha(alpha); QGradientStop stop((position / 100), stopColor); stops.append(stop); } } if (!gradientEnd.isEmpty()) { QColor endColor = QColor(gradientEnd); endColor.setAlpha(gradientAlpha); QGradientStop stop(1.0, endColor); stops.append(stop); } if (direction == "radial") { QRadialGradient gradient; gradient.setCoordinateMode(QGradient::ObjectBoundingMode); float x1 = 0.5, y1 = 0.5, radius = 0.5; gradient.setCenter(x1,y1); gradient.setFocalPoint(x1,y1); gradient.setRadius(radius); gradient.setStops(stops); brush = QBrush(gradient); } else // Linear { QLinearGradient gradient; gradient.setCoordinateMode(QGradient::ObjectBoundingMode); float x1 = 0.0, y1 = 0.0, x2 = 0.0, y2 = 0.0; if (direction == "vertical") { x1 = 0.5; x2 = 0.5; y1 = 0.0; y2 = 1.0; } else if (direction == "diagonal") { x1 = 0.0; x2 = 1.0; y1 = 0.0; y2 = 1.0; } else // Horizontal { x1 = 0.0; x2 = 1.0; y1 = 0.5; y2 = 0.5; } gradient.setStart(x1, y1); gradient.setFinalStop(x2, y2); gradient.setStops(stops); brush = QBrush(gradient); } return brush; }
void transferGradientPosition(const QGradient * srcGradient, QGradient * dstGradient) { // first check if gradients have the same type if (srcGradient->type() == dstGradient->type()) { switch (srcGradient->type()) { case QGradient::LinearGradient: { const QLinearGradient * src = static_cast<const QLinearGradient*>(srcGradient); QLinearGradient * dst = static_cast<QLinearGradient*>(dstGradient); dst->setStart(src->start()); dst->setFinalStop(src->finalStop()); break; } case QGradient::RadialGradient: { const QRadialGradient * src = static_cast<const QRadialGradient*>(srcGradient); QRadialGradient * dst = static_cast<QRadialGradient*>(dstGradient); dst->setCenter(src->center()); dst->setRadius(src->radius()); dst->setFocalPoint(src->focalPoint()); break; } case QGradient::ConicalGradient: { const QConicalGradient * src = static_cast<const QConicalGradient*>(srcGradient); QConicalGradient * dst = static_cast<QConicalGradient*>(dstGradient); dst->setCenter(src->center()); dst->setAngle(src->angle()); break; } default: return; } return; } // try to preserve gradient positions as best as possible QPointF start, stop; switch (srcGradient->type()) { case QGradient::LinearGradient: { const QLinearGradient * g = static_cast<const QLinearGradient*>(srcGradient); start = g->start(); stop = g->finalStop(); break; } case QGradient::RadialGradient: { const QRadialGradient * g = static_cast<const QRadialGradient*>(srcGradient); start = g->center(); stop = QPointF(g->radius(), 0.0); break; } case QGradient::ConicalGradient: { const QConicalGradient * g = static_cast<const QConicalGradient*>(srcGradient); start = g->center(); qreal radAngle = g->angle() * M_PI / 180.0; stop = QPointF(50.0 * cos(radAngle), 50.*sin(radAngle)); break; } default: start = QPointF(0.0, 0.0); stop = QPointF(50.0, 50.0); } switch (dstGradient->type()) { case QGradient::LinearGradient: { QLinearGradient * g = static_cast<QLinearGradient*>(dstGradient); g->setStart(start); g->setFinalStop(stop); break; } case QGradient::RadialGradient: { QRadialGradient * g = static_cast<QRadialGradient*>(dstGradient); QPointF diff = stop - start; qreal radius = sqrt(diff.x() * diff.x() + diff.y() * diff.y()); g->setCenter(start); g->setFocalPoint(start); g->setRadius(radius); break; } case QGradient::ConicalGradient: { QConicalGradient * g = static_cast<QConicalGradient*>(dstGradient); QPointF diff = stop - start; qreal angle = atan2(diff.y(), diff.x()); if (angle < 0.0) angle += 2 * M_PI; g->setCenter(start); g->setAngle(angle*180 / M_PI); break; } default: return; } }
QPixmap ThumbBarDock::generateFuzzyRect(const QSize& size, const QColor& color, int radius) { QPixmap pix(size); pix.fill(Qt::transparent); QPainter painter(&pix); painter.setRenderHint(QPainter::Antialiasing, true); // Draw corners ---------------------------------- QRadialGradient gradient; gradient.setColorAt(1, Qt::transparent); gradient.setColorAt(0, color); gradient.setRadius(radius); QPoint center; // Top Left center = QPoint(radius, radius); gradient.setCenter(center); gradient.setFocalPoint(center); painter.fillRect(0, 0, radius, radius, gradient); // Top right center = QPoint(size.width() - radius, radius); gradient.setCenter(center); gradient.setFocalPoint(center); painter.fillRect(center.x(), 0, radius, radius, gradient); // Bottom left center = QPoint(radius, size.height() - radius); gradient.setCenter(center); gradient.setFocalPoint(center); painter.fillRect(0, center.y(), radius, radius, gradient); // Bottom right center = QPoint(size.width() - radius, size.height() - radius); gradient.setCenter(center); gradient.setFocalPoint(center); painter.fillRect(center.x(), center.y(), radius, radius, gradient); // Draw borders ---------------------------------- QLinearGradient linearGradient; linearGradient.setColorAt(1, Qt::transparent); linearGradient.setColorAt(0, color); // Top linearGradient.setStart(0, radius); linearGradient.setFinalStop(0, 0); painter.fillRect(radius, 0, size.width() - 2*radius, radius, linearGradient); // Bottom linearGradient.setStart(0, size.height() - radius); linearGradient.setFinalStop(0, size.height()); painter.fillRect(radius, int(linearGradient.start().y()), size.width() - 2*radius, radius, linearGradient); // Left linearGradient.setStart(radius, 0); linearGradient.setFinalStop(0, 0); painter.fillRect(0, radius, radius, size.height() - 2*radius, linearGradient); // Right linearGradient.setStart(size.width() - radius, 0); linearGradient.setFinalStop(size.width(), 0); painter.fillRect(int(linearGradient.start().x()), radius, radius, size.height() - 2*radius, linearGradient); return pix; }
void Ckeyb::drawPressed(QImage *_img) { if (keyPressedList.isEmpty()) { return; } keyPressing.lock(); QMapIterator<int, quint64> i(keyPressedList); keyPressing.unlock(); while (i.hasNext()) { i.next(); QRect _rect = getKey(i.key()).Rect; _rect.setCoords(_rect.x()*pPC->internalImageRatio, _rect.y()*pPC->internalImageRatio, (_rect.x()+_rect.width())*pPC->internalImageRatio, (_rect.y()+_rect.height())*pPC->internalImageRatio); int _m = qMin(_rect.width(), _rect.height())*.25; _rect+= QMargins(_m,_m,_m,_m); int dim = qMax(_rect.width(), _rect.height()); int magnifierSize = dim * 2; int radius = magnifierSize / 2; int ring = radius - 15; QSize box = QSize(magnifierSize, magnifierSize); QPixmap maskPixmap; maskPixmap = QPixmap(box); maskPixmap.fill(Qt::transparent); QRadialGradient g; g.setCenter(radius, radius); g.setFocalPoint(radius, radius); g.setRadius(radius); g.setColorAt(1.0, QColor(64, 64, 64, 0)); g.setColorAt(0.5, QColor(0, 0, 0, 255)); QPainter mask(&maskPixmap); mask.setRenderHint(QPainter::Antialiasing); mask.setCompositionMode(QPainter::CompositionMode_Source); mask.setBrush(g); mask.setPen(Qt::NoPen); mask.drawRect(maskPixmap.rect()); mask.setBrush(QColor(Qt::transparent)); mask.drawEllipse(g.center(), ring, ring); mask.end(); QPoint center = _rect.center() - QPoint(0, radius); center = center + QPoint(0, radius / 2); QPoint corner = center - QPoint(radius, radius); QPoint xy = center * 2 - QPoint(radius, radius); // only set the dimension to the magnified portion QPixmap zoomPixmap = QPixmap(box); zoomPixmap.fill(Qt::lightGray); QPainter pz(&zoomPixmap); // pz.translate(-xy); QRect target=QRect(QPoint(0,0),box); pz.drawImage(target, *_img,_rect); pz.end(); QPainterPath clipPath; clipPath.addEllipse(center, ring, ring); QPainter p(_img); p.setRenderHint(QPainter::Antialiasing); p.setClipPath(clipPath); p.drawPixmap(corner, zoomPixmap); p.setClipping(false); p.drawPixmap(corner, maskPixmap); p.setPen(Qt::gray); p.drawPath(clipPath); p.end(); } }
void LightMaps::paintEvent(QPaintEvent *event) { QPainter p; p.begin(this); m_normalMap->render(&p, event->rect()); p.setPen(Qt::black); p.drawText(rect(), Qt::AlignBottom | Qt::TextWordWrap, "Map data CCBYSA 2009 OpenStreetMap.org contributors"); p.end(); if (zoomed) { int dim = qMin(width(), height()); int magnifierSize = qMin(MAX_MAGNIFIER, dim * 2 / 3); int radius = magnifierSize / 2; int ring = radius - 15; QSize box = QSize(magnifierSize, magnifierSize); // reupdate our mask if (maskPixmap.size() != box) { maskPixmap = QPixmap(box); maskPixmap.fill(Qt::transparent); QRadialGradient g; g.setCenter(radius, radius); g.setFocalPoint(radius, radius); g.setRadius(radius); g.setColorAt(1.0, QColor(255, 255, 255, 0)); g.setColorAt(0.5, QColor(128, 128, 128, 255)); QPainter mask(&maskPixmap); mask.setRenderHint(QPainter::Antialiasing); mask.setCompositionMode(QPainter::CompositionMode_Source); mask.setBrush(g); mask.setPen(Qt::NoPen); mask.drawRect(maskPixmap.rect()); mask.setBrush(QColor(Qt::transparent)); mask.drawEllipse(g.center(), ring, ring); mask.end(); } QPoint center = dragPos - QPoint(0, radius); center = center + QPoint(0, radius / 2); QPoint corner = center - QPoint(radius, radius); QPoint xy = center * 2 - QPoint(radius, radius); // only set the dimension to the magnified portion if (zoomPixmap.size() != box) { zoomPixmap = QPixmap(box); zoomPixmap.fill(Qt::lightGray); } if (true) { QPainter p(&zoomPixmap); p.translate(-xy); m_largeMap->render(&p, QRect(xy, box)); p.end(); } QPainterPath clipPath; clipPath.addEllipse(center, ring, ring); QPainter p(this); p.setRenderHint(QPainter::Antialiasing); p.setClipPath(clipPath); p.drawPixmap(corner, zoomPixmap); p.setClipping(false); p.drawPixmap(corner, maskPixmap); p.setPen(Qt::gray); p.drawPath(clipPath); } if (invert) { QPainter p(this); p.setCompositionMode(QPainter::CompositionMode_Difference); p.fillRect(event->rect(), Qt::white); p.end(); } }
QPixmap PostEffect::generateFuzzyRect(const QSize& size, const QColor& color, int radius) { QPixmap pix(size); const QColor transparent(0, 0, 0, 0); pix.fill(transparent); QPainter painter(&pix); painter.setRenderHint(QPainter::Antialiasing, true); // Fill middle painter.fillRect(pix.rect().adjusted(radius, radius, -radius, -radius), color); // Corners QRadialGradient gradient; gradient.setColorAt(0, color); gradient.setColorAt(1, transparent); gradient.setRadius(radius); QPoint center; // Top Left center = QPoint(radius, radius); gradient.setCenter(center); gradient.setFocalPoint(center); painter.fillRect(0, 0, radius, radius, gradient); // Top right center = QPoint(size.width() - radius, radius); gradient.setCenter(center); gradient.setFocalPoint(center); painter.fillRect(center.x(), 0, radius, radius, gradient); // Bottom left center = QPoint(radius, size.height() - radius); gradient.setCenter(center); gradient.setFocalPoint(center); painter.fillRect(0, center.y(), radius, radius, gradient); // Bottom right center = QPoint(size.width() - radius, size.height() - radius); gradient.setCenter(center); gradient.setFocalPoint(center); painter.fillRect(center.x(), center.y(), radius, radius, gradient); // Borders QLinearGradient linearGradient; linearGradient.setColorAt(0, color); linearGradient.setColorAt(1, transparent); // Top linearGradient.setStart(0, radius); linearGradient.setFinalStop(0, 0); painter.fillRect(radius, 0, size.width() - 2 * radius, radius, linearGradient); // Bottom linearGradient.setStart(0, size.height() - radius); linearGradient.setFinalStop( 0, size.height() ); painter.fillRect(radius, int( linearGradient.start().y() ), size.width() - 2 * radius, radius, linearGradient); // Left linearGradient.setStart(radius, 0); linearGradient.setFinalStop(0, 0); painter.fillRect(0, radius, radius, size.height() - 2 * radius, linearGradient); // Right linearGradient.setStart(size.width() - radius, 0); linearGradient.setFinalStop(size.width(), 0); painter.fillRect(int( linearGradient.start().x() ), radius, radius, size.height() - 2 * radius, linearGradient); return pix; }