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 window = Views.AnalysisWindow()
23 window.Root.Owner <- parent
24 window.Root.Left <- parent
.Left + parent
.ActualWidth / 2. - window.Root.Width / 2.
25 window.Root.Top <- parent
.Top + parent
.ActualHeight / 2. - window.Root.Height / 2.
27 let ctrl (name
: string): 'a = window.Root.FindName(name) :?> 'a
29 let butClose: Button = ctrl "butClose"
30 let butStart: Button = ctrl "butStart"
32 let stackImagesSourceSelection: StackPanel = ctrl "stackImagesSourceSelection"
33 let progressBar: ProgressBar = ctrl "progress"
34 let textLog: TextBlock = ctrl "textLog"
35 let scrollLog: ScrollViewer = ctrl "scrollLog"
38 { new Logger.IListener with
39 member this
.NewEntry severity mess
=
40 window.Root.Dispatcher.Invoke(fun () ->
41 textLog.Inlines.Add(Documents.Run(mess
))
42 textLog.Inlines.Add(Documents.LineBreak())
43 scrollLog.ScrollToBottom()) }
45 Logger.Log.AddListener(logListener)
49 let parseAndValidatePPI (input
: string) : float option =
51 if Double.TryParse(input
, res) && !res >= minPPI && !res <= maxPPI
55 let monitor = Object()
56 let mutable atLeastOneAnalysisPerformed = false
57 let mutable analysisPerformed = false
58 let mutable analysisCancelled = false
60 let updateSourceImages () =
61 stackImagesSourceSelection.Children.Clear()
62 let width = int stackImagesSourceSelection.ActualWidth
63 for srcImg
in state
.SourceImages do
64 let imageSourceSelection = Views.ImageSourceSelection(Tag = srcImg
, Margin = Thickness(3.))
66 let updateResolution () =
67 match parseAndValidatePPI imageSourceSelection.txtResolution
.Text with
68 | Some resolution
-> srcImg
.config
.Parameters <- { srcImg
.config
.Parameters with resolution
= resolution
* 1.<ppi
> }
71 imageSourceSelection.txtImageNumber
.Text <- srcImg
.num
.ToString()
72 let height = srcImg
.img
.Height * width / srcImg
.img
.Width
73 imageSourceSelection.imagePreview
.Source <- BitmapSourceConvert.ToBitmapSource(srcImg
.img
.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic))
74 imageSourceSelection.chkSelection
.IsChecked <- Nullable<bool>(srcImg
.dateLastAnalysis
.Ticks = 0L)
75 imageSourceSelection.lblDateLastAnalysis
.Content <- if srcImg
.dateLastAnalysis
.Ticks = 0L then "<Never>" else srcImg.dateLastAnalysis
.ToString()
77 imageSourceSelection.txtResolution
.Text <- srcImg.config
.Parameters.resolution
.ToString()
78 imageSourceSelection.menuZoom50X
.Click.AddHandler(fun obj args
-> imageSourceSelection.txtResolution
.Text <- "230000"; updateResolution ())
79 imageSourceSelection.menuZoom100X
.Click.AddHandler(fun obj args
-> imageSourceSelection.txtResolution
.Text <- "460000"; updateResolution ())
81 imageSourceSelection.txtResolution
.PreviewTextInput.AddHandler(fun obj args
->
82 let text = imageSourceSelection.txtResolution
.Text + args
.Text
83 args
.Handled <- match parseAndValidatePPI text with Some _ -> false | None -> true)
85 imageSourceSelection.imagePreview
.MouseLeftButtonDown.AddHandler(fun obj args
->
86 let checkbox = imageSourceSelection.chkSelection
87 checkbox.IsChecked <- Nullable<bool>(not
(checkbox.IsChecked.HasValue && checkbox.IsChecked.Value)))
89 imageSourceSelection.txtResolution
.LostFocus.AddHandler(fun obj args
-> updateResolution ())
91 stackImagesSourceSelection.Children.Add(imageSourceSelection) |> ignore
93 butClose.Click.AddHandler(fun obj args
-> window.Root.Close())
95 butStart.Click.AddHandler(fun obj args
->
96 let imagesToProcess = [
97 for imageSelection
in stackImagesSourceSelection.Children |> Seq.cast
<Views.ImageSourceSelection> do
98 let chk = imageSelection
.chkSelection
.IsChecked
99 if chk.HasValue && chk.Value
101 let srcImg = imageSelection
.Tag :?> SourceImage
102 yield
srcImg.num
.ToString(), srcImg.config
, srcImg.img
]
104 if imagesToProcess.IsEmpty
106 MessageBox.Show("No image selected", "Cannot start analysis", MessageBoxButton.OK, MessageBoxImage.Information) |> ignore
108 analysisPerformed <- false
109 butStart.IsEnabled <- false
110 butClose.Content <- "Abort"
113 ParasitemiaCore.Analysis.doMultipleAnalysis
115 (Some (fun progress
-> window.Root.Dispatcher.Invoke(fun () -> progressBar.Value <- float progress)))
119 if not
analysisCancelled
121 for id
, cells
in results do
122 state
.SetResult (int id) cells
124 window.Root.Dispatcher.Invoke(fun () ->
125 butStart.IsEnabled <- true
126 butClose.Content <- "Close"
127 updateSourceImages ())
129 Logger.Log.User("All analyses terminated successfully")
130 atLeastOneAnalysisPerformed <- true
131 analysisPerformed <- true)
134 window.Root.Loaded.AddHandler(fun obj args
-> updateSourceImages ())
136 window.Root.ShowDialog() |> ignore
138 Logger.Log.RmListener(logListener)
140 lock
monitor (fun () ->
141 if not
analysisPerformed
143 analysisCancelled <- true
144 atLeastOneAnalysisPerformed)