1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/* Dia -- an diagram creation/manipulation program
* Copyright (C) 1998 Alexander Larsson
*
* measure.c -- shape to measure distance or angle
* Copyright (C) 2008 Hans Breuer, <Hans@Breuer.Org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include "object.h"
#include "diarenderer.h"
#include "attributes.h"
#include "properties.h"
#include "boundingbox.h"
#include "connection.h"
#include "units.h"
#include "intl.h"
#include "pixmaps/measure.xpm"
/* Object definition */
typedef struct _Measure {
Connection connection;
DiaFont *font;
real font_height;
Color line_color;
real line_width;
real scale;
DiaUnit unit;
int precision;
/* caclculated data */
char *name; /* the calculated measurment */
Point text_pos;
} Measure;
/* Type definition */
static DiaObject *measure_create (Point *startpoint,
void *user_data,
Handle **handle1,
Handle **handle2);
static DiaObject *
measure_load(ObjectNode obj_node, int version,DiaContext *ctx);
static ObjectTypeOps measure_type_ops =
{
(CreateFunc)measure_create,
(LoadFunc) measure_load,
(SaveFunc) object_save_using_properties, /* measure_save */
(GetDefaultsFunc) NULL,
(ApplyDefaultsFunc) NULL
};
DiaObjectType measure_type =
{
"Misc - Measure", /* name */
0, /* version */
measure_xpm, /* pixmap */
&measure_type_ops, /* ops */
NULL, /* pixmap_file */
0 /* default_user_data */
};
/* make accesible from the outside for type regristation */
DiaObjectType *_measure_type = (DiaObjectType *) &measure_type;
/* Class definition */
static ObjectChange* measure_move_handle (Measure *measure,
Handle *handle,
Point *to, ConnectionPoint *cp,
HandleMoveReason reason, ModifierKeys modifiers);
static ObjectChange* measure_move (Measure *measure, Point *to);
static void measure_select(Measure *measure, Point *clicked_point,
DiaRenderer *interactive_renderer);
static void measure_draw(Measure *measure, DiaRenderer *renderer);
static real measure_distance_from (Measure *measure, Point *point);
static void measure_update_data (Measure *measure);
static void measure_destroy (Measure *measure);
static DiaMenu *measure_get_object_menu(Measure *measure,
Point *clickedpoint);
static const PropDescription *measure_describe_props(Measure *measure);
static void measure_get_props(Measure *measure, GPtrArray *props);
static void measure_set_props(Measure *measure, GPtrArray *props);
static ObjectOps measure_ops = {
(DestroyFunc) measure_destroy,
(DrawFunc) measure_draw,
(DistanceFunc) measure_distance_from,
(SelectFunc) measure_select,
(CopyFunc) object_copy_using_properties,
(MoveFunc) measure_move,
(MoveHandleFunc) measure_move_handle,
(GetPropertiesFunc) object_create_props_dialog,
(ApplyPropertiesDialogFunc) object_apply_props_from_dialog,
(ObjectMenuFunc) measure_get_object_menu,
(DescribePropsFunc) measure_describe_props,
(GetPropsFunc) measure_get_props,
(SetPropsFunc) measure_set_props,
(TextEditFunc) 0,
(ApplyPropertiesListFunc) object_apply_props,
};
/* Type implementation */
/*! Factory function - create default object */
static DiaObject *
measure_create (Point *startpoint,
void *user_data,
Handle **handle1,
Handle **handle2)
{
Measure *measure;
Connection *conn;
DiaObject *obj;
Point defaultlen = { 1.0, 1.0 };
measure = g_new0 (Measure,1);
obj = &measure->connection.object;
obj->type = &measure_type;
obj->ops = &measure_ops;
conn = &measure->connection;
conn->endpoints[0] = *startpoint;
conn->endpoints[1] = *startpoint;
point_add(&conn->endpoints[1], &defaultlen);
connection_init(conn, 2, 0);
/* kind of measurement can only be set via user data */
attributes_get_default_font (&measure->font, &measure->font_height);
measure->line_width = attributes_get_default_linewidth();
measure->line_color = attributes_get_foreground();
measure->scale = 1.0;
measure->unit = DIA_UNIT_CENTIMETER;
measure->precision = 3;
measure_update_data(measure);
*handle1 = obj->handles[0];
*handle2 = obj->handles[1];
return obj;
}
static DiaObject *
measure_load(ObjectNode obj_node, int version,DiaContext *ctx)
{
return object_load_using_properties(&measure_type,
obj_node,version,ctx);
}
static PropNumData scale_range = {1e-9, 1e9, 1 };
static PropEnumData unit_data[] = {
{ NC_("length unit", "cm"), DIA_UNIT_CENTIMETER },
{ NC_("length unit", "dm"), DIA_UNIT_DECIMETER },
{ NC_("length unit", "ft"), DIA_UNIT_FEET },
{ NC_("length unit", "in"), DIA_UNIT_INCH },
{ NC_("length unit", "m"), DIA_UNIT_METER },
{ NC_("length unit", "mm"), DIA_UNIT_MILLIMETER },
{ NC_("length unit", "pt"), DIA_UNIT_POINT },
{ NC_("length unit", "pi"), DIA_UNIT_PICA },
{ NULL, }
};
static PropNumData precision_data = {1, 9, 1};
/* Class/Object implementation */
static PropDescription measure_props[] = {
CONNECTION_COMMON_PROPERTIES,
{ "name", PROP_TYPE_STRING,/*PROP_FLAG_VISIBLE|*/PROP_FLAG_DONT_MERGE,
N_("Measurement"),NULL }, /* FIXME: mark read-only */
{ "scale", PROP_TYPE_REAL, PROP_FLAG_VISIBLE, N_("Scale"), NULL, &scale_range},
{ "unit", PROP_TYPE_ENUM, PROP_FLAG_VISIBLE, N_("Unit"), NULL, &unit_data },
{ "precision", PROP_TYPE_INT, PROP_FLAG_VISIBLE, N_("Precision"), NULL, &precision_data },
/* the default PROP_STD_TEXT_FONT has PROP_FLAG_DONT_SAVE, we need saving */
PROP_STD_TEXT_FONT_OPTIONS(PROP_FLAG_VISIBLE),
PROP_STD_TEXT_HEIGHT_OPTIONS(PROP_FLAG_VISIBLE),
PROP_STD_LINE_WIDTH,
PROP_STD_LINE_COLOUR,
PROP_DESC_END
};
static PropOffset measure_offsets[] = {
CONNECTION_COMMON_PROPERTIES_OFFSETS,
{ "name", PROP_TYPE_STRING, offsetof(Measure, name) },
{ "scale", PROP_TYPE_REAL, offsetof(Measure, scale) },
{ "unit", PROP_TYPE_ENUM, offsetof(Measure, unit) },
{ "precision", PROP_TYPE_INT, offsetof(Measure, precision) },
{ "text_font",PROP_TYPE_FONT,offsetof(Measure, font) },
{ PROP_STDNAME_TEXT_HEIGHT,PROP_STDTYPE_TEXT_HEIGHT,offsetof(Measure, font_height) },
{ PROP_STDNAME_LINE_WIDTH, PROP_STDTYPE_LINE_WIDTH, offsetof(Measure, line_width) },
{ "line_colour", PROP_TYPE_COLOUR, offsetof(Measure, line_color) },
{ NULL, 0, 0 }
};
#define MEASURE_ARROW(measure) { ARROW_FILLED_TRIANGLE, measure->font_height, measure->font_height/2 }
/*! Not in the object interface but very important anyway. Used to recalculate the object data after a change */
static void
measure_update_data (Measure *measure)
{
Connection *conn = &measure->connection;
DiaObject *obj = &measure->connection.object;
real value;
Point *ends = measure->connection.endpoints;
LineBBExtras *extra = &conn->extra_spacing;
Rectangle bbox;
Arrow arrow = MEASURE_ARROW(measure);
real ascent, width, theta;
g_return_if_fail (obj->handles != NULL);
connection_update_handles(conn);
extra->start_trans =
extra->end_trans =
extra->start_long =
extra->end_long = (measure->line_width / 2.0);
g_free (measure->name);
value = distance_point_point (&ends[0], &ends[1]);
value *= measure->scale;
value *= (28.346457 / units[measure->unit].factor);
measure->name = g_strdup_printf ("%.*g %s", measure->precision, value, units[measure->unit].unit);
ascent = dia_font_ascent (measure->name, measure->font, measure->font_height);
width = dia_font_string_width (measure->name, measure->font, measure->font_height);
theta = atan2(ends[1].y-ends[0].y, ends[1].x-ends[0].x);
theta = (0 >= theta ? theta + M_PI : theta);
if (theta >= M_PI * 3/4) {
measure->text_pos.x = (ends[0].x + ends[1].x) / 2 - sin(theta) * measure->font_height/2 - width * (2.5 - 2/M_PI * ( theta - 3/4 * M_PI ));
measure->text_pos.y = (ends[0].y + ends[1].y) / 2 + cos(theta) * measure->font_height/2;
} else {
measure->text_pos.x = (ends[0].x + ends[1].x) / 2 + sin(theta) * measure->font_height/2;
measure->text_pos.y = (ends[0].y + ends[1].y) / 2 - cos(theta) * measure->font_height/2;
}
line_bbox (&ends[0], &ends[0], &conn->extra_spacing,&conn->object.bounding_box);
arrow_bbox (&arrow, measure->line_width, &ends[0], &ends[1], &bbox);
rectangle_union(&obj->bounding_box, &bbox);
arrow_bbox (&arrow, measure->line_width, &ends[1], &ends[0], &bbox);
rectangle_union(&obj->bounding_box, &bbox);
bbox.left = measure->text_pos.x;
bbox.top = measure->text_pos.y - ascent;
bbox.bottom = bbox.top + measure->font_height;
bbox.right = bbox.left + width;
rectangle_union(&obj->bounding_box, &bbox);
obj->position = conn->endpoints[0];
}
static void
measure_draw(Measure *measure, DiaRenderer *renderer)
{
Arrow arrow = MEASURE_ARROW(measure);
DIA_RENDERER_GET_CLASS (renderer)->set_linewidth (renderer, measure->line_width);
DIA_RENDERER_GET_CLASS (renderer)->set_linestyle (renderer, LINESTYLE_SOLID, 0.0);
DIA_RENDERER_GET_CLASS (renderer)->set_linejoin(renderer, LINEJOIN_MITER);
DIA_RENDERER_GET_CLASS (renderer)->set_linecaps(renderer, LINECAPS_ROUND);
DIA_RENDERER_GET_CLASS (renderer)->draw_line_with_arrows (
renderer,
&measure->connection.endpoints[0], &measure->connection.endpoints[1],
measure->line_width, &measure->line_color,
&arrow, &arrow);
DIA_RENDERER_GET_CLASS (renderer)->set_font(renderer,
measure->font, measure->font_height);
DIA_RENDERER_GET_CLASS (renderer)->draw_string (
renderer, measure->name,
&measure->text_pos, ALIGN_LEFT,
&measure->line_color);
}
/*! A standard props compliant object needs to describe its parameters */
static const PropDescription *
measure_describe_props (Measure *measure)
{
if (measure_props[0].quark == 0)
prop_desc_list_calculate_quarks(measure_props);
return measure_props;
}
static void
measure_get_props (Measure *measure, GPtrArray *props)
{
object_get_props_from_offsets(&measure->connection.object, measure_offsets, props);
}
static void
measure_set_props (Measure *measure, GPtrArray *props)
{
object_set_props_from_offsets(&measure->connection.object, measure_offsets, props);
measure_update_data (measure);
}
static real
measure_distance_from (Measure *measure, Point *point)
{
return distance_line_point (
&measure->connection.endpoints[0],
&measure->connection.endpoints[1],
measure->line_width, point);
}
static void
measure_select(Measure *measure, Point *clicked_point,
DiaRenderer *interactive_renderer)
{
connection_update_handles(&measure->connection);
}
static ObjectChange*
measure_move_handle (Measure *measure,
Handle *handle,
Point *to, ConnectionPoint *cp,
HandleMoveReason reason, ModifierKeys modifiers)
{
connection_move_handle(&measure->connection, handle->id, to, cp, reason, modifiers);
measure_update_data(measure);
return NULL;
}
static DiaMenu *
measure_get_object_menu(Measure *measure,
Point *clickedpoint)
{
return NULL;
}
static ObjectChange*
measure_move (Measure *measure, Point *to)
{
Point start_to_end;
Point *ends = &measure->connection.endpoints[0];
start_to_end = ends[1];
point_sub(&start_to_end, &ends[0]);
ends[1] = ends[0] = *to;
point_add(&ends[1], &start_to_end);
measure_update_data (measure);
return NULL;
}
static void
measure_destroy(Measure *measure)
{
connection_destroy(&measure->connection);
}
|