=== modified file 'src/display/canvas-axonomgrid.cpp' --- src/display/canvas-axonomgrid.cpp 2014-08-17 00:23:01 +0000 +++ src/display/canvas-axonomgrid.cpp 2014-12-11 13:09:00 +0000 @@ -46,6 +46,7 @@ #include "preferences.h" #include "sp-namedview.h" #include "sp-object.h" +#include "sp-root.h" #include "svg/svg-color.h" #include "2geom/line.h" #include "2geom/angle.h" @@ -213,22 +214,53 @@ void CanvasAxonomGrid::readRepr() { + SPRoot *root = doc->getRoot(); + double scale_x = 1.0; + double scale_y = 1.0; + if( root->viewBox_set ) { + scale_x = root->width.computed / root->viewBox.width(); + scale_y = root->height.computed / root->viewBox.height(); + } + gchar const *value; + if ( (value = repr->attribute("originx")) ) { + Inkscape::Util::Quantity q = unit_table.parseQuantity(value); - gridunit = q.unit; - origin[Geom::X] = q.value("px"); + + if( q.unit->type == UNIT_TYPE_LINEAR ) { + // Legacy grid not in 'user units' + origin[Geom::X] = q.value("px"); + } else { + // Grid in 'user units' + origin[Geom::X] = q.quantity * scale_x; + } } + if ( (value = repr->attribute("originy")) ) { + Inkscape::Util::Quantity q = unit_table.parseQuantity(value); - gridunit = q.unit; - origin[Geom::Y] = q.value("px"); + + if( q.unit->type == UNIT_TYPE_LINEAR ) { + // Legacy grid not in 'user units' + origin[Geom::Y] = q.value("px"); + } else { + // Grid in 'user units' + origin[Geom::Y] = q.quantity * scale_y; + } } if ( (value = repr->attribute("spacingy")) ) { + Inkscape::Util::Quantity q = unit_table.parseQuantity(value); - gridunit = q.unit; - lengthy = q.value("px"); + + if( q.unit->type == UNIT_TYPE_LINEAR ) { + // Legacy grid not in 'user units' + lengthy = q.value("px"); + } else { + // Grid in 'user units' + lengthy = q.quantity * scale_y; // We do not handle scale_x != scale_y + } if (lengthy < 0.0500) lengthy = 0.0500; } @@ -281,6 +313,10 @@ snapper->setSnapVisibleOnly(strcmp(value,"false") != 0 && strcmp(value, "0") != 0); } + if ( (value = repr->attribute("units")) ) { + gridunit = unit_table.getUnit(value); // Display unit identifier in grid menu + } + for (GSList *l = canvasitems; l != NULL; l = l->next) { sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) ); } @@ -316,11 +352,14 @@ Inkscape::UI::Widget::RegisteredUnitMenu *_rumg = Gtk::manage( new Inkscape::UI::Widget::RegisteredUnitMenu( _("Grid _units:"), "units", _wr, repr, doc) ); Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_ox = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit( - _("_Origin X:"), _("X coordinate of grid origin"), "originx", *_rumg, _wr, repr, doc) ); + _("_Origin X:"), _("X coordinate of grid origin"), "originx", + *_rumg, _wr, repr, doc, Inkscape::UI::Widget::RSU_x) ); Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_oy = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit( - _("O_rigin Y:"), _("Y coordinate of grid origin"), "originy", *_rumg, _wr, repr, doc) ); + _("O_rigin Y:"), _("Y coordinate of grid origin"), "originy", + *_rumg, _wr, repr, doc, Inkscape::UI::Widget::RSU_y) ); Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_sy = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit( - _("Spacing _Y:"), _("Base length of z-axis"), "spacingy", *_rumg, _wr, repr, doc) ); + _("Spacing _Y:"), _("Base length of z-axis"), "spacingy", + *_rumg, _wr, repr, doc, Inkscape::UI::Widget::RSU_y) ); Inkscape::UI::Widget::RegisteredScalar *_rsu_ax = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalar( _("Angle X:"), _("Angle of x-axis"), "gridanglex", _wr, repr, doc ) ); Inkscape::UI::Widget::RegisteredScalar *_rsu_az = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalar( === modified file 'src/display/canvas-grid.cpp' --- src/display/canvas-grid.cpp 2014-03-30 21:43:02 +0000 +++ src/display/canvas-grid.cpp 2014-12-11 13:09:53 +0000 @@ -47,6 +47,7 @@ #include "preferences.h" #include "sp-namedview.h" #include "sp-object.h" +#include "sp-root.h" #include "svg/svg-color.h" #include "svg/stringstream.h" #include "util/mathfns.h" @@ -393,17 +394,21 @@ return snapper->getEnabled(); } +// Used to shift origin when page size changed to fit drawing. void CanvasGrid::setOrigin(Geom::Point const &origin_px) { + SPRoot *root = doc->getRoot(); + double scale_x = 1.0; + double scale_y = 1.0; + if( root->viewBox_set ) { + scale_x = root->viewBox.width() / root->width.computed; + scale_y = root->viewBox.height() / root->height.computed; + } + + // Write out in 'user-units' Inkscape::SVGOStringStream os_x, os_y; - gdouble val; - - val = origin_px[Geom::X]; - val = Inkscape::Util::Quantity::convert(val, "px", gridunit); - os_x << val << gridunit->abbr; - val = origin_px[Geom::Y]; - val = Inkscape::Util::Quantity::convert(val, "px", gridunit); - os_y << val << gridunit->abbr; + os_x << origin_px[Geom::X] * scale_x; + os_y << origin_px[Geom::Y] * scale_y; repr->setAttribute("originx", os_x.str().c_str()); repr->setAttribute("originy", os_y.str().c_str()); } @@ -530,33 +535,6 @@ return TRUE; } -/** If the passed scalar is invalid (<=0), then set the widget and the scalar - to use the given old value. - - @param oldVal Old value to use if the new one is invalid. - @param pTarget The scalar to validate. - @param widget Widget associated with the scalar. -*/ -static void validateScalar(double oldVal, - double* pTarget) -{ - // Avoid nullness. - if ( pTarget == NULL ) - return; - - // Invalid new value? - if ( *pTarget <= 0 ) { - // If the old value is somehow invalid as well, then default to 1. - if ( oldVal <= 0 ) - oldVal = 1; - - // Reset the scalar and associated widget to the old value. - *pTarget = oldVal; - } //if - -} //validateScalar - - /** If the passed int is invalid (<=0), then set the widget and the int to use the given old value. @@ -586,34 +564,78 @@ void CanvasXYGrid::readRepr() { + SPRoot *root = doc->getRoot(); + double scale_x = 1.0; + double scale_y = 1.0; + if( root->viewBox_set ) { + scale_x = root->width.computed / root->viewBox.width(); + scale_y = root->height.computed / root->viewBox.height(); + } + gchar const *value; + if ( (value = repr->attribute("originx")) ) { + Inkscape::Util::Quantity q = unit_table.parseQuantity(value); - gridunit = q.unit; - origin[Geom::X] = q.value("px"); + + if( q.unit->type == UNIT_TYPE_LINEAR ) { + // Legacy grid not in 'user units' + origin[Geom::X] = q.value("px"); + } else { + // Grid in 'user units' + origin[Geom::X] = q.quantity * scale_x; + } } if ( (value = repr->attribute("originy")) ) { + Inkscape::Util::Quantity q = unit_table.parseQuantity(value); - gridunit = q.unit; - origin[Geom::Y] = q.value("px"); + + if( q.unit->type == UNIT_TYPE_LINEAR ) { + // Legacy grid not in 'user units' + origin[Geom::Y] = q.value("px"); + } else { + // Grid in 'user units' + origin[Geom::Y] = q.quantity * scale_y; + } } if ( (value = repr->attribute("spacingx")) ) { - double oldVal = spacing[Geom::X]; + + // Ensure a valid default value + if( spacing[Geom::X] <= 0.0 ) + spacing[Geom::X] = 1.0; + Inkscape::Util::Quantity q = unit_table.parseQuantity(value); - gridunit = q.unit; - spacing[Geom::X] = q.quantity; - validateScalar(oldVal, &spacing[Geom::X]); - spacing[Geom::X] = Inkscape::Util::Quantity::convert(spacing[Geom::X], gridunit, "px"); + // Ensure a valid new value + if( q.quantity > 0 ) { + if( q.unit->type == UNIT_TYPE_LINEAR ) { + // Legacy grid not in 'user units' + spacing[Geom::X] = q.value("px"); + } else { + // Grid in 'user units' + spacing[Geom::X] = q.quantity * scale_x; + } + } } + if ( (value = repr->attribute("spacingy")) ) { - double oldVal = spacing[Geom::Y]; + + // Ensure a valid default value + if( spacing[Geom::Y] <= 0.0 ) + spacing[Geom::Y] = 1.0; + Inkscape::Util::Quantity q = unit_table.parseQuantity(value); - gridunit = q.unit; - spacing[Geom::Y] = q.quantity; - validateScalar(oldVal, &spacing[Geom::Y]); - spacing[Geom::Y] = Inkscape::Util::Quantity::convert(spacing[Geom::Y], gridunit, "px"); + // Ensure a valid new value + if( q.quantity > 0 ) { + if( q.unit->type == UNIT_TYPE_LINEAR ) { + // Legacy grid not in 'user units' + spacing[Geom::Y] = q.value("px"); + } else { + // Grid in 'user units' + spacing[Geom::Y] = q.quantity * scale_y; + } + } } if ( (value = repr->attribute("color")) ) { @@ -655,6 +677,10 @@ snapper->setSnapVisibleOnly(strcmp(value,"false") != 0 && strcmp(value, "0") != 0); } + if ( (value = repr->attribute("units")) ) { + gridunit = unit_table.getUnit(value); // Display unit identifier in grid menu + } + for (GSList *l = canvasitems; l != NULL; l = l->next) { sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) ); } @@ -692,13 +718,17 @@ Inkscape::UI::Widget::RegisteredUnitMenu *_rumg = Gtk::manage( new Inkscape::UI::Widget::RegisteredUnitMenu( _("Grid _units:"), "units", _wr, repr, doc) ); Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_ox = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit( - _("_Origin X:"), _("X coordinate of grid origin"), "originx", *_rumg, _wr, repr, doc) ); + _("_Origin X:"), _("X coordinate of grid origin"), "originx", + *_rumg, _wr, repr, doc, Inkscape::UI::Widget::RSU_x) ); Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_oy = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit( - _("O_rigin Y:"), _("Y coordinate of grid origin"), "originy", *_rumg, _wr, repr, doc) ); + _("O_rigin Y:"), _("Y coordinate of grid origin"), "originy", + *_rumg, _wr, repr, doc, Inkscape::UI::Widget::RSU_y) ); Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_sx = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit( - _("Spacing _X:"), _("Distance between vertical grid lines"), "spacingx", *_rumg, _wr, repr, doc) ); + _("Spacing _X:"), _("Distance between vertical grid lines"), "spacingx", + *_rumg, _wr, repr, doc, Inkscape::UI::Widget::RSU_x) ); Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_sy = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit( - _("Spacing _Y:"), _("Distance between horizontal grid lines"), "spacingy", *_rumg, _wr, repr, doc) ); + _("Spacing _Y:"), _("Distance between horizontal grid lines"), "spacingy", + *_rumg, _wr, repr, doc, Inkscape::UI::Widget::RSU_y) ); Inkscape::UI::Widget::RegisteredColorPicker *_rcp_gcol = Gtk::manage( new Inkscape::UI::Widget::RegisteredColorPicker( === modified file 'src/ui/widget/registered-widget.cpp' --- src/ui/widget/registered-widget.cpp 2014-03-30 21:43:02 +0000 +++ src/ui/widget/registered-widget.cpp 2014-12-11 10:09:36 +0000 @@ -36,6 +36,8 @@ // for interruptability bug: #include "display/sp-canvas.h" +#include "sp-root.h" + namespace Inkscape { namespace UI { namespace Widget { @@ -150,7 +152,7 @@ _value_changed_connection.disconnect(); } -RegisteredScalarUnit::RegisteredScalarUnit (const Glib::ustring& label, const Glib::ustring& tip, const Glib::ustring& key, const RegisteredUnitMenu &rum, Registry& wr, Inkscape::XML::Node* repr_in, SPDocument *doc_in) +RegisteredScalarUnit::RegisteredScalarUnit (const Glib::ustring& label, const Glib::ustring& tip, const Glib::ustring& key, const RegisteredUnitMenu &rum, Registry& wr, Inkscape::XML::Node* repr_in, SPDocument *doc_in, RSU_UserUnits user_units) : RegisteredWidget(label, tip, UNIT_TYPE_LINEAR, "", "", rum.getUnitMenu()), _um(0) { @@ -162,6 +164,7 @@ setUnit (rum.getUnitMenu()->getUnitAbbr()); setDigits (2); _um = rum.getUnitMenu(); + _user_units = user_units; _value_changed_connection = signal_value_changed().connect (sigc::mem_fun (*this, &RegisteredScalarUnit::on_value_changed)); } @@ -180,12 +183,28 @@ _wr->setUpdating (true); Inkscape::SVGOStringStream os; - os << getValue(""); - if (_um) - os << _um->getUnitAbbr(); + if (_user_units != RSU_none) { + // Output length in 'user units', taking into account scale in 'x' or 'y'. + double scale = 1.0; + if (doc) { + SPRoot *root = doc->getRoot(); + if (root->viewBox_set) { + if (_user_units == RSU_x) { + scale = root->viewBox.width() / root->width.computed; + } else { + scale = root->viewBox.height() / root->height.computed; + } + } + } + os << getValue("px") * scale; + } else { + // Output using unit identifiers. + os << getValue(""); + if (_um) + os << _um->getUnitAbbr(); + } write_to_xml(os.str().c_str()); - _wr->setUpdating (false); } === modified file 'src/ui/widget/registered-widget.h' --- src/ui/widget/registered-widget.h 2014-03-27 01:33:44 +0000 +++ src/ui/widget/registered-widget.h 2014-12-11 10:09:00 +0000 @@ -181,6 +181,14 @@ void on_changed(); }; +// Allow RegisteredScalarUnit to output lengths in 'user units' (which may have direction dependent +// scale factors). +enum RSU_UserUnits { + RSU_none, + RSU_x, + RSU_y +}; + class RegisteredScalarUnit : public RegisteredWidget { public: ~RegisteredScalarUnit(); @@ -190,12 +198,14 @@ const RegisteredUnitMenu &rum, Registry& wr, Inkscape::XML::Node* repr_in = NULL, - SPDocument *doc_in = NULL ); + SPDocument *doc_in = NULL, + RSU_UserUnits _user_units = RSU_none ); protected: sigc::connection _value_changed_connection; UnitMenu *_um; void on_value_changed(); + RSU_UserUnits _user_units; }; class RegisteredScalar : public RegisteredWidget {