diff --git a/Library/LibraryDataView.cs b/Library/LibraryDataView.cs index 612ac1aa4..7825c4245 100644 --- a/Library/LibraryDataView.cs +++ b/Library/LibraryDataView.cs @@ -290,7 +290,6 @@ namespace MatterHackers.MatterControl.PrintLibrary itemHolder.MouseEnterBounds += new EventHandler(itemToAdd_MouseEnterBounds); itemHolder.MouseLeaveBounds += new EventHandler(itemToAdd_MouseLeaveBounds); - itemHolder.MouseDownInBounds += itemHolder_MouseDownInBounds; itemHolder.ParentChanged += new EventHandler(itemHolder_ParentChanged); } @@ -438,24 +437,27 @@ namespace MatterHackers.MatterControl.PrintLibrary { topToBottomItemList.RemoveAllChildren(); - if (LibraryDataView.CurrentLibraryProvider.ParentLibraryProvider != null) + var provider = LibraryDataView.CurrentLibraryProvider; + + if (provider != null) { - PrintItemCollection parent = new PrintItemCollection("..", LibraryDataView.CurrentLibraryProvider.ProviderKey); - LibraryRowItem queueItem = new LibraryRowItemCollection(parent, this, LibraryDataView.CurrentLibraryProvider.ParentLibraryProvider, GetThumbnailWidget(true, LibraryDataView.CurrentLibraryProvider.ParentLibraryProvider, parent)); + PrintItemCollection parent = new PrintItemCollection("..", provider.ProviderKey); + LibraryRowItem queueItem = new LibraryRowItemCollection(parent, this, provider.ParentLibraryProvider, GetThumbnailWidget(true, provider.ParentLibraryProvider, parent)); AddListItemToTopToBottom(queueItem); } - for (int i = 0; i < LibraryDataView.CurrentLibraryProvider.CollectionCount; i++) + for (int i = 0; i < provider.CollectionCount; i++) { - PrintItemCollection item = LibraryDataView.CurrentLibraryProvider.GetCollectionItem(i); + PrintItemCollection item = provider.GetCollectionItem(i); LibraryRowItem queueItem = new LibraryRowItemCollection(item, this, null, GetThumbnailWidget(false, null, item)); AddListItemToTopToBottom(queueItem); } - for (int i = 0; i < LibraryDataView.CurrentLibraryProvider.ItemCount; i++) + for (int i = 0; i < provider.ItemCount; i++) { - GuiWidget thumbnailWidget = LibraryDataView.CurrentLibraryProvider.GetItemThumbnail(i); - LibraryRowItem queueItem = new LibraryRowItemPart(LibraryDataView.CurrentLibraryProvider, i, this, thumbnailWidget); + GuiWidget thumbnailWidget = provider.GetItemThumbnail(i); + LibraryRowItem queueItem = new LibraryRowItemPart(provider, i, this, thumbnailWidget); + AddListItemToTopToBottom(queueItem); } } @@ -465,16 +467,11 @@ namespace MatterHackers.MatterControl.PrintLibrary HoverIndex = -1; } - 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); } diff --git a/Library/LibraryRowItem.cs b/Library/LibraryRowItem.cs index fe5b88244..9f69c16c0 100644 --- a/Library/LibraryRowItem.cs +++ b/Library/LibraryRowItem.cs @@ -33,6 +33,7 @@ using MatterHackers.Agg.VertexSource; using MatterHackers.VectorMath; using System; using System.Globalization; +using System.Threading.Tasks; namespace MatterHackers.MatterControl.PrintLibrary { @@ -52,13 +53,15 @@ namespace MatterHackers.MatterControl.PrintLibrary private ConditionalClickWidget primaryClickContainer; private GuiWidget thumbnailWidget; + private event EventHandler unregisterEvents; + public LibraryRowItem(LibraryDataView libraryDataView, GuiWidget thumbnailWidget) { this.thumbnailWidget = thumbnailWidget; this.libraryDataView = libraryDataView; } - private event EventHandler unregisterEvents; + public string ItemName { get; protected set; } public bool EditMode { @@ -183,9 +186,7 @@ namespace MatterHackers.MatterControl.PrintLibrary middleColumn.VAnchor = Agg.UI.VAnchor.ParentBottomTop; middleColumn.Margin = new BorderDouble(10, 6); { - string labelName = GetItemName(); - labelName = labelName.Replace('_', ' '); - partLabel = new TextWidget(labelName, pointSize: 14); + partLabel = new TextWidget(this.ItemName.Replace('_', ' '), pointSize: 14); partLabel.TextColor = WidgetTextColor; partLabel.MinimumSize = new Vector2(1, 18); partLabel.VAnchor = VAnchor.ParentCenter; @@ -227,8 +228,6 @@ namespace MatterHackers.MatterControl.PrintLibrary protected abstract SlideWidget GetItemActionButtons(); - protected abstract string GetItemName(); - protected abstract void RemoveThisFromPrintLibrary(); #endregion Abstract Functions diff --git a/Library/LibraryRowItemCollection.cs b/Library/LibraryRowItemCollection.cs index 624e0f9ad..2ac28a735 100644 --- a/Library/LibraryRowItemCollection.cs +++ b/Library/LibraryRowItemCollection.cs @@ -54,6 +54,8 @@ namespace MatterHackers.MatterControl.PrintLibrary { this.parentProvider = parentProvider; this.printItemCollection = collection; + this.ItemName = printItemCollection.Name; + CreateGuiElements(); } @@ -115,11 +117,6 @@ namespace MatterHackers.MatterControl.PrintLibrary private ConditionalClickWidget primaryClickContainer; - protected override string GetItemName() - { - return printItemCollection.Name; - } - protected override SlideWidget GetItemActionButtons() { SlideWidget buttonContainer = new SlideWidget(); diff --git a/Library/LibraryRowItemPart.cs b/Library/LibraryRowItemPart.cs index 95e85cdc9..6a81dcfad 100644 --- a/Library/LibraryRowItemPart.cs +++ b/Library/LibraryRowItemPart.cs @@ -39,6 +39,7 @@ using MatterHackers.PolygonMesh; using System; using System.Collections.Generic; using System.IO; +using System.Threading.Tasks; namespace MatterHackers.MatterControl.PrintLibrary { @@ -46,21 +47,9 @@ namespace MatterHackers.MatterControl.PrintLibrary { public bool isActivePrint = false; LibraryProvider libraryProvider; - int itemIndex; - private PrintItemWrapper printItemWrapper; - public PrintItemWrapper PrintItemWrapper - { - get - { - if (printItemWrapper == null) - { - printItemWrapper = libraryProvider.GetPrintItemWrapper(itemIndex); - } - return printItemWrapper; - } - } + private int itemIndex; - //public override PrintItemCollection PrintItemCollection { get { return null; } } + public PrintItemWrapper printItemInstance = null; private ExportPrintItemWindow exportingWindow; private PartPreviewMainWindow viewingWindow; @@ -68,8 +57,10 @@ namespace MatterHackers.MatterControl.PrintLibrary public LibraryRowItemPart(LibraryProvider libraryProvider, int itemIndex, LibraryDataView libraryDataView, GuiWidget thumbnailWidget) : base(libraryDataView, thumbnailWidget) { + this.ItemName = libraryProvider.GetPrintItemName(itemIndex); this.libraryProvider = libraryProvider; this.itemIndex = itemIndex; + CreateGuiElements(); } @@ -81,21 +72,36 @@ namespace MatterHackers.MatterControl.PrintLibrary } } - public override void AddToQueue() + public async Task GetPrintItemWrapperAsync() { - // create a new item that will be only in the queue - QueueData.Instance.AddItem(MakeCopyForQueue()); + if (printItemInstance == null) + { + printItemInstance = await libraryProvider.GetPrintItemWrapperAsync(this.itemIndex); + } + + return printItemInstance; } - private PrintItemWrapper MakeCopyForQueue() + public async override void AddToQueue() { - PrintItem printItemToCopy = PrintItemWrapper.PrintItem; + // create a new item that will be only in the queue + QueueData.Instance.AddItem(await MakeCopyForQueue()); + } + + private async Task MakeCopyForQueue() + { + var printItemWrapper = await this.GetPrintItemWrapperAsync(); + + PrintItem printItemToCopy = printItemWrapper.PrintItem; string fileName = Path.ChangeExtension(Path.GetRandomFileName(), Path.GetExtension(printItemToCopy.FileLocation)); string newFileLocation = Path.Combine(ApplicationDataStorage.Instance.ApplicationLibraryDataPath, fileName); + File.Copy(printItemToCopy.FileLocation, newFileLocation); - PrintItem printItemForQueue = new PrintItem(printItemToCopy.Name, newFileLocation); - printItemForQueue.Protected = printItemToCopy.Protected; - return new PrintItemWrapper(printItemForQueue); + + return new PrintItemWrapper(new PrintItem(printItemToCopy.Name, newFileLocation) + { + Protected = printItemToCopy.Protected + }); } public override void Edit() @@ -103,9 +109,9 @@ namespace MatterHackers.MatterControl.PrintLibrary OpenPartViewWindow(PartPreviewWindow.View3DWidget.OpenMode.Editing); } - public override void Export() + public async override void Export() { - OpenExportWindow(PrintItemWrapper); + OpenExportWindow(await this.GetPrintItemWrapperAsync()); } public override void OnDraw(Graphics2D graphics2D) @@ -159,11 +165,12 @@ namespace MatterHackers.MatterControl.PrintLibrary base.OnMouseDown(mouseEvent); } - public void OpenPartViewWindow(View3DWidget.OpenMode openMode = View3DWidget.OpenMode.Viewing) + public async void OpenPartViewWindow(View3DWidget.OpenMode openMode = View3DWidget.OpenMode.Viewing) { if (viewingWindow == null) { - viewingWindow = new PartPreviewMainWindow(this.PrintItemWrapper, View3DWidget.AutoRotate.Enabled, openMode); + var printItemWrapper = await this.GetPrintItemWrapperAsync(); + viewingWindow = new PartPreviewMainWindow(printItemWrapper, View3DWidget.AutoRotate.Enabled, openMode); viewingWindow.Closed += new EventHandler(PartPreviewMainWindow_Closed); } else @@ -172,9 +179,9 @@ namespace MatterHackers.MatterControl.PrintLibrary } } - public override void RemoveFromCollection() + public async override void RemoveFromCollection() { - LibraryDataView.CurrentLibraryProvider.RemoveItem(PrintItemWrapper); + LibraryDataView.CurrentLibraryProvider.RemoveItem(await this.GetPrintItemWrapperAsync()); } protected override SlideWidget GetItemActionButtons() @@ -194,21 +201,9 @@ namespace MatterHackers.MatterControl.PrintLibrary printButton.VAnchor = VAnchor.ParentBottomTop; printButton.BackgroundColor = ActiveTheme.Instance.PrimaryAccentColor; printButton.Width = 100; - printButton.Click += (sender, e) => - { - if (!PrinterCommunication.PrinterConnectionAndCommunication.Instance.PrintIsActive) - { - QueueData.Instance.AddItem(MakeCopyForQueue(), 0); - QueueData.Instance.SelectedIndex = QueueData.Instance.Count - 1; - PrinterCommunication.PrinterConnectionAndCommunication.Instance.PrintActivePartIfPossible(); - } - else - { - QueueData.Instance.AddItem(MakeCopyForQueue()); - } - buttonContainer.SlideOut(); - this.Invalidate(); - }; ; + printButton.Click += printButton_Click; + // HACK: No clear immediate workaround beyond this + printButton.Click += (s, e) => buttonContainer.SlideOut(); TextWidget viewButtonLabel = new TextWidget("View".Localize()); viewButtonLabel.TextColor = RGBA_Bytes.White; @@ -230,14 +225,27 @@ namespace MatterHackers.MatterControl.PrintLibrary return buttonContainer; } - protected override string GetItemName() + private async void printButton_Click(object sender, EventArgs e) { - return libraryProvider.GetItemName(itemIndex); + var newItem = await MakeCopyForQueue(); + + if (!PrinterCommunication.PrinterConnectionAndCommunication.Instance.PrintIsActive) + { + QueueData.Instance.AddItem(newItem, 0); + QueueData.Instance.SelectedIndex = QueueData.Instance.Count - 1; + PrinterCommunication.PrinterConnectionAndCommunication.Instance.PrintActivePartIfPossible(); + } + else + { + QueueData.Instance.AddItem(newItem); + } + this.Invalidate(); } - protected override void RemoveThisFromPrintLibrary() + protected async override void RemoveThisFromPrintLibrary() { - LibraryDataView.CurrentLibraryProvider.RemoveItem(this.PrintItemWrapper); + // TODO: The LibraryProvider does not need a printitemwrapper to remove an item! Why not an interger like the others? + LibraryDataView.CurrentLibraryProvider.RemoveItem(await this.GetPrintItemWrapperAsync()); } private void ExportQueueItemWindow_Closed(object sender, EventArgs e) @@ -308,11 +316,11 @@ namespace MatterHackers.MatterControl.PrintLibrary }); } - private void OpenExportWindow() + private async void OpenExportWindow() { if (exportingWindow == null) { - exportingWindow = new ExportPrintItemWindow(this.PrintItemWrapper); + exportingWindow = new ExportPrintItemWindow(await this.GetPrintItemWrapperAsync()); exportingWindow.Closed += new EventHandler(ExportQueueItemWindow_Closed); exportingWindow.ShowAsSystemWindow(); } @@ -336,9 +344,11 @@ namespace MatterHackers.MatterControl.PrintLibrary } } - private void openPartView(View3DWidget.OpenMode openMode = View3DWidget.OpenMode.Viewing) + private async void openPartView(View3DWidget.OpenMode openMode = View3DWidget.OpenMode.Viewing) { - string pathAndFile = this.PrintItemWrapper.FileLocation; + var printItemWrapper = await this.GetPrintItemWrapperAsync(); + + string pathAndFile = printItemWrapper.FileLocation; if (File.Exists(pathAndFile)) { OpenPartViewWindow(openMode); diff --git a/Library/PrintLibraryWidget.cs b/Library/PrintLibraryWidget.cs index 1b9a9c8ed..4d9000691 100644 --- a/Library/PrintLibraryWidget.cs +++ b/Library/PrintLibraryWidget.cs @@ -411,7 +411,7 @@ namespace MatterHackers.MatterControl.PrintLibrary SetEditButtonsStates(); } - private void SetEditButtonsStates() + private async void SetEditButtonsStates() { int selectedCount = libraryDataView.SelectedItems.Count; diff --git a/Library/Provider/LibraryProvider.cs b/Library/Provider/LibraryProvider.cs index a0cdbc4b8..c89eee0ce 100644 --- a/Library/Provider/LibraryProvider.cs +++ b/Library/Provider/LibraryProvider.cs @@ -35,6 +35,7 @@ using MatterHackers.PolygonMesh; using System; using System.Collections.Generic; using System.ComponentModel; +using System.Threading.Tasks; namespace MatterHackers.MatterControl.PrintLibrary.Provider { @@ -106,7 +107,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public abstract PrintItemCollection GetCollectionItem(int collectionIndex); - public abstract PrintItemWrapper GetPrintItemWrapper(int itemIndex); + public abstract Task GetPrintItemWrapperAsync(int itemIndex); public abstract LibraryProvider GetProviderForItem(PrintItemCollection collection); @@ -127,9 +128,9 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider #endregion Static Methods - public virtual string GetItemName(int printItemIndex) + public virtual string GetPrintItemName(int itemIndex) { - return GetPrintItemWrapper(printItemIndex).Name; + return ""; } public virtual bool IsItemProtected(int itemIndex) @@ -144,8 +145,8 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public virtual GuiWidget GetItemThumbnail(int printItemIndex) { - PartThumbnailWidget thumbnailWidget = new PartThumbnailWidget(GetPrintItemWrapper(printItemIndex), "part_icon_transparent_40x40.png", "building_thumbnail_40x40.png", PartThumbnailWidget.ImageSizes.Size50x50); - return thumbnailWidget; + var printItemWrapper = GetPrintItemWrapperAsync(printItemIndex).Result; + return new PartThumbnailWidget(printItemWrapper, "part_icon_transparent_40x40.png", "building_thumbnail_40x40.png", PartThumbnailWidget.ImageSizes.Size50x50); } } diff --git a/Library/Provider/LibraryProviderFileSystem.cs b/Library/Provider/LibraryProviderFileSystem.cs index 2d1f635c6..928125fc7 100644 --- a/Library/Provider/LibraryProviderFileSystem.cs +++ b/Library/Provider/LibraryProviderFileSystem.cs @@ -39,6 +39,7 @@ using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Threading; +using System.Threading.Tasks; namespace MatterHackers.MatterControl.PrintLibrary.Provider { @@ -171,11 +172,18 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider return new PrintItemCollection(Path.GetFileNameWithoutExtension(directoryName), Path.Combine(rootPath, directoryName)); } - public override PrintItemWrapper GetPrintItemWrapper(int itemIndex) + public override string GetPrintItemName(int itemIndex) + { + return Path.GetFileName(currentDirectoryFiles[itemIndex]); + } + + public async override Task GetPrintItemWrapperAsync(int itemIndex) { string fileName = currentDirectoryFiles[itemIndex]; + List providerLocator = GetProviderLocator(); string providerLocatorJson = JsonConvert.SerializeObject(providerLocator); + return new PrintItemWrapper(new DataStorage.PrintItem(Path.GetFileNameWithoutExtension(fileName), fileName, providerLocatorJson)); } diff --git a/Library/Provider/LibraryProviderSelector.cs b/Library/Provider/LibraryProviderSelector.cs index d95fab833..a5bf33b67 100644 --- a/Library/Provider/LibraryProviderSelector.cs +++ b/Library/Provider/LibraryProviderSelector.cs @@ -38,6 +38,7 @@ using MatterHackers.Localizations; using System.IO; using System.Linq; using MatterHackers.Agg.UI; +using System.Threading.Tasks; namespace MatterHackers.MatterControl.PrintLibrary.Provider { @@ -213,13 +214,9 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider return new PrintItemCollection(provider.Name, provider.ProviderKey); } - public override PrintItemWrapper GetPrintItemWrapper(int itemIndex) + public async override Task GetPrintItemWrapperAsync(int itemIndex) { - if (visibleProviders[0].ProviderKey != LibraryProviderSQLite.StaticProviderKey) - { - throw new Exception("It is expected these are the same."); - } - return visibleProviders[0].GetPrintItemWrapper(itemIndex); + throw new NotImplementedException("Print items are not allowed at the root level"); } public override LibraryProvider GetProviderForItem(PrintItemCollection collection) diff --git a/Library/Provider/LibraryProviderSqlite.cs b/Library/Provider/LibraryProviderSqlite.cs index 9553db3ad..2d4f3dfbd 100644 --- a/Library/Provider/LibraryProviderSqlite.cs +++ b/Library/Provider/LibraryProviderSqlite.cs @@ -81,6 +81,11 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider } } + public override string GetPrintItemName(int itemIndex) + { + return ""; + } + public static string StaticProviderKey { get @@ -301,7 +306,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider return childCollections[collectionIndex]; } - public override PrintItemWrapper GetPrintItemWrapper(int index) + public async override Task GetPrintItemWrapperAsync(int index) { if (index >= 0 && index < printItems.Count) { diff --git a/Tests/MatterControl.Tests/MatterControl/LibraryProviderTests.cs b/Tests/MatterControl.Tests/MatterControl/LibraryProviderTests.cs index 70a7c6a85..1484147c7 100644 --- a/Tests/MatterControl.Tests/MatterControl/LibraryProviderTests.cs +++ b/Tests/MatterControl.Tests/MatterControl/LibraryProviderTests.cs @@ -78,7 +78,7 @@ namespace MatterControl.Tests Assert.IsTrue(dataReloaded == true); Assert.IsTrue(Directory.Exists(createdDirectory)); - PrintItemWrapper itemAtRoot = testProvider.GetPrintItemWrapper(0); + PrintItemWrapper itemAtRoot = testProvider.GetPrintItemWrapperAsync(0).Result; List providerLocator = itemAtRoot.PrintItem.GetLibraryProviderLocator(); Assert.IsTrue(providerLocator.Count == 1); @@ -105,7 +105,7 @@ namespace MatterControl.Tests // remove item works dataReloaded = false; Assert.IsTrue(dataReloaded == false); - subProvider.RemoveItem(subProvider.GetPrintItemWrapper(0)); + subProvider.RemoveItem(subProvider.GetPrintItemWrapperAsync(0).Result); Assert.IsTrue(dataReloaded == true); Assert.IsTrue(!File.Exists(subPathAndFile)); @@ -137,7 +137,7 @@ namespace MatterControl.Tests Assert.IsTrue(dataReloaded == true); Assert.IsTrue(NamedCollectionExists(collectionName)); // assert that the record does exist in the DB - PrintItemWrapper itemAtRoot = testProvider.GetPrintItemWrapper(0); + PrintItemWrapper itemAtRoot = testProvider.GetPrintItemWrapperAsync(0).Result; List providerLocator = itemAtRoot.PrintItem.GetLibraryProviderLocator(); Assert.IsTrue(providerLocator.Count == 1); @@ -159,7 +159,7 @@ namespace MatterControl.Tests // remove item works dataReloaded = false; Assert.IsTrue(dataReloaded == false); - testProvider.RemoveItem(testProvider.GetPrintItemWrapper(1)); + testProvider.RemoveItem(testProvider.GetPrintItemWrapperAsync(1).Result); Assert.IsTrue(dataReloaded == true); Assert.IsTrue(!NamedItemExists(fileNameWithExtension));