1
module ParasitemiaUI.GUI
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.
18 open ParasitemiaCore.Config
19 open ParasitemiaCore.Utils
22 let run (defaultConfig
: Config) (fileToOpen
: string option) =
23 let app = new Application()
24 let win = Views.MainWindow()
26 let state = State.State(defaultConfig
)
27 let mutable currentScale = 1.
28 let mutable displayHealthy = false
29 let warningBelowNumberOfRBC = 1000
32 let canvasCurrentImageColor = win.canvasCurrentImage
.Background
33 win.menuHightlightRBC
.IsChecked <- displayHealthy
36 let extractRBCPreview (img
: Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte
>) (rbc
: RBC) : Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte
> =
37 let rbcWidth = rbc
.size
.Width
38 let rbcHeight = rbc
.size
.Height
39 let x = rbc
.center
.X - rbcWidth / 2. |> roundInt
40 let y = rbc
.center
.Y - rbcHeight / 2. |> roundInt
41 let w = roundInt
rbcWidth
42 let h = roundInt
rbcHeight
43 img
.GetSubRect(System.Drawing.Rectangle(System.Drawing.Point((if x < 0 then 0 else x), (if y < 0 then 0 else y)),
44 System.Drawing.Size((if x + w >= img
.Width then img
.Width - x else w),
45 (if y + h >= img
.Height then img
.Height - y else h))))
47 let setRBCFrameStyle (srcImg
: SourceImage) (rbc
: RBC) (frame
: Views.RBCFrame) =
48 frame
.Opacity <- if displayHealthy || rbc.setManually
|| rbc.infected
then 1. else 0.
49 let color = if rbc.infected
then srcImg
.InfectedRBCColor else srcImg.HealthyRBCColor
50 frame
.manuallyAdded
.Visibility <- if rbc.setManually
then Visibility.Visible else Visibility.Hidden
51 frame
.manuallyAdded
.Fill <- color
52 frame
.border
.Stroke <- color
54 let RBCFrameFromExisting (srcImg : SourceImage) (rbc : RBC) (frame
: Views.RBCFrame) : Views.RBCFrame =
55 frame
.Visibility <- Visibility.Visible
56 frame
.Height <- rbc.size
.Height
57 frame
.Width <- rbc.size
.Width
59 setRBCFrameStyle srcImg rbc frame
60 frame
.border
.StrokeThickness <- 1.
61 frame
.txtRBCNumber
.Text <- rbc.num
.ToString()
64 let updateDocumentStatus () =
65 win.txtDocumentStatus
.Text <- if state.FilePath = "" then "<New document>" else state.FilePath
67 let statusMessageTimer = Threading.DispatcherTimer()
68 statusMessageTimer.Tick.AddHandler(fun obj args
-> statusMessageTimer.Stop(); win.txtMessageStatus
.Text <- "")
69 statusMessageTimer.Interval <- TimeSpan(0, 0, 2)
70 let displayStatusMessage (message
: string) =
71 win.txtMessageStatus
.Text <- message
72 statusMessageTimer.Stop()
73 statusMessageTimer.Start()
75 let highlightRBCFrame (frame
: Views.RBCFrame) (highlight
: bool) =
76 let rbc = frame
.Tag :?> RBC
78 frame
.border
.StrokeThickness <- 3.
79 if not
rbc.infected
&& not
rbc.setManually
&& not
displayHealthy then frame
.Opacity <- 1.
81 frame
.border
.StrokeThickness <- 1.
82 if not
rbc.infected
&& not
rbc.setManually
&& not
displayHealthy then frame
.Opacity <- 0.
84 let zoomToRBC (rbc : RBC) =
85 win.scrollViewCurrentImage
.ScrollToHorizontalOffset(rbc.center
.X * currentScale - win.scrollViewCurrentImage
.ViewportWidth / 2. + win.borderCurrentImage
.BorderThickness.Left)
86 win.scrollViewCurrentImage
.ScrollToVerticalOffset(rbc.center
.Y * currentScale - win.scrollViewCurrentImage
.ViewportHeight / 2. + win.borderCurrentImage
.BorderThickness.Top)
89 let txtImageName_TextChanged =
90 TextChangedEventHandler(fun obj args
-> state.CurrentImage |> Option.iter
(fun srcImg -> state.SetName srcImg win.txtImageName
.Text))
92 let updateCurrentImageInformation () =
93 win.txtImageName
.TextChanged.RemoveHandler(txtImageName_TextChanged)
94 win.txtImageInformation1
.Inlines.Clear()
95 win.txtImageInformation2
.Inlines.Clear()
96 win.txtImageName
.Text <- ""
98 match state.CurrentImage with
100 win.gridImageInformation
.Visibility <- Visibility.Visible
101 win.txtImageName
.Text <- srcImg.name
102 win.txtImageName
.TextChanged.AddHandler(txtImageName_TextChanged)
105 let parasitemiaStr = Utils.percentText
(state.ImageParasitemia srcImg)
106 win.txtImageInformation1
.Inlines.Add(Documents.Run("Parasitemia: ", FontWeight = FontWeights.Bold))
107 win.txtImageInformation1
.Inlines.Add(parasitemiaStr)
108 win.txtImageInformation1
.Inlines.Add(Documents.LineBreak())
110 win.txtImageInformation1
.Inlines.Add(Documents.Run("Last analysis: ", FontWeight = FontWeights.Bold))
111 win.txtImageInformation1
.Inlines.Add(Documents.Run(if srcImg.dateLastAnalysis
.Ticks = 0L then "<Never>" else srcImg.dateLastAnalysis
.ToLocalTime().ToString()))
113 // The right part part.
114 win.txtImageInformation2
.Inlines.Add(Documents.Run("Added infected erythrocyte: ", FontWeight = FontWeights.Bold))
115 win.txtImageInformation2
.Inlines.Add(Documents.Run((state.ImageNbManuallyChangedRBCStr srcImg true) + " " + (state.ImageManuallyChangedRBCStr srcImg true)))
116 win.txtImageInformation2
.Inlines.Add(Documents.LineBreak())
117 win.txtImageInformation2
.Inlines.Add(Documents.Run("Removed infected erythrocyte: ", FontWeight = FontWeights.Bold))
118 win.txtImageInformation2
.Inlines.Add(Documents.Run((state.ImageNbManuallyChangedRBCStr srcImg false) + " " + (state.ImageManuallyChangedRBCStr srcImg false)))
121 win.gridImageInformation
.Visibility <- Visibility.Hidden
123 let updateGlobalParasitemia () =
124 win.txtGlobalParasitemia
.Inlines.Clear()
125 let total, infected
= state.GlobalParasitemia
126 win.txtGlobalParasitemia
.Inlines.Add(Documents.Run(Utils.percentText
(total, infected
), FontWeight = FontWeights.Bold))
127 if total > 0 && total < warningBelowNumberOfRBC then
128 win.txtGlobalParasitemia
.Inlines.Add(
130 sprintf
" Warning: the number of erythrocytes should be above %d" warningBelowNumberOfRBC,
131 FontWeight = FontWeights.Bold,
132 Foreground = Brushes.Red
136 let updateViewportPreview () =
137 for preview
in win.stackPreviews
.Children |> Seq.cast
<Views.ImageSourcePreview> do
138 let srcImg = preview
.Tag :?> SourceImage
139 if Some srcImg = state.CurrentImage then
140 preview
.viewport
.Visibility <- Visibility.Visible
142 let canvasWidth = win.canvasCurrentImage
.ActualWidth * currentScale
143 let canvasHeight = win.canvasCurrentImage
.ActualHeight * currentScale
144 let previewWidth = (preview
.ActualWidth - preview
.BorderThickness.Left - preview
.BorderThickness.Right)
145 let previewHeight = (preview
.ActualHeight - preview
.BorderThickness.Top - preview
.BorderThickness.Bottom)
147 let marginLeft = previewWidth * (win.scrollViewCurrentImage
.HorizontalOffset - win.borderCurrentImage
.BorderThickness.Left) / canvasWidth - 2.
148 let marginRight = previewWidth * (canvasWidth - (win.scrollViewCurrentImage
.HorizontalOffset - win.borderCurrentImage
.BorderThickness.Right) - win.scrollViewCurrentImage
.ViewportWidth) / canvasWidth - 2.
149 let marginTop = previewHeight * (win.scrollViewCurrentImage
.VerticalOffset - win.borderCurrentImage
.BorderThickness.Top) / canvasHeight - 2.
150 let marginBottom = previewHeight * (canvasHeight - (win.scrollViewCurrentImage
.VerticalOffset - win.borderCurrentImage
.BorderThickness.Bottom) - win.scrollViewCurrentImage
.ViewportHeight) / canvasHeight - 2.
152 preview
.viewport
.Margin <-
160 preview
.viewport
.Visibility <- Visibility.Hidden
162 let rec setAsInfected
(srcImg : SourceImage) (rbc : RBC) (infected
: bool) =
163 state.SetAsInfected rbc infected
164 win.canvasCurrentImage
.Children
165 |> Seq.cast
<Views.RBCFrame>
168 if (frame
.Tag :?> RBC) = rbc then
169 setRBCFrameStyle srcImg rbc frame
171 updateRBCFramesPreview
()
172 updateCurrentImageInformation ()
173 updateGlobalParasitemia ()
175 and RBCFrame (srcImg : SourceImage) (rbc : RBC) : Views.RBCFrame =
176 let frame = RBCFrameFromExisting srcImg rbc (Views.RBCFrame())
177 frame.SetValue(Panel.ZIndexProperty, Int32.MaxValue - rbc.num
) // To be sure the
178 frame.menuRBCSetAsHealthy
.Click.AddHandler(fun obj args
-> setAsInfected
srcImg (frame.Tag :?> RBC) false)
179 frame.menuRBCSetAsInfected
.Click.AddHandler(fun obj args
-> setAsInfected
srcImg (frame.Tag :?> RBC) true)
180 frame.ContextMenuOpening.AddHandler(
182 if (frame.Tag :?> RBC).infected
then
183 frame.menuRBCSetAsHealthy
.Visibility <- Visibility.Visible
184 frame.menuRBCSetAsInfected
.Visibility <- Visibility.Collapsed
186 frame.menuRBCSetAsHealthy
.Visibility <- Visibility.Collapsed
187 frame.menuRBCSetAsInfected
.Visibility <- Visibility.Visible
189 frame.ContextMenuClosing.AddHandler(fun obj args
-> if not
frame.IsMouseOver then highlightRBCFrame frame false )
190 frame.MouseEnter.AddHandler(fun obj args
-> highlightRBCFrame frame true)
191 frame.MouseLeave.AddHandler(fun obj args
-> if not
frame.grid
.ContextMenu.IsOpen then highlightRBCFrame frame false)
194 and updateRBCFramesPreview
() =
195 match state.CurrentImage with
197 let mutable currentPreview = 0
198 for rbc in srcImg.rbcs
|> List.filter
(fun rbc -> displayHealthy || rbc.infected
) do
199 let previewInfected =
200 if currentPreview < win.stackRBC
.Children.Count then
201 RBCFrameFromExisting srcImg rbc (win.stackRBC
.Children.[currentPreview] :?> Views.RBCFrame)
203 let f = RBCFrame srcImg rbc
204 f.MouseLeftButtonUp.AddHandler(fun obj args
-> zoomToRBC (f.Tag :?> RBC))
205 win.stackRBC
.Children.Add(f) |> ignore
208 currentPreview <- currentPreview + 1
210 previewInfected.Height <- win.stackRBC
.ActualHeight
211 previewInfected.Width <- win.stackRBC
.ActualHeight * rbc.size
.Width / rbc.size
.Height
212 previewInfected.border
.Fill <- ImageBrush(BitmapSourceConvert.ToBitmapSource(extractRBCPreview srcImg.img
rbc))
214 win.stackRBC
.Children.RemoveRange(currentPreview, win.stackRBC
.Children.Count - currentPreview)
217 updateViewportPreview ()
219 let updateRBCFramesCurrent () =
220 match state.CurrentImage with
222 let mutable currentCanvas = 0
223 for rbc in srcImg.rbcs
do
225 if currentCanvas < win.canvasCurrentImage
.Children.Count then
226 RBCFrameFromExisting srcImg rbc (win.canvasCurrentImage
.Children.[currentCanvas] :?> Views.RBCFrame)
228 let f = RBCFrame srcImg rbc
230 win.canvasCurrentImage
.Children.Add(f) |> ignore
233 currentCanvas <- currentCanvas + 1
235 Canvas.SetLeft(frame, rbc.center
.X - rbc.size
.Width / 2.)
236 Canvas.SetTop(frame, rbc.center
.Y - rbc.size
.Height / 2.)
238 for i
in currentCanvas .. win.canvasCurrentImage
.Children.Count - 1 do
239 win.canvasCurrentImage
.Children.[i
].Visibility <- Visibility.Hidden
242 let askDocumentPathToSave () : string option =
243 let dialog = SaveFileDialog(AddExtension = true, DefaultExt = PiaZ.extension
, Filter = PiaZ.filter
)
245 if state.FilePath <> "" then
246 dialog.FileName <- FileInfo(state.FilePath).Name
247 elif
state.PatientID <> "" then
248 dialog.FileName <- state.PatientID + PiaZ.extension
250 let res = dialog.ShowDialog()
251 if res.HasValue && res.Value then
256 let saveCurrentDocument () =
258 if state.FilePath = "" then
259 match askDocumentPathToSave () with
261 state.FilePath <- filepath
266 updateDocumentStatus ()
267 displayStatusMessage "Document saved"
269 | :? IOException as ex ->
270 Log.Error(ex.ToString())
271 MessageBox.Show(sprintf
"The document cannot be save in \"%s\"" state.FilePath, "Error saving the document", MessageBoxButton.OK, MessageBoxImage.Error) |> ignore
273 let saveCurrentDocumentAsNewFile () =
274 match askDocumentPathToSave () with
276 state.FilePath <- filepath
277 saveCurrentDocument ()
280 // Ask the use to save the current document if neccessary.
281 let askSaveCurrent () =
282 if state.AlteredSinceLastSave then
283 match MessageBox.Show("Would you like to save the current document?", "Saving the current document", MessageBoxButton.YesNo, MessageBoxImage.Question) with
284 | MessageBoxResult.Yes -> saveCurrentDocument ()
287 let updateCurrentImage () =
288 match state.CurrentImage with
290 win.imgLogos
.Visibility <- Visibility.Collapsed
292 // Highlight the preview.
293 win.stackPreviews
.Children
294 |> Seq.cast
<Views.ImageSourcePreview>
295 |> Seq.iter
(fun preview
-> preview
.border
.BorderThickness <- Thickness(if preview
.Tag = (srcImg :> Object) then 3. else 0.))
297 win.canvasCurrentImage
.Height <- float srcImg.img
.Height
298 win.canvasCurrentImage
.Width <- float srcImg.img
.Width
299 win.canvasCurrentImage
.Background <- ImageBrush(BitmapSourceConvert.ToBitmapSource(srcImg.img
))
301 updateRBCFramesCurrent ()
302 updateRBCFramesPreview
()
305 win.imgLogos
.Visibility <- Visibility.Visible
307 win.stackRBC
.Children.Clear()
308 win.canvasCurrentImage
.Children.Clear()
309 win.canvasCurrentImage
.Background <- canvasCurrentImageColor
311 updateCurrentImageInformation ()
313 let setCurrentImage (srcImg : SourceImage) =
314 if state.CurrentImage.IsNone || state.CurrentImage.Value <> srcImg then
315 state.CurrentImage <- Some srcImg
316 updateCurrentImage ()
318 let addPreview (srcImg : SourceImage) =
319 let imgCtrl = Views.ImageSourcePreview(Margin = Thickness(3.))
321 imgCtrl.menuRemoveImage
.Click.AddHandler(fun obj args
->
322 win.stackPreviews
.Children.Remove(imgCtrl)
323 let srcImg = imgCtrl.Tag :?> SourceImage
324 let currentRemoved = Some srcImg = state.CurrentImage
325 state.RemoveSourceImage srcImg
326 if currentRemoved then
329 updateGlobalParasitemia()
331 // Update image numbers.
332 win.stackPreviews
.Children |> Seq.cast
<Views.ImageSourcePreview> |> Seq.iter
(fun imgPreview
-> imgPreview
.txtImageNumber
.Text <- (imgPreview
.Tag :?> SourceImage).num
.ToString()))
334 imgCtrl.Tag <- srcImg
335 imgCtrl.txtImageNumber
.Text <- srcImg.num
.ToString()
337 let height = srcImg.img
.Height * width / srcImg.img
.Width
338 imgCtrl.imagePreview
.Source <- BitmapSourceConvert.ToBitmapSource(srcImg.img
.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic))
339 win.stackPreviews
.Children.Add(imgCtrl) |> ignore
341 // Zoom to a mouse position into the control 'imgCtrl'.
342 let zoomTo (mousePos
: Point) =
343 let canvasW = win.canvasCurrentImage
.ActualWidth * currentScale
344 let canvasH = win.canvasCurrentImage
.ActualHeight * currentScale
345 let centerX = (mousePos
.X - imgCtrl.BorderThickness.Left) / (imgCtrl.ActualWidth - imgCtrl.BorderThickness.Left) * canvasW
346 let centerY = (mousePos
.Y - imgCtrl.BorderThickness.Top) / (imgCtrl.ActualHeight - imgCtrl.BorderThickness.Top) * canvasH
347 win.scrollViewCurrentImage
.ScrollToHorizontalOffset(centerX - win.scrollViewCurrentImage
.ViewportWidth / 2. + win.borderCurrentImage
.BorderThickness.Left)
348 win.scrollViewCurrentImage
.ScrollToVerticalOffset(centerY - win.scrollViewCurrentImage
.ViewportHeight / 2. + win.borderCurrentImage
.BorderThickness.Top)
350 imgCtrl.MouseLeftButtonDown.AddHandler(fun obj args
->
351 setCurrentImage (state.SourceImages |> Seq.find
(fun srcImg -> (srcImg :> Object) = imgCtrl.Tag))
352 imgCtrl.UpdateLayout()
353 zoomTo (args
.GetPosition(imgCtrl))
354 imgCtrl.CaptureMouse() |> ignore
357 imgCtrl.MouseMove.AddHandler(fun obj args
->
358 if imgCtrl.IsMouseCaptured then
359 zoomTo (args
.GetPosition(imgCtrl))
362 imgCtrl.MouseLeftButtonUp.AddHandler(fun obj args
->
363 if imgCtrl.IsMouseCaptured then
364 imgCtrl.ReleaseMouseCapture()
367 let updatePreviews () =
368 win.stackPreviews
.Children.Clear ()
369 for srcImg in state.SourceImages do
371 updateCurrentImage ()
374 win.txtPatient
.Text <- state.PatientID
376 updateGlobalParasitemia ()
377 updateDocumentStatus ()
379 let loadFile (filepath
: string) =
381 let previousFilePath = state.FilePath
383 state.FilePath <- filepath
387 | :? IOException as ex ->
388 Log.Error(ex.ToString())
389 state.FilePath <- previousFilePath
390 MessageBox.Show(sprintf
"The document cannot be loaded from \"%s\"" filepath
, "Error loading the document", MessageBoxButton.OK, MessageBoxImage.Error) |> ignore
393 let dialog = OpenFileDialog(Filter = PiaZ.filter
)
394 let res = dialog.ShowDialog()
395 if res.HasValue && res.Value then
396 loadFile dialog.FileName
403 let exportResults () =
404 let extension = ".txt"
405 let dialog = SaveFileDialog(AddExtension = true, DefaultExt = extension)
407 if state.FilePath <> "" then
408 dialog.FileName <- Path.GetFileNameWithoutExtension(state.FilePath) + extension
409 elif
state.PatientID <> "" then
410 dialog.FileName <- state.PatientID + extension
412 let res = dialog.ShowDialog()
413 if res.HasValue && res.Value then
415 Export.exportResults state dialog.FileName
417 | :? IOException as ex ->
418 Log.Error(ex.ToString())
419 MessageBox.Show(sprintf
"The results cannot be exported in \"%s\"" state.FilePath, "Error exporting the files", MessageBoxButton.OK, MessageBoxImage.Error) |> ignore
422 let dialog = OpenFileDialog(Filter = "Image Files|*.png;*.jpg;*.tif;*.tiff", Multiselect = true)
423 let res = dialog.ShowDialog()
424 if res.HasValue && res.Value then
425 let noSourceImage = state.SourceImages.Count() = 0
427 for filename
in dialog.FileNames do
429 let srcImg = state.AddSourceImage filename defaultConfig
433 Log.Error(ex.ToString())
434 MessageBox.Show(sprintf
"Unable to read the image from \"%s\"" filename, "Error adding an image", MessageBoxButton.OK, MessageBoxImage.Error) |> ignore
436 updateGlobalParasitemia ()
438 if noSourceImage then
439 updateCurrentImage ()
441 let showAnalysisWindow () =
442 if Analysis.showWindow
win state then
443 updateGlobalParasitemia ()
444 updateCurrentImage ()
446 let setHighlightRBC (highlight
: bool) =
447 displayHealthy <- highlight
448 updateRBCFramesPreview
()
449 updateRBCFramesCurrent ()
451 win.txtPatient
.TextChanged.AddHandler(fun obj args
-> state.PatientID <- win.txtPatient
.Text)
453 win.menuExit
.Click.AddHandler(fun obj args
-> win.Close())
454 win.menuSave
.Click.AddHandler(fun obj args
-> saveCurrentDocument ())
455 win.menuSaveAs
.Click.AddHandler(fun obj args
-> saveCurrentDocumentAsNewFile ())
456 win.menuOpen
.Click.AddHandler(fun obj args
-> askLoadFile ())
457 win.menuNew
.Click.AddHandler(fun obj args
-> newFile ())
458 win.menuExportResults
.Click.AddHandler(fun obj args
-> exportResults ())
460 win.menuAddSourceImage
.Click.AddHandler(fun obj args
-> importImage ())
462 win.menuAnalysis
.SubmenuOpened.AddHandler(fun obj args
-> win.menuStartAnalysis
.IsEnabled <- state.SourceImages.Count() > 0)
464 win.menuStartAnalysis
.Click.AddHandler(fun obj args
-> showAnalysisWindow ())
466 win.menuHightlightRBC
.Click.AddHandler(fun obj args
-> setHighlightRBC win.menuHightlightRBC
.IsChecked)
468 win.menuAbout
.Click.AddHandler(fun obj args
-> About.showWindow
win)
470 win.Closing.AddHandler(fun obj args
-> askSaveCurrent ())
472 // Zoom on the current image.
473 let adjustCurrentImageBorders (deltaX
: float) (deltaY
: float) =
474 win.borderCurrentImage
.BorderThickness <-
476 (win.scrollViewCurrentImage
.ViewportWidth + deltaX
) / 2.,
477 (win.scrollViewCurrentImage
.ViewportHeight + deltaY
) / 2.,
478 (win.scrollViewCurrentImage
.ViewportWidth + deltaX
) / 2.,
479 (win.scrollViewCurrentImage
.ViewportHeight + deltaY
) / 2.
482 win.canvasCurrentImage
.SizeChanged.AddHandler(fun obj args
->
483 let deltaX = args
.NewSize.Width - args
.PreviousSize.Width
484 let deltaY = args
.NewSize.Height - args
.PreviousSize.Height
485 if deltaX > 0.5 || deltaY > 0.5 then
486 adjustCurrentImageBorders 0.0
0.0
487 // Center the view at the center of the image initialy.
488 win.scrollViewCurrentImage
.UpdateLayout()
489 win.scrollViewCurrentImage
.ScrollToHorizontalOffset(win.borderCurrentImage
.ActualWidth / 2. - win.scrollViewCurrentImage
.ViewportWidth / 2.)
490 win.scrollViewCurrentImage
.ScrollToVerticalOffset(win.borderCurrentImage
.ActualHeight / 2. - win.scrollViewCurrentImage
.ViewportHeight / 2.)
493 win.scrollViewCurrentImage
.SizeChanged.AddHandler(fun obj args
->
494 let deltaX = args
.NewSize.Width - args
.PreviousSize.Width
495 let deltaY = args
.NewSize.Height - args
.PreviousSize.Height
496 adjustCurrentImageBorders deltaX deltaY
497 win.scrollViewCurrentImage
.ScrollToHorizontalOffset(win.scrollViewCurrentImage
.HorizontalOffset + deltaX / 8.)
498 win.scrollViewCurrentImage
.ScrollToVerticalOffset(win.scrollViewCurrentImage
.VerticalOffset + deltaY / 8.)
501 let mutable maxScale = 4.
502 let mutable minScale = 0.25
503 let currentImageScaleTransform = ScaleTransform()
504 win.canvasCurrentImage
.LayoutTransform <- currentImageScaleTransform
505 win.borderCurrentImage
.PreviewMouseWheel.AddHandler(fun obj args
->
506 let scaleFactor = if args
.Delta > 0 then 2.0 else 0.5
507 if scaleFactor > 1. && currentScale < maxScale || scaleFactor < 1. && currentScale > minScale then
508 let previousScale = currentScale
510 let newScale = currentScale * scaleFactor
511 if newScale > maxScale then maxScale elif newScale < minScale then minScale else newScale
512 let realScaleFactor = currentScale / previousScale
514 let centerX = win.scrollViewCurrentImage
.HorizontalOffset + win.scrollViewCurrentImage
.ViewportWidth / 2. - win.borderCurrentImage
.BorderThickness.Left
515 let centerY = win.scrollViewCurrentImage
.VerticalOffset + win.scrollViewCurrentImage
.ViewportHeight / 2. - win.borderCurrentImage
.BorderThickness.Top
517 currentImageScaleTransform.ScaleX <- currentScale
518 currentImageScaleTransform.ScaleY <- currentScale
520 win.scrollViewCurrentImage
.ScrollToHorizontalOffset(centerX * realScaleFactor - win.scrollViewCurrentImage
.ViewportWidth / 2. + win.borderCurrentImage
.BorderThickness.Left)
521 win.scrollViewCurrentImage
.ScrollToVerticalOffset(centerY * realScaleFactor - win.scrollViewCurrentImage
.ViewportHeight / 2. + win.borderCurrentImage
.BorderThickness.Top)
526 // Pan on the current image.
527 let mutable scrollStartPosition = Point(0., 0.)
528 let mutable scrollStartOffsetX = 0.
529 let mutable scrollStartOffsetY = 0.
530 win.borderCurrentImage
.PreviewMouseLeftButtonDown.AddHandler(fun obj args
->
531 scrollStartPosition <- args
.GetPosition(win.scrollViewCurrentImage
)
532 scrollStartOffsetX <- win.scrollViewCurrentImage
.HorizontalOffset
533 scrollStartOffsetY <- win.scrollViewCurrentImage
.VerticalOffset
534 win.borderCurrentImage
.Cursor <- Input.Cursors.ScrollAll
535 win.borderCurrentImage
.CaptureMouse() |> ignore
539 win.borderCurrentImage
.PreviewMouseMove.AddHandler(fun obj args
->
540 if win.borderCurrentImage
.IsMouseCaptured then
541 let position = args
.GetPosition(win.scrollViewCurrentImage
)
542 let deltaX = scrollStartPosition.X - position.X
543 let deltaY = scrollStartPosition.Y - position.Y
544 win.scrollViewCurrentImage
.ScrollToHorizontalOffset(deltaX + scrollStartOffsetX)
545 win.scrollViewCurrentImage
.ScrollToVerticalOffset(deltaY + scrollStartOffsetY)
550 win.borderCurrentImage
.PreviewMouseLeftButtonUp.AddHandler(fun obj args
->
551 if win.borderCurrentImage
.IsMouseCaptured then
552 win.borderCurrentImage
.Cursor <- Input.Cursors.Arrow
553 win.borderCurrentImage
.ReleaseMouseCapture()
559 win.InputBindings.Add(
561 ViewModule.FunCommand((fun obj
-> saveCurrentDocument ()), (fun obj
-> true)),
562 Input.KeyGesture(Input.Key.S, Input.ModifierKeys.Control)
567 win.InputBindings.Add(
569 ViewModule.FunCommand((fun obj
-> saveCurrentDocumentAsNewFile ()), (fun obj
-> true)),
570 Input.KeyGesture(Input.Key.S, Input.ModifierKeys.Control ||| Input.ModifierKeys.Shift)
575 win.InputBindings.Add(
577 ViewModule.FunCommand((fun obj
-> askLoadFile ()), (fun obj
-> true)),
578 Input.KeyGesture(Input.Key.O, Input.ModifierKeys.Control)
583 win.InputBindings.Add(
585 ViewModule.FunCommand((fun obj
-> newFile ()), (fun obj
-> true)),
586 Input.KeyGesture(Input.Key.N, Input.ModifierKeys.Control)
591 win.InputBindings.Add(
593 ViewModule.FunCommand((fun obj
-> exportResults ()), (fun obj
-> true)),
594 Input.KeyGesture(Input.Key.E, Input.ModifierKeys.Control)
599 win.InputBindings.Add(
601 ViewModule.FunCommand((fun obj
-> importImage ()), (fun obj
-> true)),
602 Input.KeyGesture(Input.Key.A, Input.ModifierKeys.Control)
606 // Show analysis dialog.
607 win.InputBindings.Add(
609 ViewModule.FunCommand((fun obj
-> showAnalysisWindow ()), (fun obj
-> state.SourceImages.Count() > 0)),
610 Input.KeyGesture(Input.Key.Y, Input.ModifierKeys.Control)
614 // Toggle RBC highlight.
615 win.InputBindings.Add(
617 ViewModule.FunCommand(
620 win.menuHightlightRBC
.IsChecked <- not
win.menuHightlightRBC
.IsChecked
621 setHighlightRBC win.menuHightlightRBC
.IsChecked
625 Input.KeyGesture(Input.Key.H, Input.ModifierKeys.Control)
630 win.scrollViewCurrentImage
.ScrollChanged.AddHandler(fun obj args
-> updateViewportPreview ())
632 updateDocumentStatus ()
633 win.gridImageInformation
.Visibility <- Visibility.Hidden
637 match fileToOpen
with
638 | Some filepath
-> loadFile filepath