Skip to content

Commit

Permalink
Merge pull request #7171 from 3nids/sip_python_repr
Browse files Browse the repository at this point in the history
create Python __repr__ methods for QgsPoint and QgsPointXY
  • Loading branch information
3nids authored Jun 19, 2018
2 parents 2aa2ef4 + ff1988f commit d360150
Show file tree
Hide file tree
Showing 37 changed files with 358 additions and 12 deletions.
5 changes: 5 additions & 0 deletions python/core/auto_generated/geometry/qgscircle.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ Returns true if the circle contains the ``point``.
virtual QString toString( int pointPrecision = 17, int radiusPrecision = 17, int azimuthPrecision = 2 ) const;


SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsCircle: %1>" ).arg( sipCpp->toString() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End
};

/************************************************************************
Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/geometry/qgscircularstring.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ Sets the circular string's points
virtual QgsCircularString *createEmptyWithSameType() const /Factory/;


SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsCircularString: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

protected:

virtual QgsRectangle calculateBoundingBox() const;
Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/geometry/qgscompoundcurve.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ Appends first point if not already closed.
virtual QgsCompoundCurve *createEmptyWithSameType() const /Factory/;


SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsCompoundCurve: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

protected:

virtual QgsRectangle calculateBoundingBox() const;
Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/geometry/qgscurvepolygon.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ Returns approximate rotation angle for a vertex. Usually average angle between a
virtual QgsCurvePolygon *createEmptyWithSameType() const /Factory/;


SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsCurvePolygon: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

protected:

virtual int childCount() const;
Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/geometry/qgsellipse.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ returns a string representation of the ellipse.
Members will be truncated to the specified precision.
%End

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsEllipse: %1>" ).arg( sipCpp->toString() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

protected:

};
Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/geometry/qgsgeometry.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,12 @@ Exports the geometry to WKT
precision parameter added in QGIS 2.4
%End

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsGeometry: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

QString asJson( int precision = 17 ) const;
%Docstring
Exports the geometry to a GeoJSON string.
Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/geometry/qgslinestring.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ of the curve.
virtual QgsLineString *createEmptyWithSameType() const /Factory/;


SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsLineString: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

protected:

virtual QgsRectangle calculateBoundingBox() const;
Expand Down
5 changes: 5 additions & 0 deletions python/core/auto_generated/geometry/qgsmulticurve.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ Returns a copy of the multi curve, where each component curve has had its line d
virtual QgsMultiCurve *createEmptyWithSameType() const /Factory/;


SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsMulitCurve: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

};

Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/geometry/qgsmultilinestring.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ Returns the geometry converted to the more generic curve type :py:class:`QgsMult
virtual QgsMultiLineString *createEmptyWithSameType() const /Factory/;


SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsMultiLineString: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

protected:

virtual bool wktOmitChildType() const;
Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/geometry/qgsmultipoint.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ Multi point geometry collection.
virtual QgsMultiPoint *createEmptyWithSameType() const /Factory/;


SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsMultiPoint: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

protected:

virtual bool wktOmitChildType() const;
Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/geometry/qgsmultipolygon.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ Returns the geometry converted to the more generic curve type :py:class:`QgsMult
virtual QgsMultiPolygon *createEmptyWithSameType() const /Factory/;


SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsMultiPolygon: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

protected:

virtual bool wktOmitChildType() const;
Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/geometry/qgspoint.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,12 @@ Angle undefined. Always returns 0.0
virtual QgsPoint *createEmptyWithSameType() const /Factory/;


SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsPoint: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

protected:

virtual int childCount() const;
Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/geometry/qgspolygon.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ negative if the point lies outside the polygon.
virtual QgsPolygon *createEmptyWithSameType() const /Factory/;


SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsPolygon: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

protected:


Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/geometry/qgsrectangle.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ Converts the rectangle to a 3D box, with the specified

operator QVariant() const;

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsRectangle: %1>" ).arg( sipCpp->asWktCoordinates() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

};


Expand Down
3 changes: 1 addition & 2 deletions python/core/auto_generated/qgspointxy.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ Multiply x and y by the given value

SIP_PYOBJECT __repr__();
%MethodCode
QString str = "(" + QString::number( sipCpp->x() ) + "," + QString::number( sipCpp->y() ) + ")";
//QString str("(%f,%f)").arg(sipCpp->x()).arg(sipCpp->y());
QString str = QStringLiteral( "<QgsPointXY: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
%End

Expand Down
17 changes: 13 additions & 4 deletions scripts/sipify.pl
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ sub detect_non_method_member{
}

# SIP_SKIP
if ( $LINE =~ m/SIP_SKIP|SIP_PYTHON_OPERATOR_/ ){
if ( $LINE =~ m/SIP_SKIP|SIP_PYTHON_SPECIAL_/ ){
dbg_info('SIP SKIP!');
# if multiline definition, remove previous lines
if ( $MULTILINE_DEFINITION != MULTILINE_NO){
Expand All @@ -711,9 +711,18 @@ sub detect_non_method_member{
detect_and_remove_following_body_or_initializerlist();
# line skipped, go to next iteration

if ($LINE =~ m/SIP_PYTHON_OPERATOR_(\w+)\(\s*(\w+)\s*\)/ ){
my $pyop = "${ACTUAL_CLASS}.__" . lc($1) . "__ = lambda self: self.$2()";
dbg_info("PYTHON OPERATOR $pyop");
if ($LINE =~ m/SIP_PYTHON_SPECIAL_(\w+)\(\s*(".*"|\w+)\s*\)/ ){
my $method_or_code = $2;
dbg_info("PYTHON SPECIAL method or code: $method_or_code");
my $pyop = "${ACTUAL_CLASS}.__" . lc($1) . "__ = lambda self: ";
if ( $method_or_code =~ m/^"(.*)"$/ ){
$pyop .= $1;
}
else
{
$pyop .= "self.${method_or_code}()";
}
dbg_info("PYTHON SPECIAL $pyop");
if ($python_output ne ''){
push @OUTPUT_PYTHON, "$pyop\n";
}
Expand Down
10 changes: 10 additions & 0 deletions scripts/sipify_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ count=0

modules=(core gui analysis server)
for module in "${modules[@]}"; do

# clean auto_additions and auto_generated folders
rm -rf python/${module}/auto_additions/*.py
rm -rf python/${module}/auto_generated/*.py
# put back __init__.py
echo '"""
This folder is completed using sipify.pl script
It is not aimed to be manually edited
"""' > python/${module}/auto_additions/__init__.py

while read -r sipfile; do
echo "$sipfile.in"
header=$(${GP}sed -E 's@(.*)\.sip@src/\1.h@; s@auto_generated/@@' <<< $sipfile)
Expand Down
7 changes: 7 additions & 0 deletions src/core/geometry/qgscircle.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ class CORE_EXPORT QgsCircle : public QgsEllipse

QString toString( int pointPrecision = 17, int radiusPrecision = 17, int azimuthPrecision = 2 ) const override;

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsCircle: %1>" ).arg( sipCpp->toString() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
% End
#endif
};

#endif // QGSCIRCLE_H
8 changes: 8 additions & 0 deletions src/core/geometry/qgscircularstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ class CORE_EXPORT QgsCircularString: public QgsCurve

QgsCircularString *createEmptyWithSameType() const override SIP_FACTORY;

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsCircularString: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
% End
#endif

protected:

QgsRectangle calculateBoundingBox() const override;
Expand Down
8 changes: 8 additions & 0 deletions src/core/geometry/qgscompoundcurve.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ class CORE_EXPORT QgsCompoundCurve: public QgsCurve

QgsCompoundCurve *createEmptyWithSameType() const override SIP_FACTORY;

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsCompoundCurve: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
% End
#endif

protected:

QgsRectangle calculateBoundingBox() const override;
Expand Down
8 changes: 8 additions & 0 deletions src/core/geometry/qgscurvepolygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ class CORE_EXPORT QgsCurvePolygon: public QgsSurface

QgsCurvePolygon *createEmptyWithSameType() const override SIP_FACTORY;

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsCurvePolygon: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
% End
#endif

protected:

int childCount() const override;
Expand Down
8 changes: 8 additions & 0 deletions src/core/geometry/qgsellipse.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ class CORE_EXPORT QgsEllipse
*/
virtual QString toString( int pointPrecision = 17, int axisPrecision = 17, int azimuthPrecision = 2 ) const;

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsEllipse: %1>" ).arg( sipCpp->toString() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
% End
#endif

protected:
QgsPoint mCenter;
double mSemiMajorAxis = 0.0;
Expand Down
8 changes: 8 additions & 0 deletions src/core/geometry/qgsgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,14 @@ class CORE_EXPORT QgsGeometry
*/
QString asWkt( int precision = 17 ) const;

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsGeometry: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
% End
#endif

/**
* Exports the geometry to a GeoJSON string.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/core/geometry/qgslinestring.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@ class CORE_EXPORT QgsLineString: public QgsCurve

QgsLineString *createEmptyWithSameType() const override SIP_FACTORY;

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsLineString: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
% End
#endif

protected:

QgsRectangle calculateBoundingBox() const override;
Expand Down
7 changes: 7 additions & 0 deletions src/core/geometry/qgsmulticurve.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ class CORE_EXPORT QgsMultiCurve: public QgsGeometryCollection

QgsMultiCurve *createEmptyWithSameType() const override SIP_FACTORY;

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsMulitCurve: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
% End
#endif

};

Expand Down
8 changes: 8 additions & 0 deletions src/core/geometry/qgsmultilinestring.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ class CORE_EXPORT QgsMultiLineString: public QgsMultiCurve

QgsMultiLineString *createEmptyWithSameType() const override SIP_FACTORY;

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsMultiLineString: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
% End
#endif

protected:

bool wktOmitChildType() const override;
Expand Down
8 changes: 8 additions & 0 deletions src/core/geometry/qgsmultipoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ class CORE_EXPORT QgsMultiPoint: public QgsGeometryCollection

QgsMultiPoint *createEmptyWithSameType() const override SIP_FACTORY;

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsMultiPoint: %1>" ).arg( sipCpp->asWkt() );
sipRes = PyUnicode_FromString( str.toUtf8().data() );
% End
#endif

protected:

bool wktOmitChildType() const override;
Expand Down
Loading

0 comments on commit d360150

Please sign in to comment.