Remove useless 'yield'
[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.Owner <- parent
19 win.Left <- parent.Left + parent.ActualWidth / 2. - win.Width / 2.
20 win.Top <- parent.Top + parent.ActualHeight / 2. - win.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
36 (result {
37 let! sensorResolution = parseDouble win.txtSensorResolution.Text "The sensor resolution is not valid"
38 let! zoom = parseDouble win.txtZoom.Text "The zoom is not valid"
39 let wPixel = 1.<px> * sqrt (sensorResolution * 1e6 / ratio)
40 return! Success (float <| resolution wPixel w zoom)
41 }) with
42 | Success res -> win.txtImageResolution.Text <- int (res / 1000.) * 1000 |> string
43 | Fail mess -> win.txtImageResolution.Text <- mess
44
45 win.butCancel.Click.AddHandler (fun obj args -> win.DialogResult <- Nullable<bool> (false); win.Close ())
46 win.butOK.Click.AddHandler (fun obj args -> win.DialogResult <- Nullable<bool> (true); win.Close ())
47
48 win.cmbSensorSize.SelectionChanged.AddHandler (fun obj arg -> updateCurrentResolution ())
49 win.txtSensorResolution.TextChanged.AddHandler (fun obj arg -> updateCurrentResolution ())
50 win.txtZoom.TextChanged.AddHandler (fun obj arg -> updateCurrentResolution ())
51
52 let result = win.ShowDialog ()
53 if result.HasValue && result.Value then
54 match Int32.TryParse win.txtImageResolution.Text with
55 | true, res -> Some res
56 | _ -> None
57 else
58 None
59