Add column enable menu.
[gmpc.git] / src / Widgets / gmpc-data-view.vala
blob771aca6dcb0c7c6bcda312b11d3ada141342c640
1 using Gmpc;
3 using Gtk;
5 const string log_domain = "Gmpc.DataView";
7 /** The Default column width. */
8 const int default_column_width = 200;
10 /**
11 * List of columns.
12 * * List of column ids to show.
13 * * Name of each column
14 * * Default set of enabled columns.
16 const int NUM_COLS = 20;
17 const int[] gmpc_data_view_col_ids = {
18 Gmpc.MpdData.ColumnTypes.MARKUP,
19 Gmpc.MpdData.ColumnTypes.SONG_ARTIST, /* album name */
20 Gmpc.MpdData.ColumnTypes.SONG_ALBUM, /* album name */
21 Gmpc.MpdData.ColumnTypes.SONG_TITLE, /* song title */
22 Gmpc.MpdData.ColumnTypes.SONG_TITLEFILE, /* song title */
23 Gmpc.MpdData.ColumnTypes.SONG_GENRE, /* song genre */
24 Gmpc.MpdData.ColumnTypes.SONG_TRACK, /* song track */
25 Gmpc.MpdData.ColumnTypes.SONG_NAME, /* stream name */
26 Gmpc.MpdData.ColumnTypes.SONG_COMPOSER, /* composer name */
27 Gmpc.MpdData.ColumnTypes.SONG_PERFORMER, /* performer */
28 Gmpc.MpdData.ColumnTypes.SONG_DATE, /* date */
29 Gmpc.MpdData.ColumnTypes.SONG_LENGTH_FORMAT, /* length formatted */
30 Gmpc.MpdData.ColumnTypes.SONG_DISC, /* disc */
31 Gmpc.MpdData.ColumnTypes.SONG_COMMENT, /* comment */
32 Gmpc.MpdData.ColumnTypes.ICON_ID, /* icon id */
33 Gmpc.MpdData.ColumnTypes.SONG_POS,
34 Gmpc.MpdData.ColumnTypes.SONG_ALBUMARTIST,
35 Gmpc.MpdData.ColumnTypes.PATH_EXTENSION, /* Extension */
36 Gmpc.MpdData.ColumnTypes.PATH_DIRECTORY, /* Directory */
37 Gmpc.MpdData.ColumnTypes.SONG_PRIORITY
39 const string[] gmpc_data_view_col_names = {
40 N_("Markup"),
41 N_("Artist"),
42 N_("Album"),
43 N_("Title"),
44 N_("File"),
45 N_("Genre"),
46 N_("Track"),
47 N_("Name"),
48 N_("Composer"),
49 N_("Performer"),
50 N_("Date"),
51 N_("Duration"),
52 N_("Disc"),
53 N_("Comment"),
54 N_("Icon Id"),
55 N_("Position"),
56 N_("AlbumArtist"),
57 N_("Extension"),
58 N_("Directory"),
59 N_("Priority")
62 const bool[] gmpc_data_view_col_enabled = {
63 false,//"Markup",
64 true, //"Artist",
65 true,//"Album",
66 true,//"Title",
67 false,//"File",
68 false,//"Genre",
69 false,//"Track",
70 false,//"Name",
71 false,//"Composer",
72 false,//"Performer",
73 false,//"Date",
74 false,//"Duration",
75 false,//"Disc",
76 false,//"Comment",
77 true,//"Icon Id"
78 false,//"Position"
79 false,//"AlbumArtist"
80 false,//Extension
81 false,//Directory
82 false//Priority
86 const int[] gmpc_data_view_col_position = {
87 14,//"Markup",
88 3, //"Artist",
89 2,//"Album",
90 1,//"Title",
91 4,//"File",
92 5,//"Genre",
93 6,//"Track",
94 7,//"Name",
95 8,//"Composer",
96 9,//"Performer",
97 10,//"Date",
98 11,//"Duration",
99 12,//"Disc",
100 13,//"Comment",
101 0,//"Icon Id"
102 15, // "Position"
103 18, // "AlbumArtist"
104 16,// Extension
105 17, // Directory
109 public class Gmpc.DataView : Gtk.TreeView
111 private Gtk.TreeViewColumn[] tree_columns = new Gtk.TreeViewColumn[NUM_COLS];
113 * If we are a play-queue we should treat the content.
114 * slightly different.
115 * e.g. add-replace will be play-crop
117 public bool is_play_queue {get; set; default=false;}
120 * The name of the treeview.
121 * This is used to store the column layout.
123 public string uid {get; set; default="default";}
127 * Construction function.
129 public DataView(string name)
131 log(log_domain, LogLevelFlags.LEVEL_INFO, "Constructing dataview: "+name);
133 this.uid = name;
134 // Connect row activated signal.
135 this.row_activated.connect(__row_activated);
136 this.key_release_event.connect(__key_release_event_callback);
137 // When it getst he destroy signal.
138 this.destroy.connect(column_store_state);
140 this.set_rules_hint(true);
142 this.get_selection().set_mode(Gtk.SelectionMode.MULTIPLE);
143 this.set_fixed_height_mode(true);
144 // Create the view.
145 column_populate();
150 * Deconstructor.
152 ~DataView()
157 * Internal functions.
161 * Store the position, visibility and width of the columns
163 private void column_store_state()
165 // Save the position of the columns
166 var columns = get_columns();
167 int index = 0;
168 foreach(var column in columns)
170 int col_index = column.get_data("index");
171 int width = column.get_width();
172 config.set_int(uid+"-colpos", gmpc_data_view_col_names[col_index], index);
173 config.set_bool(uid+"-colshow", gmpc_data_view_col_names[col_index], column.visible);
174 // Only store width if bigger then 0.
175 if(width > 0 ) {
176 config.set_int(uid+"-colsize",gmpc_data_view_col_names[col_index], width);
178 index++;
181 // Hack to make vala not destroy the menu directly.
182 private Gtk.Menu column_selection_menu = null;
183 private void column_show_selection_menu()
185 column_selection_menu = new Gtk.Menu();
186 foreach(var col in tree_columns)
188 int index = col.get_data("index");
189 // Do not show the icon id in the selection list.
190 if(gmpc_data_view_col_ids[index] == MpdData.ColumnTypes.ICON_ID) continue;
191 var item = new Gtk.CheckMenuItem.with_label(gmpc_data_view_col_names[index]);
192 if(col.visible) {
193 item.set_active(true);
195 // On activation toggle the state.
196 item.activate.connect((source) => {
197 col.visible = (source as Gtk.CheckMenuItem).get_active();
199 column_selection_menu.append(item);
201 column_selection_menu.show_all();
202 column_selection_menu.popup(null, null, null, 0, Gtk.get_current_event_time());
205 * Populate the treeview with the right columns.
206 * The treeview should have a name now.
208 private void column_populate()
210 for(int i = 0; i < NUM_COLS; i++)
212 Gtk.TreeViewColumn col = new Gtk.TreeViewColumn();
213 col.set_data("index", i);
214 if(gmpc_data_view_col_ids[i] == Gmpc.MpdData.ColumnTypes.ICON_ID)
217 * Picture.
219 var renderer = new Gtk.CellRendererPixbuf();
220 renderer.xalign = 0.0f;
221 // Set up column
222 col.pack_start(renderer, true);
223 col.set_attributes(renderer, "icon-name", Gmpc.MpdData.ColumnTypes.ICON_ID);
224 col.set_resizable(false);
225 col.set_fixed_width(20);
226 col.clickable = true;
227 // If the user clicks on the column, show dropdown allowing to enable/disable columns.
228 col.clicked.connect((source) => {
229 column_show_selection_menu();
231 } else {
233 * Text column
235 col.set_title(gmpc_data_view_col_names[i]);
236 var renderer = new Gtk.CellRendererText();
237 renderer.ellipsize = Pango.EllipsizeMode.END;
238 renderer.weight_set = true;
239 // Set up column
240 col.pack_start(renderer, true);
241 col.set_attributes(renderer, "text", gmpc_data_view_col_ids[i]);
242 col.set_resizable(true);
243 if(is_play_queue) {
244 // TODO fix this.
245 // col.set_cell_data_func(renderer, (Gtk.CellLayoutDataFunc)highlight_row_current_song_playing);
248 int width = config.get_int_with_default(uid+"-colsize",gmpc_data_view_col_names[i], default_column_width);
249 // Do not set to size 0, then revert back to 200.
250 col.set_fixed_width(width>0?width:default_column_width);
252 col.set_sizing(Gtk.TreeViewColumnSizing.FIXED);
253 col.set_reorderable(true);
255 // Fixed width.
256 int pos = config.get_int_with_default(uid+"-colpos", gmpc_data_view_col_names[i], gmpc_data_view_col_position[i]);
257 this.tree_columns[pos] = col;
259 // Add the columns (in right order)
260 for(int i = 0; i < NUM_COLS; i++) {
261 int index = this.tree_columns[i].get_data("index");
262 this.insert_column(this.tree_columns[i], i);
263 this.tree_columns[i].set_visible(config.get_bool_with_default(uid+"-colshow", gmpc_data_view_col_names[index], gmpc_data_view_col_enabled[index]));
272 * Function handles the row-activate signal.
274 private void __row_activated (Gtk.TreePath path, Gtk.TreeViewColumn col)
276 Gtk.TreeModel? model = this.get_model();
277 if(model != null)
279 Gtk.TreeIter iter;
280 if(!model.get_iter(out iter, path)) return;
281 if(is_play_queue) {
282 /* If we are play-queue, play the selected song. */
283 int song_id;
284 model.get(iter, Gmpc.MpdData.ColumnTypes.SONG_ID, out song_id);
285 MPD.Player.play_id(Gmpc.server, song_id);
286 } else {
287 /* If we are a song browser, add the path and play it. */
288 string song_path;
289 model.get(iter, Gmpc.MpdData.ColumnTypes.PATH, out song_path);
290 MpdInteraction.play_path(song_path);
295 * Check if current row is playing.
296 * TODO
298 private void highlight_row_current_song_playing(Gtk.TreeViewColumn col, Gtk.CellRenderer renderer, Gtk.TreeModel model, Gtk.TreeIter iter)
300 (renderer as Gtk.CellRendererText).weight = Pango.Weight.BOLD;
305 * Handle keyboard input.
307 private bool __key_release_event_callback(Gdk.EventKey event)
309 if(event.keyval == Gdk.Key_y)
311 // Copy data to clipboard
314 else if (event.keyval == Gdk.Key_c)
316 // Cut (if available) into clipboard
318 else if (event.keyval == Gdk.Key_P)
320 // Paste before
322 else if (event.keyval == Gdk.Key_p)
324 // Paste after
326 else if (event.keyval == Gdk.Key_Escape)
331 // Commands specific to play_queue
332 if(is_play_queue)
334 if (event.keyval == Gdk.Key_Q)
336 // remove priority.
337 return selected_songs_remove_priority();
339 else if (event.keyval == Gdk.Key_q)
341 // Raise priority.
342 return selected_songs_raise_priority();
344 else if (event.keyval == Gdk.Key_d)
346 if(!selected_songs_remove())
348 // Detach model (for some reason keeping it attached
349 // Makes thing break, work-around for now)
350 // TODO: fixme
351 var model = get_model();
352 this.model = null;
353 // Clear
354 MPD.PlayQueue.clear(server);
355 // Re-add model
356 this.model = model;
357 return true;
361 else
363 if(event.keyval == Gdk.Key_i)
365 // Insert
368 return false;
372 * Interaction on selected songs.
374 // Set priority on the selected songs.
375 private bool selected_songs_raise_priority()
377 if(server.check_command_allowed("prioid") != MPD.Server.Command.ALLOWED) return false;
378 var selection = this.get_selection();
380 if(selection.count_selected_rows() > 254) {
381 Gmpc.Messages.show(_("You can only queue 254 songs at the time."), Gmpc.Messages.Level.WARNING);
382 return false;
385 int priority = 255;
386 Gtk.TreeModel model;
387 foreach(var path in selection.get_selected_rows(out model))
389 Gtk.TreeIter iter;
390 if(model.get_iter(out iter, path))
392 int song_id;
393 model.get(iter,Gmpc.MpdData.ColumnTypes.SONG_ID, out song_id);
394 MPD.PlayQueue.set_priority(server, song_id, priority--);
397 return true;
399 // Remove the set priority from the selected songs.
400 private bool selected_songs_remove_priority()
402 if(server.check_command_allowed("prioid") != MPD.Server.Command.ALLOWED) return false;
403 var selection = this.get_selection();
405 Gtk.TreeModel model;
406 foreach(var path in selection.get_selected_rows(out model))
408 Gtk.TreeIter iter;
409 if(model.get_iter(out iter, path))
411 int song_id;
412 model.get(iter,Gmpc.MpdData.ColumnTypes.SONG_ID, out song_id);
413 MPD.PlayQueue.set_priority(server, song_id, 0);
416 return true;
418 // Remove the selected songs from the play queue.
419 private bool selected_songs_remove()
421 int deleted_rows = 0;
422 var selection = this.get_selection();
424 Gtk.TreeModel model;
425 foreach(var path in selection.get_selected_rows(out model))
427 Gtk.TreeIter iter;
428 if(model.get_iter(out iter, path))
430 int song_id;
431 model.get(iter,Gmpc.MpdData.ColumnTypes.SONG_ID, out song_id);
432 MPD.PlayQueue.queue_delete_id(server, song_id);
433 deleted_rows++;
436 MPD.PlayQueue.queue_commit(server);
437 return (deleted_rows > 0);