summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavmjong Bah <tavmjong@free.fr>2014-12-11 13:31:24 +0000
committertavmjong-free <tavmjong@free.fr>2014-12-11 13:31:24 +0000
commit22b51fa87ea56f6ca9422c84bea18546412d7e9d (patch)
tree3ad238131718e7babac3d77aa75b5de91673ef5c
parentFix build with poppler 0.29.0 (Bug #1399811) (diff)
downloadinkscape-22b51fa87ea56f6ca9422c84bea18546412d7e9d.tar.gz
inkscape-22b51fa87ea56f6ca9422c84bea18546412d7e9d.zip
Change grids to use 'user units'. Grids using absolute units are read in but
may be scaled incorrectly due to change of 90 to 96 dpi. (bzr r13795)
-rw-r--r--src/display/canvas-axonomgrid.cpp57
-rw-r--r--src/display/canvas-grid.cpp136
-rw-r--r--src/ui/widget/registered-widget.cpp29
-rw-r--r--src/ui/widget/registered-widget.h12
4 files changed, 166 insertions, 68 deletions
diff --git a/src/display/canvas-axonomgrid.cpp b/src/display/canvas-axonomgrid.cpp
index 592c962a6..b81400266 100644
--- a/src/display/canvas-axonomgrid.cpp
+++ b/src/display/canvas-axonomgrid.cpp
@@ -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 @@ static gboolean sp_nv_read_opacity(gchar const *str, guint32 *color)
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 @@ CanvasAxonomGrid::readRepr()
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 @@ _wr.setUpdating (true);
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(
diff --git a/src/display/canvas-grid.cpp b/src/display/canvas-grid.cpp
index 2eeaa7006..0bcb6c375 100644
--- a/src/display/canvas-grid.cpp
+++ b/src/display/canvas-grid.cpp
@@ -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"
@@ -367,17 +368,21 @@ bool CanvasGrid::isEnabled() const
return snapper->getEnabled();
}
+// Used to shift origin when page size changed to fit drawing.
void CanvasGrid::setOrigin(Geom::Point const &origin_px)
{
- Inkscape::SVGOStringStream os_x, os_y;
- gdouble val;
+ 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;
+ }
- 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;
+ // Write out in 'user-units'
+ Inkscape::SVGOStringStream os_x, os_y;
+ 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());
}
@@ -504,33 +509,6 @@ static gboolean sp_nv_read_opacity(gchar const *str, guint32 *color)
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.
@@ -560,34 +538,78 @@ static void validateInt(gint oldVal,
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")) ) {
@@ -629,6 +651,10 @@ CanvasXYGrid::readRepr()
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) );
}
@@ -666,13 +692,17 @@ CanvasXYGrid::newSpecificWidget()
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(
diff --git a/src/ui/widget/registered-widget.cpp b/src/ui/widget/registered-widget.cpp
index e97285de4..4065bd550 100644
--- a/src/ui/widget/registered-widget.cpp
+++ b/src/ui/widget/registered-widget.cpp
@@ -36,6 +36,8 @@
// for interruptability bug:
#include "display/sp-canvas.h"
+#include "sp-root.h"
+
namespace Inkscape {
namespace UI {
namespace Widget {
@@ -203,7 +205,7 @@ RegisteredScalarUnit::~RegisteredScalarUnit()
_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<ScalarUnit>(label, tip, UNIT_TYPE_LINEAR, "", "", rum.getUnitMenu()),
_um(0)
{
@@ -215,6 +217,7 @@ RegisteredScalarUnit::RegisteredScalarUnit (const Glib::ustring& label, const Gl
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));
}
@@ -233,12 +236,28 @@ RegisteredScalarUnit::on_value_changed()
_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);
}
diff --git a/src/ui/widget/registered-widget.h b/src/ui/widget/registered-widget.h
index 1f505a3cd..15302a165 100644
--- a/src/ui/widget/registered-widget.h
+++ b/src/ui/widget/registered-widget.h
@@ -206,6 +206,14 @@ protected:
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<ScalarUnit> {
public:
~RegisteredScalarUnit();
@@ -215,12 +223,14 @@ public:
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<Scalar> {