Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / sp-item-notify-moveto.cpp
1 /** \file
2  * Implementation of sp_item_notify_moveto().
3  */
5 #include <sp-item.h>
6 #include <2geom/transforms.h>
7 #include <sp-guide.h>
8 #include <sp-item-rm-unsatisfied-cns.h>
9 using std::vector;
12 /**
13  * Called by sp_guide_moveto to indicate that the guide line corresponding to g has been moved, and
14  * that consequently this item should move with it.
15  *
16  * \pre exist [cn in item.constraints] g eq cn.g.
17  */
18 void sp_item_notify_moveto(SPItem &item, SPGuide const &mv_g, int const snappoint_ix,
19                            double const position, bool const commit)
20 {
21     g_return_if_fail(SP_IS_ITEM(&item));
22     g_return_if_fail( unsigned(snappoint_ix) < 8 );
23     Geom::Point const dir( mv_g.normal_to_line );
24     double const dir_lensq(dot(dir, dir));
25     g_return_if_fail( dir_lensq != 0 );
27     std::vector<Inkscape::SnapCandidatePoint> snappoints;
28     item.getSnappoints(snappoints, NULL);
29     g_return_if_fail( snappoint_ix < int(snappoints.size()) );
31     double const pos0 = dot(dir, snappoints[snappoint_ix].getPoint());
32     /// \todo effic: skip if mv_g is already satisfied.
34     /* Translate along dir to make dot(dir, snappoints(item)[snappoint_ix]) == position. */
36     /* Calculation:
37        dot(dir, snappoints[snappoint_ix] + s * dir) = position.
38        dot(dir, snappoints[snappoint_ix]) + dot(dir, s * dir) = position.
39        pos0 + s * dot(dir, dir) = position.
40        s * lensq(dir) = position - pos0.
41        s = (position - pos0) / dot(dir, dir). */
42     Geom::Translate const tr( ( position - pos0 )
43                             * ( dir / dir_lensq ) );
44     item.set_i2d_affine(item.i2d_affine() * tr);
45     /// \todo Reget snappoints, check satisfied.
47     if (commit) {
48         /// \todo Consider maintaining a set of dirty items.
50         /* Commit repr. */
51         {
52             item.doWriteTransform(SP_OBJECT_REPR(&item), item.transform);
53         }
55         sp_item_rm_unsatisfied_cns(item);
56 #if 0 /* nyi */
57         move_cn_to_front(mv_g, snappoint_ix, item.constraints);
58         /** \note If the guideline is connected to multiple snappoints of
59          * this item, then keeping those cns in order requires that the
60          * guide send notifications in order of increasing importance.
61          */
62 #endif
63     }
64 }
67 /*
68   Local Variables:
69   mode:c++
70   c-file-style:"stroustrup"
71   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
72   indent-tabs-mode:nil
73   fill-column:99
74   End:
75 */
76 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :