Fix 2315706: "Rename Exported Valuenode from imported canvases crash". Stop the...
[synfig.git] / synfig-studio / trunk / src / synfigapp / canvasinterface.cpp
index 8bf6e89..da1a802 100644 (file)
@@ -41,6 +41,7 @@
 #include <synfig/valuenode_reference.h>
 #include <synfig/valuenode_twotone.h>
 #include <synfig/valuenode_stripes.h>
+#include <synfig/valuenode_bline.h>
 
 #include <synfig/waypoint.h>
 #include <synfig/loadcanvas.h>
@@ -249,7 +250,10 @@ CanvasInterface::add_layer_to(synfig::String name, synfig::Canvas::Handle canvas
                                                if (iter2->get_type() != ValueBase::TYPE_BLINEPOINT)
                                                        break;
                                        if (iter2 == list.end())
+                                       {
                                                value_node=LinkableValueNode::create("bline",iter->second);
+                                               ValueNode_BLine::Handle::cast_dynamic(value_node)->set_member_canvas(canvas);
+                                       }
                                }
 
                                if (!value_node)
@@ -473,30 +477,59 @@ CanvasInterface::set_rend_desc(const synfig::RendDesc &rend_desc)
                get_ui_interface()->error(_("Action Failed."));
 }
 
-bool
+void
 CanvasInterface::set_name(const synfig::String &x)
 {
-       //! \todo This needs to be converted into an action
-       get_canvas()->set_name(x);
+       Action::Handle  action(Action::create("canvas_name_set"));
+
+       assert(action);
+       if(!action)
+               return;
+
+       action->set_param("canvas",get_canvas());
+       action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
+       action->set_param("name",x);
+
+       if(!get_instance()->perform_action(action))
+               get_ui_interface()->error(_("Action Failed."));
+
        signal_id_changed_();
-       return true;
 }
 
-bool
+void
 CanvasInterface::set_description(const synfig::String &x)
 {
-       //! \todo This needs to be converted into an action
-       get_canvas()->set_description(x);
-       return true;
+       Action::Handle  action(Action::create("canvas_description_set"));
+
+       assert(action);
+       if(!action)
+               return;
+
+       action->set_param("canvas",get_canvas());
+       action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
+       action->set_param("description",x);
+
+       if(!get_instance()->perform_action(action))
+               get_ui_interface()->error(_("Action Failed."));
 }
 
-bool
+void
 CanvasInterface::set_id(const synfig::String &x)
 {
-       //! \todo This needs to be converted into an action
-       get_canvas()->set_id(x);
+       Action::Handle  action(Action::create("canvas_id_set"));
+
+       assert(action);
+       if(!action)
+               return;
+
+       action->set_param("canvas",get_canvas());
+       action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
+       action->set_param("id",x);
+
+       if(!get_instance()->perform_action(action))
+               get_ui_interface()->error(_("Action Failed."));
+
        signal_id_changed_();
-       return true;
 }
 
 
@@ -527,7 +560,7 @@ CanvasInterface::jump_to_prev_keyframe()
 }
 
 bool
-CanvasInterface::import(const synfig::String &filename, bool /*copy*/)
+CanvasInterface::import(const synfig::String &filename, synfig::String &errors, synfig::String &warnings, bool resize_image)
 {
        Action::PassiveGrouper group(get_instance().get(),_("Import Image"));
 
@@ -546,16 +579,16 @@ CanvasInterface::import(const synfig::String &filename, bool /*copy*/)
        // If this is a SIF file, then we need to do things slightly differently
        if(ext=="sif" || ext=="sifz")try
        {
-
-               Canvas::Handle outside_canvas(synfig::open_canvas(filename));
+               Canvas::Handle outside_canvas(synfig::open_canvas(filename, errors, warnings));
                if(!outside_canvas)
-                       throw String(_("Unable to open this composition"));
+                       throw String(_("Unable to open this composition")) + ":\n\n" + errors;
 
                Layer::Handle layer(add_layer_to("PasteCanvas",get_canvas()));
                if(!layer)
                        throw String(_("Unable to create \"Paste Canvas\" layer"));
                if(!layer->set_param("canvas",ValueBase(outside_canvas)))
                        throw int();
+               get_canvas()->register_external_canvas(filename, outside_canvas);
 
                //layer->set_description(basename(filename));
                signal_layer_new_description()(layer,filename);
@@ -563,7 +596,7 @@ CanvasInterface::import(const synfig::String &filename, bool /*copy*/)
        }
        catch(String x)
        {
-               get_ui_interface()->error(x+" -- "+filename);
+               get_ui_interface()->error(filename + ": " + x);
                return false;
        }
        catch(...)
@@ -590,22 +623,34 @@ CanvasInterface::import(const synfig::String &filename, bool /*copy*/)
                h=layer->get_param("_height").get(int());
                if(w&&h)
                {
-                       Vector size=ValueBase(get_canvas()->rend_desc().get_br()-get_canvas()->rend_desc().get_tl());
-                       Vector x;
-                       if(size[0]<size[1])
+                       Vector x, size=ValueBase(get_canvas()->rend_desc().get_br()-get_canvas()->rend_desc().get_tl());
+
+                       // vector from top left of canvas to bottom right
+                       if (resize_image)
                        {
-                               x[0]=size[0];
-                               x[1]=size[0]/w*h;
-                               if((size[0]<0) ^ (size[1]<0))
-                                       x[1]=-x[1];
+                               if(abs(size[0])<abs(size[1]))   // if canvas is tall and thin
+                               {
+                                       x[0]=size[0];   // use full width
+                                       x[1]=size[0]/w*h; // and scale for height
+                                       if((size[0]<0) ^ (size[1]<0))
+                                               x[1]=-x[1];
+                               }
+                               else                            // else canvas is short and fat (or maybe square)
+                               {
+                                       x[1]=size[1];   // use full height
+                                       x[0]=size[1]/h*w; // and scale for width
+                                       if((size[0]<0) ^ (size[1]<0))
+                                               x[0]=-x[0];
+                               }
                        }
                        else
                        {
-                               x[1]=size[1];
-                               x[0]=size[1]/h*w;
-                               if((size[0]<0) ^ (size[1]<0))
-                                       x[0]=-x[0];
+                               x[0] = w/60.0;
+                               x[1] = h/60.0;
+                               if((size[0]<0)) x[0]=-x[0];
+                               if((size[1]<0)) x[1]=-x[1];
                        }
+
                        if(!layer->set_param("tl",ValueBase(-x/2)))
                                throw int();
                        if(!layer->set_param("br",ValueBase(x/2)))