Class: Sirens::ListView

Inherits:
WidgetView show all
Defined in:
lib/views/list_view.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from WidgetView

#add_view, #apply_prop, #apply_props, #initialize, #main_handle

Methods inherited from AbstractView

accepted_styles, #accepted_styles, #add_view, #attribute_at, #background_color=, #foreground_color=, #height, #height=, #initialize, #main_handle, #populate_popup_menu_block=, #remove_view, #set_attribute, #show, #show_popup_menu, #state_colors_from, #width, #width=

Constructor Details

This class inherits a constructor from Sirens::WidgetView

Class Method Details

.view_accepted_stylesObject

Answer the styles accepted by this view.



10
11
12
# File 'lib/views/list_view.rb', line 10

def view_accepted_styles()
    super() + [:show_headers, :clickable_headers].freeze
end

Instance Method Details

#add_column_with_props(props) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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:) ⇒ Object



169
170
171
172
173
# 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:) ⇒ Object

Adding



163
164
165
166
167
# 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_itemsObject

Actions



157
158
159
# File 'lib/views/list_view.rb', line 157

def clear_items()
    list_store.clear
end

#clickable_headers=(boolean) ⇒ Object



128
129
130
# File 'lib/views/list_view.rb', line 128

def clickable_headers=(boolean)
    tree_view.headers_clickable = boolean
end

#clickable_headers?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/views/list_view.rb', line 132

def clickable_headers?()
    tree_view.headers_clickable?
end

#define_columns(columns_props_array) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# 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) ⇒ Object



211
212
213
214
215
216
217
218
219
# 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) ⇒ Object



40
41
42
43
44
# File 'lib/views/list_view.rb', line 40

def get_item_block(&block)
    @get_item_block = block

    self
end

#initialize_handlesObject

Initializing



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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_storeObject

Accessing



100
101
102
# File 'lib/views/list_view.rb', line 100

def list_store()
    tree_view.model
end

#list_store_type_for(column_props) ⇒ Object

Building columns



48
49
50
51
52
# 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) ⇒ Object

Handlers



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# 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) ⇒ Object

Configuring callbacks



34
35
36
37
38
# 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:) ⇒ Object



197
198
199
200
201
# 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:) ⇒ Object

Removing



191
192
193
194
195
# 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

#rowsObject

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



112
113
114
115
116
# File 'lib/views/list_view.rb', line 112

def rows()
    list_store.collect { |store, path, iter|
        iter[0]
    }
end

#selection_indicesObject

Querying



223
224
225
226
227
228
229
230
231
# 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:) ⇒ Object



203
204
205
206
207
208
209
# 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) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
# 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) ⇒ Object

Styles



120
121
122
# File 'lib/views/list_view.rb', line 120

def show_headers=(boolean)
    tree_view.headers_visible = boolean
end

#show_headers?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/views/list_view.rb', line 124

def show_headers?()
    tree_view.headers_visible?
end

#subscribe_to_ui_eventsObject

Hooking GUI signals



88
89
90
91
92
93
94
95
96
# 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) ⇒ Object

Utility methods



250
251
252
253
# File 'lib/views/list_view.rb', line 250

def tree_path_to_index(tree_path)
    tree_path.to_s
        .to_i
end

#tree_viewObject



104
105
106
# File 'lib/views/list_view.rb', line 104

def tree_view()
    @tree_view
end

#update_item(item:, index:) ⇒ Object



183
184
185
186
187
# 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:) ⇒ Object

Updating



177
178
179
180
181
# 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