Skip to content

Commit

Permalink
Avoid sip property code for old api compatibility
Browse files Browse the repository at this point in the history
This causes crashes on Qt6. Use Python monkey patching instead.
  • Loading branch information
nyalldawson committed Feb 16, 2024
1 parent ef1d7a1 commit 2357d80
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 29 deletions.
6 changes: 6 additions & 0 deletions python/PyQt6/core/__init__.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ GEOPROJ4 = geoProj4()
GEO_EPSG_CRS_AUTHID = geoEpsgCrsAuthId()
GEO_NONE = geoNone()

# TODO QGIS 4.0 - remove, require use of explicit getter/setter

QgsAbstractMetadataBaseValidator.ValidationResult.identifier = property(QgsAbstractMetadataBaseValidator.ValidationResult._identifier)
QgsAbstractMetadataBaseValidator.ValidationResult.identifier = QgsAbstractMetadataBaseValidator.ValidationResult.identifier.setter(QgsAbstractMetadataBaseValidator.ValidationResult.setIdentifier)


# TODO QGIS 4.0 - remove, replaced by Qgis.LinePlacementFlags
class LinePlacementFlags:
OnLine = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,15 @@ Constructor for ValidationResult.
QString section;


%Property( name = identifier, get = _identifier, set = _setIdentifier )

QVariant _identifier() const;
QVariant identifier() const /PyName=_identifier/;
%Docstring
Returns the optional identifier for the failed metadata item.
For instance, in list type metadata elements this
will be set to the list index of the failed metadata
item.
%End

void _setIdentifier( QVariant identifier );
void setIdentifier( const QVariant &identifier );
%Docstring
Sets the optional ``identifier`` for the failed metadata item.
For instance, in list type metadata elements this
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# The following has been generated automatically from src/gui/qgslayermetadataresultsmodel.h
QgsLayerMetadataResultsModel.Metadata = QgsLayerMetadataResultsModel.Roles.Metadata
QgsLayerMetadataResultsModel.Roles = QgsLayerMetadataResultsModel.CustomRole
# monkey patching scoped based enum
QgsLayerMetadataResultsModel.Metadata = QgsLayerMetadataResultsModel.CustomRole.Metadata
QgsLayerMetadataResultsModel.Metadata.is_monkey_patched = True
QgsLayerMetadataResultsModel.Metadata.__doc__ = "Layer metadata role"
QgsLayerMetadataResultsModel.CustomRole.__doc__ = "The Roles enum represents the user roles for the model.\n\n.. note::\n\n Prior to QGIS 3.36 this was available as QgsLayerMetadataResultsModel.Roles\n\n.. versionadded:: 3.36\n\n" + '* ``Metadata``: ' + QgsLayerMetadataResultsModel.CustomRole.Metadata.__doc__
# --
QgsLayerMetadataResultsModel.CustomRole.baseClass = QgsLayerMetadataResultsModel
QgsLayerMetadataResultsModel.Identifier = QgsLayerMetadataResultsModel.Sections.Identifier
QgsLayerMetadataResultsModel.Title = QgsLayerMetadataResultsModel.Sections.Title
QgsLayerMetadataResultsModel.Abstract = QgsLayerMetadataResultsModel.Sections.Abstract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ Load/Reload model data synchronously.
Load/Reload model data asynchronously using threads.
%End

enum Roles /BaseType=IntEnum/

enum class CustomRole /BaseType=IntEnum/
{
//! Layer metadata role
Metadata
Metadata,
};

enum Sections /BaseType=IntEnum/
Expand Down
5 changes: 5 additions & 0 deletions python/core/__init__.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ GEO_EPSG_CRS_AUTHID = geoEpsgCrsAuthId()
GEO_NONE = geoNone()


# TODO QGIS 4.0 - remove, require use of explicit getter/setter

QgsAbstractMetadataBaseValidator.ValidationResult.identifier = property(QgsAbstractMetadataBaseValidator.ValidationResult._identifier)
QgsAbstractMetadataBaseValidator.ValidationResult.identifier = QgsAbstractMetadataBaseValidator.ValidationResult.identifier.setter(QgsAbstractMetadataBaseValidator.ValidationResult.setIdentifier)


# TODO QGIS 4.0 - remove, replaced by Qgis.LinePlacementFlags
class LinePlacementFlags:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,15 @@ Constructor for ValidationResult.
QString section;


%Property( name = identifier, get = _identifier, set = _setIdentifier )

QVariant _identifier() const;
QVariant identifier() const /PyName=_identifier/;
%Docstring
Returns the optional identifier for the failed metadata item.
For instance, in list type metadata elements this
will be set to the list index of the failed metadata
item.
%End

void _setIdentifier( QVariant identifier );
void setIdentifier( const QVariant &identifier );
%Docstring
Sets the optional ``identifier`` for the failed metadata item.
For instance, in list type metadata elements this
Expand Down
9 changes: 9 additions & 0 deletions python/gui/auto_additions/qgslayermetadataresultsmodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The following has been generated automatically from src/gui/qgslayermetadataresultsmodel.h
QgsLayerMetadataResultsModel.Roles = QgsLayerMetadataResultsModel.CustomRole
# monkey patching scoped based enum
QgsLayerMetadataResultsModel.Metadata = QgsLayerMetadataResultsModel.CustomRole.Metadata
QgsLayerMetadataResultsModel.Metadata.is_monkey_patched = True
QgsLayerMetadataResultsModel.Metadata.__doc__ = "Layer metadata role"
QgsLayerMetadataResultsModel.CustomRole.__doc__ = "The Roles enum represents the user roles for the model.\n\n.. note::\n\n Prior to QGIS 3.36 this was available as QgsLayerMetadataResultsModel.Roles\n\n.. versionadded:: 3.36\n\n" + '* ``Metadata``: ' + QgsLayerMetadataResultsModel.CustomRole.Metadata.__doc__
# --
QgsLayerMetadataResultsModel.CustomRole.baseClass = QgsLayerMetadataResultsModel
6 changes: 3 additions & 3 deletions python/gui/auto_generated/qgslayermetadataresultsmodel.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ Load/Reload model data synchronously.
Load/Reload model data asynchronously using threads.
%End

enum Roles

enum class CustomRole
{
//! Layer metadata role
Metadata
Metadata,
};

enum Sections
Expand Down
10 changes: 3 additions & 7 deletions src/core/metadata/qgslayermetadatavalidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,23 @@ class CORE_EXPORT QgsAbstractMetadataBaseValidator
//! Metadata section which failed the validation
QString section;

// TODO QGIS 4.0 - fix this

#ifdef SIP_RUN
SIP_PROPERTY( name = identifier, get = _identifier, set = _setIdentifier )
#endif
// TODO QGIS 4.0 - remove compatibility code

/**
* Returns the optional identifier for the failed metadata item.
* For instance, in list type metadata elements this
* will be set to the list index of the failed metadata
* item.
*/
QVariant _identifier() const { return mIdentifier; }
QVariant identifier() const SIP_PYNAME( _identifier ) { return mIdentifier; }

/**
* Sets the optional \a identifier for the failed metadata item.
* For instance, in list type metadata elements this
* will be set to the list index of the failed metadata
* item.
*/
void _setIdentifier( QVariant identifier ) { mIdentifier = identifier; }
void setIdentifier( const QVariant &identifier ) { mIdentifier = identifier; }

//! The reason behind the validation failure.
QString note;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgslayermetadataresultsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ QVariant QgsLayerMetadataResultsModel::data( const QModelIndex &index, int role
}
break;
}
case Roles::Metadata:
case static_cast< int >( CustomRole::Metadata ):
{
return QVariant::fromValue( mResult.metadata().at( index.row() ) );
}
Expand Down
12 changes: 9 additions & 3 deletions src/gui/qgslayermetadataresultsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,20 @@ class GUI_EXPORT QgsLayerMetadataResultsModel : public QAbstractTableModel
//! Load/Reload model data asynchronously using threads.
void reloadAsync( );

// *INDENT-OFF*

/**
* The Roles enum represents the user roles for the model.
*
* \note Prior to QGIS 3.36 this was available as QgsLayerMetadataResultsModel::Roles
* \since QGIS 3.36
*/
enum Roles
enum class CustomRole SIP_MONKEYPATCH_SCOPEENUM_UNNEST( QgsLayerMetadataResultsModel, Roles ) : int
{
//! Layer metadata role
Metadata = Qt::ItemDataRole::UserRole
Metadata = Qt::ItemDataRole::UserRole, //!< Layer metadata role
};
Q_ENUM( CustomRole )
// *INDENT-ON*

/**
* The Sections enum represents the data columns.
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgslayermetadatasearchwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ void QgsLayerMetadataSearchWidget::addButtonClicked()
{
for ( const auto &selectedIndex : std::as_const( selectedIndexes ) )
{
const QgsLayerMetadataProviderResult metadataResult { mSourceModel->data( mProxyModel->mapToSource( selectedIndex ), QgsLayerMetadataResultsModel::Roles::Metadata ).value<QgsLayerMetadataProviderResult>() };
const QgsLayerMetadataProviderResult metadataResult { mSourceModel->data( mProxyModel->mapToSource( selectedIndex ), static_cast< int >( QgsLayerMetadataResultsModel::CustomRole::Metadata ) ).value<QgsLayerMetadataProviderResult>() };
switch ( metadataResult.layerType() )
{
case Qgis::LayerType::Raster:
Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsmetadatawidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -855,9 +855,9 @@ bool QgsMetadataWidget::checkMetadata()
for ( const QgsAbstractMetadataBaseValidator::ValidationResult &result : std::as_const( validationResults ) )
{
errors += QLatin1String( "<b>" ) % result.section;
if ( ! QgsVariantUtils::isNull( result._identifier() ) )
if ( ! QgsVariantUtils::isNull( result.identifier() ) )
{
errors += QLatin1Char( ' ' ) % QVariant( result._identifier().toInt() + 1 ).toString();
errors += QLatin1Char( ' ' ) % QVariant( result.identifier().toInt() + 1 ).toString();
}
errors += QLatin1String( "</b>: " ) % result.note % QLatin1String( "<br />" );
}
Expand Down

0 comments on commit 2357d80

Please sign in to comment.