private void boxView_CreateBox(object sender, Rect bounds) { boxView.SelectedItems.Clear(); var box = new TessBoxControl(); box.Value = "?"; box.Width = bounds.Width; box.Height = bounds.Height; Canvas.SetLeft(box, bounds.Left); Canvas.SetTop(box, bounds.Top); _viewModel.Boxes.Add(box); }
public void AddAfterSelectedBoxes(IEnumerable <TessBoxControl> selectedBoxes) { var box = selectedBoxes.LastOrDefault(); if (box != null) { var pos = Boxes.IndexOf(box); var newBox = new TessBoxControl() { Width = box.Width, Height = box.Height, Value = "n" }; Canvas.SetLeft(newBox, box.Left + box.Width); Canvas.SetTop(newBox, box.Top); Boxes.Insert(pos + 1, newBox); } }
void _boxHost_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (!_boxHost.IsMouseCaptured) { _mouseDownPos = e.GetPosition(_boxHost); _boxHost.CaptureMouse(); _mouseDownHitBox = GetHitBox(_mouseDownPos); if (_mouseDownHitBox != null) { _mouseDownHitBox.Focus(); } else { Focus(); } } e.Handled = true; }
public void Load(string imageFileName) { var fi = new FileInfo(imageFileName); var boxFileName = Path.Combine(fi.DirectoryName, Path.GetFileNameWithoutExtension(fi.Name) + ".box"); if (!File.Exists(boxFileName)) { throw new ApplicationException(string.Format("Box file '{0}' was not found", Path.GetFileName(boxFileName))); } Close(); _fileStream = new FileStream(imageFileName, FileMode.Open, FileAccess.Read); if (TiffHelper.IsTiffFile(_fileStream)) { _tiffDecoder = new TiffBitmapDecoder(_fileStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); MaxPageIndex = _tiffDecoder.Frames.Count - 1; } else { MaxPageIndex = 0; } int recordIndex = 0; using (var reader = new StreamReader(boxFileName, Encoding.UTF8)) { ObservableCollection <TessBoxControl> boxes = null; while (true) { var line = reader.ReadLine(); if (line == null) { break; } var fields = line.Split(' '); if (fields.Length != 6) { throw new ApplicationException(string.Format("Invalid box file record at line {0}", recordIndex)); } var boxValue = fields[0]; var pageIndex = int.Parse(fields[5]); var img = GetPageImage(pageIndex); //Box file coordinates are with an origin at the bottom left on the image var left = int.Parse(fields[1]); var bottom = img.PixelHeight - int.Parse(fields[2]); var right = int.Parse(fields[3]); var top = img.PixelHeight - int.Parse(fields[4]); var width = right - left; var height = bottom - top; var box = new TessBoxControl(); box.Value = boxValue; box.Width = width; box.Height = height; Canvas.SetLeft(box, left); Canvas.SetTop(box, top); if (!_pageBoxMap.TryGetValue(pageIndex, out boxes)) { boxes = new ObservableCollection <TessBoxControl>(); _pageBoxMap[pageIndex] = boxes; } boxes.Add(box); recordIndex++; } } _imageFileName = imageFileName; _boxFileName = boxFileName; NotifyPropertyChanged("WindowTitle"); //set manually to force SelPageIndex to accept the change _selPageIndex = -1; SelPageIndex = 0; }
void _boxHost_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (_boxHost.IsMouseCaptured) { var mouseUpPos = e.GetPosition(_boxHost); if (_rubberBand != null) { _rubberBandHost.Children.Remove(_rubberBand); _rubberBand = null; } _boxHost.ReleaseMouseCapture(); var selectRec = new Rect(_mouseDownPos, mouseUpPos); //if they are holding one of the ctrl keys, make a new box... if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) { if (selectRec.Size.Width > 2 && selectRec.Height > 2) { var hanlder = CreateBox; if (hanlder != null) { hanlder.Invoke(this, selectRec); } } } else { var mouseUpHitBox = GetHitBox(mouseUpPos); if (_mouseDownHitBox == null) { var boxes = GetHitBoxes(selectRec); SelectedItems.Clear(); foreach (var box in boxes) { SelectedItems.Add(box); } } else { if (_mouseDownHitBox == mouseUpHitBox) { //mouse down and up on the same box! if (!Keyboard.IsKeyDown(Key.LeftCtrl) && !Keyboard.IsKeyDown(Key.RightCtrl)) { SelectedItems.Clear(); } if (mouseUpHitBox.IsSelected) { SelectedItems.Remove(mouseUpHitBox); } else { SelectedItems.Add(mouseUpHitBox); } } else { var boxes = GetHitBoxes(selectRec); SelectedItems.Clear(); foreach (var box in boxes) { SelectedItems.Add(box); } } } } _mouseDownHitBox = null; } e.Handled = true; }