BioGTK  6.5.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
App.cs
1using Bio;
2using Gdk;
3using Gtk;
4using OpenSlideGTK;
5using org.checkerframework.common.returnsreceiver.qual;
6using System;
7using System.Collections.Generic;
8using System.Linq;
9using System.Runtime.InteropServices;
10using System.Text;
11using System.Threading.Tasks;
12using Monitor = Gdk.Monitor;
13using Window = Gtk.Window;
14
15namespace BioGTK
16{
17 public static class App
18 {
19 public static Tools tools;
20 public static TabsView tabsView;
21 public static ImageView viewer;
22 public static ColorTool color;
23 public static Progress progress;
24 public static About about;
25 public static NodeView nodeView;
26 public static FiltersView filters;
27 public static ApplyFilter applyFilter;
28 public static ChannelsTool channelsTool;
29 public static ROIManager roiManager;
30 public static Scripting scripting;
31 public static BioConsole console;
32 public static Functions funcs;
33 public static StackTools stack;
34 public static SetTool setTool;
35 public static Resolutions resolutions;
36 public static Recorder recorder;
37 public static SAMTool samTool;
38 public static Updater updater;
39 private static int selectedIndex;
40 static bool useGPU = true, useVips = false, useImageSharp = false;
41 public static bool UseGPU
42 {
43 get
44 {
45 return useGPU;
46 }
47 set
48 {
49 useGPU = value;
50 useVips = !value;
51 useImageSharp = !value;
52 UpdateStitching();
53 }
54 }
55 public static bool UseVips
56 {
57 get
58 {
59 return useVips;
60 }
61 set
62 {
63 useVips = value;
64 useImageSharp = !value;
65 useGPU = !value;
66 UpdateStitching();
67 }
68 }
69 public static bool UseImageSharp
70 {
71 get
72 {
73 return useImageSharp;
74 }
75 set
76 {
77 useImageSharp = value;
78 useVips = !value;
79 useGPU = !value;
80 UpdateStitching();
81 }
82 }
83 private static void UpdateStitching()
84 {
85 if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
86 {
87 useGPU = false;
88 OpenSlideBase.useGPU = false;
89 //SlideBase.useGPU = false;
90 OpenSlideBase.UseVips = false;
91 SlideBase.UseVips = false;
92 return;
93 }
94 OpenSlideBase.useGPU = useGPU;
95 //SlideBase.useGPU = useGPU;
96 OpenSlideBase.UseVips = useVips;
97 SlideBase.UseVips = useVips;
98
99 }
100 public static bool ToConsole
101 {
102 get
103 {
104 return BioLib.Recorder.outputConsole;
105 }
106 set
107 {
108 BioLib.Recorder.outputConsole = value;
109 }
110 }
111 static bool useFiji = true;
112 public static bool UseFiji
113 {
114 get
115 {
116 if (OperatingSystem.IsMacOS())
117 {
118 return true;
119 }
120 else
121 {
122 if(Fiji.ImageJPath.Contains("Fiji"))
123 return true;
124 else
125 return false;
126 }
127 }
128 }
133 public static bool SetFijiOrImageJPath()
134 {
135 bool ifb;
136 BioLib.Settings.Load();
137 string st = BioLib.Settings.GetSettings("ImageJPath");
138 if (st != "")
139 {
140 if (st.Contains("Fiji"))
141 {
142 BioLib.Fiji.ImageJPath = st;
143 }
144 else
145 BioLib.ImageJ.ImageJPath = st;
146 return true;
147 }
148 string title = "Select ImageJ Executable Location";
149 if (OperatingSystem.IsMacOS())
150 title = "Select ImageJ Executable Location (Fiji.app/Contents/MacOS/ImageJ-macosx)";
151 Gtk.FileChooserDialog filechooser =
152 new Gtk.FileChooserDialog(title, Scripting.window,
153 FileChooserAction.Open,
154 "Cancel", ResponseType.Cancel,
155 "Save", ResponseType.Accept);
156 filechooser.SetCurrentFolder(System.IO.Path.GetDirectoryName(Environment.ProcessPath));
157 if (filechooser.Run() != (int)ResponseType.Accept)
158 return false;
159 if(filechooser.Filename.Contains("Fiji"))
160 Fiji.ImageJPath = filechooser.Filename;
161 else
162 ImageJ.ImageJPath = filechooser.Filename;
163 BioLib.Settings.AddSettings("ImageJPath", filechooser.Filename);
164 filechooser.Destroy();
165 BioLib.Settings.Save();
166 return true;
167 }
168 public static void MoveToMain(Window window)
169 {
170 MoveToMonitor(window, 0);
171 }
172 public static void MoveToMonitor(Window window, int monitorIndex)
173 {
174 Display display = Display.Default;
175 if (display == null)
176 return;
177
178 int monitorCount = display.NMonitors;
179
180 if (monitorIndex >= monitorCount)
181 monitorIndex = 0; // fallback to primary
182
183 Monitor monitor = display.GetMonitor(monitorIndex);
184 Rectangle geometry = monitor.Geometry;
185
186 // Move window to monitor origin
187 window.Move(geometry.X, geometry.Y);
188 }
189
191 public static void Initialize()
192 {
193 Console.WriteLine("Initializing components.");
194 SetFijiOrImageJPath();
195 if (Fiji.ImageJPath == "")
196 {
197 ImageJ.Initialize(ImageJ.ImageJPath);
198 BioImage.Initialize(ImageJ.ImageJPath);
199 }
200 else
201 {
202 Fiji.Initialize(Fiji.ImageJPath);
203 BioImage.Initialize(Fiji.ImageJPath);
204 }
205 Console.WriteLine("Loading settings.");
206 BioLib.Settings.Load();
207 updater = Updater.Create();
208 tools = Tools.Create();
209 filters = FiltersView.Create();
210 roiManager = ROIManager.Create();
211 scripting = Scripting.Create();
212 funcs = Functions.Create();
213 progress = Progress.Create("","","");
214 tools= Tools.Create();
215 //channelsTool = ChannelsTool.Create();
216 console = BioConsole.Create();
217 stack = StackTools.Create();
218 about = About.Create();
219 setTool = SetTool.Create();
220 recorder = Recorder.Create();
221 //color = ColorTool.Create();
222 UpdateStitching();
223 BioLib.Settings.Load();
224 }
225
226 public static void ApplyStyles(Widget widget)
227 {
228
229 if (widget != null)
230 {
231 if (widget is not Button || widget is not SpinButton)
232 {
233 widget.ModifyBg(StateType.Normal, new Gdk.Color(49, 91, 138));
234 widget.ModifyFg(StateType.Normal, new Gdk.Color(255, 255, 255));
235 }
236 }
237 try
238 {
239 if (widget is Container)
240 {
241 foreach (var child in ((Container)widget).Children)
242 {
243 if(child is not Button || child is not SpinButton)
244 ApplyStyles(child);
245 }
246 }
247 }
248 catch (Exception e)
249 {
250 Console.WriteLine(e.Message.ToString());
251 }
252 if(widget is Gtk.Window)
253 {
254 var window = (Gtk.Window)widget;
255 window.Icon = new Gdk.Pixbuf("bio.png");
256 window.DeleteEvent += Window_DeleteEvent;
257 window.DestroyEvent += Window_DestroyEvent;
258 window.FocusInEvent += Window_FocusInEvent;
259 }
260 }
261 private static void Window_FocusInEvent(object o, FocusInEventArgs args)
262 {
263 Gtk.Window w = (Gtk.Window)o;
264 w.GrabFocus();
265 }
266 private static void Window_DestroyEvent(object o, DestroyEventArgs args)
267 {
268 Gtk.Window w = (Gtk.Window)o;
269 w.Hide();
270 w.Destroy();
271 }
272
273 private static void Dialog_DeleteEvent(object o, DeleteEventArgs args)
274 {
275 Dialog w = (Dialog)o;
276 w.Hide();
277 w.Destroy();
278 }
279 private static void Dialog_DestroyEvent(object o, DestroyEventArgs args)
280 {
281 Dialog w = (Dialog)o;
282 w.Hide();
283 w.Destroy();
284 }
285 private static void Window_DeleteEvent(object o, DeleteEventArgs args)
286 {
287 Gtk.Window w = (Gtk.Window)o;
288 w.Hide();
289 w.Destroy();
290 }
291
292 public static void SelectWindow(string name)
293 {
294 TabsView tbs = App.tabsView;
295
296 int c = tbs.GetViewerCount();
297 for (int v = 0; v < c; v++)
298 {
299 int cc = tbs.GetViewer(v).Images.Count;
300 for (int im = 0; im < cc; im++)
301 {
302 if (tbs.GetViewer(v).Images[im].Filename == Path.GetFileName(name))
303 {
304 tbs.SetTab(v);
305 BioLib.Recorder.Record("App.SelectWindow(\"" + name + "\")");
306 ImageView.SelectedImage = tbs.GetViewer(v).Images[im];
307 viewer = tbs.GetViewer(v);
308 viewer.Present();
309 return;
310 }
311 }
312 }
313 }
314
315 public static void CloseWindow(string name)
316 {
317 BioLib.Recorder.Record("App.CloseWindow(\"" + name + "\")");
318 TabsView tbs = App.tabsView;
319 int c = tbs.GetViewerCount();
320 if (name == "ROI Manager")
321 App.roiManager.Close();
322 else
323 if (name == "Stack Tool")
324 App.stack.Close();
325 else
326 if (name == "Channels Tool")
327 App.channelsTool.Close();
328 else
329 if (name == "Scripting")
330 App.scripting.Close();
331 else
332 if (name == "SAM Tool")
333 App.samTool.Close();
334 else
335 if (name == "Recorder")
336 App.recorder.Close();
337 else
338 if (name == "Color Tool")
339 App.color.Destroy();
340 else
341 for (int v = 0; v < c; v++)
342 {
343 int cc = tbs.GetViewer(v).Images.Count;
344 for (int im = 0; im < cc; im++)
345 {
346 ImageView vi = tbs.GetViewer(v);
347 if (vi.Images[im].Filename == Path.GetFileName(name) || vi.Title == name)
348 {
349 tbs.RemoveTab(name);
350 vi.Close();
351 vi.Destroy();
352 Images.RemoveImage(vi.Images[im]);
353 return;
354 }
355 }
356 }
357
358 }
359
360 public static void Rename(string text)
361 {
362 App.tabsView.RenameTab(ImageView.SelectedImage.Filename, text);
363 ImageView.SelectedImage.Rename(text);
364 ImageView v = App.tabsView.GetViewer(ImageView.SelectedImage.Filename);
365 v.SetTitle(ImageView.SelectedImage.Filename);
366 BioLib.Recorder.AddLine("App.Rename(\"" + text + "\");",false);
367 }
368
378 static MenuItem FindItem(Menu w,string label)
379 {
380 foreach (Widget item in w.Children)
381 {
382 //We use a try block incase this widget is a menu instead of a menuitem.
383 try
384 {
385 Menu m = (Menu)item;
386 for (int i = 0; i < m.Children.Length; i++)
387 {
388 //We again use a try block incase this is a Menu instead of a menuitem
389 try
390 {
391 MenuItem it = (MenuItem)m.Children[i];
392 if (it.Label == label)
393 {
394 return it;
395 }
396 }
397 catch (Exception)
398 {
399
400 }
401 }
402 }
403 catch (Exception)
404 {
405 MenuItem it = (MenuItem)item;
406 if (it.Label == label)
407 {
408 return it;
409 }
410 }
411 }
412 return null;
413 }
414
430 private static void GetMainMenuItem(string path, out Widget wid, out bool Menu)
431 {
432 wid = null;
433 Menu = false;
434 string[] s = path.Split('/');
435 MenuBar w = tabsView.MainMenu;
436 for (int i = 0; i < s.Length; i++)
437 {
438 foreach (Widget item in w.Children)
439 {
440 //We use a try block incase this widget is a MenuItem instead of Menu.
441 try
442 {
443 MenuItem m = FindItem((Menu)item, s[i]);
444 if(m != null && i == s.Length -1)
445 {
446 wid = m;
447 Menu = true;
448 return;
449 }
450 }
451 catch (Exception)
452 {
453 MenuItem mi = (MenuItem)item;
454 if (mi.Label == s[i])
455 {
456 wid = mi;
457 Menu = false;
458 return;
459 }
460 }
461 }
462 }
463 if(wid == null)
464 GetMainImageJMenuItem(path, out wid, out Menu);
465 }
466
482 private static void GetMainImageJMenuItem(string path, out Widget wid, out bool Menu)
483 {
484 string[] s = path.Split('/');
485 MenuBar w = tabsView.ImageJMenu;
486 for (int i = 0; i < s.Length; i++)
487 {
488 foreach (Widget item in w.Children)
489 {
490 //We use a try block incase this widget is a MenuItem instead of Menu.
491 try
492 {
493 MenuItem m = FindItem((Menu)item, s[i]);
494 if (m != null && i == s.Length - 1)
495 {
496 wid = m;
497 Menu = true;
498 return;
499 }
500 }
501 catch (Exception)
502 {
503 MenuItem mi = (MenuItem)item;
504 if (mi.Label == s[i] && i == s.Length - 1)
505 {
506 wid = mi;
507 Menu = false;
508 return;
509 }
510 }
511 }
512 }
513 wid = null;
514 Menu = false;
515 }
516
532 private static void GetContextMenuItem(string path, out Widget wid, out bool Menu)
533 {
534 string[] s = path.Split('/');
535 Menu w = App.viewer.contextMenu;
536 for (int i = 0; i < s.Length; i++)
537 {
538 foreach (Widget item in w.Children)
539 {
540 //We use a try block incase this widget is a MenuItem instead of Menu.
541 try
542 {
543 MenuItem m = FindItem((Menu)item, s[i]);
544 if (m != null && i == s.Length - 1)
545 {
546 wid = m;
547 Menu = true;
548 return;
549 }
550 }
551 catch (Exception)
552 {
553 MenuItem mi = (MenuItem)item;
554 if (mi.Label == s[i] && i == s.Length - 1)
555 {
556 wid = mi;
557 Menu = false;
558 return;
559 }
560 }
561 }
562 }
563 wid = null;
564 Menu = false;
565 }
566
574 public static void AddMenu(string path)
575 {
576 if (path == null || path == "")
577 return;
578 //We get the Menu containing the MenuItem specified by the path.
579 Widget w;
580 bool menu;
581 GetMainMenuItem(System.IO.Path.GetDirectoryName(path), out w, out menu);
582 if(!menu && w != null)
583 {
584 MenuItem m = (MenuItem)w;
585 if(m.Submenu != null)
586 {
587 Menu me = (Menu)m.Submenu;
588 MenuItem mi = new MenuItem(System.IO.Path.GetFileName(path));
589 mi.ButtonPressEvent += ItemClicked;
590 me.Append(mi);
591 me.ShowAll();
592 return;
593 }
594 else
595 {
596 //If the item we need to add a child item to, is not already a Menu we need to make it a menu
597 Menu me = new Menu();
598 MenuItem mi = new MenuItem(System.IO.Path.GetFileName(path));
599 mi.ButtonPressEvent += ItemClicked;
600 me.Append(mi);
601 m.Submenu = me;
602 me.ShowAll();
603 return;
604 }
605 }
606 else if(menu && w!=null)
607 {
608 //Since this is already a Menu we can just append the item to it.
609 Menu m = (Menu)w;
610 MenuItem mi = new MenuItem(System.IO.Path.GetFileName(path));
611 mi.ButtonPressEvent += ItemClicked;
612 m.Append(mi);
613 }
614
615 GetMainImageJMenuItem(System.IO.Path.GetDirectoryName(path), out w, out menu);
616 if (!menu && w != null)
617 {
618 MenuItem m = (MenuItem)w;
619 if (m.Submenu != null)
620 {
621 Menu me = (Menu)m.Submenu;
622 MenuItem mi = new MenuItem(System.IO.Path.GetFileName(path));
623 mi.ButtonPressEvent += ItemClicked;
624 me.Append(mi);
625 me.ShowAll();
626 }
627 else
628 {
629 //If the item we need to add a child item to, is not already a Menu we need to make it a menu
630 Menu me = new Menu();
631 MenuItem mi = new MenuItem(System.IO.Path.GetFileName(path));
632 mi.ButtonPressEvent += ItemClicked;
633 me.Append(mi);
634 m.Submenu = me;
635 me.ShowAll();
636 }
637 }
638 else if (menu && w != null)
639 {
640 //Since this is already a Menu we can just append the item to it.
641 Menu m = (Menu)w;
642 MenuItem mi = new MenuItem(System.IO.Path.GetFileName(path));
643 mi.ButtonPressEvent += ItemClicked;
644 m.Append(mi);
645 }
646 }
653 public static void AddContextMenu(string path)
654 {
655 if (path == null || path == "")
656 return;
657 string[] s = path.Split('/');
658 //We get the Menu containing the MenuItem specified by the path.
659
660 Widget w;
661 bool menu;
662 GetContextMenuItem(System.IO.Path.GetDirectoryName(path), out w, out menu);
663 if (!menu && w != null)
664 {
665 MenuItem m = (MenuItem)w;
666 if (m.Submenu != null)
667 {
668 Menu me = (Menu)m.Submenu;
669 MenuItem mi = new MenuItem(System.IO.Path.GetFileName(path));
670 mi.ButtonPressEvent += ItemClicked;
671 me.Append(mi);
672 me.ShowAll();
673 }
674 else
675 {
676 //If the item we need to add a child item to, is not already a Menu we need to make it a menu
677 Menu me = new Menu();
678 MenuItem mi = new MenuItem(System.IO.Path.GetFileName(path));
679 mi.ButtonPressEvent += ItemClicked;
680 me.Append(mi);
681 m.Submenu = me;
682 me.ShowAll();
683 }
684 }
685 else if (!menu && w == null)
686 {
687 MenuItem mi = new MenuItem(System.IO.Path.GetFileName(path));
688 mi.ButtonPressEvent += ItemClicked;
689 viewer.contextMenu.Append(mi);
690 viewer.contextMenu.ShowAll();
691 }
692 }
702 private static void ItemClicked(object o, ButtonPressEventArgs args)
703 {
704 MenuItem ts = (MenuItem)o;
705 if (Function.Functions.ContainsKey(ts.Label))
706 {
707 Function f = Function.Functions[ts.Label];
708 f.PerformFunction(true);
709 }
710 else
711 {
712 if (ts.Label.EndsWith(".dll"))
713 {
714 try
715 {
716 Plugin.Plugins[ts.Label].Execute(new string[] { });
717 }
718 catch (Exception e)
719 {
720 Console.WriteLine(e.Message.ToString());
721 }
722 }
723 else if(ts.Label.EndsWith(".onnx") || ts.Label.EndsWith(".pt"))
724 {
725 try
726 {
727 ML.ML.Run(ts.Label, ImageView.SelectedImage);
728 }
729 catch (Exception e)
730 {
731 Console.WriteLine(e.Message.ToString());
732 }
733 }
734 }
735 }
736 }
737}
Definition ML.cs:17