BioImager  4.9.0
A .NET microscopy imaging application based on Bio library. Supports various microscopes by using imported libraries & GUI automation. Supports XInput game controllers to move stage, take images, run ImageJ macros on images or Bio C# scripts.
Loading...
Searching...
No Matches
App.cs
1using AForge;
2using BioImager;
3using System;
4using System.Collections.Generic;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8using System.Windows.Forms;
9using BioLib;
10namespace BioImager
11{
12 public class App
13 {
14 public static ROIManager manager = null;
15 public static ChannelsTool channelsTool = null;
16 public static TabsView tabsView = null;
17 public static NodeView nodeView = null;
18 public static Scripting runner = null;
19 public static Recorder recorder = null;
20 public static Imager imager = null;
21 public static Tools tools = null;
22 public static StackTools stackTools = null;
23 public static ImageView viewer = null;
24 public static StageTool stage = null;
25 public static Series seriesTool = null;
26 public static Recordings recordings = null;
27 public static Automation automation = null;
28 public static MicroscopeSetup setup = null;
29 public static CellImager cellImager = null;
30 public static SlideImager slideImager = null;
31 public static BioConsole console = null;
32 public static Light lightTool = null;
33 public static Library lib = null;
34 public static List<string> recent = new List<string>();
35
36
37 public static void UpdateImages()
38 {
39 do
40 {
41 Thread.Sleep(1000);
42 foreach (var item in Images.images)
43 {
44 if(App.tabsView!=null)
45 if (App.tabsView.HasTab(item.Filename))
46 continue;
47 else
48 App.tabsView.AddTab(item);
49 }
50 } while (true);
51 }
52
53 /* A property that returns the current image. */
54 public static BioImage Image
55 {
56 get
57 {
58 if (ImageView.SelectedImage == null)
59 return tabsView.Image;
60 return ImageView.SelectedImage;
61 }
62 }
63 public static List<Channel> Channels
64 {
65 get { return Image.Channels; }
66 }
67
68 public static List<ROI> Annotations
69 {
70 get { return Image.Annotations; }
71 }
73 public static void Initialize()
74 {
75 BioImage.Initialize(ImageJ.ImageJPath);
76 Thread th = new Thread(UpdateImages);
77 th.Start();
78 do
79 {
80 Thread.Sleep(100);
81 } while (!BioImage.Initialized);
82 imager = new Imager();
83 Microscope.Initialize();
85 setup = new MicroscopeSetup();
86 stage = new StageTool();
87 cellImager = new CellImager();
88 stackTools = new StackTools();
89 tools = new Tools();
90 manager = new ROIManager();
91 runner = new Scripting();
92 recorder = new Recorder();
93 seriesTool = new Series();
94 recordings = new Recordings();
95 automation = new Automation();
96 slideImager = new SlideImager();
97 if (!Properties.Settings.Default.PycroManager && !Properties.Settings.Default.PMicroscope)
98 {
99 lightTool = new Light();
100 }
101 lib = new Library();
102
103 console = new BioConsole();
104 imager.Show();
105
106 //ImageJ.Initialize(Properties.Settings.Default.ImageJPath);
107 }
109 public static void Hide()
110 {
111 tools.Hide();
112 stackTools.Hide();
113 manager.Hide();
114 runner.Hide();
115 recorder.Hide();
116 seriesTool.Hide();
117 recordings.Hide();
118 console.Hide();
119 }
120
124 private static IEnumerable<ToolStripMenuItem> GetItems(ToolStripMenuItem item)
125 {
126 foreach (var dropDownItem in item.DropDownItems)
127 {
128 if (dropDownItem.GetType() == typeof(ToolStripMenuItem))
129 {
130 if (((ToolStripMenuItem)dropDownItem).HasDropDownItems)
131 {
132 foreach (ToolStripMenuItem subItem in GetItems((ToolStripMenuItem)dropDownItem))
133 yield return subItem;
134 }
135 yield return (ToolStripMenuItem)dropDownItem;
136 }
137 }
138 }
142 public static List<ToolStripMenuItem> GetMenuItems()
143 {
144 List<ToolStripMenuItem> allItems = new List<ToolStripMenuItem>();
145 foreach (ToolStripMenuItem toolItem in tabsView.MainMenuStrip.Items)
146 {
147 allItems.Add(toolItem);
148 allItems.AddRange(GetItems(toolItem));
149 }
150 return allItems;
151 }
155 public static List<ToolStripMenuItem> GetContextItems()
156 {
157 List<ToolStripMenuItem> allItems = new List<ToolStripMenuItem>();
158 foreach (ToolStripMenuItem toolItem in viewer.ViewContextMenu.Items)
159 {
160 allItems.Add(toolItem);
161 allItems.AddRange(GetItems(toolItem));
162 }
163 return allItems;
164 }
172 public static ToolStripItem GetMenuItemFromPath(string s, Function f)
173 {
174 if (s == "" || s == null)
175 return null;
176 string[] sts = s.Split('/');
177 start:
178 List<ToolStripMenuItem> allItems = GetMenuItems();
179 //Find path or create it.
180 bool found = false;
181 ToolStripMenuItem item = null;
182
183 for (int t = 0; t < sts.Length; t++)
184 {
185 found = false;
186 for (int i = 0; i < allItems.Count; i++)
187 {
188 if (allItems[i].Text == sts[t])
189 {
190 item = allItems[i];
191 found = true;
192 if (t == sts.Length - 1)
193 return allItems[i];
194 }
195 }
196 if (!found)
197 {
198 if(t == 0)
199 {
200 tabsView.MainMenuStrip.Items.Add(sts[t]);
201 goto start;
202 }
203 else if(t > 0 && t < sts.Length)
204 {
205 ToolStripMenuItem itm = new ToolStripMenuItem();
206 itm.Tag = f;
207 itm.Name = f.Name;
208 item.DropDownItems.Add(f.Name, null, ItemClicked);
209 return item;
210 }
211 else
212 {
213 item.DropDownItems.Add(sts[t]);
214 }
215 }
216 }
217 return item;
218 }
226 public static ToolStripItem GetContextMenuItemFromPath(string s, Function f)
227 {
228 if (s == "" || s == null)
229 return null;
230 string[] sts = s.Split('/');
231 start:
232 List<ToolStripMenuItem> allItems = GetContextItems();
233 //Find path or create it.
234 bool found = false;
235 ToolStripMenuItem item = null;
236
237 for (int t = 0; t < sts.Length; t++)
238 {
239 found = false;
240 for (int i = 0; i < allItems.Count; i++)
241 {
242 if (allItems[i].Text == sts[t])
243 {
244 item = allItems[i];
245 found = true;
246 if (t == sts.Length - 1)
247 return allItems[i];
248 }
249 }
250 if (!found)
251 {
252 if (t == 0)
253 {
254 viewer.ViewContextMenu.Items.Add(sts[t]);
255 goto start;
256 }
257 else if (t > 0 && t < sts.Length)
258 {
259 ToolStripMenuItem itm = new ToolStripMenuItem();
260 itm.Tag = f;
261 itm.Name = f.Name;
262 item.DropDownItems.Add(f.Name, null, ItemClicked);
263 return item;
264 }
265 else
266 {
267 item.DropDownItems.Add(sts[t]);
268 }
269 }
270 }
271 return item;
272 }
273
281 public static ToolStripItem GetMenuItemFromPath(string s)
282 {
283 if (s == "" || s == null)
284 return null;
285 string[] sts = s.Split('/');
286 start:
287 List<ToolStripMenuItem> allItems = GetMenuItems();
288 //Find path or create it.
289 bool found = false;
290 ToolStripMenuItem item = null;
291
292 for (int t = 0; t < sts.Length; t++)
293 {
294 found = false;
295 for (int i = 0; i < allItems.Count; i++)
296 {
297 if (allItems[i].Text == sts[t])
298 {
299 item = allItems[i];
300 found = true;
301 if (t == sts.Length - 1)
302 return allItems[i];
303 }
304 }
305 if (!found)
306 {
307 if (t == 0)
308 {
309 tabsView.MainMenuStrip.Items.Add(sts[t]);
310 goto start;
311 }
312 else if (t > 0 && t < sts.Length)
313 {
314 ToolStripMenuItem itm = new ToolStripMenuItem();
315 itm.Text = Path.GetFileName(s);
316 item.DropDownItems.Add(Path.GetFileName(s), null, ItemClicked);
317 return item;
318 }
319 else
320 {
321 item.DropDownItems.Add(sts[t]);
322 }
323 }
324 }
325 return item;
326 }
334 public static ToolStripItem GetContextMenuItemFromPath(string s)
335 {
336 if (s == "" || s == null)
337 return null;
338 string[] sts = s.Split('/');
339 start:
340 List<ToolStripMenuItem> allItems = GetContextItems();
341 //Find path or create it.
342 bool found = false;
343 ToolStripMenuItem item = null;
344
345 for (int t = 0; t < sts.Length; t++)
346 {
347 found = false;
348 for (int i = 0; i < allItems.Count; i++)
349 {
350 if (allItems[i].Text == sts[t])
351 {
352 item = allItems[i];
353 found = true;
354 if (t == sts.Length - 1)
355 return allItems[i];
356 }
357 }
358 if (!found)
359 {
360 if (t == 0)
361 {
362 viewer.ViewContextMenu.Items.Add(sts[t]);
363 goto start;
364 }
365 else if (t > 0 && t < sts.Length)
366 {
367 ToolStripMenuItem itm = new ToolStripMenuItem();
368 itm.Name = Path.GetFileName(s);
369 item.DropDownItems.Add(Path.GetFileName(s), null, ItemClicked);
370 return item;
371 }
372 else
373 {
374 item.DropDownItems.Add(sts[t]);
375 }
376 }
377 }
378 return item;
379 }
380
386 public static void AddMenu(string menu, Function f)
387 {
388 GetMenuItemFromPath(menu, f);
389 }
394 public static void AddContextMenu(string menu, Function f)
395 {
397 }
398
404 public static void AddMenu(string menu)
405 {
407 }
412 public static void AddContextMenu(string menu)
413 {
415 }
416
417
423 private static void ItemClicked(object sender, EventArgs e)
424 {
425 ToolStripMenuItem ts = (ToolStripMenuItem)sender;
426 if(ts.Text.EndsWith(".dll"))
427 {
428 try
429 {
430 Plugin.Plugins[ts.Text].Execute(new string[] { });
431 }
432 catch (Exception ex)
433 {
434 Console.WriteLine(ex.ToString());
435 }
436
437 }
438 else if(ts.Text.EndsWith(".ijm"))
439 {
440 string st = File.ReadAllText(Path.GetDirectoryName(ImageJ.ImageJPath) + "/macros/" + ts.Text);
441 ImageJ.RunOnImage(st, BioConsole.headless, BioConsole.onTab, BioConsole.useBioformats, BioConsole.newTab);
442 }
443 else if(ts.Text.EndsWith(".pt") || ts.Text.EndsWith(".onnx"))
444 {
445 try
446 {
447 ML.ML.Run(ts.Text, ImageView.SelectedImage);
448 }
449 catch (Exception ex)
450 {
451 Console.WriteLine(ex.Message.ToString());
452 }
453 }
454 else
455 {
456 Function f = (Function)ts.Tag;
457 f.PerformFunction(true);
458 }
459 }
463 public static void AddROI(string an)
464 {
465 Annotations.Add(BioImage.StringToROI(an));
466 Recorder.AddLine("App.AddROI(" + '"' + an + "'" + ");");
467 }
468 }
469}
static void AddMenu(string menu, Function f)
Definition App.cs:386
static void Hide()
Hide() hides all the tools.
Definition App.cs:109
static ToolStripItem GetMenuItemFromPath(string s)
Definition App.cs:281
static ToolStripItem GetContextMenuItemFromPath(string s, Function f)
Definition App.cs:226
static ToolStripItem GetMenuItemFromPath(string s, Function f)
Definition App.cs:172
static List< ToolStripMenuItem > GetContextItems()
Definition App.cs:155
static ToolStripItem GetContextMenuItemFromPath(string s)
Definition App.cs:334
static void AddContextMenu(string menu)
Definition App.cs:412
static List< ToolStripMenuItem > GetMenuItems()
Definition App.cs:142
static void Initialize()
Initialize() is a function that initializes the BioImager UI.
Definition App.cs:73
static void AddROI(string an)
Definition App.cs:463
static void AddMenu(string menu)
Definition App.cs:404
static void AddContextMenu(string menu, Function f)
Definition App.cs:394
static void RunOnImage(string con, bool headless, bool onTab, bool bioformats, bool newTab)
Definition ImageJ.cs:189
static bool Initialize()
Definition ImageJ.cs:247
ImageView control for image stacks, pyramidal and whole-slide-images.
ContextMenuStrip ViewContextMenu
A property that returns a ContextMenuStrip object. *‍/.
Definition ImageView.cs:831
void AddTab(BioImage b)
Definition TabsView.cs:129