7d72e729cfb2e96978493ab643c717f17b87e098
1
module Parasitemia.GUI.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.
20 let showWindow (parent
: Window) (state
: State.State) : bool =
21 let window = Views.AnalysisWindow()
22 window.Root.Owner <- parent
23 window.Root.Left <- parent
.Left + parent
.ActualWidth / 2. - window.Root.Width / 2.
24 window.Root.Top <- parent
.Top + parent
.ActualHeight / 2. - window.Root.Height / 2.
26 let ctrl (name
: string): 'a = window.Root.FindName(name) :?> 'a
28 let butClose: Button = ctrl "butClose"
29 let butStart: Button = ctrl "butStart"
31 let stackImagesSourceSelection: StackPanel = ctrl "stackImagesSourceSelection"
32 let progressBar: ProgressBar = ctrl "progress"
33 let textLog: TextBlock = ctrl "textLog"
34 let scrollLog: ScrollViewer = ctrl "scrollLog"
36 Utils.log
<- (fun mess
-> window.Root.Dispatcher.Invoke(fun () ->
37 textLog.Inlines.Add(Documents.Run(mess
))
38 textLog.Inlines.Add(Documents.LineBreak())
39 scrollLog.ScrollToBottom()))
43 let parseAndValidatePPI (input
: string) : float option =
45 if Double.TryParse(input
, res) && !res >= minPPI && !res <= maxPPI
49 let monitor = Object()
50 let mutable atLeastOneAnalysisPerformed = false
51 let mutable analysisPerformed = false
52 let mutable analysisCancelled = false
54 let updateSourceImages () =
55 stackImagesSourceSelection.Children.Clear()
56 let width = int stackImagesSourceSelection.ActualWidth
57 for srcImg
in state
.SourceImages do
58 let imageSourceSelection = Views.ImageSourceSelection(Tag = srcImg
, Margin = Thickness(3.))
60 let updateResolution () =
61 match parseAndValidatePPI imageSourceSelection.txtResolution
.Text with
62 | Some resolution
-> srcImg
.config
.Parameters <- { srcImg
.config
.Parameters with resolution
= resolution
* 1.<ppi
> }
65 imageSourceSelection.txtImageNumber
.Text <- srcImg
.num
.ToString()
66 let height = srcImg
.img
.Height * width / srcImg
.img
.Width
67 imageSourceSelection.imagePreview
.Source <- BitmapSourceConvert.ToBitmapSource(srcImg
.img
.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic))
68 imageSourceSelection.chkSelection
.IsChecked <- Nullable<bool>(srcImg
.dateLastAnalysis
.Ticks = 0L)
69 imageSourceSelection.lblDateLastAnalysis
.Content <- if srcImg
.dateLastAnalysis
.Ticks = 0L then "<Never>" else srcImg.dateLastAnalysis
.ToString()
71 imageSourceSelection.txtResolution
.Text <- srcImg.config
.Parameters.resolution
.ToString()
72 imageSourceSelection.menuZoom50X
.Click.AddHandler(fun obj args
-> imageSourceSelection.txtResolution
.Text <- "230000"; updateResolution ())
73 imageSourceSelection.menuZoom100X
.Click.AddHandler(fun obj args
-> imageSourceSelection.txtResolution
.Text <- "460000"; updateResolution ())
75 imageSourceSelection.txtResolution
.PreviewTextInput.AddHandler(fun obj args
->
76 let text = imageSourceSelection.txtResolution
.Text + args
.Text
77 args
.Handled <- match parseAndValidatePPI text with Some _ -> false | None -> true)
79 imageSourceSelection.imagePreview
.MouseLeftButtonDown.AddHandler(fun obj args
->
80 let checkbox = imageSourceSelection.chkSelection
81 checkbox.IsChecked <- Nullable<bool>(not
(checkbox.IsChecked.HasValue && checkbox.IsChecked.Value)))
83 imageSourceSelection.txtResolution
.LostFocus.AddHandler(fun obj args
-> updateResolution ())
85 stackImagesSourceSelection.Children.Add(imageSourceSelection) |> ignore
87 butClose.Click.AddHandler(fun obj args
-> window.Root.Close())
89 butStart.Click.AddHandler(fun obj args
->
90 let imagesToProcess = [
91 for imageSelection
in stackImagesSourceSelection.Children |> Seq.cast
<Views.ImageSourceSelection> do
92 let chk = imageSelection
.chkSelection
.IsChecked
93 if chk.HasValue && chk.Value
95 let srcImg = imageSelection
.Tag :?> SourceImage
96 yield
srcImg.num
.ToString(), srcImg.config
, srcImg.img
]
98 if imagesToProcess.IsEmpty
100 MessageBox.Show("No image selected", "Cannot start analysis", MessageBoxButton.OK, MessageBoxImage.Information) |> ignore
102 analysisPerformed <- false
103 butStart.IsEnabled <- false
104 butClose.Content <- "Abort"
107 ImageAnalysis.doMultipleAnalysis
109 (Some (fun progress
-> window.Root.Dispatcher.Invoke(fun () -> progressBar.Value <- float progress)))
113 if not
analysisCancelled
115 for id
, cells
in results do
116 state
.SetResult (int id) cells
118 window.Root.Dispatcher.Invoke(fun () ->
119 butStart.IsEnabled <- true
120 butClose.Content <- "Close"
121 updateSourceImages ())
123 Utils.log
"All analyses terminated successfully"
124 atLeastOneAnalysisPerformed <- true
125 analysisPerformed <- true)
128 window.Root.Loaded.AddHandler(fun obj args
-> updateSourceImages ())
130 window.Root.ShowDialog() |> ignore
132 lock
monitor (fun () ->
133 if not
analysisPerformed
135 analysisCancelled <- true
136 atLeastOneAnalysisPerformed)