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 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
|
/*
* $Id: param-def.c 25075 2022-10-11 13:47:20Z yeti-dn $
* Copyright (C) 2021-2022 David Necas (Yeti).
* E-mail: yeti@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 "libgwyddion/gwymacros.h"
#include "libprocess/gwyprocesstypes.h"
#include "libprocess/gwyprocessenums.h"
#include "param-def.h"
#include "param-internal.h"
#define DEBUG_USERS
typedef struct _GwyParamDefPrivate GwyParamDefPrivate;
struct _GwyParamDefPrivate {
const gchar *function_name;
GwyParamDefItem *defs;
gint *ids;
guint ndefs;
guint nalloc;
gint maxid; /* Keeping maximum used id makes typical param construction linear-time. */
gboolean is_used;
#ifdef DEBUG_USERS
GSList *users;
#endif
};
static void gwy_param_def_finalize (GObject *gobject);
static void gwy_param_def_append (GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
GwyParamDefItem *item);
static void gwy_param_def_append_enum (GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
GType gtype,
const GwyEnum *values,
gint nvalues,
gint default_value);
static void gwy_param_def_append_flags (GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
GType gtype,
const GwyEnum *values,
gint nvalues,
guint default_value);
static gchar* rectify_string (const gchar *value,
GwyParamStringFlags flags,
GwyRectifyStringFunc rectify);
static gint find_param_def (GwyParamDefPrivate *priv,
gint id);
G_DEFINE_TYPE(GwyParamDef, gwy_param_def, G_TYPE_OBJECT);
static void
gwy_param_def_class_init(GwyParamDefClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
gobject_class->finalize = gwy_param_def_finalize;
g_type_class_add_private(klass, sizeof(GwyParamDefPrivate));
}
static void
gwy_param_def_init(GwyParamDef *pardef)
{
GwyParamDefPrivate *priv;
pardef->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE(pardef, GWY_TYPE_PARAM_DEF, GwyParamDefPrivate);
priv->maxid = -1;
}
static void
gwy_param_def_finalize(GObject *gobject)
{
GwyParamDef *pardef = (GwyParamDef*)gobject;
GwyParamDefPrivate *priv = pardef->priv;
guint i;
for (i = 0; i < priv->ndefs; i++) {
if (priv->defs[i].type == GWY_PARAM_STRING)
g_free(priv->defs[i].def.s.default_value);
}
g_free(priv->ids);
g_free(priv->defs);
G_OBJECT_CLASS(gwy_param_def_parent_class)->finalize(gobject);
}
/**
* gwy_param_def_new:
*
* Creates a new empty set of parameter definitions.
*
* Definitions can be added only during construction, i.e. until the first time it is used to create #GwyParams. Then
* the definition set must be considered immutable.
*
* Returns: A newly create paramete definition set.
*
* Since: 2.59
**/
GwyParamDef*
gwy_param_def_new(void)
{
return g_object_new(GWY_TYPE_PARAM_DEF, NULL);
}
/**
* gwy_param_def_set_function_name:
* @pardef: A set of parameter definitions.
* @name: Settings function name, or %NULL.
*
* Sets the settings function name of a set of parameter definitions.
*
* Usually the function name should be given simply as gwy_process_func_current() (or analogous functions for other
* module types). The exception is functions with settings keys different from module function name. If the name is
* unset then #GwyParams settings functions like gwy_params_new_from_settings() cannot be used.
*
* The function name can be set freely even after construction. This allows having just a single set of parameter
* definitions for multiple related module functions which all have the same parameters.
*
* Since: 2.59
**/
void
gwy_param_def_set_function_name(GwyParamDef *pardef,
const gchar *name)
{
g_return_if_fail(GWY_IS_PARAM_DEF(pardef));
pardef->priv->function_name = name;
}
/**
* gwy_param_def_get_function_name:
* @pardef: A set of parameter definitions.
*
* Gets the settings function name of a set of parameter definitions.
*
* Returns: The functions name (possibly %NULL).
*
* Since: 2.59
**/
const gchar*
gwy_param_def_get_function_name(GwyParamDef *pardef)
{
g_return_val_if_fail(GWY_IS_PARAM_DEF(pardef), NULL);
return pardef->priv->function_name;
}
/**
* gwy_param_def_get_param_name:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
*
* Gets the name of an already defined parameter in a set of parameter definitions.
*
* See gwy_param_def_get_param_label() for parameter labels.
*
* Returns: The parameter name, as a string owned by @pardef. It may be %NULL if the parameter is not stored to
* settings.
*
* Since: 2.62
**/
const gchar*
gwy_param_def_get_param_name(GwyParamDef *pardef,
gint id)
{
const GwyParamDefItem *item;
g_return_val_if_fail(GWY_IS_PARAM_DEF(pardef), NULL);
item = _gwy_param_def_item(pardef, _gwy_param_def_index(pardef, id));
g_return_val_if_fail(item, NULL);
return item->name;
}
/**
* gwy_param_def_get_param_label:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
*
* Gets the label of an already defined parameter in a set of parameter definitions.
*
* See gwy_param_def_get_param_name() for parameter names (keys).
*
* Returns: The parameter name, as a string owned by @pardef. It may be %NULL if the parameter has no label.
*
* Since: 2.62
**/
const gchar*
gwy_param_def_get_param_label(GwyParamDef *pardef,
gint id)
{
const GwyParamDefItem *item;
g_return_val_if_fail(GWY_IS_PARAM_DEF(pardef), NULL);
item = _gwy_param_def_item(pardef, _gwy_param_def_index(pardef, id));
g_return_val_if_fail(item, NULL);
return item->desc;
}
static gint
count_enum_values(const GwyEnum *values, gint nvalues)
{
if (nvalues < 0) {
nvalues = 0;
while (values[nvalues].name)
nvalues++;
}
return nvalues;
}
static gint
find_enum_value(const GwyEnum *values, gint nvalues, gint value)
{
gint i;
for (i = 0; i < nvalues; i++) {
if (values[i].value == value)
return i;
}
return -1;
}
/**
* gwy_param_def_add_gwyenum:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for the list header label in GUI. May be also %NULL for no label.
* @values: An array of corresponding string-integer pairs.
* @nvalues: The number of @nvalues items. It is possible to pass -1 to count the values automatically if the array
* is terminated by %NULL name.
* @default_value: Default value of the parameter.
*
* Defines a new parameter with enumerated values defined by a #GwyEnum.
*
* See the introduction for @name and @desc lifetime rules.
*
* See gwy_param_def_add_enum() for predefined enums.
*
* Since: 2.59
**/
void
gwy_param_def_add_gwyenum(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
const GwyEnum *values,
gint nvalues,
gint default_value)
{
gwy_param_def_append_enum(pardef, id, name, desc, 0, values, nvalues, default_value);
}
/**
* gwy_param_def_add_gwyflags:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for the list header label in GUI. May be also %NULL for no label.
* @values: An array of corresponding string-integer pairs.
* @nvalues: The number of @nvalues items. It is possible to pass -1 to count the values automatically if the array
* is terminated by %NULL name.
* @default_value: Default value of the parameter.
*
* Defines a new parameter with bit flag values defined by a #GwyEnum.
*
* The values in @values must represent a set of flags, i.e. numbers with exactly one 1-bit in binary.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_gwyflags(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
const GwyEnum *values,
gint nvalues,
guint default_value)
{
gwy_param_def_append_flags(pardef, id, name, desc, 0, values, nvalues, default_value);
}
/**
* gwy_param_def_add_enum:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI. May be %NULL for the default label.
* @enum_gtype: Type of the enum, for instance %GWY_TYPE_MASKING_TYPE.
* @default_value: Default value of the parameter.
*
* Defines a new parameter with enumerated values from a standard Gwyddion enum.
*
* Supported enums include #GwyMaskingType, #GwyInterpolationType, #GwyOrientation, #GwyMergeType,
* #GwyDistanceTransformType, #GwyWindowingType. They have standard labels allowing to pass %NULL as @desc.
*
* See the introduction for @name and @desc lifetime rules.
*
* See gwy_param_def_add_gwyenum() for enums defined by a #GwyEnum table.
*
* Since: 2.59
**/
void
gwy_param_def_add_enum(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
GType enum_gtype,
gint default_value)
{
static const GwyEnum dummy_enum[] = { { "???", 0 }, { NULL, 0 } };
const GwyEnum *values = NULL;
const gchar *standard_desc = NULL;
g_return_if_fail(enum_gtype);
if (enum_gtype == GWY_TYPE_MASKING_TYPE) {
values = gwy_masking_type_get_enum();
standard_desc = _("_Masking");
}
else if (enum_gtype == GWY_TYPE_INTERPOLATION_TYPE) {
values = gwy_interpolation_type_get_enum();
standard_desc = _("_Interpolation type");
}
else if (enum_gtype == GWY_TYPE_ORIENTATION) {
values = gwy_orientation_get_enum();
standard_desc = _("_Direction");
}
else if (enum_gtype == GWY_TYPE_MERGE_TYPE) {
values = gwy_merge_type_get_enum();
standard_desc = _("Combine with existing mask");
}
else if (enum_gtype == GWY_TYPE_WINDOWING_TYPE) {
values = gwy_windowing_type_get_enum();
standard_desc = _("_Windowing type");
}
else if (enum_gtype == GWY_TYPE_DISTANCE_TRANSFORM_TYPE) {
values = gwy_distance_transform_type_get_enum();
standard_desc = _("_Distance type");
}
else if (enum_gtype == GWY_TYPE_GRAPH_CURVE_TYPE) {
values = gwy_graph_curve_type_get_enum();
standard_desc = _("Plot _style");
}
else {
/* We could play with GEnumClass here but why. It should be implemented if it is used. */
standard_desc = g_type_name(enum_gtype);
g_warning("Enum %s is unimplemented. Should be?", standard_desc);
values = dummy_enum;
}
gwy_param_def_append_enum(pardef, id, name, desc ? desc : standard_desc, enum_gtype, values, -1, default_value);
}
static void
gwy_param_def_append_enum(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
GType gtype,
const GwyEnum *values,
gint nvalues,
gint default_value)
{
GwyParamDefItem item;
GwyParamDefEnum *e = &item.def.e;
if (!nvalues || !values) {
g_warning("Enum param %s (%s) has no values.", name ? name : "???", desc ? desc : "");
nvalues = 0;
values = NULL;
}
/* XXX: We might want to check that GType values match GwyEnum values. But as long as the only use of the
* functions is internal then it is probably unnecessary. */
item.type = GWY_PARAM_ENUM;
e->gtype = gtype;
e->table = values;
e->nvalues = nvalues = count_enum_values(values, nvalues);
e->default_value_index = find_enum_value(values, nvalues, default_value);
if (e->default_value_index < 0) {
g_warning("Enum param %s (%s) default value %d is not in the enum.",
name ? name : "???", desc ? desc : "", default_value);
e->default_value_index = 0;
}
gwy_param_def_append(pardef, id, name, desc, &item);
}
static void
gwy_param_def_append_flags(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
GType gtype,
const GwyEnum *values,
gint nvalues,
guint default_value)
{
GwyParamDefItem item;
GwyParamDefFlags *f = &item.def.f;
guint i, b, allset;
gboolean warned = FALSE;
if (!nvalues || !values) {
g_warning("Flags param %s (%s) has no values.", name ? name : "???", desc ? desc : "");
nvalues = 0;
values = NULL;
}
/* XXX: We might want to check that GType values match GwyEnum values. But as long as the only use of the
* functions is internal then it is probably unnecessary. */
item.type = GWY_PARAM_FLAGS;
f->gtype = gtype;
f->table = values;
f->nvalues = nvalues = count_enum_values(values, nvalues);
allset = 0;
for (i = 0; i < nvalues; i++) {
b = values[i].value;
if (b & (b - 1)) {
/* Can't fix this one. */
g_warning("Flags param %s (%s) flag %u does not have exactly 1 bit set.",
name ? name : "???", desc ? desc : "", values[i].value);
warned = TRUE;
}
allset |= b;
}
f->allset = allset;
if (!warned) {
/* Count set bits (Brian Kernighan's). */
b = 0;
while (allset) {
allset &= allset - 1;
b++;
}
if (b != nvalues) {
/* Can't fix this one. */
g_warning("Flags param %s (%s) value bits are not independent.",
name ? name : "???", desc ? desc : "");
}
}
f->default_value = _gwy_param_def_rectify_flags(&item, default_value);
if (f->default_value != default_value) {
g_warning("Flags param %s (%s) default value %u has bits not among the flags.",
name ? name : "???", desc ? desc : "", default_value);
}
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_int:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI.
* @minimum: Minimum allowed value (inclusive).
* @maximum: Maximum allowed value (inclusive).
* @default_value: Default value of the parameter.
*
* Defines a new parameter with integer values.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_int(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
gint minimum,
gint maximum,
gint default_value)
{
GwyParamDefItem item;
GwyParamDefInt *i = &item.def.i;
if (minimum > maximum) {
g_warning("Int param %s (%s) has minimum > maximum (%d > %d).",
name ? name : "???", desc ? desc : "", minimum, maximum);
GWY_SWAP(gint, minimum, maximum);
}
item.type = GWY_PARAM_INT;
i->minimum = minimum;
i->maximum = maximum;
i->default_value = _gwy_param_def_rectify_int(&item, default_value);
if (i->default_value != default_value) {
g_warning("Int param %s (%s) default value %d is out of range [%d..%d].",
name ? name : "???", desc ? desc : "", default_value, minimum, maximum);
}
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_active_page:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL if not stored in settings, but then the active page parameter
* would have no reason to exist.
* @desc: Parameter description, usually %NULL.
*
* Defines a new parameter with integer values representing module dialog active page.
*
* The active page parameter can be connected to a #GtkNotebook using the utility function
* gwy_param_active_page_link_to_notebook(). This is also the only function that should be used with such parameter.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_active_page(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc)
{
GwyParamDefItem item;
item.type = GWY_PARAM_ACTIVE_PAGE;
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_boolean:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI.
* @default_value: Default value of the parameter.
*
* Defines a new parameter with boolean values.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_boolean(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
gboolean default_value)
{
GwyParamDefItem item;
GwyParamDefBoolean *b = &item.def.b;
b->default_value = !!default_value;
b->is_instant_updates = FALSE;
b->seed_id = -1;
item.type = GWY_PARAM_BOOLEAN;
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_instant_updates:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI. Usually %NULL for the default label.
* @default_value: Default value of the parameter.
*
* Defines a new boolean parameter representing the instant updates option.
*
* Compared to a plain boolean, a parameter defined using this function has a predefined label and is recognised by
* #GwyDialog. Therefore, it is not necessary to call gwy_dialog_set_instant_updates_param(). If your module GUI
* needs non-standard update settings then define the parameters manually.
*
* Furthermore, it is normally not necessary (in fact, counterproductive) to call gwy_dialog_invalidate() when the
* instant updates parameter changes. Recalculation is only necessary when instant updates were just switched on
* and the current preview is no longer valid. #GwyDialog handles this case automatically.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_instant_updates(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
gboolean default_value)
{
GwyParamDefItem item;
GwyParamDefBoolean *b = &item.def.b;
b->default_value = !!default_value;
b->is_instant_updates = TRUE;
b->seed_id = -1;
if (!desc)
desc = _("I_nstant updates");
item.type = GWY_PARAM_BOOLEAN;
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_randomize:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @seed_id: Identifier of the corresponding random seed parameter. It must already be defined.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI. Usually %NULL for the default label.
* @default_value: Default value of the parameter.
*
* Defines a new boolean parameter representing the randomize option for a random seed.
*
* See gwy_param_def_add_seed() for random seed parameters.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_randomize(GwyParamDef *pardef,
gint id,
gint seed_id,
const gchar *name,
const gchar *desc,
gboolean default_value)
{
GwyParamDefPrivate *priv = pardef->priv;
GwyParamDefItem item;
GwyParamDefBoolean *b = &item.def.b;
GwyParamDefRandomSeed *rs;
gint i;
item.type = GWY_PARAM_BOOLEAN;
b->default_value = !!default_value;
b->is_instant_updates = FALSE;
b->seed_id = seed_id;
i = find_param_def(priv, seed_id);
g_assert(i >= 0);
rs = &priv->defs[i].def.rs;
g_assert(rs->randomize_id < 0);
rs->randomize_id = id;
if (!desc)
desc = _("Randomi_ze");
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_double:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI.
* @minimum: Minimum allowed value (inclusive).
* @maximum: Maximum allowed value (inclusive).
* @default_value: Default value of the parameter.
*
* Defines a new parameter with floating point values.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_double(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
gdouble minimum,
gdouble maximum,
gdouble default_value)
{
GwyParamDefItem item;
GwyParamDefDouble *d = &item.def.d;
gwy_clear(d, 1);
if (minimum > maximum) {
g_warning("Double param %s (%s) has minimum > maximum (%.14g > %.14g).",
name ? name : "???", desc ? desc : "", minimum, maximum);
GWY_SWAP(gdouble, minimum, maximum);
}
item.type = GWY_PARAM_DOUBLE;
d->minimum = minimum;
d->maximum = maximum;
d->default_value = _gwy_param_def_rectify_double(&item, default_value);
if (d->default_value != default_value) {
g_warning("Double param %s (%s) default value %.14g is out of range [%.14g..%.14g].",
name ? name : "???", desc ? desc : "", default_value, minimum, maximum);
}
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_angle:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI.
* @positive: %TRUE for positive angles, %FALSE for angles symmetrical around zero.
* @folding: Fraction of the range [0,2π] which should be the range.
* @default_value: Default value of the parameter.
*
* Defines a new parameter with floating point values representing angles.
*
* Percentages do not have to be defined using this function. You can also set up the conversion factor and unit
* string directly using gwy_param_table_slider_set_factor() and gwy_param_table_set_unitstr() when constructing the
* parameter table (this happens automatically for percentage parameters). This function may be somewhat more
* convenient and it also ensures correct folding when loading angle from settings.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_angle(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
gboolean positive,
gint folding,
gdouble default_value)
{
GwyParamDefItem item;
GwyParamDefDouble *d = &item.def.d;
gwy_clear(d, 1);
item.type = GWY_PARAM_DOUBLE;
if (!folding || folding > 12) {
g_warning("Wrong folding value %d.", folding);
folding = 1;
}
if (positive) {
d->minimum = 0.0;
d->maximum = 2.0/folding*G_PI;
}
else {
d->maximum = G_PI/folding;
d->minimum = -d->maximum;
}
d->is_angle = TRUE;
d->angle_positive = positive;
d->angle_folding = folding;
d->default_value = _gwy_param_def_rectify_double(&item, default_value);
if (d->default_value != default_value) {
g_warning("Double param %s (%s) default value %.14g is out of range [%.14g..%.14g].",
name ? name : "???", desc ? desc : "", default_value, d->minimum, d->maximum);
}
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_percentage:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI.
* @default_value: Default value of the parameter.
*
* Defines a new parameter with floating point values representing fraction of some base value.
*
* The value range is [0,1], but it is displayed in percents in the GUI.
*
* Percentages do not have to be defined using this function. You can also set up the conversion factor and unit
* string directly using gwy_param_table_slider_set_factor() and gwy_param_table_set_unitstr() when constructing the
* parameter table (this happens automatically for percentage parameters). This function may be somewhat more
* convenient.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_percentage(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
gdouble default_value)
{
GwyParamDefItem item;
GwyParamDefDouble *d = &item.def.d;
gwy_clear(d, 1);
item.type = GWY_PARAM_DOUBLE;
d->is_percentage = TRUE;
d->minimum = 0.0;
d->maximum = 1.0;
d->default_value = _gwy_param_def_rectify_double(&item, default_value);
if (d->default_value != default_value) {
g_warning("Double param %s (%s) default value %.14g is out of range [%.14g..%.14g].",
name ? name : "???", desc ? desc : "", default_value, d->minimum, d->maximum);
}
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_mask_color:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. Usually it is %NULL because the colour is taken from data, not settings.
* @desc: Parameter description which will be used for a label in GUI. Usually %NULL for the default label.
*
* Defines a new mask colour parameter.
*
* This is usually a parameter not stored in settings, but handled specially by taking the mask colour from the image
* and then setting mask colours on the output. The default value is the default mask colour.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_mask_color(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc)
{
/* Use a fixed colour. ‘The current mask colour at the time the parameter was defined’ does not work as a good
* default value. The default colour must be handled properly later. */
const GwyRGBA default_mask_color = { 1.0, 0.0, 0.0, 0.5 };
GwyParamDefItem item;
GwyParamDefColor *c = &item.def.c;
if (!desc)
desc = _("_Mask color");
item.type = GWY_PARAM_COLOR;
c->is_mask = TRUE;
c->has_alpha = TRUE;
c->default_value = default_mask_color;
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_target_graph:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI. May be %NULL for the default label.
*
* Defines a new parameter with values that are graph ids.
*
* Data identifier parameters are not stored permanently in settings. However, they are remembered until Gwyddion
* exits. Hence @name does not actually refer to settings, but it should still uniquely identify the parameter as in
* settings.
*
* See the introduction for @name and @desc lifetime rules.
*
* See gwy_param_def_add_target_graph() for a generic graph parameter.
*
* Since: 2.59
**/
void
gwy_param_def_add_target_graph(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc)
{
GwyParamDefItem item;
GwyParamDefDataId *di = &item.def.di;
item.type = GWY_PARAM_GRAPH_ID;
di->is_target_graph = TRUE;
gwy_param_def_append(pardef, id, name, desc ? desc : _("Target _graph"), &item);
}
/**
* gwy_param_def_add_graph_id:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI.
*
* Defines a new parameter with values that are graph ids.
*
* Data identifier parameters are not stored permanently in settings. However, they are remembered until Gwyddion
* exits. Hence @name does not actually refer to settings, but it should still uniquely identify the parameter as in
* settings.
*
* See the introduction for @name and @desc lifetime rules.
*
* See gwy_param_def_add_target_graph() for a specialised parameter for target graphs.
*
* Since: 2.60
**/
void
gwy_param_def_add_graph_id(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc)
{
GwyParamDefItem item;
GwyParamDefDataId *di = &item.def.di;
item.type = GWY_PARAM_GRAPH_ID;
di->is_target_graph = FALSE;
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_image_id:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI.
*
* Defines a new parameter with values that are image ids.
*
* Data identifier parameters are not stored permanently in settings. However, they are remembered until Gwyddion
* exits. Hence @name does not actually refer to settings, but it should still uniquely identify the parameter as in
* settings.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_image_id(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc)
{
GwyParamDefItem item;
item.type = GWY_PARAM_IMAGE_ID;
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_volume_id:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI.
*
* Defines a new parameter with values that are volume data ids.
*
* Data identifier parameters are not stored permanently in settings. However, they are remembered until Gwyddion
* exits. Hence @name does not actually refer to settings, but it should still uniquely identify the parameter as in
* settings.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_volume_id(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc)
{
GwyParamDefItem item;
item.type = GWY_PARAM_VOLUME_ID;
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_xyz_id:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI.
*
* Defines a new parameter with values that are xyz data ids.
*
* Data identifier parameters are not stored permanently in settings. However, they are remembered until Gwyddion
* exits. Hence @name does not actually refer to settings, but it should still uniquely identify the parameter as in
* settings.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_xyz_id(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc)
{
GwyParamDefItem item;
item.type = GWY_PARAM_XYZ_ID;
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_curve_map_id:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI.
*
* Defines a new parameter with values that are curve map data ids.
*
* Data identifier parameters are not stored permanently in settings. However, they are remembered until Gwyddion
* exits. Hence @name does not actually refer to settings, but it should still uniquely identify the parameter as in
* settings.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.60
**/
void
gwy_param_def_add_curve_map_id(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc)
{
GwyParamDefItem item;
item.type = GWY_PARAM_CURVE_MAP_ID;
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_graph_curve:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings.
* @desc: Parameter description which will be used for a label in GUI. It may be %NULL for the default label.
*
* Defines a new parameter with values that are graph curve numbers.
*
* Curve numbers can be any non-negative integers. The upper bound is determined by #GwyParamTable once the GUI for
* choosing a curve from specific graph model is set up. Value -1 is also allowed for empty graph models with no
* curves, but this should not normally occur.
*
* Curve parameters are stored in settings as curve labels, even though they are treated integers otherwise. This
* helps selecting a curve with the same function/interpretation the next time the module is invoked – or the first
* curve if there is none such.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.60
**/
void
gwy_param_def_add_graph_curve(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc)
{
GwyParamDefItem item;
GwyParamDefInt *i = &item.def.i;
item.type = GWY_PARAM_GRAPH_CURVE;
i->minimum = -1;
i->maximum = G_MAXINT;
i->default_value = 0;
if (!desc)
desc = _("C_urve");
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_lawn_curve:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings.
* @desc: Parameter description which will be used for a label in GUI. It may be %NULL for the default label.
*
* Defines a new parameter with values that are lawn curve numbers.
*
* Curve numbers can be any non-negative integers. The upper bound is determined by #GwyParamTable once the GUI for
* choosing a curve from specific lawn is set up.
*
* Curve parameters are stored in settings as curve labels, even though they are treated integers otherwise. This
* helps selecting a curve with the same function/interpretation the next time the module is invoked – or the first
* curve if there is none such.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.60
**/
void
gwy_param_def_add_lawn_curve(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc)
{
GwyParamDefItem item;
GwyParamDefInt *i = &item.def.i;
item.type = GWY_PARAM_LAWN_CURVE;
i->minimum = 0;
i->maximum = G_MAXINT;
i->default_value = 0;
if (!desc)
desc = _("C_urve");
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_lawn_segment:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings.
* @desc: Parameter description which will be used for a label in GUI. It may be %NULL for the default label.
*
* Defines a new parameter with values that are lawn segment numbers.
*
* Segment numbers can be any non-negative integers. The upper bound is determined by #GwyParamTable once the GUI for
* choosing a segment from specific lawn is set up. Value -1 is also allowed for empty lawns with no segments.
*
* Segment parameters are stored in settings as segment labels, even though they are treated integers otherwise. This
* helps selecting a segment with the same function/interpretation the next time the module is invoked – or the first
* segment if there is none such.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.60
**/
void
gwy_param_def_add_lawn_segment(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc)
{
GwyParamDefItem item;
GwyParamDefInt *i = &item.def.i;
item.type = GWY_PARAM_LAWN_SEGMENT;
i->minimum = -1;
i->maximum = G_MAXINT;
i->default_value = 0;
if (!desc)
desc = _("_Segment");
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_report_type:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI. The label, if not %NULL, will be used for the
* save dialogue as the export controls carry no label.
* @style: Report style, defining allowed report types. See gwy_results_export_set_style() for description.
* @default_value: Default value of the parameter.
*
* Defines a new parameter with values that are report types.
*
* Report type values are neither pure enumerated values nor flags; they are combinations of both. See
* #GwyResultsExport for discussion.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_report_type(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
GwyResultsExportStyle style,
GwyResultsReportType default_value)
{
GwyParamDefItem item;
GwyParamDefReportType *rt = &item.def.rt;
gwy_clear(rt, 1);
item.type = GWY_PARAM_REPORT_TYPE;
if (style != GWY_RESULTS_EXPORT_PARAMETERS
&& style != GWY_RESULTS_EXPORT_TABULAR_DATA
&& style != GWY_RESULTS_EXPORT_FIXED_FORMAT) {
g_warning("Report type param %s (%s) has invalid style (%u).",
name ? name : "???", desc ? desc : "", style);
style = GWY_RESULTS_EXPORT_PARAMETERS;
}
rt->style = style;
rt->default_value = _gwy_param_def_rectify_report_type(&item, default_value);
if (rt->default_value != default_value) {
g_warning("Report type param %s (%s) default value %u is not among allowed values.",
name ? name : "???", desc ? desc : "", default_value);
}
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_seed:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI. Normally pass %NULL for the default label.
*
* Defines a new parameter with values that are random seeds.
*
* Random seeds are integers from the range [1,2³¹-1] which can take random values. They are usually accompanied by
* a boolean parameter controlling whether the value is random or fixed (taken from settings), added afterwards using
* gwy_param_def_add_randomize().
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_seed(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc)
{
GwyParamDefItem item;
GwyParamDefRandomSeed *rs = &item.def.rs;
item.type = GWY_PARAM_RANDOM_SEED;
rs->randomize_id = -1;
if (!desc)
desc = _("R_andom seed");
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_string:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI.
* @flags: Simple treatment of special values. If any are given they are enforced, i.e. they apply both before and
* after @rectify in the case both are given.
* @rectify: Function which takes a string which may or may not be valid parameter value and returns a valid one.
* Pass %NULL if anything goes (or validation is handled otherwise).
* @default_value: Default value of the parameter.
*
* Defines a new parameter with values that are strings.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_string(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
GwyParamStringFlags flags,
GwyRectifyStringFunc rectify,
const gchar *default_value)
{
GwyParamDefItem item;
GwyParamDefString *s = &item.def.s;
item.type = GWY_PARAM_STRING;
s->flags = flags;
s->rectify = rectify;
s->default_value = _gwy_param_def_rectify_string(&item, default_value);
if (g_strcmp0(s->default_value, default_value)) {
g_warning("String param %s (%s) default value \"%s\" does not rectify to itself but to \"%s\".",
name ? name : "???", desc ? desc : "", default_value, s->default_value);
}
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_unit:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI.
* @default_value: Default value of the parameter. %NULL is allowed.
*
* Defines a new parameter with values that are strings representing units.
*
* Units parameters are not enforced to parse to meaningful units. However, they are conceptually different from
* arbitrary strings and have several special unit-oriented functions.
*
* Units behave as strings with the %GWY_PARAM_STRING_EMPTY_IS_NULL flag.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_unit(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
const gchar *default_value)
{
GwyParamDefItem item;
GwyParamDefUnit *si = &item.def.si;
item.type = GWY_PARAM_UNIT;
si->default_value = g_intern_string(default_value);
gwy_param_def_append(pardef, id, name, desc, &item);
}
/**
* gwy_param_def_add_resource:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which will be used for a label in GUI.
* @inventory: Inventory holding the resources to choose from.
* @default_value: Default value of the parameter (resource name).
*
* Defines a new parameter with values that are string resource names.
*
* Use gwy_params_get_string() to get the string name and gwy_params_get_resource() to get the corresponding resource
* object.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.59
**/
void
gwy_param_def_add_resource(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
GwyInventory *inventory,
const gchar *default_value)
{
GwyParamDefItem item;
GwyParamDefResource *res = &item.def.res;
item.type = GWY_PARAM_RESOURCE;
g_assert(GWY_IS_INVENTORY(inventory));
res->inventory = inventory;
res->default_value = g_intern_string(default_value);
if (!gwy_inventory_get_item(inventory, res->default_value)) {
g_warning("Resource param %s (%s) default value \"%s\" is not in the inventory.",
name ? name : "???", desc ? desc : "", default_value);
}
gwy_param_def_append(pardef, id, name, desc, &item);
}
static gpointer
init_grain_value_flags(gpointer user_data)
{
GwyEnum *values = (GwyEnum*)user_data;
guint i;
for (i = 0; values[i].value != -1; i++) {
values[i].name = gwy_grain_value_group_name(values[i].value);
values[i].value = 1u << values[i].value;
}
return values;
}
/**
* gwy_param_def_add_grain_groups:
* @pardef: A set of parameter definitions.
* @id: Parameter identifier.
* @name: Parameter name for settings. It can be %NULL for derived parameters not stored in settings.
* @desc: Parameter description which could be used for a label in GUI, but usually %NULL.
* @default_value: Default value of the parameter (bits corresponding to grain value groups).
*
* Defines a new flag parameter with values that are bits corresponding to grain value groups.
*
* Such parameter is usually used to represent the set of expanded grain value groups in a tree view.
*
* See the introduction for @name and @desc lifetime rules.
*
* Since: 2.61
**/
void
gwy_param_def_add_grain_groups(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
guint default_value)
{
static GwyEnum values[] = {
{ NULL, GWY_GRAIN_VALUE_GROUP_ID, },
{ NULL, GWY_GRAIN_VALUE_GROUP_POSITION, },
{ NULL, GWY_GRAIN_VALUE_GROUP_VALUE, },
{ NULL, GWY_GRAIN_VALUE_GROUP_AREA, },
{ NULL, GWY_GRAIN_VALUE_GROUP_VOLUME, },
{ NULL, GWY_GRAIN_VALUE_GROUP_BOUNDARY, },
{ NULL, GWY_GRAIN_VALUE_GROUP_SLOPE, },
{ NULL, GWY_GRAIN_VALUE_GROUP_CURVATURE, },
{ NULL, GWY_GRAIN_VALUE_GROUP_MOMENT, },
{ NULL, GWY_GRAIN_VALUE_GROUP_USER, },
{ NULL, -1, },
};
static GOnce once = G_ONCE_INIT;
g_once(&once, init_grain_value_flags, values);
if (!desc)
desc = _("Expanded groups");
gwy_param_def_append_flags(pardef, id, name, desc, 0, values, G_N_ELEMENTS(values)-1, default_value);
}
static void
gwy_param_def_append(GwyParamDef *pardef,
gint id,
const gchar *name,
const gchar *desc,
GwyParamDefItem *item)
{
GwyParamDefPrivate *priv;
gint i, len;
g_return_if_fail(GWY_IS_PARAM_DEF(pardef));
priv = pardef->priv;
if (priv->is_used) {
g_critical("Parameter definitions can only be modified during construction.");
return;
}
if (id <= priv->maxid && (i = find_param_def(priv, id)) >= 0) {
g_critical("Item with id %d already exists.", id);
return;
}
if (desc && (len = strlen(desc)) && desc[len-1] == ':') {
gchar *s;
g_warning("Parameter description (%s) should not have trailing colons.", desc);
s = g_strndup(desc, len-1);
g_strstrip(s);
desc = g_intern_string(s);
g_free(s);
}
item->id = id;
item->name = name;
item->desc = desc;
if (priv->ndefs == priv->nalloc) {
priv->nalloc = MAX(2*priv->nalloc, priv->nalloc + 8);
priv->defs = g_renew(GwyParamDefItem, priv->defs, priv->nalloc);
priv->ids = g_renew(gint, priv->ids, priv->nalloc);
}
priv->defs[priv->ndefs] = *item;
priv->ids[priv->ndefs] = id;
priv->ndefs++;
priv->maxid = MAX(priv->maxid, id);
}
static gint
find_param_def(GwyParamDefPrivate *priv, gint id)
{
gint *ids = priv->ids;
gint i, n = priv->ndefs;
for (i = 0; i < n; i++) {
if (ids[i] == id)
return i;
}
return -1;
}
#ifdef DEBUG_USERS
static void
remove_current_user(gpointer user_data, GObject *where_the_object_was)
{
GwyParamDef *pardef = (GwyParamDef*)user_data;
pardef->priv->users = g_slist_remove(pardef->priv->users, where_the_object_was);
}
#endif
void
_gwy_param_def_use(GwyParamDef *pardef, GwyParams *params)
{
GwyParamDefPrivate *priv = pardef->priv;
/* XXX: Here we could sort the definitions by id because they cannot change any more. Then find_param_def() could
* use binary search. */
priv->is_used = TRUE;
#ifdef DEBUG_USERS
/* When presets are implemented using GwyParams then multiple GwyParams instances created for one GwyParamDef
* are common. Keep enabled this check during development but we may want a mechanism to exclude presets. */
if (!g_slist_find(priv->users, params)) {
/* XXX: Hardcoded warning suppression for known modules with GwyParamResource presets. */
if (priv->users && !gwy_stramong(priv->function_name, "rawfile", NULL)) {
g_warning("Parameter definitions for %s are used multiple times. "
"Check module function %s; it is probably leaking GwyParams objects!",
priv->function_name, priv->function_name);
}
priv->users = g_slist_prepend(priv->users, params);
g_object_weak_ref(G_OBJECT(params), remove_current_user, pardef);
}
#endif
}
guint
_gwy_param_def_size(GwyParamDef *pardef)
{
return pardef->priv->ndefs;
}
gint
_gwy_param_def_index(GwyParamDef *pardef, gint id)
{
return find_param_def(pardef->priv, id);
}
/* Return NULL for negative i to make safe _gwy_param_def_item(pardef, _gwy_param_def_index(pardef, id)) */
const GwyParamDefItem*
_gwy_param_def_item(GwyParamDef *pardef, gint i)
{
return (i >= 0 ? pardef->priv->defs + i : NULL);
}
gint
_gwy_param_def_rectify_enum(const GwyParamDefItem *def, gint value)
{
const GwyParamDefEnum *e;
gint i;
g_return_val_if_fail(def && def->type == GWY_PARAM_ENUM, 0);
e = &def->def.e;
i = find_enum_value(e->table, e->nvalues, value);
return (i >= 0 ? value : e->table[e->default_value_index].value);
}
guint
_gwy_param_def_rectify_flags(const GwyParamDefItem *def, guint value)
{
const GwyParamDefFlags *f;
g_return_val_if_fail(def && def->type == GWY_PARAM_FLAGS, 0);
f = &def->def.f;
return value & f->allset;
}
gint
_gwy_param_def_rectify_int(const GwyParamDefItem *def, gint value)
{
const GwyParamDefInt *i;
g_return_val_if_fail(def, 0);
if (def->type == GWY_PARAM_ACTIVE_PAGE)
return value;
g_return_val_if_fail(def->type == GWY_PARAM_INT || param_type_is_curve_no(def->type), 0);
i = &def->def.i;
if ((value < 0 && i->minimum >= 0) || (value > 0 && i->maximum <= 0))
value = -value;
return CLAMP(value, i->minimum, i->maximum);
}
gdouble
_gwy_param_def_rectify_double(const GwyParamDefItem *def, gdouble value)
{
const GwyParamDefDouble *d;
g_return_val_if_fail(def && def->type == GWY_PARAM_DOUBLE, 0.0);
d = &def->def.d;
if (d->is_angle) {
value = fmod(value, 2.0/d->angle_folding*G_PI);
/* fmod() rounds to zero, so we now have range (-2π/f,2π/f). */
if (d->angle_positive) {
if (value < 0.0)
value += 2.0/d->angle_folding*G_PI;
}
else {
if (value > d->maximum)
value -= d->maximum;
else if (value < d->minimum)
value += d->maximum;
}
}
else {
if ((value < 0.0 && d->minimum >= 0.0) || (value > 0.0 && d->maximum <= 0.0))
value = -value;
}
return CLAMP(value, d->minimum, d->maximum);
}
GwyRGBA
_gwy_param_def_rectify_color(const GwyParamDefItem *def,
GwyRGBA value)
{
GwyRGBA color = gwy_param_fallback_color;
const GwyParamDefColor *c;
g_return_val_if_fail(def && def->type == GWY_PARAM_COLOR, color);
c = &def->def.c;
color.r = CLAMP(value.r, 0.0, 1.0);
color.g = CLAMP(value.g, 0.0, 1.0);
color.b = CLAMP(value.b, 0.0, 1.0);
color.a = (c->has_alpha ? CLAMP(value.a, 0.0, 1.0) : 1.0);
return color;
}
GwyResultsReportType
_gwy_param_def_rectify_report_type(const GwyParamDefItem *def, GwyResultsReportType value)
{
const GwyParamDefReportType *rt;
GwyResultsReportType base_type, flags;
g_return_val_if_fail(def && def->type == GWY_PARAM_REPORT_TYPE,
GWY_RESULTS_REPORT_COLON | GWY_RESULTS_REPORT_MACHINE);
flags = (value & GWY_RESULTS_REPORT_MACHINE);
base_type = (value & 0x3);
base_type = CLAMP(base_type, GWY_RESULTS_REPORT_COLON, GWY_RESULTS_REPORT_CSV);
rt = &def->def.rt;
if (rt->style == GWY_RESULTS_EXPORT_TABULAR_DATA && base_type == GWY_RESULTS_REPORT_COLON)
base_type = GWY_RESULTS_REPORT_TABSEP;
return base_type | flags;
}
gint
_gwy_param_def_rectify_random_seed(const GwyParamDefItem *def, gint value)
{
G_GNUC_UNUSED const GwyParamDefRandomSeed *rs;
g_return_val_if_fail(def && def->type == GWY_PARAM_RANDOM_SEED, 42);
rs = &def->def.rs;
return CLAMP(value, 1, 0x7fffffff);
}
gchar*
_gwy_param_def_rectify_string(const GwyParamDefItem *def, const gchar *value)
{
const GwyParamDefString *s;
if (!def || def->type != GWY_PARAM_STRING) {
g_assert(def && def->type == GWY_PARAM_STRING);
return g_strdup("");
}
s = &def->def.s;
return rectify_string(value, s->flags, s->rectify);
}
gchar*
_gwy_param_def_rectify_unit(const GwyParamDefItem *def, const gchar *value)
{
if (!def || def->type != GWY_PARAM_UNIT) {
g_assert(def && def->type == GWY_PARAM_UNIT);
return NULL;
}
return rectify_string(value, GWY_PARAM_STRING_EMPTY_IS_NULL, NULL);
}
static gchar*
rectify_string(const gchar *value, GwyParamStringFlags flags, GwyRectifyStringFunc rectify)
{
gchar *newvalue, *tmpvalue = NULL;
if ((flags & GWY_PARAM_STRING_NULL_IS_EMPTY) && !value)
value = "";
else if ((flags & GWY_PARAM_STRING_EMPTY_IS_NULL) && value && !*value)
value = NULL;
if (!(flags & GWY_PARAM_STRING_DO_NOT_STRIP) && value && *value) {
if (g_ascii_isspace(value[0]) || g_ascii_isspace(value[strlen(value)-1]))
value = tmpvalue = g_strstrip(g_strdup(value));
}
if (rectify) {
newvalue = rectify(value);
if ((flags & GWY_PARAM_STRING_NULL_IS_EMPTY) && !newvalue)
newvalue = g_strdup("");
else if ((flags & GWY_PARAM_STRING_EMPTY_IS_NULL) && newvalue && !*newvalue) {
g_free(newvalue);
newvalue = NULL;
}
if (!(flags & GWY_PARAM_STRING_DO_NOT_STRIP) && newvalue)
g_strstrip(newvalue);
}
else {
if (tmpvalue) {
newvalue = tmpvalue;
tmpvalue = NULL;
}
else
newvalue = g_strdup(value);
}
g_free(tmpvalue);
return newvalue;
}
const gchar*
_gwy_param_def_rectify_resource(const GwyParamDefItem *def, const gchar *value)
{
const GwyParamDefResource *res;
gpointer item;
if (!def || def->type != GWY_PARAM_RESOURCE) {
g_assert(def && def->type == GWY_PARAM_RESOURCE);
return NULL;
}
res = &def->def.res;
item = gwy_inventory_get_item_or_default(res->inventory, value);
return item ? gwy_resource_get_name(GWY_RESOURCE(item)) : NULL;
}
/**
* SECTION:param-def
* @title: GwyParamDef
* @short_description: Module parameter definitions
*
* #GwyParamDef represents a set of module parameter definitions. Once constructed, it is an immutable object which
* modules generally keep around (as a static variable) and use it to fetch parameters from settings as #GwyParams.
*
* Parameters are idenfitied by integers which must be unique within one set of definitions. The integers are not
* public interface. They only exist within the module and can change between module versions. In fact, you can
* assign them run-time (and differently each time Gwyddion is run) if you need. However, the usual way to define the
* parameters is using an enum:
* |[
* enum {
* PARAM_DEGREE,
* PARAM_MASKING,
* PARAM_UPDATE,
* };
* ]|
* and the module then refers to the parameters using the enum values. The corresponding parameter definition code
* could be as follows. Notice specialised parameter types exists for some common settings, such as instant updates:
* |[
* static GwyParamDef*
* define_module_params()
* {
* static GwyParamDef *paramdef = NULL;
*
* if (paramdef)
* return paramdef;
*
* paramdef = gwy_param_def_new();
* gwy_param_def_set_function_name(paramdef, gwy_process_func_current());
* gwy_param_def_add_int(paramdef, PARAM_DEGREE, "degree", _("_Degree"), 0, 12, 3);
* gwy_param_def_add_gwyenum(paramdef, PARAM_MASKING, "masking", NULL, GWY_TYPE_MASKING_TYPE, GWY_MASK_IGNORE);
* gwy_param_def_add_instant_updates(paramdef, PARAM_UPDATE, "update", NULL, FALSE);
* return paramdef;
* }
* ]|
* Functions such as g_once() and g_once_init_enter() can also be used to ensure a single initialisation, although it
* is probably overkill here.
*
* In all the parameter definition constructors strings @name and @desc are not copied. They must exist for the
* entire lifetime of the @pardef objects. Usually they are static strings. In uncommon scenarios the caller must
* ensure their existence. Alternatively, g_intern_string() can be used to make semantically static versions of the
* strings.
**/
/**
* GwyParamDef:
*
* Object representing a set of parameter definitions.
*
* The #GwyParamDef struct contains no public fields.
*
* Since: 2.59
**/
/**
* GwyParamDefClass:
*
* Class of parameter definition sets.
*
* Since: 2.59
**/
/**
* GwyParamStringFlags:
* @GWY_PARAM_STRING_EMPTY_IS_NULL: Fold empty string to %NULL.
* @GWY_PARAM_STRING_NULL_IS_EMPTY: Ensure strings are non-empty by replacing %NULL with an empty string.
* @GWY_PARAM_STRING_DO_NOT_STRIP: Preserve whitespace at the beginning and end of the string.
*
* Flags that can be used when defining a string parameter.
*
* Since: 2.59
**/
/**
* GwyRectifyStringFunc:
* @s: Input string.
*
* Type of function returning a valid string, given possibly invalid input.
*
* Either the input and output can be %NULL (if the string definition allows it).
*
* Returns: Newly allocated string, presumably corrected.
*
* Since: 2.59
**/
/* 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 : */
|