|
| 1 | +--[[ |
| 2 | + change group leader |
| 3 | +
|
| 4 | + copyright (c) 2022 Bill Ferguson <[email protected]> |
| 5 | +
|
| 6 | + darktable is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + darktable is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with darktable. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +]] |
| 19 | + |
| 20 | +--[[ |
| 21 | +CHANGE GROUP LEADER |
| 22 | +automatically change the leader of raw+jpg paired image groups |
| 23 | +
|
| 24 | +INSTALLATION |
| 25 | +* copy this file in $CONFIGDIR/lua/ where CONFIGDIR is your darktable configuration directory |
| 26 | +* add the following line in the file $CONFIGDIR/luarc |
| 27 | + require "change_group_leader" |
| 28 | +
|
| 29 | +USAGE |
| 30 | +* in lighttable mode, select the image groups you wish to process, |
| 31 | + select whether you want to set the leader to "jpg" or "raw", |
| 32 | + and click "Execute" |
| 33 | +]] |
| 34 | + |
| 35 | +local dt = require "darktable" |
| 36 | +local du = require "lib/dtutils" |
| 37 | +local debug = require "darktable.debug" |
| 38 | + |
| 39 | +local MODULE = "change_group_leader" |
| 40 | + |
| 41 | +du.check_min_api_version("3.0.0", MODULE) |
| 42 | + |
| 43 | +-- create a namespace to contain persistent data and widgets |
| 44 | +chg_grp_ldr = {} |
| 45 | + |
| 46 | +local cgl = chg_grp_ldr |
| 47 | + |
| 48 | +cgl.widgets = {} |
| 49 | + |
| 50 | +cgl.event_registered = false |
| 51 | +cgl.module_installed = false |
| 52 | + |
| 53 | +-- - - - - - - - - - - - - - - - - - - - - - - - |
| 54 | +-- F U N C T I O N S |
| 55 | +-- - - - - - - - - - - - - - - - - - - - - - - - |
| 56 | + |
| 57 | +local function install_module() |
| 58 | + if not cgl.module_installed then |
| 59 | + dt.register_lib( |
| 60 | + MODULE, -- Module name |
| 61 | + "change_group_leader", -- Visible name |
| 62 | + true, -- expandable |
| 63 | + false, -- resetable |
| 64 | + {[dt.gui.views.lighttable] = {"DT_UI_CONTAINER_PANEL_RIGHT_CENTER", 700}}, -- containers |
| 65 | + cgl.widgets.box, |
| 66 | + nil,-- view_enter |
| 67 | + nil -- view_leave |
| 68 | + ) |
| 69 | + cgl.module_installed = true |
| 70 | + end |
| 71 | +end |
| 72 | + |
| 73 | +local function find_group_leader(images, mode) |
| 74 | + for _, img in ipairs(images) do |
| 75 | + dt.print_log("checking image " .. img.id .. " named " .. img.filename) |
| 76 | + local found = false |
| 77 | + if mode == "jpg" then |
| 78 | + if string.match(string.lower(img.filename), "jpg$") then |
| 79 | + dt.print_log("jpg matched image " .. img.filename) |
| 80 | + found = true |
| 81 | + end |
| 82 | + elseif mode == "raw" then |
| 83 | + if img.is_raw and img.duplicate_index == 0 then |
| 84 | + dt.print_log("found raw " .. img.filename) |
| 85 | + found = true |
| 86 | + end |
| 87 | + elseif mode == "non-raw" then |
| 88 | + if img.is_ldr then |
| 89 | + dt.print_log("found ldr " .. img.filename) |
| 90 | + found = true |
| 91 | + end |
| 92 | + else |
| 93 | + dt.print_error(MODULE .. ": unrecognized mode " .. mode) |
| 94 | + return |
| 95 | + end |
| 96 | + if found then |
| 97 | + dt.print_log("making " .. img.filename .. " group leader") |
| 98 | + img:make_group_leader() |
| 99 | + return |
| 100 | + end |
| 101 | + end |
| 102 | +end |
| 103 | + |
| 104 | +local function process_image_groups(images) |
| 105 | + if #images < 1 then |
| 106 | + dt.print("No images selected.") |
| 107 | + dt.print_log(MODULE .. "no images seletected, returning...") |
| 108 | + else |
| 109 | + local mode = cgl.widgets.mode.value |
| 110 | + for _,img in ipairs(images) do |
| 111 | + dt.print_log("checking image " .. img.id) |
| 112 | + local group_images = img:get_group_members() |
| 113 | + if group_images == 1 then |
| 114 | + dt.print_log("only one image in group for image " .. image.id) |
| 115 | + else |
| 116 | + find_group_leader(group_images, mode) |
| 117 | + end |
| 118 | + end |
| 119 | + end |
| 120 | +end |
| 121 | + |
| 122 | +-- - - - - - - - - - - - - - - - - - - - - - - - |
| 123 | +-- W I D G E T S |
| 124 | +-- - - - - - - - - - - - - - - - - - - - - - - - |
| 125 | + |
| 126 | +cgl.widgets.mode = dt.new_widget("combobox"){ |
| 127 | + label = "select new group leader", |
| 128 | + tooltip = "select type of image to be group leader", |
| 129 | + selected = 1, |
| 130 | + "jpg", "raw", "non-raw", |
| 131 | +} |
| 132 | + |
| 133 | +cgl.widgets.execute = dt.new_widget("button"){ |
| 134 | + label = "Execute", |
| 135 | + clicked_callback = function() |
| 136 | + process_image_groups(dt.gui.action_images) |
| 137 | + end |
| 138 | +} |
| 139 | + |
| 140 | +cgl.widgets.box = dt.new_widget("box"){ |
| 141 | + orientation = "vertical", |
| 142 | + cgl.widgets.mode, |
| 143 | + cgl.widgets.execute, |
| 144 | +} |
| 145 | + |
| 146 | +-- - - - - - - - - - - - - - - - - - - - - - - - |
| 147 | +-- D A R K T A B L E I N T E G R A T I O N |
| 148 | +-- - - - - - - - - - - - - - - - - - - - - - - - |
| 149 | + |
| 150 | +if dt.gui.current_view().id == "lighttable" then |
| 151 | + install_module() |
| 152 | +else |
| 153 | + if not cgl.event_registered then |
| 154 | + dt.register_event( |
| 155 | + "view-changed", |
| 156 | + function(event, old_view, new_view) |
| 157 | + if new_view.name == "lighttable" and old_view.name == "darkroom" then |
| 158 | + install_module() |
| 159 | + end |
| 160 | + end |
| 161 | + ) |
| 162 | + cgl.event_registered = true |
| 163 | + end |
| 164 | +end |
0 commit comments