Improve rating widget.
[gmpc.git] / src / Widgets / gmpc-rating.vala
blobe1b13abd6220b2e70995cc96bc6a736c8ef72ce4
1 /* Gnome Music Player Client (GMPC)
2 * Copyright (C) 2004-2014 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpc.wikia.com/
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 using GLib;
21 using Gtk;
22 using Gdk;
23 using Cairo;
24 using MPD;
25 using Gmpc;
29 public class Gmpc.Rating : Gtk.Frame
31 private const bool use_transition = Gmpc.use_transition;
32 private unowned MPD.Server server = null;
33 private MPD.Song song = null;
34 private Gtk.Image[] rat;
35 private Gtk.HBox box;
36 public Gtk.EventBox event_box;
37 private int rating = -1;
38 static int id = id+1;
40 private ulong status_changed_id = 0;
42 ~Rating() {
43 if (this.status_changed_id > 0 && GLib.SignalHandler.is_connected(gmpcconn,
44 this.status_changed_id)) {
45 GLib.SignalHandler.disconnect(gmpcconn, this.status_changed_id);
46 this.status_changed_id = 0;
50 public bool button_press_event_callback(Gtk.Widget wid, Gdk.EventButton event)
52 if(event.type == Gdk.EventType.BUTTON_RELEASE)
54 if(event.button == 1)
56 Gtk.Allocation ns;
57 this.event_box.get_allocation(out ns);
58 int width = ns.width;
59 int button = (int)((((event.x)/(double)width)+0.15)*5);
60 if(button == rating) { button = 0; }
61 MPD.Sticker.Song.set(this.server, this.song.file, "rating", (button*2).to_string());
62 this.set_rating(button);
66 return false;
69 private void status_changed ( MPD.Server server, MPD.Status.Changed what,Gmpc.Connection conn)
71 if(((what&MPD.Status.Changed.STICKER) != 0))
73 this.update();
77 public Rating (MPD.Server server, MPD.Song song) {
78 this.server = server;
79 this.song = song.copy();
80 this.update();
81 this.status_changed_id = GLib.Signal.connect_swapped(gmpcconn, "status_changed",
82 (GLib.Callback)status_changed, this);
85 construct {
86 int i;
87 this.shadow_type = Gtk.ShadowType.NONE;
89 this.box = new Gtk.HBox(true,6);
90 this.rat = new Gtk.Image[5];
91 this.event_box = new Gtk.EventBox();
92 this.event_box.visible_window = false;
93 this.event_box.set_above_child(true);
95 for(i=0;i<5;i++) {
96 // Copy value.
97 var curr_entry = i+1;
98 this.rat[i] = new Gtk.Image.from_icon_name("rating", Gtk.IconSize.MENU);
99 this.box.pack_start(this.rat[i], false, false, 0);
102 this.event_box.add(this.box);
103 this.event_box.button_release_event.connect(button_press_event_callback);
104 this.add(this.event_box);
105 this.show_all();
109 public void set_rating(int rating)
111 int i=0;
112 if(rating != this.rating)
114 for(i=0;i<5;i++)
116 this.rat[i].sensitive = i<rating;
118 this.rating = rating;
121 public void update()
123 var value = MPD.Sticker.Song.get(this.server,this.song.file, "rating");
124 if(value == null) {
125 this.set_rating(0);
126 } else {
127 this.set_rating(value.to_int()/2);