* Add logos CHUV and HES-SO.
[master-thesis.git] / Parasitemia / ParasitemiaUI / GUI.fs
index efe01c3..a71aa96 100644 (file)
@@ -60,7 +60,10 @@ let run (defaultConfig: Config) (fileToOpen: string option) =
     let scrollRBC: ScrollViewer = ctrl "scrollRBC"
     let stackRBC: StackPanel = ctrl "stackRBC"
 
+    let imgLogos: Border = ctrl "imgLogos"
+
     // Initializations.
+    let canvasCurrentImageColor = canvasCurrentImage.Background
     menuHightlightRBC.IsChecked <- displayHealthy
 
     // Utils.
@@ -126,10 +129,12 @@ let run (defaultConfig: Config) (fileToOpen: string option) =
             sprintf "%.1f %% (%d / %d)" percent nb nbTotal
 
     let updateCurrentImageInformation () =
+        txtImageInformation1.Inlines.Clear()
+        txtImageInformation2.Inlines.Clear()
+
         match state.CurrentImage with
         | Some srcImg ->
             let parasitemiaStr = percentText (state.ImageParasitemia srcImg)
-            txtImageInformation1.Inlines.Clear()
             txtImageInformation1.Inlines.Add(Documents.Run("Parasitemia: ", FontWeight = FontWeights.Bold))
             txtImageInformation1.Inlines.Add(parasitemiaStr)
             txtImageInformation1.Inlines.Add(Documents.LineBreak())
@@ -137,7 +142,6 @@ let run (defaultConfig: Config) (fileToOpen: string option) =
             txtImageInformation1.Inlines.Add(Documents.Run("Last analysis: ", FontWeight = FontWeights.Bold))
             txtImageInformation1.Inlines.Add(Documents.Run(if srcImg.dateLastAnalysis.Ticks = 0L then "<Never>" else srcImg.dateLastAnalysis.ToLocalTime().ToString()))
 
-            txtImageInformation2.Inlines.Clear()
             let alteredStr = percentText (state.ImageNbAltered srcImg)
             txtImageInformation2.Inlines.Add(Documents.Run("Number of erytrocytes manually altered: ", FontWeight = FontWeights.Bold))
             txtImageInformation2.Inlines.Add(Documents.Run(alteredStr))
@@ -310,6 +314,8 @@ let run (defaultConfig: Config) (fileToOpen: string option) =
     let updateCurrentImage () =
         match state.CurrentImage with
         | Some srcImg ->
+            imgLogos.Visibility <- Visibility.Collapsed
+
             // Highlight the preview.
             stackPreviews.Children
             |> Seq.cast<Views.ImageSourcePreview>
@@ -321,11 +327,13 @@ let run (defaultConfig: Config) (fileToOpen: string option) =
 
             updateRBCFramesCurrent ()
             updateRBCFramesPreview ()
-            updateCurrentImageInformation ()
         | None ->
+            imgLogos.Visibility <- Visibility.Visible
             stackRBC.Children.Clear()
             canvasCurrentImage.Children.Clear()
-            canvasCurrentImage.Background <- Brushes.Black
+            canvasCurrentImage.Background <- canvasCurrentImageColor
+
+        updateCurrentImageInformation ()
 
     let setCurrentImage (srcImg: SourceImage) =
         if state.CurrentImage.IsNone || state.CurrentImage.Value <> srcImg
@@ -344,6 +352,10 @@ let run (defaultConfig: Config) (fileToOpen: string option) =
             if currentRemoved
             then
                 updateCurrentImage()
+
+            updateGlobalParasitemia()
+
+            // Update image numbers.
             stackPreviews.Children |> Seq.cast<Views.ImageSourcePreview> |> Seq.iter (fun imgPreview -> imgPreview.txtImageNumber.Text <- (imgPreview.Tag :?> SourceImage).num.ToString()))
 
         imgCtrl.Tag <- srcImg
@@ -403,26 +415,24 @@ let run (defaultConfig: Config) (fileToOpen: string option) =
             state.FilePath <- previousFilePath
             MessageBox.Show(sprintf "The document cannot be loaded from '%s'" state.FilePath, "Error loading the document", MessageBoxButton.OK, MessageBoxImage.Error) |> ignore
 
-    txtPatient.TextChanged.AddHandler(fun obj args -> state.PatientID <- txtPatient.Text)
-
-    menuExit.Click.AddHandler(fun obj args ->
-        askSaveCurrent ()
-        mainWindow.Root.Close())
-
-    menuSaveFile.Click.AddHandler(fun obj args -> saveCurrentDocument ())
-    menuSaveAsFile.Click.AddHandler(fun obj args -> saveCurrentDocumentAsNewFile ())
-
-    menuLoadFile.Click.AddHandler(fun obj args ->
-        // TODO: if current state not saved and not empty, ask to save it.
+    let askLoadFile () =
         let dialog = OpenFileDialog(Filter = PiaZ.filter)
         let res = dialog.ShowDialog()
         if res.HasValue && res.Value
-        then loadFile dialog.FileName)
+        then loadFile dialog.FileName
 
-    menuNewFile.Click.AddHandler(fun obj args ->
+    let newFile () =
         askSaveCurrent ()
         state.Reset()
-        updateGUI())
+        updateGUI()
+
+    txtPatient.TextChanged.AddHandler(fun obj args -> state.PatientID <- txtPatient.Text)
+
+    menuExit.Click.AddHandler(fun obj args -> mainWindow.Root.Close())
+    menuSaveFile.Click.AddHandler(fun obj args -> saveCurrentDocument ())
+    menuSaveAsFile.Click.AddHandler(fun obj args -> saveCurrentDocumentAsNewFile ())
+    menuLoadFile.Click.AddHandler(fun obj args -> askLoadFile ())
+    menuNewFile.Click.AddHandler(fun obj args -> newFile ())
 
     menuAddSourceImage.Click.AddHandler(fun obj args ->
         let dialog = OpenFileDialog(Filter = "Image Files|*.png;*.jpg;*.tif;*.tiff", Multiselect = true)
@@ -461,6 +471,8 @@ let run (defaultConfig: Config) (fileToOpen: string option) =
 
     menuAbout.Click.AddHandler(fun obj args -> About.showWindow mainWindow.Root)
 
+    mainWindow.Root.Closing.AddHandler(fun obj args -> askSaveCurrent ())
+
     // Zoom on the current image.
     let adjustCurrentImageBorders (deltaX: float) (deltaY: float) =
         borderCurrentImage.BorderThickness <-
@@ -544,11 +556,30 @@ let run (defaultConfig: Config) (fileToOpen: string option) =
             args.Handled <- true)
 
     // Shortcuts.
+    // Save.
     mainWindow.Root.InputBindings.Add(
         Input.KeyBinding(
             FSharp.ViewModule.FunCommand((fun obj -> saveCurrentDocument ()), (fun obj -> true)),
             Input.KeyGesture(Input.Key.S, Input.ModifierKeys.Control))) |> ignore
 
+    // Save as.
+    mainWindow.Root.InputBindings.Add(
+        Input.KeyBinding(
+            FSharp.ViewModule.FunCommand((fun obj -> saveCurrentDocumentAsNewFile ()), (fun obj -> true)),
+            Input.KeyGesture(Input.Key.S, Input.ModifierKeys.Control ||| Input.ModifierKeys.Shift))) |> ignore
+
+    // Open.
+    mainWindow.Root.InputBindings.Add(
+        Input.KeyBinding(
+            FSharp.ViewModule.FunCommand((fun obj -> askLoadFile ()), (fun obj -> true)),
+            Input.KeyGesture(Input.Key.O, Input.ModifierKeys.Control))) |> ignore
+
+    // New file.
+    mainWindow.Root.InputBindings.Add(
+        Input.KeyBinding(
+            FSharp.ViewModule.FunCommand((fun obj -> newFile ()), (fun obj -> true)),
+            Input.KeyGesture(Input.Key.N, Input.ModifierKeys.Control))) |> ignore
+
     // Viewport preview.
     scrollViewCurrentImage.ScrollChanged.AddHandler(fun obj args -> updateViewportPreview ())