forked from darktable-org/lua-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicktag.lua
More file actions
252 lines (192 loc) · 7.37 KB
/
Copy pathquicktag.lua
File metadata and controls
252 lines (192 loc) · 7.37 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
--[[
This file is part of darktable,
Copyright (C) 2017 by Christian Kanzian
Thanks to
Copyright (C) 2016 Bill Ferguson
Copyright (C) 2017 Tobias Jakobs
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
]]
--[[
QUICKTAG
For faster attaching your favorite tags, the script adds shortcuts and
the module "quicktag" in lighttable mode with a changeable number of buttons.
To each button a tag can be assigned. If the tags do not exist in your database,
they are added to the database once they get the first time attached to an image.
The number of buttons/shortcuts can be changed in the lua preferences.
Changes in the number require a restart of darktable.
INSTALATION
* copy this file in $CONFIGDIR/lua/ where CONFIGDIR is your darktable configuration directory
* add the following line in the file $CONFIGDIR/luarc require "quicktag"
USAGE
* set the number of quicktags between 2 and 10 in the preferences dialog and restart darktable
* if wanted set the shortcuts in the preferences dialog
* to add or change a quicktag, first select the old tag with the combobox "old quicktag",
enter a new tag in the "new quicktag" filed and press "set quicktag"
* use a shortcut or button to attach the tag to selected images
]]
local dt = require "darktable"
local du = require "lib/dtutils"
local debug = require "darktable.debug"
local gettext = dt.gettext
du.check_min_api_version("3.0.0", "quicktag")
-- Tell gettext where to find the .mo file translating messages for a particular domain
gettext.bindtextdomain("quicktag",dt.configuration.config_dir.."/lua/locale/")
local function _(msgid)
return gettext.dgettext("quicktag", msgid)
end
-- maximum length of button labels
dt.preferences.register("quickTag",
"labellength",
"integer", -- type
_("max. length of button labels"), -- label
_("may range from 15 to 60 - needs a restart"), -- tooltip
25, -- default
15, -- min
60)
local max_label_length = dt.preferences.read("quickTag", "labellength", "integer")
-- register number of quicktags
dt.preferences.register("quickTag",
"quickTagnumber",
"integer", -- type
_("number of quicktag fields"), -- label
_("may range from 2 to 20 - needs a restart"), -- tooltip
5, -- default
2, -- min
20)
local qnr = dt.preferences.read("quickTag", "quickTagnumber", "integer")
-- get quicktags from the preferences
local quicktag_table = {}
local function read_pref_tags()
for j=1,qnr do
quicktag_table[j] = dt.preferences.read("quickTag", "quicktag"..j, "string")
end
end
local quicktag_label = {}
local function abbrevate_tags(t)
for i=1,qnr do
local taglength = string.len(t[i])
if taglength > max_label_length then
local max_2 = math.floor(max_label_length/2)
quicktag_label[i] = string.sub(t[i],0,max_2).."..."..string.sub(t[i],taglength - max_2)
else
quicktag_label[i] = t[i]
end
-- print(quicktag_label[i])
end
end
-- quicktag function to attach tags
local function tagattach(tag,qtagnr)
if tag == "" then
dt.print(string.format(_("quicktag %i is empty, please set a tag"), qtagnr))
return true
end
local tagnr = dt.tags.find(tag)
--create tag if it does not exist
if tagnr == nil then
dt.tags.create(tag)
tagnr = dt.tags.find(tag)
end
local sel_images = dt.gui.action_images
if next(sel_images) == nil then
dt.print(_("no images selected"))
return true
end
local counter = 0
for _,image in ipairs(sel_images) do
dt.tags.attach(tagnr,image)
counter = counter+1
end
dt.print(string.format(_("tag \"%s\" attached to %i image(s)"), tag, counter))
end
-- create quicktag module elements
-- read tags from preferences for initialization
read_pref_tags()
abbrevate_tags(quicktag_table)
local button = {}
-- function to create buttons with tags as labels
for j=1,qnr do
button[#button+1] = dt.new_widget("button") {
label = j..": "..quicktag_table[j],
clicked_callback = function() tagattach(tostring(quicktag_table[j]),j) end}
end
local old_quicktag = dt.new_widget("combobox"){
label = _("old tag"),
tooltip = _("select the quicktag to replace")
}
local function update_quicktag_list()
-- abrevate long tags for the labels
abbrevate_tags(quicktag_table)
for j=1,qnr do
button[j].label = j..": "..quicktag_label[j]
old_quicktag[j] = j..": "..quicktag_table[j]
end
end
update_quicktag_list()
local new_quicktag = dt.new_widget("entry"){
text = "",
placeholder = _("new tag"),
is_password = true,
editable = true,
tooltip = _("enter your tag here")
}
local set_quicktag_button = dt.new_widget("button") {
label = _("set tag"),
clicked_callback = function()
local old_tag = quicktag_table[old_quicktag.selected]
if new_quicktag.text == "" then
dt.print(_("new quicktag is empty!"))
else
quicktag_table[old_quicktag.selected] = new_quicktag.text
dt.preferences.write("quickTag", "quicktag"..old_quicktag.selected, "string", new_quicktag.text)
dt.print(string.format(_("quicktag \"%s\" replaced by \"%s\""), old_tag, new_quicktag.text))
update_quicktag_list()
new_quicktag.text = ""
end
end,
}
local new_qt_widget = dt.new_widget ("box") {
orientation = "horizontal",
dt.new_widget("label") { label = _("new tag") },
new_quicktag,
set_quicktag_button
}
-- back UI elements in a table
-- thanks to wpferguson for the hint
local widget_table = {}
for i=1,qnr do
widget_table[#widget_table + 1] = button[i]
end
widget_table[#widget_table + 1] = dt.new_widget("separator"){}
widget_table[#widget_table + 1] = old_quicktag
widget_table[#widget_table + 1] = new_qt_widget
--create module
dt.register_lib(
"quicktag", -- Module name
"quicktag", -- name
true, -- expandable
false, -- resetable
{[dt.gui.views.lighttable] = {"DT_UI_CONTAINER_PANEL_RIGHT_CENTER", 490}},
dt.new_widget("box"){
orientation = "vertical",
table.unpack(widget_table),
},
nil,-- view_enter
nil -- view_leave
)
-- create shortcuts
for i=1,qnr do
dt.register_event("shortcut",
function(event, shortcut) tagattach(tostring(quicktag_table[i])) end,
string.format(_("quicktag %i"),i))
end
-- vim: shiftwidth=2 expandtab tabstop=2 cindent syntax=lua
-- kate: tab-indents: off; indent-width 2; replace-tabs on; remove-trailing-space on;