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`
.. 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
%End

View File

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

View File

@ -3043,6 +3043,16 @@ bool QgsStyle::isXmlStyleFile( const QString &path )
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 )
{
QDomDocument doc( QStringLiteral( "dummy" ) );

View File

@ -203,6 +203,28 @@ class CORE_EXPORT QgsStyle : public QObject
*/
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.
*
@ -1147,6 +1169,7 @@ class CORE_EXPORT QgsStyle : public QObject
private:
QString mName;
bool mReadOnly = false;
QgsSymbolMap mSymbols;
QgsVectorColorRampMap mColorRamps;

View File

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