Code

NR::Maybe => boost::optional
[inkscape.git] / src / dialogs / tiledialog.cpp
index d56d083ba0e138fc57d0aa5bce6deb81be8285ab..f9bde3c0b18bf5cabd4aa5f9fe6e26ee489f0a75 100644 (file)
@@ -49,18 +49,23 @@ sp_compare_x_position(SPItem *first, SPItem *second)
     using NR::X;
     using NR::Y;
 
-    NR::Rect const a = first->getBounds(sp_item_i2doc_affine(first));
-    double const a_height = a.dimensions()[Y];
+    boost::optional<NR::Rect> a = first->getBounds(from_2geom(sp_item_i2doc_affine(first)));
+    boost::optional<NR::Rect> b = second->getBounds(from_2geom(sp_item_i2doc_affine(second)));
 
-    NR::Rect const b = second->getBounds(sp_item_i2doc_affine(second));
-    double const b_height = b.dimensions()[Y];
+    if ( !a || !b ) {
+        // FIXME?
+        return 0;
+    }
+
+    double const a_height = a->dimensions()[Y];
+    double const b_height = b->dimensions()[Y];
     
     bool a_in_b_vert = false;
-    if ((a.min()[Y] < b.min()[Y] + 0.1) && (a.min()[Y] > b.min()[Y] - b_height)) {
+    if ((a->min()[Y] < b->min()[Y] + 0.1) && (a->min()[Y] > b->min()[Y] - b_height)) {
         a_in_b_vert = true;
-    } else if ((b.min()[Y] < a.min()[Y] + 0.1) && (b.min()[Y] > a.min()[Y] - a_height)) {
+    } else if ((b->min()[Y] < a->min()[Y] + 0.1) && (b->min()[Y] > a->min()[Y] - a_height)) {
         a_in_b_vert = true;
-    } else if (b.min()[Y] == a.min()[Y]) {
+    } else if (b->min()[Y] == a->min()[Y]) {
         a_in_b_vert = true;
     } else {
         a_in_b_vert = false;
@@ -69,10 +74,10 @@ sp_compare_x_position(SPItem *first, SPItem *second)
     if (!a_in_b_vert) {
         return -1;
     }
-    if (a_in_b_vert && a.min()[X] > b.min()[X]) {
+    if (a_in_b_vert && a->min()[X] > b->min()[X]) {
         return 1;
     }
-    if (a_in_b_vert && a.min()[X] < b.min()[X]) {
+    if (a_in_b_vert && a->min()[X] < b->min()[X]) {
         return -1;
     }
     return 0;
@@ -84,13 +89,18 @@ sp_compare_x_position(SPItem *first, SPItem *second)
 int
 sp_compare_y_position(SPItem *first, SPItem *second)
 {
-    NR::Rect const a = first->getBounds(sp_item_i2doc_affine(first));
-    NR::Rect const b = second->getBounds(sp_item_i2doc_affine(second));
+    boost::optional<NR::Rect> a = first->getBounds(from_2geom(sp_item_i2doc_affine(first)));
+    boost::optional<NR::Rect> b = second->getBounds(from_2geom(sp_item_i2doc_affine(second)));
+
+    if ( !a || !b ) {
+        // FIXME?
+        return 0;
+    }
 
-    if (a.min()[NR::Y] > b.min()[NR::Y]) {
+    if (a->min()[NR::Y] > b->min()[NR::Y]) {
         return 1;
     }
-    if (a.min()[NR::Y] < b.min()[NR::Y]) {
+    if (a->min()[NR::Y] < b->min()[NR::Y]) {
         return -1;
     }
     
@@ -151,7 +161,7 @@ void TileDialog::Grid_Arrange ()
     grid_left = 99999;
     grid_top = 99999;
 
-    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
+    SPDesktop *desktop = getDesktop();
     sp_document_ensure_up_to_date(sp_desktop_document(desktop));
 
     Inkscape::Selection *selection = sp_desktop_selection (desktop);
@@ -159,17 +169,22 @@ void TileDialog::Grid_Arrange ()
     cnt=0;
     for (; items != NULL; items = items->next) {
         SPItem *item = SP_ITEM(items->data);
-        NR::Rect const b = item->getBounds(sp_item_i2doc_affine(item));
-        width = b.dimensions()[NR::X];
-        height = b.dimensions()[NR::Y];
-        cx = b.midpoint()[NR::X];
-        cy = b.midpoint()[NR::Y];
-
-        if (b.min()[NR::X] < grid_left) {
-            grid_left = b.min()[NR::X];
+        boost::optional<NR::Rect> b = item->getBounds(from_2geom(sp_item_i2doc_affine(item)));
+        if (!b) {
+            continue;
+        }
+
+        width = b->dimensions()[NR::X];
+        height = b->dimensions()[NR::Y];
+
+        cx = b->midpoint()[NR::X];
+        cy = b->midpoint()[NR::Y];
+
+        if (b->min()[NR::X] < grid_left) {
+            grid_left = b->min()[NR::X];
         }
-        if (b.min()[NR::Y] < grid_top) {
-            grid_top = b.min()[NR::Y];
+        if (b->min()[NR::Y] < grid_top) {
+            grid_top = b->min()[NR::Y];
         }
         if (width > col_width) {
             col_width = width;
@@ -196,15 +211,18 @@ void TileDialog::Grid_Arrange ()
         const GSList *sizes = sorted;
         for (; sizes != NULL; sizes = sizes->next) {
             SPItem *item = SP_ITEM(sizes->data);
-            NR::Rect const b = item->getBounds(sp_item_i2doc_affine(item));
-            width = b.dimensions()[NR::X];
-            height = b.dimensions()[NR::Y];
-            if (width > col_widths[(cnt % NoOfCols)]) {
-                col_widths[(cnt % NoOfCols)] = width;
-            }
-            if (height > row_heights[(cnt / NoOfCols)]) {
-                row_heights[(cnt / NoOfCols)] = height;
+            boost::optional<NR::Rect> b = item->getBounds(from_2geom(sp_item_i2doc_affine(item)));
+            if (b) {
+                width = b->dimensions()[NR::X];
+                height = b->dimensions()[NR::Y];
+                if (width > col_widths[(cnt % NoOfCols)]) {
+                    col_widths[(cnt % NoOfCols)] = width;
+                }
+                if (height > row_heights[(cnt / NoOfCols)]) {
+                    row_heights[(cnt / NoOfCols)] = height;
+                }
             }
+
             cnt++;
         }
 
@@ -250,14 +268,14 @@ void TileDialog::Grid_Arrange ()
     }
 
 
+    boost::optional<NR::Rect> sel_bbox = selection->bounds();
     // Fit to bbox, calculate padding between rows accordingly.
-    if (!SpaceManualRadioButton.get_active()){
-        NR::Rect b = selection->bounds();
+    if ( sel_bbox && !SpaceManualRadioButton.get_active() ){
 #ifdef DEBUG_GRID_ARRANGE
 g_print("\n row = %f     col = %f selection x= %f selection y = %f", total_row_height,total_col_width, b.extent(NR::X), b.extent(NR::Y));
 #endif
-        paddingx = (b.extent(NR::X) - total_col_width) / (NoOfCols -1);
-        paddingy = (b.extent(NR::Y) - total_row_height) / (NoOfRows -1);
+        paddingx = (sel_bbox->extent(NR::X) - total_col_width) / (NoOfCols -1);
+        paddingy = (sel_bbox->extent(NR::Y) - total_row_height) / (NoOfRows -1);
     }
 
 /*
@@ -300,24 +318,29 @@ g_print("\n row = %f     col = %f selection x= %f selection y = %f", total_row_h
              for (; current_row != NULL; current_row = current_row->next) {
                  SPItem *item=SP_ITEM(current_row->data);
                  Inkscape::XML::Node *repr = SP_OBJECT_REPR(item);
-                 NR::Rect const b = item->getBounds(sp_item_i2doc_affine(item));
-                 width = b.dimensions()[NR::X];
-                 height = b.dimensions()[NR::Y];
+                 boost::optional<NR::Rect> b = item->getBounds(from_2geom(sp_item_i2doc_affine(item)));
+                 NR::Point min;
+                 if (b) {
+                     width = b->dimensions()[NR::X];
+                     height = b->dimensions()[NR::Y];
+                     min = b->min();
+                 } else {
+                     width = height = 0;
+                     min = NR::Point(0, 0);
+                 }
+
                  row = cnt / NoOfCols;
                  col = cnt % NoOfCols;
 
-                // original before I started fecking about with it.
-                // new_x = grid_left + (((col_width - width)/2)*HorizAlign) + (( col_width + paddingx ) * (cnt % NoOfCols));
-                // new_y = grid_top + (((row_height - height)/2)*VertAlign) +(( row_height + paddingy ) * (cnt / NoOfCols));
-
                  new_x = grid_left + (((col_widths[col] - width)/2)*HorizAlign) + col_xs[col];
                  new_y = grid_top + (((row_heights[row] - height)/2)*VertAlign) + row_ys[row];
 
-                 NR::Point move = NR::Point(new_x - b.min()[NR::X], b.min()[NR::Y] - new_y); // why are the two args the opposite ways round???
+                 // signs are inverted between x and y due to y inversion
+                 NR::Point move = NR::Point(new_x - min[NR::X], min[NR::Y] - new_y);
                  NR::Matrix const &affine = NR::Matrix(NR::translate(move));
-                 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
+                 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * to_2geom(affine));
                  sp_item_write_transform(item, repr, item->transform,  NULL);
-                 SP_OBJECT (current_row->data)->updateRepr(repr, SP_OBJECT_WRITE_EXT);
+                 SP_OBJECT (current_row->data)->updateRepr();
                  cnt +=1;
              }
              g_slist_free (current_row);
@@ -355,7 +378,7 @@ void TileDialog::on_row_spinbutton_changed()
 
     // in turn, prevent listener from responding
     updating = true;
-    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
+    SPDesktop *desktop = getDesktop();
 
     Inkscape::Selection *selection = sp_desktop_selection (desktop);
 
@@ -380,7 +403,7 @@ void TileDialog::on_col_spinbutton_changed()
 
     // in turn, prevent listener from responding
     updating = true;
-    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
+    SPDesktop *desktop = getDesktop();
     Inkscape::Selection *selection = sp_desktop_selection (desktop);
 
     GSList const *items = selection->itemList();
@@ -542,7 +565,7 @@ void TileDialog::updateSelection()
     row_height=0;
     // in turn, prevent listener from responding
     updating = true;
-    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
+    SPDesktop *desktop = getDesktop();
     Inkscape::Selection *selection = sp_desktop_selection (desktop);
     const GSList *items = selection->itemList();
     int selcount = g_slist_length((GSList *)items);
@@ -570,13 +593,11 @@ void TileDialog::updateSelection()
 
 
 
-
-
 /*##########################
 ## Experimental
 ##########################*/
 
-static void updateSelectionCallback(Inkscape::Application *inkscape, Inkscape::Selection *selection, TileDialog *dlg)
+static void updateSelectionCallback(Inkscape::Application */*inkscape*/, Inkscape::Selection */*selection*/, TileDialog *dlg)
 {
     TileDialog *tledlg = (TileDialog *) dlg;
     tledlg->updateSelection();
@@ -590,7 +611,7 @@ static void updateSelectionCallback(Inkscape::Application *inkscape, Inkscape::S
  * Constructor
  */
 TileDialog::TileDialog()
-    : Dialog ("dialogs.gridtiler", SP_VERB_SELECTION_GRIDTILE)
+    : UI::Widget::Panel("", "dialogs.gridtiler", SP_VERB_SELECTION_GRIDTILE)
 {
      // bool used by spin button callbacks to stop loops where they change each other.
     updating = false;
@@ -605,13 +626,13 @@ TileDialog::TileDialog()
         g_signal_connect ( G_OBJECT (INKSCAPE), "change_selection", G_CALLBACK (updateSelectionCallback), this);
     }
 
-    Gtk::VBox *mainVBox = get_vbox();
+    Gtk::Box *contents = _getContents();
 
 #define MARGIN 2
 
     //##Set up the panel
 
-    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
+    SPDesktop *desktop = getDesktop();
 
     Inkscape::Selection *selection = sp_desktop_selection (desktop);
     int selcount = 1;
@@ -828,7 +849,7 @@ TileDialog::TileDialog()
 
     TileBox.pack_start(SizesHBox, false, false, MARGIN);
 
-    mainVBox->pack_start(TileBox);
+    contents->pack_start(TileBox);
 
     double SpacingType = prefs_get_double_attribute ("dialogs.gridtiler", "SpacingType", 15);
     if (SpacingType>0) {
@@ -841,7 +862,7 @@ TileDialog::TileDialog()
     SizesHBox.set_sensitive (ManualSpacing);
 
     //## The OK button
-    TileOkButton     = add_button(Gtk::Stock::APPLY,   GTK_RESPONSE_APPLY);
+    TileOkButton = addResponseButton(_("Arrange"), GTK_RESPONSE_APPLY);
     tips.set_tip((*TileOkButton), _("Arrange selected objects"));
 
     show_all_children();