Skip to content

Commit

Permalink
fix(StyleSettings): Make project dirty on style settings modification
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 committed Aug 14, 2024
1 parent 5f3d887 commit 730d214
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 8 deletions.
61 changes: 61 additions & 0 deletions src/core/project/qgsprojectstylesettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,30 @@ void QgsProjectStyleSettings::setDefaultSymbol( Qgis::SymbolType symbolType, Qgs
switch ( symbolType )
{
case Qgis::SymbolType::Marker:
if ( mDefaultMarkerSymbol.get() == symbol )
return;

mDefaultMarkerSymbol.reset( symbol ? symbol->clone() : nullptr );

makeDirty();
break;

case Qgis::SymbolType::Line:
if ( mDefaultLineSymbol.get() == symbol )
return;

mDefaultLineSymbol.reset( symbol ? symbol->clone() : nullptr );

makeDirty();
break;

case Qgis::SymbolType::Fill:
if ( mDefaultFillSymbol.get() == symbol )
return;

mDefaultFillSymbol.reset( symbol ? symbol->clone() : nullptr );

makeDirty();
break;

case Qgis::SymbolType::Hybrid:
Expand All @@ -94,7 +109,12 @@ QgsColorRamp *QgsProjectStyleSettings::defaultColorRamp() const

void QgsProjectStyleSettings::setDefaultColorRamp( QgsColorRamp *colorRamp )
{
if ( mDefaultColorRamp.get() == colorRamp )
return;

mDefaultColorRamp.reset( colorRamp ? colorRamp->clone() : nullptr );

makeDirty();
}

QgsTextFormat QgsProjectStyleSettings::defaultTextFormat() const
Expand All @@ -104,7 +124,32 @@ QgsTextFormat QgsProjectStyleSettings::defaultTextFormat() const

void QgsProjectStyleSettings::setDefaultTextFormat( const QgsTextFormat &textFormat )
{
if ( mDefaultTextFormat == textFormat )
return;

mDefaultTextFormat = textFormat;

makeDirty();
}

void QgsProjectStyleSettings::setDefaultSymbolOpacity( double opacity )
{
if ( mDefaultSymbolOpacity == opacity )
return;

mDefaultSymbolOpacity = opacity;

makeDirty();
}

void QgsProjectStyleSettings::setRandomizeDefaultSymbolColor( bool randomized )
{
if ( mRandomizeDefaultSymbolColor == randomized )
return;

mRandomizeDefaultSymbolColor = randomized;

makeDirty();
}

void QgsProjectStyleSettings::reset()
Expand Down Expand Up @@ -464,7 +509,13 @@ QgsCombinedStyleModel *QgsProjectStyleSettings::combinedStyleModel()

void QgsProjectStyleSettings::setColorModel( Qgis::ColorModel colorModel )
{
if ( mColorModel == colorModel )
return;

mColorModel = colorModel;

makeDirty();

#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
if ( mColorSpace.isValid() && QgsColorUtils::toColorModel( mColorSpace.colorModel() ) != colorModel )
{
Expand All @@ -480,6 +531,9 @@ Qgis::ColorModel QgsProjectStyleSettings::colorModel() const

void QgsProjectStyleSettings::setColorSpace( const QColorSpace &colorSpace )
{
if ( mColorSpace == colorSpace )
return;

if ( !mProject )
{
QgsDebugError( "Impossible to attach ICC profile, no project defined" );
Expand All @@ -504,6 +558,8 @@ void QgsProjectStyleSettings::setColorSpace( const QColorSpace &colorSpace )
mColorSpace = colorSpace;
#endif

makeDirty();

if ( !mColorSpace.isValid() )
return;

Expand All @@ -523,6 +579,11 @@ QColorSpace QgsProjectStyleSettings::colorSpace() const
return mColorSpace;
}

void QgsProjectStyleSettings::makeDirty()
{
if ( mProject )
mProject->setDirty( true );
}

//
// QgsProjectStyleDatabaseModel
Expand Down
7 changes: 5 additions & 2 deletions src/core/project/qgsprojectstylesettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class CORE_EXPORT QgsProjectStyleSettings : public QObject
/**
* Sets whether the default symbol fill color is randomized.
*/
void setRandomizeDefaultSymbolColor( bool randomized ) { mRandomizeDefaultSymbolColor = randomized; }
void setRandomizeDefaultSymbolColor( bool randomized );

/**
* Returns the default symbol opacity.
Expand All @@ -117,7 +117,7 @@ class CORE_EXPORT QgsProjectStyleSettings : public QObject
/**
* Sets the default symbol opacity.
*/
void setDefaultSymbolOpacity( double opacity ) { mDefaultSymbolOpacity = opacity; }
void setDefaultSymbolOpacity( double opacity );

/**
* Resets the settings to a default state.
Expand Down Expand Up @@ -354,6 +354,9 @@ class CORE_EXPORT QgsProjectStyleSettings : public QObject
void loadStyleAtPath( const QString &path );
void clearStyles();

//! propage dirtyness to project
void makeDirty();

friend class TestQgsProjectProperties;
};

Expand Down
35 changes: 29 additions & 6 deletions tests/src/python/test_qgsprojectstylesettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,51 +53,70 @@
class TestQgsProjectViewSettings(QgisTestCase):

def testDefaultSymbol(self):
p = QgsProjectStyleSettings()
project = QgsProject()
p = project.styleSettings()
self.assertFalse(project.isDirty())
self.assertFalse(p.defaultSymbol(Qgis.SymbolType.Marker))
self.assertFalse(p.defaultSymbol(Qgis.SymbolType.Line))
self.assertFalse(p.defaultSymbol(Qgis.SymbolType.Fill))

marker = QgsSymbol.defaultSymbol(QgsWkbTypes.GeometryType.PointGeometry)
p.setDefaultSymbol(Qgis.SymbolType.Marker, marker)
self.assertTrue(p.defaultSymbol(Qgis.SymbolType.Marker))
self.assertTrue(project.isDirty())
project.setDirty(False)

line = QgsSymbol.defaultSymbol(QgsWkbTypes.GeometryType.LineGeometry)
p.setDefaultSymbol(Qgis.SymbolType.Line, line)
self.assertTrue(project.isDirty())
self.assertTrue(p.defaultSymbol(Qgis.SymbolType.Line))
project.setDirty(False)

fill = QgsSymbol.defaultSymbol(QgsWkbTypes.GeometryType.PolygonGeometry)
p.setDefaultSymbol(Qgis.SymbolType.Fill, fill)
self.assertTrue(project.isDirty())
self.assertTrue(p.defaultSymbol(Qgis.SymbolType.Fill))

def testDefaultColorRamp(self):
p = QgsProjectStyleSettings()
project = QgsProject()
p = project.styleSettings()
self.assertFalse(project.isDirty())
self.assertFalse(p.defaultColorRamp())

ramp = QgsGradientColorRamp(QColor(255, 255, 255), QColor(255, 0, 0))
p.setDefaultColorRamp(ramp)
self.assertTrue(p.defaultColorRamp())
self.assertTrue(project.isDirty())

def testDefaultTextFormat(self):
p = QgsProjectStyleSettings()
project = QgsProject()
p = project.styleSettings()
self.assertFalse(project.isDirty())
self.assertFalse(p.defaultTextFormat().isValid())

textFormat = QgsTextFormat()
textFormat.setFont(QFont())
p.setDefaultTextFormat(textFormat)
self.assertTrue(p.defaultTextFormat().isValid())
self.assertTrue(project.isDirty())

def testRandomizeDefaultSymbolColor(self):
p = QgsProjectStyleSettings()
project = QgsProject()
p = project.styleSettings()
self.assertFalse(project.isDirty())
self.assertTrue(p.randomizeDefaultSymbolColor())
p.setRandomizeDefaultSymbolColor(False)
self.assertFalse(p.randomizeDefaultSymbolColor())
self.assertTrue(project.isDirty())

def testDefaultSymbolOpacity(self):
p = QgsProjectStyleSettings()
project = QgsProject()
p = project.styleSettings()
self.assertFalse(project.isDirty())
self.assertEqual(p.defaultSymbolOpacity(), 1.0)
p.setDefaultSymbolOpacity(0.25)
self.assertEqual(p.defaultSymbolOpacity(), 0.25)
self.assertTrue(project.isDirty())

def testProjectStyle(self):
project = QgsProject()
Expand Down Expand Up @@ -414,7 +433,7 @@ def testStylePaths(self):

def testReadWrite(self):
project = QgsProject()
p = QgsProjectStyleSettings(project)
p = project.styleSettings()

line = QgsSymbol.defaultSymbol(QgsWkbTypes.GeometryType.LineGeometry)
p.setDefaultSymbol(Qgis.SymbolType.Line, line)
Expand Down Expand Up @@ -454,13 +473,16 @@ def testColorSettings(self):
"""
project = QgsProject()
settings = project.styleSettings()
self.assertFalse(project.isDirty())

self.assertEqual(settings.colorModel(), Qgis.ColorModel.Rgb)
self.assertFalse(settings.colorSpace().isValid())

# set Cmyk color model and color space

settings.setColorModel(Qgis.ColorModel.Cmyk)
self.assertTrue(project.isDirty())
project.setDirty(False)
self.assertEqual(settings.colorModel(), Qgis.ColorModel.Cmyk)

with open(os.path.join(TEST_DATA_DIR, "sRGB2014.icc"), mode='rb') as f:
Expand All @@ -469,6 +491,7 @@ def testColorSettings(self):
self.assertTrue(colorSpace.isValid())

settings.setColorSpace(colorSpace)
self.assertTrue(project.isDirty())
self.assertTrue(settings.colorSpace().isValid())
self.assertEqual(settings.colorSpace().primaries(), QColorSpace.Primaries.SRgb)
self.assertEqual(len(project.attachedFiles()), 2)
Expand Down

0 comments on commit 730d214

Please sign in to comment.