Skip to content

Commit d4eb657

Browse files
authored
Merge pull request darktable-org#445 from wpferguson/jpg_group_leader
Make jpg group leader for raw + jpg pairs
2 parents 025f429 + 23835ed commit d4eb657

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed

contrib/jpg_group_leader.lua

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
--[[
2+
3+
jpg_group_leader.lua - Make jpg image group leader
4+
5+
Copyright (C) 2024 Bill Ferguson <[email protected]>.
6+
7+
This program is free software: you can redistribute it and/or modify
8+
it under the terms of the GNU General Public License as published by
9+
the Free Software Foundation; either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
]]
20+
--[[
21+
jpg_group_leader - Make jpg image group leader
22+
23+
After a film roll is imported, check for RAW-JPG image groups
24+
and make the JPG image the group leader. This is on by default
25+
but can be disabled in preferences.
26+
27+
Shortcuts are included to filter existing collections or
28+
selections of images and make the jpg the group leader.
29+
30+
ADDITIONAL SOFTWARE NEEDED FOR THIS SCRIPT
31+
None
32+
33+
USAGE
34+
Start script from script_manager
35+
Assign keys to the shortcuts
36+
37+
BUGS, COMMENTS, SUGGESTIONS
38+
Bill Ferguson <[email protected]>
39+
40+
CHANGES
41+
]]
42+
43+
local dt = require "darktable"
44+
local du = require "lib/dtutils"
45+
local df = require "lib/dtutils.file"
46+
47+
48+
-- - - - - - - - - - - - - - - - - - - - - - - -
49+
-- C O N S T A N T S
50+
-- - - - - - - - - - - - - - - - - - - - - - - -
51+
52+
local MODULE = "jpg_group_leader"
53+
54+
-- - - - - - - - - - - - - - - - - - - - - - - -
55+
-- A P I C H E C K
56+
-- - - - - - - - - - - - - - - - - - - - - - - -
57+
58+
du.check_min_api_version("7.0.0", MODULE)
59+
60+
61+
-- - - - - - - - - - - - - - - - - - - - - - - - - -
62+
-- S C R I P T M A N A G E R I N T E G R A T I O N
63+
-- - - - - - - - - - - - - - - - - - - - - - - - - -
64+
65+
local script_data = {}
66+
67+
script_data.destroy = nil -- function to destory the script
68+
script_data.destroy_method = nil -- set to hide for libs since we can't destroy them commpletely yet
69+
script_data.restart = nil -- how to restart the (lib) script after it's been hidden - i.e. make it visible again
70+
script_data.show = nil -- only required for libs since the destroy_method only hides them
71+
72+
-- - - - - - - - - - - - - - - - - - - - - - - - - -
73+
-- I 1 8 N
74+
-- - - - - - - - - - - - - - - - - - - - - - - - - -
75+
76+
local gettext = dt.gettext
77+
78+
-- Tell gettext where to find the .mo file translating messages for a particular domain
79+
gettext.bindtextdomain(MODULE, dt.configuration.config_dir .. "/lua/locale/")
80+
81+
local function _(msgid)
82+
return gettext.dgettext(MODULE, msgid)
83+
end
84+
85+
86+
-- - - - - - - - - - - - - - - - - - - - - - - -
87+
-- P R E F E R E N C E S
88+
-- - - - - - - - - - - - - - - - - - - - - - - -
89+
90+
dt.preferences.register(MODULE, "on_import", "bool", _("make jpg group leader on import"), _("automatically make the jpg file the group leader when raw + jpg are imported"), true)
91+
92+
-- - - - - - - - - - - - - - - - - - - - - - - -
93+
-- N A M E S P A C E
94+
-- - - - - - - - - - - - - - - - - - - - - - - -
95+
96+
local jgloi = {}
97+
jgloi.images = {}
98+
99+
-- - - - - - - - - - - - - - - - - - - - - - - -
100+
-- F U N C T I O N S
101+
-- - - - - - - - - - - - - - - - - - - - - - - -
102+
103+
local function toggle_global_toolbox_grouping()
104+
dt.gui.libs.global_toolbox.grouping = false
105+
dt.gui.libs.global_toolbox.grouping = true
106+
end
107+
108+
-- - - - - - - - - - - - - - - - - - - - - - - -
109+
-- M A I N P R O G R A M
110+
-- - - - - - - - - - - - - - - - - - - - - - - -
111+
112+
local function make_jpg_group_leader(images)
113+
-- if the image is part of a group, make it the leader
114+
for _, image in ipairs(images) do
115+
if #image:get_group_members() > 1 then
116+
image:make_group_leader()
117+
end
118+
end
119+
if dt.gui.libs.global_toolbox.grouping then
120+
-- toggle the grouping to make the new leader show
121+
toggle_global_toolbox_grouping()
122+
end
123+
end
124+
125+
local function make_existing_jpg_group_leader(images)
126+
for _, image in ipairs(images) do
127+
if string.lower(df.get_filetype(image.filename)) == "jpg" then
128+
if #image:get_group_members() > 1 then
129+
image:make_group_leader()
130+
end
131+
end
132+
end
133+
if dt.gui.libs.global_toolbox.grouping then
134+
-- toggle the grouping to make the new leader show
135+
toggle_global_toolbox_grouping()
136+
end
137+
end
138+
139+
-- - - - - - - - - - - - - - - - - - - - - - - -
140+
-- D A R K T A B L E I N T E G R A T I O N
141+
-- - - - - - - - - - - - - - - - - - - - - - - -
142+
143+
local function destroy()
144+
if dt.preferences.read(MODULE, "on_import", "bool") then
145+
dt.destroy_event(MODULE, "post-import-film")
146+
dt.destroy_event(MODULE, "post-import-image")
147+
end
148+
dt.destroy_event(MODULE .. "_collect", "shortcut")
149+
dt.destroy_event(MODULE .. "_select", "shortcut")
150+
end
151+
152+
script_data.destroy = destroy
153+
154+
-- - - - - - - - - - - - - - - - - - - - - - - -
155+
-- E V E N T S
156+
-- - - - - - - - - - - - - - - - - - - - - - - -
157+
158+
if dt.preferences.read(MODULE, "on_import", "bool") then
159+
dt.register_event(MODULE, "post-import-film",
160+
function(event, film_roll)
161+
-- ignore the film roll, it contains all the images, not just the imported
162+
local images = jgloi.images
163+
if #images > 0 then
164+
jgloi.images = {}
165+
make_jpg_group_leader(images)
166+
end
167+
end
168+
)
169+
170+
dt.register_event(MODULE, "post-import-image",
171+
function(event, image)
172+
if string.lower(df.get_filetype(image.filename)) == "jpg" then
173+
table.insert(jgloi.images, image)
174+
end
175+
end
176+
)
177+
end
178+
179+
dt.register_event(MODULE .. "_collect", "shortcut",
180+
function(event, shortcut)
181+
-- ignore the film roll, it contains all the images, not just the imported
182+
local images = dt.collection
183+
make_existing_jpg_group_leader(images)
184+
end,
185+
_("Make jpg group leader for collection")
186+
)
187+
188+
dt.register_event(MODULE .. "_select", "shortcut",
189+
function(event, shortcut)
190+
local images = dt.gui.selection()
191+
make_existing_jpg_group_leader(images)
192+
end,
193+
_("Make jpg group leader for selection")
194+
)
195+
196+
return script_data

0 commit comments

Comments
 (0)