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 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
|
/*
* $Id: gwyplaintool.c 24952 2022-08-26 15:24:56Z yeti-dn $
* Copyright (C) 2006-2022 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 <stdlib.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include "libgwyddion/gwymacros.h"
#include "libgwyddion/gwycontainer.h"
#include "libprocess/elliptic.h"
#include "libgwydgets/gwydgetutils.h"
#include "menu.h"
#include "log.h"
#include "gwyplaintool.h"
#include "param-internal.h"
#define ITEM_CHANGED "item-changed::"
enum {
RLABEL_X, RLABEL_Y, RLABEL_W, RLABEL_H, NRLABELS
};
typedef void (*GwyRectSelectionLabelsFunc)(gpointer user_data);
struct _GwyRectSelectionLabels {
GtkWidget *table;
GtkLabel *real[NRLABELS];
GtkWidget *px[NRLABELS];
GtkSpinButton *pix[NRLABELS];
gulong pix_id[NRLABELS];
gboolean in_update;
gboolean none_is_full;
GCallback callback;
gpointer cbdata;
};
static void gwy_plain_tool_finalize (GObject *object);
static void gwy_plain_tool_show (GwyTool *tool);
static void gwy_plain_tool_hide (GwyTool *tool);
static void gwy_plain_tool_data_switched (GwyTool *tool,
GwyDataView *data_view);
static void gwy_plain_tool_reconnect_container (GwyPlainTool *plain_tool,
GwyDataView *data_view);
static void gwy_plain_tool_data_item_changed (GwyContainer *container,
GQuark quark,
GwyPlainTool *plain_tool);
static void gwy_plain_tool_mask_item_changed (GwyContainer *container,
GQuark quark,
GwyPlainTool *plain_tool);
static void gwy_plain_tool_show_item_changed (GwyContainer *container,
GQuark quark,
GwyPlainTool *plain_tool);
static void gwy_plain_tool_selection_item_changed (GwyContainer *container,
GQuark quark,
GwyPlainTool *plain_tool);
static void gwy_plain_tool_data_changed (GwyPlainTool *plain_tool);
static void gwy_plain_tool_mask_changed (GwyPlainTool *plain_tool);
static void gwy_plain_tool_show_changed (GwyPlainTool *plain_tool);
static void gwy_plain_tool_selection_disconnect (GwyPlainTool *plain_tool);
static void gwy_plain_tool_selection_reconnect (GwyPlainTool *plain_tool);
static void gwy_plain_tool_selection_changed (GwySelection *selection,
gint hint,
GwyPlainTool *plain_tool);
static void gwy_plain_tool_selection_finished (GwySelection *selection,
GwyPlainTool *plain_tool);
static void gwy_plain_tool_update_units (GwyPlainTool *plain_tool);
static void gwy_plain_tool_response (GwyTool *tool,
gint response_id);
static void gwy_rect_selection_labels_spinned (GtkSpinButton *spin,
GwyRectSelectionLabels *rlabels);
static void gwy_rect_selection_labels_set_sensitive(GwyRectSelectionLabels *rlabels,
gboolean sensitive);
G_DEFINE_ABSTRACT_TYPE(GwyPlainTool, gwy_plain_tool, GWY_TYPE_TOOL)
static guint param_table_param_changed = 0;
static GType selection_type_rect = 0;
static void
gwy_plain_tool_class_init(GwyPlainToolClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
GwyToolClass *tool_class = GWY_TOOL_CLASS(klass);
gobject_class->finalize = gwy_plain_tool_finalize;
tool_class->hide = gwy_plain_tool_hide;
tool_class->show = gwy_plain_tool_show;
tool_class->data_switched = gwy_plain_tool_data_switched;
tool_class->response = gwy_plain_tool_response;
}
static void
gwy_plain_tool_finalize(GObject *object)
{
GwyPlainTool *plain_tool;
plain_tool = GWY_PLAIN_TOOL(object);
gwy_plain_tool_selection_disconnect(plain_tool);
GWY_OBJECT_UNREF(plain_tool->layer);
gwy_plain_tool_reconnect_container(plain_tool, NULL);
g_free(plain_tool->selection_bname);
GWY_SI_VALUE_FORMAT_FREE(plain_tool->coord_format);
GWY_SI_VALUE_FORMAT_FREE(plain_tool->value_format);
G_OBJECT_CLASS(gwy_plain_tool_parent_class)->finalize(object);
/* This is a bit weird, but in contrast to GwyDialog which *is* a dialog, we just *hold* a dialog. We want to run
*
* gtk_widget_destroy(tool->dialog);
*
* in GwyTool first, i.e. destroy the GwyParamTable's widget before destroing the table object itself. This is the
* normal order of things. GwyParamTable may need to support also the opposite order, but then it needs to
* carefully disconnect all widget signal handlers before disconnecting from "destroy" – and it is not currently
* doing that. */
gwy_debug("finalising param tables");
g_ptr_array_free(plain_tool->param_tables, TRUE);
}
static void
gwy_plain_tool_init(GwyPlainTool *plain_tool)
{
plain_tool->param_tables = g_ptr_array_new_full(0, g_object_unref);
plain_tool->id = -1;
}
static void
gwy_plain_tool_show(GwyTool *tool)
{
GwyPlainTool *plain_tool;
GwyPlainToolClass *klass;
plain_tool = GWY_PLAIN_TOOL(tool);
if (plain_tool->lazy_updates && plain_tool->pending_updates) {
klass = GWY_PLAIN_TOOL_GET_CLASS(tool);
/* Since we remember only what has changed, the order can be different. The following order should cause the
* least odd effects. If a tool has problems with it, it can simply look at @pending_updates in its show()
* method, update self and clear @pending_updates before calling parent's show(). */
if (plain_tool->pending_updates & GWY_PLAIN_TOOL_CHANGED_DATA) {
gwy_plain_tool_update_units(plain_tool);
if (klass->data_changed)
klass->data_changed(plain_tool);
plain_tool->pending_updates &= ~GWY_PLAIN_TOOL_CHANGED_DATA;
}
if (plain_tool->pending_updates & GWY_PLAIN_TOOL_CHANGED_MASK) {
if (klass->mask_changed)
klass->mask_changed(plain_tool);
plain_tool->pending_updates &= ~GWY_PLAIN_TOOL_CHANGED_MASK;
}
if (plain_tool->pending_updates & GWY_PLAIN_TOOL_CHANGED_SHOW) {
if (klass->show_changed)
klass->show_changed(plain_tool);
plain_tool->pending_updates &= ~GWY_PLAIN_TOOL_CHANGED_SHOW;
}
if (plain_tool->pending_updates & GWY_PLAIN_TOOL_CHANGED_SELECTION) {
/* This is gross. */
if (klass->selection_changed)
klass->selection_changed(plain_tool, -1);
plain_tool->pending_updates &= ~GWY_PLAIN_TOOL_CHANGED_SELECTION;
}
if (plain_tool->pending_updates & GWY_PLAIN_TOOL_FINISHED_SELECTION) {
if (klass->selection_finished)
klass->selection_finished(plain_tool);
plain_tool->pending_updates &= ~GWY_PLAIN_TOOL_FINISHED_SELECTION;
}
if (plain_tool->pending_updates) {
g_warning("Stray bits in pending_updates: %u", plain_tool->pending_updates);
plain_tool->pending_updates = 0;
}
}
GWY_TOOL_CLASS(gwy_plain_tool_parent_class)->show(tool);
}
static void
gwy_plain_tool_hide(GwyTool *tool)
{
if (GWY_PLAIN_TOOL(tool)->pending_updates)
g_warning("We have pending updates when tool is being hidden?");
GWY_TOOL_CLASS(gwy_plain_tool_parent_class)->hide(tool);
}
void
gwy_plain_tool_data_switched(GwyTool *tool,
GwyDataView *data_view)
{
GwyPlainTool *plain_tool;
gwy_debug("%s %p", GWY_TOOL_GET_CLASS(tool)->title, data_view);
if (GWY_TOOL_CLASS(gwy_plain_tool_parent_class)->data_switched)
GWY_TOOL_CLASS(gwy_plain_tool_parent_class)->data_switched(tool, data_view);
plain_tool = GWY_PLAIN_TOOL(tool);
if (data_view == plain_tool->data_view)
return;
gwy_plain_tool_selection_disconnect(plain_tool);
gwy_plain_tool_reconnect_container(plain_tool, data_view);
gwy_plain_tool_update_units(plain_tool);
if (data_view && plain_tool->layer_type) {
gwy_plain_tool_ensure_layer(plain_tool, plain_tool->layer_type);
gwy_plain_tool_selection_reconnect(plain_tool);
}
else {
GWY_OBJECT_UNREF(plain_tool->layer);
gwy_plain_tool_selection_changed(NULL, -1, plain_tool);
}
}
/**
* gwy_plain_tool_reconnect_container:
* @plain_tool: A plain tool.
* @data_view: A new data view to switch the tool to.
*
* Performs signal diconnection and reconnection when data is swtiched.
*
* The @data_view and @container fields have to still point to the old
* objects (or be %NULL).
**/
static void
gwy_plain_tool_reconnect_container(GwyPlainTool *plain_tool,
GwyDataView *data_view)
{
GwyPixmapLayer *layer;
const gchar *data_key;
gchar *key, *sigdetail;
guint len;
gwy_debug("%s %p", GWY_TOOL_GET_CLASS(plain_tool)->title, data_view);
GWY_SIGNAL_HANDLER_DISCONNECT(plain_tool->data_field, plain_tool->data_id);
GWY_SIGNAL_HANDLER_DISCONNECT(plain_tool->mask_field, plain_tool->mask_id);
GWY_SIGNAL_HANDLER_DISCONNECT(plain_tool->show_field, plain_tool->show_id);
GWY_SIGNAL_HANDLER_DISCONNECT(plain_tool->container, plain_tool->data_item_id);
GWY_SIGNAL_HANDLER_DISCONNECT(plain_tool->container, plain_tool->mask_item_id);
GWY_SIGNAL_HANDLER_DISCONNECT(plain_tool->container, plain_tool->show_item_id);
GWY_OBJECT_UNREF(plain_tool->data_field);
GWY_OBJECT_UNREF(plain_tool->mask_field);
GWY_OBJECT_UNREF(plain_tool->show_field);
GWY_OBJECT_UNREF(plain_tool->container);
plain_tool->id = -1;
if (!(plain_tool->data_view = data_view))
return;
plain_tool->container = gwy_data_view_get_data(data_view);
g_object_ref(plain_tool->container);
layer = gwy_data_view_get_base_layer(data_view);
data_key = gwy_pixmap_layer_get_data_key(layer);
/* @sigdetail has the form "item-changed::/0/data", @key is a pointer to the key part. The "data" tail is
* subsequently replaced with "mask" and "show". */
len = strlen(data_key);
g_return_if_fail(len > 5 && data_key[0] == '/' && gwy_strequal(data_key + len-5, "/data"));
plain_tool->id = atoi(data_key + 1);
g_return_if_fail(plain_tool->id >= 0);
len += sizeof(ITEM_CHANGED)-1;
sigdetail = g_new(gchar, len+1);
key = sigdetail + sizeof(ITEM_CHANGED)-1;
strcpy(sigdetail, ITEM_CHANGED);
strcpy(sigdetail + sizeof(ITEM_CHANGED)-1, data_key);
plain_tool->data_item_id = g_signal_connect(plain_tool->container, sigdetail,
G_CALLBACK(gwy_plain_tool_data_item_changed), plain_tool);
if (gwy_container_gis_object_by_name(plain_tool->container, key, &plain_tool->data_field)) {
g_object_ref(plain_tool->data_field);
plain_tool->data_id = g_signal_connect_swapped(plain_tool->data_field, "data-changed",
G_CALLBACK(gwy_plain_tool_data_changed), plain_tool);
}
strcpy(sigdetail + len-4, "mask");
plain_tool->mask_item_id = g_signal_connect(plain_tool->container, sigdetail,
G_CALLBACK(gwy_plain_tool_mask_item_changed), plain_tool);
if (gwy_container_gis_object_by_name(plain_tool->container, key, &plain_tool->mask_field)) {
g_object_ref(plain_tool->mask_field);
plain_tool->mask_id = g_signal_connect_swapped(plain_tool->mask_field, "data-changed",
G_CALLBACK(gwy_plain_tool_mask_changed), plain_tool);
}
strcpy(sigdetail + len-4, "show");
plain_tool->show_item_id = g_signal_connect(plain_tool->container, sigdetail,
G_CALLBACK(gwy_plain_tool_show_item_changed), plain_tool);
if (gwy_container_gis_object_by_name(plain_tool->container, key, &plain_tool->show_field)) {
g_object_ref(plain_tool->show_field);
plain_tool->show_id = g_signal_connect_swapped(plain_tool->show_field, "data-changed",
G_CALLBACK(gwy_plain_tool_show_changed), plain_tool);
}
g_free(sigdetail);
}
static void
gwy_plain_tool_data_item_changed(GwyContainer *container,
GQuark quark,
GwyPlainTool *plain_tool)
{
GWY_SIGNAL_HANDLER_DISCONNECT(plain_tool->data_field, plain_tool->data_id);
GWY_OBJECT_UNREF(plain_tool->data_field);
if (gwy_container_gis_object(container, quark, &plain_tool->data_field)) {
g_object_ref(plain_tool->data_field);
plain_tool->data_id = g_signal_connect_swapped(plain_tool->data_field, "data-changed",
G_CALLBACK(gwy_plain_tool_data_changed), plain_tool);
gwy_plain_tool_data_changed(plain_tool);
}
else {
/* No data field means we disconnect completely. */
gwy_tool_data_switched(GWY_TOOL(plain_tool), NULL);
}
}
static void
gwy_plain_tool_mask_item_changed(GwyContainer *container,
GQuark quark,
GwyPlainTool *plain_tool)
{
GWY_SIGNAL_HANDLER_DISCONNECT(plain_tool->mask_field, plain_tool->mask_id);
GWY_OBJECT_UNREF(plain_tool->mask_field);
if (gwy_container_gis_object(container, quark, &plain_tool->mask_field)) {
g_object_ref(plain_tool->mask_field);
plain_tool->mask_id = g_signal_connect_swapped(plain_tool->mask_field, "data-changed",
G_CALLBACK(gwy_plain_tool_mask_changed), plain_tool);
}
gwy_plain_tool_mask_changed(plain_tool);
}
static void
gwy_plain_tool_show_item_changed(GwyContainer *container,
GQuark quark,
GwyPlainTool *plain_tool)
{
GWY_SIGNAL_HANDLER_DISCONNECT(plain_tool->show_field, plain_tool->show_id);
GWY_OBJECT_UNREF(plain_tool->show_field);
if (gwy_container_gis_object(container, quark, &plain_tool->show_field)) {
g_object_ref(plain_tool->show_field);
plain_tool->show_id = g_signal_connect_swapped(plain_tool->show_field, "data-changed",
G_CALLBACK(gwy_plain_tool_show_changed), plain_tool);
}
gwy_plain_tool_show_changed(plain_tool);
}
static void
gwy_plain_tool_selection_item_changed(GwyContainer *container,
GQuark quark,
GwyPlainTool *plain_tool)
{
GWY_SIGNAL_HANDLER_DISCONNECT(plain_tool->selection, plain_tool->selection_cid);
GWY_SIGNAL_HANDLER_DISCONNECT(plain_tool->selection, plain_tool->selection_fid);
GWY_OBJECT_UNREF(plain_tool->selection);
if (gwy_container_gis_object(container, quark, &plain_tool->selection)) {
g_object_ref(plain_tool->selection);
plain_tool->selection_cid = g_signal_connect(plain_tool->selection, "changed",
G_CALLBACK(gwy_plain_tool_selection_changed), plain_tool);
plain_tool->selection_fid = g_signal_connect(plain_tool->selection, "finished",
G_CALLBACK(gwy_plain_tool_selection_finished), plain_tool);
}
gwy_plain_tool_selection_changed(plain_tool->selection, -1, plain_tool);
}
static void
gwy_plain_tool_data_changed(GwyPlainTool *plain_tool)
{
GwyPlainToolClass *klass;
klass = GWY_PLAIN_TOOL_GET_CLASS(plain_tool);
if (plain_tool->lazy_updates && !gwy_tool_is_visible(GWY_TOOL(plain_tool)))
plain_tool->pending_updates |= GWY_PLAIN_TOOL_CHANGED_DATA;
else {
gwy_plain_tool_update_units(plain_tool);
if (klass->data_changed)
klass->data_changed(plain_tool);
}
}
static void
gwy_plain_tool_mask_changed(GwyPlainTool *plain_tool)
{
GwyPlainToolClass *klass;
klass = GWY_PLAIN_TOOL_GET_CLASS(plain_tool);
if (plain_tool->lazy_updates && !gwy_tool_is_visible(GWY_TOOL(plain_tool)))
plain_tool->pending_updates |= GWY_PLAIN_TOOL_CHANGED_MASK;
else if (klass->mask_changed)
klass->mask_changed(plain_tool);
}
static void
gwy_plain_tool_show_changed(GwyPlainTool *plain_tool)
{
GwyPlainToolClass *klass;
klass = GWY_PLAIN_TOOL_GET_CLASS(plain_tool);
if (plain_tool->lazy_updates && !gwy_tool_is_visible(GWY_TOOL(plain_tool)))
plain_tool->pending_updates |= GWY_PLAIN_TOOL_CHANGED_SHOW;
else if (klass->show_changed)
klass->show_changed(plain_tool);
}
static void
gwy_plain_tool_selection_changed(GwySelection *selection,
gint hint,
GwyPlainTool *plain_tool)
{
GwyPlainToolClass *klass;
if (plain_tool->clear)
gtk_widget_set_sensitive(plain_tool->clear, selection && gwy_selection_get_data(selection, NULL));
klass = GWY_PLAIN_TOOL_GET_CLASS(plain_tool);
if (plain_tool->lazy_updates && !gwy_tool_is_visible(GWY_TOOL(plain_tool)))
plain_tool->pending_updates |= GWY_PLAIN_TOOL_CHANGED_SELECTION;
else if (klass->selection_changed)
klass->selection_changed(plain_tool, hint);
}
static void
gwy_plain_tool_selection_finished(G_GNUC_UNUSED GwySelection *selection,
GwyPlainTool *plain_tool)
{
GwyPlainToolClass *klass;
klass = GWY_PLAIN_TOOL_GET_CLASS(plain_tool);
if (plain_tool->lazy_updates && !gwy_tool_is_visible(GWY_TOOL(plain_tool)))
plain_tool->pending_updates |= GWY_PLAIN_TOOL_FINISHED_SELECTION;
else if (klass->selection_finished)
klass->selection_finished(plain_tool);
}
/**
* gwy_plain_tool_update_units:
* @plain_tool: A plain tool.
*
* Updates plain tool's unit formats.
*
* More precisely, @coord_format and @value_format are updated according to the current data field and @unit_style.
* If @unit_style is %GWY_SI_UNIT_FORMAT_NONE existing formats are destroyed and set to %NULL.
**/
static void
gwy_plain_tool_update_units(GwyPlainTool *plain_tool)
{
if (plain_tool->data_field && plain_tool->unit_style) {
plain_tool->coord_format = gwy_data_field_get_value_format_xy(plain_tool->data_field,
plain_tool->unit_style, plain_tool->coord_format);
plain_tool->value_format = gwy_data_field_get_value_format_z(plain_tool->data_field,
plain_tool->unit_style, plain_tool->value_format);
}
else {
GWY_SI_VALUE_FORMAT_FREE(plain_tool->coord_format);
GWY_SI_VALUE_FORMAT_FREE(plain_tool->value_format);
}
}
/**
* gwy_plain_tool_check_layer_type:
* @plain_tool: A plain tool.
* @name: Layer type name (e.g. <literal>"GwyLayerPoint"</literal>).
*
* Checks for a required layer type.
*
* If the layer exists, its #GType is returned. If it does not exist, zero is returned and a warning message is added
* to the tool dialog. In addition, it sets @init_failed to %TRUE.
*
* Therefore, this function should be called early in tool instance initialization and it should not be called again
* once it fails.
*
* Returns: The type of the layer, or 0 on failure.
**/
GType
gwy_plain_tool_check_layer_type(GwyPlainTool *plain_tool,
const gchar *name)
{
GtkWidget *label;
GtkBox *vbox;
GType type;
gchar *s;
g_return_val_if_fail(GWY_IS_PLAIN_TOOL(plain_tool), 0);
g_return_val_if_fail(name, 0);
if (plain_tool->init_failed) {
g_warning("Tool layer check already failed.");
return 0;
}
type = g_type_from_name(name);
if (type)
return type;
plain_tool->init_failed = TRUE;
vbox = GTK_BOX(GTK_DIALOG(GWY_TOOL(plain_tool)->dialog)->vbox);
label = gtk_label_new(NULL);
gtk_label_set_markup(GTK_LABEL(label), _("<big><b>Missing layer module.</b></big>"));
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
gtk_misc_set_padding(GTK_MISC(label), 12, 0);
gtk_box_pack_start(vbox, label, FALSE, FALSE, 6);
label = gtk_label_new(NULL);
s = g_strdup_printf(_("This tool requires layer of type %s to work, which does not seem to be installed. "
"Please check your installation."),
name);
gtk_label_set_markup(GTK_LABEL(label), s);
gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
gtk_misc_set_padding(GTK_MISC(label), 12, 0);
gtk_box_pack_start(vbox, label, FALSE, FALSE, 6);
g_free(s);
gwy_tool_add_hide_button(GWY_TOOL(plain_tool), TRUE);
gtk_widget_show_all(GTK_WIDGET(vbox));
return 0;
}
/**
* gwy_plain_tool_connect_selection:
* @plain_tool: A plain tool.
* @layer_type: Layer type. Use gwy_plain_tool_check_layer_type() in tool instance initialization to check for layer
* types.
* @bname: Selection key base name, for example <literal>"line"</literal>.
*
* Sets up a plain tool to automatically manage layer selection.
*
* When @layer_type is 0 and @bname %NULL, plain tool stops automatically managing selection (hopefully).
*
* This method performs gwy_plain_tool_ensure_layer() and gwy_plain_tool_set_selection_key(), connecting to the
* selection and making sure the @selection field always points to the correct selection object (or is %NULL).
*
* The @selection_changed method of #GwyPlainToolClass is only invoked for a tool instance once this method was called
* to set up the selection tracking.
**/
void
gwy_plain_tool_connect_selection(GwyPlainTool *plain_tool,
GType layer_type,
const gchar *bname)
{
gwy_debug("%s %s", GWY_TOOL_GET_CLASS(plain_tool)->title, bname);
g_return_if_fail(GWY_IS_PLAIN_TOOL(plain_tool));
if (layer_type || bname) {
g_return_if_fail(g_type_is_a(layer_type, GWY_TYPE_VECTOR_LAYER));
g_return_if_fail(bname);
}
gwy_plain_tool_selection_disconnect(plain_tool);
gwy_assign_string(&plain_tool->selection_bname, bname);
plain_tool->layer_type = layer_type;
if (!bname || !layer_type || !plain_tool->data_view)
return;
gwy_plain_tool_ensure_layer(plain_tool, layer_type);
gwy_plain_tool_selection_reconnect(plain_tool);
}
static void
gwy_plain_tool_selection_disconnect(GwyPlainTool *plain_tool)
{
gwy_debug("");
GWY_SIGNAL_HANDLER_DISCONNECT(plain_tool->container, plain_tool->selection_item_id);
GWY_SIGNAL_HANDLER_DISCONNECT(plain_tool->selection, plain_tool->selection_cid);
GWY_SIGNAL_HANDLER_DISCONNECT(plain_tool->selection, plain_tool->selection_fid);
GWY_OBJECT_UNREF(plain_tool->selection);
}
static void
gwy_plain_tool_selection_reconnect(GwyPlainTool *plain_tool)
{
const gchar *key;
gchar *sigdetail;
gwy_debug("%s", plain_tool->selection_bname);
if (!plain_tool->selection_bname)
return;
key = gwy_plain_tool_set_selection_key(plain_tool, plain_tool->selection_bname);
sigdetail = g_strconcat(ITEM_CHANGED, key, NULL);
plain_tool->selection_item_id = g_signal_connect(plain_tool->container, sigdetail,
G_CALLBACK(gwy_plain_tool_selection_item_changed), plain_tool);
if (gwy_container_gis_object_by_name(plain_tool->container, key, &plain_tool->selection)) {
g_object_ref(plain_tool->selection);
plain_tool->selection_cid = g_signal_connect(plain_tool->selection, "changed",
G_CALLBACK(gwy_plain_tool_selection_changed), plain_tool);
plain_tool->selection_fid = g_signal_connect(plain_tool->selection, "finished",
G_CALLBACK(gwy_plain_tool_selection_finished), plain_tool);
}
g_free(sigdetail);
gwy_plain_tool_selection_changed(plain_tool->selection, -1, plain_tool);
}
/**
* gwy_plain_tool_set_selection_key:
* @plain_tool: A plain tool.
* @bname: Selection key base name, for example <literal>"line"</literal>.
*
* Constructs selection key from data key and sets it on the vector layer.
*
* This is a low-level function, normally you would use
* gwy_plain_tool_connect_selection().
*
* Returns: The full key (as a layer-owned string).
**/
const gchar*
gwy_plain_tool_set_selection_key(GwyPlainTool *plain_tool,
const gchar *bname)
{
GwyPixmapLayer *layer;
const gchar *data_key;
gchar *key;
guint len;
gwy_debug("%s", bname);
g_return_val_if_fail(GWY_IS_PLAIN_TOOL(plain_tool), NULL);
g_return_val_if_fail(GWY_IS_DATA_VIEW(plain_tool->data_view), NULL);
g_return_val_if_fail(GWY_IS_VECTOR_LAYER(plain_tool->layer), NULL);
g_return_val_if_fail(bname, NULL);
layer = gwy_data_view_get_base_layer(plain_tool->data_view);
data_key = gwy_pixmap_layer_get_data_key(layer);
gwy_debug("data_key: <%s>", data_key);
len = strlen(data_key);
g_return_val_if_fail(len > 5 && gwy_strequal(data_key + len-5, "/data"), NULL);
key = g_strdup_printf("%.*s/select/%s", len-5, data_key, bname);
gwy_vector_layer_set_selection_key(plain_tool->layer, key);
gwy_debug("selection key: <%s>", key);
g_free(key);
gwy_vector_layer_ensure_selection(plain_tool->layer);
/* The returned value is a quark and should be safe to use anytime. */
return gwy_vector_layer_get_selection_key(plain_tool->layer);
}
/**
* gwy_plain_tool_ensure_layer:
* @plain_tool: A plain tool.
* @layer_type: Layer type. Use gwy_plain_tool_check_layer_type() in tool instance initialization to check for layer
* types.
*
* Makes sure a plain tool's layer is of the correct type.
*
* This is a low-level function, normally you would use gwy_plain_tool_connect_selection().
**/
void
gwy_plain_tool_ensure_layer(GwyPlainTool *plain_tool,
GType layer_type)
{
gwy_debug("%s %s", GWY_TOOL_GET_CLASS(plain_tool)->title, g_type_name(layer_type));
g_return_if_fail(GWY_IS_PLAIN_TOOL(plain_tool));
g_return_if_fail(g_type_is_a(layer_type, GWY_TYPE_VECTOR_LAYER));
GWY_OBJECT_UNREF(plain_tool->layer);
plain_tool->layer = gwy_data_view_get_top_layer(plain_tool->data_view);
if (!plain_tool->layer || G_TYPE_FROM_INSTANCE(plain_tool->layer) != layer_type) {
plain_tool->layer = g_object_new(layer_type, NULL);
gwy_data_view_set_top_layer(plain_tool->data_view, plain_tool->layer);
}
g_object_ref(plain_tool->layer);
}
/**
* gwy_plain_tool_add_clear_button:
* @plain_tool: A plain tool.
*
* Adds a `Clear' button to a plain tool.
*
* This button works with automatically managed selection (see gwy_plain_tool_connect_selection()). If you want to
* manage selection yourself add the button with gtk_dialog_add_button().
*
* Returns: The button widget.
**/
GtkWidget*
gwy_plain_tool_add_clear_button(GwyPlainTool *plain_tool)
{
GtkDialog *dialog;
g_return_val_if_fail(GWY_IS_PLAIN_TOOL(plain_tool), NULL);
g_return_val_if_fail(!plain_tool->clear, NULL);
dialog = GTK_DIALOG(GWY_TOOL(plain_tool)->dialog);
plain_tool->clear = gtk_dialog_add_button(dialog, GTK_STOCK_CLEAR, GWY_TOOL_RESPONSE_CLEAR);
gtk_widget_set_tooltip_text(plain_tool->clear, _("Clear selected objects"));
if (!plain_tool->selection || !gwy_selection_get_data(plain_tool->selection, NULL))
gtk_widget_set_sensitive(plain_tool->clear, FALSE);
return plain_tool->clear;
}
static void
gwy_plain_tool_response(GwyTool *tool,
gint response_id)
{
GwyPlainTool *plain_tool;
plain_tool = GWY_PLAIN_TOOL(tool);
if (response_id == GWY_TOOL_RESPONSE_CLEAR && plain_tool->clear) {
gwy_selection_clear(plain_tool->selection);
}
}
/**
* gwy_plain_tool_get_z_average:
* @data_field: A data field.
* @point: Real X and Y-coordinate of area center in physical units.
* @radius: Area radius in pixels, 1 means a signle pixel. The actual radius passed to
* gwy_data_field_circular_area_extract() is @radius-0.5.
*
* Computes average value over a part of data field @dfield.
*
* It is not an error if part of it lies outside the data field borders (it is simply not counted in), however the
* intersection have to be nonempty.
*
* Returns: The average value.
**/
gdouble
gwy_plain_tool_get_z_average(GwyDataField *data_field,
const gdouble *point,
gint radius)
{
gint col, row, n, i;
gdouble *values;
gdouble avg;
g_return_val_if_fail(GWY_IS_DATA_FIELD(data_field), 0.0);
g_return_val_if_fail(point, 0.0);
g_return_val_if_fail(radius > 0, 0.0);
col = gwy_data_field_rtoj(data_field, point[0]);
row = gwy_data_field_rtoi(data_field, point[1]);
if (radius == 1)
return gwy_data_field_get_val(data_field, col, row);
values = g_new(gdouble, (2*radius + 1)*(2*radius + 1));
n = gwy_data_field_circular_area_extract(data_field, col, row, radius - 0.5, values);
avg = 0.0;
if (n) {
for (i = 0; i < n; i++)
avg += values[i];
avg /= n;
}
else
g_warning("Z average calculated from an empty area");
g_free(values);
return avg;
}
static gboolean
gwy_plain_tool_delete_selection_object(GtkTreeView *treeview,
GdkEventKey *event,
GwyPlainTool *plain_tool)
{
GtkTreeSelection *selection;
GtkTreeModel *model;
GtkTreePath *path;
GtkTreeIter iter;
const gint *indices;
if (event->keyval != GDK_Delete)
return FALSE;
selection = gtk_tree_view_get_selection(treeview);
if (!gtk_tree_selection_get_selected(selection, &model, &iter))
return FALSE;
path = gtk_tree_model_get_path(model, &iter);
indices = gtk_tree_path_get_indices(path);
gwy_selection_delete_object(plain_tool->selection, indices[0]);
gtk_tree_path_free(path);
return TRUE;
}
/**
* gwy_plain_tool_enable_object_deletion:
* @plain_tool: A plain tool.
* @treeview: A tree view that displays selection objects in order, each row corresponding to one selection object.
*
* Enables deletion of selection objects by presssing Delete in a tree view
* diplaying them.
*
* Since: 2.7
**/
void
gwy_plain_tool_enable_object_deletion(GwyPlainTool *plain_tool,
GtkTreeView *treeview)
{
g_return_if_fail(GTK_IS_TREE_VIEW(treeview));
g_return_if_fail(GWY_IS_PLAIN_TOOL(plain_tool));
g_signal_connect(treeview, "key-press-event", G_CALLBACK(gwy_plain_tool_delete_selection_object), plain_tool);
}
static GtkLabel*
gwy_rect_selection_labels_make_rlabel(GtkTable *table,
gint col, gint row)
{
GtkLabel *label;
label = GTK_LABEL(gtk_label_new(NULL));
gtk_label_set_single_line_mode(label, TRUE);
gtk_label_set_width_chars(label, 10);
gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
gtk_table_attach(table, GTK_WIDGET(label), col, col+1, row, row+1, GTK_EXPAND | GTK_FILL, 0, 0, 0);
return label;
}
static GtkSpinButton*
gwy_rect_selection_labels_make_pspin(GtkTable *table,
gint col, gint row,
gboolean from_1,
GtkWidget **px)
{
GtkWidget *spin, *label, *hbox;
GtkObject *adj;
if (from_1)
adj = gtk_adjustment_new(1.0, 1.0, 100.0, 1.0, 10.0, 0.0);
else
adj = gtk_adjustment_new(0.0, 0.0, 100.0, 1.0, 10.0, 0.0);
spin = gtk_spin_button_new(GTK_ADJUSTMENT(adj), 0.0, 0);
gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(spin), TRUE);
gtk_entry_set_width_chars(GTK_ENTRY(spin), 4);
hbox = gtk_hbox_new(FALSE, 4);
label = gtk_label_new(_("px"));
gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
gtk_box_pack_end(GTK_BOX(hbox), spin, FALSE, FALSE, 0);
gtk_table_attach(table, hbox, col, col+1, row, row+1, GTK_EXPAND | GTK_FILL, 0, 0, 0);
if (px)
*px = label;
return GTK_SPIN_BUTTON(spin);
}
/**
* gwy_rect_selection_labels_new:
* @none_is_full: %TRUE to treat unselected state as full data selected.
* @callback: Callback to call when the selection is edited.
* @cbdata: Data to pass to @callback (as the first argument).
*
* Creates a table displaying rectangular selection information.
*
* The returned object will destroy itself when the table is destroyed.
*
* Returns: The newly created rectangular selection information, as an opaque pointer. The table widget can be
* obtained with gwy_rect_selection_labels_get_table().
**/
GwyRectSelectionLabels*
gwy_rect_selection_labels_new(gboolean none_is_full,
GCallback callback,
gpointer cbdata)
{
GwyRectSelectionLabels *rlabels;
GtkTable *table;
GtkWidget *label, *px;
guint i;
if (!selection_type_rect) {
selection_type_rect = g_type_from_name("GwySelectionRectangle");
g_return_val_if_fail(selection_type_rect, FALSE);
}
if (!callback)
g_warning("Without a callback, reverse updates will hardly work");
rlabels = g_new(GwyRectSelectionLabels, 1);
rlabels->none_is_full = none_is_full;
rlabels->callback = callback;
rlabels->cbdata = cbdata;
rlabels->in_update = TRUE;
rlabels->table = gtk_table_new(6, 3, FALSE);
table = GTK_TABLE(rlabels->table);
gtk_table_set_col_spacings(table, 8);
gtk_table_set_row_spacings(table, 2);
gtk_container_set_border_width(GTK_CONTAINER(table), 4);
g_signal_connect_swapped(table, "destroy", G_CALLBACK(g_free), rlabels);
/* TRANSLATORS: Origin of the selected rectangle. */
label = gwy_label_new_header(_("Origin"));
gtk_table_attach(table, label, 0, 1, 0, 1, GTK_EXPAND | GTK_FILL, 0, 0, 0);
label = gtk_label_new("X");
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
gtk_table_attach(table, label, 0, 1, 1, 2, GTK_EXPAND | GTK_FILL, 0, 0, 0);
label = gtk_label_new("Y");
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
gtk_table_attach(table, label, 0, 1, 2, 3, GTK_EXPAND | GTK_FILL, 0, 0, 0);
label = gwy_label_new_header(_("Size"));
gtk_table_attach(table, label, 0, 1, 3, 4, GTK_EXPAND | GTK_FILL, 0, 0, 0);
label = gtk_label_new(_("Width"));
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
gtk_table_attach(table, label, 0, 1, 4, 5, GTK_EXPAND | GTK_FILL, 0, 0, 0);
label = gtk_label_new(_("Height"));
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
gtk_table_attach(table, label, 0, 1, 5, 6, GTK_EXPAND | GTK_FILL, 0, 0, 0);
for (i = 0; i < NRLABELS; i++) {
rlabels->real[i] = gwy_rect_selection_labels_make_rlabel(table, 1, i+1 + i/2);
rlabels->pix[i] = gwy_rect_selection_labels_make_pspin(table, 2, i+1 + i/2, i/2, &px);
rlabels->px[i] = px;
g_signal_connect(rlabels->pix[i], "value-changed", G_CALLBACK(gwy_rect_selection_labels_spinned), rlabels);
}
gwy_rect_selection_labels_set_sensitive(rlabels, FALSE);
rlabels->in_update = FALSE;
return rlabels;
}
/**
* gwy_rect_selection_labels_get_table:
* @rlabels: Rectangular selection information table.
*
* Gets the table widget of a rectangular selection information.
*
* Returns: The table as a #GtkWidget.
**/
GtkWidget*
gwy_rect_selection_labels_get_table(GwyRectSelectionLabels *rlabels)
{
return rlabels->table;
}
static void
gwy_rect_selection_labels_spinned(GtkSpinButton *spin,
GwyRectSelectionLabels *rlabels)
{
GtkAdjustment *adj;
gint val;
if (rlabels->in_update)
return;
rlabels->in_update = TRUE;
adj = gtk_spin_button_get_adjustment(spin);
val = gwy_adjustment_get_int(adj);
if (spin == rlabels->pix[RLABEL_X])
gtk_spin_button_set_range(rlabels->pix[RLABEL_W], 2.0, adj->upper + 1.0 - val);
else if (spin == rlabels->pix[RLABEL_Y])
gtk_spin_button_set_range(rlabels->pix[RLABEL_H], 2.0, adj->upper + 1.0 - val);
else if (spin == rlabels->pix[RLABEL_W]) {
}
else if (spin == rlabels->pix[RLABEL_H]) {
}
else
g_critical("Stray spin button signal");
if (rlabels->callback) {
GwyRectSelectionLabelsFunc func;
func = (GwyRectSelectionLabelsFunc)rlabels->callback;
func(rlabels->cbdata);
}
rlabels->in_update = FALSE;
}
/**
* gwy_rect_selection_labels_select:
* @rlabels: Rectangular selection information table.
* @selection: A rectangular selection to fill information into.
* @dfield: A data field to use for real/pixel coordinate transforms.
*
* Updates selection data using rectangular selection information table.
**/
void
gwy_rect_selection_labels_select(GwyRectSelectionLabels *rlabels,
GwySelection *selection,
GwyDataField *dfield)
{
GtkAdjustment *adj;
gint isel[4];
gdouble sel[4];
guint i;
g_return_if_fail(!dfield || GWY_IS_DATA_FIELD(dfield));
g_return_if_fail(!selection || g_type_is_a(G_TYPE_FROM_INSTANCE(selection), selection_type_rect));
if (!GTK_WIDGET_SENSITIVE(rlabels->pix[0])) {
g_warning("Nothing is selected");
return;
}
rlabels->in_update = TRUE;
for (i = 0; i < NRLABELS; i++) {
adj = gtk_spin_button_get_adjustment(rlabels->pix[i]);
isel[i] = gwy_adjustment_get_int(adj);
}
if (isel[2] && isel[3]) {
isel[2] += isel[0] - 1;
isel[3] += isel[1] - 1;
sel[0] = gwy_data_field_jtor(dfield, isel[0] + 0.5);
sel[1] = gwy_data_field_itor(dfield, isel[1] + 0.5);
sel[2] = gwy_data_field_jtor(dfield, isel[2] + 0.5);
sel[3] = gwy_data_field_itor(dfield, isel[3] + 0.5);
gwy_selection_set_data(selection, 1, sel);
}
else
gwy_selection_clear(selection);
rlabels->in_update = FALSE;
}
/**
* gwy_rect_selection_labels_fill:
* @rlabels: Rectangular selection info table.
* @selection: A rectangular selection to fill information from. It can be %NULL to clear the labels.
* @dfield: A data field to use for real/pixel coordinate transforms. It can be %NULL to clear the labels.
* @selreal: If not %NULL, must be an array of size at least 4 and will be filled with selection data xmin, ymin,
* xmax, ymax in physical units.
* @selpix: If not %NULL, must be an array of size at least 4 and will be filled with selection data xmin, ymin, xmax,
* ymax in pixels.
*
* Updates rectangular selection info display.
*
* It is possible to pass %NULL @dfield but non-%NULL @selection. This can lead to %TRUE return value (if the
* selection is non-empty), but the labels will be still cleared as there is no way to convert between real and pixel
* coordinates.
*
* Returns: %TRUE if a selection is present, %FALSE otherwise.
**/
gboolean
gwy_rect_selection_labels_fill(GwyRectSelectionLabels *rlabels,
GwySelection *selection,
GwyDataField *dfield,
gdouble *selreal,
gint *selpix)
{
GwySIValueFormat *vf;
gdouble sel[4];
gint isel[4];
static gchar buffer[64];
gdouble xoff, yoff;
gboolean is_selected;
GtkAdjustment *adj;
gint xres, yres;
guint i;
g_return_val_if_fail(!dfield || GWY_IS_DATA_FIELD(dfield), FALSE);
g_return_val_if_fail(!selection || g_type_is_a(G_TYPE_FROM_INSTANCE(selection), selection_type_rect), FALSE);
rlabels->in_update = TRUE;
is_selected = selection && gwy_selection_get_object(selection, 0, sel);
if (!dfield || (!is_selected && !rlabels->none_is_full)) {
gwy_rect_selection_labels_set_sensitive(rlabels, FALSE);
for (i = 0; i < NRLABELS; i++) {
gtk_label_set_text(rlabels->real[i], "");
gtk_spin_button_set_value(rlabels->pix[i], 0.0);
}
rlabels->in_update = FALSE;
return is_selected;
}
xres = gwy_data_field_get_xres(dfield);
yres = gwy_data_field_get_yres(dfield);
xoff = gwy_data_field_get_xoffset(dfield);
yoff = gwy_data_field_get_yoffset(dfield);
if (is_selected) {
GWY_ORDER(gdouble, sel[0], sel[2]);
GWY_ORDER(gdouble, sel[1], sel[3]);
isel[0] = gwy_data_field_rtoj(dfield, sel[0]);
isel[1] = gwy_data_field_rtoi(dfield, sel[1]);
isel[2] = gwy_data_field_rtoj(dfield, sel[2]);
isel[3] = gwy_data_field_rtoi(dfield, sel[3]);
}
else {
sel[0] = sel[1] = 0.0;
sel[2] = gwy_data_field_get_xreal(dfield);
sel[3] = gwy_data_field_get_yreal(dfield);
isel[0] = isel[1] = 0;
isel[2] = xres-1;
isel[3] = yres-1;
}
sel[0] = gwy_data_field_jtor(dfield, isel[0]);
sel[1] = gwy_data_field_itor(dfield, isel[1]);
sel[2] = gwy_data_field_jtor(dfield, isel[2]+1);
sel[3] = gwy_data_field_itor(dfield, isel[3]+1);
/* Provide sorted real and pixel selection to the caller; but before we add offsets and convert to position
* & size. The documentation has always said min and max pixel coordinates, so we must have res-1 there for
* full-size selections. */
if (selreal)
gwy_assign(selreal, sel, 4);
if (selpix)
gwy_assign(selpix, isel, 4);
isel[2] -= isel[0] - 1;
isel[3] -= isel[1] - 1;
sel[2] -= sel[0];
sel[3] -= sel[1];
sel[0] += xoff;
sel[1] += yoff;
vf = gwy_data_field_get_value_format_xy(dfield, GWY_SI_UNIT_FORMAT_VFMARKUP, NULL);
for (i = 0; i < NRLABELS; i++) {
g_snprintf(buffer, sizeof(buffer), "%.*f%s%s",
vf->precision, sel[i]/vf->magnitude,
*vf->units ? " " : "", vf->units);
gtk_label_set_markup(rlabels->real[i], buffer);
}
gwy_si_unit_value_format_free(vf);
gtk_spin_button_set_range(rlabels->pix[RLABEL_X], 0.0, xres - 1.0);
gtk_spin_button_set_range(rlabels->pix[RLABEL_W], 2.0, xres - isel[0]);
gtk_spin_button_set_range(rlabels->pix[RLABEL_Y], 0.0, yres - 1.0);
gtk_spin_button_set_range(rlabels->pix[RLABEL_H], 2.0, yres - isel[1]);
for (i = 0; i < NRLABELS; i++) {
gwy_rect_selection_labels_set_sensitive(rlabels, TRUE);
adj = gtk_spin_button_get_adjustment(rlabels->pix[i]);
gtk_adjustment_set_value(adj, isel[i]);
}
rlabels->in_update = FALSE;
return is_selected;
}
static void
gwy_rect_selection_labels_set_sensitive(GwyRectSelectionLabels *rlabels,
gboolean sensitive)
{
guint i;
for (i = 0; i < NRLABELS; i++) {
gtk_widget_set_sensitive(GTK_WIDGET(rlabels->pix[i]), sensitive);
gtk_widget_set_sensitive(rlabels->px[i], sensitive);
}
}
/**
* gwy_plain_tool_log_add:
* @plain_tool: A plain tool.
*
* Logs a data modification operation of a plain tool.
*
* This is a convenience wrapper for gwy_app_channel_log_add(), setting automatically the container, data id and
* function name from the tool. It can only be used for simple data modification operations in which the source and
* target ids are the same. In more complex cases you have to use gwy_app_channel_log_add() directly.
*
* Since: 2.35
**/
void
gwy_plain_tool_log_add(GwyPlainTool *plain_tool)
{
gchar *qualname;
g_return_if_fail(GWY_IS_PLAIN_TOOL(plain_tool));
g_return_if_fail(plain_tool->container);
qualname = g_strconcat("tool::", G_OBJECT_TYPE_NAME(plain_tool), NULL);
gwy_app_channel_log_add(plain_tool->container,
plain_tool->id, plain_tool->id,
qualname,
NULL);
}
/**
* gwy_plain_tool_add_param_table:
* @plain_tool: A plain tool.
* @partable: Parameter table to add.
*
* Registers a parameter table with a data processing module dialog window.
*
* See gwy_dialog_add_param_table() for details.
*
* Since: 2.62
**/
void
gwy_plain_tool_add_param_table(GwyPlainTool *plain_tool,
GwyParamTable *partable)
{
GPtrArray *tables;
guint i, n;
g_return_if_fail(GWY_IS_PLAIN_TOOL(plain_tool));
g_return_if_fail(GWY_IS_PARAM_TABLE(partable));
if (!param_table_param_changed)
param_table_param_changed = g_signal_lookup("param-changed", GWY_TYPE_PARAM_TABLE);
tables = plain_tool->param_tables;
n = tables->len;
for (i = 0; i < n; i++) {
GwyParamTable *othertable = g_ptr_array_index(tables, i);
if (othertable == partable) {
g_warning("Parameter table is already present in dialog.");
return;
}
}
g_object_ref_sink(partable);
g_ptr_array_add(tables, partable);
/* XXX: This function should not be called inside param change handlers. If it ever does we need to introduce
* the complicated in_update counting as in GwyDialog. */
_gwy_param_table_set_parent_plain_tool(partable, plain_tool);
}
void
_gwy_plain_tool_param_table_update_started(GwyPlainTool *plain_tool)
{
GPtrArray *tables = plain_tool->param_tables;
guint i, n = tables->len;
for (i = 0; i < n; i++)
_gwy_param_table_in_update((GwyParamTable*)g_ptr_array_index(tables, i), TRUE);
}
void
_gwy_plain_tool_param_table_update_finished(GwyPlainTool *plain_tool)
{
GPtrArray *tables = plain_tool->param_tables;
guint i, n = tables->len;
for (i = 0; i < n; i++)
_gwy_param_table_in_update((GwyParamTable*)g_ptr_array_index(tables, i), FALSE);
}
/************************** Documentation ****************************/
/**
* SECTION:gwyplaintool
* @title: GwyPlainTool
* @short_description: Base class for simple tools
**/
/**
* GwyPlainToolChanged:
* @GWY_PLAIN_TOOL_CHANGED_DATA: Channel data field was changed.
* @GWY_PLAIN_TOOL_CHANGED_MASK: Mask data field was changed.
* @GWY_PLAIN_TOOL_CHANGED_SHOW: Presentation data field was changed.
* @GWY_PLAIN_TOOL_CHANGED_SELECTION: Selection was changed.
* @GWY_PLAIN_TOOL_FINISHED_SELECTION: Selection was finished (that is emitted #GwySelection::finished).
*
* The type of pending changes that accumulated during tool inactivity.
**/
/* vim: set cin columns=120 tw=118 et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */
|