BioGTK  6.7.0
A .NET library & program for annotating, editing various microscopy imaging formats using Bioformats supported images. Including whole slide, pyramidal, and series.
Loading...
Searching...
No Matches
Functions.cs
1using Newtonsoft.Json;
2using Gtk;
3using System;
4using System.Collections.Generic;
5using System.IO;
6using Gdk;
7
8namespace BioGTK
9{
13 public class Functions : Gtk.Window
14 {
15 //public static InputSimulator input = new InputSimulator();
16 Function func = new Function();
17 public Function Func
18 {
19 get
20 {
21 return func;
22 }
23 set
24 {
25 func = value;
26 }
27 }
28
29 #region Properties
30 private Builder _builder;
31 int line = 0;
32#pragma warning disable 649
33 [Builder.Object]
34 private Button okBut;
35 [Builder.Object]
36 private Button cancelBut;
37 [Builder.Object]
38 private TextView textBox;
39 [Builder.Object]
40 private ComboBox funcsBox;
41 [Builder.Object]
42 private Entry nameBox;
43 [Builder.Object]
44 private ComboBox keysBox;
45 [Builder.Object]
46 private ComboBox stateBox;
47 [Builder.Object]
48 private ComboBox modifierBox;
49 [Builder.Object]
50 private SpinButton valBox;
51 [Builder.Object]
52 private Entry menuPath;
53 [Builder.Object]
54 private Entry contextMenuPath;
55 [Builder.Object]
56 private RadioButton imagejRadioBut;
57 [Builder.Object]
58 private RadioButton bioRadioBut;
59 [Builder.Object]
60 private Button setMacroFileBut;
61 [Builder.Object]
62 private Button setScriptFileBut;
63 [Builder.Object]
64 private Label fileLabel;
65#pragma warning restore 649
66
67 #endregion
68
69 #region Constructors / Destructors
72 public static Functions Create()
73 {
74 Builder builder = new Builder(new FileStream(System.IO.Path.GetDirectoryName(Environment.ProcessPath) + "/" + "Glade/Functions.glade", FileMode.Open));
75 return new Functions(builder, builder.GetObject("functions").Handle);
76 }
77
81 protected Functions(Builder builder, IntPtr handle) : base(handle)
82 {
83 _builder = builder;
84 builder.Autoconnect(this);
86 Init();
87 App.ApplyStyles(this);
88 }
89
90 #endregion
91
92 #region Handlers
93
95 protected void SetupHandlers()
96 {
97 funcsBox.Changed += FuncsBox_Changed;
98 keysBox.Changed += KeysBox_Changed;
99 stateBox.Changed += StateBox_Changed;
100 modifierBox.Changed += ModifiersBox_Changed;
101 okBut.Clicked += OkBut_Clicked;
102 cancelBut.Clicked += CancelBut_Clicked;
103 setMacroFileBut.Clicked += SetMacroFileBut_Clicked;
104 setScriptFileBut.Clicked += SetScriptFileBut_Clicked;
105 valBox.ChangeValue += ValBox_ChangeValue;
106 textBox.Buffer.Changed += Buffer_Changed;
107 imagejRadioBut.Clicked += ImagejRadioBut_Clicked;
108 bioRadioBut.Clicked += BioRadioBut_Clicked;
109 this.KeyPressEvent += Functions_KeyPressEvent;
110 this.DeleteEvent += Functions_DeleteEvent;
111 }
112 int i = 0;
113 private bool IterateComboBoxItems(ITreeModel model, TreePath path, TreeIter iter)
114 {
115 // Retrieve the value from the model
116 string item = (string)model.GetValue(iter, 0);
117 if (item == func.Key.ToString())
118 keysBox.Active = i;
119 i++;
120 // Return false to continue iterating
121 return false;
122 }
123 private void Functions_KeyPressEvent(object o, KeyPressEventArgs args)
124 {
125 i = 0;
126 func.Key = args.Event.Key;
127 keysBox.Model.Foreach(new TreeModelForeachFunc(IterateComboBoxItems));
128 }
129
134 private void Functions_DeleteEvent(object o, DeleteEventArgs args)
135 {
136 args.RetVal = true;
137 Hide();
138 }
139
144 private void BioRadioBut_Clicked(object sender, EventArgs e)
145 {
146 func.FuncType = Function.FunctionType.Script;
147 }
148
153 private void ImagejRadioBut_Clicked(object sender, EventArgs e)
154 {
155 func.FuncType = Function.FunctionType.ImageJ;
156 }
157
163 private void Buffer_Changed(object sender, EventArgs e)
164 {
165 func.Script = textBox.Buffer.Text;
166 if (bioRadioBut.Active)
167 func.FuncType = Function.FunctionType.Script;
168 else
169 func.FuncType = Function.FunctionType.ImageJ;
170 }
171
177 private void ValBox_ChangeValue(object o, ChangeValueArgs args)
178 {
179 Func.Value = valBox.Value;
180 }
181
189 private void SetScriptFileBut_Clicked(object sender, EventArgs e)
190 {
191 Gtk.FileChooserDialog filechooser =
192 new Gtk.FileChooserDialog("Choose the script file to open",
193 this,
194 FileChooserAction.Open,
195 "Cancel", ResponseType.Cancel,
196 "Open", ResponseType.Ok);
197 if (filechooser.Run() != (int)ResponseType.Ok)
198 return;
199 func.File = filechooser.Filename;
200 fileLabel.Text = func.File;
201 func.FuncType = Function.FunctionType.Script;
202 }
203
211 private void SetMacroFileBut_Clicked(object sender, EventArgs e)
212 {
213 Gtk.FileChooserDialog filechooser =
214 new Gtk.FileChooserDialog("Choose the ImageJ macro file to open",
215 this,
216 FileChooserAction.Open,
217 "Cancel", ResponseType.Cancel,
218 "Open", ResponseType.Ok);
219 if (filechooser.Run() != (int)ResponseType.Ok)
220 return;
221 func.File = filechooser.Filename;
222 fileLabel.Text = func.File;
223 func.FuncType = Function.FunctionType.ImageJ;
224 }
225
230 private void CancelBut_Clicked(object sender, EventArgs e)
231 {
232 Close();
233 }
234
239 private void OkBut_Clicked(object sender, EventArgs e)
240 {
241 func.Name = nameBox.Text;
242 func.MenuPath = menuPath.Text;
243 func.ContextPath = contextMenuPath.Text;
244 func.Value = valBox.Value;
245 if (!Function.Functions.ContainsKey(func.Name))
246 {
247 Function.Functions.Add(func.Name, func);
248 }
249 else
250 {
251 Function.Functions[func.Name] = func;
252 }
253 if (func.MenuPath != null && func.MenuPath != "")
254 {
255 if (func.MenuPath.EndsWith("/"))
256 func.MenuPath = func.MenuPath.TrimEnd('/');
257 if (func.MenuPath.EndsWith(func.Name) && func.MenuPath.Contains("/"))
258 func.MenuPath.Remove(func.MenuPath.IndexOf('/'), func.MenuPath.Length - func.MenuPath.IndexOf('/'));
259 App.AddMenu(func.MenuPath);
260 }
261 if (func.ContextPath != null && func.ContextPath != "")
262 {
263 if (func.ContextPath.EndsWith("/"))
264 func.ContextPath = func.ContextPath.TrimEnd('/');
265 if (func.ContextPath.EndsWith(func.Name) && func.ContextPath.Contains("/"))
266 func.ContextPath.Remove(func.ContextPath.IndexOf('/'), func.ContextPath.Length - func.ContextPath.IndexOf('/'));
267 App.AddContextMenu(func.ContextPath);
268 }
269 func.Save();
270 Init();
271 Close();
272 }
273
279 private void ModifiersBox_Changed(object sender, EventArgs e)
280 {
281 Func.Modifier = (Gdk.ModifierType)modifierBox.Active;
282 Func.FuncType = Function.FunctionType.Key;
283 }
284
290 private void StateBox_Changed(object sender, EventArgs e)
291 {
292 Func.State = (Function.ButtonState)stateBox.Active;
293 Func.FuncType = Function.FunctionType.Key;
294 }
295
301 private void KeysBox_Changed(object sender, EventArgs e)
302 {
303 Func.Key = (Gdk.Key)keysBox.Active;
304 Func.FuncType = Function.FunctionType.Key;
305 }
306
311 private void FuncsBox_Changed(object sender, EventArgs e)
312 {
313 func = Function.Functions[funcsBox.ActiveId];
314 UpdateItems();
315 }
316
317 #endregion
318
321 private void Init()
322 {
323 textBox.Buffer.Text = func.Script;
324 var funcs = new ListStore(typeof(string));
325 // Add items to the ListStore
326 foreach (Function item in Function.Functions.Values)
327 {
328 funcs.AppendValues(item.Name);
329 }
330 // Set the model for the ComboBox
331 funcsBox.Model = funcs;
332 // Set the text column to display
333 var renderer = new CellRendererText();
334 funcsBox.PackStart(renderer, false);
335 funcsBox.AddAttribute(renderer, "text", 0);
336
337 nameBox.Text = func.Name;
338
339 var states = new ListStore(typeof(string));
340 //We add button states to buttonState box.
341 foreach (Function.ButtonState val in Enum.GetValues(typeof(Function.ButtonState)))
342 {
343 states.AppendValues(val.ToString());
344 }
345 // Set the model for the ComboBox
346 stateBox.Model = states;
347 // Set the text column to display
348 var renderer2 = new CellRendererText();
349 stateBox.PackStart(renderer2, false);
350 stateBox.AddAttribute(renderer2, "text", 0);
351 stateBox.Active = (int)func.State;
352
353 //We add keys to keys box.
354 var keys = new ListStore(typeof(string));
355 foreach (Gdk.Key val in Enum.GetValues(typeof(Gdk.Key)))
356 {
357 keys.AppendValues(val.ToString());
358 }
359 keysBox.Model = keys;
360 // Set the text column to display
361 var renderer3 = new CellRendererText();
362 keysBox.PackStart(renderer3, false);
363 keysBox.AddAttribute(renderer3, "text", 0);
364 //We add keys to keys box.
365 var mods = new ListStore(typeof(string));
366 foreach (ModifierType val in Enum.GetValues(typeof(Gdk.ModifierType)))
367 {
368 mods.AppendValues(val.ToString());
369 }
370 modifierBox.Model = mods;
371 // Set the text column to display
372 var renderer4 = new CellRendererText();
373 modifierBox.PackStart(renderer4, false);
374 modifierBox.AddAttribute(renderer4, "text", 0);
375 valBox.Value = func.Value;
376 menuPath.Text = func.MenuPath;
377 contextMenuPath.Text = func.ContextPath;
378 }
380 private void UpdateItems()
381 {
382 textBox.Buffer.Text = func.Script;
383 nameBox.Text = func.Name;
384 stateBox.Active = (int)func.State;
385 keysBox.Active = (int)func.Key;
386 modifierBox.Active = (int)func.Modifier;
387 valBox.Value = func.Value;
388 if (func.FuncType == Function.FunctionType.ImageJ)
389 imagejRadioBut.Active = true;
390 else
391 imagejRadioBut.Active = false;
392 menuPath.Text = func.MenuPath;
393 contextMenuPath.Text = func.ContextPath;
394 }
395
396 }
397
398 public class Function
399 {
400 public static Dictionary<string, Function> Functions = new Dictionary<string, Function>();
401 /* Defining an enum. */
402 public enum FunctionType
403 {
404 Key,
405 Microscope,
406 Objective,
407 StoreCoordinate,
408 NextCoordinate,
409 PreviousCoordinate,
410 NextSnapCoordinate,
411 PreviousSnapCoordinate,
412 Recording,
413 Property,
414 ImageJ,
415 Script,
416 None
417 }
418 /* Defining an enumeration. */
419 public enum ButtonState
420 {
421 Pressed = 0,
422 Released = 1,
423 }
424
425 private ButtonState buttonState = ButtonState.Pressed;
426 /* A property. */
427 public ButtonState State
428 {
429 get
430 {
431 return buttonState;
432 }
433 set
434 {
435 buttonState = value;
436 }
437 }
438
439 private Gdk.Key key;
440 public Gdk.Key Key
441 {
442 get
443 {
444 return key;
445 }
446 set
447 {
448 key = value;
449 FuncType = FunctionType.Key;
450 }
451 }
452
453 private Gdk.ModifierType modifier;
454 public Gdk.ModifierType Modifier
455 {
456 get
457 {
458 return modifier;
459 }
460 set
461 {
462 FuncType = FunctionType.Key;
463 modifier = value;
464 }
465 }
466
467 private FunctionType functionType;
468 public FunctionType FuncType
469 {
470 get
471 {
472 return functionType;
473 }
474 set
475 {
476 functionType = value;
477 }
478 }
479
480 private string file;
481 public string File
482 {
483 get { return file; }
484 set { file = value; }
485 }
486
487 private string script;
488 public string Script
489 {
490 get { return script; }
491 set
492 {
493 script = value;
494 }
495 }
496
497 private string name;
498 public string Name
499 {
500 get
501 {
502 return name;
503 }
504 set
505 {
506 name = value;
507 }
508 }
509 private string menuPath;
510 public string MenuPath
511 {
512 get
513 {
514 return menuPath;
515 }
516 set
517 {
518 menuPath = value;
519 }
520 }
521 private string contextPath;
522 public string ContextPath
523 {
524 get
525 {
526 return contextPath;
527 }
528 set
529 {
530 contextPath = value;
531 }
532 }
533
534 private double val;
535 public double Value
536 {
537 get
538 {
539 return val;
540 }
541 set
542 {
543 val = value;
544 }
545 }
546 private string microscope;
547 public string Microscope
548 {
549 get
550 {
551 return microscope;
552 }
553 set
554 {
555 microscope = value;
556 }
557 }
558 public override string ToString()
559 {
560 return name + ", " + MenuPath;
561 }
567 public static Function Parse(string s)
568 {
569 if (s == "")
570 return new Function();
571 return JsonConvert.DeserializeObject<Function>(s);
572 }
573
577 public string Serialize()
578 {
579 return JsonConvert.SerializeObject(this);
580 }
589 public object PerformFunction(bool imagej)
590 {
591 if (FuncType == FunctionType.Key && Script != "")
592 if (imagej)
593 {
594 FuncType = FunctionType.ImageJ;
595 }
596 else
597 FuncType = FunctionType.Script;
598 if (FuncType == Function.FunctionType.Script)
599 {
600 return Scripting.RunString(script);
601 }
602 if (FuncType == Function.FunctionType.ImageJ)
603 {
604 Fiji.RunOnImage(ImageView.SelectedImage,script, false, BioConsole.onTab, BioConsole.useBioformats, BioConsole.resultInNewTab);
605 }
606 return null;
607 }
608
611 public static void InitializeMainMenu()
612 {
613 string st = System.IO.Path.GetDirectoryName(Environment.ProcessPath) + "/Functions";
614 if (!Directory.Exists(st))
615 Directory.CreateDirectory(st);
616 string[] sts = Directory.GetFiles(st);
617 for (int i = 0; i < sts.Length; i++)
618 {
619 string fs = System.IO.File.ReadAllText(sts[i]);
620 Function f = Function.Parse(fs);
621 if (!Functions.ContainsKey(f.Name))
622 Functions.Add(f.Name, f);
623 App.AddMenu(f.MenuPath);
624 }
625 }
628 public static void InitializeContextMenu()
629 {
630 string st = System.IO.Path.GetDirectoryName(Environment.ProcessPath) + "/Functions";
631 if (!Directory.Exists(st))
632 Directory.CreateDirectory(st);
633 string[] sts = Directory.GetFiles(st);
634 for (int i = 0; i < sts.Length; i++)
635 {
636 string fs = System.IO.File.ReadAllText(sts[i]);
637 Function f = Function.Parse(fs);
638 if (!Functions.ContainsKey(f.Name))
639 Functions.Add(f.Name, f);
640 App.AddContextMenu(f.ContextPath);
641 }
642 }
644 public static void SaveAll()
645 {
646 string st = System.IO.Path.GetDirectoryName(Environment.ProcessPath);
647
648 if (!Directory.Exists(st + "/Functions"))
649 {
650 Directory.CreateDirectory(st + "/Functions");
651 }
652 foreach (Function f in Functions.Values)
653 {
654 System.IO.File.WriteAllText(st + "/Functions/" + f.Name + ".func", f.Serialize());
655 }
656 }
658 public void Save()
659 {
660 string st = System.IO.Path.GetDirectoryName(Environment.ProcessPath);
661 if (!Directory.Exists(st + "/Functions"))
662 {
663 Directory.CreateDirectory(st + "/Functions");
664 }
665 System.IO.File.WriteAllText(st + "/Functions/" + Name + ".func", Serialize());
666 }
667 }
668
669}
static void InitializeContextMenu()
Definition Functions.cs:628
static void InitializeMainMenu()
Definition Functions.cs:611
void Save()
It saves the function to a file.
Definition Functions.cs:658
string Serialize()
Definition Functions.cs:577
static void SaveAll()
It saves all the functions in the Functions dictionary to a file.
Definition Functions.cs:644
static Function Parse(string s)
Definition Functions.cs:567
object PerformFunction(bool imagej)
Definition Functions.cs:589
Functions(Builder builder, IntPtr handle)
Specialised constructor for use only by derived class.
Definition Functions.cs:81
void SetupHandlers()
Sets up the handlers.
Definition Functions.cs:95
static Functions Create()
Default Shared Constructor.
Definition Functions.cs:72
static object RunString(string st)
Definition Scripting.cs:606
Definition ML.cs:17