File: model_selector.rb

package info (click to toggle)
mikutter 4.1.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,260 kB
  • sloc: ruby: 20,126; sh: 183; makefile: 19
file content (70 lines) | stat: -rw-r--r-- 1,709 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
module Plugin::Activity
  class ModelSelector < Gtk::TreeView
    ICON = 0
    MODEL_KIND = 1
    TITLE = 2
    MODEL = 3
    def initialize(*rest)
      super
      initialize_model
      initialize_column
      set_headers_visible(false)
      self.ssc(:row_activated, &self.class.open_block)
    end

    def set(models)
      model.clear
      models.each do |record|
        iter = model.append
        if record.respond_to? :icon
          iter[ICON] = record.icon.load_pixbuf(width: 24, height: 24) do |loaded_icon|
            iter[ICON] = loaded_icon
          end
        end
        iter[MODEL_KIND] = record.class.spec[:name]
        iter[TITLE] = record.title
        iter[MODEL] = record
      end
    end

    private

    def initialize_model
      set_model(Gtk::ListStore.new(GdkPixbuf::Pixbuf, String, String, Diva::Model))
    end

    def initialize_column
      initialize_column_icon
      initialize_column_type
      initialize_column_title
    end

    def initialize_column_icon
      col = Gtk::TreeViewColumn.new('icon', Gtk::CellRendererPixbuf.new, pixbuf: 0)
      col.resizable = false
      append_column(col)
    end

    def initialize_column_type
      col = Gtk::TreeViewColumn.new('kind', Gtk::CellRendererText.new, text: 1)
      col.resizable = false
      append_column(col)
    end

    def initialize_column_title
      col = Gtk::TreeViewColumn.new('title', Gtk::CellRendererText.new, text: 2)
      col.resizable = false
      append_column(col)
    end

    def self.open_block
      ->(treeview, path, column) {
        iter = treeview.model.get_iter(path)
        Plugin.call(:open, iter[MODEL])
        false
      }
    end

  end
end