From 4e56dadd2518bddf9fba13053e836fa4ef42caae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Sun, 2 Aug 2020 23:28:42 +0200 Subject: [PATCH] A list of files (pictures) can be searched and shown in listbox --- ImageHandlingLibrary/Filing.cs | 90 +++++++++++++++++++ ImageHandlingLibrary/Helpers/Dialogs.cs | 63 +++++++++++++ .../ImageHandlingLibrary.csproj | 4 + ImageHandlingLibrary/InterFaces/IDialogs.cs | 14 +++ ImageHandlingLibrary/InterFaces/IFiling.cs | 7 +- ImageHandlingUI/Bootstrapper.cs | 2 + ImageHandlingUI/ViewModels/ShellViewModel.cs | 57 ++++++++++-- ImageHandlingUI/Views/ShellView.xaml | 9 +- 8 files changed, 236 insertions(+), 10 deletions(-) create mode 100644 ImageHandlingLibrary/Helpers/Dialogs.cs create mode 100644 ImageHandlingLibrary/InterFaces/IDialogs.cs diff --git a/ImageHandlingLibrary/Filing.cs b/ImageHandlingLibrary/Filing.cs index 17819fd..f81a3af 100644 --- a/ImageHandlingLibrary/Filing.cs +++ b/ImageHandlingLibrary/Filing.cs @@ -1,6 +1,8 @@ using ImageHandlingLibrary.InterFaces; +using ImageHandlingLibrary.Models; using System; using System.Collections.Generic; +using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; @@ -41,5 +43,93 @@ namespace ImageHandlingLibrary var output = _registring.GetRegistryRootDir(); return $"Root directory value is {output} OBS Test!!"; } + + public List CreateListBoxDataSource(List fileInfos, string rootCatalog) + { + + var outPut = new List(); + + foreach (var fi in fileInfos) + { + var bufPicture = new LocalPicture(); + + bufPicture.RootPosition = rootCatalog; + + bufPicture.PictureFileName = fi.Name; + + bufPicture.InitialPath = fi.DirectoryName; + + bufPicture.AktImage = Image.FromFile(bufPicture.PictureFullPath); + + bufPicture.AktImWidth = bufPicture.AktImage.Width; + bufPicture.AktImHeight = bufPicture.AktImage.Height; + + bufPicture.CreatedDate = TakenDate(bufPicture.AktImage); + + outPut.Add(bufPicture); + } + + + return FillupDateGaps(outPut); + } + + private string TakenDate(Image aktImage) + { + string sPropDatTaken = ""; + for (int i = 0; i < aktImage.PropertyIdList.Length; i++) + { + if (aktImage.PropertyIdList[i] == 306) + { + for (int j = 0; j < aktImage.PropertyItems[i].Len; j++) + { + sPropDatTaken += Convert.ToChar(aktImage.PropertyItems[i].Value[j]); + } + } + } + return sPropDatTaken; + } + + private List FillupDateGaps(List inPut) + { + var picReg = new SortedList(); + foreach (var pict in inPut) + { + picReg.Add(pict.PictureFileName, pict); + } + var tmpPicture = new LocalPicture(); + tmpPicture.CreatedDate = ""; + Stack stack = new Stack(); + var outPut = new List(); + + foreach (KeyValuePair lPic in picReg) + { + if (lPic.Value.CreatedDate == "") + { + if (tmpPicture.CreatedDate != "") + { + lPic.Value.CreatedDate = tmpPicture.CreatedDate; + lPic.Value.CreatedDateChanged = true; + outPut.Add(lPic.Value); + } + else + { + stack.Push(lPic.Key); + } + } + else + { + tmpPicture = lPic.Value; + while (stack.Count > 0) + { + var picRegKey = stack.Pop(); + picReg[picRegKey].CreatedDate = tmpPicture.CreatedDate; + picReg[picRegKey].CreatedDateChanged = true; + outPut.Add(picReg[picRegKey]); + } + outPut.Add(lPic.Value); + } + } + return outPut; + } } } diff --git a/ImageHandlingLibrary/Helpers/Dialogs.cs b/ImageHandlingLibrary/Helpers/Dialogs.cs new file mode 100644 index 0000000..6574d59 --- /dev/null +++ b/ImageHandlingLibrary/Helpers/Dialogs.cs @@ -0,0 +1,63 @@ + +using System; +using System.Collections.Generic; +using System.Windows.Forms; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; +using ImageHandlingLibrary.InterFaces; + +namespace ImageHandlingLibrary.Helpers +{ + public class Dialogs : IDialogs + { + + public List ChooseFiles() + { + using (OpenFileDialog ofd = new OpenFileDialog()) + { + var outPut = new List(); + ofd.Title = "Välj bild-fil!"; + ofd.InitialDirectory = "C:\\"; + ofd.Filter = "Picture files (*.jpg)|*.jpg;*.jpeg|All files (*.*)|*.*"; + ofd.FilterIndex = 1; + ofd.RestoreDirectory = true; + ofd.Multiselect = true; + ofd.ShowDialog(); + var fnames = ofd.FileNames; + if (fnames.Length > 1) + { + foreach (var fname in fnames) + { + outPut.Add(new FileInfo(fname)); + } + } + //else + //{ + // string fName = ofd.FileName; + // if (fName.Trim() != "") + // { + // int fNamePos = fName.LastIndexOf(@"\") + 1; + // lblFileName.Text = fName.Substring(fNamePos); + // lblPath.Text = fName.Substring(0, fNamePos); + // listLocalFiles(); + + // for (int i = 0; i < lstPicFiles.Items.Count; i++) + // { + // if (lstPicFiles.Items[i].ToString() == lblFileName.Text) + // { + // lstPicFiles.SelectedIndex = i; + // break; + // } + // } + + // } + + //} + return outPut; + + } + } + } +} \ No newline at end of file diff --git a/ImageHandlingLibrary/ImageHandlingLibrary.csproj b/ImageHandlingLibrary/ImageHandlingLibrary.csproj index 9dbf86e..632eeb4 100644 --- a/ImageHandlingLibrary/ImageHandlingLibrary.csproj +++ b/ImageHandlingLibrary/ImageHandlingLibrary.csproj @@ -32,9 +32,11 @@ + + @@ -45,6 +47,8 @@ + + diff --git a/ImageHandlingLibrary/InterFaces/IDialogs.cs b/ImageHandlingLibrary/InterFaces/IDialogs.cs new file mode 100644 index 0000000..f110116 --- /dev/null +++ b/ImageHandlingLibrary/InterFaces/IDialogs.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ImageHandlingLibrary.InterFaces +{ + public interface IDialogs + { + List ChooseFiles(); + } +} diff --git a/ImageHandlingLibrary/InterFaces/IFiling.cs b/ImageHandlingLibrary/InterFaces/IFiling.cs index efdcb52..c35303d 100644 --- a/ImageHandlingLibrary/InterFaces/IFiling.cs +++ b/ImageHandlingLibrary/InterFaces/IFiling.cs @@ -1,6 +1,8 @@ -using System; +using ImageHandlingLibrary.Models; +using System; using System.Collections.Generic; using System.Drawing; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -11,6 +13,9 @@ namespace ImageHandlingLibrary.InterFaces public interface IFiling { BitmapImage ConvertFromDrawImage(Image _image); + List CreateListBoxDataSource(List fileInfos, string rootCatalog); string ShowText(); } + + } diff --git a/ImageHandlingUI/Bootstrapper.cs b/ImageHandlingUI/Bootstrapper.cs index 5d576d3..e204f87 100644 --- a/ImageHandlingUI/Bootstrapper.cs +++ b/ImageHandlingUI/Bootstrapper.cs @@ -1,5 +1,6 @@ using Caliburn.Micro; using ImageHandlingLibrary; +using ImageHandlingLibrary.Helpers; using ImageHandlingLibrary.InterFaces; using ImageHandlingUI.ViewModels; using System; @@ -33,6 +34,7 @@ namespace ImageHandlingUI _container .PerRequest() + .PerRequest() .PerRequest(); GetType().Assembly.GetTypes() diff --git a/ImageHandlingUI/ViewModels/ShellViewModel.cs b/ImageHandlingUI/ViewModels/ShellViewModel.cs index 62016b5..24bf243 100644 --- a/ImageHandlingUI/ViewModels/ShellViewModel.cs +++ b/ImageHandlingUI/ViewModels/ShellViewModel.cs @@ -1,5 +1,6 @@ using Caliburn.Micro; using ImageHandlingLibrary.InterFaces; +using ImageHandlingLibrary.Models; using System; using System.Collections.Generic; using System.Drawing.Imaging; @@ -9,6 +10,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Controls; +using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; @@ -19,16 +21,22 @@ namespace ImageHandlingUI.ViewModels private string _rootCatalog; private readonly IRegistring _registring; private readonly IFiling _filing; + private readonly IDialogs _dialogs; private string initialFileName; ImageSource _selectedImage = null; + private List fileInfos; + private bool canChooseFile = true; + private BindableCollection picList = new BindableCollection(); - public ShellViewModel(IRegistring registring, IFiling filing) + + public ShellViewModel(IRegistring registring, IFiling filing, IDialogs dialogs) { initialFileName = @"D:\CsharpDevelop\PictureHandlingProject\pictures\AgueroVillage.JPG"; _selectedImage = filing.ConvertFromDrawImage(System.Drawing.Image.FromFile(InitialFileName)); _registring = registring; _filing = filing; + _dialogs = dialogs; } @@ -47,29 +55,64 @@ namespace ImageHandlingUI.ViewModels public string RootCatalog { - get { + get + { _rootCatalog = _registring.GetRegistryRootDir(); - return _rootCatalog; + return _rootCatalog; } set { if (value != _rootCatalog) { _registring.SetRegistryValues(value); - } - _rootCatalog = value; } + } + _rootCatalog = value; + } } private string _groundPath; public string GroundPath { - get { + get + { _groundPath = _filing.ShowText(); - return _groundPath; + return _groundPath; } set { _groundPath = value; } } + + + public bool CanChooseFile + { + get { return canChooseFile; } + set { canChooseFile = value; } + } + + + public void ChooseFile() + { + fileInfos = _dialogs.ChooseFiles(); + if (fileInfos.Count > 0) + { + //Cursor.Current = Cursors.WaitCursor; + var localPicList = _filing.CreateListBoxDataSource(fileInfos, _rootCatalog); + foreach (var lPicture in localPicList) + { + picList.Add(lPicture); + } + NotifyOfPropertyChange(() => PicList); + } + } + + + public BindableCollection PicList + { + get { return picList; } + set { picList = value; } + } + + } } diff --git a/ImageHandlingUI/Views/ShellView.xaml b/ImageHandlingUI/Views/ShellView.xaml index 23fcbda..c70d317 100644 --- a/ImageHandlingUI/Views/ShellView.xaml +++ b/ImageHandlingUI/Views/ShellView.xaml @@ -103,8 +103,13 @@ Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="2" Margin="3,3,0,3" MinHeight="300" MinWidth="150" - HorizontalAlignment="Left" Width="150"> - + HorizontalAlignment="Left" Width="150" + ItemsSource="{Binding PicList}"> + + + + +