Skip to contents

Renames a theme and re-points everything in the project that refers to it by name. A theme is named in five places, not one, which is why rewriting the preset's name attribute by hand leaves a project half-renamed:

Usage

rfp_qgs_theme_rename(path, from, to, merge = FALSE, backup = TRUE)

Arguments

path

Character. Path to the .qgs.

from

Character. The theme's current name.

to

Character. The new name.

merge

Logical. When to already exists, retire from into it rather than refuse. Default FALSE. Ignored when to does not exist.

backup

Logical. Back up the .qgs first, outside the project (see rfp-backups). Default TRUE.

Value

Invisibly, a data frame with one row per surface: surface and n, the number of references re-pointed. Under merge, the preset row counts the from presets removed.

Details

  • the <visibility-preset name=> itself;

  • a print layout map that follows the theme (LayoutItem@followPresetName);

  • a map canvas showing it (mapcanvas@theme, the main canvas and each additional map view);

  • a 3D view's terrain (terrain@map-theme);

  • a layout's PDF export theme list (the pdfExportThemes layout property, ~~~-joined, where only the matching entry moves).

These are the listeners of QGIS 4.2's own mapThemeRenamed signal, so this is the edit QGIS Desktop makes. A surface left behind does not error: a layout map following a name that no longer exists silently draws the project's current layer state instead, which is how a live project came to carry a dangling followPresetName="Overview Report Map". rfp_qgs_theme_rm() now warns when it would leave one.

Matching is by attribute on each surface, never by searching for the string: a theme name is often also a layer-tree group name, and the group and the preset's group state are left alone.

Refuses, writing nothing, when from is not in the project, when from equals to, or when to already exists - as QGIS's renameMapTheme() does. A project that already carries both names is retired into the existing one with merge = TRUE: every reference to from is re-pointed at to, the from preset is removed, and to's own preset is left as it is. A PDF export list naming both keeps to once. Removing from with rfp_qgs_theme_rm() instead would leave those references following a theme that no longer exists. If two presets are named from, both are renamed (or both removed under merge), as rfp_qgs_theme_rm() removes both.

Examples

src <- system.file("templates", "bcrestoration_mobile.qgs", package = "rfp")
p <- file.path(tempdir(), "renamed.qgs")
file.copy(src, p, overwrite = TRUE)
#> [1] TRUE

rfp_qgs_theme_rename(p, "Land Tenure", "Tenure", backup = FALSE)
#> ✔ Renamed theme "Land Tenure" to "Tenure" in /tmp/RtmpGuotGj/renamed.qgs.
rfp_qgs_theme_names(p)
#> [1] "High Detail - Crossings"       "Tenure"                       
#> [3] "Low Detail - Bull Trout Model" "Low Detail - Salmon Model"    
#> [5] "Low Detail - Steelhead Model"