From 593aee44f912e026d7c3b47821a14a97511e21b0 Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Mon, 29 Jun 2015 18:03:56 -0700 Subject: [PATCH] Writing unit tests for the library provider stuff. Got the file folder provider acting like nodes rather than a static set. --- PartPreviewWindow/View3D/View3DWidget.cs | 4 +- PrintLibrary/LibraryDataView.cs | 629 +++++++++--------- PrintLibrary/LibraryRowItem.cs | 2 - PrintLibrary/LibraryRowItemCollection.cs | 19 +- PrintLibrary/LibraryRowItemPart.cs | 9 +- PrintLibrary/PrintLibraryWidget.cs | 10 +- PrintLibrary/Provider/LibraryProvider.cs | 67 +- .../Provider/LibraryProviderFileSystem.cs | 71 +- .../Provider/LibraryProviderPlugin.cs | 2 +- .../Provider/LibraryProviderSelector.cs | 227 ++----- .../Provider/LibraryProviderSqlite.cs | 55 +- PrintLibrary/Provider/LibrarySQLiteData.cs | 2 +- PrintQueue/QueueDataWidget.cs | 2 +- .../MatterControl.Tests.csproj | 1 + .../MatterControl/LibraryProviderTests.cs | 123 ++++ .../LibraryProviderData/Box20x20x10.stl | Bin 0 -> 684 bytes 16 files changed, 599 insertions(+), 624 deletions(-) create mode 100644 Tests/MatterControl.Tests/MatterControl/LibraryProviderTests.cs create mode 100644 Tests/TestData/TestMeshes/LibraryProviderData/Box20x20x10.stl diff --git a/PartPreviewWindow/View3D/View3DWidget.cs b/PartPreviewWindow/View3D/View3DWidget.cs index 179af9955..3e04642ce 100644 --- a/PartPreviewWindow/View3D/View3DWidget.cs +++ b/PartPreviewWindow/View3D/View3DWidget.cs @@ -1870,8 +1870,8 @@ namespace MatterHackers.MatterControl.PartPreviewWindow } saveSucceded = true; - //LibraryProvider.Instance.SaveToCollection(printItemWrapper.PrintItem.LibraryProviderLocator - LibraryProvider.Instance.SaveToLibrary(printItemWrapper, asynchMeshGroups); + //LibraryDataView.CurrentLibraryProvider.SaveToCollection(printItemWrapper.PrintItem.LibraryProviderLocator + LibraryProviderSQLite.Instance.SaveToLibrary(printItemWrapper, asynchMeshGroups); } catch (System.UnauthorizedAccessException) { diff --git a/PrintLibrary/LibraryDataView.cs b/PrintLibrary/LibraryDataView.cs index 47dd20859..35cfee29e 100644 --- a/PrintLibrary/LibraryDataView.cs +++ b/PrintLibrary/LibraryDataView.cs @@ -39,10 +39,76 @@ namespace MatterHackers.MatterControl.PrintLibrary { public class LibraryDataView : ScrollableWidget { - private event EventHandler unregisterEvents; + public SelectedListItems SelectedItems = new SelectedListItems(); + + protected FlowLayoutWidget topToBottomItemList; + + private static LibraryProvider currentLibraryProvider; + + private RGBA_Bytes baseColor = new RGBA_Bytes(255, 255, 255); private bool editMode = false; + private RGBA_Bytes hoverColor = new RGBA_Bytes(204, 204, 204, 255); + + private int hoverIndex = -1; + + private RGBA_Bytes selectedColor = new RGBA_Bytes(180, 180, 180, 255); + + private int selectedIndex = -1; + + private bool settingLocalBounds = false; + + public LibraryDataView() + { + // set the display attributes + { + this.AnchorAll(); + this.BackgroundColor = ActiveTheme.Instance.SecondaryBackgroundColor; + this.ScrollArea.Padding = new BorderDouble(3, 3, 5, 3); + } + + ScrollArea.HAnchor = HAnchor.ParentLeftRight; + + AutoScroll = true; + topToBottomItemList = new FlowLayoutWidget(FlowDirection.TopToBottom); + topToBottomItemList.HAnchor = HAnchor.ParentLeftRight; + AddChild(topToBottomItemList); + + AddAllItems(); + + this.MouseLeaveBounds += new EventHandler(control_MouseLeaveBounds); + LibraryProvider.DataReloaded.RegisterEvent(LibraryDataReloaded, ref unregisterEvents); + LibraryProvider.ItemAdded.RegisterEvent(ItemAddedToLibrary, ref unregisterEvents); + LibraryProvider.ItemRemoved.RegisterEvent(ItemRemovedFromToLibrary, ref unregisterEvents); + } + + public delegate void HoverValueChangedEventHandler(object sender, EventArgs e); + + public event HoverValueChangedEventHandler HoverValueChanged; + + public event Action SelectedValueChanged; + + private event EventHandler unregisterEvents; + + public static LibraryProvider CurrentLibraryProvider + { + get + { + if (currentLibraryProvider == null) + { + currentLibraryProvider = LibraryProviderSelector.Instance; + } + + return currentLibraryProvider; + } + + set + { + currentLibraryProvider = value; + } + } + public bool EditMode { get { return editMode; } @@ -59,80 +125,6 @@ namespace MatterHackers.MatterControl.PrintLibrary } } - public void RemoveSelectedIndex() - { - if (SelectedIndex >= 0 && SelectedIndex < Count) - { - RemoveChild(SelectedIndex); - } - } - - public PrintItemWrapper SelectedPart - { - get - { - if (SelectedIndex >= 0) - { - return LibraryProvider.Instance.GetPrintItemWrapper(SelectedIndex); - } - else - { - return null; - } - } - } - - public void ClearSelectedItems() - { - foreach (LibraryRowItem item in SelectedItems) - { - item.isSelectedItem = false; - item.selectionCheckBox.Checked = false; - } - this.SelectedItems.Clear(); - } - - public event Action SelectedValueChanged; - - public delegate void HoverValueChangedEventHandler(object sender, EventArgs e); - - public event HoverValueChangedEventHandler HoverValueChanged; - - protected FlowLayoutWidget topToBottomItemList; - - private RGBA_Bytes hoverColor = new RGBA_Bytes(204, 204, 204, 255); - private RGBA_Bytes selectedColor = new RGBA_Bytes(180, 180, 180, 255); - private RGBA_Bytes baseColor = new RGBA_Bytes(255, 255, 255); - - public SelectedListItems SelectedItems = new SelectedListItems(); - private int selectedIndex = -1; - private int hoverIndex = -1; - - private int Count - { - get - { - return topToBottomItemList.Children.Count; - } - } - - public int SelectedIndex - { - get - { - return selectedIndex; - } - set - { - if (value < -1 || value >= topToBottomItemList.Children.Count) - { - throw new ArgumentOutOfRangeException(); - } - selectedIndex = value; - OnSelectedIndexChanged(); - } - } - public int HoverIndex { get @@ -170,123 +162,6 @@ namespace MatterHackers.MatterControl.PrintLibrary } } - public LibraryDataView() - { - // set the display attributes - { - this.AnchorAll(); - this.BackgroundColor = ActiveTheme.Instance.SecondaryBackgroundColor; - this.ScrollArea.Padding = new BorderDouble(3, 3, 5, 3); - } - - ScrollArea.HAnchor = HAnchor.ParentLeftRight; - - AutoScroll = true; - topToBottomItemList = new FlowLayoutWidget(FlowDirection.TopToBottom); - topToBottomItemList.HAnchor = HAnchor.ParentLeftRight; - AddChild(topToBottomItemList); - - AddAllItems(); - - this.MouseLeaveBounds += new EventHandler(control_MouseLeaveBounds); - LibraryProvider.DataReloaded.RegisterEvent(LibraryDataReloaded, ref unregisterEvents); - LibraryProvider.ItemAdded.RegisterEvent(ItemAddedToLibrary, ref unregisterEvents); - LibraryProvider.ItemRemoved.RegisterEvent(ItemRemovedFromToLibrary, ref unregisterEvents); - } - - public void RebuildView() - { - AddAllItems(); - } - - private void AddAllItems() - { - topToBottomItemList.RemoveAllChildren(); - - PrintItemCollection parent = LibraryProvider.Instance.GetParentCollectionItem(); - if (parent != null) - { - LibraryRowItem queueItem = new LibraryRowItemCollection(parent, this, false); - AddListItemToTopToBottom(queueItem); - } - - for (int i = 0; i < LibraryProvider.Instance.CollectionCount; i++) - { - PrintItemCollection item = LibraryProvider.Instance.GetCollectionItem(i); - LibraryRowItem queueItem = new LibraryRowItemCollection(item, this); - AddListItemToTopToBottom(queueItem); - } - - for (int i = 0; i < LibraryProvider.Instance.ItemCount; i++) - { - PrintItemWrapper item = LibraryProvider.Instance.GetPrintItemWrapper(i); - LibraryRowItem queueItem = new LibraryRowItemPart(item, this); - AddListItemToTopToBottom(queueItem); - } - } - - private void LibraryDataReloaded(object sender, EventArgs e) - { - AddAllItems(); - } - - public override void OnClosed(EventArgs e) - { - if (unregisterEvents != null) - { - unregisterEvents(this, null); - } - base.OnClosed(e); - } - - private void ItemAddedToLibrary(object sender, EventArgs e) - { - IndexArgs addedIndexArgs = e as IndexArgs; - PrintItemWrapper item = LibraryProvider.Instance.GetPrintItemWrapper(addedIndexArgs.Index); - LibraryRowItem libraryItem = new LibraryRowItemPart(item, this); - - int displayIndexToAdd = addedIndexArgs.Index + LibraryProvider.Instance.CollectionCount; - if (LibraryProvider.Instance.HasParent) - { - displayIndexToAdd++; - } - AddListItemToTopToBottom(libraryItem, displayIndexToAdd); - } - - private void ItemRemovedFromToLibrary(object sender, EventArgs e) - { - IndexArgs removeIndexArgs = e as IndexArgs; - int indexToRemove = removeIndexArgs.Index + LibraryProvider.Instance.CollectionCount; - if (LibraryProvider.Instance.HasParent) - { - indexToRemove++; - } - topToBottomItemList.RemoveChild(indexToRemove); - - if (LibraryProvider.Instance.ItemCount > 0) - { - SelectedIndex = Math.Max(SelectedIndex - 1, 0); - } - } - - public void AddListItemToTopToBottom(GuiWidget child, int indexInChildrenList = -1) - { - FlowLayoutWidget itemHolder = new FlowLayoutWidget(); - itemHolder.Name = "list item holder"; - itemHolder.Margin = new BorderDouble(0, 0, 0, 0); - itemHolder.HAnchor = Agg.UI.HAnchor.Max_FitToChildren_ParentWidth; - itemHolder.AddChild(child); - itemHolder.VAnchor = VAnchor.FitToChildren; - topToBottomItemList.AddChild(itemHolder, indexInChildrenList); - - itemHolder.MouseEnterBounds += new EventHandler(itemToAdd_MouseEnterBounds); - itemHolder.MouseLeaveBounds += new EventHandler(itemToAdd_MouseLeaveBounds); - itemHolder.MouseDownInBounds += itemHolder_MouseDownInBounds; - itemHolder.ParentChanged += new EventHandler(itemHolder_ParentChanged); - } - - private bool settingLocalBounds = false; - public override RectangleDouble LocalBounds { set @@ -321,122 +196,19 @@ namespace MatterHackers.MatterControl.PrintLibrary } } - public void RemoveSelectedItems() + public int SelectedIndex { - foreach (LibraryRowItem item in SelectedItems) + get { - item.RemoveFromParentCollection(); + return selectedIndex; } - } - - public override void RemoveChild(int index) - { - topToBottomItemList.RemoveChild(index); - } - - public override void RemoveChild(GuiWidget childToRemove) - { - for (int i = topToBottomItemList.Children.Count - 1; i >= 0; i--) + set { - GuiWidget itemHolder = topToBottomItemList.Children[i]; - if (itemHolder == childToRemove || itemHolder.Children[0] == childToRemove) + if (value < -1 || value >= topToBottomItemList.Children.Count) { - topToBottomItemList.RemoveChild(itemHolder); + throw new ArgumentOutOfRangeException(); } - } - } - - private void itemHolder_ParentChanged(object sender, EventArgs e) - { - FlowLayoutWidget itemHolder = (FlowLayoutWidget)sender; - itemHolder.MouseEnterBounds -= new EventHandler(itemToAdd_MouseEnterBounds); - itemHolder.MouseLeaveBounds -= new EventHandler(itemToAdd_MouseLeaveBounds); - itemHolder.MouseDownInBounds -= itemHolder_MouseDownInBounds; - itemHolder.ParentChanged -= new EventHandler(itemHolder_ParentChanged); - } - - private void itemHolder_MouseDownInBounds(object sender, MouseEventArgs mouseEvent) - { - } - - private void control_MouseLeaveBounds(object sender, EventArgs e) - { - HoverIndex = -1; - } - - private void itemToAdd_MouseLeaveBounds(object sender, EventArgs e) - { - GuiWidget widgetLeft = ((GuiWidget)sender); - - if (SelectedIndex >= 0) - { - if (widgetLeft != topToBottomItemList.Children[SelectedIndex]) - { - widgetLeft.BackgroundColor = new RGBA_Bytes(); - widgetLeft.Invalidate(); - Invalidate(); - } - } - } - - private void itemToAdd_MouseEnterBounds(object sender, EventArgs e) - { - GuiWidget widgetEntered = ((GuiWidget)sender); - for (int index = 0; index < topToBottomItemList.Children.Count; index++) - { - GuiWidget child = topToBottomItemList.Children[index]; - if (child == widgetEntered) - { - HoverIndex = index; - } - } - } - - public void OnSelectedIndexChanged() - { - Invalidate(); - if (SelectedValueChanged != null) - { - SelectedValueChanged(this, null); - } - } - - public void OnHoverIndexChanged() - { - Invalidate(); - if (HoverValueChanged != null) - { - HoverValueChanged(this, null); - } - } - - public override void OnDraw(Graphics2D graphics2D) - { - //activeView.OnDraw(graphics2D); - - base.OnDraw(graphics2D); - } - - public override void OnMouseDown(MouseEventArgs mouseEvent) - { - base.OnMouseDown(mouseEvent); - } - - public override void OnMouseUp(MouseEventArgs mouseEvent) - { - base.OnMouseUp(mouseEvent); - } - - public override void OnMouseMove(MouseEventArgs mouseEvent) - { - base.OnMouseMove(mouseEvent); - } - - public void ClearSelected() - { - if (selectedIndex != -1) - { - selectedIndex = -1; + selectedIndex = value; OnSelectedIndexChanged(); } } @@ -464,5 +236,258 @@ namespace MatterHackers.MatterControl.PrintLibrary } } } + + public PrintItemWrapper SelectedPart + { + get + { + if (SelectedIndex >= 0) + { + return LibraryDataView.CurrentLibraryProvider.GetPrintItemWrapper(SelectedIndex); + } + else + { + return null; + } + } + } + + private int Count + { + get + { + return topToBottomItemList.Children.Count; + } + } + + public void AddListItemToTopToBottom(GuiWidget child, int indexInChildrenList = -1) + { + FlowLayoutWidget itemHolder = new FlowLayoutWidget(); + itemHolder.Name = "list item holder"; + itemHolder.Margin = new BorderDouble(0, 0, 0, 0); + itemHolder.HAnchor = Agg.UI.HAnchor.Max_FitToChildren_ParentWidth; + itemHolder.AddChild(child); + itemHolder.VAnchor = VAnchor.FitToChildren; + topToBottomItemList.AddChild(itemHolder, indexInChildrenList); + + itemHolder.MouseEnterBounds += new EventHandler(itemToAdd_MouseEnterBounds); + itemHolder.MouseLeaveBounds += new EventHandler(itemToAdd_MouseLeaveBounds); + itemHolder.MouseDownInBounds += itemHolder_MouseDownInBounds; + itemHolder.ParentChanged += new EventHandler(itemHolder_ParentChanged); + } + + public void ClearSelected() + { + if (selectedIndex != -1) + { + selectedIndex = -1; + OnSelectedIndexChanged(); + } + } + + public void ClearSelectedItems() + { + foreach (LibraryRowItem item in SelectedItems) + { + item.isSelectedItem = false; + item.selectionCheckBox.Checked = false; + } + this.SelectedItems.Clear(); + } + + public override void OnClosed(EventArgs e) + { + if (unregisterEvents != null) + { + unregisterEvents(this, null); + } + base.OnClosed(e); + } + + public override void OnDraw(Graphics2D graphics2D) + { + //activeView.OnDraw(graphics2D); + + base.OnDraw(graphics2D); + } + + public void OnHoverIndexChanged() + { + Invalidate(); + if (HoverValueChanged != null) + { + HoverValueChanged(this, null); + } + } + + public override void OnMouseDown(MouseEventArgs mouseEvent) + { + base.OnMouseDown(mouseEvent); + } + + public override void OnMouseMove(MouseEventArgs mouseEvent) + { + base.OnMouseMove(mouseEvent); + } + + public override void OnMouseUp(MouseEventArgs mouseEvent) + { + base.OnMouseUp(mouseEvent); + } + + public void OnSelectedIndexChanged() + { + Invalidate(); + if (SelectedValueChanged != null) + { + SelectedValueChanged(this, null); + } + } + + public void RebuildView() + { + AddAllItems(); + } + + public override void RemoveChild(int index) + { + topToBottomItemList.RemoveChild(index); + } + + public override void RemoveChild(GuiWidget childToRemove) + { + for (int i = topToBottomItemList.Children.Count - 1; i >= 0; i--) + { + GuiWidget itemHolder = topToBottomItemList.Children[i]; + if (itemHolder == childToRemove || itemHolder.Children[0] == childToRemove) + { + topToBottomItemList.RemoveChild(itemHolder); + } + } + } + + public void RemoveSelectedIndex() + { + if (SelectedIndex >= 0 && SelectedIndex < Count) + { + RemoveChild(SelectedIndex); + } + } + + public void RemoveSelectedItems() + { + foreach (LibraryRowItem item in SelectedItems) + { + throw new NotImplementedException(); + //item.RemoveFromParentCollection(); + } + } + + private void AddAllItems() + { + topToBottomItemList.RemoveAllChildren(); + + if (LibraryDataView.CurrentLibraryProvider.ParentLibraryProvider != null) + { + PrintItemCollection parent = new PrintItemCollection("..", LibraryDataView.CurrentLibraryProvider.ProviderKey); + LibraryRowItem queueItem = new LibraryRowItemCollection(parent, this, LibraryDataView.CurrentLibraryProvider.ParentLibraryProvider); + AddListItemToTopToBottom(queueItem); + } + + for (int i = 0; i < LibraryDataView.CurrentLibraryProvider.CollectionCount; i++) + { + PrintItemCollection item = LibraryDataView.CurrentLibraryProvider.GetCollectionItem(i); + LibraryRowItem queueItem = new LibraryRowItemCollection(item, this, null); + AddListItemToTopToBottom(queueItem); + } + + for (int i = 0; i < LibraryDataView.CurrentLibraryProvider.ItemCount; i++) + { + PrintItemWrapper item = LibraryDataView.CurrentLibraryProvider.GetPrintItemWrapper(i); + LibraryRowItem queueItem = new LibraryRowItemPart(item, this); + AddListItemToTopToBottom(queueItem); + } + } + + private void control_MouseLeaveBounds(object sender, EventArgs e) + { + HoverIndex = -1; + } + + private void ItemAddedToLibrary(object sender, EventArgs e) + { + IndexArgs addedIndexArgs = e as IndexArgs; + PrintItemWrapper item = LibraryDataView.CurrentLibraryProvider.GetPrintItemWrapper(addedIndexArgs.Index); + LibraryRowItem libraryItem = new LibraryRowItemPart(item, this); + + int displayIndexToAdd = addedIndexArgs.Index + LibraryDataView.CurrentLibraryProvider.CollectionCount; + if (LibraryDataView.CurrentLibraryProvider.HasParent) + { + displayIndexToAdd++; + } + AddListItemToTopToBottom(libraryItem, displayIndexToAdd); + } + + private void itemHolder_MouseDownInBounds(object sender, MouseEventArgs mouseEvent) + { + } + + private void itemHolder_ParentChanged(object sender, EventArgs e) + { + FlowLayoutWidget itemHolder = (FlowLayoutWidget)sender; + itemHolder.MouseEnterBounds -= new EventHandler(itemToAdd_MouseEnterBounds); + itemHolder.MouseLeaveBounds -= new EventHandler(itemToAdd_MouseLeaveBounds); + itemHolder.MouseDownInBounds -= itemHolder_MouseDownInBounds; + itemHolder.ParentChanged -= new EventHandler(itemHolder_ParentChanged); + } + + private void ItemRemovedFromToLibrary(object sender, EventArgs e) + { + IndexArgs removeIndexArgs = e as IndexArgs; + int indexToRemove = removeIndexArgs.Index + LibraryDataView.CurrentLibraryProvider.CollectionCount; + if (LibraryDataView.CurrentLibraryProvider.HasParent) + { + indexToRemove++; + } + topToBottomItemList.RemoveChild(indexToRemove); + + if (LibraryDataView.CurrentLibraryProvider.ItemCount > 0) + { + SelectedIndex = Math.Max(SelectedIndex - 1, 0); + } + } + + private void itemToAdd_MouseEnterBounds(object sender, EventArgs e) + { + GuiWidget widgetEntered = ((GuiWidget)sender); + for (int index = 0; index < topToBottomItemList.Children.Count; index++) + { + GuiWidget child = topToBottomItemList.Children[index]; + if (child == widgetEntered) + { + HoverIndex = index; + } + } + } + + private void itemToAdd_MouseLeaveBounds(object sender, EventArgs e) + { + GuiWidget widgetLeft = ((GuiWidget)sender); + + if (SelectedIndex >= 0) + { + if (widgetLeft != topToBottomItemList.Children[SelectedIndex]) + { + widgetLeft.BackgroundColor = new RGBA_Bytes(); + widgetLeft.Invalidate(); + Invalidate(); + } + } + } + + private void LibraryDataReloaded(object sender, EventArgs e) + { + AddAllItems(); + } } } \ No newline at end of file diff --git a/PrintLibrary/LibraryRowItem.cs b/PrintLibrary/LibraryRowItem.cs index ccf7082ff..584c4627b 100644 --- a/PrintLibrary/LibraryRowItem.cs +++ b/PrintLibrary/LibraryRowItem.cs @@ -221,8 +221,6 @@ namespace MatterHackers.MatterControl.PrintLibrary public abstract void RemoveFromCollection(); - public abstract void RemoveFromParentCollection(); - protected abstract SlideWidget GetItemActionButtons(); protected abstract string GetItemName(); diff --git a/PrintLibrary/LibraryRowItemCollection.cs b/PrintLibrary/LibraryRowItemCollection.cs index 4a17354c8..a50c7b375 100644 --- a/PrintLibrary/LibraryRowItemCollection.cs +++ b/PrintLibrary/LibraryRowItemCollection.cs @@ -46,13 +46,13 @@ namespace MatterHackers.MatterControl.PrintLibrary { public class LibraryRowItemCollection : LibraryRowItem { + LibraryProvider parentProvider; PrintItemCollection collection; - bool isSubdirector; - public LibraryRowItemCollection(PrintItemCollection collection, LibraryDataView libraryDataView, bool isSubdirector = true) + public LibraryRowItemCollection(PrintItemCollection collection, LibraryDataView libraryDataView, LibraryProvider parentProvider) : base(libraryDataView) { - this.isSubdirector = isSubdirector; + this.parentProvider = parentProvider; this.collection = collection; CreateGuiElements(); } @@ -77,17 +77,12 @@ namespace MatterHackers.MatterControl.PrintLibrary throw new NotImplementedException(); } - public override void RemoveFromParentCollection() - { - throw new NotImplementedException(); - } - private ConditionalClickWidget primaryClickContainer; protected override GuiWidget GetThumbnailWidget() { string path = Path.Combine("Icons", "FileDialog", "folder.png"); - if(!isSubdirector) + if(parentProvider != null) { path = Path.Combine("Icons", "FileDialog", "upfolder.png"); } @@ -136,13 +131,13 @@ namespace MatterHackers.MatterControl.PrintLibrary private void ChangeCollection() { - if (isSubdirector) + if (parentProvider == null) { - LibraryProvider.Instance.SetCollectionBase(collection); + LibraryDataView.CurrentLibraryProvider = LibraryDataView.CurrentLibraryProvider.GetProviderForItem(collection); } else { - LibraryProvider.Instance.SetCollectionBase(LibraryProvider.Instance.GetParentCollectionItem()); + LibraryDataView.CurrentLibraryProvider = parentProvider; } UiThread.RunOnIdle(libraryDataView.RebuildView); diff --git a/PrintLibrary/LibraryRowItemPart.cs b/PrintLibrary/LibraryRowItemPart.cs index 36379cba7..02d970797 100644 --- a/PrintLibrary/LibraryRowItemPart.cs +++ b/PrintLibrary/LibraryRowItemPart.cs @@ -137,12 +137,7 @@ namespace MatterHackers.MatterControl.PrintLibrary public override void RemoveFromCollection() { - LibraryProvider.Instance.RemoveItem(printItemWrapper); - } - - public override void RemoveFromParentCollection() - { - LibraryProvider.Instance.RemoveItem(printItemWrapper); + LibraryDataView.CurrentLibraryProvider.RemoveItem(printItemWrapper); } protected override SlideWidget GetItemActionButtons() @@ -211,7 +206,7 @@ namespace MatterHackers.MatterControl.PrintLibrary protected override void RemoveThisFromPrintLibrary() { - LibraryProvider.Instance.RemoveItem(this.printItemWrapper); + LibraryDataView.CurrentLibraryProvider.RemoveItem(this.printItemWrapper); } private void ExportQueueItemWindow_Closed(object sender, EventArgs e) diff --git a/PrintLibrary/PrintLibraryWidget.cs b/PrintLibrary/PrintLibraryWidget.cs index 062db7831..6bbb58587 100644 --- a/PrintLibrary/PrintLibraryWidget.cs +++ b/PrintLibrary/PrintLibraryWidget.cs @@ -211,7 +211,7 @@ namespace MatterHackers.MatterControl.PrintLibrary private void CreateNamedFolder(CreateFolderWindow.CreateFolderReturnInfo returnInfo) { - LibraryProvider.Instance.AddCollectionToLibrary(returnInfo.newName); + LibraryDataView.CurrentLibraryProvider.AddCollectionToLibrary(returnInfo.newName); } private void CreateEditBarButtons() @@ -261,7 +261,7 @@ namespace MatterHackers.MatterControl.PrintLibrary private void CollectionChanged(object sender, EventArgs e) { - List providerLocator = LibraryProvider.Instance.GetProviderLocator(); + List providerLocator = LibraryDataView.CurrentLibraryProvider.GetProviderLocator(); StringBuilder path = new StringBuilder(); bool first = true; foreach (ProviderLocatorNode node in providerLocator) @@ -322,7 +322,7 @@ namespace MatterHackers.MatterControl.PrintLibrary private void searchButtonClick(object sender, EventArgs mouseEvent) { string searchText = searchInput.Text.Trim(); - LibraryProvider.Instance.KeywordFilter = searchText; + LibraryDataView.CurrentLibraryProvider.KeywordFilter = searchText; libraryDataView.ClearSelectedItems(); } @@ -441,7 +441,7 @@ namespace MatterHackers.MatterControl.PrintLibrary public override void OnDragDrop(FileDropEventArgs fileDropEventArgs) { - LibraryProvider.Instance.AddFilesToLibrary(fileDropEventArgs.DroppedFiles, LibraryProvider.Instance.GetProviderLocator()); + LibraryDataView.CurrentLibraryProvider.AddFilesToLibrary(fileDropEventArgs.DroppedFiles, LibraryDataView.CurrentLibraryProvider.GetProviderLocator()); base.OnDragDrop(fileDropEventArgs); } @@ -456,7 +456,7 @@ namespace MatterHackers.MatterControl.PrintLibrary { if (openParams.FileNames != null) { - LibraryProvider.Instance.AddFilesToLibrary(openParams.FileNames, null); + LibraryDataView.CurrentLibraryProvider.AddFilesToLibrary(openParams.FileNames, null); } } } diff --git a/PrintLibrary/Provider/LibraryProvider.cs b/PrintLibrary/Provider/LibraryProvider.cs index 9d0a7f6a5..57f1a5e9d 100644 --- a/PrintLibrary/Provider/LibraryProvider.cs +++ b/PrintLibrary/Provider/LibraryProvider.cs @@ -39,45 +39,26 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { public abstract class LibraryProvider { - private string parentProviderKey = null; - public string ParentProviderKey { get { return parentProviderKey; } } - public static RootedObjectEventHandler CollectionChanged = new RootedObjectEventHandler(); public static RootedObjectEventHandler DataReloaded = new RootedObjectEventHandler(); public static RootedObjectEventHandler ItemAdded = new RootedObjectEventHandler(); public static RootedObjectEventHandler ItemRemoved = new RootedObjectEventHandler(); + private LibraryProvider parentLibraryProvider = null; - private static LibraryProvider instance; - - public static LibraryProvider Instance + public LibraryProvider(LibraryProvider parentLibraryProvider) { - get - { - if (instance == null) - { - instance = new LibraryProviderSelector(null); - } - - return instance; - } - - set - { - instance = value; - } + this.parentLibraryProvider = parentLibraryProvider; } - public LibraryProvider(string parentProviderKey) - { - this.parentProviderKey = parentProviderKey; - } + public LibraryProvider ParentLibraryProvider { get { return parentLibraryProvider; } } #region Member Methods + public bool HasParent { get { - if (this.parentProviderKey != null) + if (this.ParentLibraryProvider != null) { return true; } @@ -85,7 +66,22 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider return false; } } - #endregion + + // A key,value list that threads into the current collection looks like "key0,displayName0|key1,displayName1|key2,displayName2|...|keyN,displayNameN". + public List GetProviderLocator() + { + List providerLocator = new List(); + if (ParentLibraryProvider != null) + { + providerLocator.AddRange(ParentLibraryProvider.GetProviderLocator()); + } + + providerLocator.Add(new ProviderLocatorNode(ProviderKey, Name, ProviderData)); + + return providerLocator; + } + + #endregion Member Methods #region Abstract Methods @@ -97,6 +93,8 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public abstract string Name { get; } + public abstract string ProviderData { get; } + public abstract string ProviderKey { get; } public abstract void AddCollectionToLibrary(string collectionName); @@ -107,12 +105,9 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public abstract PrintItemCollection GetCollectionItem(int collectionIndex); - public abstract PrintItemCollection GetParentCollectionItem(); - public abstract PrintItemWrapper GetPrintItemWrapper(int itemIndex); - // A key,value list that threads into the current collection looks like "key0,displayName0|key1,displayName1|key2,displayName2|...|keyN,displayNameN". - public abstract List GetProviderLocator(); + public abstract LibraryProvider GetProviderForItem(PrintItemCollection collection); public abstract void RemoveCollection(string collectionName); @@ -120,25 +115,23 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public abstract void SaveToLibrary(PrintItemWrapper printItemWrapper, List meshGroupsToSave, List providerSavePath = null); - public abstract void SetCollectionBase(PrintItemCollection collectionBase); - #endregion Abstract Methods #region Static Methods public static void OnDataReloaded(EventArgs eventArgs) { - DataReloaded.CallEvents(Instance, eventArgs); + DataReloaded.CallEvents(null, eventArgs); } public static void OnItemAdded(EventArgs eventArgs) { - ItemAdded.CallEvents(Instance, eventArgs); + ItemAdded.CallEvents(null, eventArgs); } public static void OnItemRemoved(EventArgs eventArgs) { - ItemRemoved.CallEvents(Instance, eventArgs); + ItemRemoved.CallEvents(null, eventArgs); } #endregion Static Methods @@ -148,11 +141,13 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { public string Key; public string Name; + public string ProviderData; - public ProviderLocatorNode(string key, string name) + public ProviderLocatorNode(string key, string name, string providerData) { this.Key = key; this.Name = name; + this.ProviderData = providerData; } } } \ No newline at end of file diff --git a/PrintLibrary/Provider/LibraryProviderFileSystem.cs b/PrintLibrary/Provider/LibraryProviderFileSystem.cs index 8ce915f26..7866c4794 100644 --- a/PrintLibrary/Provider/LibraryProviderFileSystem.cs +++ b/PrintLibrary/Provider/LibraryProviderFileSystem.cs @@ -52,15 +52,16 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider private string keywordFilter = string.Empty; private string rootPath; - public LibraryProviderFileSystem(string rootPath, string description, string parentProviderKey) - : base(parentProviderKey) + public LibraryProviderFileSystem(string rootPath, string description, LibraryProvider parentLibraryProvider) + : base(parentLibraryProvider) { this.description = description; this.rootPath = rootPath; key = keyCount.ToString(); keyCount++; - SetCollectionBase(null); + + directoryWatcher.Path = rootPath; directoryWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; @@ -71,6 +72,8 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider // Begin watching. directoryWatcher.EnableRaisingEvents = true; + + GetFilesAndCollectionsInCurrentDirectory(); } public override int CollectionCount @@ -109,6 +112,11 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public override string Name { get { return description; } } + public override string ProviderData + { + get { return rootPath; } + } + public override string ProviderKey { get @@ -158,42 +166,28 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider return new PrintItemCollection(Path.GetFileNameWithoutExtension(directoryName), directoryName); } - public override PrintItemCollection GetParentCollectionItem() - { - if (currentDirectory == ".") - { - if (ParentProviderKey != null) - { - return new PrintItemCollection("..", ParentProviderKey); - } - else - { - return null; - } - } - else - { - string parentDirectory = Path.GetDirectoryName(currentDirectory); - return new PrintItemCollection("..", parentDirectory); - } - } - public override PrintItemWrapper GetPrintItemWrapper(int itemIndex) { string fileName = currentDirectoryFiles[itemIndex]; - List providerLocator = LibraryProvider.Instance.GetProviderLocator(); + List providerLocator = GetProviderLocator(); string providerLocatorJson = JsonConvert.SerializeObject(providerLocator); return new PrintItemWrapper(new DataStorage.PrintItem(Path.GetFileNameWithoutExtension(fileName), fileName, providerLocatorJson)); } - public override List GetProviderLocator() + public override LibraryProvider GetProviderForItem(PrintItemCollection collection) { - throw new NotImplementedException(); + return new LibraryProviderFileSystem(Path.Combine(rootPath, collection.Key), collection.Name, this); } public override void RemoveCollection(string collectionName) { - throw new NotImplementedException(); + string directoryPath = Path.Combine(rootPath, currentDirectory, collectionName); + if (Directory.Exists(directoryPath)) + { + Directory.Delete(directoryPath); + GetFilesAndCollectionsInCurrentDirectory(); + LibraryProvider.OnDataReloaded(null); + } } public override void RemoveItem(PrintItemWrapper printItemWrapper) @@ -208,27 +202,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider throw new NotImplementedException(); } - public override void SetCollectionBase(PrintItemCollection collectionBase) - { - if (collectionBase == null) - { - currentDirectory = "."; - } - else - { - string collectionPath = collectionBase.Key; - int startOfCurrentDir = collectionPath.IndexOf('.'); - if (startOfCurrentDir != -1) - { - this.currentDirectory = collectionPath.Substring(startOfCurrentDir); - } - } - - GetFilesAndCollectionsInCurrentDirectory(); - - directoryWatcher.Path = Path.Combine(rootPath, currentDirectory); - } - private static void CopyAllFiles(IList files, string destPath) { // make sure the directory exists @@ -262,7 +235,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { int endingNumber; // check if the last set of characters is a number - if(int.TryParse(fileNameWithoutExtension.Substring(lastSpaceIndex), out endingNumber)) + if (int.TryParse(fileNameWithoutExtension.Substring(lastSpaceIndex), out endingNumber)) { fileNameWithoutExtension = fileNameWithoutExtension.Substring(0, lastSpaceIndex); } diff --git a/PrintLibrary/Provider/LibraryProviderPlugin.cs b/PrintLibrary/Provider/LibraryProviderPlugin.cs index 900cb23e2..86f707bba 100644 --- a/PrintLibrary/Provider/LibraryProviderPlugin.cs +++ b/PrintLibrary/Provider/LibraryProviderPlugin.cs @@ -38,7 +38,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { public class LibraryProviderPlugin { - virtual public LibraryProvider CreateLibraryProvider(string parentProviderKey) + virtual public LibraryProvider CreateLibraryProvider(LibraryProvider parentLibraryProvider) { throw new NotImplementedException(); } diff --git a/PrintLibrary/Provider/LibraryProviderSelector.cs b/PrintLibrary/Provider/LibraryProviderSelector.cs index 28ca48ec6..72de3364c 100644 --- a/PrintLibrary/Provider/LibraryProviderSelector.cs +++ b/PrintLibrary/Provider/LibraryProviderSelector.cs @@ -40,49 +40,60 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { public class LibraryProviderSelector : LibraryProvider { + private static LibraryProviderSelector instance = null; private List libraryProviders = new List(); - private int selectedLibraryProvider = -1; - public LibraryProviderSelector(string parentProviderKey) - : base(parentProviderKey) + private LibraryProviderSelector() + : base(null) { // put in the sqlite provider - libraryProviders.Add(LibraryProviderSQLite.Instance); + libraryProviders.Add(new LibraryProviderSQLite(this)); // and any directory providers (sd card provider, etc...) - libraryProviders.Add(new LibraryProviderFileSystem(Path.Combine("C:\\", "Users", "LarsBrubaker", "Downloads"), "Downloads", this.ProviderKey)); + //libraryProviders.Add(new LibraryProviderFileSystem(Path.Combine("C:\\", "Users", "LarsBrubaker", "Downloads"), "Downloads", this)); //#if __ANDROID__ //libraryProviders.Add(new LibraryProviderFileSystem(ApplicationDataStorage.Instance.PublicDataStoragePath, "Downloads", this.ProviderKey)); - PrintItemCollection libraryCollection = new PrintItemCollection("Library Folder1", Path.Combine("C:\\", "Users", "LarsBrubaker", "AppData", "Local", "MatterControl", "Library")); - //libraryProviders.Add(new LibraryProviderFileSystem(libraryCollection, "Library Folder2", this.ProviderKey)); - // Check for LibraryProvider factories and put them in the list too. PluginFinder libraryProviderPlugins = new PluginFinder(); foreach (LibraryProviderPlugin libraryProviderPlugin in libraryProviderPlugins.Plugins) { - libraryProviders.Add(libraryProviderPlugin.CreateLibraryProvider(this.ProviderKey)); + libraryProviders.Add(libraryProviderPlugin.CreateLibraryProvider(this)); } providerLocationStack.Add(new PrintItemCollection("..", ProviderKey)); } + public static LibraryProviderSelector Instance + { + get + { + if (instance == null) + { + instance = new LibraryProviderSelector(); + } + + return instance; + } + } + #region Overriden Abstract Methods private List providerLocationStack = new List(); + public static string LibraryProviderSelectorKey + { + get + { + return "ProviderSelectorKey"; + } + } + public override int CollectionCount { get { - if (selectedLibraryProvider == -1) - { - return libraryProviders.Count; - } - else - { - return libraryProviders[selectedLibraryProvider].CollectionCount; - } + return libraryProviders.Count; } } @@ -90,14 +101,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { get { - if (selectedLibraryProvider == -1) - { - return 0; - } - else - { - return libraryProviders[selectedLibraryProvider].ItemCount; - } + return 0; } } @@ -105,25 +109,11 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { get { - if (selectedLibraryProvider == -1) - { - return ""; - } - else - { - return libraryProviders[selectedLibraryProvider].KeywordFilter; - } + return ""; } set { - if (selectedLibraryProvider == -1) - { - } - else - { - libraryProviders[selectedLibraryProvider].KeywordFilter = value; - } } } @@ -135,6 +125,11 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider } } + public override string ProviderData + { + get { return ""; } + } + public override string ProviderKey { get @@ -143,24 +138,9 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider } } - public static string LibraryProviderSelectorKey - { - get - { - return "ProviderSelectorKey"; - } - } - public override void AddCollectionToLibrary(string collectionName) { - if (selectedLibraryProvider == -1) - { - throw new NotImplementedException(); - } - else - { - libraryProviders[selectedLibraryProvider].AddCollectionToLibrary(collectionName); - } + throw new NotImplementedException(); } public override void AddFilesToLibrary(IList files, List providerSavePath, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null) @@ -178,85 +158,35 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public override PrintItemCollection GetCollectionItem(int collectionIndex) { - if (selectedLibraryProvider == -1) - { - LibraryProvider provider = libraryProviders[collectionIndex]; - return new PrintItemCollection(provider.Name, provider.ProviderKey); - } - else - { - return libraryProviders[selectedLibraryProvider].GetCollectionItem(collectionIndex); - } - } - - public override PrintItemCollection GetParentCollectionItem() - { - if (selectedLibraryProvider == -1) - { - return null; - } - else - { - return libraryProviders[selectedLibraryProvider].GetParentCollectionItem(); - } + LibraryProvider provider = libraryProviders[collectionIndex]; + return new PrintItemCollection(provider.Name, provider.ProviderKey); } public override PrintItemWrapper GetPrintItemWrapper(int itemIndex) { - if (selectedLibraryProvider == -1) + if (libraryProviders[0].ProviderKey != LibraryProviderSQLite.StaticProviderKey) { - if (libraryProviders[0].ProviderKey != LibraryProviderSQLite.StaticProviderKey) - { - throw new Exception("It is expected these are the same."); - } - return libraryProviders[0].GetPrintItemWrapper(itemIndex); - } - else - { - return libraryProviders[selectedLibraryProvider].GetPrintItemWrapper(itemIndex); + throw new Exception("It is expected these are the same."); } + return libraryProviders[0].GetPrintItemWrapper(itemIndex); } - // A key,value list that threads into the current collection loos like "key0,displayName0|key1,displayName1|key2,displayName2|...|keyN,displayNameN". - public override List GetProviderLocator() + public override LibraryProvider GetProviderForItem(PrintItemCollection collection) { - if (selectedLibraryProvider == -1) + foreach (LibraryProvider libraryProvider in libraryProviders) { - return new List(); - } - else - { - List providerPathNodes = new List(); - bool first = true; - - for (int i = 0; i < providerLocationStack.Count; i++) + if (collection.Key == libraryProvider.ProviderKey) { - PrintItemCollection collection = providerLocationStack[i]; - if (first) - { - providerPathNodes.Add(new ProviderLocatorNode(collection.Key, collection.Name)); - first = false; - } - else - { - providerPathNodes.Add(new ProviderLocatorNode(collection.Key, collection.Name)); - } + return libraryProvider; } - - return providerPathNodes; } + + throw new NotImplementedException(); } public override void RemoveCollection(string collectionName) { - if (selectedLibraryProvider == -1) - { - throw new NotImplementedException(); - } - else - { - libraryProviders[selectedLibraryProvider].RemoveCollection(collectionName); - } + throw new NotImplementedException(); } public override void RemoveItem(PrintItemWrapper printItemWrapper) @@ -269,61 +199,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public override void SaveToLibrary(PrintItemWrapper printItemWrapper, List meshGroupsToSave, List providerSavePath = null) { - if (selectedLibraryProvider == -1) - { - throw new NotImplementedException(); - } - else - { - List subProviderSavePath; - int libraryProviderToUseIndex = GetProviderIndex(printItemWrapper, out subProviderSavePath); - - libraryProviders[libraryProviderToUseIndex].SaveToLibrary(printItemWrapper, meshGroupsToSave, subProviderSavePath); - } - } - - public override void SetCollectionBase(PrintItemCollection collectionBase) - { - // This logic may need to be move legitamately into the virtual functions of the providers rather than all - // gathered up here. If you find that this is not working the way you want ask me. LBB - if ((providerLocationStack.Count > 2 - && collectionBase.Key == providerLocationStack[providerLocationStack.Count - 2].Key) - || (providerLocationStack.Count > 1 - && selectedLibraryProvider != -1 - && collectionBase.Key == libraryProviders[selectedLibraryProvider].GetParentCollectionItem().Key) - ) - { - providerLocationStack.RemoveAt(providerLocationStack.Count - 1); - } - else - { - providerLocationStack.Add(collectionBase); - } - - if (collectionBase.Key == this.ProviderKey) - { - selectedLibraryProvider = -1; - } - else - { - bool wasSet = false; - for (int i = 0; i < libraryProviders.Count; i++) - { - if (libraryProviders[i].ProviderKey == collectionBase.Key) - { - selectedLibraryProvider = i; - wasSet = true; - break; - } - } - - if (!wasSet) - { - libraryProviders[selectedLibraryProvider].SetCollectionBase(collectionBase); - } - } - - CollectionChanged.CallEvents(this, null); + throw new NotImplementedException(); } private int GetProviderIndex(PrintItemWrapper printItemWrapper, out List subProviderSavePath) @@ -355,5 +231,10 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider } #endregion Overriden Abstract Methods + + public static LibraryProvider GetProviderForItem(PrintItemWrapper printItemWrapper) + { + throw new NotImplementedException(); + } } } \ No newline at end of file diff --git a/PrintLibrary/Provider/LibraryProviderSqlite.cs b/PrintLibrary/Provider/LibraryProviderSqlite.cs index bf0926e89..b21e955e9 100644 --- a/PrintLibrary/Provider/LibraryProviderSqlite.cs +++ b/PrintLibrary/Provider/LibraryProviderSqlite.cs @@ -47,25 +47,26 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { private static LibraryProviderSQLite instance = null; private static PrintItemCollection rotLibraryCollection; + private List childCollections = new List(); private PrintItemCollection collectionBase = GetRootLibraryCollection(); private string keywordFilter = string.Empty; private List printItems = new List(); - public LibraryProviderSQLite() - : base(LibraryProviderSelector.LibraryProviderSelectorKey) + public LibraryProviderSQLite(LibraryProvider parentLibraryProvider) + : base(parentLibraryProvider) { LoadLibraryItems(); } - public new static LibraryProviderSQLite Instance + public static LibraryProvider Instance { get { if (instance == null) { - instance = new LibraryProviderSQLite(); + instance = new LibraryProviderSQLite(null); } return instance; @@ -117,6 +118,11 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider } } + public override string ProviderData + { + get { throw new NotImplementedException(); } + } + public override string ProviderKey { get @@ -153,7 +159,8 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider if (itemsToAdd.Length > 0) { // Import any files sync'd to disk into the library, then add them to the queue - Instance.AddFilesToLibrary(itemsToAdd); + LibraryProviderSQLite rootLibrary = new LibraryProviderSQLite(null); + rootLibrary.AddFilesToLibrary(itemsToAdd); } } return rotLibraryCollection; @@ -198,7 +205,8 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider return calibrationPrintFileNames.Where(fileName => { // Filter out items that already exist in the library - return Instance.GetLibraryItems(Path.GetFileNameWithoutExtension(fileName)).Count() <= 0; + LibraryProviderSQLite rootLibrary = new LibraryProviderSQLite(null); + return rootLibrary.GetLibraryItems(Path.GetFileNameWithoutExtension(fileName)).Count() <= 0; }).Select(fileName => { // Copy calibration prints from StaticData to the filesystem before importing into the library @@ -253,7 +261,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider } printItems.Insert(indexToInsert, item); // Check if the collection we are adding to is the the currently visible collection. - List currentDisplayedCollection = LibraryProvider.Instance.GetProviderLocator(); + List currentDisplayedCollection = GetProviderLocator(); if (currentDisplayedCollection.Count > 0 && currentDisplayedCollection[1].Key == LibraryProviderSQLite.StaticProviderKey) { OnItemAdded(new IndexArgs(indexToInsert)); @@ -267,18 +275,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider return childCollections[collectionIndex]; } - public override PrintItemCollection GetParentCollectionItem() - { - if (ParentProviderKey != null) - { - return new PrintItemCollection("..", ParentProviderKey); - } - else - { - return null; - } - } - public override PrintItemWrapper GetPrintItemWrapper(int index) { if (index >= 0 && index < printItems.Count) @@ -289,7 +285,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider return null; } - public override List GetProviderLocator() + public override LibraryProvider GetProviderForItem(PrintItemCollection collection) { throw new NotImplementedException(); } @@ -337,14 +333,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider throw new NotImplementedException(); } - public override void SetCollectionBase(PrintItemCollection collectionBase) - { - this.collectionBase = collectionBase; - - LoadLibraryItems(); - } - - private static void AddStlOrGcode(string loadedFileName, string extension) + private static void AddStlOrGcode(LibraryProvider libraryToAddTo, string loadedFileName, string extension) { PrintItem printItem = new PrintItem(); printItem.Name = Path.GetFileNameWithoutExtension(loadedFileName); @@ -360,7 +349,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { PrintItemWrapper printItemWrapper = new PrintItemWrapper(printItem); SaveToLibraryFolder(printItemWrapper, meshToConvertAndSave, false); - Instance.AddItem(printItemWrapper); + libraryToAddTo.AddItem(printItemWrapper); } catch (System.UnauthorizedAccessException) { @@ -383,7 +372,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider PrintItemWrapper printItemWrapper = new PrintItemWrapper(printItem); if (false) { - Instance.AddItem(printItemWrapper); + libraryToAddTo.AddItem(printItemWrapper); } else // save a copy to the library and update this to point at it { @@ -397,7 +386,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider printItemWrapper.PrintItem.Commit(); // let the queue know that the item has changed so it load the correct part - Instance.AddItem(printItemWrapper); + libraryToAddTo.AddItem(printItemWrapper); } } } @@ -453,13 +442,13 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { foreach (PrintItem part in partFiles) { - AddStlOrGcode(part.FileLocation, Path.GetExtension(part.FileLocation).ToUpper()); + AddStlOrGcode(this, part.FileLocation, Path.GetExtension(part.FileLocation).ToUpper()); } } } else { - AddStlOrGcode(loadedFileName, extension); + AddStlOrGcode(this, loadedFileName, extension); } } } diff --git a/PrintLibrary/Provider/LibrarySQLiteData.cs b/PrintLibrary/Provider/LibrarySQLiteData.cs index 8c7a151d8..1a7a8509f 100644 --- a/PrintLibrary/Provider/LibrarySQLiteData.cs +++ b/PrintLibrary/Provider/LibrarySQLiteData.cs @@ -122,7 +122,7 @@ namespace MatterHackers.MatterControl.PrintLibrary } PrintItems.Insert(indexToInsert, item); // Check if the collection we are adding to is the the currently visible collection. - List currentDisplayedCollection = LibraryProvider.Instance.GetProviderLocator(); + List currentDisplayedCollection = LibraryProviderSQLite.Instance.GetProviderLocator(); if (currentDisplayedCollection.Count > 0 && currentDisplayedCollection[1].Key == LibraryProviderSQLite.StaticProviderKey) { OnItemAdded(new IndexArgs(indexToInsert)); diff --git a/PrintQueue/QueueDataWidget.cs b/PrintQueue/QueueDataWidget.cs index a01a2a9c5..e96c3f79d 100644 --- a/PrintQueue/QueueDataWidget.cs +++ b/PrintQueue/QueueDataWidget.cs @@ -460,7 +460,7 @@ namespace MatterHackers.MatterControl.PrintQueue { foreach (QueueRowItem queueItem in queueDataView.SelectedItems) { - LibraryProvider.Instance.AddItem(queueItem.PrintItemWrapper); + LibraryDataView.CurrentLibraryProvider.AddItem(queueItem.PrintItemWrapper); } } diff --git a/Tests/MatterControl.Tests/MatterControl.Tests.csproj b/Tests/MatterControl.Tests/MatterControl.Tests.csproj index 1214f30ba..01cc0291b 100644 --- a/Tests/MatterControl.Tests/MatterControl.Tests.csproj +++ b/Tests/MatterControl.Tests/MatterControl.Tests.csproj @@ -57,6 +57,7 @@ + diff --git a/Tests/MatterControl.Tests/MatterControl/LibraryProviderTests.cs b/Tests/MatterControl.Tests/MatterControl/LibraryProviderTests.cs new file mode 100644 index 000000000..8fff8cf9b --- /dev/null +++ b/Tests/MatterControl.Tests/MatterControl/LibraryProviderTests.cs @@ -0,0 +1,123 @@ +/* +Copyright (c) 2015, Lars Brubaker +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +The views and conclusions contained in the software and documentation are those +of the authors and should not be interpreted as representing official policies, +either expressed or implied, of the FreeBSD Project. +*/ + +using MatterHackers.MatterControl; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Reflection; +using System.Text; +using MatterHackers.MatterControl.PrintLibrary.Provider; +using MatterHackers.MatterControl.PrintQueue; +using MatterHackers.MatterControl.DataStorage; +using System.IO; +using MatterHackers.Agg.PlatformAbstract; + +namespace MatterControl.Tests +{ + [TestFixture] + public class LibraryProviderTests + { + static string pathToMesh = Path.Combine("..", "..", "..", "TestData", "TestMeshes", "LibraryProviderData"); + static string meshFileName = Path.Combine(pathToMesh, "Box20x20x10.stl"); + + bool collectionChanged = false; + bool dataReloaded = false; + bool itemAdded = false; + bool itemRemoved = false; + private event EventHandler unregisterEvents; + + public LibraryProviderTests() + { +#if !__ANDROID__ + // Set the static data to point to the directory of MatterControl + StaticData.Instance = new MatterHackers.Agg.FileSystemStaticData(Path.Combine("..", "..", "..", "..")); +#endif + } + + [TestFixtureSetUp] + void SetupBeforeTest() + { + collectionChanged = false; + dataReloaded = false; + itemAdded = false; + itemRemoved = false; + + LibraryProvider.CollectionChanged.RegisterEvent((sender, e) => { collectionChanged = true; }, ref unregisterEvents); + LibraryProvider.DataReloaded.RegisterEvent((sender, e) => { dataReloaded = true; }, ref unregisterEvents); + LibraryProvider.ItemAdded.RegisterEvent((sender, e) => { itemAdded = true; }, ref unregisterEvents); + LibraryProvider.ItemRemoved.RegisterEvent((sender, e) => { itemRemoved = true; }, ref unregisterEvents); + } + + [TearDown] + void TeardownAfterTest() + { + unregisterEvents(this, null); + } + + [Test, Category("LibraryProviderFileSystem")] + public void LibraryProviderFileSystem_NavigationWorking() + { + LibraryProviderFileSystem testProvider = new LibraryProviderFileSystem(pathToMesh, "TestPath", null); + Assert.IsTrue(testProvider.CollectionCount == 0, "Start with a new database for these tests."); + Assert.IsTrue(testProvider.ItemCount == 1, "Start with a new database for these tests."); + PrintItemWrapper itemAtRoot = testProvider.GetPrintItemWrapper(0); + List providerLocator = itemAtRoot.PrintItem.GetLibraryProviderLocator(); + Assert.IsTrue(providerLocator.Count == 1); + + // create a collection and make sure it is on disk + string collectionName = "Collection1"; + string createdDirectory = Path.Combine(pathToMesh, collectionName); + Assert.IsTrue(!Directory.Exists(createdDirectory)); + Assert.IsTrue(collectionChanged == false); + testProvider.AddCollectionToLibrary(collectionName); + Assert.IsTrue(collectionChanged == true); + Assert.IsTrue(Directory.Exists(createdDirectory)); + + collectionChanged = false; + // make sure removing it gets rid of it + Assert.IsTrue(collectionChanged == false); + testProvider.RemoveCollection("Collection1"); + Assert.IsTrue(collectionChanged == true); + Assert.IsTrue(!Directory.Exists(createdDirectory)); + } + + [Test, Category("LibraryProviderSqlite")] + public void LibraryProviderSqlite_NavigationWorking() + { + LibraryProviderSQLite testProvider = new LibraryProviderSQLite(null); + Assert.IsTrue(testProvider.CollectionCount == 0, "Start with a new database for these tests."); + Assert.IsTrue(testProvider.ItemCount == 0, "Start with a new database for these tests."); + PrintItem printItem = new PrintItem("Test_RootItem", meshFileName); + testProvider.AddItem(new PrintItemWrapper(printItem)); + } + } +} diff --git a/Tests/TestData/TestMeshes/LibraryProviderData/Box20x20x10.stl b/Tests/TestData/TestMeshes/LibraryProviderData/Box20x20x10.stl new file mode 100644 index 0000000000000000000000000000000000000000..dfce443a259c05e8f3c4823aa2e9408959a5eb54 GIT binary patch literal 684 zcmZReGT^~5PvIZ~0~k9Z(J=Nw4CM?^RZtnYPM8iyd^!|Bsz4ZICx}MEsQNI>B~Ay- zRUmWG*dV*H*a0&aL=&e2>O+uuAPh1S>{_U95Fde&Wk9My7$gg#L2@8ADh7#yWkG&G p#vm6$?Z&PHB#KQZNE{V|RYBbgW<$+|u|YI;9UvJnM$-)v0|0i1G$#N6 literal 0 HcmV?d00001