Module: Alexandria::ComboBoxOverrides

Extended by:
GetText
Includes:
GetText
Defined in:
lib/alexandria/ui/libraries_combo.rb

Instance Method Summary collapse

Instance Method Details

#append_entry(icon, label, is_new) ⇒ Object



77
78
79
80
81
82
# File 'lib/alexandria/ui/libraries_combo.rb', line 77

def append_entry(icon, label, is_new)
  iter = model.append
  iter[0] = icon
  iter[1] = label
  iter[2] = is_new
end

#populate_with_libraries(libraries, selected_library) ⇒ Object



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
# File 'lib/alexandria/ui/libraries_combo.rb', line 29

def populate_with_libraries(libraries, selected_library)
  libraries_names = libraries.map(&:name)
  if selected_library
    libraries_names.delete selected_library.name
    libraries_names.unshift selected_library.name
  end
  clear
  self.model = Gtk::ListStore.new(GdkPixbuf::Pixbuf, String, TrueClass)
  libraries_names.each do |library_name|
    append_entry(Alexandria::UI::Icons::LIBRARY_SMALL, library_name, false)
  end
  append_entry(nil, "-", nil)
  append_entry(Alexandria::UI::Icons::LIBRARY_SMALL, _("New Library"), true)
  renderer = Gtk::CellRendererPixbuf.new
  pack_start(renderer, false)
  set_attributes(renderer, pixbuf: 0)
  renderer = Gtk::CellRendererText.new
  pack_start(renderer, true)
  set_attributes(renderer, text: 1)
  set_row_separator_func do |model, iter|
    # TODO: Replace with iter[1] if possible
    model.get_value(iter, 1) == "-"
  end
  self.active = 0
  # self.sensitive = libraries.length > 1
  # This prohibits us from adding a "New Library" from this combo
  # when we only have a single library
end

#selection_from_libraries(libraries) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/alexandria/ui/libraries_combo.rb', line 58

def selection_from_libraries(libraries)
  iter = active_iter
  is_new = false
  library = nil
  if iter[2]
    name = Alexandria::Library.generate_new_name(libraries)
    library = Alexandria::Library.load(name)
    libraries << library
    is_new = true
  else
    library = libraries.find do |x|
      x.name == active_iter[1]
    end
  end
  raise unless library

  [library, is_new]
end