Code

GSoC C++-ificiation merge and cleanup.
[inkscape.git] / src / box3d-side.cpp
index 4cd3765c90967aeda7cbb4adf28988a263ccf7b4..72a479ca4ab0957ead622c60404acbdfd15e131b 100644 (file)
@@ -1,10 +1,9 @@
-#define __BOX3D_SIDE_C__
-
 /*
  * 3D box face implementation
  *
  * Authors:
  *   Maximilian Albert <Anhalter42@gmx.de>
+ *   Abhishek Sharma
  *
  * Copyright (C) 2007  Authors
  *
@@ -21,7 +20,7 @@
 #include "inkscape.h"
 #include "persp3d.h"
 #include "box3d-context.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "desktop-style.h"
 #include "box3d.h"
 
@@ -29,7 +28,7 @@ static void box3d_side_class_init (Box3DSideClass *klass);
 static void box3d_side_init (Box3DSide *side);
 
 static void box3d_side_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
-static Inkscape::XML::Node *box3d_side_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *box3d_side_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 static void box3d_side_set (SPObject *object, unsigned int key, const gchar *value);
 static void box3d_side_update (SPObject *object, SPCtx *ctx, guint flags);
 
@@ -93,24 +92,23 @@ box3d_side_init (Box3DSide * side)
     side->front_or_rear = Box3D::FRONT;
 }
 
-static void
-box3d_side_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
+static void box3d_side_build(SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
 {
-    if (((SPObjectClass *) parent_class)->build)
-        ((SPObjectClass *) parent_class)->build (object, document, repr);
+    if (((SPObjectClass *) parent_class)->build) {
+        ((SPObjectClass *) parent_class)->build(object, document, repr);
+    }
 
-    sp_object_read_attr(object, "inkscape:box3dsidetype");
+    object->readAttr( "inkscape:box3dsidetype" );
 }
 
 static Inkscape::XML::Node *
-box3d_side_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
+box3d_side_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     Box3DSide *side = SP_BOX3D_SIDE (object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
         // this is where we end up when saving as plain SVG (also in other circumstances?)
         // thus we don' set "sodipodi:type" so that the box is only saved as an ordinary svg:path
-        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
         repr = xml_doc->createElement("svg:path");
     }
 
@@ -118,24 +116,20 @@ box3d_side_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
         sp_repr_set_int(repr, "inkscape:box3dsidetype", side->dir1 ^ side->dir2 ^ side->front_or_rear);
     }
 
-    sp_shape_set_shape ((SPShape *) object);
+    static_cast<SPShape *>(object)->setShape();
 
     /* Duplicate the path */
-    SPCurve *curve = ((SPShape *) object)->curve;
+    SPCurve const *curve = ((SPShape *) object)->curve;
     //Nulls might be possible if this called iteratively
     if ( !curve ) {
         return NULL;
     }
-    NArtBpath *bpath = SP_CURVE_BPATH(curve);
-    if ( !bpath ) {
-        return NULL;
-    }
-    char *d = sp_svg_write_path ( bpath );
+    char *d = sp_svg_write_path ( curve->get_pathvector() );
     repr->setAttribute("d", d);
     g_free (d);
 
     if (((SPObjectClass *) (parent_class))->write)
-        ((SPObjectClass *) (parent_class))->write (object, repr, flags);
+        ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
 
     return repr;
 }
@@ -184,13 +178,33 @@ box3d_side_update (SPObject *object, SPCtx *ctx, guint flags)
     if (flags & (SP_OBJECT_MODIFIED_FLAG |
                  SP_OBJECT_STYLE_MODIFIED_FLAG |
                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
-        sp_shape_set_shape ((SPShape *) object);
+        static_cast<SPShape *>(object)->setShape ();
     }
 
     if (((SPObjectClass *) parent_class)->update)
         ((SPObjectClass *) parent_class)->update (object, ctx, flags);
 }
 
+/* Create a new Box3DSide and append it to the parent box */
+Box3DSide * Box3DSide::createBox3DSide(SPBox3D *box)
+{
+       Box3DSide *box3d_side = 0;
+       Inkscape::XML::Document *xml_doc = box->document->rdoc;
+       Inkscape::XML::Node *repr_side = xml_doc->createElement("svg:path");
+       repr_side->setAttribute("sodipodi:type", "inkscape:box3dside");
+       box3d_side = static_cast<Box3DSide *>(box->appendChildRepr(repr_side));
+       return box3d_side;
+}
+
+/*
+ * Function which return the type attribute for Box3D. 
+ * Acts as a replacement for directly accessing the XML Tree directly.
+ */
+long long int Box3DSide::getFaceId()
+{
+           return this->getIntAttribute("inkscape:box3dsidetype", -1);
+}
+
 void
 box3d_side_position_set (Box3DSide *side) {
     box3d_side_set_shape (SP_SHAPE (side));
@@ -221,7 +235,6 @@ box3d_side_set_shape (SPShape *shape)
         return;
     }
 
-    SPCurve *c = sp_curve_new ();
     // TODO: Draw the correct quadrangle here
     //       To do this, determine the perspective of the box, the orientation of the side (e.g., XY-FRONT)
     //       compute the coordinates of the corners in P^3, project them onto the canvas, and draw the
@@ -230,40 +243,38 @@ box3d_side_set_shape (SPShape *shape)
     unsigned int corners[4];
     box3d_side_compute_corner_ids(side, corners);
 
-    sp_curve_moveto (c, box3d_get_corner_screen(box, corners[0]));
-    sp_curve_lineto (c, box3d_get_corner_screen(box, corners[1]));
-    sp_curve_lineto (c, box3d_get_corner_screen(box, corners[2]));
-    sp_curve_lineto (c, box3d_get_corner_screen(box, corners[3]));
+    SPCurve *c = new SPCurve();
 
-    sp_curve_closepath (c);
-    sp_shape_perform_path_effect(c, SP_SHAPE (side));
-    sp_shape_set_curve_insync (SP_SHAPE (side), c, TRUE);
-    sp_curve_unref (c);
-}
+    if (!box3d_get_corner_screen(box, corners[0]).isFinite() ||
+        !box3d_get_corner_screen(box, corners[1]).isFinite() ||
+        !box3d_get_corner_screen(box, corners[2]).isFinite() ||
+        !box3d_get_corner_screen(box, corners[3]).isFinite() )
+    {
+        g_warning ("Trying to draw a 3D box side with invalid coordinates.\n");
+        return;
+    }
 
-void
-box3d_side_apply_style (Box3DSide *side) {
-    Inkscape::XML::Node *repr_face = SP_OBJECT_REPR(SP_OBJECT(side));
-
-    gchar *descr = g_strconcat ("desktop.", box3d_side_axes_string (side), NULL);
-    const gchar * cur_style = prefs_get_string_attribute(descr, "style");
-    g_free (descr);    
-    
-    SPDesktop *desktop = inkscape_active_desktop();
-    bool use_current = prefs_get_int_attribute("tools.shapes.3dbox", "usecurrent", 0);
-    if (use_current && cur_style !=NULL) {
-        /* use last used style */
-        repr_face->setAttribute("style", cur_style);
-    } else {
-        /* use default style */
-        GString *pstring = g_string_new("");
-        g_string_printf (pstring, "tools.shapes.3dbox.%s", box3d_side_axes_string(side));
-        sp_desktop_apply_style_tool (desktop, repr_face, pstring->str, false);
+    c->moveto(box3d_get_corner_screen(box, corners[0]));
+    c->lineto(box3d_get_corner_screen(box, corners[1]));
+    c->lineto(box3d_get_corner_screen(box, corners[2]));
+    c->lineto(box3d_get_corner_screen(box, corners[3]));
+    c->closepath();
+
+    /* Reset the shape'scurve to the "original_curve"
+     * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/
+    shape->setCurveInsync( c, TRUE);
+    if (sp_lpe_item_has_path_effect(SP_LPE_ITEM(shape)) && sp_lpe_item_path_effects_enabled(SP_LPE_ITEM(shape))) {
+        SPCurve *c_lpe = c->copy();
+        bool success = sp_lpe_item_perform_path_effect(SP_LPE_ITEM (shape), c_lpe);
+        if (success) {
+            shape->setCurveInsync( c_lpe, TRUE);
+        }
+        c_lpe->unref();
     }
+    c->unref();
 }
 
-gchar *
-box3d_side_axes_string(Box3DSide *side)
+gchar *box3d_side_axes_string(Box3DSide *side)
 {
     GString *pstring = g_string_new("");
     g_string_printf (pstring, "%s", Box3D::string_from_axes ((Box3D::Axis) (side->dir1 ^ side->dir2)));
@@ -298,15 +309,14 @@ box3d_side_perspective(Box3DSide *side) {
     return SP_BOX3D(SP_OBJECT(side)->parent)->persp_ref->getObject();
 }
 
-Inkscape::XML::Node *
-box3d_side_convert_to_path(Box3DSide *side) {
+Inkscape::XML::Node *box3d_side_convert_to_path(Box3DSide *side) {
     // TODO: Copy over all important attributes (see sp_selected_item_to_curved_repr() for an example)
     SPDocument *doc = SP_OBJECT_DOCUMENT(side);
-    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
+    Inkscape::XML::Document *xml_doc = doc->getReprDoc();
 
     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
-    repr->setAttribute("d", SP_OBJECT_REPR(side)->attribute("d"));
-    repr->setAttribute("style", SP_OBJECT_REPR(side)->attribute("style"));
+    repr->setAttribute("d", side->getAttribute("d"));
+    repr->setAttribute("style", side->getAttribute("style"));
 
     return repr;
 }
@@ -320,4 +330,4 @@ box3d_side_convert_to_path(Box3DSide *side) {
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :