Save predefined PPI and sensor sizes in JSON files.
[master-thesis.git] / Parasitemia / ParasitemiaUI / DPICalculator.fs
1 module ParasitemiaUI.PPICalculator
2
3 open System
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.Diagnostics
10
11 open ParasitemiaCore.Types
12 open ParasitemiaCore.UnitsOfMeasure
13
14 open Types
15
16 let showWindow (parent: Window) : int option =
17 let win = Views.PPICalculatorWindow()
18 win.Root.Owner <- parent
19 win.Root.Left <- parent.Left + parent.ActualWidth / 2. - win.Root.Width / 2.
20 win.Root.Top <- parent.Top + parent.ActualHeight / 2. - win.Root.Height / 2.
21
22 for size in Utils.sensorSizes do
23 win.cmbSensorSize.Items.Add(size) |> ignore
24 win.cmbSensorSize.SelectedIndex <- 0
25
26 let resolution (w_p: float<px>) (w_mm: float<mm>) (zoom: float) : float<ppi> =
27 w_p * zoom / mmToInch w_mm
28
29 let updateCurrentResolution () =
30 let { w = w; h = h } = win.cmbSensorSize.SelectedValue :?> SensorSize
31 let ratio = h / w
32
33 let parseDouble txt errorMess = match Double.TryParse(txt) with true, value -> Success value | _ -> Fail errorMess
34
35 match result
36 { let! sensorResolution = parseDouble win.txtSensorResolution.Text "The sensor resolution is not valid"
37 let! zoom = parseDouble win.txtZoom.Text "The zoom is not valid"
38 let wPixel = 1.<px> * sqrt (sensorResolution * 1e6 / ratio)
39 return! Success (float <| resolution wPixel w zoom) } with
40 | Success res -> win.txtImageResolution.Text <- (int (res / 1000.) * 1000).ToString()
41 | Fail mess -> win.txtImageResolution.Text <- mess
42
43 win.butCancel.Click.AddHandler(fun obj args -> win.Root.DialogResult <- Nullable<bool>(false); win.Root.Close())
44 win.butOK.Click.AddHandler(fun obj args -> win.Root.DialogResult <- Nullable<bool>(true); win.Root.Close())
45
46 win.cmbSensorSize.SelectionChanged.AddHandler(fun obj arg -> updateCurrentResolution ())
47 win.txtSensorResolution.TextChanged.AddHandler(fun obj arg -> updateCurrentResolution ())
48 win.txtZoom.TextChanged.AddHandler(fun obj arg -> updateCurrentResolution ())
49
50 let result = win.Root.ShowDialog()
51 if result.HasValue && result.Value
52 then
53 match Int32.TryParse win.txtImageResolution.Text with
54 | true, res -> Some res
55 | _ -> None
56 else
57 None
58