Code

Merge from trunk
[inkscape.git] / src / dialogs / tiledialog.cpp
index f9bde3c0b18bf5cabd4aa5f9fe6e26ee489f0a75..f597c601baf783b3aeae1c812063e4dfedfc1a13 100644 (file)
@@ -23,9 +23,8 @@
 #include <glibmm/i18n.h>
 #include <gtkmm/stock.h>
 
-#include "libnr/nr-matrix-ops.h"
 #include "verbs.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "inkscape.h"
 #include "desktop-handles.h"
 #include "selection.h"
 int
 sp_compare_x_position(SPItem *first, SPItem *second)
 {
-    using NR::X;
-    using NR::Y;
+    using Geom::X;
+    using Geom::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)));
+    boost::optional<Geom::Rect> a = first->getBounds(sp_item_i2doc_affine(first));
+    boost::optional<Geom::Rect> b = second->getBounds(sp_item_i2doc_affine(second));
 
     if ( !a || !b ) {
         // FIXME?
@@ -89,18 +88,18 @@ sp_compare_x_position(SPItem *first, SPItem *second)
 int
 sp_compare_y_position(SPItem *first, SPItem *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)));
+    boost::optional<Geom::Rect> a = first->getBounds(sp_item_i2doc_affine(first));
+    boost::optional<Geom::Rect> b = second->getBounds(sp_item_i2doc_affine(second));
 
     if ( !a || !b ) {
         // FIXME?
         return 0;
     }
 
-    if (a->min()[NR::Y] > b->min()[NR::Y]) {
+    if (a->min()[Geom::Y] > b->min()[Geom::Y]) {
         return 1;
     }
-    if (a->min()[NR::Y] < b->min()[NR::Y]) {
+    if (a->min()[Geom::Y] < b->min()[Geom::Y]) {
         return -1;
     }
     
@@ -169,22 +168,22 @@ void TileDialog::Grid_Arrange ()
     cnt=0;
     for (; items != NULL; items = items->next) {
         SPItem *item = SP_ITEM(items->data);
-        boost::optional<NR::Rect> b = item->getBounds(from_2geom(sp_item_i2doc_affine(item)));
+        boost::optional<Geom::Rect> b = item->getBounds(sp_item_i2doc_affine(item));
         if (!b) {
             continue;
         }
 
-        width = b->dimensions()[NR::X];
-        height = b->dimensions()[NR::Y];
+        width = b->dimensions()[Geom::X];
+        height = b->dimensions()[Geom::Y];
 
-        cx = b->midpoint()[NR::X];
-        cy = b->midpoint()[NR::Y];
+        cx = b->midpoint()[Geom::X];
+        cy = b->midpoint()[Geom::Y];
 
-        if (b->min()[NR::X] < grid_left) {
-            grid_left = b->min()[NR::X];
+        if (b->min()[Geom::X] < grid_left) {
+            grid_left = b->min()[Geom::X];
         }
-        if (b->min()[NR::Y] < grid_top) {
-            grid_top = b->min()[NR::Y];
+        if (b->min()[Geom::Y] < grid_top) {
+            grid_top = b->min()[Geom::Y];
         }
         if (width > col_width) {
             col_width = width;
@@ -211,10 +210,10 @@ void TileDialog::Grid_Arrange ()
         const GSList *sizes = sorted;
         for (; sizes != NULL; sizes = sizes->next) {
             SPItem *item = SP_ITEM(sizes->data);
-            boost::optional<NR::Rect> b = item->getBounds(from_2geom(sp_item_i2doc_affine(item)));
+            boost::optional<Geom::Rect> b = item->getBounds(sp_item_i2doc_affine(item));
             if (b) {
-                width = b->dimensions()[NR::X];
-                height = b->dimensions()[NR::Y];
+                width = b->dimensions()[Geom::X];
+                height = b->dimensions()[Geom::Y];
                 if (width > col_widths[(cnt % NoOfCols)]) {
                     col_widths[(cnt % NoOfCols)] = width;
                 }
@@ -268,14 +267,14 @@ void TileDialog::Grid_Arrange ()
     }
 
 
-    boost::optional<NR::Rect> sel_bbox = selection->bounds();
+    boost::optional<Geom::Rect> sel_bbox = selection->bounds();
     // Fit to bbox, calculate padding between rows accordingly.
     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));
+g_print("\n row = %f     col = %f selection x= %f selection y = %f", total_row_height,total_col_width, b.extent(Geom::X), b.extent(Geom::Y));
 #endif
-        paddingx = (sel_bbox->extent(NR::X) - total_col_width) / (NoOfCols -1);
-        paddingy = (sel_bbox->extent(NR::Y) - total_row_height) / (NoOfRows -1);
+        paddingx = (sel_bbox->width() - total_col_width) / (NoOfCols -1);
+        paddingy = (sel_bbox->height() - total_row_height) / (NoOfRows -1);
     }
 
 /*
@@ -318,15 +317,15 @@ 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);
-                 boost::optional<NR::Rect> b = item->getBounds(from_2geom(sp_item_i2doc_affine(item)));
-                 NR::Point min;
+                 boost::optional<Geom::Rect> b = item->getBounds(sp_item_i2doc_affine(item));
+                 Geom::Point min;
                  if (b) {
-                     width = b->dimensions()[NR::X];
-                     height = b->dimensions()[NR::Y];
+                     width = b->dimensions()[Geom::X];
+                     height = b->dimensions()[Geom::Y];
                      min = b->min();
                  } else {
                      width = height = 0;
-                     min = NR::Point(0, 0);
+                     min = Geom::Point(0, 0);
                  }
 
                  row = cnt / NoOfCols;
@@ -336,9 +335,9 @@ g_print("\n row = %f     col = %f selection x= %f selection y = %f", total_row_h
                  new_y = grid_top + (((row_heights[row] - height)/2)*VertAlign) + row_ys[row];
 
                  // 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) * to_2geom(affine));
+                 Geom::Point move = Geom::Point(new_x - min[Geom::X], min[Geom::Y] - new_y);
+                 Geom::Matrix const affine = Geom::Matrix(Geom::Translate(move));
+                 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
                  sp_item_write_transform(item, repr, item->transform,  NULL);
                  SP_OBJECT (current_row->data)->updateRepr();
                  cnt +=1;
@@ -346,11 +345,8 @@ g_print("\n row = %f     col = %f selection x= %f selection y = %f", total_row_h
              g_slist_free (current_row);
     }
 
-    NRRect b;
-            selection->bounds(&b);
-
-            sp_document_done (sp_desktop_document (desktop), SP_VERB_SELECTION_GRIDTILE, 
-                              _("Arrange in a grid"));
+    sp_document_done (sp_desktop_document (desktop), SP_VERB_SELECTION_GRIDTILE, 
+                      _("Arrange in a grid"));
 
 }
 
@@ -387,7 +383,8 @@ void TileDialog::on_row_spinbutton_changed()
 
     double PerCol = ceil(selcount / NoOfColsSpinner.get_value());
     NoOfRowsSpinner.set_value(PerCol);
-    prefs_set_double_attribute ("dialogs.gridtiler", "NoOfCols", NoOfColsSpinner.get_value());
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    prefs->setDouble("/dialogs/gridtiler/NoOfCols", NoOfColsSpinner.get_value());
     updating=false;
 }
 
@@ -411,7 +408,8 @@ void TileDialog::on_col_spinbutton_changed()
 
     double PerRow = ceil(selcount / NoOfRowsSpinner.get_value());
     NoOfColsSpinner.set_value(PerRow);
-    prefs_set_double_attribute ("dialogs.gridtiler", "NoOfCols", PerRow);
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    prefs->setDouble("/dialogs/gridtiler/NoOfCols", PerRow);
 
     updating=false;
 }
@@ -421,8 +419,8 @@ void TileDialog::on_col_spinbutton_changed()
  */
 void TileDialog::on_xpad_spinbutton_changed()
 {
-
-    prefs_set_double_attribute ("dialogs.gridtiler", "XPad", XPadSpinner.get_value());
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    prefs->setDouble("/dialogs/gridtiler/XPad", XPadSpinner.get_value());
 
 }
 
@@ -431,9 +429,8 @@ void TileDialog::on_xpad_spinbutton_changed()
  */
 void TileDialog::on_ypad_spinbutton_changed()
 {
-
-    prefs_set_double_attribute ("dialogs.gridtiler", "YPad", YPadSpinner.get_value());
-
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    prefs->setDouble("/dialogs/gridtiler/YPad", YPadSpinner.get_value());
 }
 
 
@@ -442,13 +439,13 @@ void TileDialog::on_ypad_spinbutton_changed()
  */
 void TileDialog::on_RowSize_checkbutton_changed()
 {
-
-   if (RowHeightButton.get_active()) {
-       prefs_set_double_attribute ("dialogs.gridtiler", "AutoRowSize", 20);
-   } else {
-       prefs_set_double_attribute ("dialogs.gridtiler", "AutoRowSize", -20);
-   }
-   RowHeightBox.set_sensitive ( !RowHeightButton.get_active());
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    if (RowHeightButton.get_active()) {
+        prefs->setDouble("/dialogs/gridtiler/AutoRowSize", 20);
+    } else {
+        prefs->setDouble("/dialogs/gridtiler/AutoRowSize", -20);
+    }
+    RowHeightBox.set_sensitive ( !RowHeightButton.get_active());
 }
 
 /**
@@ -456,14 +453,13 @@ void TileDialog::on_RowSize_checkbutton_changed()
  */
 void TileDialog::on_ColSize_checkbutton_changed()
 {
-
-   if (ColumnWidthButton.get_active()) {
-       prefs_set_double_attribute ("dialogs.gridtiler", "AutoColSize", 20);
-   } else {
-       prefs_set_double_attribute ("dialogs.gridtiler", "AutoColSize", -20);
-   }
-   ColumnWidthBox.set_sensitive ( !ColumnWidthButton.get_active());
-
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    if (ColumnWidthButton.get_active()) {
+        prefs->setDouble("/dialogs/gridtiler/AutoColSize", 20);
+    } else {
+        prefs->setDouble("/dialogs/gridtiler/AutoColSize", -20);
+    }
+    ColumnWidthBox.set_sensitive ( !ColumnWidthButton.get_active());
 }
 
 /**
@@ -478,7 +474,8 @@ void TileDialog::on_rowSize_spinbutton_changed()
 
     // in turn, prevent listener from responding
     updating = true;
-    prefs_set_double_attribute ("dialogs.gridtiler", "RowHeight", RowHeightSpinner.get_value());
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    prefs->setDouble("/dialogs/gridtiler/RowHeight", RowHeightSpinner.get_value());
     updating=false;
 
 }
@@ -495,7 +492,8 @@ void TileDialog::on_colSize_spinbutton_changed()
 
     // in turn, prevent listener from responding
     updating = true;
-    prefs_set_double_attribute ("dialogs.gridtiler", "ColWidth", ColumnWidthSpinner.get_value());
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    prefs->setDouble("/dialogs/gridtiler/ColWidth", ColumnWidthSpinner.get_value());
     updating=false;
 
 }
@@ -505,13 +503,14 @@ void TileDialog::on_colSize_spinbutton_changed()
  */
 void TileDialog::Spacing_button_changed()
 {
-   if (SpaceManualRadioButton.get_active()) {
-       prefs_set_double_attribute ("dialogs.gridtiler", "SpacingType", 20);
-   } else {
-       prefs_set_double_attribute ("dialogs.gridtiler", "SpacingType", -20);
-   }
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    if (SpaceManualRadioButton.get_active()) {
+        prefs->setDouble("/dialogs/gridtiler/SpacingType", 20);
+    } else {
+        prefs->setDouble("/dialogs/gridtiler/SpacingType", -20);
+    }
 
-   SizesHBox.set_sensitive ( SpaceManualRadioButton.get_active());
+    SizesHBox.set_sensitive ( SpaceManualRadioButton.get_active());
 }
 
 /**
@@ -519,17 +518,17 @@ void TileDialog::Spacing_button_changed()
  */
 void TileDialog::VertAlign_changed()
 {
-   if (VertTopRadioButton.get_active()) {
-       VertAlign = 0;
-       prefs_set_double_attribute ("dialogs.gridtiler", "VertAlign", 0);
-   } else if (VertCentreRadioButton.get_active()){
-       VertAlign = 1;
-       prefs_set_double_attribute ("dialogs.gridtiler", "VertAlign", 1);
-   } else if (VertBotRadioButton.get_active()){
-       VertAlign = 2;
-       prefs_set_double_attribute ("dialogs.gridtiler", "VertAlign", 2);
-   }
-
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    if (VertTopRadioButton.get_active()) {
+        VertAlign = 0;
+        prefs->setInt("/dialogs/gridtiler/VertAlign", 0);
+    } else if (VertCentreRadioButton.get_active()){
+        VertAlign = 1;
+        prefs->setInt("/dialogs/gridtiler/VertAlign", 1);
+    } else if (VertBotRadioButton.get_active()){
+        VertAlign = 2;
+        prefs->setInt("/dialogs/gridtiler/VertAlign", 2);
+    }
 }
 
 /**
@@ -537,17 +536,17 @@ void TileDialog::VertAlign_changed()
  */
 void TileDialog::HorizAlign_changed()
 {
-   if (HorizLeftRadioButton.get_active()) {
-       HorizAlign = 0;
-       prefs_set_double_attribute ("dialogs.gridtiler", "HorizAlign", 0);
-   } else if (HorizCentreRadioButton.get_active()){
-       HorizAlign = 1;
-       prefs_set_double_attribute ("dialogs.gridtiler", "HorizAlign", 1);
-   } else if (HorizRightRadioButton.get_active()){
-       HorizAlign = 2;
-       prefs_set_double_attribute ("dialogs.gridtiler", "HorizAlign", 2);
-   }
-
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    if (HorizLeftRadioButton.get_active()) {
+        HorizAlign = 0;
+        prefs->setInt("/dialogs/gridtiler/HorizAlign", 0);
+    } else if (HorizCentreRadioButton.get_active()){
+        HorizAlign = 1;
+        prefs->setInt("/dialogs/gridtiler/HorizAlign", 1);
+    } else if (HorizRightRadioButton.get_active()){
+        HorizAlign = 2;
+        prefs->setInt("/dialogs/gridtiler/HorizAlign", 2);
+    }
 }
 
 /**
@@ -555,14 +554,14 @@ void TileDialog::HorizAlign_changed()
  */
 void TileDialog::updateSelection()
 {
-    double col_width, row_height;
     // quit if run by the attr_changed listener
     if (updating) {
-            return;
-        }
+        return;
+    }
 
-    col_width=0;
-    row_height=0;
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    double col_width = 0;
+    double row_height = 0;
     // in turn, prevent listener from responding
     updating = true;
     SPDesktop *desktop = getDesktop();
@@ -570,7 +569,7 @@ void TileDialog::updateSelection()
     const GSList *items = selection->itemList();
     int selcount = g_slist_length((GSList *)items);
 
-    if (NoOfColsSpinner.get_value()>1){
+    if (NoOfColsSpinner.get_value()>1 && NoOfRowsSpinner.get_value()>1){
         // Update the number of rows assuming number of columns wanted remains same.
         double NoOfRows = ceil(selcount / NoOfColsSpinner.get_value());
         NoOfRowsSpinner.set_value(NoOfRows);
@@ -579,11 +578,14 @@ void TileDialog::updateSelection()
         if (selcount<NoOfColsSpinner.get_value()) {
             double NoOfCols = ceil(selcount / NoOfRowsSpinner.get_value());
             NoOfColsSpinner.set_value(NoOfCols);
-            prefs_set_double_attribute ("dialogs.gridtiler", "NoOfCols", NoOfCols);
+            prefs->setInt("/dialogs/gridtiler/NoOfCols", NoOfCols);
         }
     } else {
-        NoOfColsSpinner.set_value(selcount);
-        prefs_set_double_attribute ("dialogs.gridtiler", "NoOfCols", selcount);
+        double PerRow = ceil(sqrt(selcount));
+        double PerCol = ceil(sqrt(selcount));
+        NoOfRowsSpinner.set_value(PerRow);
+        NoOfColsSpinner.set_value(PerCol);
+        prefs->setInt("/dialogs/gridtiler/NoOfCols", static_cast<int>(PerCol));
 
     }
 
@@ -611,10 +613,11 @@ static void updateSelectionCallback(Inkscape::Application */*inkscape*/, Inkscap
  * Constructor
  */
 TileDialog::TileDialog()
-    : UI::Widget::Panel("", "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;
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
     // could not do this in gtkmm - there's no Gtk::SizeGroup public constructor (!)
     GtkSizeGroup *_col1 = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
@@ -644,8 +647,8 @@ TileDialog::TileDialog()
 
     /*#### Number of Rows ####*/
 
-    double PerRow = selcount;
-    double PerCol = 1;
+    double PerRow = ceil(sqrt(selcount));
+    double PerCol = ceil(sqrt(selcount));
 
     #ifdef DEBUG_GRID_ARRANGE
         g_print("/n PerRox = %f PerCol = %f selcount = %d",PerRow,PerCol,selcount);
@@ -664,7 +667,7 @@ TileDialog::TileDialog()
     gtk_size_group_add_widget(_col1, (GtkWidget *) NoOfRowsBox.gobj());
 
     RowHeightButton.set_label(_("Equal height"));
-    double AutoRow = prefs_get_double_attribute ("dialogs.gridtiler", "AutoRowSize", 15);
+    double AutoRow = prefs->getDouble("/dialogs/gridtiler/AutoRowSize", 15);
     if (AutoRow>0)
          AutoRowSize=true;
     else
@@ -694,7 +697,7 @@ TileDialog::TileDialog()
         VertBotRadioButton.signal_toggled().connect(sigc::mem_fun(*this, &TileDialog::VertAlign_changed));
         VertAlignVBox.pack_start(VertBotRadioButton, false, false, 0);
 
-        VertAlign = prefs_get_double_attribute ("dialogs.gridtiler", "VertAlign", 1);
+        VertAlign = prefs->getInt("/dialogs/gridtiler/VertAlign", 1);
         if (VertAlign == 0) {
             VertTopRadioButton.set_active(TRUE);
         }
@@ -734,7 +737,7 @@ TileDialog::TileDialog()
     gtk_size_group_add_widget(_col3, (GtkWidget *) NoOfColsBox.gobj());
 
     ColumnWidthButton.set_label(_("Equal width"));
-    double AutoCol = prefs_get_double_attribute ("dialogs.gridtiler", "AutoColSize", 15);
+    double AutoCol = prefs->getDouble("/dialogs/gridtiler/AutoColSize", 15);
     if (AutoCol>0)
          AutoColSize=true;
     else
@@ -768,7 +771,7 @@ TileDialog::TileDialog()
 
         HorizAlignHBox.pack_start(*(new Gtk::HBox()), true, true, 0); // centering strut
 
-        HorizAlign = prefs_get_double_attribute ("dialogs.gridtiler", "HorizAlign", 1);
+        HorizAlign = prefs->getInt("/dialogs/gridtiler/HorizAlign", 1);
         if (HorizAlign == 0) {
             HorizLeftRadioButton.set_active(TRUE);
         }
@@ -811,7 +814,7 @@ TileDialog::TileDialog()
         YPadSpinner.set_digits(1);
         YPadSpinner.set_increments(0.2, 2);
         YPadSpinner.set_range(-10000, 10000);
-        double YPad = prefs_get_double_attribute ("dialogs.gridtiler", "YPad", 15);
+        double YPad = prefs->getDouble("/dialogs/gridtiler/YPad", 15);
         YPadSpinner.set_value(YPad);
         YPadBox.pack_start(YPadSpinner, true, true, MARGIN);
         tips.set_tip(YPadSpinner, _("Vertical spacing between rows (px units)"));
@@ -836,7 +839,7 @@ TileDialog::TileDialog()
         XPadSpinner.set_digits(1);
         XPadSpinner.set_increments(0.2, 2);
         XPadSpinner.set_range(-10000, 10000);
-        double XPad = prefs_get_double_attribute ("dialogs.gridtiler", "XPad", 15);
+        double XPad = prefs->getDouble("/dialogs/gridtiler/XPad", 15);
         XPadSpinner.set_value(XPad);
         XPadBox.pack_start(XPadSpinner, true, true, MARGIN);
         tips.set_tip(XPadSpinner, _("Horizontal spacing between columns (px units)"));
@@ -851,7 +854,7 @@ TileDialog::TileDialog()
 
     contents->pack_start(TileBox);
 
-    double SpacingType = prefs_get_double_attribute ("dialogs.gridtiler", "SpacingType", 15);
+    double SpacingType = prefs->getDouble("/dialogs/gridtiler/SpacingType", 15);
     if (SpacingType>0) {
         ManualSpacing=true;
     } else {
@@ -868,20 +871,10 @@ TileDialog::TileDialog()
     show_all_children();
 }
 
-
-
-
-
-
 } //namespace Dialog
 } //namespace UI
 } //namespace Inkscape
 
-//#########################################################################
-//## E N D    O F    F I L E
-//#########################################################################
-
-
 /*
   Local Variables:
   mode:c++
@@ -891,7 +884,4 @@ TileDialog::TileDialog()
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
-
-
-
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 ::