acb9ce243f198f3156cda1025dd966e410a4cfcd
[master-thesis.git] / Parasitemia / Parasitemia / GUI / Analysis.fs
1 module Parasitemia.GUI.Analysis
2
3 open System
4 open System.Windows
5 open System.Windows.Media
6 open System.Windows.Markup
7 open System.Windows.Shapes
8 open System.Windows.Controls
9
10 open Config
11
12 let showWindow (parent: Window) (state: State.State) (defaultConfig: Config) : bool =
13 let window = Views.AnalysisWindow()
14 window.Root.Owner <- parent
15 window.Root.Left <- parent.Left + parent.ActualWidth / 2. - window.Root.Width / 2.
16 window.Root.Top <- parent.Top + parent.ActualHeight / 2. - window.Root.Height / 2.
17
18 let ctrl (name: string): 'a =
19 window.Root.FindName(name) :?> 'a
20
21 let butClose: Button = ctrl "butClose"
22 let butStart: Button = ctrl "butStart"
23
24 let progressBar: ProgressBar = ctrl "progress"
25 let textLog: TextBlock = ctrl "textLog"
26 let scrollLog: ScrollViewer = ctrl "scrollLog"
27
28 Utils.log <- (fun mess -> window.Root.Dispatcher.Invoke(fun () ->
29 textLog.Inlines.Add(Documents.Run(mess))
30 textLog.Inlines.Add(Documents.LineBreak())
31 scrollLog.ScrollToBottom()))
32
33 let monitor = Object()
34 let mutable analysisPerformed = false
35 let mutable analysisCancelled = false
36
37 butClose.Click.AddHandler(fun obj args -> window.Root.Close())
38
39 butStart.Click.AddHandler(fun obj args ->
40 butStart.IsEnabled <- false
41 butClose.Content <- "Abort"
42 async {
43 let results =
44 ImageAnalysis.doMultipleAnalysis
45 (state.SourceImages |> Seq.map (fun srcImg -> string srcImg.num, srcImg.img) |> Seq.toList)
46 defaultConfig
47 (Some (fun progress -> window.Root.Dispatcher.Invoke(fun () -> progressBar.Value <- float progress)))
48
49 lock monitor (
50 fun() ->
51 if not analysisCancelled
52 then
53 for id, rbcRadius, cells in results do
54 state.SetResult (int id) rbcRadius cells
55
56 window.Root.Dispatcher.Invoke(fun () ->
57 butStart.IsEnabled <- false
58 butClose.Content <- "Close")
59
60 Utils.log "Analysis terminated successfully"
61 analysisPerformed <- true)
62 } |> Async.Start)
63
64 window.Root.ShowDialog() |> ignore
65
66 lock monitor (fun () ->
67 if not analysisPerformed
68 then
69 analysisCancelled <- true
70 analysisPerformed)
71
72
73 (*let results = ImageAnalysis.doMultipleAnalysis (state.SourceImages |> Seq.map (fun srcImg -> string srcImg.num, srcImg.img) |> Seq.toList) defaultConfig
74 for id, rbcRadius, cells in results do
75 state.SetResult (int id) rbcRadius cells*)