BioCore  5.0.0
A Windows .NET microscopy library and application for viewing, editing, and annotating images.
Loading...
Searching...
No Matches
FunctionForm.cs
Go to the documentation of this file.
1using BioCore;
2using Newtonsoft.Json;
3using WindowsInput;
4using System.ComponentModel;
5
6namespace BioCore
7{
8 public partial class FunctionForm : Form
9 {
10 public static InputSimulator input = new InputSimulator();
11 private Function func;
12 /* A property. */
13 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
15 {
16 get
17 {
18 return func;
19 }
20 set
21 {
22 func = value;
23 }
24 }
25 /* Initializing the form. */
27 {
28 InitializeComponent();
29 Func = func;
30 Init();
31 }
32 /* Initializing the form. */
33 public FunctionForm(Function func, string name)
34 {
35 InitializeComponent();
36 Func = func;
37 func.Name = name;
38 Init();
39 }
41 private void Init()
42 {
43 int ind = 0;
44 int sel = 0;
45
46 textBox.Text = func.Script;
47
48 funcsBox.Items.Clear();
49 foreach (Function f in Function.Functions.Values)
50 {
51 funcsBox.Items.Add(f);
52 }
53
54 nameBox.Text = func.Name;
55 //We add button states to buttonState box.
56 stateBox.Items.Clear();
57 foreach (Function.ButtonState val in Enum.GetValues(typeof(Function.ButtonState)))
58 {
59 stateBox.Items.Add(val);
60 }
61 stateBox.SelectedItem = func.State;
62
63 //We add button states to buttonState box.
64 ind = 0;
65 keysBox.Items.Clear();
66 foreach (VirtualKeyCode val in Enum.GetValues(typeof(VirtualKeyCode)))
67 {
68 keysBox.Items.Add(val);
69 if (val == func.Key)
70 keysBox.SelectedIndex = ind;
71 ind++;
72 }
73 //We add the modifiers to modifierBox
74 modifierBox.Items.Clear();
75 modifierBox.Items.Add(VirtualKeyCode.NONAME);
76 modifierBox.Items.Add(VirtualKeyCode.RCONTROL);
77 modifierBox.Items.Add(VirtualKeyCode.LCONTROL);
78 modifierBox.Items.Add(VirtualKeyCode.RSHIFT);
79 modifierBox.Items.Add(VirtualKeyCode.LSHIFT);
80 modifierBox.Items.Add(VirtualKeyCode.MENU);
81 modifierBox.Items.Add(VirtualKeyCode.RWIN);
82 modifierBox.Items.Add(VirtualKeyCode.LWIN);
83 modifierBox.SelectedItem = func.Modifier;
84 ind = 0;
85 valBox.Value = (decimal)func.Value;
86 menuPath.Text = func.MenuPath;
87 contextMenuPath.Text = func.ContextPath;
88 }
90 private void UpdateItems()
91 {
92 textBox.Text = func.Script;
93 nameBox.Text = func.Name;
94 stateBox.SelectedItem = func.State;
95 keysBox.SelectedItem = func.Key;
96 modifierBox.SelectedItem = func.Modifier;
97 valBox.Value = (decimal)func.Value;
98 if (func.FuncType == Function.FunctionType.ImageJ)
99 imageJRadioBut.Checked = true;
100 else
101 imageJRadioBut.Checked = false;
102 menuPath.Text = func.MenuPath;
103 contextMenuPath.Text = func.ContextPath;
104 }
109 private void keysBox_SelectedIndexChanged(object sender, EventArgs e)
110 {
111 Func.Key = (VirtualKeyCode)keysBox.SelectedItem;
112 Func.FuncType = Function.FunctionType.Key;
113 }
119 private void modifierBox_SelectedIndexChanged(object sender, EventArgs e)
120 {
121 Func.Modifier = (VirtualKeyCode)modifierBox.SelectedItem;
122 Func.FuncType = Function.FunctionType.Key;
123 }
129 private void stateBox_SelectedIndexChanged(object sender, EventArgs e)
130 {
131 Func.State = (Function.ButtonState)stateBox.SelectedItem;
132 Func.FuncType = Function.FunctionType.Key;
133 }
138 private void applyButton_Click(object sender, EventArgs e)
139 {
140 func.Name = nameBox.Text;
141 this.DialogResult = DialogResult.OK;
142 if (!Function.Functions.ContainsKey(func.Name))
143 {
144 Function.Functions.Add(func.Name, func);
145 }
146 else
147 {
148 Function.Functions[func.Name] = func;
149 }
150 if (func.MenuPath != null && func.MenuPath != "")
151 {
152 if (func.MenuPath.EndsWith("/"))
153 func.MenuPath = func.MenuPath.TrimEnd('/');
154 if (func.MenuPath.EndsWith(func.Name) && func.MenuPath.Contains("/"))
155 func.MenuPath.Remove(func.MenuPath.IndexOf('/'), func.MenuPath.Length - func.MenuPath.IndexOf('/'));
156 App.AddMenu(func.MenuPath, func);
157 }
158 if (func.ContextPath != null && func.ContextPath != "")
159 {
160 if (func.ContextPath.EndsWith("/"))
161 func.ContextPath = func.ContextPath.TrimEnd('/');
162 if (func.ContextPath.EndsWith(func.Name) && func.ContextPath.Contains("/"))
163 func.ContextPath.Remove(func.ContextPath.IndexOf('/'), func.ContextPath.Length - func.ContextPath.IndexOf('/'));
164 App.AddContextMenu(func.ContextPath, func);
165 }
166 func.Save();
167 Init();
168 }
169
176 private void setMacroFileBut_Click(object sender, EventArgs e)
177 {
178 if (openFileDialog.ShowDialog() != DialogResult.OK)
179 return;
180 Func.File = openFileDialog.FileName;
181 Func.FuncType = Function.FunctionType.ImageJ;
182 }
189 private void setScriptFileBut_Click(object sender, EventArgs e)
190 {
191 if (openFileDialog.ShowDialog() != DialogResult.OK)
192 return;
193 Func.File = openFileDialog.FileName;
194 Func.FuncType = Function.FunctionType.Script;
195 }
201 private void valBox_ValueChanged(object sender, EventArgs e)
202 {
203 Func.Value = (double)valBox.Value;
204 }
210 private void performBut_Click(object sender, EventArgs e)
211 {
212 MessageBox.Show(func.PerformFunction(imageJRadioBut.Checked).ToString());
213 }
214
220 private void cancelBut_Click(object sender, EventArgs e)
221 {
222 this.DialogResult = DialogResult.Cancel;
223 }
228 private void textBox_TextChanged(object sender, EventArgs e)
229 {
230 func.Script = textBox.Text;
231 if (bioRadioBut.Checked)
232 func.FuncType = Function.FunctionType.Script;
233 else
234 func.FuncType = Function.FunctionType.ImageJ;
235 }
236
241 private void bioRadioBut_CheckedChanged(object sender, EventArgs e)
242 {
243 if (bioRadioBut.Checked)
244 func.FuncType = Function.FunctionType.Script;
245 }
246
251 private void imageJRadioBut_CheckedChanged(object sender, EventArgs e)
252 {
253 if (imageJRadioBut.Checked)
254 func.FuncType = Function.FunctionType.ImageJ;
255 }
256
261 private void menuPath_TextChanged(object sender, EventArgs e)
262 {
263 func.MenuPath = menuPath.Text;
264 }
265
270 private void FunctionForm_Activated(object sender, EventArgs e)
271 {
272
273 }
274
281 private void funcsBox_SelectedIndexChanged(object sender, EventArgs e)
282 {
283 if (funcsBox.SelectedIndex == -1)
284 return;
285 func = (Function)funcsBox.SelectedItem;
286 UpdateItems();
287 }
288
293 private void FunctionForm_FormClosing(object sender, FormClosingEventArgs e)
294 {
295
296 }
297
303 private void contextMenuPath_TextChanged(object sender, EventArgs e)
304 {
305 func.ContextPath = contextMenuPath.Text;
306 }
307 }
308
309 public class Function
310 {
311 public static Dictionary<string, Function> Functions = new Dictionary<string, Function>();
312 /* Defining an enum. */
329 public enum ButtonState
330 {
331 Pressed,
333 }
334
335 private ButtonState buttonState = ButtonState.Pressed;
337 {
338 get
339 {
340 return buttonState;
341 }
342 set
343 {
344 buttonState = value;
345 }
346 }
347
348 private VirtualKeyCode key;
349 public VirtualKeyCode Key
350 {
351 get
352 {
353 return key;
354 }
355 set
356 {
357 key = value;
359 }
360 }
361
362 private VirtualKeyCode modifier = VirtualKeyCode.NONAME;
363 public VirtualKeyCode Modifier
364 {
365 get
366 {
367 return modifier;
368 }
369 set
370 {
372 modifier = value;
373 }
374 }
375
376 private FunctionType functionType;
378 {
379 get
380 {
381 return functionType;
382 }
383 set
384 {
385 functionType = value;
386 }
387 }
388
389 private string file;
390 public string File
391 {
392 get { return file; }
393 set { file = value; }
394 }
395
396 private string script;
397 public string Script
398 {
399 get { return script; }
400 set
401 {
402 script = value;
403 }
404 }
405
406 private string name;
407 public string Name
408 {
409 get
410 {
411 return name;
412 }
413 set
414 {
415 name = value;
416 }
417 }
418 private string menuPath;
419 public string MenuPath
420 {
421 get
422 {
423 return menuPath;
424 }
425 set
426 {
427 menuPath = value;
428 }
429 }
430 private string contextPath;
431 public string ContextPath
432 {
433 get
434 {
435 return contextPath;
436 }
437 set
438 {
439 contextPath = value;
440 }
441 }
442
443 private double val;
444 public double Value
445 {
446 get
447 {
448 return val;
449 }
450 set
451 {
452 val = value;
453 }
454 }
455 private string microscope;
456 public string Microscope
457 {
458 get
459 {
460 return microscope;
461 }
462 set
463 {
464 microscope = value;
465 }
466 }
467 public override string ToString()
468 {
469 return name + ", " + MenuPath;
470 }
477 public static Function Parse(string s)
478 {
479 if (s == "")
480 return new Function();
481 return JsonConvert.DeserializeObject<Function>(s);
482 }
483
487 public string Serialize()
488 {
489 return JsonConvert.SerializeObject(this);
490 }
491
492 public static InputSimulator input = new InputSimulator();
498 public object PerformFunction(bool imagej)
499 {
500 if (FuncType == FunctionType.Key && Script != "")
501 if (imagej)
502 {
503 FuncType = FunctionType.ImageJ;
504 }
505 else
506 FuncType = FunctionType.Script;
507 if (FuncType == Function.FunctionType.Script)
508 {
509 Scripting.RunString(script);
510 }
511 if (FuncType == Function.FunctionType.ImageJ)
512 {
514 }
515
516 if (FuncType == Function.FunctionType.Key)
517 {
518 if (Modifier != VirtualKeyCode.NONAME)
519 {
520 input.Keyboard.ModifiedKeyStroke(Modifier, Key);
521 }
522 else
523 {
524 input.Keyboard.KeyPress(Key);
525 }
526 return null;
527 }
528 return null;
529 }
532 public static void Initialize()
533 {
534 string st = Application.StartupPath + "/Functions";
535 if (!Directory.Exists(st))
536 Directory.CreateDirectory(st);
537 string[] sts = Directory.GetFiles(st);
538 for (int i = 0; i < sts.Length; i++)
539 {
540 string fs = System.IO.File.ReadAllText(sts[i]);
541 Function f = Function.Parse(fs);
542 if (!Functions.ContainsKey(f.Name))
543 Functions.Add(f.Name, f);
544 App.AddMenu(f.MenuPath, f);
546 }
547 }
549 public void Save()
550 {
551 string st = Application.StartupPath;
552
553 if (!Directory.Exists(st + "/Functions"))
554 {
555 Directory.CreateDirectory(st + "/Functions");
556 }
557 foreach (Function f in Functions.Values)
558 {
559 System.IO.File.WriteAllText(st + "/Functions/" + f.Name + ".func", f.Serialize());
560 }
561 }
562 }
563
564}
System.Windows.Forms.Application Application
Definition ImageView.cs:14
static void AddMenu(string menu)
Definition App.cs:207
static void AddContextMenu(string menu)
Definition App.cs:215
static bool newTab
Definition BioConsole.cs:23
static bool useBioformats
Definition BioConsole.cs:21
static bool onTab
Definition BioConsole.cs:20
FunctionForm(Function func)
static InputSimulator input
FunctionForm(Function func, string name)
static void Initialize()
static Dictionary< string, Function > Functions
override string ToString()
static InputSimulator input
static Function Parse(string s)
object PerformFunction(bool imagej)
FunctionType FuncType
void Save()
It saves all the functions in the Functions dictionary to a file.
VirtualKeyCode Key
VirtualKeyCode Modifier
ButtonState State
ImageView control for image stacks, pyramidal and whole-slide-images.
static BioImage SelectedImage
Definition ImageView.cs:306
static void RunString(string st)
Definition Scripting.cs:417