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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
|
/*
* $Id: gwy3dlabel.c 21680 2018-11-26 10:39:39Z yeti-dn $
* Copyright (C) 2005 David Necas (Yeti), Petr Klapetek.
* E-mail: yeti@gwyddion.net, klapetek@gwyddion.net.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include <string.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwyserializable.h>
#include <libgwyddion/gwydebugobjects.h>
#include <libgwydgets/gwy3dlabel.h>
#define GWY_3D_LABEL_TYPE_NAME "Gwy3DLabel"
#define PSPEC(id, type) GWY_FIND_PSPEC(GWY_TYPE_3D_LABEL, id, type)
enum {
PROP_0,
PROP_DELTA_X,
PROP_DELTA_Y,
PROP_ROTATION,
PROP_SIZE,
PROP_FIXED_SIZE,
PROP_TEXT,
PROP_DEFAULT_TEXT
};
static void gwy_3d_label_finalize (GObject *object);
static void gwy_3d_label_serializable_init(GwySerializableIface *iface);
static GByteArray* gwy_3d_label_serialize (GObject *obj,
GByteArray *buffer);
static gsize gwy_3d_label_get_size (GObject *obj);
static GObject* gwy_3d_label_deserialize (const guchar *buffer,
gsize size,
gsize *position);
static GObject* gwy_3d_label_duplicate_real (GObject *object);
static void gwy_3d_label_clone_real (GObject *source,
GObject *copy);
static void gwy_3d_label_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gwy_3d_label_get_property (GObject*object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
G_DEFINE_TYPE_EXTENDED
(Gwy3DLabel, gwy_3d_label, G_TYPE_OBJECT, 0,
GWY_IMPLEMENT_SERIALIZABLE(gwy_3d_label_serializable_init))
static void
gwy_3d_label_serializable_init(GwySerializableIface *iface)
{
iface->serialize = gwy_3d_label_serialize;
iface->deserialize = gwy_3d_label_deserialize;
iface->get_size = gwy_3d_label_get_size;
iface->duplicate = gwy_3d_label_duplicate_real;
iface->clone = gwy_3d_label_clone_real;
}
static void
gwy_3d_label_class_init(Gwy3DLabelClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
gobject_class->finalize = gwy_3d_label_finalize;
gobject_class->set_property = gwy_3d_label_set_property;
gobject_class->get_property = gwy_3d_label_get_property;
/**
* Gwy3DLabel:delta-x:
*
* The :delta-x property represents horizontal label offset in pixels
* (in screen coordinates after mapping from 3D to 2D).
**/
g_object_class_install_property
(gobject_class,
PROP_DELTA_X,
g_param_spec_double("delta-x",
"Horizontal offset",
"Horizontal label offset, in pixels",
-1000, 1000, 0.0, G_PARAM_READWRITE));
/**
* Gwy3DLabel:delta-y:
*
* The :delta-y property represents vertical label offset in pixels
* (in screen coordinates after mapping from 3D to 2D).
**/
g_object_class_install_property
(gobject_class,
PROP_DELTA_Y,
g_param_spec_double("delta-y",
"Vertical offset",
"Vertical label offset, in pixels",
-1000, 1000, 0.0, G_PARAM_READWRITE));
/**
* Gwy3DLabel:rotation:
*
* The :rotation property represents label rotation in radians,
* counterclokwise (on screen, after mapping from 3D to 2D).
**/
g_object_class_install_property
(gobject_class,
PROP_ROTATION,
g_param_spec_double("rotation",
"Rotation",
"Label rotation in radians, counterclokwise",
-G_PI, G_PI, 0.0, G_PARAM_READWRITE));
/**
* Gwy3DLabel:size:
*
* The :size property represents label size in pixels. When :fixed_size
* is %FALSE, its value is overwritten with automatic size.
**/
g_object_class_install_property
(gobject_class,
PROP_SIZE,
g_param_spec_double("size",
"Font size",
"Label font size",
1.0, 100.0, 14.0, G_PARAM_READWRITE));
/**
* Gwy3DLabel:fixed-size:
*
* The :fixed-size property controls whether the :size property is kept and
* honoured, or conversely ignored and overwritten with automatic size.
**/
g_object_class_install_property
(gobject_class,
PROP_FIXED_SIZE,
g_param_spec_boolean("fixed-size",
"Fixed size",
"Whether label size is fixed and doesn't scale",
FALSE, G_PARAM_READWRITE));
g_object_class_install_property
(gobject_class,
PROP_TEXT,
g_param_spec_string("text",
"Text",
"The label text template",
"", G_PARAM_READWRITE));
g_object_class_install_property
(gobject_class,
PROP_DEFAULT_TEXT,
g_param_spec_string("default-text",
"Default text",
"Default label text",
"", G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}
static void
gwy_3d_label_init(Gwy3DLabel *label)
{
label->delta_x = PSPEC("delta-x", DOUBLE)->default_value;
label->delta_y = PSPEC("delta-y", DOUBLE)->default_value;
label->rotation = PSPEC("rotation", DOUBLE)->default_value;
label->size = PSPEC("size", DOUBLE)->default_value;
/* default text is a construction property */
label->text = g_string_new(NULL);
}
static void
gwy_3d_label_finalize(GObject *object)
{
Gwy3DLabel *label = (Gwy3DLabel*)object;
g_free(label->default_text);
if (label->text)
g_string_free(label->text, TRUE);
G_OBJECT_CLASS(gwy_3d_label_parent_class)->finalize(object);
}
static GByteArray*
gwy_3d_label_serialize(GObject *obj,
GByteArray *buffer)
{
Gwy3DLabel *label;
gwy_debug("");
g_return_val_if_fail(GWY_IS_3D_LABEL(obj), NULL);
label = GWY_3D_LABEL(obj);
{
GwySerializeSpec spec[] = {
{ 'd', "delta_x", &label->delta_x, NULL, },
{ 'd', "delta_y", &label->delta_y, NULL, },
{ 'd', "rotation", &label->rotation, NULL, },
{ 'd', "size", &label->size, NULL, },
{ 's', "text", &label->text->str, NULL, },
{ 's', "default_text", &label->default_text, NULL, },
{ 'b', "fixed_size", &label->fixed_size, NULL, },
};
return gwy_serialize_pack_object_struct(buffer,
GWY_3D_LABEL_TYPE_NAME,
G_N_ELEMENTS(spec), spec);
}
}
static gsize
gwy_3d_label_get_size(GObject *obj)
{
Gwy3DLabel *label;
gwy_debug("");
g_return_val_if_fail(GWY_IS_3D_LABEL(obj), 0);
label = GWY_3D_LABEL(obj);
{
GwySerializeSpec spec[] = {
{ 'd', "delta_x", &label->delta_x, NULL, },
{ 'd', "delta_y", &label->delta_y, NULL, },
{ 'd', "rotation", &label->rotation, NULL, },
{ 'd', "size", &label->size, NULL, },
{ 's', "text", &label->text->str, NULL, },
{ 's', "default_text", &label->default_text, NULL, },
{ 'b', "fixed_size", &label->fixed_size, NULL, },
};
return gwy_serialize_get_struct_size(GWY_3D_LABEL_TYPE_NAME,
G_N_ELEMENTS(spec), spec);
}
}
static GObject*
gwy_3d_label_deserialize(const guchar *buffer,
gsize size,
gsize *position)
{
gboolean fixed_size;
gdouble delta_x, delta_y, scale, rotation;
gchar *text = NULL, *default_text = NULL;
Gwy3DLabel *label;
GwySerializeSpec spec[] = {
{ 'd', "delta_x", &delta_x, NULL, },
{ 'd', "delta_y", &delta_y, NULL, },
{ 'd', "rotation", &rotation, NULL, },
{ 'd', "size", &scale, NULL, },
{ 's', "text", &text, NULL, },
{ 's', "default_text", &default_text, NULL, },
{ 'b', "fixed_size", &fixed_size, NULL, },
};
gwy_debug("");
g_return_val_if_fail(buffer, NULL);
delta_x = PSPEC("delta-x", DOUBLE)->default_value;
delta_y = PSPEC("delta-y", DOUBLE)->default_value;
rotation = PSPEC("rotation", DOUBLE)->default_value;
scale = PSPEC("size", DOUBLE)->default_value;
if (!gwy_serialize_unpack_object_struct(buffer, size, position,
GWY_3D_LABEL_TYPE_NAME,
G_N_ELEMENTS(spec), spec)
|| !default_text) {
g_free(text);
g_free(default_text);
return NULL;
}
label = gwy_3d_label_new(default_text);
label->fixed_size = fixed_size;
label->delta_x = delta_x;
label->delta_y = delta_y;
label->rotation = rotation;
label->size = scale;
g_string_assign(label->text, text);
g_free(default_text);
g_free(text);
return (GObject*)label;
}
static GObject*
gwy_3d_label_duplicate_real(GObject *object)
{
Gwy3DLabel *label, *duplicate;
g_return_val_if_fail(GWY_IS_3D_LABEL(object), NULL);
label = GWY_3D_LABEL(object);
duplicate = gwy_3d_label_new(label->default_text);
gwy_serializable_clone(G_OBJECT(label), G_OBJECT(duplicate));
return (GObject*)duplicate;
}
static void
gwy_3d_label_clone_real(GObject *source,
GObject *copy)
{
Gwy3DLabel *label, *clone;
g_return_if_fail(GWY_IS_3D_LABEL(source));
g_return_if_fail(GWY_IS_3D_LABEL(copy));
label = GWY_3D_LABEL(source);
clone = GWY_3D_LABEL(copy);
if (!gwy_strequal(label->default_text, clone->default_text)) {
g_warning("Trying to change construction-only property by cloning");
g_free(clone->default_text);
clone->default_text = g_strdup(label->default_text);
}
g_object_freeze_notify(copy);
if (clone->fixed_size != label->fixed_size) {
clone->fixed_size = !!label->fixed_size;
g_object_notify(copy, "fixed-size");
}
if (clone->delta_x != label->delta_x) {
clone->delta_x = label->delta_x;
g_object_notify(copy, "delta-x");
}
if (clone->delta_y != label->delta_y) {
clone->delta_y = label->delta_y;
g_object_notify(copy, "delta-y");
}
if (clone->rotation != label->rotation) {
clone->rotation = label->rotation;
g_object_notify(copy, "rotation");
}
if (clone->size != label->size) {
clone->size = label->size;
g_object_notify(copy, "size");
}
gwy_3d_label_set_text(clone, label->text->str);
g_object_thaw_notify(copy);
}
static void
gwy_3d_label_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
Gwy3DLabel *label = GWY_3D_LABEL(object);
switch (prop_id) {
case PROP_DELTA_X:
label->delta_x = g_value_get_double(value);
break;
case PROP_DELTA_Y:
label->delta_y = g_value_get_double(value);
break;
case PROP_ROTATION:
label->rotation = g_value_get_double(value);
break;
case PROP_SIZE:
label->size = g_value_get_double(value);
break;
case PROP_FIXED_SIZE:
label->fixed_size = g_value_get_boolean(value);
break;
case PROP_TEXT:
g_string_assign(label->text, g_value_get_string(value));
break;
case PROP_DEFAULT_TEXT:
/* This can happen only on construction */
g_assert(!label->default_text);
label->default_text = g_value_dup_string(value);
g_assert(label->text);
g_string_assign(label->text, label->default_text);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
gwy_3d_label_get_property(GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
Gwy3DLabel *label = GWY_3D_LABEL(object);
switch (prop_id) {
case PROP_DELTA_X:
g_value_set_double(value, label->delta_x);
break;
case PROP_DELTA_Y:
g_value_set_double(value, label->delta_y);
break;
case PROP_ROTATION:
g_value_set_double(value, label->rotation);
break;
case PROP_SIZE:
g_value_set_double(value, label->size);
break;
case PROP_FIXED_SIZE:
g_value_set_boolean(value, label->fixed_size);
break;
case PROP_TEXT:
g_value_set_string(value, label->text->str);
break;
case PROP_DEFAULT_TEXT:
g_value_set_string(value, label->default_text);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
/**
* gwy_3d_label_new:
* @default_text: Label default text.
*
* Creates a new 3D view label.
*
* Returns: A newly created 3D label.
**/
Gwy3DLabel*
gwy_3d_label_new(const gchar *default_text)
{
Gwy3DLabel *label;
label = g_object_new(GWY_TYPE_3D_LABEL, "default-text", default_text, NULL);
return label;
}
/**
* gwy_3d_label_set_text:
* @label: A 3D label.
* @text: New label text.
*
* Sets the text of a 3D label.
**/
void
gwy_3d_label_set_text(Gwy3DLabel *label,
const gchar *text)
{
g_return_if_fail(GWY_IS_3D_LABEL(label));
if (gwy_strequal(text, label->text->str))
return;
g_string_assign(label->text, text);
g_object_notify(G_OBJECT(label), "text");
}
/**
* gwy_3d_label_get_text:
* @label: A 3D label.
*
* Gets the text of a 3D label.
*
* Returns: The label text. The returned string is owned by label and must no
* be modified or freed.
**/
const gchar*
gwy_3d_label_get_text(Gwy3DLabel *label)
{
g_return_val_if_fail(GWY_IS_3D_LABEL(label), NULL);
return label->text->str;
}
/**
* gwy_3d_label_expand_text:
* @label: A 3D label.
* @variables: Hash table with variable values.
*
* Substitutes variables in label text.
*
* Returns: A newly allocated string with variables from @variables substituted
* with values.
**/
gchar*
gwy_3d_label_expand_text(Gwy3DLabel *label,
GHashTable *variables)
{
GString *buffer, *key;
gchar *s, *lb;
g_return_val_if_fail(GWY_IS_3D_LABEL(label), NULL);
lb = label->text->str;
buffer = g_string_new(NULL);
key = g_string_new(NULL);
while (lb && *lb) {
if (!(s = strchr(lb, '$'))) {
g_string_append(buffer, lb);
break;
}
g_string_append_len(buffer, lb, s - lb);
lb = s + 1;
if (!g_ascii_isalpha(*lb)) {
g_string_append_c(buffer, '$');
continue;
}
for (s = lb; g_ascii_isalpha(*s); s++)
;
g_string_append_len(g_string_truncate(key, 0), lb, s-lb);
g_string_ascii_down(key);
s = g_hash_table_lookup(variables, key->str);
if (s) {
g_string_append(buffer, s);
lb += strlen(key->str);
}
else
g_string_append_c(buffer, '$');
}
s = buffer->str;
g_string_free(buffer, FALSE);
g_string_free(key, TRUE);
return s;
}
/**
* gwy_3d_label_reset:
* @label: A 3D label.
*
* Resets all 3D label properties and text to default values.
**/
void
gwy_3d_label_reset(Gwy3DLabel *label)
{
GObject *object;
gdouble defval;
g_return_if_fail(GWY_IS_3D_LABEL(label));
object = G_OBJECT(label);
g_object_freeze_notify(object);
if (label->fixed_size) {
label->fixed_size = FALSE;
g_object_notify(object, "fixed-size");
}
if (label->delta_x != (defval = PSPEC("delta-x", DOUBLE)->default_value)) {
label->delta_x = defval;
g_object_notify(object, "delta-x");
}
if (label->delta_y != (defval = PSPEC("delta-y", DOUBLE)->default_value)) {
label->delta_y = defval;
g_object_notify(object, "delta-y");
}
if (label->rotation
!= (defval = PSPEC("rotation", DOUBLE)->default_value)) {
label->rotation = defval;
g_object_notify(object, "rotation");
}
if (label->size != (defval = PSPEC("size", DOUBLE)->default_value)) {
label->size = defval;
g_object_notify(object, "size");
}
gwy_3d_label_set_text(label, label->default_text);
g_object_thaw_notify(object);
}
/**
* gwy_3d_label_reset_text:
* @label: A 3D label.
*
* Resets 3D label text to default values.
**/
void
gwy_3d_label_reset_text(Gwy3DLabel *label)
{
g_return_if_fail(GWY_IS_3D_LABEL(label));
gwy_3d_label_set_text(label, label->default_text);
}
/**
* gwy_3d_label_user_size:
* @label: A 3D label.
* @user_size: Size of the text to be set.
*
* Eventually sets size of a 3D label.
*
* If label size si fixed, it does not change and it is simply returned.
* Otherwise label size is changed and @user_size itself is returned.
*
* Returns: Size of label.
**/
gdouble
gwy_3d_label_user_size(Gwy3DLabel *label,
gdouble user_size)
{
g_return_val_if_fail(GWY_IS_3D_LABEL(label), 0.0);
if (label->fixed_size)
return label->size;
if (label->size != user_size) {
label->size = user_size;
g_object_notify(G_OBJECT(label), "size");
}
return user_size;
}
/************************** Documentation ****************************/
/**
* SECTION:gwy3dlabel
* @title: Gwy3DLabel
* @short_description: Label on Gwy3DView
**/
/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */
|