-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_leader.lua
executable file
·216 lines (187 loc) · 5.68 KB
/
change_leader.lua
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
--[[
]]
--[[
CHANGE LEADER
allows you to change the leaders in the selected groups based on the file type or modification
note: you need to reload the collection or expand and collapse the groups
]]
local dt = require "darktable"
local du = require "lib/dtutils"
local MODULE = "change_leader"
du.check_min_api_version("7.0.0", MODULE)
local script_data = {}
GUI = {
box = {},
mode_combo = {},
prefer_modified = {},
exec_selected = {},
exec_collection = {},
}
local chg_ldr = {}
chg_ldr.event_registered = false
chg_ldr.module_installed = false
script_data.destroy = nil
script_data.destroy_method = nil
script_data.restart = nil
local function install_module()
if not chg_ldr.module_installed then
dt.print_log(MODULE .. ": installing module")
dt.register_lib(
MODULE, -- Module name
"change leader", -- Visible name
true, -- expandable
false, -- resetable
{[dt.gui.views.lighttable] = {"DT_UI_CONTAINER_PANEL_RIGHT_CENTER", 700}}, -- containers
GUI.box,
nil, -- view_enter
nil -- view_leave
)
chg_ldr.module_installed = true
dt.print_log(MODULE .. ": module installed" )
end
end
local function find_group_leader(images, mode, prefer_mod)
local candidate_img = nil
for _, img in ipairs(images) do
dt.print_log(MODULE .. ": checking image " .. img.id .. " named " .. img.filename)
if prefer_mod and img.is_altered then
dt.print_log(MODULE .. ": found modified " .. img.filename)
return img
elseif mode == "jpg/jpeg" then
if string.match(img.filename, "[jJ][pP][eE]?[gG]$") then
dt.print_log(MODULE .. ": found jpg/jpeg " .. img.filename)
if not candidate_img then
candidate_img = img
end
end
elseif mode == "raw" then
if img.is_raw and img.duplicate_index == 0 then
dt.print_log(MODULE .. ": found raw " .. img.filename)
if not candidate_img then
candidate_img = img
end
end
elseif mode == "ldr" then
if img.is_ldr then
dt.print_log(MODULE .. ": found ldr " .. img.filename)
if not candidate_img then
candidate_img = img
end
end
elseif mode == "hdr" then
if img.is_hdr then
dt.print_log(MODULE .. ": found hdr " .. img.filename)
if not candidate_img then
candidate_img = img
end
end
else
dt.print_error(MODULE .. ": unrecognized mode " .. mode)
return nil
end
if candidate_img and not prefer_mod then
return candidate_img
end
end
if prefer_mod then
dt.print_log(MODULE .. ": no modified image found, falling back to mode selection")
return candidate_img
else
return nil
end
end
local function process_image_groups(images)
if #images < 1 then
dt.print("No images selected.")
dt.print_log(MODULE .. ": no images seletected, returning...")
else
local groups = {}
local group_count = 0
for _,img in ipairs(images) do
local group_images = img:get_group_members()
if not group_images then
dt.print_log(MODULE .. ": " .. img.filename .. " is not grouped")
elseif #group_images == 1 then
dt.print_log(MODULE .. ": only one image in group for image " .. img.filename)
else
if not groups[group_images[1].group_leader.id] then
group_count = group_count + 1
groups[group_images[1].group_leader.id] = group_images
end
end
end
if group_count < 1 then
dt.print("No images to process")
return
end
local mode = GUI.mode.value
local prefer_modified = GUI.prefer_modified.value
for leader_id, group_imgs in pairs(groups) do
dt.print_log(MODULE .. ": processing group " .. leader_id)
local leader = find_group_leader(group_imgs, mode, prefer_modified)
if leader then
dt.print_log(MODULE .. ": setting " .. group_imgs[1].group_leader.filename .. " as leader")
leader:make_group_leader()
else
dt.print("No leader found for group " .. group_imgs[1].group_leader.filename)
dt.print_log(MODULE .. ": no leader found for group " .. group_imgs[1].group_leader.filename)
end
end
end
end
local function destroy()
dt.gui.libs[MODULE].visible = false
end
local function restart()
dt.gui.libs[MODULE].visible = true
end
GUI.mode = dt.new_widget("combobox"){
label = "select new group leader",
tooltip = "select type of image to be group leader",
selected = 1,
"jpg/jpeg", "raw", "ldr", "hdr",
}
GUI.exec_selected = dt.new_widget("button"){
label = "Execute on selected",
clicked_callback = function()
process_image_groups(dt.gui.action_images)
end
}
GUI.exec_collection = dt.new_widget("button"){
label = "Execute on collection",
clicked_callback = function()
process_image_groups(dt.collection)
end
}
GUI.prefer_modified = dt.new_widget("check_button"){
label = "Prefer modified",
tooltip = "Make the first modified image as leader. If none is modified use selected type.",
value = true,
}
GUI.box = dt.new_widget("box"){
orientation = "vertical",
GUI.mode,
GUI.prefer_modified,
GUI.exec_selected,
GUI.exec_collection,
}
if dt.gui.current_view().id == "lighttable" then
install_module()
else
if not chg_ldr.event_registered then
dt.register_event(
"view-changed",
function(event, old_view, new_view)
if new_view.name == "lighttable" and old_view.name == "darkroom" then
install_module()
end
end
)
chg_ldr.event_registered = true
end
end
script_data.destroy = destroy
script_data.destroy_method = "hide"
script_data.restart = restart
script_data.show = restart
return script_data