Better way to mark styles as read only

This commit is contained in:
Nyall Dawson 2022-05-17 08:04:24 +10:00
parent 8615123178
commit 1251b115f4
5 changed files with 68 additions and 4 deletions

View File

@ -114,6 +114,34 @@ Sets the ``name`` of the style.
.. seealso:: :py:func:`name` .. seealso:: :py:func:`name`
.. versionadded:: 3.26
%End
bool isReadOnly() const;
%Docstring
Returns ``True`` if the style is considered a read-only library.
.. note::
This flag is used to control GUI operations, and does not prevent calling functions
which mutate the style directly via the API.
.. seealso:: :py:func:`setReadOnly`
.. versionadded:: 3.26
%End
void setReadOnly( bool readOnly );
%Docstring
Sets whether the style is considered a read-only library.
.. note::
This flag is used to control GUI operations, and does not prevent calling functions
which mutate the style directly via the API.
.. seealso:: :py:func:`isReadOnly`
.. versionadded:: 3.26 .. versionadded:: 3.26
%End %End

View File

@ -312,6 +312,7 @@ void QgsProjectStyleSettings::loadStyleAtPath( const QString &path )
style->createMemoryDatabase(); style->createMemoryDatabase();
style->importXml( path ); style->importXml( path );
style->setFileName( path ); style->setFileName( path );
style->setReadOnly( true );
} }
else else
{ {
@ -519,11 +520,9 @@ bool QgsProjectStyleDatabaseProxyModel::filterAcceptsRow( int sourceRow, const Q
{ {
if ( mFilters & Filter::FilterHideReadOnly ) if ( mFilters & Filter::FilterHideReadOnly )
{ {
const QString path = sourceModel()->data( sourceModel()->index( sourceRow, 0, sourceParent ), QgsProjectStyleDatabaseModel::Role::PathRole ).toString(); if ( const QgsStyle *style = qobject_cast< QgsStyle * >( sourceModel()->data( sourceModel()->index( sourceRow, 0, sourceParent ), QgsProjectStyleDatabaseModel::Role::StyleRole ).value< QObject * >() ) )
if ( !path.isEmpty() )
{ {
const QFileInfo fi( path ); if ( style->isReadOnly() )
if ( fi.suffix().compare( QLatin1String( "xml" ), Qt::CaseInsensitive ) == 0 )
return false; return false;
} }
} }

View File

@ -3043,6 +3043,16 @@ bool QgsStyle::isXmlStyleFile( const QString &path )
return line == QLatin1String( "<!DOCTYPE qgis_style>" ); return line == QLatin1String( "<!DOCTYPE qgis_style>" );
} }
bool QgsStyle::isReadOnly() const
{
return mReadOnly;
}
void QgsStyle::setReadOnly( bool readOnly )
{
mReadOnly = readOnly;
}
bool QgsStyle::updateSymbol( StyleEntity type, const QString &name ) bool QgsStyle::updateSymbol( StyleEntity type, const QString &name )
{ {
QDomDocument doc( QStringLiteral( "dummy" ) ); QDomDocument doc( QStringLiteral( "dummy" ) );

View File

@ -203,6 +203,28 @@ class CORE_EXPORT QgsStyle : public QObject
*/ */
void setName( const QString &name ); void setName( const QString &name );
/**
* Returns TRUE if the style is considered a read-only library.
*
* \note This flag is used to control GUI operations, and does not prevent calling functions
* which mutate the style directly via the API.
*
* \see setReadOnly()
* \since QGIS 3.26
*/
bool isReadOnly() const;
/**
* Sets whether the style is considered a read-only library.
*
* \note This flag is used to control GUI operations, and does not prevent calling functions
* which mutate the style directly via the API.
*
* \see isReadOnly()
* \since QGIS 3.26
*/
void setReadOnly( bool readOnly );
/** /**
* Adds an \a entity to the style, with the specified \a name. Ownership is not transferred. * Adds an \a entity to the style, with the specified \a name. Ownership is not transferred.
* *
@ -1147,6 +1169,7 @@ class CORE_EXPORT QgsStyle : public QObject
private: private:
QString mName; QString mName;
bool mReadOnly = false;
QgsSymbolMap mSymbols; QgsSymbolMap mSymbols;
QgsVectorColorRampMap mColorRamps; QgsVectorColorRampMap mColorRamps;

View File

@ -204,6 +204,10 @@ void TestStyle::testProperties()
s.setFileName( QStringLiteral( "file name" ) ); s.setFileName( QStringLiteral( "file name" ) );
QCOMPARE( s.fileName(), QStringLiteral( "file name" ) ); QCOMPARE( s.fileName(), QStringLiteral( "file name" ) );
QVERIFY( !s.isReadOnly() );
s.setReadOnly( true );
QVERIFY( s.isReadOnly() );
} }
void TestStyle::testCreateSymbols() void TestStyle::testCreateSymbols()