Code

NR::Maybe => boost::optional
[inkscape.git] / src / selection-chemistry.cpp
index d4964c284525d6859bd64cf8176257d3301e035c..114ed9c645a67470811fe9d9502201820303eab0 100644 (file)
@@ -567,12 +567,12 @@ sp_item_list_common_parent_group(GSList const *items)
 }
 
 /** Finds out the minimum common bbox of the selected items. */
-static NR::Maybe<NR::Rect>
+static boost::optional<NR::Rect>
 enclose_items(GSList const *items)
 {
     g_assert(items != NULL);
 
-    NR::Maybe<NR::Rect> r = NR::Nothing();
+    boost::optional<NR::Rect> r;
     for (GSList const *i = items; i; i = i->next) {
         r = NR::union_bounds(r, sp_item_bbox_desktop((SPItem *) i->data));
     }
@@ -621,7 +621,7 @@ sp_selection_raise()
     rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
 
     // Determine the common bbox of the selected items.
-    NR::Maybe<NR::Rect> selected = enclose_items(items);
+    boost::optional<NR::Rect> selected = enclose_items(items);
 
     // Iterate over all objects in the selection (starting from top).
     if (selected) {
@@ -631,7 +631,7 @@ sp_selection_raise()
             for (SPObject *newref = child->next; newref; newref = newref->next) {
                 // if the sibling is an item AND overlaps our selection,
                 if (SP_IS_ITEM(newref)) {
-                    NR::Maybe<NR::Rect> newref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
+                    boost::optional<NR::Rect> newref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
                     if ( newref_bbox && selected->intersects(*newref_bbox) ) {
                         // AND if it's not one of our selected objects,
                         if (!g_slist_find((GSList *) items, newref)) {
@@ -713,7 +713,7 @@ sp_selection_lower()
     Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
 
     // Determine the common bbox of the selected items.
-    NR::Maybe<NR::Rect> selected = enclose_items(items);
+    boost::optional<NR::Rect> selected = enclose_items(items);
 
     /* Construct direct-ordered list of selected children. */
     GSList *rev = g_slist_copy((GSList *) items);
@@ -728,7 +728,7 @@ sp_selection_lower()
             for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
                 // if the sibling is an item AND overlaps our selection,
                 if (SP_IS_ITEM(newref)) {
-                    NR::Maybe<NR::Rect> ref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
+                    boost::optional<NR::Rect> ref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
                     if ( ref_bbox && selected->intersects(*ref_bbox) ) {
                         // AND if it's not one of our selected objects,
                         if (!g_slist_find((GSList *) items, newref)) {
@@ -1248,7 +1248,7 @@ sp_selection_scale_absolute(Inkscape::Selection *selection,
     if (selection->isEmpty())
         return;
 
-    NR::Maybe<NR::Rect> const bbox(selection->bounds());
+    boost::optional<NR::Rect> const bbox(selection->bounds());
     if ( !bbox || bbox->isEmpty() ) {
         return;
     }
@@ -1270,7 +1270,7 @@ void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const
     if (selection->isEmpty())
         return;
 
-    NR::Maybe<NR::Rect> const bbox(selection->bounds());
+    boost::optional<NR::Rect> const bbox(selection->bounds());
 
     if ( !bbox || bbox->isEmpty() ) {
         return;
@@ -1379,7 +1379,7 @@ sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
     if (selection->isEmpty())
         return;
 
-    NR::Maybe<NR::Point> center = selection->center();
+    boost::optional<NR::Point> center = selection->center();
     if (!center) {
         return;
     }
@@ -1403,8 +1403,8 @@ sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
     if (selection->isEmpty())
         return;
 
-    NR::Maybe<NR::Rect> const bbox(selection->bounds());
-    NR::Maybe<NR::Point> center = selection->center();
+    boost::optional<NR::Rect> const bbox(selection->bounds());
+    boost::optional<NR::Point> center = selection->center();
 
     if ( !bbox || !center ) {
         return;
@@ -1432,7 +1432,7 @@ sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
     if (selection->isEmpty())
         return;
 
-    NR::Maybe<NR::Rect> const bbox(selection->bounds());
+    boost::optional<NR::Rect> const bbox(selection->bounds());
     if (!bbox) {
         return;
     }
@@ -1469,7 +1469,7 @@ sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
     if (selection->isEmpty())
         return;
 
-    NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
+    boost::optional<NR::Rect> sel_bbox = selection->bounds();
 
     if (!sel_bbox) {
         return;
@@ -1683,7 +1683,9 @@ void sp_selection_edit_clip_or_mask(SPDesktop * dt, bool clip)
                         }
 
                         ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
-                        shape_editor->set_item(SP_ITEM(child));
+                        // TODO: should we set the item for nodepath or knotholder or both? seems to work with both.
+                        shape_editor->set_item(SP_ITEM(child), SH_NODEPATH);
+                        shape_editor->set_item(SP_ITEM(child), SH_KNOTHOLDER);
                         Inkscape::NodePath::Path *np = shape_editor->get_nodepath();
                         if (np) {
                             // take colors from prefs (same as used in outline mode)
@@ -1793,7 +1795,7 @@ SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
 {
     NR::Rect dbox = desktop->get_display_area();
-    NR::Maybe<NR::Rect> sbox = sp_item_bbox_desktop(item);
+    boost::optional<NR::Rect> sbox = sp_item_bbox_desktop(item);
 
     if ( sbox && dbox.contains(*sbox) == false ) {
         NR::Point const s_dt = sbox->midpoint();
@@ -1887,7 +1889,6 @@ sp_selection_relink()
     gchar *newref = g_strdup_printf ("#%s", newid);
 
     // Get a copy of current selection.
-    GSList *new_select = NULL;
     bool relinked = false;
     for (GSList *items = (GSList *) selection->itemList();
          items != NULL;
@@ -2053,8 +2054,8 @@ void sp_selection_to_marker(bool apply)
     }
 
     sp_document_ensure_up_to_date(doc);
-    NR::Maybe<NR::Rect> r = selection->bounds();
-    NR::Maybe<NR::Point> c = selection->center();
+    boost::optional<NR::Rect> r = selection->bounds();
+    boost::optional<NR::Point> c = selection->center();
     if ( !r || !c || r->isEmpty() ) {
         return;
     }
@@ -2176,7 +2177,7 @@ sp_selection_tile(bool apply)
     }
 
     sp_document_ensure_up_to_date(doc);
-    NR::Maybe<NR::Rect> r = selection->bounds();
+    boost::optional<NR::Rect> r = selection->bounds();
     if ( !r || r->isEmpty() ) {
         return;
     }
@@ -2544,7 +2545,8 @@ sp_selection_create_bitmap_copy ()
     // Run filter, if any
     if (run) {
         g_print ("Running external filter: %s\n", run);
-        system (run);
+        int retval;
+        retval = system (run);
     }
 
     // Import the image back
@@ -2749,7 +2751,9 @@ void sp_selection_unset_mask(bool apply_clip_path) {
     sp_document_ensure_up_to_date(doc);
 
     gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
-    std::map<SPObject*,SPItem*> referenced_objects;
+    std::map<SPObject*,SPItem*> referenced_objects; 
+    // SPObject* refers to a group containing the clipped path or mask itself, 
+    // whereas SPItem* refers to the item being clipped or masked
     for (GSList const *i = selection->itemList(); NULL != i; i = i->next) {
         if (remove_original) {
             // remember referenced mask/clippath, so orphaned masks can be moved back to document
@@ -2773,9 +2777,10 @@ void sp_selection_unset_mask(bool apply_clip_path) {
 
     // restore mask objects into a document
     for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
-        SPObject *obj = (*it).first;
+        SPObject *obj = (*it).first; // Group containing the clipped paths or masks
         GSList *items_to_move = NULL;
         for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
+            // Collect all clipped paths and masks within a single group 
             Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
             items_to_move = g_slist_prepend (items_to_move, copy);
         }
@@ -2789,6 +2794,7 @@ void sp_selection_unset_mask(bool apply_clip_path) {
         Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
         gint pos = SP_OBJECT_REPR((*it).second)->position();
 
+        // Iterate through all clipped paths / masks
         for (GSList *i = items_to_move; NULL != i; i = i->next) {
             Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
 
@@ -2814,33 +2820,67 @@ void sp_selection_unset_mask(bool apply_clip_path) {
         sp_document_done (doc, SP_VERB_OBJECT_UNSET_MASK, _("Release mask"));
 }
 
-void fit_canvas_to_selection(SPDesktop *desktop) {
-    g_return_if_fail(desktop != NULL);
+/**
+ * Returns true if an undoable change should be recorded.
+ */
+bool
+fit_canvas_to_selection(SPDesktop *desktop)
+{
+    g_return_val_if_fail(desktop != NULL, false);
     SPDocument *doc = sp_desktop_document(desktop);
 
-    g_return_if_fail(doc != NULL);
-    g_return_if_fail(desktop->selection != NULL);
+    g_return_val_if_fail(doc != NULL, false);
+    g_return_val_if_fail(desktop->selection != NULL, false);
 
     if (desktop->selection->isEmpty()) {
         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to fit canvas to."));
-        return;
+        return false;
     }
-    NR::Maybe<NR::Rect> const bbox(desktop->selection->bounds());
+    boost::optional<NR::Rect> const bbox(desktop->selection->bounds());
     if (bbox && !bbox->isEmpty()) {
         doc->fitToRect(*bbox);
+        return true;
+    } else {
+        return false;
     }
-};
+}
 
-void fit_canvas_to_drawing(SPDocument *doc) {
-    g_return_if_fail(doc != NULL);
+/**
+ * Fit canvas to the bounding box of the selection, as an undoable action.
+ */
+void
+verb_fit_canvas_to_selection(SPDesktop *const desktop)
+{
+    if (fit_canvas_to_selection(desktop)) {
+        sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_SELECTION,
+                         _("Fit Page to Selection"));
+    }
+}
+
+bool
+fit_canvas_to_drawing(SPDocument *doc)
+{
+    g_return_val_if_fail(doc != NULL, false);
 
     sp_document_ensure_up_to_date(doc);
     SPItem const *const root = SP_ITEM(doc->root);
-    NR::Maybe<NR::Rect> const bbox(root->getBounds(from_2geom(sp_item_i2r_affine(root))));
+    boost::optional<NR::Rect> const bbox(root->getBounds(from_2geom(sp_item_i2r_affine(root))));
     if (bbox && !bbox->isEmpty()) {
         doc->fitToRect(*bbox);
+        return true;
+    } else {
+        return false;
     }
-};
+}
+
+void
+verb_fit_canvas_to_drawing(SPDesktop *desktop)
+{
+    if (fit_canvas_to_drawing(sp_desktop_document(desktop))) {
+        sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_DRAWING,
+                         _("Fit Page to Drawing"));
+    }
+}
 
 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
     g_return_if_fail(desktop != NULL);
@@ -2849,14 +2889,13 @@ void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
     g_return_if_fail(doc != NULL);
     g_return_if_fail(desktop->selection != NULL);
 
-    if (desktop->selection->isEmpty()) {
-        fit_canvas_to_drawing(doc);
-    } else {
-        fit_canvas_to_selection(desktop);
+    bool const changed = ( desktop->selection->isEmpty()
+                           ? fit_canvas_to_drawing(doc)
+                           : fit_canvas_to_selection(desktop) );
+    if (changed) {
+        sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_SELECTION_OR_DRAWING,
+                         _("Fit Page to Selection or Drawing"));
     }
-
-    sp_document_done(doc, SP_VERB_FIT_CANVAS_TO_DRAWING,
-                     _("Fit page to selection"));
 };
 
 static void itemtree_map(void (*f)(SPItem *, SPDesktop *), SPObject *root, SPDesktop *desktop) {