summaryrefslogtreecommitdiffstats
path: root/src/document.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/document.cpp')
-rw-r--r--src/document.cpp141
1 files changed, 94 insertions, 47 deletions
diff --git a/src/document.cpp b/src/document.cpp
index e5b827937..2caefb4ed 100644
--- a/src/document.cpp
+++ b/src/document.cpp
@@ -50,7 +50,7 @@
#include "document-private.h"
#include "document-undo.h"
#include "id-clash.h"
-#include "inkscape-private.h"
+#include "inkscape.h"
#include "inkscape-version.h"
#include "libavoid/router.h"
#include "persp3d.h"
@@ -154,6 +154,14 @@ SPDocument::~SPDocument() {
router = NULL;
}
+ if (oldSignalsConnected) {
+ priv->selChangeConnection.disconnect();
+ priv->desktopActivatedConnection.disconnect();
+ } else {
+ _selection_changed_connection.disconnect();
+ _desktop_activated_connection.disconnect();
+ }
+
if (priv) {
if (priv->partial) {
sp_repr_free_log(priv->partial);
@@ -208,17 +216,8 @@ SPDocument::~SPDocument() {
rerouting_handler_id = 0;
}
- if (oldSignalsConnected) {
- g_signal_handlers_disconnect_by_func(G_OBJECT(INKSCAPE),
- reinterpret_cast<gpointer>(DocumentUndo::resetKey),
- static_cast<gpointer>(this));
- } else {
- _selection_changed_connection.disconnect();
- _desktop_activated_connection.disconnect();
- }
-
if (keepalive) {
- inkscape_unref();
+ inkscape_unref(INKSCAPE);
keepalive = FALSE;
}
@@ -447,7 +446,7 @@ SPDocument *SPDocument::createDoc(Inkscape::XML::Document *rdoc,
rdf_set_defaults( document );
if (keepalive) {
- inkscape_ref();
+ inkscape_ref(INKSCAPE);
}
// Check if the document already has a perspective (e.g., when opening an existing
@@ -462,10 +461,14 @@ SPDocument *SPDocument::createDoc(Inkscape::XML::Document *rdoc,
DocumentUndo::setUndoSensitive(document, true);
// reset undo key when selection changes, so that same-key actions on different objects are not coalesced
- g_signal_connect(G_OBJECT(INKSCAPE), "change_selection",
- G_CALLBACK(DocumentUndo::resetKey), document);
- g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop",
- G_CALLBACK(DocumentUndo::resetKey), document);
+ document->priv->selChangeConnection = INKSCAPE.signal_selection_changed.connect(
+ sigc::hide(sigc::bind(
+ sigc::ptr_fun(&DocumentUndo::resetKey), document)
+ ));
+ document->priv->desktopActivatedConnection = INKSCAPE.signal_activate_desktop.connect(
+ sigc::hide(sigc::bind(
+ sigc::ptr_fun(&DocumentUndo::resetKey), document)
+ ));
document->oldSignalsConnected = true;
return document;
@@ -593,10 +596,67 @@ SPDocument *SPDocument::doUnref()
}
/// guaranteed not to return nullptr
-Inkscape::Util::Unit const* SPDocument::getDefaultUnit() const
+Inkscape::Util::Unit const* SPDocument::getDisplayUnit() const
+{
+ SPNamedView const* nv = sp_document_namedview(this, NULL);
+ return nv ? nv->getDisplayUnit() : unit_table.getUnit("px");
+}
+
+/// guaranteed not to return nullptr
+// returns 'px' units as default, like legacy Inkscape
+// THIS SHOULD NOT BE USED... INSTEAD USE DOCUMENT SCALE
+Inkscape::Util::Unit const& SPDocument::getSVGUnit() const
{
SPNamedView const* nv = sp_document_namedview(this, NULL);
- return nv ? nv->getDefaultUnit() : unit_table.getUnit("pt");
+ return nv ? nv->getSVGUnit() : *unit_table.getUnit("px");
+}
+
+/// Returns document scale as defined by width/height and viewBox (real world to user-units).
+Geom::Scale SPDocument::getDocumentScale() const
+{
+ Geom::Scale scale;
+ if( root->viewBox_set ) {
+ double scale_x = 1.0;
+ double scale_y = 1.0;
+ if( root->viewBox.width() > 0.0 ) {
+ scale_x = root->width.computed / root->viewBox.width();
+ }
+ if( root->viewBox.height() > 0.0 ) {
+ scale_y = root->height.computed / root->viewBox.height();
+ }
+ scale = Geom::Scale(scale_x, scale_y);
+ }
+ // std::cout << "SPDocument::getDocumentScale():\n" << scale << std::endl;
+ return scale;
+}
+
+// Avoid calling root->updateRepr() twice by combining setting width and height.
+// (As done on every delete as clipboard calls this via fitToRect(). Also called in page-sizer.cpp)
+void SPDocument::setWidthAndHeight(const Inkscape::Util::Quantity &width, const Inkscape::Util::Quantity &height, bool changeSize)
+{
+ Inkscape::Util::Unit const *old_width_units = unit_table.getUnit("px");
+ if (root->width.unit)
+ old_width_units = unit_table.getUnit(root->width.unit);
+ gdouble old_width_converted = Inkscape::Util::Quantity::convert(root->width.value, old_width_units, width.unit);
+
+ root->width.computed = width.value("px");
+ root->width.value = width.quantity;
+ root->width.unit = (SVGLength::Unit) width.unit->svgUnit();
+
+ Inkscape::Util::Unit const *old_height_units = unit_table.getUnit("px");
+ if (root->height.unit)
+ old_height_units = unit_table.getUnit(root->height.unit);
+ gdouble old_height_converted = Inkscape::Util::Quantity::convert(root->height.value, old_height_units, height.unit);
+
+ root->height.computed = height.value("px");
+ root->height.value = height.quantity;
+ root->height.unit = (SVGLength::Unit) height.unit->svgUnit();
+
+ if (root->viewBox_set && changeSize)
+ root->viewBox.setMax(Geom::Point(
+ root->viewBox.left() + (root->width.value / old_width_converted ) * root->viewBox.width(),
+ root->viewBox.top() + (root->height.value / old_height_converted) * root->viewBox.height()));
+ root->updateRepr();
}
Inkscape::Util::Quantity SPDocument::getWidth() const
@@ -616,24 +676,18 @@ Inkscape::Util::Quantity SPDocument::getWidth() const
return Inkscape::Util::Quantity(result, unit_table.getUnit(u));
}
-void SPDocument::setWidth(const Inkscape::Util::Quantity &width)
+void SPDocument::setWidth(const Inkscape::Util::Quantity &width, bool changeSize)
{
Inkscape::Util::Unit const *old_units = unit_table.getUnit("px");
if (root->width.unit)
old_units = unit_table.getUnit(root->width.unit);
gdouble old_converted = Inkscape::Util::Quantity::convert(root->width.value, old_units, width.unit);
+
root->width.computed = width.value("px");
- /* SVG does not support meters as a unit, so we must translate meters to
- * cm when writing */
- if (*width.unit == *unit_table.getUnit("m")) {
- root->width.value = width.value("cm");
- root->width.unit = SVGLength::CM;
- } else {
- root->width.value = width.quantity;
- root->width.unit = (SVGLength::Unit) width.unit->svgUnit();
- }
+ root->width.value = width.quantity;
+ root->width.unit = (SVGLength::Unit) width.unit->svgUnit();
- if (root->viewBox_set)
+ if (root->viewBox_set && changeSize)
root->viewBox.setMax(Geom::Point(root->viewBox.left() + (root->width.value / old_converted) * root->viewBox.width(), root->viewBox.bottom()));
root->updateRepr();
@@ -657,24 +711,18 @@ Inkscape::Util::Quantity SPDocument::getHeight() const
return Inkscape::Util::Quantity(result, unit_table.getUnit(u));
}
-void SPDocument::setHeight(const Inkscape::Util::Quantity &height)
+void SPDocument::setHeight(const Inkscape::Util::Quantity &height, bool changeSize)
{
Inkscape::Util::Unit const *old_units = unit_table.getUnit("px");
if (root->height.unit)
old_units = unit_table.getUnit(root->height.unit);
gdouble old_converted = Inkscape::Util::Quantity::convert(root->height.value, old_units, height.unit);
+
root->height.computed = height.value("px");
- /* SVG does not support meters as a unit, so we must translate meters to
- * cm when writing */
- if (*height.unit == *unit_table.getUnit("m")) {
- root->height.value = height.value("cm");
- root->height.unit = SVGLength::CM;
- } else {
- root->height.value = height.quantity;
- root->height.unit = (SVGLength::Unit) height.unit->svgUnit();
- }
+ root->height.value = height.quantity;
+ root->height.unit = (SVGLength::Unit) height.unit->svgUnit();
- if (root->viewBox_set)
+ if (root->viewBox_set && changeSize)
root->viewBox.setMax(Geom::Point(root->viewBox.right(), root->viewBox.top() + (root->height.value / old_converted) * root->viewBox.height()));
root->updateRepr();
@@ -713,11 +761,9 @@ void SPDocument::fitToRect(Geom::Rect const &rect, bool with_margins)
double const old_height = getHeight().value("px");
Inkscape::Util::Unit const *nv_units = unit_table.getUnit("px");
+ if (root->height.unit)
+ nv_units = unit_table.getUnit(root->height.unit);
SPNamedView *nv = sp_document_namedview(this, NULL);
- if (nv != NULL) {
- if (nv->getAttribute("units"))
- nv_units = unit_table.getUnit(nv->getAttribute("units"));
- }
/* in px */
double margin_top = 0.0;
@@ -742,9 +788,10 @@ void SPDocument::fitToRect(Geom::Rect const &rect, bool with_margins)
rect.min() - Geom::Point(margin_left, margin_bottom),
rect.max() + Geom::Point(margin_right, margin_top));
-
- setWidth(Inkscape::Util::Quantity(Inkscape::Util::Quantity::convert(rect_with_margins.width(), "px", nv_units), nv_units));
- setHeight(Inkscape::Util::Quantity(Inkscape::Util::Quantity::convert(rect_with_margins.height(), "px", nv_units), nv_units));
+ setWidthAndHeight(
+ Inkscape::Util::Quantity(Inkscape::Util::Quantity::convert(rect_with_margins.width(), "px", nv_units), nv_units),
+ Inkscape::Util::Quantity(Inkscape::Util::Quantity::convert(rect_with_margins.height(), "px", nv_units), nv_units)
+ );
Geom::Translate const tr(
Geom::Point(0, old_height - rect_with_margins.height())