class Sirens::ListView

Public Class Methods

view_accepted_styles() click to toggle source

Answer the styles accepted by this view.

Calls superclass method
# File lib/views/list_view.rb, line 10
def view_accepted_styles()
    super() + [:show_headers, :clickable_headers].freeze
end

Public Instance Methods

add_column_with_props(props) click to toggle source
# File lib/views/list_view.rb, line 66
def add_column_with_props(props)
    column_index = tree_view.columns.size

    col = nil

    column_label = props[:label]

    if props.has_image_block?
        renderer = Gtk::CellRendererPixbuf.new

        col = Gtk::TreeViewColumn.new(column_label, renderer, pixbuf: column_index)
    else
        renderer = Gtk::CellRendererText.new

        col = Gtk::TreeViewColumn.new(column_label, renderer, text: column_index)
    end

    tree_view.append_column(col)
end
add_item(item:, index:) click to toggle source
# File lib/views/list_view.rb, line 169
def add_item(item:, index:)
    iter = list_store.insert(index)

    set_item_column_values(item: item, iter: iter)
end
add_items(items:, index:) click to toggle source

Adding

# File lib/views/list_view.rb, line 163
def add_items(items:, index:)
    items.each_with_index do |each_item, i|
        add_item(item: each_item, index: index + i)
    end
end
clear_items() click to toggle source

Actions

# File lib/views/list_view.rb, line 157
def clear_items()
    list_store.clear
end
clickable_headers=(boolean) click to toggle source
# File lib/views/list_view.rb, line 128
def clickable_headers=(boolean)
    tree_view.headers_clickable = boolean
end
clickable_headers?() click to toggle source
# File lib/views/list_view.rb, line 132
def clickable_headers?()
    tree_view.headers_clickable?
end
define_columns(columns_props_array) click to toggle source
# File lib/views/list_view.rb, line 54
def define_columns(columns_props_array)
    @columns_props = columns_props_array

    list_store_types = @columns_props.collect { |type| list_store_type_for(type) }

    tree_view.set_model(Gtk::ListStore.new(*list_store_types))

    @columns_props.each do |each_column_props|
        add_column_with_props(each_column_props)
    end
end
display_data_of(item, column, column_index) click to toggle source
# File lib/views/list_view.rb, line 211
def display_data_of(item, column, column_index)
    if column.has_image_block?
        image_file = column.display_image_of(item).to_s

        return GdkPixbuf::Pixbuf.new(file: image_file, width: 16, height: 16)
    end

    column.display_text_of(item)
end
get_item_block(&block) click to toggle source
# File lib/views/list_view.rb, line 40
def get_item_block(&block)
    @get_item_block = block

    self
end
initialize_handles() click to toggle source

Initializing

# File lib/views/list_view.rb, line 17
def initialize_handles()
    @tree_view = Gtk::TreeView.new()
    @tree_view.set_model(Gtk::ListStore.new(String))

    @main_handle = Gtk::ScrolledWindow.new
    @main_handle.add(tree_view)
    @main_handle.set_policy(:automatic, :automatic)

    @current_selection_indices = []

    @columns_props = []

    @on_selection_changed = nil
end
list_store() click to toggle source

Accessing

# File lib/views/list_view.rb, line 100
def list_store()
    tree_view.model
end
list_store_type_for(column_props) click to toggle source

Building columns

# File lib/views/list_view.rb, line 48
def list_store_type_for(column_props)
    return GdkPixbuf::Pixbuf if column_props.has_image_block?

    String
end
on_selection_changed(tree_selection) click to toggle source

Handlers

# File lib/views/list_view.rb, line 138
def on_selection_changed(tree_selection)
    indices = []
    items = []

    tree_selection.each do |tree_store, tree_path, iter|
        index = tree_path_to_index(tree_path)

        indices << index
        items << @get_item_block.call(index)
    end

    @on_selection_changed_block.call(
        selection_items: items,
        selection_indices: indices
    )
end
on_selection_changed_block(&block) click to toggle source

Configuring callbacks

# File lib/views/list_view.rb, line 34
def on_selection_changed_block(&block)
    @on_selection_changed_block = block

    self
end
remove_item(item:, index:) click to toggle source
# File lib/views/list_view.rb, line 197
def remove_item(item:, index:)
    iter = list_store.get_iter(index.to_s)

    list_store.remove(iter)
end
remove_items(items:, indices:) click to toggle source

Removing

# File lib/views/list_view.rb, line 191
def remove_items(items:, indices:)
    items.each_with_index do |each_item, i|
        remove_item(item: each_item, index: indices[i])
    end
end
rows() click to toggle source

Returns the rows contents of the list. For testing and debugging only.

# File lib/views/list_view.rb, line 112
def rows()
    list_store.collect { |store, path, iter|
        iter[0]
    }
end
selection_indices() click to toggle source

Querying

# File lib/views/list_view.rb, line 223
def selection_indices()
    indices = []

    tree_view.selection.each { |list, tree_path, iter|
        indices << tree_path_to_index(tree_path)
    }

    indices
end
set_item_column_values(item:, iter:) click to toggle source
# File lib/views/list_view.rb, line 203
def set_item_column_values(item:, iter:)
    @columns_props.each_with_index { |column, column_index|
        colum_value = display_data_of(item, column, column_index)

        iter.set_value(column_index, colum_value)
    }
end
set_selection_indices(indices) click to toggle source
# File lib/views/list_view.rb, line 233
def set_selection_indices(indices)
    if indices.empty?
        tree_view.unselect_all
        return
    end

    path = Gtk::TreePath.new(
            indices.join(':')
        )

    tree_view.selection.select_path(path)

    tree_view.scroll_to_cell(path, nil, false, 0.0, 0.0)
end
show_headers=(boolean) click to toggle source

Styles

# File lib/views/list_view.rb, line 120
def show_headers=(boolean)
    tree_view.headers_visible = boolean
end
show_headers?() click to toggle source
# File lib/views/list_view.rb, line 124
def show_headers?()
    tree_view.headers_visible?
end
subscribe_to_ui_events() click to toggle source

Hooking GUI signals

# File lib/views/list_view.rb, line 88
def subscribe_to_ui_events()
    tree_view.selection.signal_connect('changed') { |tree_selection|
        on_selection_changed(tree_selection)
    }

    tree_view.signal_connect('row-activated') {
        @on_selection_action.call(self) unless @on_selection_action.nil?
    }
end
tree_path_to_index(tree_path) click to toggle source

Utility methods

# File lib/views/list_view.rb, line 250
def tree_path_to_index(tree_path)
    tree_path.to_s
        .to_i
end
tree_view() click to toggle source
# File lib/views/list_view.rb, line 104
def tree_view()
    @tree_view
end
update_item(item:, index:) click to toggle source
# File lib/views/list_view.rb, line 183
def update_item(item:, index:)
    iter = list_store.get_iter(index.to_s)

    set_item_column_values(item: item, iter: iter)
end
update_items(items:, indices:) click to toggle source

Updating

# File lib/views/list_view.rb, line 177
def update_items(items:, indices:)
    items.each_with_index do |each_item, i|
        update_item(item: each_item, index: indices[i])
    end
end