Hi Holger,
There is no such component in DT Lua API, however you can create simply
workaround just putting list of the check buttons. You can make it a bit
better If you add horizontal box with two buttons to change order and
checkbox to make it selectable. I've prepare simple example.

BR,
Dominik


sob., 17 lut 2018 o 10:35 użytkownik Holger Klemm <
darkta...@multimedia4linux.de> napisał:

> Hello,
> I wrote a lua script to export a slideshow.
> https://www.multimedia4linux.de/images/darktable/plugins/
> slideshow_export-1.1.0.tar
>
>
> https://www.multimedia4linux.de/index.php/bildbearbeitung/darktable/darktable-plugin-diashow-export
>
> At the moment you can only attach one audio file.
>
> I would like to attach several audio files and put them in a list box (as
> used
> in the tag module).  The files should then be selectable in the list box
> to be
> able to change the order, to be able to remove the file or to be able to
> play
> the selected file.
>
> Unfortunately, I found no way in Lua to create such a listbox.
> A multi-line label or text_view does not work and does not look like a
> listbox.
>
> Does anyone have an idea how I can implement the listbox or could you
> integrate the GUI element in LUA?
>
> greeting
> Holger
> ___________________________________________________________________________
> darktable developer mailing list
> to unsubscribe send a mail to
> darktable-dev+unsubscr...@lists.darktable.org
>
> --
Best Regards,
Dominik Markiewicz

___________________________________________________________________________
darktable developer mailing list
to unsubscribe send a mail to darktable-dev+unsubscr...@lists.darktable.org

local dt = require 'darktable'

--- List component

-- private utils methods 

local list_metatable = {}
list_metatable.__index = list_metatable

function list_metatable._add_single_row(self, id)
  local function check_callback(w)
    self.values[id].value = w.value
    if self.on_select then
      self.on_select(id, self.values[id].text, self.values[id].value)
    end
  end 

  local function up_callback(w)
    self.values[id - 1], self.values[id] = self.values[id], self.values[id - 1]
    self:_update()
    if self.on_change_order then
      self.on_change_order(id-1, id, self.values)
    end
  end

  local function down_callback(w)
    self.values[id + 1], self.values[id] = self.values[id], self.values[id + 1]
    self:_update()
    if self.on_change_order then
      self.on_change_order(id, id + 1, self.values)
    end
  end 

  local hbox = dt.new_widget('box'){orientation='horizontal'}
  self._boxes[id] = {
    box = hbox, 
    up =  dt.new_widget('button'){label='▲', sensitive=id>1, clicked_callback=up_callback},
    down = dt.new_widget('button'){label='▼', sensitive=#self.values>id, clicked_callback=down_callback},
    cb = dt.new_widget('check_button'){label='', clicked_callback=check_callback, value=false}
  }
  hbox[1] = self._boxes[id].up
  hbox[2] = self._boxes[id].down
  hbox[3] = self._boxes[id].cb
  self.widget[id] = hbox
end

function list_metatable._remove_single_row(self, idx)
  self._boxes[idx].box[3] = nil
  self._boxes[idx].box[2] = nil
  self._boxes[idx].box[1] = nil
  self._boxes[idx] = nil
  self.widget[idx] = nil
end

function list_metatable._clear_container(self)
  old_values_num = #self.values
  for idx = #self.values,1,-1 do
    self:_remove_single_row(idx)
  end
end

function list_metatable._update_items_number(self, new)
  local old = #self.values
  if old == new then return end
  if old < new then
	  for idx = old+1,new do
      if idx > 1 then
        self._boxes[idx-1].down.sensitive = true 
      end
      self:_add_single_row(idx)
	  end
  else  
    for idx = old,new+1,-1 do
      self:_remove_single_row(idx)
    end
    if new >= 1 then
      self._boxes[new].down.sensitive = false
    end
  end 
end 

function list_metatable._update(self)
  local len = #self.values
  for id, entry in ipairs(self.values) do
	  self._boxes[id].cb.label = entry.text
    self._boxes[id].cb.value = entry.value
  end
end

-- public API

function list_metatable.insert_value(self, idx, value)
  if idx > #self.values then
	  idx = #self.values + 1
  elseif idx < 1 then
	  idx = 1
  end
  self:_update_items_number(#self.values + 1)
  table.insert(self.values, idx, {text=value, value=false})
  self:_update()
end

function list_metatable.remove_value(self, idx)
   self:_update_items_number(#self.values - 1)
   table.remove(self.values, idx)
   self:_update()
end

function list_metatable.remove_values(self, idxs)
  self:_update_items_number(#self.values - #idxs)
  table.sort(idxs, function(a,b) return a > b end)
  for _, idx in ipairs(idxs) do
    table.remove(self.values, idx)
  end
  self:_update()
end 

function list_metatable.append_values(self, values)
  self:_update_items_number(#self.values + #values)
  for _, v in ipairs(values) do
    table.insert(self.values, {text= v, value= false})
  end
  self:_update()
end 

local function List(selection_callback, change_order_callback)
   local container = dt.new_widget('box'){}
   local obj = {
     widget = container,
     values = {},
     on_select = selection_callback,
     on_change_order = change_order_callback,
     _boxes = {} -- this is dueto bug that is fixed in #1496 - I cannot access member of the collection by id...
   }
   return setmetatable(obj, list_metatable)
end

-- usage example stuff

local function selection_callback(id, txt, value)
  dt.print(string.format("%s %s %s", id, txt, value))
end

local function change_order_callback(id1, id2, values)
  dt.print(string.format("%s (%s) <-> %s (%s)", values[id1].text, values[id2].text, id1, id2))
end

local l1 = List(selection_callback, change_order_callback)

l1:append_values({'Lorem ipsum dolor sit amet', 'consectetur adipiscing elit', 'consectetur adipiscing elit'})

local add_random = dt.new_widget('button'){
  label='+', 
  clicked_callback=function() 
    local label =  "test " .. tostring(math.random(0, 1000))
    local position = math.random(0, 10)
    l1:insert_value(position, label) 
  end
}

local remove_selected = dt.new_widget('button'){
  label='remove selected', 
  clicked_callback = function()
     local to_remove = {}
     for idx, entry in ipairs(l1.values) do
	if entry.value then
	   table.insert(to_remove, idx)
	end 
     end
     l1:remove_values(to_remove)
  end
}


local box = dt.new_widget('box'){}
box[1] = l1.widget --  GOTCHA, you need to get widget out of the table
box[2] = add_random
box[3] = remove_selected

dt.register_lib(
	'test_lib', 
	'test', 
	true, 
	false, 
	{[dt.gui.views.lighttable] = {"DT_UI_CONTAINER_PANEL_LEFT_CENTER", 100}},
	box,
	nil,
	nil
)

Reply via email to