Code

Rough pass of Fill-n-Stroke swatch conversion.
authorJon A. Cruz <jon@joncruz.org>
Fri, 2 Jul 2010 08:18:10 +0000 (01:18 -0700)
committerJon A. Cruz <jon@joncruz.org>
Fri, 2 Jul 2010 08:18:10 +0000 (01:18 -0700)
src/sp-gradient.cpp
src/sp-gradient.h
src/ui/dialog/swatches.cpp
src/widgets/fill-style.cpp
src/widgets/paint-selector.cpp

index 0c0c947840aa148f86a2c3049dbf83e13a48c56f..9c1ea0da7896016dc0d68b4b49d2420e5bf97bf9 100644 (file)
@@ -288,6 +288,18 @@ SPGradientSpread SPGradient::getSpread() const
     return spread;
 }
 
+void SPGradient::setSwatch()
+{
+    if ( !isSwatch() ) {
+        if ( hasStops() && (getStopCount() == 0) ) {
+            repr->setAttribute("osb:paint", "solid");
+        } else {
+            repr->setAttribute("osb:paint", "gradient");
+        }
+        requestModified(SP_OBJECT_MODIFIED_FLAG);
+    }
+}
+
 /**
  * Return stop's color as 32bit value.
  */
index 7e6afe0520a11094ed3595fffdf0d8bdb54137dd..f1705f1c1a07055efd7e3ab54d84c7e5293c31c2 100644 (file)
@@ -142,6 +142,8 @@ public:
     SPGradientSpread fetchSpread();
     SPGradientUnits fetchUnits();
 
+    void setSwatch();
+
 private:
     bool invalidateVector();
     void rebuildVector();
index 90e9e5f7bcba5f414ab20358b068360d94407848..755a10519c2e1f02219a3af6065b6e265991df0f 100644 (file)
@@ -72,7 +72,8 @@ static std::map<SwatchesPanel*, SPDocument*> docPerPanel;
 class SwatchesPanelHook : public SwatchesPanel
 {
 public:
-    static void convertGradient( GtkMenuItem * menuitem, gpointer userData );
+    static void convertGradient( GtkMenuItem *menuitem, gpointer userData );
+    static void addNewGradient( GtkMenuItem *menuitem, gpointer user_data );
 };
 
 static void handleClick( GtkWidget* /*widget*/, gpointer callback_data ) {
@@ -139,7 +140,7 @@ static void editGradient( GtkMenuItem */*menuitem*/, gpointer /*user_data*/ )
     }
 }
 
-static void addNewGradient( GtkMenuItem */*menuitem*/, gpointer /*user_data*/ )
+void SwatchesPanelHook::addNewGradient( GtkMenuItem */*menuitem*/, gpointer /*user_data*/ )
 {
     if ( bounceTarget ) {
         SwatchesPanel* swp = bouncePanel;
@@ -147,21 +148,22 @@ static void addNewGradient( GtkMenuItem */*menuitem*/, gpointer /*user_data*/ )
         SPDocument *doc = desktop ? desktop->doc() : 0;
         if (doc) {
             Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
+            SPGradient * gr = 0;
+            {
+                Inkscape::XML::Node *repr = xml_doc->createElement("svg:linearGradient");
+                Inkscape::XML::Node *stop = xml_doc->createElement("svg:stop");
+                stop->setAttribute("offset", "0");
+                stop->setAttribute("style", "stop-color:#000;stop-opacity:1;");
+                repr->appendChild(stop);
+                Inkscape::GC::release(stop);
 
-            Inkscape::XML::Node *repr = xml_doc->createElement("svg:linearGradient");
-            repr->setAttribute("osb:paint", "solid");
-            Inkscape::XML::Node *stop = xml_doc->createElement("svg:stop");
-            stop->setAttribute("offset", "0");
-            stop->setAttribute("style", "stop-color:#000;stop-opacity:1;");
-            repr->appendChild(stop);
-            Inkscape::GC::release(stop);
-
-            SP_OBJECT_REPR( SP_DOCUMENT_DEFS(doc) )->addChild(repr, NULL);
-
-            SPGradient * gr = static_cast<SPGradient *>(doc->getObjectByRepr(repr));
+                SP_OBJECT_REPR( SP_DOCUMENT_DEFS(doc) )->addChild(repr, NULL);
 
-            Inkscape::GC::release(repr);
+                gr = static_cast<SPGradient *>(doc->getObjectByRepr(repr));
+                Inkscape::GC::release(repr);
+            }
 
+            gr->setSwatch();
 
             editGradientImpl( gr );
         }
@@ -182,12 +184,9 @@ void SwatchesPanelHook::convertGradient( GtkMenuItem * /*menuitem*/, gpointer us
             for (const GSList *item = gradients; item; item = item->next) {
                 SPGradient* grad = SP_GRADIENT(item->data);
                 if ( targetName == grad->getId() ) {
-                    grad->repr->setAttribute("osb:paint", "solid"); // TODO make conditional
-
+                    grad->setSwatch();
                     sp_document_done(doc, SP_VERB_CONTEXT_GRADIENT,
                                      _("Add gradient stop"));
-
-                    handleGradientsChange(doc); // work-around for signal not being emitted
                     break;
                 }
             }
@@ -253,7 +252,7 @@ gboolean colorItemHandleButtonPress( GtkWidget* widget, GdkEventButton* event, g
             child = gtk_menu_item_new_with_label(_("Add"));
             g_signal_connect( G_OBJECT(child),
                               "activate",
-                              G_CALLBACK(addNewGradient),
+                              G_CALLBACK(SwatchesPanelHook::addNewGradient),
                               user_data );
             gtk_menu_shell_append(GTK_MENU_SHELL(popupMenu), child);
             popupExtras.push_back(child);
@@ -887,21 +886,25 @@ void SwatchesPanel::handleDefsModified(SPDocument *document)
         std::map<ColorItem*, SPGradient*> tmpGrads;
         recalcSwatchContents(document, tmpColors, tmpPrevs, tmpGrads);
 
-        int cap = std::min(docPalette->_colors.size(), tmpColors.size());
-        for (int i = 0; i < cap; i++) {
-            ColorItem* newColor = tmpColors[i];
-            ColorItem* oldColor = docPalette->_colors[i];
-            if ( (newColor->def.getType() != oldColor->def.getType()) ||
-                 (newColor->def.getR() != oldColor->def.getR()) ||
-                 (newColor->def.getG() != oldColor->def.getG()) ||
-                 (newColor->def.getB() != oldColor->def.getB()) ) {
-                oldColor->def.setRGB(newColor->def.getR(), newColor->def.getG(), newColor->def.getB());
-            }
-            if (tmpGrads.find(newColor) != tmpGrads.end()) {
-                oldColor->setGradient(tmpGrads[newColor]);
-            }
-            if ( tmpPrevs.find(newColor) != tmpPrevs.end() ) {
-                oldColor->setPixData(tmpPrevs[newColor], PREVIEW_PIXBUF_WIDTH, VBLOCK);
+        if ( tmpColors.size() != docPalette->_colors.size() ) {
+            handleGradientsChange(document);
+        } else {
+            int cap = std::min(docPalette->_colors.size(), tmpColors.size());
+            for (int i = 0; i < cap; i++) {
+                ColorItem* newColor = tmpColors[i];
+                ColorItem* oldColor = docPalette->_colors[i];
+                if ( (newColor->def.getType() != oldColor->def.getType()) ||
+                     (newColor->def.getR() != oldColor->def.getR()) ||
+                     (newColor->def.getG() != oldColor->def.getG()) ||
+                     (newColor->def.getB() != oldColor->def.getB()) ) {
+                    oldColor->def.setRGB(newColor->def.getR(), newColor->def.getG(), newColor->def.getB());
+                }
+                if (tmpGrads.find(newColor) != tmpGrads.end()) {
+                    oldColor->setGradient(tmpGrads[newColor]);
+                }
+                if ( tmpPrevs.find(newColor) != tmpPrevs.end() ) {
+                    oldColor->setPixData(tmpPrevs[newColor], PREVIEW_PIXBUF_WIDTH, VBLOCK);
+                }
             }
         }
     }
index 5a7256d8355a3584e71b98e7cf1f87bf9ed31048..f1342f3de3374d47c14ee64b32eb166c5b315288 100644 (file)
@@ -556,8 +556,12 @@ void FillNStroke::updateFromPaint()
                         }
 
                         if (!vector) {
+                            SPGradient *gr = sp_gradient_vector_for_object(document, desktop, SP_OBJECT(i->data), kind == FILL);
+                            if ( gr && (psel->mode == SPPaintSelector::MODE_SWATCH) ) {
+                                gr->setSwatch();
+                            }
                             sp_item_set_gradient(SP_ITEM(i->data),
-                                                 sp_gradient_vector_for_object(document, desktop, SP_OBJECT(i->data), kind == FILL),
+                                                 gr,
                                                  gradient_type, kind == FILL);
                         } else {
                             sp_item_set_gradient(SP_ITEM(i->data), vector, gradient_type, kind == FILL);
index 8759854a02bc12696f45b5f0edcfd4be3af5ca6f..a569d00bb7126f0ea4777b568244709d6cea865e 100644 (file)
@@ -1102,6 +1102,7 @@ static void sp_paint_selector_set_mode_swatch(SPPaintSelector *psel, SPPaintSele
 
         gtk_frame_set_label(GTK_FRAME(psel->frame), _("Swatch fill"));
     }
+
 #ifdef SP_PS_VERBOSE
     g_print("Swatch req\n");
 #endif