1
module ParasitemiaUI.Analysis
7 open System.Windows.Media
8 open System.Windows.Markup
9 open System.Windows.Shapes
10 open System.Windows.Controls
11 open System.Diagnostics
12 open Microsoft.Win32 // For the common dialogs.
16 open ParasitemiaCore.UnitsOfMeasure
17 open ParasitemiaCore.Config
21 let showWindow (parent
: Window) (state
: State.State) : bool =
22 let win = Views.AnalysisWindow()
23 win.Root.Owner <- parent
24 win.Root.Left <- parent
.Left + parent
.ActualWidth / 2. - win.Root.Width / 2.
25 win.Root.Top <- parent
.Top + parent
.ActualHeight / 2. - win.Root.Height / 2.
28 { new Logger.IListener with
29 member this
.NewEntry severity mess
=
30 win.Root.Dispatcher.Invoke(fun () ->
31 win.textLog
.Inlines.Add(Documents.Run(mess
))
32 win.textLog
.Inlines.Add(Documents.LineBreak())
33 win.scrollLog
.ScrollToBottom()) }
35 Logger.Log.AddListener(logListener)
39 let parseAndValidatePPI (input
: string) : float option =
40 match Double.TryParse(input
) with
41 | true, value
when value
>= minPPI && value
<= maxPPI -> Some value
44 let monitor = Object()
45 let mutable atLeastOneAnalysisPerformed = false
46 let mutable analysisPerformed = false
47 let mutable analysisCancelled = false
49 let updateSourceImages () =
50 win.stackSourceImagesSelection
.Children.Clear()
51 let width = int win.stackSourceImagesSelection
.ActualWidth
52 for srcImg
in state
.SourceImages do
53 let imageSourceSelection = Views.ImageSourceSelection(Tag = srcImg
, Margin = Thickness(3.))
54 imageSourceSelection.Tag <- srcImg
56 imageSourceSelection.txtImageNumber
.Text <- srcImg
.num
.ToString()
57 let height = srcImg
.img
.Height * width / srcImg
.img
.Width
58 imageSourceSelection.imagePreview
.Source <- BitmapSourceConvert.ToBitmapSource(srcImg
.img
.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic))
59 imageSourceSelection.chkSelection
.IsChecked <- Nullable<bool>(srcImg
.dateLastAnalysis
.Ticks = 0L)
60 imageSourceSelection.lblDateLastAnalysis
.Content <- if srcImg
.dateLastAnalysis
.Ticks = 0L then "<Never>" else srcImg.dateLastAnalysis
.ToString()
62 imageSourceSelection.txtResolution
.Text <- if srcImg.dateLastAnalysis
.Ticks = 0L then "" else srcImg.config
.Parameters.resolution
.ToString()
63 imageSourceSelection.menuZoom50X
.Click.AddHandler(fun obj args
-> imageSourceSelection.txtResolution
.Text <- "230000")
64 imageSourceSelection.menuZoom100X
.Click.AddHandler(fun obj args
-> imageSourceSelection.txtResolution
.Text <- "460000")
66 imageSourceSelection.butDPICalculator
.Click.AddHandler(fun obj args
->
67 match DPICalculator.showWindow win.Root with
68 | Some resolution
-> imageSourceSelection.txtResolution
.Text <- resolution
.ToString()
71 imageSourceSelection.txtResolution
.PreviewTextInput.AddHandler(fun obj args
->
72 let text = imageSourceSelection.txtResolution
.Text + args
.Text
73 args
.Handled <- match parseAndValidatePPI text with Some _ -> false | None -> true)
75 imageSourceSelection.imagePreview
.MouseLeftButtonDown.AddHandler(fun obj args
->
76 let checkbox = imageSourceSelection.chkSelection
77 checkbox.IsChecked <- Nullable<bool>(not
(checkbox.IsChecked.HasValue && checkbox.IsChecked.Value)))
79 win.stackSourceImagesSelection
.Children.Add(imageSourceSelection) |> ignore
81 // Get the new parameters for each image. If an error occurs then 'None' is returned and a message box is displayed.
82 // The boolean is 'true' if the image is selected (checked).
83 let getInputImagesParameters () : (SourceImage * bool * Parameters) list
option =
84 let sourceImagesControls = win.stackSourceImagesSelection
.Children |> Seq.cast
<Views.ImageSourceSelection>
85 let parameters = seq
{
86 for srcImgCtrl
in sourceImagesControls do
87 let srcImg = srcImgCtrl
.Tag :?> SourceImage
88 let isChecked = srcImgCtrl
.chkSelection
.IsChecked
89 match parseAndValidatePPI srcImgCtrl
.txtResolution
.Text with
91 yield
Some (srcImg, isChecked.HasValue && isChecked.Value, { srcImg.config
.Parameters with resolution
= resolution
* 1.<ppi
> })
93 MessageBox.Show(sprintf
"No resolution defined for the image number %d" srcImg.num
, "No resolution defined", MessageBoxButton.OK, MessageBoxImage.Information) |> ignore
94 yield
None } |> Seq.takeWhile
(fun e
-> e
.IsSome) |> Seq.map
(fun e
-> e
.Value) |> List.ofSeq
95 if parameters.Count() <> sourceImagesControls.Count()
99 win.butClose
.Click.AddHandler(fun obj args
-> win.Root.Close())
101 win.butStart
.Click.AddHandler(fun obj args
->
102 match getInputImagesParameters () with
103 | Some imagesParameters
->
104 let imagesToProcess = [
105 for srcImg, selected, parameters in imagesParameters
do
106 srcImg.config
.Parameters <- parameters // Save parameters.
108 then yield
srcImg.num
.ToString(), srcImg.config
, srcImg.img
]
110 if imagesToProcess.IsEmpty
112 MessageBox.Show("No image selected", "Cannot start analysis", MessageBoxButton.OK, MessageBoxImage.Information) |> ignore
114 win.stackSourceImagesSelection
.IsEnabled <- false
115 analysisPerformed <- false
116 win.butStart
.IsEnabled <- false
117 win.butClose
.Content <- "Abort"
121 ParasitemiaCore.Analysis.doMultipleAnalysis
123 (Some (fun progress
-> win.Root.Dispatcher.Invoke(fun () -> win.progress
.Value <- float progress); not
analysisCancelled))
127 match maybeResults with
129 for id
, cells
in results
do
130 state
.SetResult (int id) cells
132 win.Root.Dispatcher.Invoke(fun () ->
133 win.stackSourceImagesSelection
.IsEnabled <- true
134 win.butStart
.IsEnabled <- true
135 win.butClose
.Content <- "Close"
136 updateSourceImages ())
138 Logger.Log.User("All analyses terminated successfully")
139 atLeastOneAnalysisPerformed <- true
140 analysisPerformed <- true
145 win.Root.Loaded.AddHandler(fun obj args
-> updateSourceImages ())
147 win.Root.ShowDialog() |> ignore
149 Logger.Log.RmListener(logListener)
151 lock
monitor (fun () ->
152 if not
analysisPerformed
154 analysisCancelled <- true
155 atLeastOneAnalysisPerformed)