From 08c7fad9770a23c9f39bee80a844eee3b53f299c Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Thu, 8 Jul 2010 22:02:03 -0700 Subject: [PATCH] Cleanup of gradient swatch property and collection policy. --- src/attributes.cpp | 1 + src/attributes.h | 1 + src/gradient-chemistry.cpp | 12 +++--- src/sp-gradient.cpp | 78 ++++++++++++++++++++++++++++---------- src/sp-paint-server.cpp | 20 +++------- src/sp-paint-server.h | 13 +++++-- 6 files changed, 82 insertions(+), 43 deletions(-) diff --git a/src/attributes.cpp b/src/attributes.cpp index 3cfe04fab..c44a7da4e 100644 --- a/src/attributes.cpp +++ b/src/attributes.cpp @@ -280,6 +280,7 @@ static SPStyleProp const props[] = { {SP_ATTR_GRADIENTUNITS, "gradientUnits"}, {SP_ATTR_GRADIENTTRANSFORM, "gradientTransform"}, {SP_ATTR_SPREADMETHOD, "spreadMethod"}, + {SP_ATTR_OSB_SWATCH, "osb:paint"}, /* SPRadialGradient */ {SP_ATTR_FX, "fx"}, {SP_ATTR_FY, "fy"}, diff --git a/src/attributes.h b/src/attributes.h index c21087e36..aadb4d165 100644 --- a/src/attributes.h +++ b/src/attributes.h @@ -281,6 +281,7 @@ enum SPAttributeEnum { SP_ATTR_GRADIENTUNITS, SP_ATTR_GRADIENTTRANSFORM, SP_ATTR_SPREADMETHOD, + SP_ATTR_OSB_SWATCH, /* SPRadialGradient */ SP_ATTR_FX, SP_ATTR_FY, diff --git a/src/gradient-chemistry.cpp b/src/gradient-chemistry.cpp index 8a199d4a3..974a13b5f 100644 --- a/src/gradient-chemistry.cpp +++ b/src/gradient-chemistry.cpp @@ -1203,11 +1203,13 @@ SPGradient *sp_document_default_gradient_vector( SPDocument *document, SPColor c Inkscape::XML::Node *repr = xml_doc->createElement("svg:linearGradient"); - repr->setAttribute("inkscape:collect", "always"); - // set here, but removed when it's edited in the gradient editor - // to further reduce clutter, we could - // (1) here, search gradients by color and return what is found without duplication - // (2) in fill & stroke, show only one copy of each gradient in list + if ( !singleStop ) { + repr->setAttribute("inkscape:collect", "always"); + // set here, but removed when it's edited in the gradient editor + // to further reduce clutter, we could + // (1) here, search gradients by color and return what is found without duplication + // (2) in fill & stroke, show only one copy of each gradient in list + } Glib::ustring colorStr = color.toString(); addStop( repr, colorStr, 1, "0" ); diff --git a/src/sp-gradient.cpp b/src/sp-gradient.cpp index 2a3142b04..665040616 100644 --- a/src/sp-gradient.cpp +++ b/src/sp-gradient.cpp @@ -291,15 +291,10 @@ SPGradientSpread SPGradient::getSpread() const void SPGradient::setSwatch( bool swatch ) { if ( swatch != isSwatch() ) { - if ( swatch ) { - if ( hasStops() && (getStopCount() == 0) ) { - repr->setAttribute( "osb:paint", "solid" ); - } else { - repr->setAttribute( "osb:paint", "gradient" ); - } - } else { - repr->setAttribute( "osb:paint", 0 ); - } + this->swatch = swatch; // to make isSolid() work, this happens first + gchar const* paintVal = swatch ? (isSolid() ? "solid" : "gradient") : 0; + sp_object_setAttribute( this, "osb:paint", paintVal, 0 ); + requestModified( SP_OBJECT_MODIFIED_FLAG ); } } @@ -438,11 +433,16 @@ void SPGradientImpl::build(SPObject *object, SPDocument *document, Inkscape::XML { SPGradient *gradient = SP_GRADIENT(object); - if (((SPObjectClass *) gradient_parent_class)->build) + // Work-around in case a swatch had been marked for immediate collection: + if ( repr->attribute("osb:paint") && repr->attribute("inkscape:collect") ) { + repr->setAttribute("inkscape:collect", 0); + } + + if (((SPObjectClass *) gradient_parent_class)->build) { (* ((SPObjectClass *) gradient_parent_class)->build)(object, document, repr); + } - SPObject *ochild; - for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) { + for ( SPObject *ochild = sp_object_first_child(object); ochild; ochild = ochild->next ) { if (SP_IS_STOP(ochild)) { gradient->has_stops = TRUE; break; @@ -453,6 +453,7 @@ void SPGradientImpl::build(SPObject *object, SPDocument *document, Inkscape::XML sp_object_read_attr(object, "gradientTransform"); sp_object_read_attr(object, "spreadMethod"); sp_object_read_attr(object, "xlink:href"); + sp_object_read_attr(object, "osb:paint"); /* Register ourselves */ sp_document_add_resource(document, "gradient", object); @@ -553,9 +554,31 @@ void SPGradientImpl::setGradientAttr(SPObject *object, unsigned key, gchar const gr->ref->detach(); } break; + case SP_ATTR_OSB_SWATCH: + { + bool newVal = (value != 0); + bool modified = false; + if (newVal != gr->swatch) { + gr->swatch = newVal; + modified = true; + } + if (newVal) { + // Might need to flip solid/gradient + Glib::ustring paintVal = ( gr->hasStops() && (gr->getStopCount() == 0) ) ? "solid" : "gradient"; + if ( paintVal != value ) { + sp_object_setAttribute( gr, "osb:paint", paintVal.c_str(), 0 ); + modified = true; + } + } + if (modified) { + object->requestModified(SP_OBJECT_MODIFIED_FLAG); + } + } + break; default: - if (((SPObjectClass *) gradient_parent_class)->set) + if (((SPObjectClass *) gradient_parent_class)->set) { ((SPObjectClass *) gradient_parent_class)->set(object, key, value); + } break; } } @@ -607,8 +630,8 @@ void SPGradientImpl::childAdded(SPObject *object, Inkscape::XML::Node *child, In gr->has_stops = TRUE; if ( gr->getStopCount() > 0 ) { gchar const * attr = gr->repr->attribute("osb:paint"); - if ( attr && !strcmp(attr, "solid") ) { - gr->repr->setAttribute("osb:paint", "gradient"); + if ( attr && strcmp(attr, "gradient") ) { + sp_object_setAttribute( gr, "osb:paint", "gradient", 0 ); } } } @@ -640,9 +663,10 @@ void SPGradientImpl::removeChild(SPObject *object, Inkscape::XML::Node *child) } if ( gr->getStopCount() == 0 ) { + g_message("Check on remove"); gchar const * attr = gr->repr->attribute("osb:paint"); - if ( attr && !strcmp(attr, "gradient") ) { - gr->repr->setAttribute("osb:paint", "solid"); + if ( attr && strcmp(attr, "solid") ) { + sp_object_setAttribute( gr, "osb:paint", "solid", 0 ); } } @@ -714,15 +738,17 @@ Inkscape::XML::Node *SPGradientImpl::write(SPObject *object, Inkscape::XML::Docu { SPGradient *gr = SP_GRADIENT(object); - if (((SPObjectClass *) gradient_parent_class)->write) + if (((SPObjectClass *) gradient_parent_class)->write) { (* ((SPObjectClass *) gradient_parent_class)->write)(object, xml_doc, repr, flags); + } if (flags & SP_OBJECT_WRITE_BUILD) { GSList *l = NULL; for (SPObject *child = sp_object_first_child(object); child; child = SP_OBJECT_NEXT(child)) { - Inkscape::XML::Node *crepr; - crepr = child->updateRepr(xml_doc, NULL, flags); - if (crepr) l = g_slist_prepend(l, crepr); + Inkscape::XML::Node *crepr = child->updateRepr(xml_doc, NULL, flags); + if (crepr) { + l = g_slist_prepend(l, crepr); + } } while (l) { repr->addChild((Inkscape::XML::Node *) l->data, NULL); @@ -771,6 +797,16 @@ Inkscape::XML::Node *SPGradientImpl::write(SPObject *object, Inkscape::XML::Docu } } + if ( (flags & SP_OBJECT_WRITE_EXT) && gr->isSwatch() ) { + if ( gr->isSolid() ) { + repr->setAttribute( "osb:paint", "solid" ); + } else { + repr->setAttribute( "osb:paint", "gradient" ); + } + } else { + repr->setAttribute( "osb:paint", 0 ); + } + return repr; } diff --git a/src/sp-paint-server.cpp b/src/sp-paint-server.cpp index 5858f9a0e..4b5366a46 100644 --- a/src/sp-paint-server.cpp +++ b/src/sp-paint-server.cpp @@ -21,7 +21,6 @@ #include "xml/node.h" static void sp_paint_server_class_init(SPPaintServerClass *psc); -static void sp_paint_server_init(SPPaintServer *ps); static void sp_paint_server_release(SPObject *object); @@ -30,7 +29,7 @@ static void sp_painter_stale_fill(SPPainter *painter, NRPixBlock *pb); static SPObjectClass *parent_class; static GSList *stale_painters = NULL; -GType sp_paint_server_get_type (void) +GType SPPaintServer::getType(void) { static GType type = 0; if (!type) { @@ -43,7 +42,7 @@ GType sp_paint_server_get_type (void) NULL, /* class_data */ sizeof(SPPaintServer), 16, /* n_preallocs */ - (GInstanceInitFunc) sp_paint_server_init, + (GInstanceInitFunc) SPPaintServer::init, NULL, /* value_table */ }; type = g_type_register_static(SP_TYPE_OBJECT, "SPPaintServer", &info, (GTypeFlags) 0); @@ -58,9 +57,10 @@ static void sp_paint_server_class_init(SPPaintServerClass *psc) parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT); } -static void sp_paint_server_init(SPPaintServer *ps) +void SPPaintServer::init(SPPaintServer *ps) { ps->painters = NULL; + ps->swatch = false; } static void sp_paint_server_release(SPObject *object) @@ -157,24 +157,16 @@ static void sp_painter_stale_fill(SPPainter */*painter*/, NRPixBlock *pb) bool SPPaintServer::isSwatch() const { - bool swatch = false; - if (SP_IS_GRADIENT(this)) { - SPGradient *grad = SP_GRADIENT(this); - swatch = grad->hasStops() && repr->attribute("osb:paint"); - } return swatch; } bool SPPaintServer::isSolid() const { bool solid = false; - if (SP_IS_GRADIENT(this)) { + if (swatch && SP_IS_GRADIENT(this)) { SPGradient *grad = SP_GRADIENT(this); if ( grad->hasStops() && (grad->getStopCount() == 0) ) { - gchar const * attr = repr->attribute("osb:paint"); - if (attr && !strcmp(attr, "solid")) { - solid = true; - } + solid = true; } } return solid; diff --git a/src/sp-paint-server.h b/src/sp-paint-server.h index a76daf4d1..dc7bcc9c5 100644 --- a/src/sp-paint-server.h +++ b/src/sp-paint-server.h @@ -21,7 +21,7 @@ class SPPainter; -#define SP_TYPE_PAINT_SERVER (sp_paint_server_get_type ()) +#define SP_TYPE_PAINT_SERVER (SPPaintServer::getType()) #define SP_PAINT_SERVER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SP_TYPE_PAINT_SERVER, SPPaintServer)) #define SP_PAINT_SERVER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SP_TYPE_PAINT_SERVER, SPPaintServerClass)) #define SP_IS_PAINT_SERVER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SP_TYPE_PAINT_SERVER)) @@ -47,8 +47,17 @@ struct SPPaintServer : public SPObject { /** List of paints */ SPPainter *painters; +protected: + bool swatch; +public: + + static GType getType(void); + bool isSwatch() const; bool isSolid() const; + +private: + static void init(SPPaintServer *ps); }; struct SPPaintServerClass { @@ -59,8 +68,6 @@ struct SPPaintServerClass { void (* painter_free) (SPPaintServer *ps, SPPainter *painter); }; -GType sp_paint_server_get_type (void); - SPPainter *sp_paint_server_painter_new (SPPaintServer *ps, Geom::Matrix const &full_transform, Geom::Matrix const &parent_transform, const NRRect *bbox); SPPainter *sp_painter_free (SPPainter *painter); -- 2.30.2