Add the panel to display image source previews.
[master-thesis.git] / Parasitemia / Parasitemia / GUI / GUI.fs
1 module Parasitemia.GUI
2
3 open System.IO
4 open System.Windows
5 open System.Windows.Media
6 open System.Windows.Markup
7 open System.Windows.Shapes
8 open System.Windows.Controls
9 open System.Drawing
10 open System.Diagnostics
11 open Microsoft.Win32 // For the common dialogs.
12
13 //open Emgu.CV
14 //open Emgu.CV.Structure
15 open Emgu.CV.WPF
16
17 open Config
18
19 let run (defaultConfig: Config) =
20 let app = new Application()
21 let mainWindow = Views.MainWindow()
22 let ctrl (name: string): 'a =
23 mainWindow.Root.FindName(name) :?> 'a
24
25 // Utils.log <- (fun m -> log mainWindow m)
26
27 let state = State.State()
28
29 let exit: MenuItem = ctrl "menuExit"
30 let saveFile: MenuItem = ctrl "menuSave"
31 let loadFile: MenuItem = ctrl "menuOpen"
32 let newFile: MenuItem = ctrl "menuNew"
33
34 let addSourceImage: MenuItem = ctrl "menuAddSourceImage"
35
36 let txtPatient: TextBox = ctrl "txtPatient"
37
38 let stackPreviews: StackPanel = ctrl "stackPreviews"
39
40 let butStartAnalysis: Button = ctrl "butStartAnalysis"
41
42 let synchronizeState () =
43 state.PatientID <- txtPatient.Text
44
45 let updatePreviews () =
46 stackPreviews.Children.Clear ()
47 state.ImagesSource |> Seq.iteri (fun i imgSrc ->
48 let imgCtrl = Views.ImageSourcePreview(Margin = Thickness(3.))
49 imgCtrl.lblImageNumber.Content <- i + 1
50 let width = 200
51 let height = imgSrc.img.Height * width / imgSrc.img.Width
52 imgCtrl.imagePreview.Source <- BitmapSourceConvert.ToBitmapSource(imgSrc.img.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic))
53 stackPreviews.Children.Add(imgCtrl) |> ignore )
54
55 let updateGUI () =
56 txtPatient.Text <- state.PatientID
57 updatePreviews ()
58
59 exit.Click.AddHandler(fun obj args -> mainWindow.Root.Close())
60 saveFile.Click.AddHandler(fun obj args ->
61 synchronizeState ()
62 if state.FilePath = ""
63 then
64 let dialog = SaveFileDialog(AddExtension = true, DefaultExt = Pia.extension, Filter = Pia.filter);
65 let res = dialog.ShowDialog()
66 if res.HasValue && res.Value
67 then
68 state.FilePath <- dialog.FileName
69 state.Save()
70 else
71 state.Save())
72
73 loadFile.Click.AddHandler(fun obj args ->
74 // TODO: if current state not saved and not empty, ask to save it.
75 let dialog = OpenFileDialog(Filter = Pia.filter)
76 let res = dialog.ShowDialog()
77 if res.HasValue && res.Value
78 then
79 state.FilePath <- dialog.FileName
80 state.Load()
81 updateGUI ())
82
83 newFile.Click.AddHandler(fun obj args ->
84 // TODO: if current state not saved and not empty, ask to save it.
85 state.Reset()
86 updateGUI())
87
88 addSourceImage.Click.AddHandler(fun obj args ->
89 let dialog = OpenFileDialog(Filter = "Image Files|*.png;*.jpg;*.tif;*.tiff")
90 let res = dialog.ShowDialog()
91 if res.HasValue && res.Value
92 then
93 state.AddSourceImage(dialog.FileName)
94 updatePreviews ())
95
96 butStartAnalysis.Click.AddHandler(fun obj args -> ())
97
98 (*let txtPatient: Controls.TextBox = ctrl "txtPatient"
99 txtPatient.TextChanged.AddHandler(fun obj args ->
100 state.PatientID <- txtPatient.Text)*)
101
102 (*saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
103 saveFileDialog1.FilterIndex = 2 ;
104 saveFileDialog1.RestoreDirectory = true ;*)
105
106 // display mainWindow img
107 mainWindow.Root.Show()
108 app.Run()