From: Greg Burri Date: Fri, 8 Jan 2016 20:08:00 +0000 (+0100) Subject: GUI (work in progress..) X-Git-Tag: 1.0.11~60 X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=0af5da4bffad8333c8e57851e067218c0e21d7bb;p=master-thesis.git GUI (work in progress..) --- diff --git a/Parasitemia/Parasitemia/GUI/GUI.fs b/Parasitemia/Parasitemia/GUI/GUI.fs index 155c1d1..6f11379 100644 --- a/Parasitemia/Parasitemia/GUI/GUI.fs +++ b/Parasitemia/Parasitemia/GUI/GUI.fs @@ -1,12 +1,12 @@ -module Parasitemia.GUI +module Parasitemia.GUI.Main open System.IO +open System.Linq open System.Windows open System.Windows.Media open System.Windows.Markup open System.Windows.Shapes open System.Windows.Controls -open System.Drawing open System.Diagnostics open Microsoft.Win32 // For the common dialogs. @@ -15,6 +15,7 @@ open Microsoft.Win32 // For the common dialogs. open Emgu.CV.WPF open Config +open Types let run (defaultConfig: Config) = let app = new Application() @@ -24,6 +25,8 @@ let run (defaultConfig: Config) = // Utils.log <- (fun m -> log mainWindow m) + let currentImageMargin = 0.1 // 10 %. + let state = State.State() let exit: MenuItem = ctrl "menuExit" @@ -32,25 +35,39 @@ let run (defaultConfig: Config) = let newFile: MenuItem = ctrl "menuNew" let addSourceImage: MenuItem = ctrl "menuAddSourceImage" - let txtPatient: TextBox = ctrl "txtPatient" - let stackPreviews: StackPanel = ctrl "stackPreviews" - let butStartAnalysis: Button = ctrl "butStartAnalysis" + let scrollViewCurrentImage: ScrollViewer = ctrl "scrollViewCurrentImage" + let borderCurrentImage: Border = ctrl "borderCurrentImage" + let canvasCurrentImage: Canvas = ctrl "canvasCurrentImage" + + // Initializations. + + // Operations. let synchronizeState () = state.PatientID <- txtPatient.Text + let setCurrentImage (srcImg: SourceImage) = + canvasCurrentImage.Height <- float srcImg.img.Height + canvasCurrentImage.Width <- float srcImg.img.Width + canvasCurrentImage.Background <- ImageBrush(BitmapSourceConvert.ToBitmapSource(srcImg.img)) + (*for rbc in srcImg.rbcs do + let rectangle = Rectangle(Height = float rbc.size.Height, Width = float rbc.size.Width, )*) + + let addPreview (srcImg: SourceImage) = + let imgCtrl = Views.ImageSourcePreview(Margin = Thickness(3.)) + imgCtrl.lblImageNumber.Content <- srcImg.num + let width = 200 + let height = srcImg.img.Height * width / srcImg.img.Width + imgCtrl.imagePreview.Source <- BitmapSourceConvert.ToBitmapSource(srcImg.img.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic)) + stackPreviews.Children.Add(imgCtrl) |> ignore + let updatePreviews () = stackPreviews.Children.Clear () - state.ImagesSource |> Seq.iteri (fun i imgSrc -> - let imgCtrl = Views.ImageSourcePreview(Margin = Thickness(3.)) - imgCtrl.lblImageNumber.Content <- i + 1 - let width = 200 - let height = imgSrc.img.Height * width / imgSrc.img.Width - imgCtrl.imagePreview.Source <- BitmapSourceConvert.ToBitmapSource(imgSrc.img.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic)) - stackPreviews.Children.Add(imgCtrl) |> ignore ) + for srcImg in state.SourceImages do + addPreview srcImg let updateGUI () = txtPatient.Text <- state.PatientID @@ -90,11 +107,78 @@ let run (defaultConfig: Config) = let res = dialog.ShowDialog() if res.HasValue && res.Value then - state.AddSourceImage(dialog.FileName) - updatePreviews ()) + let srcImg = state.AddSourceImage(dialog.FileName) + addPreview srcImg + if state.SourceImages.Count() = 1 + then + setCurrentImage srcImg) butStartAnalysis.Click.AddHandler(fun obj args -> ()) + // Zoom on the current image. + let adjustCurrentImageMargins () = + borderCurrentImage.BorderThickness <- + Thickness(scrollViewCurrentImage.ViewportWidth / 2., scrollViewCurrentImage.ViewportHeight / 2., scrollViewCurrentImage.ViewportWidth / 2., scrollViewCurrentImage.ViewportHeight / 2.) + + scrollViewCurrentImage.Loaded.AddHandler(fun obj args -> adjustCurrentImageMargins ()) + scrollViewCurrentImage.SizeChanged.AddHandler(fun obj args -> adjustCurrentImageMargins ()) + + let mutable currentScale = 1. + let mutable maxScale = 5. + let mutable minScale = 0.1 + let currentImageScaleTransform = ScaleTransform() + borderCurrentImage.LayoutTransform <- currentImageScaleTransform + borderCurrentImage.PreviewMouseWheel.AddHandler(fun obj args -> + let scaleFactor = if args.Delta > 0 then 2.0 else 0.5 + if scaleFactor > 1. && currentScale < maxScale || scaleFactor < 1. && currentScale > minScale + then + let previousScale = currentScale + currentScale <- + let newScale = currentScale * scaleFactor + if newScale > maxScale then maxScale elif newScale < minScale then minScale else newScale + let realScaleFactor = currentScale / previousScale + + let centerX = scrollViewCurrentImage.HorizontalOffset + scrollViewCurrentImage.ViewportWidth / 2. - canvasCurrentImage.Margin.Left + let centerY = scrollViewCurrentImage.VerticalOffset + scrollViewCurrentImage.ViewportHeight / 2. - canvasCurrentImage.Margin.Top + + canvasCurrentImage.Margin <- Thickness(canvasCurrentImage.Margin.Top * realScaleFactor) + currentImageScaleTransform.ScaleX <- currentScale + currentImageScaleTransform.ScaleY <- currentScale + + scrollViewCurrentImage.ScrollToHorizontalOffset(centerX * realScaleFactor - scrollViewCurrentImage.ViewportWidth / 2. + canvasCurrentImage.Margin.Left) + scrollViewCurrentImage.ScrollToVerticalOffset(centerY * realScaleFactor - scrollViewCurrentImage.ViewportHeight / 2. + canvasCurrentImage.Margin.Top) + args.Handled <- true) + + // Pan on the current image. + let mutable scrollStartPosition = Point(0., 0.) + let mutable scrollStartOffsetX = 0. + let mutable scrollStartOffsetY = 0. + borderCurrentImage.PreviewMouseLeftButtonDown.AddHandler(fun obj args -> + scrollStartPosition <- args.GetPosition(scrollViewCurrentImage) + scrollStartOffsetX <- scrollViewCurrentImage.HorizontalOffset + scrollStartOffsetY <- scrollViewCurrentImage.VerticalOffset + borderCurrentImage.Cursor <- Input.Cursors.ScrollAll + borderCurrentImage.CaptureMouse() |> ignore + args.Handled <- true) + + borderCurrentImage.PreviewMouseMove.AddHandler(fun obj args -> + if borderCurrentImage.IsMouseCaptured + then + let position = args.GetPosition(scrollViewCurrentImage) + let deltaX = scrollStartPosition.X - position.X + let deltaY = scrollStartPosition.Y - position.Y + scrollViewCurrentImage.ScrollToHorizontalOffset(deltaX + scrollStartOffsetX) + scrollViewCurrentImage.ScrollToVerticalOffset(deltaY + scrollStartOffsetY) + args.Handled <- true) + + borderCurrentImage.PreviewMouseLeftButtonUp.AddHandler(fun obj args -> + if borderCurrentImage.IsMouseCaptured + then + borderCurrentImage.Cursor <- Input.Cursors.Arrow + borderCurrentImage.ReleaseMouseCapture() + args.Handled <- true) + + (*let txtPatient: Controls.TextBox = ctrl "txtPatient" txtPatient.TextChanged.AddHandler(fun obj args -> state.PatientID <- txtPatient.Text)*) diff --git a/Parasitemia/Parasitemia/GUI/ImageSourcePreview.xaml.fs b/Parasitemia/Parasitemia/GUI/ImageSourcePreview.xaml.fs index b92e9af..425d436 100644 --- a/Parasitemia/Parasitemia/GUI/ImageSourcePreview.xaml.fs +++ b/Parasitemia/Parasitemia/GUI/ImageSourcePreview.xaml.fs @@ -1,4 +1,4 @@ -namespace Parasitemia.Views +namespace Parasitemia.GUI.Views open System open System.Windows diff --git a/Parasitemia/Parasitemia/GUI/MainWindow.xaml b/Parasitemia/Parasitemia/GUI/MainWindow.xaml index 2892330..9b710e5 100644 --- a/Parasitemia/Parasitemia/GUI/MainWindow.xaml +++ b/Parasitemia/Parasitemia/GUI/MainWindow.xaml @@ -37,18 +37,30 @@ - - - - - - - + + + + + + + -