static QPainterPath qwtTransformPath( const QwtScaleMap &xMap,
        const QwtScaleMap &yMap, const QPainterPath &path, bool doAlign )
{
    QPainterPath shape;
    shape.setFillRule( path.fillRule() );

    for ( int i = 0; i < path.elementCount(); i++ )
    {
        const QPainterPath::Element &element = path.elementAt( i );

        double x = xMap.transform( element.x );
        double y = yMap.transform( element.y );

        switch( element.type )
        {
            case QPainterPath::MoveToElement:
            {
                if ( doAlign )
                {
                    x = qRound( x );
                    y = qRound( y );
                }

                shape.moveTo( x, y );
                break;
            }
            case QPainterPath::LineToElement:
            {
                if ( doAlign )
                {
                    x = qRound( x );
                    y = qRound( y );
                }

                shape.lineTo( x, y );
                break;
            }
            case QPainterPath::CurveToElement:
            {
                const QPainterPath::Element& element1 = path.elementAt( ++i );
                const double x1 = xMap.transform( element1.x );
                const double y1 = yMap.transform( element1.y );

                const QPainterPath::Element& element2 = path.elementAt( ++i );
                const double x2 = xMap.transform( element2.x );
                const double y2 = yMap.transform( element2.y );

                shape.cubicTo( x, y, x1, y1, x2, y2 );
                break;
            }
            case QPainterPath::CurveToDataElement:
            {
                break;
            }
        }
    }

    return shape;
}
Exemple #2
0
static QLineF labelAttachmentLine( const QPointF &center, const QPointF &start, const QPainterPath &label )
{
    Q_ASSERT ( label.elementCount() == 5 );

    // start is assumed to lie on the outer rim of the slice(!), making it possible to derive the
    // radius of the pie
    const qreal pieRadius = QLineF( center, start ).length();

    // don't draw a line at all when the label is connected to its slice due to at least one of its
    // corners falling inside the slice.
    for ( int i = 0; i < 4; i++ ) { // point 4 is just a duplicate of point 0
        if ( QLineF( label.elementAt( i ), center ).length() < pieRadius ) {
            return QLineF();
        }
    }

    // find the closest edge in the polygon, and its two neighbors
    QPointF closeCorners[3];
    {
        QPointF closest = QPointF( 1000000, 1000000 );
        int closestIndex = 0; // better misbehave than crash
        for ( int i = 0; i < 4; i++ ) { // point 4 is just a duplicate of point 0
            QPointF p = label.elementAt( i );
            if ( QLineF( p, center ).length() < QLineF( closest, center ).length() ) {
                closest = p;
                closestIndex = i;
            }
        }

        closeCorners[ 0 ] = label.elementAt( wraparound( closestIndex - 1, 4 ) );
        closeCorners[ 1 ] = closest;
        closeCorners[ 2 ] = label.elementAt( wraparound( closestIndex + 1, 4 ) );
    }

    QLineF edge1 = QLineF( closeCorners[ 0 ], closeCorners[ 1 ] );
    QLineF edge2 = QLineF( closeCorners[ 1 ], closeCorners[ 2 ] );
    QLineF connection1 = QLineF( ( closeCorners[ 0 ] + closeCorners[ 1 ] ) / 2.0, center );
    QLineF connection2 = QLineF( ( closeCorners[ 1 ] + closeCorners[ 2 ] ) / 2.0, center );
    QLineF ret;
    // prefer the connecting line meeting its edge at a more perpendicular angle
    if ( normProjection( edge1, connection1 ) < normProjection( edge2, connection2 ) ) {
        ret = connection1;
    } else {
        ret = connection2;
    }

    // This tends to look a bit better than not doing it *shrug*
    ret.setP2( ( start + center ) / 2.0 );

    // make the line end at the rim of the slice (not 100% accurate because the line is not precisely radial)
    qreal p1Radius = QLineF( ret.p1(), center ).length();
    ret.setLength( p1Radius - pieRadius );

    return ret;
}
void UBEditableGraphicsPolygonItem::addPoint(const QPointF & point)
{
    QPainterPath painterPath = path();

    if (painterPath.elementCount() == 0)
    {
        painterPath.moveTo(point); // For the first point added, we must use moveTo().
        setPath(painterPath);

        mStartEndPoint[0] = point;
    }
    else
    {
        // If clic on first point, close the polygon
        // TODO à terme : utiliser la surface de la première poignée.
        QPointF pointDepart(painterPath.elementAt(0).x, painterPath.elementAt(0).y);
        QPointF pointFin(painterPath.elementAt(painterPath.elementCount()-1).x, painterPath.elementAt(painterPath.elementCount()-1).y);


        QGraphicsEllipseItem poigneeDepart(pointDepart.x()-10, pointDepart.y()-10, 20, 20);
        QGraphicsEllipseItem poigneeFin(pointFin.x()-10, pointFin.y()-10, 20, 20);

        if (poigneeDepart.contains(point))
        {
            setClosed(true);
        }
        else
        {
            if(poigneeFin.contains(point)){
                mIsInCreationMode = false;
                mOpened = true;
            }else{
                painterPath.lineTo(point);
                setPath(painterPath);
            }
        }

        mStartEndPoint[1] = point;
    }

    if(!mClosed && !mOpened){
        UBFreeHandle *handle = new UBFreeHandle();

        addHandle(handle);

        handle->setParentItem(this);
        handle->setEditableObject(this);
        handle->setPos(point);
        handle->setId(path().elementCount()-1);
        handle->hide();
    }
}
Exemple #4
0
static inline void qwtRevertPath( QPainterPath &path )
{
    if ( path.elementCount() == 4 )
    {
        QPainterPath::Element &el0 = 
            const_cast<QPainterPath::Element &>( path.elementAt(0) );
        QPainterPath::Element &el2 = 
            const_cast<QPainterPath::Element &>( path.elementAt(3) );

        qSwap( el0.x, el2.x );
        qSwap( el0.y, el2.y );
    }
}
Exemple #5
0
    bool tryMergePoints(QPainterPath &path,
                        const QPointF &startPoint,
                        const QPointF &endPoint,
                        qreal &distance,
                        qreal distanceThreshold,
                        bool lastSegment)
    {
        qreal length = (endPoint - startPoint).manhattanLength();

        if (lastSegment || length > distanceThreshold) {
            if (lastSegment) {
                qreal wrappedLength =
                    (endPoint - QPointF(path.elementAt(0))).manhattanLength();

                if (length < distanceThreshold ||
                    wrappedLength < distanceThreshold) {

                    return true;
                }
            }

            distance = 0;
            return false;
        }

        distance += length;

        if (distance > distanceThreshold) {
            path.lineTo(endPoint);
            distance = 0;
        }

        return true;
    }
QString PolaroidBorderDrawer::pathToSvg(const QPainterPath & path) const
{
    int count = path.elementCount();
    QString str_path_d;
    for (int i = 0; i < count; ++i)
    {
        QPainterPath::Element e = path.elementAt(i);
        switch (e.type)
        {
        case QPainterPath::LineToElement:
            str_path_d.append("L " + QString::number(e.x) + ' ' + QString::number(e.y) + ' ');
            break;
        case QPainterPath::MoveToElement:
            str_path_d.append("M " + QString::number(e.x) + ' ' + QString::number(e.y) + ' ');
            break;
        case QPainterPath::CurveToElement:
            str_path_d.append("C " + QString::number(e.x) + ' ' + QString::number(e.y) + ' ');
            break;
        case QPainterPath::CurveToDataElement:
            str_path_d.append(QString::number(e.x) + ' ' + QString::number(e.y) + ' ');
            break;
        }
    }
    str_path_d.append("z");
    return str_path_d;
}
void QgsDxfPaintEngine::drawPath( const QPainterPath& path )
{
  int pathLength = path.elementCount();
  for ( int i = 0; i < pathLength; ++i )
  {
    const QPainterPath::Element& pathElem = path.elementAt( i );
    if ( pathElem.type == QPainterPath::MoveToElement )
    {
      moveTo( pathElem.x, pathElem.y );
    }
    else if ( pathElem.type == QPainterPath::LineToElement )
    {
      lineTo( pathElem.x, pathElem.y );
    }
    else if ( pathElem.type == QPainterPath::CurveToElement )
    {
      curveTo( pathElem.x, pathElem.y );
    }
    else if ( pathElem.type == QPainterPath::CurveToDataElement )
    {
      mCurrentCurve.append( QPointF( pathElem.x, pathElem.y ) );
    }
  }
  endCurve();
  endPolygon();
}
//=============================================================================
int sstQt01PathStoreViewCls::AppendPathSymbol(int          iKey,
                                      QPainterPath oTmpPath,
                                      sstQt01ShapeType_enum eTmpPathType,
                                      QColor       oTmpColor,
                                      QPen         oTmpPen)
{

  //-----------------------------------------------------------------------------
  if ( iKey != 0) return -1;

  int iEleNum = oTmpPath.elementCount();
  for (int ii =1; ii <= iEleNum; ii++)
  {
    sstQt01PathElementCsv3Cls oShapeItemCsv;
    QPainterPath::Element oElement;
    oElement = oTmpPath.elementAt(ii-1);
    oShapeItemCsv.setAll(oElement.type,oElement.x,oElement.y, oTmpColor);
    if (oElement.type == 0)
    {
      oShapeItemCsv.setIPenStyle(oTmpPen.style());
      oShapeItemCsv.setIPenWidth(oTmpPen.width());
      oShapeItemCsv.setShapeType(eTmpPathType);
    }
  }

  return 0;
}
Exemple #9
0
QList<QPointF> Geometry::intersection(QLineF const &line, QPainterPath const &path, qreal eps)
{
	QList<QPointF> result;
	QPointF startPoint;
	QPointF endPoint;

	for (int i = 0; i < path.elementCount(); ++i) {
		QPainterPath::Element const element = path.elementAt(i);

		// Checking that element belongs to the wall path
		if (element.isMoveTo()) {
			endPoint = QPointF(element.x, element.y);
			continue;
		}

		startPoint = endPoint;
		endPoint = QPointF(element.x, element.y);
		QLineF currentLine(startPoint, endPoint);
		QPointF intersectionPoint;
		// TODO: consider curve cases
		if (line.intersect(currentLine, &intersectionPoint) != QLineF::NoIntersection
				&& belongs(intersectionPoint, currentLine, eps))
		{
			result << intersectionPoint;
		}
	}

	return result;
}
void QJsonPaintEngine::drawPath(const QPainterPath &path)
{
	Q_D(QJsonPaintEngine);
#ifdef QT_PICTURE_DEBUG
	qDebug() << " -> drawPath():" << path.boundingRect();
#endif
	QString command;
	d->s << QString("\t{\"p\": \r\n\t\t[\r\n");
	 for(int i=0; i< path.elementCount(); i++)
	 {
		 QPainterPath::Element e = path.elementAt(i);
		 switch (e.type)
		 {
		 case QPainterPath::MoveToElement:
			 command = QString("\t\t{\"m\": {\"x\": %1, \"y\":%2}},\r\n").arg(e.x).arg(e.y); break;
		 case QPainterPath::LineToElement:
			 command = QString("\t\t{\"l\": {\"x\": %1, \"y\":%2}},\r\n").arg(e.x).arg(e.y); break;
		 case QPainterPath::CurveToElement:
			 command = QString("\t\t{\"a\": (\"x1\": %1, \"y1\": %2, \"x2\": %3, \"y2\": %4}},\r\n").arg(d->pos.x()).arg(d->pos.y()).arg(e.x).arg(e.y); break;
		 case QPainterPath::CurveToDataElement:
			 command = QString("\t\t{\"ax\": (\"x1\": %1, \"y2\": %2, \"x2\": %3, \"y2\": %4}},\r\n").arg(d->pos.x()).arg(d->pos.y()).arg(e.x).arg(e.y); break;
		 default: break;
		 }

		  d->s << command;
		  d->pos.setX(e.x);
		  d->pos.setX(e.y);
	 }
	 d->s << QString("\t\t]\r\n\t},\r\n");
}
void tst_QPainterPath::testArcMoveTo()
{
    QFETCH(QRectF, rect);
    QFETCH(qreal, angle);

    QPainterPath path;
    path.arcMoveTo(rect, angle);
    path.arcTo(rect, angle, 30);
    path.arcTo(rect, angle + 30, 30);

    QPointF pos = path.elementAt(0);

    QVERIFY((path.elementCount()-1) % 3 == 0);

    qreal x_radius = rect.width() / 2.0;
    qreal y_radius = rect.height() / 2.0;

    QPointF shouldBe = rect.center()
                       + QPointF(x_radius * cos(ANGLE(angle)), -y_radius * sin(ANGLE(angle)));

    qreal iw = 1 / rect.width();
    qreal ih = 1 / rect.height();

    QVERIFY(pathFuzzyCompare(pos.x() * iw, shouldBe.x() * iw));
    QVERIFY(pathFuzzyCompare(pos.y() * ih, shouldBe.y() * ih));
}
Exemple #12
0
bool Editor::moved( const QPoint& pos )
{
    if ( plot() == NULL )
        return false;

    const QwtScaleMap xMap = plot()->canvasMap( d_editedItem->xAxis() );
    const QwtScaleMap yMap = plot()->canvasMap( d_editedItem->yAxis() );

    const QPointF p1 = QwtScaleMap::invTransform( xMap, yMap, d_currentPos );
    const QPointF p2 = QwtScaleMap::invTransform( xMap, yMap, pos );

#if QT_VERSION >= 0x040600
    const QPainterPath shape = d_editedItem->shape().translated( p2 - p1 );
#else
    const double dx = p2.x() - p1.x();
    const double dy = p2.y() - p1.y();

    QPainterPath shape = d_editedItem->shape();
    for ( int i = 0; i < shape.elementCount(); i++ )
    {
        const QPainterPath::Element &el = shape.elementAt( i );
        shape.setElementPositionAt( i, el.x + dx, el.y + dy );
    }
#endif

    d_editedItem->setShape( shape );
    d_currentPos = pos;

    return true;
}
Exemple #13
0
QPainterPath PathDeformRenderer::lensDeform(const QPainterPath &source, const QPointF &offset)
{
    QPainterPath path;
    path.addPath(source);

    qreal flip = m_intensity / qreal(100);

    for (int i=0; i<path.elementCount(); ++i) {
        const QPainterPath::Element &e = path.elementAt(i);

        qreal x = e.x + offset.x();
        qreal y = e.y + offset.y();

        qreal dx = x - m_pos.x();
        qreal dy = y - m_pos.y();
        qreal len = m_radius - qSqrt(dx * dx + dy * dy);

        if (len > 0) {
            path.setElementPositionAt(i,
                                      x + flip * dx * len / m_radius,
                                      y + flip * dy * len / m_radius);
        } else {
            path.setElementPositionAt(i, x, y);
        }

    }

    return path;
}
Exemple #14
0
    QPainterPath trySimplifyPath(const QPainterPath &path, qreal lengthThreshold)
    {
        QPainterPath newPath;
        QPointF startPoint;
        qreal distance = 0;

        int count = path.elementCount();
        for (int i = 0; i < count; i++) {
            QPainterPath::Element e = path.elementAt(i);
            QPointF endPoint = QPointF(e.x, e.y);

            switch (e.type) {
            case QPainterPath::MoveToElement:
                newPath.moveTo(endPoint);
                break;
            case QPainterPath::LineToElement:
                if (!tryMergePoints(newPath, startPoint, endPoint,
                                    distance, lengthThreshold, i == count - 1)) {

                    newPath.lineTo(endPoint);
                }
                break;
            case QPainterPath::CurveToElement: {
                Q_ASSERT(i + 2 < count);

                if (!tryMergePoints(newPath, startPoint, endPoint,
                                    distance, lengthThreshold, i == count - 1)) {

                    e = path.elementAt(i + 1);
                    Q_ASSERT(e.type == QPainterPath::CurveToDataElement);
                    QPointF ctrl1 = QPointF(e.x, e.y);
                    e = path.elementAt(i + 2);
                    Q_ASSERT(e.type == QPainterPath::CurveToDataElement);
                    QPointF ctrl2 = QPointF(e.x, e.y);
                    newPath.cubicTo(ctrl1, ctrl2, endPoint);
                }

                i += 2;
            }
            default:
                ;
            }
            startPoint = endPoint;
        }

        return newPath;
    }
Exemple #15
0
void BitmapImage::drawPath( QPainterPath path, QPen pen, QBrush brush, QPainter::CompositionMode cm, bool antialiasing)
{
    int width = pen.width();
    qreal inc = 1.0 + width / 20.0; // qreal?
    //if (inc<1) { inc=1.0; }
    extend( path.controlPointRect().adjusted(-width,-width,width,width).toRect() );

    if ( mImage != NULL && !mImage->isNull() )
    {
        QPainter painter(mImage);
        painter.setCompositionMode(cm);
        painter.setRenderHint( QPainter::Antialiasing, antialiasing );
        painter.setPen(pen);
        painter.setBrush(brush);
        painter.setTransform(QTransform().translate(-topLeft().x(), -topLeft().y()));
        painter.setMatrixEnabled(true);
        if (path.length() > 0)
        {
            
            for ( int pt = 0; pt < path.elementCount() - 1; pt++ )
            {
                qreal dx = path.elementAt(pt+1).x - path.elementAt(pt).x;
                qreal dy = path.elementAt(pt+1).y - path.elementAt(pt).y;
                qreal m = sqrt(dx*dx+dy*dy);
                qreal factorx = dx / m;
                qreal factory = dy / m;
                for ( float h = 0.f; h < m; h += inc )
                {
                    qreal x = path.elementAt(pt).x + factorx * h;
                    qreal y = path.elementAt(pt).y + factory * h;
                    painter.drawPoint( QPointF( x, y ) );
                }
            }
            
            //painter.drawPath( path );
        }
        else
        {
            // forces drawing when points are coincident (mousedown)
            painter.drawPoint( path.elementAt(0).x, path.elementAt(0).y );
        }
        painter.end();
    }
}
Exemple #16
0
static QPolygonF polygonFromPainterPath( const QPainterPath &pp )
{
    QPolygonF ret;
    for ( int i = 0; i < pp.elementCount(); i++ ) {
        const QPainterPath::Element& el = pp.elementAt( i );
        Q_ASSERT( el.type == QPainterPath::MoveToElement || el.type == QPainterPath::LineToElement );
        ret.append( el );
    }
    return ret;
}
void tst_QPainterPath::setElementPositionAt()
{
    QPainterPath path(QPointF(42., 42.));
    QCOMPARE(path.elementCount(), 1);
    QVERIFY(path.elementAt(0).type == QPainterPath::MoveToElement);
    QCOMPARE(path.elementAt(0).x, qreal(42.));
    QCOMPARE(path.elementAt(0).y, qreal(42.));

    QPainterPath copy = path;
    copy.setElementPositionAt(0, qreal(0), qreal(0));
    QCOMPARE(copy.elementCount(), 1);
    QVERIFY(copy.elementAt(0).type == QPainterPath::MoveToElement);
    QCOMPARE(copy.elementAt(0).x, qreal(0));
    QCOMPARE(copy.elementAt(0).y, qreal(0));

    QCOMPARE(path.elementCount(), 1);
    QVERIFY(path.elementAt(0).type == QPainterPath::MoveToElement);
    QCOMPARE(path.elementAt(0).x, qreal(42.));
    QCOMPARE(path.elementAt(0).y, qreal(42.));
}
static inline void qwtRevertPath( QPainterPath &path )
{
    if ( path.elementCount() == 4 )
    {
        QPainterPath::Element el0 = path.elementAt(0);
        QPainterPath::Element el3 = path.elementAt(3);

        path.setElementPositionAt( 0, el3.x, el3.y );
        path.setElementPositionAt( 3, el0.x, el0.y );
    }
}
GlyphToSVGHelper::GlyphToSVGHelper(QPainterPath path, QTransform tf)
    :m_path(path), m_transform(tf)
{
    QStringList data;
    QPointF curPos;
    for (int i = 0; i < path.elementCount(); ++i)
    {
        QPainterPath::Element cur = path.elementAt(i);
        QPointF curPoint(tf.map(cur));
        if(cur.isMoveTo())
        {
            curPos = curPoint;
            data << QString("M %1 %2").arg(curPos.x()).arg(curPos.y());
        }
        else if(cur.isLineTo())
        {
            curPos = curPoint;
            data << QString("L %1 %2").arg(curPos.x()).arg(curPos.y());
        }
        else if(cur.isCurveTo())
        {
            QPointF c1 = tf.map(path.elementAt(i + 1));
            QPointF c2 = tf.map(path.elementAt(i + 2));
            data << QString("C %1 %2 %3 %4 %5 %6")
                    .arg(curPoint.x()).arg(curPoint.y())
                    .arg(c1.x()).arg(c1.y())
                    .arg(c2.x()).arg(c2.y());

//             qDebug(data.last().toUtf8());

            i += 2;
            curPos = c2;
        }
        else
            qDebug("Unknown point type");
    }

    m_svg += QString("<path d=\"%1\" fill=\"%2\" />").arg(data.join(" ")).arg("black");

}
QString QTikzPicturePrivate::toTikzPath(const QPainterPath & path) const
{
    if (path.isEmpty()) return QString();

    QStringList pathList;
    QString currentPath;
    int currentControlPoint = 0;

    // convert QPainterPath to a TikZ path string
    for (int i = 0; i < path.elementCount(); i++) {
        const QPainterPath::Element & element = path.elementAt(i);

        switch (element.type) {
            case QPainterPath::MoveToElement: {
                // close current path + append to path list
                if (!currentPath.isEmpty()) {
                    currentPath += " -- cycle";
                    pathList << currentPath;
                }

                // indent with spaces for better readability
                const char * indentString = pathList.count() ? "    " : "";

                // start new path
                currentPath.clear();
                currentPath += indentString + toCoord(element);
                break;
            }
            case QPainterPath::LineToElement: {
                currentPath += " -- " + toCoord(element);
                break;
            }
            case QPainterPath::CurveToElement: {
                currentPath += " .. controls " + toCoord(element);
                currentControlPoint = 1;
                break;
            }
            case QPainterPath::CurveToDataElement: {
                if (currentControlPoint == 1) {
                    currentPath += " and " + toCoord(element);
                    ++currentControlPoint;
                } else if (currentControlPoint == 2) {
                    currentPath += " .. " + toCoord(element);
                    currentControlPoint = 0;
                }
                break;
            }
        }
    }

    return pathList.join("\n");
}
Exemple #21
0
QString TupPathItem::pathToString() const
{
    QPainterPath route = path();
    QString strPath = "";
    QChar t;
    int total = route.elementCount();

    for(int i=0; i < total; i++) {
        QPainterPath::Element e = route.elementAt(i);
        switch (e.type) {
            case QPainterPath::MoveToElement:
            {
                if (t != 'M') {
                    t = 'M';
                    strPath += "M " + QString::number(e.x) + " " + QString::number(e.y) + " ";
                } else {
                    strPath += QString::number(e.x) + " " + QString::number(e.y) + " ";
                }
            }
            break;
            case QPainterPath::LineToElement:
            {
                if (t != 'L') {
                    t = 'L';
                    strPath += " L " + QString::number(e.x) + " " + QString::number(e.y) + " ";
                } else {
                    strPath += QString::number(e.x) + " " + QString::number(e.y) + " ";
                }
            }
            break;
            case QPainterPath::CurveToElement:
            {
                if (t != 'C') {
                    t = 'C';
                    strPath += " C " + QString::number(e.x) + " " + QString::number(e.y) + " ";
                } else {
                    strPath += "  " + QString::number(e.x) + " " + QString::number(e.y) + " ";
                }
            }
            break;
            case QPainterPath::CurveToDataElement:
            {
                if (t == 'C')
                    strPath +=  " " + QString::number(e.x) + "  " + QString::number(e.y) + " ";
            }
            break;
        }
    }

    return strPath;
}
void tst_QPainterPath::translate()
{
    QPainterPath path;

    // Path with no elements.
    QCOMPARE(path.currentPosition(), QPointF());
    path.translate(50.5, 50.5);
    QCOMPARE(path.currentPosition(), QPointF());
    QCOMPARE(path.translated(50.5, 50.5).currentPosition(), QPointF());

    // path.isEmpty(), but we have one MoveTo element that should be translated.
    path.moveTo(50, 50);
    QCOMPARE(path.currentPosition(), QPointF(50, 50));
    path.translate(99.9, 99.9);
    QCOMPARE(path.currentPosition(), QPointF(149.9, 149.9));
    path.translate(-99.9, -99.9);
    QCOMPARE(path.currentPosition(), QPointF(50, 50));
    QCOMPARE(path.translated(-50, -50).currentPosition(), QPointF(0, 0));

    // Complex path.
    QRegion shape(100, 100, 300, 200, QRegion::Ellipse);
    shape -= QRect(225, 175, 50, 50);
    QPainterPath complexPath;
    complexPath.addRegion(shape);
    QVector<QPointF> untranslatedElements;
    for (int i = 0; i < complexPath.elementCount(); ++i)
        untranslatedElements.append(QPointF(complexPath.elementAt(i)));

    const QPainterPath untranslatedComplexPath(complexPath);
    const QPointF offset(100, 100);
    complexPath.translate(offset);

    for (int i = 0; i < complexPath.elementCount(); ++i)
        QCOMPARE(QPointF(complexPath.elementAt(i)) - offset, untranslatedElements.at(i));

    QCOMPARE(complexPath.translated(-offset), untranslatedComplexPath);
}
void UBEditableGraphicsPolygonItem::setClosed(bool closed)
{
    mClosed = closed;

    QPainterPath painterPath = path();
    if (closed)
    {
        painterPath.closeSubpath(); // Automatically add a last point, identic to the first point.
    }
    else
    {
        // if last point and first point are the same, remove the last one, in order to open the path.
        int nbElements = painterPath.elementCount();
        if ( nbElements > 1)
        {
            QPainterPath::Element firstElement = painterPath.elementAt(0);
            QPainterPath::Element lastElement = painterPath.elementAt(nbElements - 1);

            QPointF firstPoint(firstElement.x, firstElement.y);
            QPointF lastPoint(lastElement.x, lastElement.y);

            if (firstPoint == lastPoint)
            {
                // Rebuild the path, excluding the last point.
                QPainterPath newPainterPath(firstPoint);
                for(int iElement=1; iElement<nbElements - 1; iElement++)
                {
                    newPainterPath.lineTo(painterPath.elementAt(iElement));
                }
                painterPath = newPainterPath;
            }
        }
    }

    setPath(painterPath);
    mIsInCreationMode = false;
}
Exemple #24
0
QPainterPath MapObjectItem::shape() const
{
    QPainterPath path = mMapDocument->renderer()->shape(mObject);
#if QT_VERSION >= 0x040600
    path.translate(-pos());
#else
    const QPointF p = pos();
    const int elementCount = path.elementCount();
    for (int i = 0; i < elementCount; i++) {
        const QPainterPath::Element &element = path.elementAt(i);
        path.setElementPositionAt(i, element.x - p.x(), element.y - p.y());
    }
#endif
    return path;
}
Exemple #25
0
QPainterPath LensItem::lensDeform(const QPainterPath &source, const QPointF &offset, double m_radius, double s)
{
	QPainterPath path;
	path.addPath(source);
	for (int i = 0; i < path.elementCount(); ++i)
	{
		const QPainterPath::Element &e = path.elementAt(i);
		double dx = e.x - offset.x();
		double dy = e.y - offset.y();
		double len = m_radius - sqrt(dx * dx + dy * dy);
		if (len > 0)
			path.setElementPositionAt(i, e.x - s * dx * len / m_radius, e.y - s * dy * len / m_radius);
	}
	return path;
}
	QPointF geoGraphicsMultilineItem::label_pos()
	{
		QPainterPath p = this->path();
		int sz = p.elementCount();
		double x = 0, y = 0;
		for (int i=0;i<sz;++i)
		{
			x += p.elementAt(i).x;
			y += p.elementAt(i).y;
		}
		if (sz)
		{
			x /= sz;
			y /= sz;
		}

		return QPointF(x,y);
	}
    void setCornerRects( const QPainterPath &path )
    {
        QPointF pos( 0.0, 0.0 );

        for ( int i = 0; i < path.elementCount(); i++ )
        {
            QPainterPath::Element el = path.elementAt(i); 
            switch( el.type )
            {
                case QPainterPath::MoveToElement:
                case QPainterPath::LineToElement:
                {
                    pos.setX( el.x );
                    pos.setY( el.y );
                    break;
                }
                case QPainterPath::CurveToElement:
                {
                    QRectF r( pos, QPointF( el.x, el.y ) );
                    clipRects += r.normalized();

                    pos.setX( el.x );
                    pos.setY( el.y );

                    break;
                }
                case QPainterPath::CurveToDataElement:
                {
                    if ( clipRects.size() > 0 )
                    {
                        QRectF r = clipRects.last();
                        r.setCoords( 
                            qMin( r.left(), el.x ),
                            qMin( r.top(), el.y ),
                            qMax( r.right(), el.x ),
                            qMax( r.bottom(), el.y )
                        );
                        clipRects.last() = r.normalized();
                    }
                    break;
                }
            }
        }
    }
Exemple #28
0
void QGIViewPart::dumpPath(const char* text,QPainterPath path)
{
        QPainterPath::Element elem;
        Base::Console().Message(">>>%s has %d elements\n",text,path.elementCount());
        char* typeName;
        for(int iElem = 0; iElem < path.elementCount(); iElem++) {
            elem = path.elementAt(iElem);
            if(elem.isMoveTo()) {
                typeName = "MoveTo";
            } else if (elem.isLineTo()) {
                typeName = "LineTo";
            } else if (elem.isCurveTo()) {
                typeName = "CurveTo";
            } else {
                typeName = "Unknown";
            }
            Base::Console().Message(">>>>> element %d: type:%d/%s pos(%.3f,%.3f) M:%d L:%d C:%d\n",iElem,
                                    elem.type,typeName,elem.x,elem.y,elem.isMoveTo(),elem.isLineTo(),elem.isCurveTo());
        }
}
void UBGraphicsPathItem::updateHandle(UBAbstractHandle *handle)
{

    setSelected(true);
    Delegate()->showFrame(false);

    int id = handle->getId();

    QPainterPath oldPath = path();

    QPainterPath newPath;

    if(mClosed && id == 0){
        newPath.moveTo(handle->pos());
        for(int i = 1; i < oldPath.elementCount()-1; i++){
            newPath.lineTo(oldPath.elementAt(i).x, oldPath.elementAt(i).y);
        }
        newPath.lineTo(handle->pos());
    }else{
        for(int i = 0; i < oldPath.elementCount(); i++){
            if(i == 0){
                if(i == id){
                    newPath.moveTo(handle->pos());
                }else{
                    newPath.moveTo(oldPath.elementAt(i).x, oldPath.elementAt(i).y);
                }
            }else{
                if(i == id){
                    newPath.lineTo(handle->pos());
                }else{
                    newPath.lineTo(oldPath.elementAt(i).x, oldPath.elementAt(i).y);
                }
            }
        }
    }


    setPath(newPath);

    if(fillingProperty()->gradient()){
        QLinearGradient g(path().boundingRect().topLeft(), path().boundingRect().topRight());

        g.setColorAt(0, fillingProperty()->gradient()->stops().at(0).second);
        g.setColorAt(1, fillingProperty()->gradient()->stops().at(1).second);

        setFillingProperty(new UBFillProperty(g));
    }
}
	void geoGraphicsMultilineItem::adjust_coords(int nNewLevel)
	{
		if (vi() && nNewLevel != level())
		{
			/** Since the map is zooming from level() to current level,
			 * the map size zoom ratio can be calculated using pow below.
			 * We can get new coord for current zoom level by multiplicative.
			*/
			double ratio = pow(2.0,(nNewLevel - level()));
			QPainterPath p = this->path();
			int sz = p.elementCount();
			for (int i=0;i<sz;++i)
			{
				QPainterPath::Element pt = p.elementAt(i);
				pt.x *= ratio;
				pt.y *= ratio;
				p.setElementPositionAt(i,pt.x,pt.y);
			}
			this->setPath(p);
		}
	}