1 /* This file is part of LED Pattern Editor.
3 * Copyright (C) 2010 Philipp Zabel
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.
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.
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/>.
19 class LedProgramDialog : Gtk.Dialog {
21 LedPatternRX51 pattern;
23 LedColorButton led_color1;
24 LedColorButton led_color2;
28 public LedProgramDialog (LedPatternRX51 _pattern) {
30 set_title ("LED pattern editor - " +
31 (pattern.name.has_prefix ("Pattern") ?
32 pattern.name.offset (7) : pattern.name) + " pattern");
34 var content = (Gtk.VBox) get_content_area ();
35 content.set_size_request (-1, 5*70);
37 lpv = new LedPatternView (pattern.copy ());
38 lpv.set_size_request (-1, 70);
39 content.pack_start (lpv, false, false, 0);
41 var hbox = new Gtk.HBox (true, 0);
42 list1 = led_command_list (1);
43 hbox.pack_start (list1, true, true, 0);
44 list2 = led_command_list (2);
45 hbox.pack_start (list2, true, true, 0);
46 content.pack_start (hbox, true, true, 0);
51 hbox = new Gtk.HBox (true, 0);
52 led_color1 = new LedColorButton.with_color (lpv.pattern.color1);
53 led_color1.clicked.connect (on_color1_clicked);
54 hbox.pack_start (led_color1, true, true, 0);
55 led_color2 = new LedColorButton.with_color (lpv.pattern.color2);
56 led_color2.clicked.connect (on_color2_clicked);
57 hbox.pack_start (led_color2, true, true, 0);
58 add_action_widget (hbox, 2);
59 action_area.set_child_secondary (hbox, true);
61 hbox = new Gtk.HBox (true, 0);
62 var radio = (Gtk.RadioButton) Hildon.gtk_radio_button_new (Hildon.SizeType.FINGER_HEIGHT, null);
63 radio.set_mode (false);
64 radio.set_label ("1");
65 radio.set_active (true);
66 radio.toggled.connect (on_engine1_toggled);
67 hbox.pack_start (radio, true, true, 0);
68 radio = (Gtk.RadioButton) Hildon.gtk_radio_button_new_from_widget (Hildon.SizeType.FINGER_HEIGHT, radio);
69 radio.set_mode (false);
70 radio.set_label ("2");
71 radio.toggled.connect (on_engine2_toggled);
72 hbox.pack_start (radio, true, true, 0);
73 add_action_widget (hbox, 2);
74 action_area.set_child_secondary (hbox, true);
76 action_area.show_all ();
78 add_button ("Copy", 2);
79 add_button ("Test", 1);
80 add_button ("Done", Gtk.ResponseType.ACCEPT);
82 response.connect (on_response);
85 private Gtk.Widget led_command_list (int engine) {
86 var pannable = new Hildon.PannableArea ();
87 var vbox = new Gtk.VBox (false, 0);
89 var label = new Gtk.Label ("Engine %d:".printf (engine));
90 label.set_alignment (0, 0.5f);
91 vbox.pack_start (label, false, false, 0);
93 unowned List<LedCommandRX51> list = (engine == 1) ?
94 lpv.pattern.engine1 : lpv.pattern.engine2;
95 foreach (LedCommandRX51 command in list) {
96 if (command.type == CommandType.RESET_MUX)
98 var command_widget = new LedCommandWidget (lpv.pattern, list,
101 vbox.pack_start (command_widget, false, false, 0);
104 var button = new Gtk.Button.with_label ("New command");
105 Hildon.gtk_widget_set_theme_size (button, Hildon.SizeType.FINGER_HEIGHT);
106 button.clicked.connect (on_new_command_clicked);
107 vbox.pack_end (button, false, false, 0);
109 pannable.add_with_viewport (vbox);
113 void on_response (int response) {
115 Timeout.add (200, delayed_spawn);
116 } else if (response == 2) {
117 var clipboard = Gtk.Clipboard.@get (Gdk.SELECTION_CLIPBOARD);
119 clipboard.set_text (lpv.pattern.dump (), -1);
121 Hildon.Banner.show_information (null, null, "Copied");
122 } else if (response == Gtk.ResponseType.ACCEPT) {
123 if (pattern.dump () != lpv.pattern.dump ()) {
124 pattern.replace_with (lpv.pattern);
129 bool delayed_spawn () {
133 var command = "sudo /usr/bin/led-pattern-helper test \"" +
134 lpv.pattern.dump () + "\"";
135 Process.spawn_command_line_sync (command, null, out error, out exit_status);
136 if (exit_status != 0) {
137 var information = "Exit status: %d\n%s".printf (exit_status, error);
138 Hildon.Banner.show_information (null, null, information);
140 } catch (SpawnError e) {
141 Hildon.Banner.show_information (null, null, e.message);
147 void on_color1_clicked (Gtk.Button button) {
148 var dialog = new LedColorDialog ();
149 int response = dialog.run ();
151 LedColor color = (LedColor) response;
152 led_color1.set_color (color);
153 lpv.pattern.color1 = color;
154 color = led_color2.get_color () & ~response;
155 led_color2.set_color (color);
156 lpv.pattern.color2 = color;
157 lpv.pattern.changed ();
162 void on_color2_clicked (Gtk.Button button) {
163 var dialog = new LedColorDialog ();
164 int response = dialog.run ();
166 LedColor color = (LedColor) response;
167 led_color2.set_color (color);
168 lpv.pattern.color2 = color;
169 color = led_color1.get_color () & ~response;
170 led_color1.set_color (color);
171 lpv.pattern.color1 = color;
172 lpv.pattern.changed ();
177 void on_engine1_toggled (Gtk.ToggleButton source) {
178 if (source.get_active ())
184 void on_engine2_toggled (Gtk.ToggleButton source) {
185 if (source.get_active ())
191 void on_new_command_clicked (Gtk.Button button) {
192 var widget = button.parent.parent.parent;
193 unowned List<LedCommandRX51> engine;
194 if (widget == list1) {
195 engine = lpv.pattern.engine1;
196 } else if (widget == list2) {
197 engine = lpv.pattern.engine2;
202 var dialog = new Hildon.PickerDialog (this);
203 dialog.set_title ("New command");
205 var touch_selector = new Hildon.TouchSelector.text ();
206 touch_selector.append_text ("Set PWM");
207 touch_selector.append_text ("Ramp");
208 touch_selector.append_text ("Wait");
209 touch_selector.append_text ("Trigger");
210 touch_selector.append_text ("Go To Start");
211 touch_selector.append_text ("End");
212 dialog.set_selector (touch_selector);
214 int response = dialog.run ();
215 if (response == Gtk.ResponseType.OK) {
216 var command = new LedCommandRX51 ();
217 LedCommandRX51 last_command = engine.last ().data;
218 engine.append (command);
219 command.changed.connect (lpv.pattern.on_changed);
221 switch (touch_selector.get_active (0)) {
223 command.set_pwm (127);
226 if (last_command.type == CommandType.RAMP_WAIT) {
227 command.ramp_wait (last_command.step_time,
228 -last_command.steps);
229 } else if (last_command.level > 0) {
230 command.ramp_wait (125.0 / last_command.level,
231 -last_command.level);
233 command.ramp_wait (0.49, 255);
237 command.ramp_wait (100.0, 0);
240 command.type = CommandType.TRIGGER;
241 command.code = 0xe000;
245 command.go_to_start ();
251 var vbox = (Gtk.VBox) button.parent;
252 var command_widget = new LedCommandWidget (lpv.pattern, engine,
254 vbox.pack_start (command_widget, false, false, 0);
255 command_widget.show_all ();