diff --git a/src/sp-guide.cpp b/src/sp-guide.cpp
index ccaf8164949532ca82b2df536344df214928c384..26d60601c58d002ff2c4767f1a35af0341ddb52d 100644 (file)
--- a/src/sp-guide.cpp
+++ b/src/sp-guide.cpp
* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* Peter Moulder <pmoulder@mail.csse.monash.edu.au>
* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* Peter Moulder <pmoulder@mail.csse.monash.edu.au>
+ * Johan Engelen
*
* Copyright (C) 2000-2002 authors
* Copyright (C) 2004 Monash University
*
* Copyright (C) 2000-2002 authors
* Copyright (C) 2004 Monash University
+ * Copyright (C) 2007 Johan Engelen
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#endif
#include "display/guideline.h"
#include "svg/svg.h"
#endif
#include "display/guideline.h"
#include "svg/svg.h"
+#include "svg/stringstream.h"
#include "attributes.h"
#include "sp-guide.h"
#include <sp-item-notify-moveto.h>
#include "attributes.h"
#include "sp-guide.h"
#include <sp-item-notify-moveto.h>
#include <glibmm/i18n.h>
#include <xml/repr.h>
#include <remove-last.h>
#include <glibmm/i18n.h>
#include <xml/repr.h>
#include <remove-last.h>
+#include "sp-metrics.h"
+#include "inkscape.h"
+#include "desktop.h"
+#include "sp-namedview.h"
+#include <2geom/angle.h>
+
using std::vector;
enum {
using std::vector;
enum {
static void sp_guide_init(SPGuide *guide)
{
static void sp_guide_init(SPGuide *guide)
{
- guide->normal = component_vectors[NR::Y];
- /* constrain y coordinate; horizontal line. I doubt it ever matters what we initialize
- this to. */
- guide->position = 0.0;
+ guide->normal_to_line = component_vectors[NR::Y];
+ guide->point_on_line = Geom::Point(0.,0.);
guide->color = 0x0000ff7f;
guide->hicolor = 0xff00007f;
}
guide->color = 0x0000ff7f;
guide->hicolor = 0xff00007f;
}
-static void sp_guide_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
+static void sp_guide_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec */*pspec*/)
{
SPGuide &guide = *SP_GUIDE(object);
switch (prop_id) {
{
SPGuide &guide = *SP_GUIDE(object);
switch (prop_id) {
- case PROP_COLOR:
+ case PROP_COLOR:
guide.color = g_value_get_uint(value);
for (GSList *l = guide.views; l != NULL; l = l->next) {
sp_guideline_set_color(SP_GUIDELINE(l->data), guide.color);
}
break;
guide.color = g_value_get_uint(value);
for (GSList *l = guide.views; l != NULL; l = l->next) {
sp_guideline_set_color(SP_GUIDELINE(l->data), guide.color);
}
break;
- case PROP_HICOLOR:
+ case PROP_HICOLOR:
guide.hicolor = g_value_get_uint(value);
break;
}
}
guide.hicolor = g_value_get_uint(value);
break;
}
}
-static void sp_guide_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
+static void sp_guide_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec */*pspec*/)
{
SPGuide const &guide = *SP_GUIDE(object);
switch (prop_id) {
{
SPGuide const &guide = *SP_GUIDE(object);
switch (prop_id) {
- case PROP_COLOR:
+ case PROP_COLOR:
g_value_set_uint(value, guide.color);
break;
g_value_set_uint(value, guide.color);
break;
- case PROP_HICOLOR:
+ case PROP_HICOLOR:
g_value_set_uint(value, guide.hicolor);
break;
}
g_value_set_uint(value, guide.hicolor);
break;
}
@@ -168,21 +175,59 @@ static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value)
SPGuide *guide = SP_GUIDE(object);
switch (key) {
SPGuide *guide = SP_GUIDE(object);
switch (key) {
- case SP_ATTR_ORIENTATION:
+ case SP_ATTR_ORIENTATION:
+ {
if (value && !strcmp(value, "horizontal")) {
/* Visual representation of a horizontal line, constrain vertically (y coordinate). */
if (value && !strcmp(value, "horizontal")) {
/* Visual representation of a horizontal line, constrain vertically (y coordinate). */
- guide->normal = component_vectors[NR::Y];
+ guide->normal_to_line = component_vectors[NR::Y];
+ } else if (value && !strcmp(value, "vertical")) {
+ guide->normal_to_line = component_vectors[NR::X];
+ } else if (value) {
+ gchar ** strarray = g_strsplit(value, ",", 2);
+ double newx, newy;
+ unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
+ success += sp_svg_number_read_d(strarray[1], &newy);
+ g_strfreev (strarray);
+ if (success == 2) {
+ Geom::Point direction(newx, newy);
+ direction.normalize();
+ guide->normal_to_line = direction;
+ } else {
+ // default to vertical line for bad arguments
+ guide->normal_to_line = component_vectors[NR::X];
+ }
} else {
} else {
- guide->normal = component_vectors[NR::X];
+ // default to vertical line for bad arguments
+ guide->normal_to_line = component_vectors[NR::X];
}
}
- break;
- case SP_ATTR_POSITION:
- sp_svg_number_read_d(value, &guide->position);
+ sp_guide_set_normal(*guide, guide->normal_to_line.to_2geom(), false);
+ }
+ break;
+ case SP_ATTR_POSITION:
+ {
+ gchar ** strarray = g_strsplit(value, ",", 2);
+ double newx, newy;
+ unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
+ success += sp_svg_number_read_d(strarray[1], &newy);
+ g_strfreev (strarray);
+ if (success == 2) {
+ guide->point_on_line = Geom::Point(newx, newy);
+ } else if (success == 1) {
+ // before 0.46 style guideline definition.
+ const gchar *attr = SP_OBJECT_REPR(object)->attribute("orientation");
+ if (attr && !strcmp(attr, "horizontal")) {
+ guide->point_on_line = Geom::Point(0, newx);
+ } else {
+ guide->point_on_line = Geom::Point(newx, 0);
+ }
+ }
+
// update position in non-committing way
// fixme: perhaps we need to add an update method instead, and request_update here
// update position in non-committing way
// fixme: perhaps we need to add an update method instead, and request_update here
- sp_guide_moveto(*guide, guide->position, false);
- break;
- default:
+ sp_guide_moveto(*guide, guide->point_on_line, false);
+ }
+ break;
+ default:
if (((SPObjectClass *) (parent_class))->set) {
((SPObjectClass *) (parent_class))->set(object, key, value);
}
if (((SPObjectClass *) (parent_class))->set) {
((SPObjectClass *) (parent_class))->set(object, key, value);
}
@@ -192,10 +237,7 @@ static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value)
void sp_guide_show(SPGuide *guide, SPCanvasGroup *group, GCallback handler)
{
void sp_guide_show(SPGuide *guide, SPCanvasGroup *group, GCallback handler)
{
- bool const vertical_line_p = ( guide->normal == component_vectors[NR::X] );
- g_assert(( guide->normal == component_vectors[NR::X] ) ||
- ( guide->normal == component_vectors[NR::Y] ) );
- SPCanvasItem *item = sp_guideline_new(group, guide->position, vertical_line_p);
+ SPCanvasItem *item = sp_guideline_new(group, guide->point_on_line, guide->normal_to_line.to_2geom());
sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
g_signal_connect(G_OBJECT(item), "event", G_CALLBACK(handler), guide);
sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
g_signal_connect(G_OBJECT(item), "event", G_CALLBACK(handler), guide);
g_assert_not_reached();
}
g_assert_not_reached();
}
-double sp_guide_position_from_pt(SPGuide const *guide, NR::Point const &pt)
+Geom::Point sp_guide_position_from_pt(SPGuide const *guide, NR::Point const &pt)
+{
+ return -(pt.to_2geom() - guide->point_on_line);
+}
+
+double sp_guide_distance_from_pt(SPGuide const *guide, Geom::Point const &pt)
{
{
- return dot(guide->normal, pt);
+ return dot(pt - guide->point_on_line, guide->normal_to_line);
+}
+
+/**
+ * \arg commit False indicates temporary moveto in response to motion event while dragging,
+ * true indicates a "committing" version: in response to button release event after
+ * dragging a guideline, or clicking OK in guide editing dialog.
+ */
+void sp_guide_moveto(SPGuide const &guide, Geom::Point const point_on_line, bool const commit)
+{
+ g_assert(SP_IS_GUIDE(&guide));
+
+ for (GSList *l = guide.views; l != NULL; l = l->next) {
+ sp_guideline_set_position(SP_GUIDELINE(l->data), point_on_line);
+ }
+
+ /* Calling sp_repr_set_point must precede calling sp_item_notify_moveto in the commit
+ case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
+ if (commit) {
+ sp_repr_set_point(SP_OBJECT(&guide)->repr, "position", point_on_line);
+ }
+
+/* DISABLED CODE BECAUSE SPGuideAttachment IS NOT USE AT THE MOMENT (johan)
+ for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
+ iEnd(guide.attached_items.end());
+ i != iEnd; ++i)
+ {
+ SPGuideAttachment const &att = *i;
+ sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
+ }
+*/
}
/**
* \arg commit False indicates temporary moveto in response to motion event while dragging,
}
/**
* \arg commit False indicates temporary moveto in response to motion event while dragging,
- * true indicates a "committing" version: in response to button release event after
- * dragging a guideline, or clicking OK in guide editing dialog.
+ * true indicates a "committing" version: in response to button release event after
+ * dragging a guideline, or clicking OK in guide editing dialog.
*/
*/
-void sp_guide_moveto(SPGuide const &guide, gdouble const position, bool const commit)
+void sp_guide_set_normal(SPGuide const &guide, Geom::Point const normal_to_line, bool const commit)
{
g_assert(SP_IS_GUIDE(&guide));
for (GSList *l = guide.views; l != NULL; l = l->next) {
{
g_assert(SP_IS_GUIDE(&guide));
for (GSList *l = guide.views; l != NULL; l = l->next) {
- sp_guideline_set_position(SP_GUIDELINE(l->data),
- position);
+ sp_guideline_set_normal(SP_GUIDELINE(l->data), normal_to_line);
}
}
- /* Calling sp_repr_set_svg_double must precede calling sp_item_notify_moveto in the commit
+ /* Calling sp_repr_set_svg_point must precede calling sp_item_notify_moveto in the commit
case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
if (commit) {
case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
if (commit) {
- sp_repr_set_svg_double(SP_OBJECT(&guide)->repr,
- "position", position);
+ sp_repr_set_point(SP_OBJECT(&guide)->repr, "orientation", normal_to_line);
}
}
+/* DISABLED CODE BECAUSE SPGuideAttachment IS NOT USE AT THE MOMENT (johan)
for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
iEnd(guide.attached_items.end());
i != iEnd; ++i)
for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
iEnd(guide.attached_items.end());
i != iEnd; ++i)
@@ -271,6 +347,7 @@ void sp_guide_moveto(SPGuide const &guide, gdouble const position, bool const co
SPGuideAttachment const &att = *i;
sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
}
SPGuideAttachment const &att = *i;
sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
}
+*/
}
/**
}
/**
{
using NR::X;
using NR::Y;
{
using NR::X;
using NR::Y;
-
- if ( guide->normal == component_vectors[X] ) {
- return g_strdup(_("vertical guideline"));
- } else if ( guide->normal == component_vectors[Y] ) {
- return g_strdup(_("horizontal guideline"));
+
+ GString *position_string_x = SP_PX_TO_METRIC_STRING(guide->point_on_line[X], SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
+ GString *position_string_y = SP_PX_TO_METRIC_STRING(guide->point_on_line[Y], SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
+
+ if ( guide->normal_to_line == component_vectors[X] ) {
+ return g_strdup_printf(_("vertical guideline at %s"), position_string_x->str);
+ } else if ( guide->normal_to_line == component_vectors[Y] ) {
+ return g_strdup_printf(_("horizontal guideline at %s"), position_string_y->str);
} else {
} else {
- double const radians = atan2(guide->normal[X],
- guide->normal[Y]);
- /* flip y axis and rotate 90 degrees to convert to line angle */
- double const degrees = ( radians / M_PI ) * 180.0;
- int const degrees_int = (int) floor( degrees + .5 );
- return g_strdup_printf("%d degree guideline", degrees_int);
+ double const radians = guide->angle();
+ double const degrees = Geom::rad_to_deg(radians);
+ int const degrees_int = (int) round(degrees);
+ return g_strdup_printf("%d degree guideline at (%s,%s)", degrees_int, position_string_x->str, position_string_y->str);
/* Alternative suggestion: "angled guideline". */
}
/* Alternative suggestion: "angled guideline". */
}
+
+ g_string_free(position_string_x, TRUE);
+ g_string_free(position_string_y, TRUE);
}
void sp_guide_remove(SPGuide *guide)
}
void sp_guide_remove(SPGuide *guide)