Fix 2315706: "Rename Exported Valuenode from imported canvases crash". Stop the...
[synfig.git] / synfig-studio / trunk / src / synfigapp / canvasinterface.cpp
index 6619719..da1a802 100644 (file)
@@ -477,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;
 }
 
 
@@ -531,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"));
 
@@ -550,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);
@@ -567,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(...)
@@ -594,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)))