BioCore  5.0.0
A Windows .NET microscopy library and application for viewing, editing, and annotating images.
Loading...
Searching...
No Matches
Recordings.cs
Go to the documentation of this file.
1using Newtonsoft.Json;
2using System;
3using System.Drawing;
4using System.IO;
5using System.Linq;
6using System.Windows.Forms;
7
8namespace BioCore
9{
10 public partial class Recordings : Form
11 {
12 /* Creating a new directory called Recordings. */
13 public Recordings()
14 {
15 InitializeComponent();
16 if (!Directory.Exists("Recordings"))
17 Directory.CreateDirectory("Recordings");
18 foreach (string file in Directory.GetFiles("Recordings"))
19 {
20 if (file.EndsWith("reco"))
21 OpenRecording(file);
22 else if (file.EndsWith("pro"))
23 OpenProperty(file);
24 }
25 foreach (Automation.Action.ValueType val in (Automation.Action.ValueType[])Enum.GetValues(typeof(Automation.Action.ValueType)))
26 {
27 propBox.Items.Add(val);
28 }
29
30 }
32 public void InitElements()
33 {
34 view.Nodes.Clear();
35 for (int i = 0; i < Automation.Recordings.Count; i++)
36 {
37
38 }
39 foreach (Automation.Recording rec in Automation.Recordings.Values)
40 {
41 TreeNode tr = new TreeNode();
42 Node n = new Node(rec, tr, Node.Type.recording);
43 tr.Tag = n;
44 tr.Text = rec.Name;
45 foreach (Automation.Action item in rec.List)
46 {
47 try
48 {
49 TreeNode tn = new TreeNode();
50 tn.Text = item.ToString();
51
52 Node no = new Node(item, tn,Node.Type.action);
53 no.recording = rec;
54 tn.Tag = no;
55 tr.Nodes.Add(tn);
56 }
57 catch (Exception)
58 {
59
60 }
61 }
62 view.Nodes.Add(tr);
63 }
64
65 propView.Nodes.Clear();
66 foreach (Automation.Recording rec in Automation.Properties.Values)
67 {
68 TreeNode tr = new TreeNode();
69 Node n = new Node(rec, tr, Node.Type.recording);
70 tr.Tag = n;
71 tr.Text = rec.Name;
72 foreach (Automation.Action item in rec.List)
73 {
74 try
75 {
76 TreeNode tn = new TreeNode();
77 tn.Text = item.ToString();
78
79 Node no = new Node(item, tn, Node.Type.action);
80 no.recording = rec;
81 tn.Tag = no;
82 tr.Nodes.Add(tn);
83 }
84 catch (Exception)
85 {
86
87 }
88 }
89 propView.Nodes.Add(tr);
90 }
91 }
93 public void UpdateElements()
94 {
95 foreach (TreeNode rec in view.Nodes)
96 {
97 rec.Nodes.Clear();
98 Node n = (Node)rec.Tag;
99 foreach (Automation.Action item in n.recording.List)
100 {
101 try
102 {
103 TreeNode tr = new TreeNode();
104 tr.Text = item.ToString();
105 Node no = new Node(item, rec, Node.Type.action);
106 no.recording = n.recording;
107 tr.Tag = no;
108 rec.Nodes.Add(tr);
109 }
110 catch (Exception)
111 {
112
113 }
114 }
115 }
116 foreach (TreeNode rec in propView.Nodes)
117 {
118 rec.Nodes.Clear();
119 Node n = (Node)rec.Tag;
120 foreach (Automation.Action item in n.recording.List)
121 {
122 try
123 {
124 TreeNode tr = new TreeNode();
125 tr.Text = item.ToString();
126 Node no = new Node(item, rec, Node.Type.action);
127 no.recording = n.recording;
128 tr.Tag = no;
129 rec.Nodes.Add(tr);
130 }
131 catch (Exception)
132 {
133
134 }
135 }
136 }
137 }
138 /* It's a class that contains a recording and an action, and it's used to store the data in the
139 treeview */
140 public class Node
141 {
144 public Type type;
145 public enum Type
146 {
147 recording,
148 action
149 }
150
151 public TreeNode node;
152 /* Creating a new node in the tree. */
153 public Node(Automation.Action el,TreeNode n,Type t)
154 {
155 action = el;
156 node = n;
157 type = t;
159 recording.List.Add(el);
160 //items = Automation.AutomationHelpers.GetAllChildren(el.element);
161 }
162 /* Creating a new node in the tree. */
163 public Node(Automation.Recording rec, TreeNode n, Type t)
164 {
165 recording = rec;
166 node = n;
167 type = t;
168 //items = Automation.AutomationHelpers.GetAllChildren(el.element);
169 }
170 public override string ToString()
171 {
172 return action.Name + ", " + action.AutomationID + ", " + action.ClassName.ToString() + ", " + action.ActionType;
173 }
174 }
175
179 public static void Perform(string act)
180 {
182 rec.Run();
183 Recorder.AddLine("Recordings.Perform(" + rec.Name + ");");
184 }
191 public static object GetProperty(Automation.Action.ValueType automation,string pro)
192 {
193 Automation.Recording rec = null;
194 if (Automation.Properties.ContainsKey(pro))
196 else
197 return null;
198 Recorder.AddLine("Recordings.Get(" + rec.Name + ");");
199 return rec.Get();
200 }
201
206 private void view_AfterSelect(object sender, TreeViewEventArgs e)
207 {
208
209 }
210
217 private void startBut_Click(object sender, EventArgs e)
218 {
219 TextInput ti = new TextInput("Property" + propBox.Items.Count);
220 ti.Text = "Set Property Name";
221 if (ti.ShowDialog() != DialogResult.OK)
222 return;
223 recordStatusLabel.Text = "Recording: Started";
224 if (Automation.IsRecording)
225 return;
226 Automation.StartRecording();
227 }
228
235 private void stopBut_Click(object sender, EventArgs e)
236 {
237 recordStatusLabel.Text = "Recording: Stopped";
238 if (!Automation.IsRecording)
239 return;
240 Automation.StopRecording();
241 if (Automation.Recordings.Count > 0)
242 {
243 //actions = Automation.Recordings[0].List;
244 InitElements();
245 }
246 }
247
255 private void playBut_Click(object sender, EventArgs e)
256 {
257 if (view.SelectedNode == null)
258 return;
259 Node n = (Node)view.SelectedNode.Tag;
260 if (n.type == Node.Type.action)
261 return;;
262 n.recording.Run();
263 }
264
265 private void view_BeforeExpand(object sender, TreeViewCancelEventArgs e)
266 {
267
268 }
269
270 private void Elements_FormClosing(object sender, FormClosingEventArgs e)
271 {
272 this.Hide();
273 e.Cancel = true;
274 }
280 private void performToolStripMenuItem_Click(object sender, EventArgs e)
281 {
282 TreeNode tr = view.SelectedNode;
283 Node no = (Node)tr.Tag;
284 if (no.type == Node.Type.action)
285 no.action.Perform();
286 else
287 no.recording.Run();
288 }
295 private void saveToolStripMenuItem_Click(object sender, EventArgs e)
296 {
297 if (saveRecDialog.ShowDialog() != DialogResult.OK)
298 return;
299 SaveRecording(saveRecDialog.FileName);
300 }
306 private void SaveRecording(string file)
307 {
308 Automation.Recording n;
309 if (view.SelectedNode == null)
310 return;
311 else
312 {
313 n = ((Node)view.SelectedNode.Tag).recording;
314 }
315 string s = Path.GetFileNameWithoutExtension(saveRecDialog.FileName);
316 n.Name = s;
317 string j = JsonConvert.SerializeObject(n.List, Formatting.None);
318 File.WriteAllText(file, j);
319 InitElements();
320 }
327 private void SaveProperty(string file)
328 {
329 Automation.Recording n;
330 if (propView.SelectedNode == null)
331 return;
332 else
333 {
334 n = ((Node)propView.SelectedNode.Tag).recording;
335 }
336 string s = Path.GetFileNameWithoutExtension(savePropDialog.FileName);
337 n.Name = s;
338
339 string j = JsonConvert.SerializeObject(n.List, Formatting.None);
340 File.WriteAllText(file, j);
341 InitElements();
342 }
348 private Automation.Recording OpenRec(string file)
349 {
350 string st = File.ReadAllText(file);
351 Automation.Recording rec = new Automation.Recording();
352 rec.Name = Path.GetFileNameWithoutExtension(file);
353 rec.File = file;
354 Newtonsoft.Json.Linq.JArray ar = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(st);
355 for (int i = 0; i < ar.Count; i++)
356 {
357 Automation.Action.Type t = (Automation.Action.Type)Enum.Parse(typeof(Automation.Action.Type), ar[i].ElementAt(0).First.ToString());
358 if (t == Automation.Action.Type.keydown || t == Automation.Action.Type.keyup)
359 {
360 Keys k = (Keys)int.Parse(ar[i].ElementAt(2).First.ToString());
361 KeyEventArgs kea = new KeyEventArgs(k);
362 int ind = int.Parse(ar[i].ElementAt(9).First.ToString());
363 Automation.Action ac = new Automation.Action(t, ar[i].ElementAt(8).First.ToString(), ar[i].ElementAt(9).First.ToString(), ind, ar[i].ElementAt(3).First.ToString(), ar[i].ElementAt(4).First.ToString(), ar[i].ElementAt(5).First.ToString(), kea);
364 rec.List.Add(ac);
365 }
366 if (t == Automation.Action.Type.mousedown || t == Automation.Action.Type.mouseup)
367 {
368 MouseButtons mb = (MouseButtons)int.Parse(ar[i].ElementAt(2).First.ToString());
369 string ps = ar[i].ElementAt(3).First.ToString();
370 string xs = ps.Substring(0, ps.IndexOf(','));
371 string ys = ps.Substring(ps.IndexOf(',')+1, ps.Length - (ps.IndexOf(',')+1));
372 int ind = int.Parse(ar[i].ElementAt(9).First.ToString());
373 System.Drawing.Point po = new Point(int.Parse(xs),int.Parse(ys));
374 MouseEventArgs mo = new MouseEventArgs(mb, 1, po.X, po.Y, 0);
375 Automation.Action ac = new Automation.Action(t, ar[i].ElementAt(7).First.ToString(), ar[i].ElementAt(8).First.ToString(), ind, ar[i].ElementAt(4).First.ToString(), ar[i].ElementAt(5).First.ToString(), ar[i].ElementAt(6).First.ToString(),mo);
376 rec.List.Add(ac);
377 }
378 }
379 return rec;
380 }
384 public void OpenRecording(string file)
385 {
386 Automation.Recording rec = OpenRec(file);
387 Automation.Recordings.Add(rec.Name,rec);
388 InitElements();
389 }
393 public void OpenProperty(string file)
394 {
395 Automation.Recording rec = OpenRec(file);
396 Automation.Properties.Add(rec.Name,rec);
397 InitElements();
398 }
399
406 private void openToolStripMenuItem_Click(object sender, EventArgs e)
407 {
408 openRecDialog.InitialDirectory = Application.StartupPath + "\\Recordings";
409 if (openRecDialog.ShowDialog() != DialogResult.OK)
410 return;
411 OpenRecording(openRecDialog.FileName);
412 }
413
418 private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
419 {
420 Node n = (Node)view.SelectedNode.Tag;
421 if(n.type == Node.Type.recording)
422 {
423 Automation.Recordings.Remove(n.recording.Name);
424 File.Delete(n.recording.File);
425 InitElements();
426 }
427 else if (n.type == Node.Type.action)
428 {
429 n.recording.List.Remove(n.action);
431 }
432 }
433
438 private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
439 {
440 InitElements();
441 }
442
449 private void startPropBut_Click(object sender, EventArgs e)
450 {
451 TextInput ti = new TextInput("Property" + propBox.Items.Count);
452 ti.Text = "Set Property Name";
453 if (ti.ShowDialog() != DialogResult.OK)
454 return;
455 propRecStatusLabel.Text = "Property Recording: Started";
456 Automation.StartPropertyRecording(ti.Text);
457 }
458
467 private void stopPropBut_Click(object sender, EventArgs e)
468 {
469 propRecStatusLabel.Text = "Property Recording: Stopped";
470 if (!Automation.IsRecording)
471 return;
472 Automation.StopPropertyRecording();
473 if (Automation.Properties.Count > 0)
474 {
475 //actions = Automation.Recordings[0].List;
476 InitElements();
477 }
478 }
479
487 private void getPropBut_Click(object sender, EventArgs e)
488 {
489 if (propView.SelectedNode == null)
490 return;
491 Node n = (Node)propView.SelectedNode.Tag;
492 if (n.type == Node.Type.action)
493 return;
494 if (n.recording.List.Last().Value == Automation.Action.ValueType.Image)
495 Clipboard.SetImage((System.Drawing.Bitmap)Automation.GetProperty(n.recording.Name));
496 else
497 {
498 string s = (string)Automation.GetProperty(n.recording.Name);
499 MessageBox.Show(s);
500 }
501 }
508 private void setPropBut_Click(object sender, EventArgs e)
509 {
510 TextInput ti = new TextInput("Property" + propBox.Items.Count);
511 ti.Text = "Set Text To Set";
512 if (propView.SelectedNode == null)
513 return;
514 Node n = (Node)propView.SelectedNode.Tag;
515 if (n.type == Node.Type.action)
516 return;
517 Automation.SetProperty(n.recording.Name, ti.TextValue);
518 }
525 private void saveSelectedValueToolStripMenuItem_Click(object sender, EventArgs e)
526 {
527 savePropDialog.InitialDirectory = Application.StartupPath + "\\Recordings";
528 if (savePropDialog.ShowDialog() != DialogResult.OK)
529 return;
530 SaveProperty(savePropDialog.FileName);
531 }
532
539 private void openPropertyToolStripMenuItem_Click(object sender, EventArgs e)
540 {
541 openPropDialog.InitialDirectory = Application.StartupPath + "\\Recordings";
542 if (openPropDialog.ShowDialog() != DialogResult.OK)
543 return;
544 OpenProperty(openPropDialog.FileName);
545 }
546
551 private void getMenuItem_Click(object sender, EventArgs e)
552 {
553 getPropBut.PerformClick();
554 }
555
560 private void deletePropMenuItem_Click(object sender, EventArgs e)
561 {
562 Node n = (Node)propView.SelectedNode.Tag;
563 if (n.type == Node.Type.recording)
564 {
565 Automation.Properties.Remove(n.recording.Name);
566 if(n.recording.File!=null)
567 File.Delete(n.recording.File);
568 InitElements();
569 }
570 else if (n.type == Node.Type.action)
571 {
572 n.recording.List.Remove(n.action);
574 }
575 }
576
577
586 private void propView_AfterSelect(object sender, TreeViewEventArgs e)
587 {
588 if (propView.SelectedNode == null)
589 return;
590 Node n = (Node)propView.SelectedNode.Tag;
591 if (n.type == Node.Type.recording)
592 return;
593 propBox.SelectedIndex = (int)n.action.Value;
594 }
595
603 private void propBox_SelectedIndexChanged(object sender, EventArgs e)
604 {
605 if (propView.SelectedNode == null)
606 return;
607 Node n = (Node)propView.SelectedNode.Tag;
608 if (n.type == Node.Type.recording)
609 return;
610 n.action.Value = (Automation.Action.ValueType)propBox.SelectedItem;
611 }
612
619 private void moveUpToolStripMenuItem_Click(object sender, EventArgs e)
620 {
621 if (view.SelectedNode == null)
622 return;
623 Node n = (Node)view.SelectedNode.Tag;
624 if (n.type == Node.Type.recording)
625 return;
626 int oldindex = n.recording.List.IndexOf(n.action);
627 n.recording.List.RemoveAt(oldindex);
628 int newindex = oldindex - 1;
629 if (newindex > oldindex) newindex--;
630 // the actual index could have shifted due to the removal
631 n.recording.List.Insert(newindex, n.action);
633 }
634
642 private void moveDownToolStripMenuItem_Click(object sender, EventArgs e)
643 {
644 if (view.SelectedNode == null)
645 return;
646 Node n = (Node)view.SelectedNode.Tag;
647 if (n.type == Node.Type.recording)
648 return;
649 int oldindex = n.recording.List.IndexOf(n.action);
650 n.recording.List.RemoveAt(oldindex);
651 int newindex = oldindex - 1;
652 // the actual index could have shifted due to the removal
653 n.recording.List.Insert(newindex, n.action);
655 }
656
664 private void moveUpToolStripMenuItem1_Click(object sender, EventArgs e)
665 {
666 if (propView.SelectedNode == null)
667 return;
668 Node n = (Node)propView.SelectedNode.Tag;
669 if (n.type == Node.Type.recording)
670 return;
671 int oldindex = n.recording.List.IndexOf(n.action);
672 n.recording.List.RemoveAt(oldindex);
673 int newindex = oldindex - 1;
674 // the actual index could have shifted due to the removal
675 n.recording.List.Insert(newindex, n.action);
677 }
678
685 private void moveDownToolStripMenuItem1_Click(object sender, EventArgs e)
686 {
687 if (propView.SelectedNode == null)
688 return;
689 Node n = (Node)propView.SelectedNode.Tag;
690 if (n.type == Node.Type.recording)
691 return;
692 int oldindex = n.recording.List.IndexOf(n.action);
693 n.recording.List.RemoveAt(oldindex);
694 int newindex = (oldindex) + 1;
695 // the actual index could have shifted due to the removal
696 n.recording.List.Insert(newindex, n.action);
698 }
699
707 private void renameToolStripMenuItem_Click(object sender, EventArgs e)
708 {
709 if (propView.SelectedNode == null)
710 return;
711 Node n = (Node)propView.SelectedNode.Tag;
712 if (n.type == Node.Type.recording)
713 return;
714 n.action.Value = (Automation.Action.ValueType)propBox.SelectedItem;
715 }
716 }
717}
System.Windows.Forms.Keys Keys
Definition ImageView.cs:16
System.Drawing.Point Point
static Dictionary< string, Recording > Recordings
Definition Automation.cs:12
static Dictionary< string, Recording > Properties
Definition Automation.cs:13
static void AddLine(string s)
Definition Recorder.cs:6
Automation.Recording recording
Node(Automation.Action el, TreeNode n, Type t)
Node(Automation.Recording rec, TreeNode n, Type t)
Automation.Action action
override string ToString()
static void Perform(string act)
void OpenRecording(string file)
void UpdateElements()
It clears the nodes of the treeview, then adds the nodes back in.
Definition Recordings.cs:93
void InitElements()
It's a function that adds nodes to a treeview.
Definition Recordings.cs:32
void OpenProperty(string file)
static object GetProperty(Automation.Action.ValueType automation, string pro)