From 57c1dc86928f6285e02e0b14db3f1d89fc9abc2e Mon Sep 17 00:00:00 2001 From: gosha20777 Date: Sat, 21 Mar 2020 17:21:25 +0300 Subject: [PATCH 1/2] feat: add ilters --- ViewModels/FilterViewModel.cs | 11 ++++++ ViewModels/MainWindowViewModel.cs | 66 ++++++++++++++++++++----------- Views/MainWindow.xaml | 7 ++-- 3 files changed, 56 insertions(+), 28 deletions(-) create mode 100644 ViewModels/FilterViewModel.cs diff --git a/ViewModels/FilterViewModel.cs b/ViewModels/FilterViewModel.cs new file mode 100644 index 0000000..fc1b0b3 --- /dev/null +++ b/ViewModels/FilterViewModel.cs @@ -0,0 +1,11 @@ +using ReactiveUI; +using ReactiveUI.Fody.Helpers; + +namespace RescuerLaApp.ViewModels +{ + public class FilterViewModel : ReactiveObject + { + [Reactive] public int FilterIndex { get; set; } = 0; + [Reactive] public int CurrentPage { get; set; } = 0; + } +} \ No newline at end of file diff --git a/ViewModels/MainWindowViewModel.cs b/ViewModels/MainWindowViewModel.cs index 2eb64c7..1238133 100755 --- a/ViewModels/MainWindowViewModel.cs +++ b/ViewModels/MainWindowViewModel.cs @@ -52,7 +52,6 @@ public class MainWindowViewModel : ReactiveObject private readonly string _mlConfigPath = Path.Join("conf", "mlConfig.json"); private int itemPerPage = 500; private int itemcount; - private int _currentPageIndex; private int _totalPages; SourceList _photos { get; set; } = new SourceList(); private ReadOnlyObservableCollection _photoCollection; @@ -60,13 +59,17 @@ public class MainWindowViewModel : ReactiveObject public MainWindowViewModel(Window window) { _window = window; - - var filter = this + + var pageFilter = this .WhenValueChanged(x => x.CurrentPage) .Select(PageFilter); + var typeFilter = this + .WhenValueChanged(x => x.FilterIndex) + .Select(TypeFilter); _photos.Connect() - .Filter(filter) + .Filter(pageFilter) + .Filter(typeFilter) .ObserveOn(RxApp.MainThreadScheduler) .Bind(out _photoCollection) .DisposeMany() @@ -156,14 +159,11 @@ private IObservable CanSetup() public ReadOnlyObservableCollection PhotoCollection => _photoCollection; [Reactive] public int SelectedIndex { get; set; } - [Reactive] public int FilterIndex { get; set; } + [Reactive] public int CurrentPage { get; set; } = 0; + + [Reactive] public int FilterIndex { get; set; } = 0; [Reactive] public PhotoViewModel PhotoViewModel { get; set; } [Reactive] public ApplicationStatusViewModel ApplicationStatusViewModel { get; set; } - [Reactive] public int CurrentPage - { - get => _currentPageIndex; - set => _currentPageIndex = value; - } [Reactive] public int TotalPages { get => _totalPages; @@ -266,8 +266,23 @@ private void ShowLastPage() private Func PageFilter(int currentPage) { - return x => - x.Id >= itemPerPage * CurrentPage && x.Id < itemPerPage * (CurrentPage + 1); + return x => + x.Id >= itemPerPage * currentPage && x.Id < itemPerPage * (currentPage + 1); + } + private Func TypeFilter(int fitlerType) + { + SelectedIndex = 0; + switch (fitlerType) + { + case 0: + return x => true; + case 1: + return x => x.Photo.Attribute == Attribute.WithObject; + case 2: + return x => x.Photo.Attribute == Attribute.Favorite; + default: + return x => true; + } } private void CalculateTotalPages() @@ -390,15 +405,17 @@ private async void PredictAll() await model.Init(); var count = 0; var objectCount = 0; - foreach (var photoViewModel in _photoCollection) + foreach (var photoViewModel in _photos.Items) { try { photoViewModel.Annotation.Objects = await model.Predict(photoViewModel); photoViewModel.BoundBoxes = photoViewModel.GetBoundingBoxes(); + if (photoViewModel.BoundBoxes.Any()) + photoViewModel.Photo.Attribute = Attribute.WithObject; objectCount += photoViewModel.BoundBoxes.Count(); count++; - Console.WriteLine($"\tProgress: {(double) count / _photoCollection.Count() * 100} %"); + Console.WriteLine($"\tProgress: {(double) count / _photos.Items.Count() * 100} %"); } catch (Exception e) { @@ -406,7 +423,7 @@ private async void PredictAll() } } await model.Stop(); - Log.Information($"Successfully predict {_photoCollection.Count} photos. Find {objectCount} objects."); + Log.Information($"Successfully predict {_photos.Items.Count()} photos. Find {objectCount} objects."); } } catch (Exception e) @@ -484,16 +501,16 @@ private async void SaveAll() { try { - if (!_photoCollection.Any()) + if (!_photos.Items.Any()) { Log.Warning("There are no photos to save."); return; } _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Working, ""); var writer = new PhotoVMWriter(_window); - await writer.WriteMany(_photoCollection); + await writer.WriteMany(_photos.Items); _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, "Success | saved"); - Log.Information($"Saved {_photoCollection.Count} photos."); + Log.Information($"Saved {_photos.Items.Count()} photos."); } catch (Exception ex) { @@ -506,16 +523,16 @@ private async void SaveAllImagesWithObjects() { try { - if (!_photoCollection.Any()) + if (!_photos.Items.Any()) { Log.Warning("There are no photos to save."); return; } _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Working, ""); var writer = new PhotoVMWriter(_window); - await writer.WriteMany(_photoCollection.Where(x => x.Photo.Attribute == Attribute.WithObject)); + await writer.WriteMany(_photos.Items.Where(x => x.Photo.Attribute == Attribute.WithObject)); _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, "Success | saved"); - Log.Information($"Saved {_photoCollection.Count} photos."); + Log.Information($"Saved {_photos.Items.Count()} photos."); } catch (Exception ex) { @@ -528,16 +545,16 @@ private async void SaveFavoritesImages() { try { - if (!_photoCollection.Any()) + if (!_photos.Items.Any()) { Log.Warning("There are no photos to save."); return; } _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Working, ""); var writer = new PhotoVMWriter(_window); - await writer.WriteMany(_photoCollection.Where(x => x.Photo.Attribute == Attribute.Favorite)); + await writer.WriteMany(_photos.Items.Where(x => x.Photo.Attribute == Attribute.Favorite)); _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, "Success | saved"); - Log.Information($"Saved {_photoCollection.Count} photos."); + Log.Information($"Saved {_photos.Items.Count()} photos."); } catch (Exception ex) { @@ -774,6 +791,7 @@ await Dispatcher.UIThread.InvokeAsync(() => $"{Enums.Status.Ready.ToString()} | {PhotoViewModel.Path}"); Log.Debug($"Ui updated to index {SelectedIndex}"); + Log.Debug($"Ui updated to filter index {FilterIndex}"); }); } catch (Exception ex) diff --git a/Views/MainWindow.xaml b/Views/MainWindow.xaml index 976fef2..be1a351 100755 --- a/Views/MainWindow.xaml +++ b/Views/MainWindow.xaml @@ -74,7 +74,8 @@ - + + @@ -156,8 +157,6 @@ Width="{Binding CanvasWidth, Mode=TwoWay}"> - - @@ -173,7 +172,7 @@ + IsVisible="{Binding #bShowBorder.IsChecked, Mode=OneWay}"/> From e12dc9c43aeadb4dbf961f62756d3173489160c4 Mon Sep 17 00:00:00 2001 From: gosha20777 Date: Sat, 21 Mar 2020 17:59:09 +0300 Subject: [PATCH 2/2] feat: add favorite functional --- Models/Photo/Photo.cs | 2 +- ViewModels/MainWindowViewModel.cs | 27 +++++++++++++++++---------- ViewModels/PhotoViewModel.cs | 2 +- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Models/Photo/Photo.cs b/Models/Photo/Photo.cs index b771772..f1c5713 100644 --- a/Models/Photo/Photo.cs +++ b/Models/Photo/Photo.cs @@ -8,7 +8,7 @@ namespace RescuerLaApp.Models.Photo public class Photo : IPhoto { public ImageBrush ImageBrush { get; set; } - public Attribute Attribute { get; set; } + public Attribute Attribute { get; set; } = Attribute.NotProcessed; public IReadOnlyList MetaDataDirectories { get; set; } } } \ No newline at end of file diff --git a/ViewModels/MainWindowViewModel.cs b/ViewModels/MainWindowViewModel.cs index 1238133..76948c5 100755 --- a/ViewModels/MainWindowViewModel.cs +++ b/ViewModels/MainWindowViewModel.cs @@ -142,7 +142,6 @@ private void SetupCommand(IObservable canExecute, IObservable canSwi ShowAllMetadataCommand = ReactiveCommand.Create(ShowAllMetadata, canExecute); ShowGeoDataCommand = ReactiveCommand.Create(ShowGeoData, canExecute); AddToFavoritesCommand = ReactiveCommand.Create(AddToFavorites, canExecute); - SwitchBoundBoxesVisibilityCommand = ReactiveCommand.Create(SwitchBoundBoxesVisibility, canSwitchBoundBox); HelpCommand = ReactiveCommand.Create(Help); AboutCommand = ReactiveCommand.Create(About); OpenWizardCommand = ReactiveCommand.Create(OpenWizard); @@ -170,7 +169,6 @@ [Reactive] public int TotalPages set => _totalPages = value; } // TODO: update with locales - [Reactive] public string BoundBoxesStateString { get; set; } = "Hide bound boxes"; [Reactive] public string FavoritesStateString { get; set; } = "Add to favorites"; [Reactive] public double CanvasWidth { get; set; } = 500; [Reactive] public double CanvasHeight { get; set; } = 500; @@ -678,14 +676,22 @@ public void ShowAllMetadata() */ } - public void AddToFavorites() + public async void AddToFavorites() { - - } - - public void SwitchBoundBoxesVisibility() - { - + if (_photoCollection[SelectedIndex].Photo.Attribute != Attribute.Favorite) + { + _photoCollection[SelectedIndex].Photo.Attribute = Attribute.Favorite; + } + else + { + if(_photoCollection[SelectedIndex].BoundBoxes.Any()) + _photoCollection[SelectedIndex].Photo.Attribute = Attribute.WithObject; + else + { + _photoCollection[SelectedIndex].Photo.Attribute = Attribute.Empty; + } + } + await UpdateUi(); } public async void About() @@ -789,9 +795,10 @@ await Dispatcher.UIThread.InvokeAsync(() => if (_applicationStatusManager.AppStatusInfo.Status == Enums.Status.Ready) _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, $"{Enums.Status.Ready.ToString()} | {PhotoViewModel.Path}"); + + FavoritesStateString = PhotoCollection[SelectedIndex].Photo.Attribute == Attribute.Favorite ? "Remove from favorites" : "Add to favorites"; Log.Debug($"Ui updated to index {SelectedIndex}"); - Log.Debug($"Ui updated to filter index {FilterIndex}"); }); } catch (Exception ex) diff --git a/ViewModels/PhotoViewModel.cs b/ViewModels/PhotoViewModel.cs index 0bf734a..c61b367 100644 --- a/ViewModels/PhotoViewModel.cs +++ b/ViewModels/PhotoViewModel.cs @@ -31,7 +31,7 @@ [Reactive] public Annotation Annotation } [Reactive] public string Caption { get; private set; } [Reactive] public string Path { get; private set; } - [Reactive] public IEnumerable BoundBoxes { get; set; } + [Reactive] public IEnumerable BoundBoxes { get; set; } = new List(); public int Id { get; set; } private void UpdatePhotoInfo(Annotation annotation)