module ParasitemiaUI.PPICalculator open System open System.Windows open ParasitemiaUIControls open ParasitemiaCore.Types open ParasitemiaCore.UnitsOfMeasure open Types let showWindow (parent : Window) : int option = let win = PPICalculatorWindow () win.Owner <- parent win.Left <- parent.Left + parent.ActualWidth / 2. - win.Width / 2. win.Top <- parent.Top + parent.ActualHeight / 2. - win.Height / 2. for size in Utils.sensorSizes do win.cmbSensorSize.Items.Add (size) |> ignore win.cmbSensorSize.SelectedIndex <- 0 let resolution (w_p : float) (w_mm : float) (zoom : float) : float = w_p * zoom / mmToInch w_mm let updateCurrentResolution () = let { w = w; h = h } = win.cmbSensorSize.SelectedValue :?> SensorSize let ratio = h / w let parseDouble (txt : string) (errorMess : string) = match Double.TryParse (txt) with true, value -> Success value | _ -> Fail errorMess match (result { let! sensorResolution = parseDouble win.txtSensorResolution.Text "The sensor resolution is not valid" let! zoom = parseDouble win.txtZoom.Text "The zoom is not valid" let wPixel = 1. * sqrt (sensorResolution * 1e6 / ratio) return! Success (float <| resolution wPixel w zoom) }) with | Success res -> win.txtImageResolution.Text <- int (res / 1000.) * 1000 |> string | Fail mess -> win.txtImageResolution.Text <- mess win.butCancel.Click.AddHandler (fun obj args -> win.DialogResult <- Nullable (false); win.Close ()) win.butOK.Click.AddHandler (fun obj args -> win.DialogResult <- Nullable (true); win.Close ()) win.cmbSensorSize.SelectionChanged.AddHandler (fun obj arg -> updateCurrentResolution ()) win.txtSensorResolution.TextChanged.AddHandler (fun obj arg -> updateCurrentResolution ()) win.txtZoom.TextChanged.AddHandler (fun obj arg -> updateCurrentResolution ()) let result = win.ShowDialog () if result.HasValue && result.Value then match Int32.TryParse win.txtImageResolution.Text with | true, res -> Some res | _ -> None else None