Use autotools and intltools, translate user interface, add German translation
[led-pattern-ed] / src / led-command-widget.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 LedCommandWidget : Gtk.HBox {
20         private const double CYCLE_TIME_MS = 1000.0 / 32768.0;
21
22         LedPatternRX51 pattern;
23         unowned List<LedCommandRX51> engine;
24         LedCommandRX51 command;
25
26         public LedCommandWidget (LedPatternRX51 _pattern, List<LedCommandRX51> _engine,
27                                  LedCommandRX51 _command) {
28                 homogeneous = true;
29                 pattern = _pattern;
30                 engine = _engine;
31                 command = _command;
32
33                 var button = new Hildon.Button (Hildon.SizeType.FINGER_HEIGHT,
34                                                 Hildon.ButtonArrangement.VERTICAL);
35                 button.set_style (Hildon.ButtonStyle.PICKER);
36                 button.set_alignment (0, 0.5f, 0, 0.5f);
37
38                 switch (command.type) {
39                 case CommandType.UNKNOWN:
40                         button.set_title ("???");
41                         button.set_value ("0x%04x".printf (command.code));
42                         button.set_sensitive (false);
43                         break;
44                 case CommandType.RAMP_WAIT:
45                         button.set_title ((command.steps != 0) ? _("Ramp") : _("Wait"));
46                         button.set_value ((command.steps != 0) ?
47                                           _("%+d steps, %.2f ms each").printf (command.steps,
48                                                                                command.step_time) :
49                                           "%.2f ms".printf (command.duration));
50                         button.clicked.connect (on_ramp_wait_clicked);
51                         break;
52                 case CommandType.SET_PWM:
53                         button.set_title (_("Set PWM"));
54                         button.set_value (_("Level = %d").printf (command.level));
55                         button.clicked.connect (on_set_pwm_clicked);
56                         break;
57                 case CommandType.RESET_MUX:
58                         button.set_title (_("Reset Mux"));
59                         break;
60                 case CommandType.GO_TO_START:
61                         button.set_title (_("Go To Start"));
62                         button.set_value ("");
63                         button.clicked.connect (on_end_or_go_to_start_clicked);
64                         break;
65                 case CommandType.BRANCH:
66                         button.set_title (_("Branch"));
67                         button.set_value ("0x%04x".printf (command.code));
68                         button.set_sensitive (false);
69                         break;
70                 case CommandType.END:
71                         button.set_title (_("End"));
72                         button.set_value ((command.steps == -255) ? _("Reset") : _("Hold"));
73                         button.clicked.connect (on_end_or_go_to_start_clicked);
74                         break;
75                 case CommandType.TRIGGER:
76                         button.set_title (_("Trigger"));
77                         string text = "";
78                         if (0x0100 in command.code)
79                                 text += _("wait 2 ");
80                         if (0x0004 in command.code)
81                                 text += _("set 2 ");
82                         if (0x0080 in command.code)
83                                 text += _("wait 1 ");
84                         if (0x0002 in command.code)
85                                 text += _("set 1 ");
86                         if ((command.code & ~0xe186) != 0) {
87                                 text = _("Unsupported: 0x%04x").printf (command.code);
88                                 button.set_sensitive (false);
89                         }
90                         button.set_value (text);
91                         button.clicked.connect (on_trigger_clicked);
92                         break;
93                 }
94                 pack_start (button, true, true, 0);
95         }
96
97         private void on_ramp_wait_clicked (Gtk.Button source) {
98                 double old_step_time = command.step_time;
99                 int old_steps = command.steps;
100                 var dialog = new Gtk.Dialog ();
101                 dialog.set_title (_("Ramp / Wait"));
102
103                 var content = (Gtk.VBox) dialog.get_content_area ();
104
105                 var lpv = new LedPatternView (pattern);
106                 lpv.set_size_request (-1, 70);
107                 content.pack_start (lpv, true, true, 0);
108
109                 var scale1 = new Gtk.HScale.with_range (1, 62, 1);
110                 scale1.set_increments (1, 1);
111                 var v = (command.step_time / 16 / CYCLE_TIME_MS);
112                 if (v > 62)
113                         v = v / 32 + 31;
114                 scale1.set_value (v);
115                 scale1.format_value.connect ((v) => {
116                         if (v < 32)
117                                 return "%.2f ms".printf (v * 16 * CYCLE_TIME_MS);
118                         else
119                                 return "%.1f ms".printf ((v - 31) * 512 * CYCLE_TIME_MS);
120                 });
121                 scale1.value_changed.connect ((s) => {
122                         int val = (int) s.get_value ();
123                         if (val < 32)
124                                 command.ramp_wait (val * 16 * CYCLE_TIME_MS, command.steps);
125                         else
126                                 command.ramp_wait ((val - 31) * 512 * CYCLE_TIME_MS, command.steps);
127                 });
128                 content.pack_start (scale1, true, true, 0);
129
130                 var hbox = new Gtk.HBox (false, 0);
131                 var hbox2 = new Gtk.HBox (true, 0);
132                 var radio_inc = (Gtk.RadioButton) Hildon.gtk_radio_button_new (Hildon.SizeType.FINGER_HEIGHT, null);
133                 radio_inc.set_mode (false);
134                 radio_inc.set_label ("+");
135                 hbox2.pack_start (radio_inc, false, false, 0);
136                 var radio_dec = (Gtk.RadioButton) Hildon.gtk_radio_button_new_from_widget (Hildon.SizeType.FINGER_HEIGHT, radio_inc);
137                 radio_dec.set_mode (false);
138                 radio_dec.set_label ("-");
139                 bool sign = command.steps < 0;
140                 radio_dec.set_active (sign);
141                 radio_dec.toggled.connect ((s) => {
142                         sign = s.get_active ();
143                         if (sign != (command.steps < 0))
144                                 command.ramp_wait (command.step_time, -command.steps);
145                 });
146                 hbox2.pack_start (radio_dec, false, false, 0);
147                 hbox.pack_start (hbox2, false, false, 0);
148                 var scale2 = new Gtk.HScale.with_range (0, 255, 1);
149                 scale2.set_increments (1, 1);
150                 scale2.set_value ((command.steps < 0) ? -command.steps : command.steps);
151                 scale2.set_value_pos (Gtk.PositionType.RIGHT);
152                 scale2.format_value.connect ((v) => {
153                         return "%+d".printf (sign ? -(int) v : (int) v);
154                 });
155                 scale2.value_changed.connect ((s) => {
156                         if (sign)
157                                 command.ramp_wait (command.step_time, -(int) scale2.get_value ());
158                         else
159                                 command.ramp_wait (command.step_time, (int) scale2.get_value ());
160                 });
161                 hbox.pack_start (scale2, true, true, 0);
162                 content.pack_start (hbox, false, false, 0);
163
164                 content.show_all ();
165                 dialog.add_button (_("Delete"), 1);
166                 dialog.add_button (_("Done"), Gtk.ResponseType.OK);
167
168                 int response = dialog.run ();
169                 if (response == Gtk.ResponseType.OK) {
170                         var button = source as Hildon.Button;
171                         button.set_title ((command.steps != 0) ? _("Ramp") : _("Wait"));
172                         button.set_value ((command.steps != 0) ?
173                                           _("%+d steps, %.2f ms each").printf (command.steps,
174                                                                                command.step_time) :
175                                           "%.2f ms".printf (command.duration));
176                 } else if (response == 1) {
177                         engine.remove (command);
178                         engine.first ().data.changed ();
179                         this.destroy ();
180                 } else {
181                         command.ramp_wait (old_step_time, old_steps);
182                 }
183                 dialog.destroy ();
184         }
185
186         private void on_set_pwm_clicked (Gtk.Button source) {
187                 int old_level = command.level;
188                 var dialog = new Gtk.Dialog ();
189                 dialog.set_title (_("Set PWM"));
190
191                 var content = (Gtk.VBox) dialog.get_content_area ();
192
193                 var lpv = new LedPatternView (pattern);
194                 lpv.set_size_request (-1, 70);
195                 content.pack_start (lpv, true, true, 0);
196
197                 var scale = new Gtk.HScale.with_range (0, 255, 1);
198                 scale.set_increments (1, 1);
199                 scale.set_value (command.level);
200                 scale.value_changed.connect ((s) => {
201                         command.set_pwm ((int) s.get_value ());
202                 });
203                 content.pack_start (scale, true, true, 0);
204
205                 content.show_all ();
206                 dialog.add_button (_("Delete"), 1);
207                 dialog.add_button (_("Done"), Gtk.ResponseType.OK);
208
209                 int response = dialog.run ();
210                 if (response == Gtk.ResponseType.OK) {
211                         command.set_pwm ((int) scale.get_value ());
212
213                         var button = source as Hildon.Button;
214                         button.set_value (_("Level = %d").printf (command.level));
215                 } else if (response == 1) {
216                         engine.remove (command);
217                         engine.first ().data.changed ();
218                         this.destroy ();
219                 } else {
220                         command.set_pwm (old_level);
221                 }
222                 dialog.destroy ();
223         }
224
225         private void on_end_or_go_to_start_clicked (Gtk.Button source) {
226                 CommandType old_type = command.type;
227                 int old_steps = command.steps;
228                 uint16 old_code = command.code;
229                 var dialog = new Gtk.Dialog ();
230                 if (command.type == CommandType.END)
231                         dialog.set_title (_("End"));
232                 else
233                         dialog.set_title (_("Go To Start"));
234
235                 var content = (Gtk.VBox) dialog.get_content_area ();
236
237                 var lpv = new LedPatternView (pattern);
238                 lpv.set_size_request (-1, 70);
239                 content.pack_start (lpv, true, true, 0);
240
241                 var hbox = new Gtk.HBox (true, 0);
242                 var radio_end = (Gtk.RadioButton) Hildon.gtk_radio_button_new (Hildon.SizeType.FINGER_HEIGHT, null);
243                 radio_end.set_mode (false);
244                 radio_end.set_label (_("End"));
245                 hbox.pack_start (radio_end, true, true, 0);
246                 var radio_go_to_start = (Gtk.RadioButton) Hildon.gtk_radio_button_new_from_widget (Hildon.SizeType.FINGER_HEIGHT, radio_end);
247                 radio_go_to_start.set_mode (false);
248                 radio_go_to_start.set_label (_("Go To Start"));
249                 hbox.pack_start (radio_go_to_start, true, true, 0);
250                 content.pack_start (hbox, true, true, 0);
251
252                 hbox = new Gtk.HBox (true, 0);
253                 var radio_hold = (Gtk.RadioButton) Hildon.gtk_radio_button_new (Hildon.SizeType.FINGER_HEIGHT, null);
254                 radio_hold.set_mode (false);
255                 radio_hold.set_label (_("Hold"));
256                 hbox.pack_start (radio_hold, true, true, 0);
257                 var radio_reset = (Gtk.RadioButton) Hildon.gtk_radio_button_new_from_widget (Hildon.SizeType.FINGER_HEIGHT, radio_hold);
258                 radio_reset.set_mode (false);
259                 radio_reset.set_label (_("Reset"));
260                 hbox.pack_start (radio_reset, true, true, 0);
261                 content.pack_start (hbox, true, true, 0);
262
263                 if (command.type == CommandType.END)
264                         radio_end.set_active (true);
265                 else
266                         radio_go_to_start.set_active (true);
267                 radio_hold.set_sensitive (command.type == CommandType.END);
268                 radio_reset.set_sensitive (command.type == CommandType.END);
269                 radio_reset.set_active (command.steps == -255);
270
271                 radio_end.toggled.connect ((s) => {
272                         if (s.get_active ()) {
273                                 command.end (radio_reset.get_active ());
274                                 radio_hold.set_sensitive (true);
275                                 radio_reset.set_sensitive (true);
276                                 dialog.set_title (_("End"));
277                         } else {
278                                 command.go_to_start ();
279                                 radio_hold.set_sensitive (false);
280                                 radio_reset.set_sensitive (false);
281                                 dialog.set_title (_("Go To Start"));
282                         }
283                         command.changed ();
284                 });
285                 radio_reset.toggled.connect ((s) => {
286                         if (command.type == CommandType.END) {
287                                 command.end (s.get_active ());
288                         }
289                 });
290
291                 content.show_all ();
292                 dialog.add_button (_("Delete"), 1);
293                 dialog.add_button (_("Done"), Gtk.ResponseType.OK);
294
295                 int response = dialog.run ();
296                 if (response == Gtk.ResponseType.OK) {
297                         var button = source as Hildon.Button;
298                         button.set_value ((command.type == CommandType.END) ?
299                                           _("End") : _("Go To Start"));
300                         button.set_value ((command.type == CommandType.END) ?
301                                           ((command.steps == -255) ? _("Reset") : _("Hold")) : "");
302                 } else if (response == 1) {
303                         engine.remove (command);
304                         engine.first ().data.changed ();
305                         this.destroy ();
306                 } else {
307                         command.type = old_type;
308                         command.steps = old_steps;
309                         command.code = old_code;
310                         command.changed ();
311                 }
312                 dialog.destroy ();
313         }
314
315         private void on_trigger_clicked (Gtk.Button source) {
316                 uint16 old_code = command.code;
317                 var dialog = new Gtk.Dialog ();
318                 dialog.set_title (_("Trigger"));
319
320                 var content = (Gtk.VBox) dialog.get_content_area ();
321
322                 var lpv = new LedPatternView (pattern);
323                 lpv.set_size_request (-1, 70);
324                 content.pack_start (lpv, true, true, 0);
325
326                 var check = new Hildon.CheckButton (Hildon.SizeType.FINGER_HEIGHT);
327                 check.set_label (_("Set trigger"));
328                 if ((command.code & 0x0006) != 0)
329                         check.set_active (true);
330                 check.toggled.connect ((s) => {
331                         int bit = (engine == pattern.engine1) ? 0x0004 : 0x0002;
332                         if (s.get_active ())
333                                 command.code |= bit;
334                         else
335                                 command.code &= ~bit;
336                         command.changed ();
337                 });
338                 content.pack_start (check, true, true, 0);
339
340                 check = new Hildon.CheckButton (Hildon.SizeType.FINGER_HEIGHT);
341                 check.set_label (_("Wait for trigger"));
342                 if ((command.code & 0x0180) != 0)
343                         check.set_active (true);
344                 check.toggled.connect ((s) => {
345                         int bit = (engine == pattern.engine1) ? 0x0100 : 0x0080;
346                         if (s.get_active ())
347                                 command.code |= bit;
348                         else
349                                 command.code &= ~bit;
350                         command.changed ();
351                 });
352                 content.pack_start (check, true, true, 0);
353
354                 content.show_all ();
355                 dialog.add_button (_("Delete"), 1);
356                 dialog.add_button (_("Done"), Gtk.ResponseType.OK);
357
358                 int response = dialog.run ();
359                 if (response == Gtk.ResponseType.OK) {
360                         var button = source as Hildon.Button;
361                         string text = "";
362                         if (0x0100 in command.code)
363                                 text += _("wait 2 ");
364                         if (0x0004 in command.code)
365                                 text += _("set 2 ");
366                         if (0x0080 in command.code)
367                                 text += _("wait 1 ");
368                         if (0x0002 in command.code)
369                                 text += _("set 1 ");
370                         if ((command.code & ~0xe186) != 0) {
371                                 text = _("Unsupported: 0x%04x").printf (command.code);
372                                 button.set_sensitive (false);
373                         }
374                         button.set_value (text);
375                 } else if (response == 1) {
376                         engine.remove (command);
377                         engine.first ().data.changed ();
378                         this.destroy ();
379                 } else {
380                         command.code = old_code;
381                         command.changed ();
382                 }
383                 dialog.destroy ();
384         }
385 }