Class: Sh::Browse

Inherits:
Object show all
Defined in:
lib/sh_browse.rb

Constant Summary collapse

@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ Browse

Returns a new instance of Browse.



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/sh_browse.rb', line 5

def initialize view
	Sh::Log.warning 'Instance of Browse already created!' if @@instance
	@@instance = self
  @tree = TreeView.new
  @tree.selection.mode = Gtk::SELECTION_MULTIPLE

  ren_artist = CellRendererText.new
  col_artist = TreeViewColumn.new('Songs', ren_artist)
  col_artist.set_cell_data_func(ren_artist) do |tvc, cell, model, iter|
    cell.text = ''
    data = iter[0]
    if data.is_a? Sh::Song
      if data.title
        cell.text = sprintf("%.2d\t%s", data.track_num, data.title)
      else
        cell.text = data.path.split('/').last
      end
    else
      cell.text = data.to_s
    end
  end
  @tree.append_column col_artist

  @model = TreeStore.new(Object, Array)
#      TODO: use this
#      @model.set_default_sort_func do |a, b|
#        oa, ob = a[0], b[0]
#        oa.to_s <=> ob.to_s
#      end
#      @model.set_sort_column_id(TreeSortable::DEFAULT_SORT_COLUMN_ID,
#      	Gtk::SORT_ASCENDING)
  @tree.model = @model

  GLib::Timeout.add(1000) do
    fill_model
    false
  end

  @tree.signal_connect('button-press-event') do |widget, event|
    # Right-click
    if event.button == 3
      path = @tree.get_path_at_pos(event.x, event.y).first
      data = @model.get_iter(path)[0]
      selection = @tree.selection
      selected_rows = selection.selected_rows
      clicked_in_selection = false
      selected_rows.each do |sel|
        clicked_in_selection = sel.indices == path.indices
        break if clicked_in_selection
      end

      unless clicked_in_selection
        # Change the selection
        selection.unselect_all
        selection.select_path(path)
        selected_rows = selection.selected_rows
      end

      #puts selected_rows

      if data.is_a? Sh::Song
        song = data
        menu = Menu.new
        
        Actions.get(:song).each do |action|
       	pbtn = nil
       	if action[:stock_image]
       		pbtn = ImageMenuItem.new(action.name)
       		pbtn.image = Image.new(action[:stock_image], IconSize::MENU)
       	else
         	pbtn = MenuItem.new(action.name)
         end
       	menu.append pbtn
			if action.tags.include? :playlist
				pmnu = Gtk::Menu.new
				Sh::Playlists.load_playlists.each do |playlist|
        	pbtn_playlist = Gtk::MenuItem.new playlist.name
        	pbtn_playlist.signal_connect('activate') do |widget|
        		action.execute song, playlist
        	end
        	pmnu.append pbtn_playlist
        end
        pbtn.submenu = pmnu
			else
       	pbtn.signal_connect('activate') do |widget|
       		action.execute song
       	end
       end
        end
        
        menu.show_all
        menu.popup(nil, nil, event.button, event.time)
      end
    end
  end
  @tree.signal_connect('row_activated') do |widget, path, col|
    iter = @tree.model.get_iter(path)
    parent = iter.parent
    song = iter[0]
    if song.is_a? Sh::Song
      view.stop
      queue = []
      while iter.parent == parent
        queue << iter[0]
        iter.next!
      end
      prev = []
      iter = parent.first_child
      until iter.path.indices == path.indices
        prev << iter[0]
        iter.next!
      end
      queue.insert 0, *prev
      view.queue = queue
      view.queue_pos = prev.size
      view.play
    else
      view.stop
      view.queue = iter[1].dup
      view.play
    end
  end

  @scroll = ScrolledWindow.new(nil, nil)
  @scroll.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
  @scroll.add @tree
end

Class Method Details

.instanceObject



133
134
135
# File 'lib/sh_browse.rb', line 133

def self.instance
	return @@instance
end

Instance Method Details

#fill_modelObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/sh_browse.rb', line 197

def fill_model
	@model.clear
  artist_node = album_node = track_node = nil
  $db.songs.sort_by {|a| (a.to_s || '')}.each do |song|
    artist = song.artist
    if not artist_node or artist_node[0].db_id != artist.db_id
      artist_node = @model.append nil
      artist_node[0] = artist
      # Required for proper handling of multiple artists with on album
      album_node = nil
    end
    artist_node[1] ||= []
    artist_node[1] << song

    album = song.album
    if not album_node or album_node[0].db_id != album.db_id
      album_node = @model.append artist_node
      album_node[0] = album
    end
    album_node[1] ||= []
    album_node[1] << song

    track_node = @model.append album_node
    track_node[0] = song
    track_node[1] = [song]

    Kelp.process_events
  end
end

#insert_song(song) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/sh_browse.rb', line 160

def insert_song song
  artist_node = album_node = prev_song_node = nil
  @model.each do |model, path, iter|
    data = iter[0]

    if data.is_a? Artist
      artist_node = iter if data.db_id == song.artist.db_id
    elsif artist_node and data.is_a? Album
      album_node = iter if iter.parent == artist_node and data.db_id == song.album.db_id
    end

    if album_node and iter.parent == album_node and data.is_a? Song
      if data.track_num < song.track_num
        prev_song_node = iter
      end
    end
  end
  # Artist node
  unless artist_node
    artist_node = @model.append nil
    artist_node[0] = song.artist
    artist_node[1] = []
  end
  artist_node[1] << song
  # Album node
  unless album_node
    album_node = @model.append artist_node
    album_node[0] = song.album
    album_node[1] = []
  end
  album_node[1] << song
  # Song node
  song_node = @model.insert_after(album_node, prev_song_node)
  song_node[0] = song
  song_node[1] = [song]
end

#remove_song(song) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/sh_browse.rb', line 137

def remove_song song
  song_node = nil
  if song.is_a? TreeIter
    song_node = song
  else
    @model.each do |model, path, iter|
      if iter[0] == song
        song_node = iter
      end
    end
  end
  
  if song_node
    album_node = song_node.parent
    artist_node = album_node.parent
    @model.remove song_node
    album_node[1].delete song
    @model.remove album_node unless album_node.has_child?
    artist_node[1].delete song
    @model.remove artist_node unless artist_node.has_child?
  end
end

#widgetObject



227
228
229
# File 'lib/sh_browse.rb', line 227

def widget
  return @scroll
end