プロジェクト

全般

プロフィール

提案 #1258 » 1258-1.patch

Yuto Tokunaga, 2018-06-02 01:41

差分を表示:

core/plugin/extract/extract.rb
91 91
                                   true } })))
92 92
    Plugin.create :extract do
93 93
      add_tab_observer = on_extract_tab_create(&tablist.method(:add_record))
94
      update_tab_observer = on_extract_tab_update(&tablist.method(:update_record))
94 95
      delete_tab_observer = on_extract_tab_delete(&tablist.method(:remove_record))
95 96
      tablist.ssc(:destroy) do
96 97
        detach add_tab_observer
98
        detach update_tab_observer
97 99
        detach delete_tab_observer end end
98 100
  end
99 101

  
core/plugin/extract/extract_tab_list.rb
7 7
=end
8 8

  
9 9
class Plugin::Extract::ExtractTabList < ::Gtk::TreeView
10
  ITER_NAME = 0
11
  ITER_ID   = 1
10
  ICON_SIZE = 16
11

  
12
  COL_ICON  = 0
13
  COL_NAME  = 1
14
  COL_ID    = 2
15
  COL_SOUND = 3
16
  COL_POPUP = 4
12 17

  
13 18
  def initialize(plugin)
14 19
    type_strict plugin => Plugin
15 20
    @plugin = plugin
16
    super(Gtk::ListStore.new(String, Numeric))
17

  
18
    append_column Gtk::TreeViewColumn.new(plugin._("名前"), Gtk::CellRendererText.new, text: ITER_NAME)
19

  
20
    extract_tabs.each(&method(:add_record)) end
21
    super(Gtk::ListStore.new(
22
      GdkPixbuf::Pixbuf, # icon
23
      String,            # name
24
      Numeric,           # id
25
      GdkPixbuf::Pixbuf, # sound
26
      GdkPixbuf::Pixbuf  # popup
27
    ))
28

  
29
    append_column Gtk::TreeViewColumn.new(
30
      '',
31
      Gtk::CellRendererPixbuf.new,
32
      pixbuf: COL_ICON
33
    )
34
    append_column Gtk::TreeViewColumn.new(
35
      plugin._('名前'),
36
      Gtk::CellRendererText.new,
37
      text: COL_NAME
38
    ).tap { |col| col.expand = true }
39
    append_column Gtk::TreeViewColumn.new(
40
      '',
41
      Gtk::CellRendererPixbuf.new,
42
      pixbuf: COL_SOUND
43
    )
44
    append_column Gtk::TreeViewColumn.new(
45
      '',
46
      Gtk::CellRendererPixbuf.new,
47
      pixbuf: COL_POPUP
48
    )
49

  
50
    extract_tabs.each(&method(:add_record))
51
    register_signal_handlers
52
  end
21 53

  
22 54
  # 現在選択されている抽出タブのIDを返す
23 55
  # ==== Return
24 56
  # 選択されている項目のID。何も選択されていない場合はnil
25 57
  def selected_id
26 58
    selected_iter = selection.selected
27
    selected_iter[ITER_ID] if selected_iter end
59
    selected_iter[COL_ID] if selected_iter end
28 60

  
29 61
  # レコードを追加する
30 62
  # ==== Args
31 63
  # [record] 追加するレコード(Hash)
32
  # ==== Return
33
  # self
34 64
  def add_record(record)
35 65
    iter = model.append
36
    iter[Plugin::Extract::ExtractTabList::ITER_NAME] = record[:name]
37
    iter[Plugin::Extract::ExtractTabList::ITER_ID] = record[:id]
38
    self end
66
    setup_iter iter, record
67
  end
68

  
69
  # レコードをもとにリストビューを更新する
70
  # ==== Args
71
  # [record] 更新されたレコード(Hash)
72
  def update_record(record)
73
    update_iter = model.to_enum
74
                       .map { |_, _, iter| iter }
75
                       .find { |iter| iter[COL_ID] == record[:id] }
76
    setup_iter update_iter, record if update_iter
77
  end
39 78

  
40 79
  # 抽出タブをリストから削除する
41 80
  # ==== Args
......
43 82
  # ==== Return
44 83
  # self
45 84
  def remove_record(record_id)
46
    remove_iter = model.to_enum(:each).map{|_,_,iter|iter}.find{|iter| record_id == iter[ITER_ID] }
85
    remove_iter = model.to_enum(:each).map{|_,_,iter|iter}.find{|iter| record_id == iter[COL_ID] }
47 86
    model.remove(remove_iter) if remove_iter
48 87
    self end
49 88

  
50 89
  private
51 90

  
91
  def register_signal_handlers
92
    # 項目をダブルクリックして設定を開く
93
    ssc(:button_press_event) do |_, ev|
94
      next if ev.event_type != Gdk::Event::BUTTON2_PRESS
95
      id = selected_id
96
      if id
97
        Plugin.call(:extract_open_edit_dialog, id)
98
        true
99
      end
100
    end
101
  end
102

  
52 103
# ==== utility
53 104

  
54 105
  # レコードの配列を返す
......
57 108
  def extract_tabs
58 109
    UserConfig[:extract_tabs] || [] end
59 110

  
111
  # イテレータにレコードの内容をコピーする
112
  # ==== Args
113
  # [iter] TreeIter
114
  # [record] 参照するレコード (Hash)
115
  def setup_iter(iter, record)
116
    size = { width: ICON_SIZE, height: ICON_SIZE }
117
    set_icon = lambda { |col, photo|
118
      iter[col] = photo.load_pixbuf(**size) do |pb|
119
        iter[col] = pb unless destroyed?
120
      end
121
    }
122

  
123
    iter[COL_NAME] = record[:name]
124
    iter[COL_ID] = record[:id]
125
    if record[:icon]
126
      photo = Enumerator.new do |y|
127
        Plugin.filtering :photo_filter, record[:icon], y
128
      end.first
129
      set_icon.call COL_ICON, photo
130
    end
131
    if record[:sound].to_s.empty?
132
      # FIXME: 適当なアイコンを標準スキンに入れる
133
      set_icon.call COL_SOUND, Skin['speaker-x.png']
134
    else
135
      set_icon.call COL_SOUND, Skin['speaker-l.png']
136
    end
137
    if record[:popup]
138
      # FIXME: 適当なアイコンを標準スキンに入れる
139
      set_icon.call COL_POPUP, Skin['popup.png']
140
    else
141
      set_icon.call COL_POPUP, Skin['no-popup.png']
142
    end
143
  end
144

  
60 145
  # def on_created(iter)
61 146
  #   Plugin.call(:extract_tab_create,
62
  #               name: iter[ITER_NAME],
147
  #               name: iter[COL_NAME],
63 148
  #               sexp: iter[ITER_SEXP],
64 149
  #               sources: iter[ITER_SOURCE],
65
  #               id: iter[ITER_ID] = gen_uniq_id) end
150
  #               id: iter[COL_ID] = gen_uniq_id) end
66 151

  
67 152
  # def on_updated(iter)
68 153
  #   Plugin.call(:extract_tab_update,
69
  #               name: iter[ITER_NAME],
154
  #               name: iter[COL_NAME],
70 155
  #               sexp: iter[ITER_SEXP],
71 156
  #               sources: iter[ITER_SOURCE],
72
  #               id: iter[ITER_ID]) end
157
  #               id: iter[COL_ID]) end
73 158

  
74 159
  def gen_uniq_id(uniq_id = Time.now.to_i)
75 160
    if extract_tabs.any?{ |x| x[:id] == uniq_id }
(1-1/3)