* Add the analysis window.
[master-thesis.git] / Parasitemia / Parasitemia / GUI / Analysis.fs
diff --git a/Parasitemia/Parasitemia/GUI/Analysis.fs b/Parasitemia/Parasitemia/GUI/Analysis.fs
new file mode 100644 (file)
index 0000000..acb9ce2
--- /dev/null
@@ -0,0 +1,75 @@
+module Parasitemia.GUI.Analysis
+
+open System
+open System.Windows
+open System.Windows.Media
+open System.Windows.Markup
+open System.Windows.Shapes
+open System.Windows.Controls
+
+open Config
+
+let showWindow (parent: Window) (state: State.State) (defaultConfig: Config) : bool =
+    let window = Views.AnalysisWindow()
+    window.Root.Owner <- parent
+    window.Root.Left <- parent.Left + parent.ActualWidth / 2. - window.Root.Width / 2.
+    window.Root.Top <- parent.Top + parent.ActualHeight / 2. - window.Root.Height / 2.
+
+    let ctrl (name: string): 'a =
+        window.Root.FindName(name) :?> 'a
+
+    let butClose: Button = ctrl "butClose"
+    let butStart: Button = ctrl "butStart"
+
+    let progressBar: ProgressBar = ctrl "progress"
+    let textLog: TextBlock = ctrl "textLog"
+    let scrollLog: ScrollViewer = ctrl "scrollLog"
+
+    Utils.log <- (fun mess -> window.Root.Dispatcher.Invoke(fun () ->
+        textLog.Inlines.Add(Documents.Run(mess))
+        textLog.Inlines.Add(Documents.LineBreak())
+        scrollLog.ScrollToBottom()))
+
+    let monitor = Object()
+    let mutable analysisPerformed = false
+    let mutable analysisCancelled = false
+
+    butClose.Click.AddHandler(fun obj args -> window.Root.Close())
+
+    butStart.Click.AddHandler(fun obj args ->
+        butStart.IsEnabled <- false
+        butClose.Content <- "Abort"
+        async {
+            let results =
+                ImageAnalysis.doMultipleAnalysis
+                    (state.SourceImages |> Seq.map (fun srcImg -> string srcImg.num, srcImg.img) |> Seq.toList)
+                    defaultConfig
+                    (Some (fun progress -> window.Root.Dispatcher.Invoke(fun () -> progressBar.Value <- float progress)))
+
+            lock monitor (
+                fun() ->
+                    if not analysisCancelled
+                    then
+                        for id, rbcRadius, cells in results do
+                            state.SetResult (int id) rbcRadius cells
+
+                        window.Root.Dispatcher.Invoke(fun () ->
+                            butStart.IsEnabled <- false
+                            butClose.Content <- "Close")
+
+                        Utils.log "Analysis terminated successfully"
+                        analysisPerformed <- true)
+        } |> Async.Start)
+
+    window.Root.ShowDialog() |> ignore
+
+    lock monitor (fun () ->
+        if not analysisPerformed
+            then
+                analysisCancelled <- true
+        analysisPerformed)
+
+
+    (*let results = ImageAnalysis.doMultipleAnalysis (state.SourceImages |> Seq.map (fun srcImg -> string srcImg.num, srcImg.img) |> Seq.toList) defaultConfig
+    for id, rbcRadius, cells in results do
+        state.SetResult (int id) rbcRadius cells*)
\ No newline at end of file