LED color widget: fix the clipping circle
[led-pattern-ed] / src / led-program-dialog.vala
1 /* This file is part of LED Pattern Editor.
2  *
3  * Copyright (C) 2010 Philipp Zabel
4  *
5  * LED Pattern Editor 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 3 of the License, or
8  * (at your option) any later version.
9  *
10  * LED Pattern Editor 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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with LED Pattern Editor. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 class LedProgramDialog : Gtk.Dialog {
20         LedPatternView lpv;
21         LedPatternRX51 pattern;
22
23         public LedProgramDialog (LedPatternRX51 _pattern) {
24                 pattern = _pattern;
25                 set_title ("LED pattern editor - " +
26                            (pattern.name.has_prefix ("Pattern") ?
27                             pattern.name.offset (7) : pattern.name) + " pattern");
28
29                 var content = (Gtk.VBox) get_content_area ();
30                 content.set_size_request (-1, 5*70);
31
32                 lpv = new LedPatternView (pattern.copy ());
33                 lpv.set_size_request (-1, 70);
34                 content.pack_start (lpv, false, false, 0);
35
36                 var pannable = new Hildon.PannableArea ();
37                 var vbox = new Gtk.VBox (false, 0);
38
39                 foreach (LedCommand command in lpv.pattern.engine1) {
40                         var command_widget = new LedCommandWidget (command);
41
42                         vbox.pack_start (command_widget, false, false, 0);
43                 }
44
45                 pannable.add_with_viewport (vbox);
46                 content.pack_start (pannable, true, true, 0);
47
48                 content.show_all ();
49
50                 var led_color = new LedColorButton.with_map (lpv.pattern.led_map);
51                 led_color.clicked.connect (on_color_clicked);
52                 add_action_widget (led_color, 2);
53                 action_area.set_child_secondary (led_color, true);
54
55                 action_area.show_all ();
56
57                 add_button ("Test", 1);
58                 add_button ("Done", Gtk.ResponseType.ACCEPT);
59
60                 response.connect (on_response);
61         }
62
63         void on_response (int response) {
64                 if (response == 1) {
65                         Timeout.add (200, delayed_spawn);
66                         return;
67                 }
68                 if (response == Gtk.ResponseType.ACCEPT) {
69                         if (pattern.dump () != lpv.pattern.dump ()) {
70                                 pattern.replace_with (lpv.pattern);
71                         }
72                 }
73         }
74
75         bool delayed_spawn () {
76                 try {
77                         int exit_status;
78                         string error;
79                         var command = "sudo /usr/bin/led-pattern-helper test \"" +
80                                       lpv.pattern.dump () + "\"";
81                         Process.spawn_command_line_sync (command, null, out error, out exit_status);
82                         if (exit_status != 0) {
83                                 var information = "Exit status: %d\n%s".printf (exit_status, error);
84                                 Hildon.Banner.show_information (null, null, information);
85                         }
86                 } catch (SpawnError e) {
87                         Hildon.Banner.show_information (null, null, e.message);
88                 }
89
90                 return false;
91         }
92
93         void on_color_clicked (Gtk.Button button) {
94                 var dialog = new LedColorDialog ();
95                 int response = dialog.run ();
96                 if (response > 0) {
97                         ((LedColorButton) button).set_color ((LedColor) response);
98                         switch ((LedColor) response) {
99                         case LedColor.R:
100                                 lpv.pattern.led_map = "r";
101                                 break;
102                         case LedColor.G:
103                                 lpv.pattern.led_map = "g";
104                                 break;
105                         case LedColor.B:
106                                 lpv.pattern.led_map = "b";
107                                 break;
108                         case LedColor.RG:
109                                 lpv.pattern.led_map = "rg";
110                                 break;
111                         case LedColor.RB:
112                                 lpv.pattern.led_map = "rb";
113                                 break;
114                         case LedColor.GB:
115                                 lpv.pattern.led_map = "gb";
116                                 break;
117                         case LedColor.RGB:
118                                 lpv.pattern.led_map = "rgb";
119                                 break;
120                         }
121                         lpv.pattern.changed ();
122                 }
123                 dialog.destroy ();
124         }
125 }