@vectoriahq/quac 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -398,8 +398,9 @@ export declare function readCollectionPrivate(collection: StacCollection): boole
398
398
  * the counterpart of the `quac:attachment` write in `stacCatalogue.ts`.
399
399
  * A column declaring none is absent from the result.
400
400
  *
401
- * An omitted `viewer`, `filter` or `default_root` reads as null, and an
402
- * omitted or unrecognised `relative_storage` as absolute.
401
+ * An omitted `viewer`, `filter`, `default_root` or `naming` reads as null, an
402
+ * omitted or unrecognised `relative_storage` as absolute, and a
403
+ * `max_long_side` or `max_short_side` that is not a whole number above 0 as null.
403
404
  */
404
405
  export declare function readCollectionFieldAttachments(collection: StacCollection): Record<string, FieldAttachment>;
405
406
  /**
@@ -616,13 +616,18 @@ export function readCollectionFilters(collection) {
616
616
  export function readCollectionPrivate(collection) {
617
617
  return collection[QUAC_PRIVATE_FIELD] === true;
618
618
  }
619
+ /** A side length in pixels: a whole number above 0, or null. */
620
+ function readImageSide(value) {
621
+ return typeof value === 'number' && Number.isInteger(value) && value > 0 ? value : null;
622
+ }
619
623
  /**
620
624
  * The fields a Collection declares as holding a file, keyed by field name —
621
625
  * the counterpart of the `quac:attachment` write in `stacCatalogue.ts`.
622
626
  * A column declaring none is absent from the result.
623
627
  *
624
- * An omitted `viewer`, `filter` or `default_root` reads as null, and an
625
- * omitted or unrecognised `relative_storage` as absolute.
628
+ * An omitted `viewer`, `filter`, `default_root` or `naming` reads as null, an
629
+ * omitted or unrecognised `relative_storage` as absolute, and a
630
+ * `max_long_side` or `max_short_side` that is not a whole number above 0 as null.
626
631
  */
627
632
  export function readCollectionFieldAttachments(collection) {
628
633
  const attachments = nameRecord();
@@ -637,7 +642,10 @@ export function readCollectionFieldAttachments(collection) {
637
642
  defaultRoot: readNonBlank(fields.default_root),
638
643
  relativeStorage: ATTACHMENT_RELATIVE_STORAGE.includes(fields.relative_storage)
639
644
  ? fields.relative_storage ?? 'absolute'
640
- : 'absolute'
645
+ : 'absolute',
646
+ naming: readNonBlank(fields.naming),
647
+ maxLongSide: readImageSide(fields.max_long_side),
648
+ maxShortSide: readImageSide(fields.max_short_side)
641
649
  };
642
650
  }
643
651
  return attachments;
@@ -926,7 +926,10 @@ function attachmentToJson(attachment) {
926
926
  ...(attachment.viewer ? { viewer: attachment.viewer } : {}),
927
927
  ...(attachment.filter ? { filter: attachment.filter } : {}),
928
928
  ...(attachment.defaultRoot && isProjectRelativeFolder(attachment.defaultRoot) ? { default_root: attachment.defaultRoot } : {}),
929
- relative_storage: attachment.relativeStorage
929
+ relative_storage: attachment.relativeStorage,
930
+ ...(attachment.naming ? { naming: attachment.naming } : {}),
931
+ ...(attachment.maxLongSide ? { max_long_side: attachment.maxLongSide } : {}),
932
+ ...(attachment.maxShortSide ? { max_short_side: attachment.maxShortSide } : {})
930
933
  };
931
934
  }
932
935
  /**
@@ -462,6 +462,17 @@ export interface FieldAttachment {
462
462
  defaultRoot: string | null;
463
463
  /** What a stored path is relative to. */
464
464
  relativeStorage: 'absolute' | 'project' | 'default-root';
465
+ /**
466
+ * The QGIS expression a field app names a new file by: its result is the
467
+ * file's path, with `{extension}` standing for the file's extension and
468
+ * `{filename}` for its own name. From QFieldSync, else from the Mergin Maps
469
+ * plugin. Null where neither sets one.
470
+ */
471
+ naming: string | null;
472
+ /** For an image: the longest side, in pixels, a new photo is shrunk to (QFieldSync). Null for none. */
473
+ maxLongSide: number | null;
474
+ /** For an image: the shortest side, in pixels, a new photo is shrunk to (the Mergin Maps plugin). Null for none. */
475
+ maxShortSide: number | null;
465
476
  }
466
477
  /** A layer's Drag and Drop Designer form, as far as QUAC reads it: which fields it places, in its order. */
467
478
  export interface LayerFormLayout {
@@ -316,6 +316,12 @@ export interface TableColumnValue {
316
316
  }
317
317
  /** A column whose value is a path to a file. An absent member means the source declared nothing. */
318
318
  export interface TableColumnAttachment {
319
+ /** The QGIS expression a field app names a new file by, in QField's form. */
320
+ naming?: string;
321
+ /** For an image: the longest side, in pixels, a new photo is shrunk to. */
322
+ max_long_side?: number;
323
+ /** For an image: the shortest side, in pixels, a new photo is shrunk to. */
324
+ max_short_side?: number;
319
325
  /** How the file is meant to be shown. Absent where the source specifies no viewer. */
320
326
  viewer?: 'image' | 'web' | 'audio' | 'video';
321
327
  /** The file types the source supports, in the source's own wording. */
@@ -1,7 +1,8 @@
1
1
  import { readCustomPropertyList } from "./customProperties.js";
2
2
  import { parseDataSource } from "./parseDataSource.js";
3
3
  import { parseFormLayout } from "./parseFormLayout.js";
4
- import { parseQfieldSyncLocks } from "./parseQfieldSync.js";
4
+ import { parseMerginPhotoNaming, parseMerginPhotoShortSide } from "./parseMergin.js";
5
+ import { parseQfieldSyncAttachmentNaming, parseQfieldSyncLocks, parseQfieldSyncMaxImageSize } from "./parseQfieldSync.js";
5
6
  import { parseLayerStyles } from "./parseStyle.js";
6
7
  import { readQgisExpression, unescapeStringLiteral } from "./qgisExpression.js";
7
8
  import { resolveFieldIconAddress } from "../model/fieldIconAddress.js";
@@ -35,12 +36,17 @@ export function parseProjectLayers(qgisRoot, notes = []) {
35
36
  const projectLayers = qgisRoot.querySelector('projectlayers');
36
37
  if (!projectLayers)
37
38
  return [];
39
+ const photoSettings = {
40
+ merginNaming: parseMerginPhotoNaming(qgisRoot),
41
+ maxLongSide: parseQfieldSyncMaxImageSize(qgisRoot),
42
+ maxShortSide: parseMerginPhotoShortSide(qgisRoot)
43
+ };
38
44
  const layers = [];
39
45
  for (const node of Array.from(projectLayers.children)) {
40
46
  if (node.tagName !== 'maplayer')
41
47
  continue;
42
48
  try {
43
- const layer = parseMapLayer(node, notes);
49
+ const layer = parseMapLayer(node, notes, photoSettings);
44
50
  if (layer)
45
51
  layers.push(layer);
46
52
  }
@@ -60,7 +66,7 @@ export function parseProjectLayers(qgisRoot, notes = []) {
60
66
  }
61
67
  return layers;
62
68
  }
63
- function parseMapLayer(maplayer, notes) {
69
+ function parseMapLayer(maplayer, notes, photoSettings) {
64
70
  const id = maplayer.querySelector('id')?.textContent?.trim();
65
71
  const name = maplayer.querySelector('layername')?.textContent?.trim() ?? '';
66
72
  if (!id)
@@ -80,7 +86,7 @@ function parseMapLayer(maplayer, notes) {
80
86
  const fieldAliases = parseFieldAliases(maplayer);
81
87
  const { styles, activeIndex } = parseLayerStyles(maplayer);
82
88
  const fieldValueRelations = parseFieldValueRelations(maplayer);
83
- const fieldAttachments = parseFieldAttachments(maplayer);
89
+ const fieldAttachments = parseFieldAttachments(maplayer, photoSettings.merginNaming[id] ?? nameRecord(), photoSettings);
84
90
  if (dataSource.kind === 'unsupported')
85
91
  note('source', null, dataSource.hint);
86
92
  noteUnreadWidgets(maplayer, { fieldValueRelations, fieldAttachments }, note);
@@ -439,10 +445,16 @@ const SINGLE_FILE_STORAGE_MODES = new Set([0, 3]);
439
445
  * A widget whose `StorageMode` picks a directory or several files is not an
440
446
  * attachment here.
441
447
  *
448
+ * The field apps' settings for a new file are added: the naming expression
449
+ * QFieldSync sets for the field (`parseQfieldSyncAttachmentNaming`), else the
450
+ * one the Mergin Maps plugin sets (`merginNaming`), and for an image the
451
+ * project's size limits (`sizes`).
452
+ *
442
453
  * A field with no ExternalResource widget is absent from the returned map.
443
454
  */
444
- function parseFieldAttachments(maplayer) {
455
+ function parseFieldAttachments(maplayer, merginNaming, sizes) {
445
456
  const result = nameRecord();
457
+ const qfieldNaming = parseQfieldSyncAttachmentNaming(maplayer);
446
458
  for (const [name, editWidget] of layerEditWidgets(maplayer)) {
447
459
  if (result[name])
448
460
  continue;
@@ -452,11 +464,17 @@ function parseFieldAttachments(maplayer) {
452
464
  const storageMode = options.get('StorageMode');
453
465
  if (storageMode !== undefined && !SINGLE_FILE_STORAGE_MODES.has(Number(storageMode)))
454
466
  continue;
467
+ const viewer = DOCUMENT_VIEWERS[Number(options.get('DocumentViewer'))] ?? null;
455
468
  result[name] = {
456
- viewer: DOCUMENT_VIEWERS[Number(options.get('DocumentViewer'))] ?? null,
469
+ viewer,
457
470
  filter: readTrimmedOption(options, 'FileWidgetFilter'),
458
471
  defaultRoot: readTrimmedOption(options, 'DefaultRoot'),
459
- relativeStorage: RELATIVE_STORAGE[Number(options.get('RelativeStorage'))] ?? 'absolute'
472
+ relativeStorage: RELATIVE_STORAGE[Number(options.get('RelativeStorage'))] ?? 'absolute',
473
+ // QFieldSync's first where a project sets both.
474
+ naming: qfieldNaming[name] ?? merginNaming[name] ?? null,
475
+ // Both apps shrink only a photo.
476
+ maxLongSide: viewer === 'image' ? sizes.maxLongSide : null,
477
+ maxShortSide: viewer === 'image' ? sizes.maxShortSide : null
460
478
  };
461
479
  }
462
480
  return result;
@@ -17,3 +17,27 @@ import type { PresetFilter } from '../model/QgsProject.ts';
17
17
  * or the list does not read as JSON.
18
18
  */
19
19
  export declare function parseMerginFilters(qgisRoot: Element, textFilters?: PresetFilter[]): PresetFilter[];
20
+ /**
21
+ * The photo naming expressions the Mergin Maps plugin sets, keyed by layer id
22
+ * and then field name: the project entries `Mergin/PhotoNaming/<layer id>/<field
23
+ * name>` (`ProjectSettingsWidget.apply`, `Mergin/project_settings_widget.py`,
24
+ * `MerginMaps/qgis-plugin@eb2c52a`).
25
+ *
26
+ * The app evaluates the expression for the feature, takes the result as the
27
+ * file's name without its extension, adds the file's own extension, and keeps
28
+ * the original name where the result is blank (`AttributeController::renamePhotos`,
29
+ * `app/attributes/attributecontroller.cpp`, `MerginMaps/mobile@779434d`). So
30
+ * each is given here in QField's form, the result followed by `.{extension}`,
31
+ * which reads the same. A blank expression gives none.
32
+ */
33
+ export declare function parseMerginPhotoNaming(qgisRoot: Element): Record<string, Record<string, string>>;
34
+ /**
35
+ * The shorter side, in pixels, the Mergin Maps app shrinks a photo to: the
36
+ * project entry `Mergin/PhotoQuality`, 0 for Original and 1, 2 and 3 for High,
37
+ * Medium and Low (`ProjectSettingsWidget`, `Mergin/project_settings_widget.py`,
38
+ * `MerginMaps/qgis-plugin@eb2c52a`). The app scales a portrait photo to that
39
+ * width and a landscape one to that height, leaving one already that small
40
+ * (`ImageUtils::rescale`, `app/imageutils.cpp`, `MerginMaps/mobile@779434d`).
41
+ * Null for Original and where the project sets none.
42
+ */
43
+ export declare function parseMerginPhotoShortSide(qgisRoot: Element): number | null;
@@ -1,5 +1,6 @@
1
1
  import { isTrueValue } from "./customProperties.js";
2
2
  import { propertyChild, propertyText, readPropertyBlock } from "./projectProperties.js";
3
+ import { nameRecord } from "../util/nameRecord.js";
3
4
  /** The Mergin Maps filter types the filter row has a control for; `Text`, a "contains" search, is not one. */
4
5
  const READ_FILTER_TYPES = new Set(['Number', 'Date', 'Checkbox', 'Single select', 'Multi select']);
5
6
  /**
@@ -52,3 +53,56 @@ export function parseMerginFilters(qgisRoot, textFilters = []) {
52
53
  return READ_FILTER_TYPES.has(type) ? [preset] : [];
53
54
  });
54
55
  }
56
+ /** A property block's direct entries by key, in either shape `propertyChild` reads. */
57
+ function propertyEntries(parent) {
58
+ return Array.from(parent.children).map((child) => [
59
+ child.tagName === 'properties' ? child.getAttribute('name') ?? '' : child.tagName,
60
+ child
61
+ ]);
62
+ }
63
+ /**
64
+ * The photo naming expressions the Mergin Maps plugin sets, keyed by layer id
65
+ * and then field name: the project entries `Mergin/PhotoNaming/<layer id>/<field
66
+ * name>` (`ProjectSettingsWidget.apply`, `Mergin/project_settings_widget.py`,
67
+ * `MerginMaps/qgis-plugin@eb2c52a`).
68
+ *
69
+ * The app evaluates the expression for the feature, takes the result as the
70
+ * file's name without its extension, adds the file's own extension, and keeps
71
+ * the original name where the result is blank (`AttributeController::renamePhotos`,
72
+ * `app/attributes/attributecontroller.cpp`, `MerginMaps/mobile@779434d`). So
73
+ * each is given here in QField's form, the result followed by `.{extension}`,
74
+ * which reads the same. A blank expression gives none.
75
+ */
76
+ export function parseMerginPhotoNaming(qgisRoot) {
77
+ const block = readPropertyBlock(qgisRoot, 'Mergin');
78
+ const naming = block ? propertyChild(block, 'PhotoNaming') : null;
79
+ const byLayer = nameRecord();
80
+ if (!naming)
81
+ return byLayer;
82
+ for (const [layerId, layer] of propertyEntries(naming)) {
83
+ for (const [field, entry] of propertyEntries(layer)) {
84
+ const expression = entry.textContent?.trim();
85
+ if (!layerId || !field || !expression)
86
+ continue;
87
+ byLayer[layerId] ??= nameRecord();
88
+ byLayer[layerId][field] = `(${expression}) || '.{extension}'`;
89
+ }
90
+ }
91
+ return byLayer;
92
+ }
93
+ /** The shorter side, in pixels, each Mergin Maps photo quality shrinks a photo to; 0, Original, shrinks none. */
94
+ const MERGIN_PHOTO_SHORT_SIDES = { 1: 3000, 2: 1500, 3: 1000 };
95
+ /**
96
+ * The shorter side, in pixels, the Mergin Maps app shrinks a photo to: the
97
+ * project entry `Mergin/PhotoQuality`, 0 for Original and 1, 2 and 3 for High,
98
+ * Medium and Low (`ProjectSettingsWidget`, `Mergin/project_settings_widget.py`,
99
+ * `MerginMaps/qgis-plugin@eb2c52a`). The app scales a portrait photo to that
100
+ * width and a landscape one to that height, leaving one already that small
101
+ * (`ImageUtils::rescale`, `app/imageutils.cpp`, `MerginMaps/mobile@779434d`).
102
+ * Null for Original and where the project sets none.
103
+ */
104
+ export function parseMerginPhotoShortSide(qgisRoot) {
105
+ const block = readPropertyBlock(qgisRoot, 'Mergin');
106
+ const quality = Number(block ? propertyText(block, 'PhotoQuality') : null);
107
+ return MERGIN_PHOTO_SHORT_SIDES[quality] ?? null;
108
+ }
@@ -45,3 +45,29 @@ export declare function parseQfieldSyncLocks(maplayer: Element): LayerLocks;
45
45
  * project saves no `WFSLayers`.
46
46
  */
47
47
  export declare function parseWfsTransactionLayers(qgisRoot: Element): WfsTransactionLayers | null;
48
+ /**
49
+ * The naming expression QFieldSync sets for each attachment field of a layer,
50
+ * keyed by field name: the layer custom property `QFieldSync/attachment_naming`,
51
+ * a JSON object of field name to QGIS expression (`LayerSource`,
52
+ * `libqfieldsync/layer.py`, `opengisch/libqfieldsync@aad2ea6`). A layer saved
53
+ * by QFieldSync before 4.3 holds `QFieldSync/photo_naming` instead, which is
54
+ * read only where the newer key is absent, as QField reads it
55
+ * (`getAttachmentNaming`, `src/gui/qml/editorwidgets/ExternalResourceUtils.js`,
56
+ * `opengisch/QField@2cd8695`).
57
+ *
58
+ * The expression's result is the file's path, with `{extension}` standing for
59
+ * the file's extension and `{filename}` for its own name. A blank expression,
60
+ * or a key that does not read as a JSON object, gives none.
61
+ */
62
+ export declare function parseQfieldSyncAttachmentNaming(maplayer: Element): Record<string, string>;
63
+ /**
64
+ * The longest side, in pixels, QFieldSync has QField shrink a photo to:
65
+ * **Maximum image width/height** on the QField tab of Project Properties,
66
+ * saved as `<properties><qfieldsync><maximumImageWidthHeight>`, 0 for no limit
67
+ * (`ProjectConfiguration`, `libqfieldsync/project.py`,
68
+ * `opengisch/libqfieldsync@aad2ea6`). QField shrinks a photo whose width or
69
+ * height is over it so that the longer side is that size
70
+ * (`QfFileUtils::restrictImageSize`, `src/core/utils/qffileutils.cpp`,
71
+ * `opengisch/QField@2cd8695`). Null where the project sets none.
72
+ */
73
+ export declare function parseQfieldSyncMaxImageSize(qgisRoot: Element): number | null;
@@ -1,5 +1,6 @@
1
1
  import { isTrueValue, readCustomProperty } from "./customProperties.js";
2
2
  import { propertyChild, propertyList, propertyText, readPropertyBlock } from "./projectProperties.js";
3
+ import { nameRecord } from "../util/nameRecord.js";
3
4
  /**
4
5
  * The layer the QFieldSync plugin names as the one a project opens with as
5
6
  * the active layer: **Initial active layer** on the QField tab of Project
@@ -93,3 +94,55 @@ export function parseWfsTransactionLayers(qgisRoot) {
93
94
  delete: listOf('Delete')
94
95
  };
95
96
  }
97
+ /**
98
+ * The naming expression QFieldSync sets for each attachment field of a layer,
99
+ * keyed by field name: the layer custom property `QFieldSync/attachment_naming`,
100
+ * a JSON object of field name to QGIS expression (`LayerSource`,
101
+ * `libqfieldsync/layer.py`, `opengisch/libqfieldsync@aad2ea6`). A layer saved
102
+ * by QFieldSync before 4.3 holds `QFieldSync/photo_naming` instead, which is
103
+ * read only where the newer key is absent, as QField reads it
104
+ * (`getAttachmentNaming`, `src/gui/qml/editorwidgets/ExternalResourceUtils.js`,
105
+ * `opengisch/QField@2cd8695`).
106
+ *
107
+ * The expression's result is the file's path, with `{extension}` standing for
108
+ * the file's extension and `{filename}` for its own name. A blank expression,
109
+ * or a key that does not read as a JSON object, gives none.
110
+ */
111
+ export function parseQfieldSyncAttachmentNaming(maplayer) {
112
+ const naming = nameRecord();
113
+ const properties = Array.from(maplayer.children).find((child) => child.tagName === 'customproperties');
114
+ if (!properties)
115
+ return naming;
116
+ const saved = readCustomProperty(properties, 'QFieldSync/attachment_naming') ?? readCustomProperty(properties, 'QFieldSync/photo_naming');
117
+ if (!saved)
118
+ return naming;
119
+ let parsed;
120
+ try {
121
+ parsed = JSON.parse(saved);
122
+ }
123
+ catch {
124
+ return naming;
125
+ }
126
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))
127
+ return naming;
128
+ for (const [field, expression] of Object.entries(parsed)) {
129
+ if (typeof expression === 'string' && expression.trim() !== '')
130
+ naming[field] = expression;
131
+ }
132
+ return naming;
133
+ }
134
+ /**
135
+ * The longest side, in pixels, QFieldSync has QField shrink a photo to:
136
+ * **Maximum image width/height** on the QField tab of Project Properties,
137
+ * saved as `<properties><qfieldsync><maximumImageWidthHeight>`, 0 for no limit
138
+ * (`ProjectConfiguration`, `libqfieldsync/project.py`,
139
+ * `opengisch/libqfieldsync@aad2ea6`). QField shrinks a photo whose width or
140
+ * height is over it so that the longer side is that size
141
+ * (`QfFileUtils::restrictImageSize`, `src/core/utils/qffileutils.cpp`,
142
+ * `opengisch/QField@2cd8695`). Null where the project sets none.
143
+ */
144
+ export function parseQfieldSyncMaxImageSize(qgisRoot) {
145
+ const block = readPropertyBlock(qgisRoot, 'qfieldsync');
146
+ const size = Number(block ? propertyText(block, 'maximumImageWidthHeight') : null);
147
+ return Number.isInteger(size) && size > 0 ? size : null;
148
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vectoriahq/quac",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "description": "Writes and reads QUAC, which adds map layer settings to STAC catalogues.",
@@ -1017,6 +1017,20 @@
1017
1017
  "default-root"
1018
1018
  ],
1019
1019
  "description": "What a stored path is relative to."
1020
+ },
1021
+ "naming": {
1022
+ "type": "string",
1023
+ "description": "The QGIS expression a field app names a new file by. Its result is the file's path, relative as relative_storage says, with {extension} standing for the file's extension and {filename} for its own name."
1024
+ },
1025
+ "max_long_side": {
1026
+ "type": "integer",
1027
+ "minimum": 1,
1028
+ "description": "For an image: the longest side, in pixels, a new photo is shrunk to."
1029
+ },
1030
+ "max_short_side": {
1031
+ "type": "integer",
1032
+ "minimum": 1,
1033
+ "description": "For an image: the shortest side, in pixels, a new photo is shrunk to."
1020
1034
  }
1021
1035
  },
1022
1036
  "additionalProperties": false