Code

place apply horizonally
[inkscape.git] / src / ui / dialog / align-and-distribute.cpp
index dda9cad5f0a29978b17e3c21354c213d47093d73..924a987907a6591dd8d664e291845dcc3c134ae8 100644 (file)
@@ -46,7 +46,8 @@
 #include "sp-flowtext.h"
 #include "text-editing.h"
 
-#include "node-context.h" //For node align/distribute function
+#include "node-context.h"  //For access to ShapeEditor
+#include "shape-editor.h" //For node align/distribute methods
 
 #include "tools-switch.h"
 
@@ -152,9 +153,13 @@ private :
             SPItem * thing = *master;
             selected.erase(master);
             //Compute the anchor point
-            NR::Rect b = sp_item_bbox_desktop (thing);
-            mp = NR::Point(a.mx0 * b.min()[NR::X] + a.mx1 * b.max()[NR::X],
-                           a.my0 * b.min()[NR::Y] + a.my1 * b.max()[NR::Y]);
+            NR::Maybe<NR::Rect> b = sp_item_bbox_desktop (thing);
+            if (b) {
+                mp = NR::Point(a.mx0 * b->min()[NR::X] + a.mx1 * b->max()[NR::X],
+                               a.my0 * b->min()[NR::Y] + a.my1 * b->max()[NR::Y]);
+            } else {
+                return;
+            }
             break;
         }
 
@@ -165,18 +170,26 @@ private :
 
         case AlignAndDistribute::DRAWING:
         {
-            NR::Rect b = sp_item_bbox_desktop
+            NR::Maybe<NR::Rect> b = sp_item_bbox_desktop
                 ( (SPItem *) sp_document_root (sp_desktop_document (desktop)) );
-            mp = NR::Point(a.mx0 * b.min()[NR::X] + a.mx1 * b.max()[NR::X],
-                           a.my0 * b.min()[NR::Y] + a.my1 * b.max()[NR::Y]);
+            if (b) {
+                mp = NR::Point(a.mx0 * b->min()[NR::X] + a.mx1 * b->max()[NR::X],
+                               a.my0 * b->min()[NR::Y] + a.my1 * b->max()[NR::Y]);
+            } else {
+                return;
+            }
             break;
         }
 
         case AlignAndDistribute::SELECTION:
         {
-            NR::Rect b =  selection->bounds();
-            mp = NR::Point(a.mx0 * b.min()[NR::X] + a.mx1 * b.max()[NR::X],
-                           a.my0 * b.min()[NR::Y] + a.my1 * b.max()[NR::Y]);
+            NR::Maybe<NR::Rect> b =  selection->bounds();
+            if (b) {
+                mp = NR::Point(a.mx0 * b->min()[NR::X] + a.mx1 * b->max()[NR::X],
+                               a.my0 * b->min()[NR::Y] + a.my1 * b->max()[NR::Y]);
+            } else {
+                return;
+            }
             break;
         }
 
@@ -201,13 +214,15 @@ private :
              it++)
         {
             sp_document_ensure_up_to_date(sp_desktop_document (desktop));
-            NR::Rect b = sp_item_bbox_desktop (*it);
-            NR::Point const sp(a.sx0 * b.min()[NR::X] + a.sx1 * b.max()[NR::X],
-                               a.sy0 * b.min()[NR::Y] + a.sy1 * b.max()[NR::Y]);
-            NR::Point const mp_rel( mp - sp );
-            if (LInfty(mp_rel) > 1e-9) {
-                sp_item_move_rel(*it, NR::translate(mp_rel));
-                changed = true;
+            NR::Maybe<NR::Rect> b = sp_item_bbox_desktop (*it);
+            if (b) {
+                NR::Point const sp(a.sx0 * b->min()[NR::X] + a.sx1 * b->max()[NR::X],
+                                   a.sy0 * b->min()[NR::Y] + a.sy1 * b->max()[NR::Y]);
+                NR::Point const mp_rel( mp - sp );
+                if (LInfty(mp_rel) > 1e-9) {
+                    sp_item_move_rel(*it, NR::translate(mp_rel));
+                    changed = true;
+                }
             }
         }
 
@@ -216,7 +231,7 @@ private :
 
         if (changed) {
             sp_document_done ( sp_desktop_document (desktop) , SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
-                               /* TODO: annotate */ "align-and-distribute.cpp:219" );
+                               _("Align"));
         }
 
 
@@ -245,9 +260,9 @@ struct BBoxSort
     SPItem *item;
     float anchor;
     NR::Rect bbox;
-    BBoxSort(SPItem *pItem, NR::Dim2 orientation, double kBegin, double kEnd) :
+    BBoxSort(SPItem *pItem, NR::Rect bounds, NR::Dim2 orientation, double kBegin, double kEnd) :
         item(pItem),
-        bbox (sp_item_bbox_desktop (pItem))
+        bbox (bounds)
     {
         anchor = kBegin * bbox.min()[orientation] + kEnd * bbox.max()[orientation];
     }
@@ -307,8 +322,10 @@ private :
             it != selected.end();
             ++it)
         {
-            BBoxSort b (*it, _orientation, _kBegin, _kEnd);
-            sorted.push_back(b);
+            NR::Maybe<NR::Rect> bbox = sp_item_bbox_desktop(*it);
+            if (bbox) {
+                sorted.push_back(BBoxSort(*it, *bbox, _orientation, _kBegin, _kEnd));
+            }
         }
         //sort bbox by anchors
         std::sort(sorted.begin(), sorted.end());
@@ -376,7 +393,7 @@ private :
 
         if (changed) {
             sp_document_done ( sp_desktop_document (desktop), SP_VERB_DIALOG_ALIGN_DISTRIBUTE, 
-                               /* TODO: annotate */ "align-and-distribute.cpp:379");
+                               _("Distribute"));
         }
     }
     guint _index;
@@ -413,12 +430,10 @@ private :
        SPEventContext *event_context = sp_desktop_event_context(SP_ACTIVE_DESKTOP);
        if (!SP_IS_NODE_CONTEXT (event_context)) return ;
 
-        Inkscape::NodePath::Path *nodepath = SP_NODE_CONTEXT (event_context)->nodepath;
-        if (!nodepath) return;
         if (_distribute)
-            sp_nodepath_selected_distribute(nodepath, _orientation);
+            SP_NODE_CONTEXT (event_context)->shape_editor->distribute(_orientation);
         else
-            sp_nodepath_selected_align(nodepath, _orientation);
+            SP_NODE_CONTEXT (event_context)->shape_editor->align(_orientation);
 
     }
 };
@@ -487,7 +502,7 @@ private :
         prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
 
         sp_document_done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_ALIGN_DISTRIBUTE, 
-                         /* TODO: annotate */ "align-and-distribute.cpp:490");
+                         _("Remove overlaps"));
     }
 };
 
@@ -511,13 +526,13 @@ private :
         int saved_compensation = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
         prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
 
-        graphlayout(sp_desktop_selection(SP_ACTIVE_DESKTOP)->itemList(),100);
+        graphlayout(sp_desktop_selection(SP_ACTIVE_DESKTOP)->itemList());
 
         // restore compensation setting
         prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
 
         sp_document_done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_ALIGN_DISTRIBUTE, 
-                         /* TODO: annotate */ "align-and-distribute.cpp:520");
+                         _("Arrange connector network"));
     }
 };
 
@@ -547,7 +562,7 @@ private :
         prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
 
         sp_document_done (sp_desktop_document (SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_ALIGN_DISTRIBUTE, 
-                          /* TODO: annotate */ "align-and-distribute.cpp:550");
+                          _("Unclump"));
     }
 };
 
@@ -579,12 +594,16 @@ private :
         //Check 2 or more selected objects
         if (selected.size() < 2) return;
 
+        NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
+        if (!sel_bbox) {
+            return;
+        }
+
         // This bbox is cached between calls to randomize, so that there's no growth nor shrink
         // nor drift on sequential randomizations. Discard cache on global (or better active
         // desktop's) selection_change signal.
-        if (!_dialog.randomize_bbox_set) {
-            _dialog.randomize_bbox = selection->bounds();
-            _dialog.randomize_bbox_set = true;
+        if (!_dialog.randomize_bbox) {
+            _dialog.randomize_bbox = *sel_bbox;
         }
 
         // see comment in ActionAlign above
@@ -596,22 +615,24 @@ private :
             ++it)
         {
             sp_document_ensure_up_to_date(sp_desktop_document (desktop));
-            NR::Rect item_box = sp_item_bbox_desktop (*it);
-            // find new center, staying within bbox 
-            double x = _dialog.randomize_bbox.min()[NR::X] + item_box.extent(NR::X)/2 +
-                g_random_double_range (0, _dialog.randomize_bbox.extent(NR::X) - item_box.extent(NR::X));
-            double y = _dialog.randomize_bbox.min()[NR::Y] + item_box.extent(NR::Y)/2 +
-                g_random_double_range (0, _dialog.randomize_bbox.extent(NR::Y) - item_box.extent(NR::Y));
-            // displacement is the new center minus old:
-            NR::Point t = NR::Point (x, y) - 0.5*(item_box.max() + item_box.min());
-            sp_item_move_rel(*it, NR::translate(t));
+            NR::Maybe<NR::Rect> item_box = sp_item_bbox_desktop (*it);
+            if (item_box) {
+                // find new center, staying within bbox 
+                double x = _dialog.randomize_bbox->min()[NR::X] + item_box->extent(NR::X)/2 +
+                    g_random_double_range (0, _dialog.randomize_bbox->extent(NR::X) - item_box->extent(NR::X));
+                double y = _dialog.randomize_bbox->min()[NR::Y] + item_box->extent(NR::Y)/2 +
+                    g_random_double_range (0, _dialog.randomize_bbox->extent(NR::Y) - item_box->extent(NR::Y));
+                // displacement is the new center minus old:
+                NR::Point t = NR::Point (x, y) - 0.5*(item_box->max() + item_box->min());
+                sp_item_move_rel(*it, NR::translate(t));
+            }
         }
 
         // restore compensation setting
         prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
 
         sp_document_done (sp_desktop_document (SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_ALIGN_DISTRIBUTE, 
-                          /* TODO: annotate */ "align-and-distribute.cpp:614");
+                          _("Randomize positions"));
     }
 };
 
@@ -706,6 +727,11 @@ private :
                 changed = true;
             }
 
+            if (changed) {
+                sp_document_done (sp_desktop_document (SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_ALIGN_DISTRIBUTE, 
+                                  _("Distribute text baselines"));
+            }
+
         } else {
             for (std::list<SPItem *>::iterator it(selected.begin());
                  it != selected.end();
@@ -720,11 +746,11 @@ private :
                     changed = true;
                 }
             }
-        }
 
-        if (changed) {
-            sp_document_done (sp_desktop_document (SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_ALIGN_DISTRIBUTE, 
-                              /* TODO: annotate */ "align-and-distribute.cpp:727");
+            if (changed) {
+                sp_document_done (sp_desktop_document (SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_ALIGN_DISTRIBUTE, 
+                                  _("Align text baselines"));
+            }
         }
     }
 };
@@ -740,7 +766,7 @@ void on_tool_changed(Inkscape::Application *inkscape, SPEventContext *context, A
 
 void on_selection_changed(Inkscape::Application *inkscape, Inkscape::Selection *selection, AlignAndDistribute *daad)
 {
-    daad->randomize_bbox_set = false;
+    daad->randomize_bbox = NR::Nothing();
 }
 
 /////////////////////////////////////////////////////////
@@ -748,9 +774,9 @@ void on_selection_changed(Inkscape::Application *inkscape, Inkscape::Selection *
 
 
 
-AlignAndDistribute::AlignAndDistribute() 
-    : Dialog ("dialogs.align", SP_VERB_DIALOG_ALIGN_DISTRIBUTE),
-      randomize_bbox (NR::Point (0, 0), NR::Point (0, 0)),
+AlignAndDistribute::AlignAndDistribute(Behavior::BehaviorFactory behavior_factory
+    : Dialog (behavior_factory, "dialogs.align", SP_VERB_DIALOG_ALIGN_DISTRIBUTE),
+      randomize_bbox(NR::Nothing()),
       _alignFrame(_("Align")),
       _distributeFrame(_("Distribute")),
       _removeOverlapFrame(_("Remove overlaps")),
@@ -914,8 +940,7 @@ AlignAndDistribute::AlignAndDistribute()
 
     // Connect to the global selection change, to invalidate cached randomize_bbox
     g_signal_connect (G_OBJECT (INKSCAPE), "change_selection", G_CALLBACK (on_selection_changed), this);
-    randomize_bbox = NR::Rect (NR::Point (0, 0), NR::Point (0, 0));
-    randomize_bbox_set = false;
+    randomize_bbox = NR::Nothing();
 
     show_all_children();
 
@@ -1050,11 +1075,13 @@ std::list<SPItem *>::iterator AlignAndDistribute::find_master( std::list<SPItem
     {
         gdouble max = -1e18;
         for (std::list<SPItem *>::iterator it = list.begin(); it != list.end(); it++) {
-            NR::Rect b = sp_item_bbox_desktop (*it);
-            gdouble dim = b.extent(horizontal ? NR::X : NR::Y);
-            if (dim > max) {
-                max = dim;
-                master = it;
+            NR::Maybe<NR::Rect> b = sp_item_bbox_desktop (*it);
+            if (b) {
+                gdouble dim = b->extent(horizontal ? NR::X : NR::Y);
+                if (dim > max) {
+                    max = dim;
+                    master = it;
+                }
             }
         }
         return master;
@@ -1065,11 +1092,13 @@ std::list<SPItem *>::iterator AlignAndDistribute::find_master( std::list<SPItem
     {
         gdouble max = 1e18;
         for (std::list<SPItem *>::iterator it = list.begin(); it != list.end(); it++) {
-            NR::Rect b = sp_item_bbox_desktop (*it);
-            gdouble dim = b.extent(horizontal ? NR::X : NR::Y);
-            if (dim < max) {
-                max = dim;
-                master = it;
+            NR::Maybe<NR::Rect> b = sp_item_bbox_desktop (*it);
+            if (b) {
+                gdouble dim = b->extent(horizontal ? NR::X : NR::Y);
+                if (dim < max) {
+                    max = dim;
+                    master = it;
+                }
             }
         }
         return master;