Add a manager for import filters + 2 examples
This adds a new callback to darktable: darktable.register_import_filter(name, callback)
This commit is contained in:
79
official/import_filter_manager.lua
Normal file
79
official/import_filter_manager.lua
Normal file
@@ -0,0 +1,79 @@
|
||||
--[[
|
||||
This file is part of darktable,
|
||||
copyright (c) 2015 Tobias Ellinghaus
|
||||
|
||||
darktable is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
darktable is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with darktable. If not, see <http://www.gnu.org/licenses/>.
|
||||
]]
|
||||
--[[
|
||||
IMPORT FILTER MANAGER
|
||||
This script adds a dropdown list with import filters to the import dialog.
|
||||
Scripts can add new filters by registering them with
|
||||
darktable.register_import_filter(name, callback)
|
||||
The callback has type function(event, images), i.e., it is the same as when
|
||||
directly registering the pre-import event.
|
||||
|
||||
|
||||
USAGE
|
||||
* require this script from your main lua file
|
||||
* also require some files with import filters, for example import_filters.lua.
|
||||
it is important to add them AFTER this one!
|
||||
]]
|
||||
|
||||
local dt = require "darktable"
|
||||
|
||||
local import_filter_list = {}
|
||||
local n_import_filters = 1
|
||||
|
||||
-- allow changing the filter from the preferences
|
||||
dt.preferences.register("import_filter_manager", "active_filter", "string",
|
||||
"import filter", "the name of the filter used for importing images", "")
|
||||
|
||||
|
||||
-- the dropdown to select the active filter from the import dialog
|
||||
local filter_dropdown = dt.new_widget("combobox")
|
||||
{
|
||||
label = "import filter",
|
||||
editable = false,
|
||||
|
||||
changed_callback = function(widget)
|
||||
dt.preferences.write("import_filter_manager", "active_filter", "string", widget.value)
|
||||
end,
|
||||
|
||||
"" -- the first entry in the list is hard coded to "" so it's possible to have no filter
|
||||
}
|
||||
dt.gui.libs.import.register_widget(filter_dropdown)
|
||||
|
||||
|
||||
-- this is just a wrapper which calls the active import filter
|
||||
dt.register_event("pre-import", function(event, images)
|
||||
local active_filter = dt.preferences.read("import_filter_manager", "active_filter", "string")
|
||||
if active_filter == "" then return end
|
||||
local callback = import_filter_list[active_filter]
|
||||
if callback then callback(event, images) end
|
||||
end)
|
||||
|
||||
|
||||
-- add a new global function to register import filters
|
||||
dt.register_import_filter = function(name, callback)
|
||||
local active_filter = dt.preferences.read("import_filter_manager", "active_filter", "string")
|
||||
dt.print_error("registering import filter `" .. name .. "'")
|
||||
import_filter_list[name] = callback
|
||||
n_import_filters = n_import_filters + 1
|
||||
filter_dropdown[n_import_filters] = name
|
||||
if name == active_filter then filter_dropdown.value = n_import_filters end
|
||||
end
|
||||
|
||||
|
||||
-- vim: shiftwidth=2 expandtab tabstop=2 cindent
|
||||
-- kate: tab-indents: off; indent-width 2; replace-tabs on; remove-trailing-space on;
|
||||
80
official/import_filters.lua
Normal file
80
official/import_filters.lua
Normal file
@@ -0,0 +1,80 @@
|
||||
--[[
|
||||
This file is part of darktable,
|
||||
copyright (c) 2015 Tobias Ellinghaus
|
||||
|
||||
darktable is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
darktable is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with darktable. If not, see <http://www.gnu.org/licenses/>.
|
||||
]]
|
||||
--[[
|
||||
EXAMPLE IMPORT FILTERS
|
||||
This script goes along with the import filter manager. It adds two filters:
|
||||
* ignore jpegs: this one does the same as the existing option in the import dialog
|
||||
and just skips all JPEGs during import.
|
||||
* prefer raw over jpeg: this one is a bit more elaborate, it ignores JPEGs when there
|
||||
is also another file with the same basename, otherwise it
|
||||
allows JPEGs, too.
|
||||
|
||||
USAGE
|
||||
* require this script from your main lua file AFTER import_filter_manager.lua
|
||||
]]
|
||||
|
||||
local dt = require "darktable"
|
||||
|
||||
-- we get fed a sorted list of filenames. just setting images to ignore to nil is enough
|
||||
|
||||
-- ignore jpeg
|
||||
dt.register_import_filter("ignore jpegs", function(event, images)
|
||||
dt.print_error("ignoring all jpegs")
|
||||
for i, img in ipairs(images) do
|
||||
local extension = img:match("[^.]*$"):upper()
|
||||
if (extension == "JPG") or (extension == "JPEG") then
|
||||
images[i] = nil
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
|
||||
-- ignore jpeg iff another format for the image is found
|
||||
dt.register_import_filter("prefer raw over jpeg", function(event, images)
|
||||
dt.print_error("prefering raw over jpeg")
|
||||
local current_base = ""
|
||||
local jpg_indices = {}
|
||||
local other_format_found = false
|
||||
for i, img in ipairs(images) do
|
||||
local extension = img:match("[^.]*$"):upper()
|
||||
local base = img:match("^.*[.]")
|
||||
|
||||
if base ~= current_base then
|
||||
-- we are done with the base name, act according to what we found out
|
||||
if other_format_found then
|
||||
for _, jpg in ipairs(jpg_indices) do
|
||||
images[jpg] = nil
|
||||
end
|
||||
end
|
||||
current_base = base
|
||||
other_format_found = false
|
||||
for k,_ in pairs(jpg_indices) do jpg_indices[k] = nil end
|
||||
end
|
||||
|
||||
-- remember what we have here to act accordingly after all instances of this base name were checked
|
||||
if (extension == "JPG") or (extension == "JPEG") then
|
||||
table.insert(jpg_indices, i)
|
||||
else
|
||||
other_format_found = true
|
||||
end
|
||||
|
||||
end
|
||||
end)
|
||||
|
||||
-- vim: shiftwidth=2 expandtab tabstop=2 cindent
|
||||
-- kate: tab-indents: off; indent-width 2; replace-tabs on; remove-trailing-space on;
|
||||
Reference in New Issue
Block a user