Code

Pot and Dutch translation update
[inkscape.git] / src / box3d-side.cpp
1 #define __BOX3D_SIDE_C__
3 /*
4  * 3D box face implementation
5  *
6  * Authors:
7  *   Maximilian Albert <Anhalter42@gmx.de>
8  *
9  * Copyright (C) 2007  Authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include "box3d-side.h"
15 #include "document.h"
16 #include "xml/document.h"
17 #include "xml/repr.h"
18 #include "display/curve.h"
19 #include "svg/svg.h"
20 #include "attributes.h"
21 #include "inkscape.h"
22 #include "persp3d.h"
23 #include "box3d-context.h"
24 #include "preferences.h"
25 #include "desktop-style.h"
26 #include "box3d.h"
28 static void box3d_side_class_init (Box3DSideClass *klass);
29 static void box3d_side_init (Box3DSide *side);
31 static void box3d_side_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
32 static Inkscape::XML::Node *box3d_side_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
33 static void box3d_side_set (SPObject *object, unsigned int key, const gchar *value);
34 static void box3d_side_update (SPObject *object, SPCtx *ctx, guint flags);
36 static void box3d_side_set_shape (SPShape *shape);
38 static void box3d_side_compute_corner_ids(Box3DSide *side, unsigned int corners[4]);
40 static SPShapeClass *parent_class;
42 GType
43 box3d_side_get_type (void)
44 {
45     static GType type = 0;
47     if (!type) {
48         GTypeInfo info = {
49             sizeof (Box3DSideClass),
50             NULL, NULL,
51             (GClassInitFunc) box3d_side_class_init,
52             NULL, NULL,
53             sizeof (Box3DSide),
54             16,
55             (GInstanceInitFunc) box3d_side_init,
56             NULL,       /* value_table */
57         };
58         type = g_type_register_static (SP_TYPE_SHAPE, "Box3DSide", &info, (GTypeFlags)0);
59     }
60     return type;
61 }
63 static void
64 box3d_side_class_init (Box3DSideClass *klass)
65 {
66     GObjectClass * gobject_class;
67     SPObjectClass * sp_object_class;
68     SPItemClass * item_class;
69     SPPathClass * path_class;
70     SPShapeClass * shape_class;
72     gobject_class = (GObjectClass *) klass;
73     sp_object_class = (SPObjectClass *) klass;
74     item_class = (SPItemClass *) klass;
75     path_class = (SPPathClass *) klass;
76     shape_class = (SPShapeClass *) klass;
78     parent_class = (SPShapeClass *)g_type_class_ref (SP_TYPE_SHAPE);
80     sp_object_class->build = box3d_side_build;
81     sp_object_class->write = box3d_side_write;
82     sp_object_class->set = box3d_side_set;
83     sp_object_class->update = box3d_side_update;
85     shape_class->set_shape = box3d_side_set_shape;
86 }
88 static void
89 box3d_side_init (Box3DSide * side)
90 {
91     side->dir1 = Box3D::NONE;
92     side->dir2 = Box3D::NONE;
93     side->front_or_rear = Box3D::FRONT;
94 }
96 static void
97 box3d_side_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
98 {
99     if (((SPObjectClass *) parent_class)->build)
100         ((SPObjectClass *) parent_class)->build (object, document, repr);
102     sp_object_read_attr(object, "inkscape:box3dsidetype");
105 static Inkscape::XML::Node *
106 box3d_side_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
108     Box3DSide *side = SP_BOX3D_SIDE (object);
110     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
111         // this is where we end up when saving as plain SVG (also in other circumstances?)
112         // thus we don' set "sodipodi:type" so that the box is only saved as an ordinary svg:path
113         repr = xml_doc->createElement("svg:path");
114     }
116     if (flags & SP_OBJECT_WRITE_EXT) {
117         sp_repr_set_int(repr, "inkscape:box3dsidetype", side->dir1 ^ side->dir2 ^ side->front_or_rear);
118     }
120     sp_shape_set_shape ((SPShape *) object);
122     /* Duplicate the path */
123     SPCurve const *curve = ((SPShape *) object)->curve;
124     //Nulls might be possible if this called iteratively
125     if ( !curve ) {
126         return NULL;
127     }
128     char *d = sp_svg_write_path ( curve->get_pathvector() );
129     repr->setAttribute("d", d);
130     g_free (d);
132     if (((SPObjectClass *) (parent_class))->write)
133         ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
135     return repr;
138 static void
139 box3d_side_set (SPObject *object, unsigned int key, const gchar *value)
141     Box3DSide *side = SP_BOX3D_SIDE (object);
143     // TODO: In case the box was recreated (by undo, e.g.) we need to recreate the path
144     //       (along with other info?) from the parent box.
146     /* fixme: we should really collect updates */
147     switch (key) {
148         case SP_ATTR_INKSCAPE_BOX3D_SIDE_TYPE:
149             if (value) {
150                 guint desc = atoi (value);
152                 if (!Box3D::is_face_id(desc)) {
153                     g_print ("desc is not a face id: =%s=\n", value);
154                 }
155                 g_return_if_fail (Box3D::is_face_id (desc));
156                 Box3D::Axis plane = (Box3D::Axis) (desc & 0x7);
157                 plane = (Box3D::is_plane(plane) ? plane : Box3D::orth_plane_or_axis(plane));
158                 side->dir1 = Box3D::extract_first_axis_direction(plane);
159                 side->dir2 = Box3D::extract_second_axis_direction(plane);
160                 side->front_or_rear = (Box3D::FrontOrRear) (desc & 0x8);
162                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
163             }
164             break;
165     default:
166         if (((SPObjectClass *) parent_class)->set)
167             ((SPObjectClass *) parent_class)->set (object, key, value);
168         break;
169     }
172 static void
173 box3d_side_update (SPObject *object, SPCtx *ctx, guint flags)
175     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
176         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
177     }
179     if (flags & (SP_OBJECT_MODIFIED_FLAG |
180                  SP_OBJECT_STYLE_MODIFIED_FLAG |
181                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
182         sp_shape_set_shape ((SPShape *) object);
183     }
185     if (((SPObjectClass *) parent_class)->update)
186         ((SPObjectClass *) parent_class)->update (object, ctx, flags);
189 void
190 box3d_side_position_set (Box3DSide *side) {
191     box3d_side_set_shape (SP_SHAPE (side));
193     /* This call is responsible for live update of the sides during the initial drag */
194     SP_OBJECT(side)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
197 void
198 box3d_side_set_shape (SPShape *shape)
200     Box3DSide *side = SP_BOX3D_SIDE (shape);
201     if (!SP_OBJECT_DOCUMENT(side)->root) {
202         // avoid a warning caused by sp_document_height() (which is called from sp_item_i2d_affine() below)
203         // when reading a file containing 3D boxes
204         return;
205     }
207     SPObject *parent = SP_OBJECT(side)->parent;
208     if (!SP_IS_BOX3D(parent)) {
209         g_warning ("Parent of 3D box side is not a 3D box.\n");
210         return;
211     }
212     SPBox3D *box = SP_BOX3D(parent);
214     Persp3D *persp = box3d_side_perspective(side);
215     if (!persp) {
216         return;
217     }
219     // TODO: Draw the correct quadrangle here
220     //       To do this, determine the perspective of the box, the orientation of the side (e.g., XY-FRONT)
221     //       compute the coordinates of the corners in P^3, project them onto the canvas, and draw the
222     //       resulting path.
224     unsigned int corners[4];
225     box3d_side_compute_corner_ids(side, corners);
227     SPCurve *c = new SPCurve();
229     if (!box3d_get_corner_screen(box, corners[0]).isFinite() ||
230         !box3d_get_corner_screen(box, corners[1]).isFinite() ||
231         !box3d_get_corner_screen(box, corners[2]).isFinite() ||
232         !box3d_get_corner_screen(box, corners[3]).isFinite() )
233     {
234         g_warning ("Trying to draw a 3D box side with invalid coordinates.\n");
235         return;
236     }
238     c->moveto(box3d_get_corner_screen(box, corners[0]));
239     c->lineto(box3d_get_corner_screen(box, corners[1]));
240     c->lineto(box3d_get_corner_screen(box, corners[2]));
241     c->lineto(box3d_get_corner_screen(box, corners[3]));
242     c->closepath();
244     /* Reset the shape'scurve to the "original_curve"
245      * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/
246     sp_shape_set_curve_insync (shape, c, TRUE);
247     if (sp_lpe_item_has_path_effect(SP_LPE_ITEM(shape)) && sp_lpe_item_path_effects_enabled(SP_LPE_ITEM(shape))) {
248         SPCurve *c_lpe = c->copy();
249         bool success = sp_lpe_item_perform_path_effect(SP_LPE_ITEM (shape), c_lpe);
250         if (success) {
251             sp_shape_set_curve_insync (shape, c_lpe, TRUE);
252         }
253         c_lpe->unref();
254     }
255     c->unref();
258 void
259 box3d_side_apply_style (Box3DSide *side) {
260     Inkscape::XML::Node *repr_face = SP_OBJECT_REPR(SP_OBJECT(side));
261     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
263     Glib::ustring descr = "/desktop/";
264     descr += box3d_side_axes_string(side);
265     descr += "/style";
266     Glib::ustring cur_style = prefs->getString(descr);    
267     
268     SPDesktop *desktop = inkscape_active_desktop();
269     bool use_current = prefs->getBool("/tools/shapes/3dbox/usecurrent", false);
270     if (use_current && !cur_style.empty()) {
271         /* use last used style */
272         repr_face->setAttribute("style", cur_style.data());
273     } else {
274         /* use default style */
275         GString *pstring = g_string_new("");
276         g_string_printf (pstring, "/tools/shapes/3dbox/%s", box3d_side_axes_string(side));
277         sp_desktop_apply_style_tool (desktop, repr_face, pstring->str, false);
278     }
281 gchar *
282 box3d_side_axes_string(Box3DSide *side)
284     GString *pstring = g_string_new("");
285     g_string_printf (pstring, "%s", Box3D::string_from_axes ((Box3D::Axis) (side->dir1 ^ side->dir2)));
286     switch ((Box3D::Axis) (side->dir1 ^ side->dir2)) {
287         case Box3D::XY:
288             g_string_append_printf (pstring, (side->front_or_rear == Box3D::FRONT) ? "front" : "rear");
289             break;
290         case Box3D::XZ:
291             g_string_append_printf (pstring, (side->front_or_rear == Box3D::FRONT) ? "top" : "bottom");
292             break;
293         case Box3D::YZ:
294             g_string_append_printf (pstring, (side->front_or_rear == Box3D::FRONT) ? "right" : "left");
295             break;
296         default:
297             break;
298     }
299     return pstring->str;
302 static void
303 box3d_side_compute_corner_ids(Box3DSide *side, unsigned int corners[4]) {
304     Box3D::Axis orth = Box3D::third_axis_direction (side->dir1, side->dir2);
306     corners[0] = (side->front_or_rear ? orth : 0);
307     corners[1] = corners[0] ^ side->dir1;
308     corners[2] = corners[0] ^ side->dir1 ^ side->dir2;
309     corners[3] = corners[0] ^ side->dir2;
312 Persp3D *
313 box3d_side_perspective(Box3DSide *side) {
314     return SP_BOX3D(SP_OBJECT(side)->parent)->persp_ref->getObject();
317 Inkscape::XML::Node *
318 box3d_side_convert_to_path(Box3DSide *side) {
319     // TODO: Copy over all important attributes (see sp_selected_item_to_curved_repr() for an example)
320     SPDocument *doc = SP_OBJECT_DOCUMENT(side);
321     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
323     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
324     repr->setAttribute("d", SP_OBJECT_REPR(side)->attribute("d"));
325     repr->setAttribute("style", SP_OBJECT_REPR(side)->attribute("style"));
327     return repr;
330 /*
331   Local Variables:
332   mode:c++
333   c-file-style:"stroustrup"
334   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
335   indent-tabs-mode:nil
336   fill-column:99
337   End:
338 */
339 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :