Skip to content

Commit

Permalink
Add: Ability to copy-paste collections between instances
Browse files Browse the repository at this point in the history
Closes #41
  • Loading branch information
Piotrekol committed Feb 5, 2022
1 parent 83f348b commit 90ba0f0
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 31 deletions.
17 changes: 17 additions & 0 deletions App/Presenters/Controls/CollectionListingPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
using CollectionManager.Modules.CollectionsManager;
using App.Interfaces;
using GuiComponents.Interfaces;
using System.IO;
using System.Windows.Forms;
using System.Collections.Specialized;
using App.Misc;

namespace App.Presenters.Controls
{
Expand Down Expand Up @@ -95,6 +99,19 @@ private void _view_RightClick(object sender, Gui.Misc.StringEventArgs e)

args = CollectionEditArgs.DuplicateCollection(_view.SelectedCollection);
break;
case "Copy":
if (selectedCollections == null)
return;
var tempFolder = Path.Combine(Path.GetTempPath(), "CMcollections");
if (Directory.Exists(tempFolder))
Directory.Delete(tempFolder, true);

Directory.CreateDirectory(tempFolder);
var fileName = Helpers.StripInvalidFileNameCharacters(selectedCollections[0].Name, "_");
var tempLocation = Path.Combine(tempFolder, $"{fileName}.osdb");
Initalizer.OsuFileIo.CollectionLoader.SaveOsdbCollection(selectedCollections, tempLocation);
Clipboard.SetFileDropList(new StringCollection { tempLocation });
return;
default:
return;
}
Expand Down
16 changes: 8 additions & 8 deletions GuiComponents/Controls/BeatmapListingView.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 33 additions & 8 deletions GuiComponents/Controls/CollectionListingView.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 53 additions & 15 deletions GuiComponents/Controls/CollectionListingView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,37 @@ private void Bind()
}

private CollectionRenderer _collectionRenderer = new CollectionRenderer();
private RearrangingDropSink _dropsink = new RearrangingDropSink();

private void init()
{
//ListViewCollections.SelectedIndexChanged += ListViewCollectionsSelectedIndexChanged;
ListViewCollections.UseFiltering = true;
ListViewCollections.FullRowSelect = true;
ListViewCollections.HideSelection = false;
ListViewCollections.DefaultRenderer = _collectionRenderer;

var dropsink = new RearrangingDropSink();
dropsink.CanDropBetween = false;
dropsink.CanDropOnItem = true;
dropsink.CanDropOnSubItem = false;
dropsink.CanDropOnBackground = false;
ListViewCollections.DropSink = dropsink;

_dropsink.CanDropBetween = false;
_dropsink.CanDropOnItem = true;
_dropsink.CanDropOnSubItem = false;
_dropsink.CanDropOnBackground = false;
ListViewCollections.DropSink = _dropsink;
ListViewCollections.ModelDropped += ListViewCollections_ModelDropped;
ListViewCollections.CellRightClick += ListViewCollectionsOnCellRightClick;
dropsink.ModelCanDrop += DropsinkOnModelCanDrop;
dropsink.CanDrop += DropsinkOnCanDrop;
dropsink.Dropped += DropsinkOnDropped;
_dropsink.ModelCanDrop += DropsinkOnModelCanDrop;
_dropsink.CanDrop += DropsinkOnCanDrop;
_dropsink.Dropped += DropsinkOnDropped;
}

private void DropsinkOnDropped(object sender, OlvDropEventArgs e)
{
if (e.DataObject is DataObject dataObject && dataObject.GetFormats().Any(f => f == "FileDrop"))
if (e.DataObject is DataObject dataObject)
DropFile(dataObject);
}

private void DropFile(IDataObject dataObject)
{
if (dataObject.GetFormats().Any(f => f == "FileDrop"))
{
var files = (string[])dataObject.GetData(DataFormats.FileDrop);
OnLoadFile?.Invoke(this, files);
Expand Down Expand Up @@ -170,14 +177,25 @@ protected virtual void OnRightClick(StringEventArgs e)
private void MenuStripClick(object sender, EventArgs e)
{
var menuItem = (ToolStripMenuItem)sender;
if ((string)menuItem.Tag == "Paste")
{
PasteCollectionFromClipboard();
return;
}

OnRightClick(new StringEventArgs((string)menuItem.Tag));
}

private void ListViewCollections_KeyUp(object sender, KeyEventArgs e)
private void PasteCollectionFromClipboard()
{
if (ListViewCollections.SelectedObjects.Count == 0)
return;
var data = Clipboard.GetDataObject();
if (data == null) return;
DropFile(data);
return;
}

private void ListViewCollections_KeyUp(object sender, KeyEventArgs e)
{
var eventData = string.Empty;
switch (e.KeyCode)
{
Expand All @@ -189,8 +207,23 @@ private void ListViewCollections_KeyUp(object sender, KeyEventArgs e)
break;
}

if (!string.IsNullOrEmpty(eventData))
if (ListViewCollections.SelectedObjects.Count > 0 && !string.IsNullOrEmpty(eventData))
OnRightClick(new StringEventArgs(eventData));

switch (e.KeyCode)
{
case Keys.V:
if (e.Control)
PasteCollectionFromClipboard();
break;

case Keys.C:
if (e.Control)
OnRightClick(new StringEventArgs("Copy"));
break;
}

e.Handled = e.SuppressKeyPress = true;
}

private class CollectionRenderer : BaseRenderer
Expand All @@ -205,5 +238,10 @@ protected override void DrawBackground(Graphics g, Rectangle r)
g.FillRectangle(brush, new Rectangle(r.X + 2, r.Y + 2, r.Width - 4, r.Height - 4));
}
}

private void ListViewCollections_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = e.SuppressKeyPress = true;
}
}
}

0 comments on commit 90ba0f0

Please sign in to comment.