Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sip] Implement MappedType for unique_ptr to Python conversion #59513

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ Constructs a linestring with a single segment from ``p1`` to ``p2``.
.. versionadded:: 3.2
%End

static QgsLineString *fromBezierCurve( const QgsPoint &start, const QgsPoint &controlPoint1, const QgsPoint &controlPoint2, const QgsPoint &end, int segments = 30 ) /Factory/;
static std::unique_ptr< QgsLineString > fromBezierCurve( const QgsPoint &start, const QgsPoint &controlPoint1, const QgsPoint &controlPoint2, const QgsPoint &end, int segments = 30 );
%Docstring
Returns a new linestring created by segmentizing the bezier curve between ``start`` and ``end``, with
the specified control points.
Expand All @@ -257,7 +257,7 @@ Any z or m values present in the input coordinates will be interpolated along wi
.. versionadded:: 3.10
%End

static QgsLineString *fromQPolygonF( const QPolygonF &polygon ) /Factory/;
static std::unique_ptr< QgsLineString > fromQPolygonF( const QPolygonF &polygon );
%Docstring
Returns a new linestring from a QPolygonF ``polygon`` input.

Expand Down Expand Up @@ -876,15 +876,15 @@ Calculates the minimal 3D bounding box for the geometry.
.. versionadded:: 3.34
%End

QgsLineString *measuredLine( double start, double end ) const /Factory/;
std::unique_ptr< QgsLineString > measuredLine( double start, double end ) const;
%Docstring
Re-write the measure ordinate (or add one, if it isn't already there) interpolating
the measure between the supplied ``start`` and ``end`` values.

.. versionadded:: 3.36
%End

QgsLineString *interpolateM( bool use3DDistance = true ) const /Factory/;
std::unique_ptr< QgsLineString > interpolateM( bool use3DDistance = true ) const;
%Docstring
Returns a copy of this line with all missing (NaN) m values interpolated
from m values of surrounding vertices.
Expand Down
18 changes: 18 additions & 0 deletions python/PyQt6/core/conversions.sip
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,24 @@ which are not wrapped by PyQt:
%End
};

template <TYPE>
%MappedType std::unique_ptr< TYPE > /NoRelease,AllowNone,TypeHint="Optional[TYPE]",TypeHintValue="TYPE"/
{
%TypeHeaderCode
#include <memory>
%End

%ConvertFromTypeCode
const sipTypeDef *sip_type = sipFindType("TYPE*");
return sipConvertFromNewType(sipCpp->release(), sip_type, NULL);
%End

%ConvertToTypeCode
// only one way for now...
return 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume if we end up here we'll want an error to be printed somewhere

%End
};

template <TYPE>
%MappedType QVector< QVector<TYPE> >
{
Expand Down
8 changes: 4 additions & 4 deletions python/core/auto_generated/geometry/qgslinestring.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ Constructs a linestring with a single segment from ``p1`` to ``p2``.
.. versionadded:: 3.2
%End

static QgsLineString *fromBezierCurve( const QgsPoint &start, const QgsPoint &controlPoint1, const QgsPoint &controlPoint2, const QgsPoint &end, int segments = 30 ) /Factory/;
static std::unique_ptr< QgsLineString > fromBezierCurve( const QgsPoint &start, const QgsPoint &controlPoint1, const QgsPoint &controlPoint2, const QgsPoint &end, int segments = 30 );
%Docstring
Returns a new linestring created by segmentizing the bezier curve between ``start`` and ``end``, with
the specified control points.
Expand All @@ -257,7 +257,7 @@ Any z or m values present in the input coordinates will be interpolated along wi
.. versionadded:: 3.10
%End

static QgsLineString *fromQPolygonF( const QPolygonF &polygon ) /Factory/;
static std::unique_ptr< QgsLineString > fromQPolygonF( const QPolygonF &polygon );
%Docstring
Returns a new linestring from a QPolygonF ``polygon`` input.

Expand Down Expand Up @@ -876,15 +876,15 @@ Calculates the minimal 3D bounding box for the geometry.
.. versionadded:: 3.34
%End

QgsLineString *measuredLine( double start, double end ) const /Factory/;
std::unique_ptr< QgsLineString > measuredLine( double start, double end ) const;
%Docstring
Re-write the measure ordinate (or add one, if it isn't already there) interpolating
the measure between the supplied ``start`` and ``end`` values.

.. versionadded:: 3.36
%End

QgsLineString *interpolateM( bool use3DDistance = true ) const /Factory/;
std::unique_ptr< QgsLineString > interpolateM( bool use3DDistance = true ) const;
%Docstring
Returns a copy of this line with all missing (NaN) m values interpolated
from m values of surrounding vertices.
Expand Down
19 changes: 19 additions & 0 deletions python/core/conversions.sip
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,25 @@ which are not wrapped by PyQt:
%End
};

template <TYPE>
%MappedType std::unique_ptr< TYPE > /NoRelease,AllowNone,TypeHint="Optional[TYPE]",TypeHintValue="TYPE"/
{
%TypeHeaderCode
#include <memory>
%End

%ConvertFromTypeCode
const sipTypeDef *sip_type = sipFindType("TYPE*");
return sipConvertFromNewType(sipCpp->release(), sip_type, NULL);
%End

%ConvertToTypeCode
// only one way for now...
return 0;
%End
};


template <TYPE>
%MappedType QVector< QVector<TYPE> >
{
Expand Down
24 changes: 12 additions & 12 deletions src/core/geometry/qgslinestring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ static double cubicInterpolate( double a, double b,
return A * b * b * b + 3 * B * b * b * a + 3 * C * b * a * a + D * a * a * a;
}

QgsLineString *QgsLineString::fromBezierCurve( const QgsPoint &start, const QgsPoint &controlPoint1, const QgsPoint &controlPoint2, const QgsPoint &end, int segments )
std::unique_ptr< QgsLineString > QgsLineString::fromBezierCurve( const QgsPoint &start, const QgsPoint &controlPoint1, const QgsPoint &controlPoint2, const QgsPoint &end, int segments )
{
if ( segments == 0 )
return new QgsLineString();
return std::make_unique< QgsLineString >();

QVector<double> x;
x.resize( segments + 1 );
Expand Down Expand Up @@ -242,10 +242,10 @@ QgsLineString *QgsLineString::fromBezierCurve( const QgsPoint &start, const QgsP
if ( mData )
*mData = end.m();

return new QgsLineString( x, y, z, m );
return std::make_unique< QgsLineString >( x, y, z, m );
}

QgsLineString *QgsLineString::fromQPolygonF( const QPolygonF &polygon )
std::unique_ptr< QgsLineString > QgsLineString::fromQPolygonF( const QPolygonF &polygon )
{
QVector< double > x;
QVector< double > y;
Expand All @@ -262,7 +262,7 @@ QgsLineString *QgsLineString::fromQPolygonF( const QPolygonF &polygon )
src++;
}

return new QgsLineString( x, y );
return std::make_unique< QgsLineString >( x, y );
}

QgsLineString *QgsLineString::clone() const
Expand Down Expand Up @@ -2608,19 +2608,19 @@ void QgsLineString::transformVertices( const std::function<QgsPoint( const QgsPo
}


QgsLineString *QgsLineString::measuredLine( double start, double end ) const
std::unique_ptr< QgsLineString > QgsLineString::measuredLine( double start, double end ) const
{
const int nbpoints = numPoints();
std::unique_ptr< QgsLineString > cloned( clone() );

if ( !cloned->convertTo( QgsWkbTypes::addM( mWkbType ) ) )
{
return cloned.release();
return cloned;
}

if ( isEmpty() || ( nbpoints < 2 ) )
{
return cloned.release();
return cloned;
}

const double range = end - start;
Expand All @@ -2641,17 +2641,17 @@ QgsLineString *QgsLineString::measuredLine( double start, double end ) const
*mOut++ = 0.0;
}

return cloned.release();
return cloned;
}

QgsLineString *QgsLineString::interpolateM( bool use3DDistance ) const
std::unique_ptr< QgsLineString > QgsLineString::interpolateM( bool use3DDistance ) const
{
if ( !isMeasure() )
return nullptr;

const int totalPoints = numPoints();
if ( totalPoints < 2 )
return clone();
return std::unique_ptr< QgsLineString >( clone() );

const double *xData = mX.constData();
const double *yData = mY.constData();
Expand Down Expand Up @@ -2814,5 +2814,5 @@ QgsLineString *QgsLineString::interpolateM( bool use3DDistance ) const
prevZ = thisZ;
++i;
}
return new QgsLineString( xOut, yOut, zOut, mOut );
return std::make_unique< QgsLineString >( xOut, yOut, zOut, mOut );
}
8 changes: 4 additions & 4 deletions src/core/geometry/qgslinestring.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,14 @@ class CORE_EXPORT QgsLineString: public QgsCurve
*
* \since QGIS 3.10
*/
static QgsLineString *fromBezierCurve( const QgsPoint &start, const QgsPoint &controlPoint1, const QgsPoint &controlPoint2, const QgsPoint &end, int segments = 30 ) SIP_FACTORY;
static std::unique_ptr< QgsLineString > fromBezierCurve( const QgsPoint &start, const QgsPoint &controlPoint1, const QgsPoint &controlPoint2, const QgsPoint &end, int segments = 30 );

/**
* Returns a new linestring from a QPolygonF \a polygon input.
*
* \since QGIS 3.10
*/
static QgsLineString *fromQPolygonF( const QPolygonF &polygon ) SIP_FACTORY;
static std::unique_ptr< QgsLineString > fromQPolygonF( const QPolygonF &polygon );
#ifndef SIP_RUN
private:
bool fuzzyHelper( double epsilon,
Expand Down Expand Up @@ -1204,7 +1204,7 @@ class CORE_EXPORT QgsLineString: public QgsCurve
*
* \since QGIS 3.36
*/
QgsLineString *measuredLine( double start, double end ) const SIP_FACTORY;
std::unique_ptr< QgsLineString > measuredLine( double start, double end ) const;

/**
* Returns a copy of this line with all missing (NaN) m values interpolated
Expand All @@ -1219,7 +1219,7 @@ class CORE_EXPORT QgsLineString: public QgsCurve
* \see lineLocatePointByM()
* \since QGIS 3.38
*/
QgsLineString *interpolateM( bool use3DDistance = true ) const SIP_FACTORY;
std::unique_ptr< QgsLineString > interpolateM( bool use3DDistance = true ) const;

/**
* Attempts to locate a point on the linestring by m value.
Expand Down
3 changes: 2 additions & 1 deletion src/core/geometry/qgsmultilinestring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ QgsMultiLineString *QgsMultiLineString::measuredLine( double start, double end )
const double subStart{ ( start + range *lengthSoFar / length ) };
const double subEnd{ ( start + range * ( lengthSoFar + subLength ) / length ) };

result->addGeometry( qgsgeometry_cast<QgsLineString *>( geometryN( i ) )->measuredLine( subStart, subEnd ) );
std::unique_ptr< QgsLineString > measuredLine = qgsgeometry_cast<QgsLineString *>( geometryN( i ) )->measuredLine( subStart, subEnd );
result->addGeometry( measuredLine.release() );

lengthSoFar += subLength;
}
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsmaplayerutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class QgsRectangle;
class QgsCoordinateReferenceSystem;
class QgsCoordinateTransformContext;
class QgsAbstractDatabaseProviderConnection;
class QgsGeometry;

/**
* \ingroup core
Expand Down
15 changes: 10 additions & 5 deletions src/core/symbology/qgsfillsymbollayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3288,12 +3288,14 @@ void QgsLinePatternFillSymbolLayer::renderPolygon( const QPolygonF &points, cons
case Qgis::LineClipMode::ClipToIntersection:
{
shapePolygon = std::make_unique< QgsPolygon >();
shapePolygon->setExteriorRing( QgsLineString::fromQPolygonF( points ) );
std::unique_ptr< QgsLineString > fromPolygon = QgsLineString::fromQPolygonF( points );
shapePolygon->setExteriorRing( fromPolygon.release() );
if ( rings )
{
for ( const QPolygonF &ring : *rings )
{
shapePolygon->addInteriorRing( QgsLineString::fromQPolygonF( ring ) );
std::unique_ptr< QgsLineString > fromRing = QgsLineString::fromQPolygonF( ring );
shapePolygon->addInteriorRing( fromRing.release() );
}
}
shapeEngine.reset( QgsGeometry::createGeometryEngine( shapePolygon.get() ) );
Expand Down Expand Up @@ -4132,12 +4134,14 @@ void QgsPointPatternFillSymbolLayer::renderPolygon( const QPolygonF &points, con
case Qgis::MarkerClipMode::CompletelyWithin:
{
shapePolygon = std::make_unique< QgsPolygon >();
shapePolygon->setExteriorRing( QgsLineString::fromQPolygonF( points ) );
std::unique_ptr< QgsLineString > fromPolygon = QgsLineString::fromQPolygonF( points );
shapePolygon->setExteriorRing( fromPolygon.release() );
if ( rings )
{
for ( const QPolygonF &ring : *rings )
{
shapePolygon->addInteriorRing( QgsLineString::fromQPolygonF( ring ) );
std::unique_ptr< QgsLineString > fromRing = QgsLineString::fromQPolygonF( ring );
shapePolygon->addInteriorRing( fromRing.release() );
}
}
shapeEngine.reset( QgsGeometry::createGeometryEngine( shapePolygon.get() ) );
Expand Down Expand Up @@ -5627,7 +5631,8 @@ void QgsRandomMarkerFillSymbolLayer::render( QgsRenderContext &context, const QV
QgsPolygon *poly = qgsgeometry_cast< QgsPolygon * >( geom.get() );
for ( const QPolygonF &ring : part.rings )
{
poly->addInteriorRing( QgsLineString::fromQPolygonF( ring ) );
std::unique_ptr< QgsLineString > fromRing = QgsLineString::fromQPolygonF( ring );
poly->addInteriorRing( fromRing.release() );
}
}
if ( !geom.isGeosValid() )
Expand Down
3 changes: 2 additions & 1 deletion src/core/symbology/qgsgeometrygeneratorsymbollayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ void QgsGeometryGeneratorSymbolLayer::render( QgsSymbolRenderContext &context, Q
{
for ( const QPolygonF &ring : *rings )
{
polygon->addInteriorRing( QgsLineString::fromQPolygonF( ring ) );
std::unique_ptr< QgsLineString > fromRing = QgsLineString::fromQPolygonF( ring );
polygon->addInteriorRing( fromRing.release() );
}
}
drawGeometry = QgsGeometry( std::move( polygon ) );
Expand Down
Loading