Ejemplo n.º 1
0
void tst_QRegion::regionFromPath()
{
    {
        QPainterPath path;
        path.addRect(0, 0, 10, 10);
        path.addRect(0, 100, 100, 1000);

        QRegion rgn(path.toFillPolygon().toPolygon());
        QCOMPARE(rgn.rects().size(), 2);
        QCOMPARE(rgn.rects().at(0), QRect(0, 0, 10, 10));
        QCOMPARE(rgn.rects().at(1), QRect(0, 100, 100, 1000));

        QCOMPARE(rgn.boundingRect(), QRect(0, 0, 100, 1100));
    }

    {
        QPainterPath path;
        path.addRect(0, 0, 100, 100);
        path.addRect(10, 10, 80, 80);

        QRegion rgn(path.toFillPolygon().toPolygon());
        QCOMPARE(rgn.rects().size(), 4);

        QCOMPARE(rgn.rects().at(0), QRect(0, 0, 100, 10));
        QCOMPARE(rgn.rects().at(1), QRect(0, 10, 10, 80));
        QCOMPARE(rgn.rects().at(2), QRect(90, 10, 10, 80));
        QCOMPARE(rgn.rects().at(3), QRect(0, 90, 100, 10));

        QCOMPARE(rgn.boundingRect(), QRect(0, 0, 100, 100));
    }
}
Ejemplo n.º 2
0
void QgsLayoutItemShape::draw( QgsLayoutItemRenderContext &context )
{
  QPainter *painter = context.renderContext().painter();
  painter->setPen( Qt::NoPen );
  painter->setBrush( Qt::NoBrush );

  double scale = context.renderContext().convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters );

  QPolygonF shapePolygon;

  //shapes with curves must be enlarged before conversion to QPolygonF, or
  //the curves are approximated too much and appear jaggy
  QTransform t = QTransform::fromScale( 100, 100 );
  //inverse transform used to scale created polygons back to expected size
  QTransform ti = t.inverted();

  switch ( mShape )
  {
    case Ellipse:
    {
      //create an ellipse
      QPainterPath ellipsePath;
      ellipsePath.addEllipse( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ) );
      QPolygonF ellipsePoly = ellipsePath.toFillPolygon( t );
      shapePolygon = ti.map( ellipsePoly );
      break;
    }
    case Rectangle:
    {
      //if corner radius set, then draw a rounded rectangle
      if ( mCornerRadius.length() > 0 )
      {
        QPainterPath roundedRectPath;
        double radius = mLayout->convertToLayoutUnits( mCornerRadius ) * scale;
        roundedRectPath.addRoundedRect( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ), radius, radius );
        QPolygonF roundedPoly = roundedRectPath.toFillPolygon( t );
        shapePolygon = ti.map( roundedPoly );
      }
      else
      {
        shapePolygon = QPolygonF( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ) );
      }
      break;
    }
    case Triangle:
    {
      shapePolygon << QPointF( 0, rect().height() * scale );
      shapePolygon << QPointF( rect().width() * scale, rect().height() * scale );
      shapePolygon << QPointF( rect().width() / 2.0 * scale, 0 );
      shapePolygon << QPointF( 0, rect().height() * scale );
      break;
    }
  }

  QList<QPolygonF> rings; //empty list

  symbol()->startRender( context.renderContext() );
  symbol()->renderPolygon( shapePolygon, &rings, nullptr, context.renderContext() );
  symbol()->stopRender( context.renderContext() );
}
Ejemplo n.º 3
0
//! [1]
QPolygonF DiagramDrawItem::createPath()
{
		qreal dx=myPos2.x();
	    qreal dy=myPos2.y();

	    QPainterPath path;
	    QPolygonF polygon;
	    switch (myDiagramType) {
	        case Rectangle:
	            path.moveTo(0, 0);
	            path.lineTo(dx,0);
	            path.lineTo(dx,dy);
	            path.lineTo(0,dy);
	            path.lineTo(0,0);
	            polygon = path.toFillPolygon();
	            break;
	        case Ellipse:
	        	path.addEllipse(0,0,dx,dy);
	        	polygon = path.toFillPolygon();
	            break;
	        default:
	            break;
	            polygon = 0;
	    }
	    return polygon;
}
Ejemplo n.º 4
0
//! [0]
DiagramItem::DiagramItem(DiagramType diagramType, QMenu *contextMenu,
             QGraphicsItem *parent)
    : QGraphicsPolygonItem(parent)
{
    myDiagramType = diagramType;
    myContextMenu = contextMenu;

    QPainterPath path;
    switch (myDiagramType) {
        case StartEnd:
            path.moveTo(200, 50);
            path.arcTo(150, 0, 50, 50, 0, 90);
            path.arcTo(50, 0, 50, 50, 90, 90);
            path.arcTo(50, 50, 50, 50, 180, 90);
            path.arcTo(150, 50, 50, 50, 270, 90);
            path.lineTo(200, 25);
            myPolygon = path.toFillPolygon();
            break;
        case Conditional:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
            break;
        case Step:
            myPolygon << QPointF(-100, -100) << QPointF(100, -100)
                      << QPointF(100, 100) << QPointF(-100, 100)
                      << QPointF(-100, -100);
            break;
        case Oval:
            path.moveTo(-50,50);
            path.arcTo(0, -50, 100, 100, 270,180);
            path.lineTo(-50,-50);
            path.arcTo(-100, -50, 100, 100, 90, 180);
            myPolygon = path.toFillPolygon();
            break;
        case Hexagon:
            myPolygon << QPointF(-100, 0) << QPointF(-50, 100)
                      << QPointF(50, 100) << QPointF(100, 0)
                      << QPointF(50, -100) << QPointF(-50,-100) << QPointF(-100,0);
            break;
        case Pentagon:
            myPolygon << QPointF(100, 50) << QPointF(100, -100)
                      << QPointF(-100, -100) << QPointF(-100, 50)
                      << QPointF(0,100) << QPointF(100,50);
            break;
        default:
            myPolygon << QPointF(-120, -80) << QPointF(-70, 80)
                      << QPointF(120, 80) << QPointF(70, -80)
                      << QPointF(-120, -80);
            break;
    }
    setPolygon(myPolygon);
    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
}
Ejemplo n.º 5
0
void QgsComposerPolygon::_draw( QPainter *painter )
{
  //setup painter scaling to dots so that raster symbology is drawn to scale
  const double dotsPerMM = painter->device()->logicalDpiX() / 25.4;

  QgsMapSettings ms = mComposition->mapSettings();
  ms.setOutputDpi( painter->device()->logicalDpiX() );

  QgsRenderContext context = QgsRenderContext::fromMapSettings( ms );
  context.setPainter( painter );
  context.setForceVectorOutput( true );

  QScopedPointer<QgsExpressionContext> expressionContext;
  expressionContext.reset( createExpressionContext() );
  context.setExpressionContext( *expressionContext.data() );

  painter->scale( 1 / dotsPerMM, 1 / dotsPerMM ); // scale painter from mm to dots
  QTransform t = QTransform::fromScale( dotsPerMM, dotsPerMM );

  QList<QPolygonF> rings; //empty
  QPainterPath polygonPath;
  polygonPath.addPolygon( mPolygon );

  mPolygonStyleSymbol->startRender( context );
  mPolygonStyleSymbol->renderPolygon( polygonPath.toFillPolygon( t ), &rings,
                                      nullptr, context );
  mPolygonStyleSymbol->stopRender( context );
  painter->scale( dotsPerMM, dotsPerMM );
}
Ejemplo n.º 6
0
QRegion PieView::itemRegion(const QModelIndex &index) const
{
        if (!index.isValid())
                return QRegion();

        if (index.column() != 1)
                return itemRect(index);

        if (model()->data(index).toDouble() <= 0.0)
                return QRegion();

        double startAngle = 0.0;
        for (int row = 0; row < model()->rowCount(rootIndex()); ++row) {

                QModelIndex sliceIndex = model()->index(row, 1, rootIndex());
                double value = model()->data(sliceIndex).toDouble();

                if (value > 0.0) {
                        double angle = 360*value/totalValue;

                        if (sliceIndex == index) {
                                QPainterPath slicePath;
                                slicePath.moveTo(totalSize/2, totalSize/2);
                                slicePath.arcTo(margin, margin, margin+pieSize, margin+pieSize,
                                                startAngle, angle);
                                slicePath.closeSubpath();

                                return QRegion(slicePath.toFillPolygon().toPolygon());
                        }
                        startAngle += angle;
                }
        }

        return QRegion();
}
Ejemplo n.º 7
0
void QgsLayoutItemEllipseShape::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * )
{
  QPainter *painter = context.painter();
  painter->setPen( Qt::NoPen );
  painter->setBrush( Qt::NoBrush );

  double scale = context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters );

  //shapes with curves must be enlarged before conversion to QPolygonF, or
  //the curves are approximated too much and appear jaggy
  QTransform t = QTransform::fromScale( 100, 100 );
  //inverse transform used to scale created polygons back to expected size
  QTransform ti = t.inverted();

  //create an ellipse
  QPainterPath ellipsePath;
  ellipsePath.addEllipse( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ) );
  QPolygonF ellipsePoly = ellipsePath.toFillPolygon( t );
  QPolygonF shapePolygon = ti.map( ellipsePoly );

  QList<QPolygonF> rings; //empty list

  symbol()->startRender( context );
  symbol()->renderPolygon( shapePolygon, &rings, nullptr, context );
  symbol()->stopRender( context );
}
Ejemplo n.º 8
0
void QgsLayoutItemRectangularShape::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * )
{
  QPainter *painter = context.painter();
  painter->setPen( Qt::NoPen );
  painter->setBrush( Qt::NoBrush );

  double scale = context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters );

  QPolygonF shapePolygon;
  if ( mCornerRadius.length() > 0 )
  {
    //shapes with curves must be enlarged before conversion to QPolygonF, or
    //the curves are approximated too much and appear jaggy
    QTransform t = QTransform::fromScale( 100, 100 );
    //inverse transform used to scale created polygons back to expected size
    QTransform ti = t.inverted();

    QPainterPath roundedRectPath;
    double radius = mLayout->convertToLayoutUnits( mCornerRadius ) * scale;
    roundedRectPath.addRoundedRect( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ), radius, radius );
    QPolygonF roundedPoly = roundedRectPath.toFillPolygon( t );
    shapePolygon = ti.map( roundedPoly );
  }
  else
  {
    shapePolygon = QPolygonF( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ) );
  }

  QList<QPolygonF> rings; //empty list

  symbol()->startRender( context );
  symbol()->renderPolygon( shapePolygon, &rings, nullptr, context );
  symbol()->stopRender( context );
}
static QPolygonF regionToPolygon(const QRegion &region)
{
    QPainterPath path;
    foreach (const QRect &rect, region.rects())
        path.addRect(rect);
    return path.toFillPolygon();
}
Ejemplo n.º 10
0
/*==================================================================*/
void ModifyStation::initModifyStation(int no)
{
	setModal(true);
	setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint 
		| Qt::WindowMinimizeButtonHint
		| Qt::WindowMaximizeButtonHint);

	//setAttribute(Qt::WA_DeleteOnClose);

	this->setAutoFillBackground(true);
	QPalette palette;
	palette.setColor(QPalette::Background, QColor(35,202,255));
	this->setPalette(palette);

	QPainterPath path;
	QRectF rect = QRectF(this->rect());
	path.addRoundRect(rect,10,10);
	QPolygon polygon= path.toFillPolygon().toPolygon();
	QRegion region(polygon);
	setMask(region);

	//当点击确定时,执行的槽函数为ok
	connect(ui.okButton, SIGNAL(clicked()), this, SLOT(ok()));

	//记录当前焦点所在采集站的编号
	index = no;

	//读取已有的配置或增加新配置
	readIni(no);
}
Ejemplo n.º 11
0
void RotationDialog::resizeEvent(QResizeEvent *)
{
    QPainterPath p;
    p.addRoundedRect(rect(), 20, 20);
    QRegion maskedRegion(p.toFillPolygon().toPolygon());
    setMask(maskedRegion);
    updatePath();
}
Ejemplo n.º 12
0
void OSDialog::resizeEvent(QResizeEvent * event)
{
#ifdef Q_OS_MAC
  QPainterPath path;
  path.addRoundedRect(rect(),9.0,9.0);
  QPolygon p = path.toFillPolygon().toPolygon();
  QRegion region(p);
  setMask(region);
#endif
}
Ejemplo n.º 13
0
/*!
    \fn void QPaintEngine::drawEllipse(const QRectF &rect)

    Reimplement this function to draw the largest ellipse that can be
    contained within rectangle \a rect.

    The default implementation calls drawPolygon().
*/
void QPaintEngine::drawEllipse(const QRectF &rect)
{
    QPainterPath path;
    path.addEllipse(rect);
    if (hasFeature(PainterPaths)) {
        drawPath(path);
    } else {
        QPolygonF polygon = path.toFillPolygon();
        drawPolygon(polygon.data(), polygon.size(), ConvexMode);
    }
}
Ejemplo n.º 14
0
void AMExtendedControlEditorStyledInputDialog::resizeEvent(QResizeEvent *  event )
{
	QDialog::resizeEvent(event);

	// Create a rounded-rectangle mask to shape this window:
	QPainterPath path;
	path.addRoundedRect(0, 0, width(), height(), 14, 14);
	QPolygonF polygonf = path.toFillPolygon(QTransform());
	QRegion maskedRegion( polygonf.toPolygon() );
	setMask(maskedRegion);

}
Ejemplo n.º 15
0
/*!
    \fn QRegion QMatrix::map(const QRegion &region) const
    \overload

    Creates and returns a QRegion object that is a copy of the given
    \a region, mapped into the coordinate system defined by this matrix.

    Calling this method can be rather expensive if rotations or
    shearing are used.
*/
QRegion QMatrix::map(const QRegion &r) const
{
    if (_m11 == 1.0 && _m22 == 1.0 && _m12 == 0.0 && _m21 == 0.0) { // translate or identity
        if (_dx == 0.0 && _dy == 0.0) // Identity
            return r;
        QRegion copy(r);
        copy.translate(qRound(_dx), qRound(_dy));
        return copy;
    }

    QPainterPath p = map(qt_regionToPath(r));
    return p.toFillPolygon().toPolygon();
}
Ejemplo n.º 16
0
void InhibitorArcItem::paintHead(double angle)
{
    (void)angle;
    qreal circleSize = 4;

    QPointF p1 = line().p1()-QPointF(-cos(angle)*circleSize, sin(angle)*circleSize);

    QPainterPath path;
    path.addEllipse(p1, circleSize, circleSize);

    myHead.clear();

    myHead = path.toFillPolygon();
}
Ejemplo n.º 17
0
QPolygonF UBGeometryUtils::lineToPolygon(const QPointF& pStart, const QPointF& pEnd,
        const qreal& pStartWidth, const qreal& pEndWidth)
{

    qreal x1 = pStart.x();
    qreal y1 = pStart.y();

    qreal x2 = pEnd.x();
    qreal y2 = pEnd.y();

    QLineF line(pStart, pEnd);

    qreal alpha = (90.0 - line.angle()) * PI / 180.0;
    qreal hypothenuseStart = pStartWidth / 2;

    qreal hypothenuseEnd = pEndWidth / 2;

    qreal sinAlpha = sin(alpha);
    qreal cosAlpha = cos(alpha);

    // TODO UB 4.x PERF cache sin/cos table
    qreal oppositeStart = sinAlpha * hypothenuseStart;
    qreal adjacentStart = cosAlpha * hypothenuseStart;

    QPointF p1a(x1 - adjacentStart, y1 - oppositeStart);
    QPointF p1b(x1 + adjacentStart, y1 + oppositeStart);

    qreal oppositeEnd = sinAlpha * hypothenuseEnd;
    qreal adjacentEnd = cosAlpha * hypothenuseEnd;

    QPointF p2a(x2 - adjacentEnd, y2 - oppositeEnd);

    QPainterPath painterPath;

    painterPath.moveTo(p1a);
    painterPath.lineTo(p2a);

    painterPath.arcTo(x2 - hypothenuseEnd, y2 - hypothenuseEnd, pEndWidth, pEndWidth, (90.0 + line.angle()), -180.0);

    painterPath.lineTo(p1b);

    painterPath.arcTo(x1 - hypothenuseStart, y1 - hypothenuseStart, pStartWidth, pStartWidth, -1 * (90.0 - line.angle()), -180.0);

    painterPath.closeSubpath();

    return painterPath.toFillPolygon();
}
Ejemplo n.º 18
0
void UiImsBaseDialog::resizeEvent(QResizeEvent *event)
{
    if (mBroudGroundWidget && mBroudGroundPixmap)
    {
        QPalette palette(mBroudGroundWidget->palette());
        palette.setBrush(QPalette::Window, QBrush(mBroudGroundPixmap->scaled(mBroudGroundWidget->size(),
            Qt::IgnoreAspectRatio,
            Qt::SmoothTransformation)));
        mBroudGroundWidget->setPalette(palette);
    }

    //ÉèÖÃÔ²½Ç¾ØÐÎ
    QPainterPath path;
    path.addRoundedRect(rect(), 5, 5);
    setMask(QRegion(path.toFillPolygon().toPolygon()));
    return QDialog::resizeEvent(event);
}
Ejemplo n.º 19
0
Token::Token(DiagramItem *place, QGraphicsItem *parent, QGraphicsScene *scene)
    : QGraphicsPolygonItem(parent, scene)
{
    QPainterPath path;
    path.addEllipse(-4,-4,8,8);
    myPolygon = path.toFillPolygon();
    setPolygon(myPolygon);
    myPlace = place;
    destroy = false;
    animation = new QPropertyAnimation(this, "pos");
    animation->setDuration(10);
    //animation->setEasingCurve(QEasingCurve::InOutQuad);
    animation->setStartValue(place->pos());
    animation->setEndValue(place->pos());
    connect(animation, SIGNAL(stateChanged(QAbstractAnimation::State,QAbstractAnimation::State)),
            this, SLOT(startAnimation(QAbstractAnimation::State,QAbstractAnimation::State)));
    //animation->start();
}
Ejemplo n.º 20
0
QT_END_NAMESPACE
#endif

void tst_QRegion::regionToPath()
{
#ifdef QT_BUILD_INTERNAL

    QFETCH(QPainterPath, path);

    for (int i = 0; i < 360; i += 10) {

        QTransform transform;
        transform.scale(5, 5);
        transform.rotate(i);

        QPainterPath mapped = transform.map(path);
        QRegion region(mapped.toFillPolygon().toPolygon());

        QPainterPath a;
        a.addRegion(region);

        QPainterPath b = qt_regionToPath(region);

        QRect r = a.boundingRect().toAlignedRect();
        QImage ia(r.size(), QImage::Format_RGB32);
        ia.fill(0xffffffff);
        QImage ib = ia;

        QPainter p(&ia);
        p.translate(-r.x(), -r.y());
        p.fillPath(a, Qt::red);
        p.end();
        p.begin(&ib);
        p.translate(-r.x(), -r.y());
        p.fillPath(b, Qt::red);
        p.end();

        QCOMPARE(ia, ib);
        QCOMPARE(a.boundingRect(), b.boundingRect());
    }
#endif
}
Ejemplo n.º 21
0
void YigModField::redrawArc()
{
    qreal arcAngle;
    float arcWidth, arcHeight;

    arcAngle = rotationAngle;

    QPainterPath arcPath;

    arcPath.moveTo(boundingRect().center());
    /*
    //arcPath.moveTo((YigSynthGraphic::MOD_FIELD_SIZE/2) - (YigSynthGraphic::MOD_FIELD_2_SIZE/2), YigSynthGraphic::MOD_FIELD_SIZE/2);
    arcPath.cubicTo(boundingRect().center(), QPointF(0, 0), QPointF(0, boundingRect().height()/2));
    //QPointF endPoint(0, boundingRect().height()/2);
    */

    float size = rotationAngle/360;

    QRectF angleRect = QRectF((boundingRect().width()/2) - ((boundingRect().width()/2) * size),
                              (boundingRect().height()/2) - ((boundingRect().height()/2) * size),
                              boundingRect().width() * size,
                              boundingRect().height() * size);

    /*
    //arcPath.addEllipse(angleRect.center(), angleRect.width() * size /2, angleRect.height() * size /2);
    arcPath.cubicTo(angleRect.topLeft() * size,
                    angleRect.bottomRight() * size,
                    angleRect.bottomRight());
    arcPath.cubicTo(angleRect.topLeft() * size,
                    angleRect.bottomRight() * size,
                    angleRect.center());
    arcPath.setFillRule(Qt::OddEvenFill);*/

    arcPath.arcTo(boundingRect(), -180, rotationAngle);
    arcPath.cubicTo(boundingRect().bottomLeft(), boundingRect().topRight(), boundingRect().center());

    QPolygonF arcPolygon = arcPath.toFillPolygon();
    modArc.setPolygon(arcPolygon);
}
Ejemplo n.º 22
0
QPolygonF UBGeometryUtils::lineToPolygon(const QLineF& pLine, const qreal& pWidth)
{
    qreal x1 = pLine.x1();
    qreal y1 = pLine.y1();

    qreal x2 = pLine.x2();
    qreal y2 = pLine.y2();

    qreal alpha = (90.0 - pLine.angle()) * PI / 180.0;
    qreal hypothenuse = pWidth / 2;

    // TODO UB 4.x PERF cache sin/cos table
    qreal opposite = sin(alpha) * hypothenuse;
    qreal adjacent = cos(alpha) * hypothenuse;

    QPointF p1a(x1 - adjacent, y1 - opposite);
    QPointF p1b(x1 + adjacent, y1 + opposite);

    QPointF p2a(x2 - adjacent, y2 - opposite);
    QPointF p2b(x2 + adjacent, y2 + opposite);

    QPainterPath painterPath;

    painterPath.moveTo(p1a);
    painterPath.lineTo(p2a);

    painterPath.arcTo(x2 - hypothenuse, y2 - hypothenuse, pWidth, pWidth, (90.0 + pLine.angle()), -180.0);

    //painterPath.lineTo(p2b);
    painterPath.lineTo(p1b);

    painterPath.arcTo(x1 - hypothenuse, y1 - hypothenuse, pWidth, pWidth, -1 * (90.0 - pLine.angle()), -180.0);

    painterPath.closeSubpath();

    return painterPath.toFillPolygon();
}
Ejemplo n.º 23
0
void QgsComposerShape::drawShapeUsingSymbol( QPainter* p )
{
  p->save();
  p->setRenderHint( QPainter::Antialiasing );

  //setup painter scaling to dots so that raster symbology is drawn to scale
  double dotsPerMM = p->device()->logicalDpiX() / 25.4;

  //setup render context
  QgsMapSettings ms = mComposition->mapSettings();
  //context units should be in dots
  ms.setOutputDpi( p->device()->logicalDpiX() );
  QgsRenderContext context = QgsRenderContext::fromMapSettings( ms );
  context.setPainter( p );
  context.setForceVectorOutput( true );
  QgsExpressionContext* expressionContext = createExpressionContext();
  context.setExpressionContext( *expressionContext );
  delete expressionContext;

  p->scale( 1 / dotsPerMM, 1 / dotsPerMM ); // scale painter from mm to dots

  //generate polygon to draw
  QList<QPolygonF> rings; //empty list
  QPolygonF shapePolygon;

  //shapes with curves must be enlarged before conversion to QPolygonF, or
  //the curves are approximated too much and appear jaggy
  QTransform t = QTransform::fromScale( 100, 100 );
  //inverse transform used to scale created polygons back to expected size
  QTransform ti = t.inverted();

  switch ( mShape )
  {
    case Ellipse:
    {
      //create an ellipse
      QPainterPath ellipsePath;
      ellipsePath.addEllipse( QRectF( 0, 0, rect().width() * dotsPerMM, rect().height() * dotsPerMM ) );
      QPolygonF ellipsePoly = ellipsePath.toFillPolygon( t );
      shapePolygon = ti.map( ellipsePoly );
      break;
    }
    case Rectangle:
    {
      //if corner radius set, then draw a rounded rectangle
      if ( mCornerRadius > 0 )
      {
        QPainterPath roundedRectPath;
        roundedRectPath.addRoundedRect( QRectF( 0, 0, rect().width() * dotsPerMM, rect().height() * dotsPerMM ), mCornerRadius * dotsPerMM, mCornerRadius * dotsPerMM );
        QPolygonF roundedPoly = roundedRectPath.toFillPolygon( t );
        shapePolygon = ti.map( roundedPoly );
      }
      else
      {
        shapePolygon = QPolygonF( QRectF( 0, 0, rect().width() * dotsPerMM, rect().height() * dotsPerMM ) );
      }
      break;
    }
    case Triangle:
    {
      shapePolygon << QPointF( 0, rect().height() * dotsPerMM );
      shapePolygon << QPointF( rect().width() * dotsPerMM, rect().height() * dotsPerMM );
      shapePolygon << QPointF( rect().width() / 2.0 * dotsPerMM, 0 );
      shapePolygon << QPointF( 0, rect().height() * dotsPerMM );
      break;
    }
  }

  mShapeStyleSymbol->startRender( context );
  mShapeStyleSymbol->renderPolygon( shapePolygon, &rings, nullptr, context );
  mShapeStyleSymbol->stopRender( context );

  p->restore();
}
Ejemplo n.º 24
0
void KPassivePopup::updateMask()
{
    // get screen-geometry for screen our anchor is on
    // (geometry can differ from screen to screen!
    QRect deskRect = QApplication::desktop()->screenGeometry(d->anchor);

    int xh = 70, xl = 40;
    if( width() < 80 )
        xh = xl = 40;
    else if( width() < 110 )
        xh = width() - 40;

    bool bottom = (d->anchor.y() + height()) > ((deskRect.y() + deskRect.height()-48));
    bool right = (d->anchor.x() + width()) > ((deskRect.x() + deskRect.width()-48));

    QPoint corners[4] = {
        QPoint( width() - 50, 10 ),
        QPoint( 10, 10 ),
        QPoint( 10, height() - 50 ),
        QPoint( width() - 50, height() - 50 )
    };

    QBitmap mask( width(), height() );
    mask.clear();
    QPainter p( &mask );
    QBrush brush( Qt::color1, Qt::SolidPattern );
    p.setBrush( brush );

    int i = 0, z = 0;
    for (; i < 4; ++i) {
        QPainterPath path;
        path.moveTo(corners[i].x(),corners[i].y());
        path.arcTo(corners[i].x(),corners[i].y(),40,40, i * 90 , 90);
        QPolygon corner = path.toFillPolygon().toPolygon();

        d->surround.resize( z + corner.count() - 1 );
        for (int s = 1; s < corner.count() - 1; s++, z++) {
            d->surround.setPoint( z, corner[s] );
        }

        if (bottom && i == 2) {
            if (right) {
                d->surround.resize( z + 3 );
                d->surround.setPoint( z++, QPoint( width() - xh, height() - 10 ) );
                d->surround.setPoint( z++, QPoint( width() - 20, height() ) );
                d->surround.setPoint( z++, QPoint( width() - xl, height() - 10 ) );
            } else {
                d->surround.resize( z + 3 );
                d->surround.setPoint( z++, QPoint( xl, height() - 10 ) );
                d->surround.setPoint( z++, QPoint( 20, height() ) );
                d->surround.setPoint( z++, QPoint( xh, height() - 10 ) );
            }
        } else if (!bottom && i == 0) {
            if (right) {
                d->surround.resize( z + 3 );
                d->surround.setPoint( z++, QPoint( width() - xl, 10 ) );
                d->surround.setPoint( z++, QPoint( width() - 20, 0 ) );
                d->surround.setPoint( z++, QPoint( width() - xh, 10 ) );
            } else {
                d->surround.resize( z + 3 );
                d->surround.setPoint( z++, QPoint( xh, 10 ) );
                d->surround.setPoint( z++, QPoint( 20, 0 ) );
                d->surround.setPoint( z++, QPoint( xl, 10 ) );
            }
        }
    }

    d->surround.resize( z + 1 );
    d->surround.setPoint( z, d->surround[0] );
    p.drawPolygon( d->surround );
    setMask(mask);

    move( right ? d->anchor.x() - width() + 20 : ( d->anchor.x() < 11 ? 11 : d->anchor.x() - 20 ),
          bottom ? d->anchor.y() - height() : ( d->anchor.y() < 11 ? 11 : d->anchor.y() ) );

    update();
}
Ejemplo n.º 25
0
void QgsComposerShape::drawShapeUsingSymbol( QPainter* p )
{
  p->save();
  p->setRenderHint( QPainter::Antialiasing );

  QgsRenderContext context;
  context.setPainter( p );
  context.setScaleFactor( 1.0 );
  if ( mComposition->plotStyle() ==  QgsComposition::Preview )
  {
    //Limit resolution of symbol fill if composition is not being exported
    //otherwise zooming into composition slows down renders
    context.setRasterScaleFactor( qMin( horizontalViewScaleFactor(), 3.0 ) );
  }
  else
  {
    context.setRasterScaleFactor( mComposition->printResolution() / 25.4 );
  }

  //generate polygon to draw
  QList<QPolygonF> rings; //empty list
  QPolygonF shapePolygon;

  //shapes with curves must be enlarged before conversion to QPolygonF, or
  //the curves are approximated too much and appear jaggy
  QTransform t = QTransform::fromScale( 100, 100 );
  //inverse transform used to scale created polygons back to expected size
  QTransform ti = t.inverted();

  switch ( mShape )
  {
    case Ellipse:
    {
      //create an ellipse
      QPainterPath ellipsePath;
      ellipsePath.addEllipse( QRectF( 0, 0 , rect().width(), rect().height() ) );
      QPolygonF ellipsePoly = ellipsePath.toFillPolygon( t );
      shapePolygon = ti.map( ellipsePoly );
      break;
    }
    case Rectangle:
    {
      //if corner radius set, then draw a rounded rectangle
      if ( mCornerRadius > 0 )
      {
        QPainterPath roundedRectPath;
        roundedRectPath.addRoundedRect( QRectF( 0, 0 , rect().width(), rect().height() ), mCornerRadius, mCornerRadius );
        QPolygonF roundedPoly = roundedRectPath.toFillPolygon( t );
        shapePolygon = ti.map( roundedPoly );
      }
      else
      {
        shapePolygon = QPolygonF( QRectF( 0, 0, rect().width(), rect().height() ) );
      }
      break;
    }
    case Triangle:
    {
      shapePolygon << QPointF( 0, rect().height() );
      shapePolygon << QPointF( rect().width() , rect().height() );
      shapePolygon << QPointF( rect().width() / 2.0, 0 );
      shapePolygon << QPointF( 0, rect().height() );
      break;
    }
  }

  mShapeStyleSymbol->startRender( context );

  //need to render using atlas feature properties?
  if ( mComposition->atlasComposition().enabled() && mComposition->atlasMode() != QgsComposition::AtlasOff )
  {
    //using an atlas, so render using current atlas feature
    //since there may be data defined symbols using atlas feature properties
    mShapeStyleSymbol->renderPolygon( shapePolygon, &rings, mComposition->atlasComposition().currentFeature(), context );
  }
  else
  {
    mShapeStyleSymbol->renderPolygon( shapePolygon, &rings, 0, context );
  }

  mShapeStyleSymbol->stopRender( context );
  p->restore();
}
Ejemplo n.º 26
0
// ============================================================================
/// finfds aproximates outline of path.
QPolygonF findApproximateOutline( const QPainterPath& path )
{
	//TODO better implementation here
	return path.toFillPolygon( QTransform() );
}
Ejemplo n.º 27
0
Component::Component(QString name, QStringList inputs, QStringList outputs,
		QMenu *contextMenu, QGraphicsItem *parent, QGraphicsScene *scene) :
	QGraphicsPolygonItem(parent, scene), mySlots(inputs), mySignals(outputs),
			myName(name), myContextMenu(contextMenu) {
	minDeviceWidth = 100;
	maxDeviceWidth = 200;
	minDeviceHeight = 100;
	maxDeviceHeight = 600;
	deviceEdgeThickness = 2;
	bewelOfDeviceEdges = 15;
	spaceBetweenPins = 20;
	numberOfSlots = mySlots.size();
	numberOfSignals = mySignals.size();


	QGraphicsTextItem *nameTag = pTailorTextItem(myName, maxDeviceWidth - 20,
			maxDeviceHeight - 40, this);

	widthOfName = nameTag->boundingRect().width();
	heightOfName = nameTag->boundingRect().height();
	nameTag->setPos(-widthOfName / 2, -heightOfName / 2);

	if (widthOfName + 20 < minDeviceWidth)
		myWidth = minDeviceWidth;
	else if (widthOfName + 20 > maxDeviceWidth)
		myWidth = maxDeviceWidth;
	else
		myWidth = widthOfName + 20;
	//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
	Pin *tempPin;

//Búa til Signöl
	Pin::PinType myPinType = Pin::Signal;

	for (int i = 0; i < numberOfSignals; i++) {
		tempPin = new Pin(
				myWidth / 2 - 6 , spaceBetweenPins*i - (numberOfSignals-1)*spaceBetweenPins/2 - 6,
				12, 12, myPinType, this);

		mySignalPins << tempPin;
	}
	tempPin = 0;

//Búa til Slotts
	myPinType = Pin::Slot;

	for (int i = 0; i < numberOfSlots; i++) {
		tempPin = new Pin(
				- myWidth / 2 - 6 , spaceBetweenPins*i - (numberOfSlots-1)*spaceBetweenPins/2 - 6,
				12, 12, myPinType, this);

		mySlotPins << tempPin;
	}
	tempPin = 0;

//	QGraphicsEllipseItem *testEllipse = new QGraphicsEllipseItem(10,10,10,10,this);
//	testEllipse->setBrush(Qt::white);
//	testEllipse->setPen(pen);

// reikna hversu hár kassinn má vera


	//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	int childItemsHeight = qMax(heightOfName, qMax(numberOfSignals*spaceBetweenPins,numberOfSlots*spaceBetweenPins));

	if (childItemsHeight + 40 < minDeviceHeight)
			myHeight = minDeviceHeight;
	else if (childItemsHeight + 40 > maxDeviceHeight)
		myHeight = maxDeviceHeight;
	else
		myHeight = childItemsHeight + 40;

	myTopLeft = QPointF(-(myWidth / 2), -myHeight / 2);
	myBottomRight = QPointF(myWidth / 2, myHeight / 2);

	QPainterPath path;
	path.addRoundRect(QRectF(myTopLeft, myBottomRight), 100, bewelOfDeviceEdges
			* 100 / myHeight);

	myPolygon = path.toFillPolygon();
	setPen(QPen(Qt::black, deviceEdgeThickness));
	setBrush(QColor(200, 200, 200, 255));
	setPolygon(myPolygon);

	setFlag(QGraphicsItem::ItemIsMovable, true);
	setFlag(QGraphicsItem::ItemIsSelectable, true);
	//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
	//	QList<QGraphicsItem *> allTheItems;
	//	allTheItems << nameTag << testPin ;
	//	QGraphicsItemGroup *group = scene->createItemGroup(allTheItems);
	//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

}
Ejemplo n.º 28
0
//! [0]
DiagramItem::DiagramItem(DiagramType diagramType, QMenu *contextMenu,
                         QGraphicsItem *parent, QGraphicsScene *scene)
    : QGraphicsPolygonItem(parent, scene)
{
    myDiagramType = diagramType;
    myContextMenu = contextMenu;
    myGText = new QGraphicsSimpleTextItem(this,scene);

    QPainterPath path;
    switch (myDiagramType) {
    case StartEnd:
        path.moveTo(200, 50);
        path.arcTo(150, 0, 50, 50, 0, 90);
        path.arcTo(50, 0, 50, 50, 90, 90);
        path.arcTo(50, 50, 50, 50, 180, 90);
        path.arcTo(150, 50, 50, 50, 270, 90);
        path.lineTo(200, 25);
        myPolygon = path.toFillPolygon();
        myText = "StartEnd";
        break;
    case Conditional:
        myPolygon << QPointF(-50, 0) << QPointF(0, 50)
                  << QPointF(50, 0) << QPointF(0, -50)
                  << QPointF(-50, 0);
        myText = "Conditional";
        break;
    case Step:
        myPolygon << QPointF(-50, -50) << QPointF(50, -50)
                  << QPointF(50, 50) << QPointF(-50, 50)
                  << QPointF(-50, -50);
        myText = "Step";
        break;
    case Compo:
        myPolygon << QPointF(-50, 0) << QPointF(0, 50)
                  << QPointF(50, 0) << QPointF(0, -50)
                  << QPointF(-50, 0);
        myText = /*"Composition " + */QString::fromUtf8("  \u2229");
        myGText->setScale(3);
        break;
    case Union:
        myPolygon << QPointF(-50, 0) << QPointF(0, 50)
                  << QPointF(50, 0) << QPointF(0, -50)
                  << QPointF(-50, 0);
        myText = /*"Union " +*/QString::fromUtf8("  \u222A");
        myGText->setScale(3);
        break;
    case Constraint:
        myPolygon << QPointF(-50, -50) << QPointF(50, -50)
                  << QPointF(50, 50) << QPointF(-50, 50)
                  << QPointF(-50, -50);
        myText = "Constraint";
        break;
    case Io:
        myPolygon << QPointF(-60, -40) << QPointF(-35, 40)
                  << QPointF(60, 40) << QPointF(35, -40)
                  << QPointF(-60, -40);
        myText = "Input/Output";
        break;
    default:
        myPolygon << QPointF(-60, -40) << QPointF(-35, 40)
                  << QPointF(60, 40) << QPointF(35, -40)
                  << QPointF(-60, -40);
        myText = "Default";
        break;
    }
    setPolygon(myPolygon);
    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);

    myGText->setText(myText);

    myGText->setPos(this->boundingRect().topLeft());

    ub = 0;
    lb = 0;
}
Ejemplo n.º 29
0
void tst_QPainterPath::contains_QPointF_data()
{
    QTest::addColumn<QPainterPath>("path");
    QTest::addColumn<QPointF>("pt");
    QTest::addColumn<bool>("contained");

    QPainterPath path;
    path.addRect(0, 0, 100, 100);

    // #####
    // #   #
    // #   #
    // #   #
    // #####

    QTest::newRow("[0,0] in [0,0,100,100]") << path << QPointF(0, 0) << true;

    QTest::newRow("[99,0] in [0,0,100,100]") << path << QPointF(99, 0) << true;
    QTest::newRow("[0,99] in [0,0,100,100]") << path << QPointF(0, 99) << true;
    QTest::newRow("[99,99] in [0,0,100,100]") << path << QPointF(99, 99) << true;

    QTest::newRow("[99.99,0] in [0,0,100,100]") << path << QPointF(99.99, 0) << true;
    QTest::newRow("[0,99.99] in [0,0,100,100]") << path << QPointF(0, 99.99) << true;
    QTest::newRow("[99.99,99.99] in [0,0,100,100]") << path << QPointF(99.99, 99.99) << true;

    QTest::newRow("[0.01,0.01] in [0,0,100,100]") << path << QPointF(0.01, 0.01) << true;
    QTest::newRow("[0,0.01] in [0,0,100,100]") << path << QPointF(0, 0.01) << true;
    QTest::newRow("[0.01,0] in [0,0,100,100]") << path << QPointF(0.01, 0) << true;

    QTest::newRow("[-0.01,-0.01] in [0,0,100,100]") << path << QPointF(-0.01, -0.01) << false;
    QTest::newRow("[-0,-0.01] in [0,0,100,100]") << path << QPointF(0, -0.01) << false;
    QTest::newRow("[-0.01,0] in [0,0,100,100]") << path << QPointF(-0.01, 0) << false;


    QTest::newRow("[-10,0] in [0,0,100,100]") << path << QPointF(-10, 0) << false;
    QTest::newRow("[100,0] in [0,0,100,100]") << path << QPointF(100, 0) << false;

    QTest::newRow("[0,-10] in [0,0,100,100]") << path << QPointF(0, -10) << false;
    QTest::newRow("[0,100] in [0,0,100,100]") << path << QPointF(0, 100) << false;

    QTest::newRow("[100.1,0] in [0,0,100,100]") << path << QPointF(100.1, 0) << false;
    QTest::newRow("[0,100.1] in [0,0,100,100]") << path << QPointF(0, 100.1) << false;

    path.addRect(50, 50, 100, 100);

    // #####
    // #   #
    // # #####
    // # # # #
    // ##### #
    //   #   #
    //   #####

    QTest::newRow("[49,49] in 2 rects") << path << QPointF(49,49) << true;
    QTest::newRow("[50,50] in 2 rects") << path << QPointF(50,50) << false;
    QTest::newRow("[100,100] in 2 rects") << path << QPointF(100,100) << true;

    path.setFillRule(Qt::WindingFill);
    QTest::newRow("[50,50] in 2 rects (winding)") << path << QPointF(50,50) << true;

    path.addEllipse(0, 0, 150, 150);

    // #####
    // ##  ##
    // # #####
    // # # # #
    // ##### #
    //  ##  ##
    //   #####

    QTest::newRow("[50,50] in complex (winding)") << path << QPointF(50, 50) << true;

    path.setFillRule(Qt::OddEvenFill);
    QTest::newRow("[50,50] in complex (windinf)") << path << QPointF(50, 50) << true;
    QTest::newRow("[49,49] in complex") << path << QPointF(49,49) << false;
    QTest::newRow("[100,100] in complex") << path << QPointF(49,49) << false;


    // unclosed triangle
    path = QPainterPath();
    path.moveTo(100, 100);
    path.lineTo(130, 70);
    path.lineTo(150, 110);

    QTest::newRow("[100,100] in triangle") << path << QPointF(100, 100) << true;
    QTest::newRow("[140,100] in triangle") << path << QPointF(140, 100) << true;
    QTest::newRow("[130,80] in triangle") << path << QPointF(130, 80) << true;

    QTest::newRow("[110,80] in triangle") << path << QPointF(110, 80) << false;
    QTest::newRow("[150,100] in triangle") << path << QPointF(150, 100) << false;
    QTest::newRow("[120,110] in triangle") << path << QPointF(120, 110) << false;

    QRectF base_rect(0, 0, 20, 20);

    path = QPainterPath();
    path.addEllipse(base_rect);

    // not strictly precise, but good enougth to verify fair precision.
    QPainterPath inside;
    inside.addEllipse(base_rect.adjusted(5, 5, -5, -5));
    QPolygonF inside_poly = inside.toFillPolygon();
    for (int i=0; i<inside_poly.size(); ++i)
        QTest::newRow("inside_ellipse") << path << inside_poly.at(i) << true;

    QPainterPath outside;
    outside.addEllipse(base_rect.adjusted(-5, -5, 5, 5));
    QPolygonF outside_poly = outside.toFillPolygon();
    for (int i=0; i<outside_poly.size(); ++i)
        QTest::newRow("outside_ellipse") << path << outside_poly.at(i) << false;

    path = QPainterPath();
    base_rect = QRectF(50, 50, 200, 200);
    path.addEllipse(base_rect);
    path.setFillRule(Qt::WindingFill);

    QTest::newRow("topleft outside ellipse") << path << base_rect.topLeft() << false;
    QTest::newRow("topright outside ellipse") << path << base_rect.topRight() << false;
    QTest::newRow("bottomright outside ellipse") << path << base_rect.bottomRight() << false;
    QTest::newRow("bottomleft outside ellipse") << path << base_rect.bottomLeft() << false;

    // Test horizontal curve segment
    path = QPainterPath();
    path.moveTo(100, 100);
    path.cubicTo(120, 100, 180, 100, 200, 100);
    path.lineTo(150, 200);
    path.closeSubpath();

    QTest::newRow("horizontal cubic, out left") << path << QPointF(0, 100) << false;
    QTest::newRow("horizontal cubic, out right") << path << QPointF(300, 100) <<false;
    QTest::newRow("horizontal cubic, in mid") << path << QPointF(150, 100) << true;
}
Ejemplo n.º 30
0
QRegion ICMainWindow::getRegion() const
{
        QPainterPath path;
        path.addRoundedRect(rect().adjusted(1, -1, -1, 0), 6, 6);
        return QRegion(path.toFillPolygon().toPolygon());
}