1
module ParasitemiaUI.PPICalculator
5 open System.Windows.Media
6 open System.Windows.Markup
7 open System.Windows.Shapes
8 open System.Windows.Controls
9 open System.Diagnostics
11 open ParasitemiaCore.Types
12 open ParasitemiaCore.UnitsOfMeasure
16 let showWindow (parent
: Window) : int option =
17 let win = Views.PPICalculatorWindow ()
19 win.Left <- parent
.Left + parent
.ActualWidth / 2. - win.Width / 2.
20 win.Top <- parent
.Top + parent
.ActualHeight / 2. - win.Height / 2.
22 for size
in Utils.sensorSizes
do
23 win.cmbSensorSize
.Items.Add (size
) |> ignore
24 win.cmbSensorSize
.SelectedIndex <- 0
26 let resolution (w_p
: float<px
>) (w_mm
: float<mm
>) (zoom
: float) : float<ppi
> =
27 w_p
* zoom
/ mmToInch w_mm
29 let updateCurrentResolution () =
30 let { w
= w
; h
= h
} = win.cmbSensorSize
.SelectedValue :?> SensorSize
33 let parseDouble txt
errorMess = match Double.TryParse (txt
) with true, value
-> Success value | _ -> Fail errorMess
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)
42 | Success res
-> win.txtImageResolution
.Text <- int (res
/ 1000.) * 1000 |> string
43 | Fail mess
-> win.txtImageResolution
.Text <- mess
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 ())
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 ())
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