Add multi-select context menu support (rmcrackan/Libation#1195)
This commit is contained in:
parent
bfcd226795
commit
c77f2e2162
10 changed files with 455 additions and 229 deletions
4
Source/LibationWinForms/Form1.Designer.cs
generated
4
Source/LibationWinForms/Form1.Designer.cs
generated
|
|
@ -537,9 +537,9 @@
|
|||
this.productsDisplay.TabIndex = 9;
|
||||
this.productsDisplay.VisibleCountChanged += new System.EventHandler<int>(this.productsDisplay_VisibleCountChanged);
|
||||
this.productsDisplay.RemovableCountChanged += new System.EventHandler<int>(this.productsDisplay_RemovableCountChanged);
|
||||
this.productsDisplay.LiberateClicked += new System.EventHandler<DataLayer.LibraryBook>(this.ProductsDisplay_LiberateClicked);
|
||||
this.productsDisplay.LiberateClicked += ProductsDisplay_LiberateClicked;
|
||||
this.productsDisplay.LiberateSeriesClicked += new System.EventHandler<LibationUiBase.GridView.ISeriesEntry>(this.ProductsDisplay_LiberateSeriesClicked);
|
||||
this.productsDisplay.ConvertToMp3Clicked += new System.EventHandler<DataLayer.LibraryBook>(this.ProductsDisplay_ConvertToMp3Clicked);
|
||||
this.productsDisplay.ConvertToMp3Clicked += ProductsDisplay_ConvertToMp3Clicked;
|
||||
this.productsDisplay.InitialLoaded += new System.EventHandler(this.productsDisplay_InitialLoaded);
|
||||
//
|
||||
// toggleQueueHideBtn
|
||||
|
|
|
|||
|
|
@ -23,36 +23,53 @@ namespace LibationWinForms
|
|||
this.Width = width;
|
||||
}
|
||||
|
||||
private void ProductsDisplay_LiberateClicked(object sender, LibraryBook libraryBook)
|
||||
private void ProductsDisplay_LiberateClicked(object sender, LibraryBook[] libraryBooks)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (libraryBook.Book.UserDefinedItem.BookStatus is LiberatedStatus.NotLiberated or LiberatedStatus.PartialDownload)
|
||||
if (libraryBooks.Length == 1)
|
||||
{
|
||||
Serilog.Log.Logger.Information("Begin single book backup of {libraryBook}", libraryBook);
|
||||
SetQueueCollapseState(false);
|
||||
processBookQueue1.AddDownloadDecrypt(libraryBook);
|
||||
}
|
||||
else if (libraryBook.Book.UserDefinedItem.PdfStatus is LiberatedStatus.NotLiberated)
|
||||
{
|
||||
Serilog.Log.Logger.Information("Begin single pdf backup of {libraryBook}", libraryBook);
|
||||
SetQueueCollapseState(false);
|
||||
processBookQueue1.AddDownloadPdf(libraryBook);
|
||||
}
|
||||
else if (libraryBook.Book.Audio_Exists())
|
||||
{
|
||||
// liberated: open explorer to file
|
||||
var filePath = AudibleFileStorage.Audio.GetPath(libraryBook.Book.AudibleProductId);
|
||||
if (!Go.To.File(filePath?.ShortPathName))
|
||||
var item = libraryBooks[0];
|
||||
if (item.Book.UserDefinedItem.BookStatus is LiberatedStatus.NotLiberated or LiberatedStatus.PartialDownload)
|
||||
{
|
||||
var suffix = string.IsNullOrWhiteSpace(filePath) ? "" : $":\r\n{filePath}";
|
||||
MessageBox.Show($"File not found" + suffix);
|
||||
Serilog.Log.Logger.Information("Begin single book backup of {libraryBook}", item);
|
||||
SetQueueCollapseState(false);
|
||||
processBookQueue1.AddDownloadDecrypt(item);
|
||||
}
|
||||
else if (item.Book.UserDefinedItem.PdfStatus is LiberatedStatus.NotLiberated)
|
||||
{
|
||||
Serilog.Log.Logger.Information("Begin single pdf backup of {libraryBook}", item);
|
||||
SetQueueCollapseState(false);
|
||||
processBookQueue1.AddDownloadPdf(item);
|
||||
}
|
||||
else if (item.Book.Audio_Exists())
|
||||
{
|
||||
// liberated: open explorer to file
|
||||
var filePath = AudibleFileStorage.Audio.GetPath(item.Book.AudibleProductId);
|
||||
if (!Go.To.File(filePath?.ShortPathName))
|
||||
{
|
||||
var suffix = string.IsNullOrWhiteSpace(filePath) ? "" : $":\r\n{filePath}";
|
||||
MessageBox.Show($"File not found" + suffix);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var toLiberate
|
||||
= libraryBooks
|
||||
.Where(x => x.Book.UserDefinedItem.BookStatus is LiberatedStatus.NotLiberated or LiberatedStatus.PartialDownload || x.Book.UserDefinedItem.PdfStatus is LiberatedStatus.NotLiberated)
|
||||
.ToArray();
|
||||
|
||||
if (toLiberate.Length > 0)
|
||||
{
|
||||
SetQueueCollapseState(false);
|
||||
processBookQueue1.AddDownloadDecrypt(toLiberate);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Serilog.Log.Logger.Error(ex, "An error occurred while handling the stop light button click for {libraryBook}", libraryBook);
|
||||
Serilog.Log.Logger.Error(ex, "An error occurred while handling the stop light button click for {libraryBook}", libraryBooks);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -72,20 +89,21 @@ namespace LibationWinForms
|
|||
}
|
||||
}
|
||||
|
||||
private void ProductsDisplay_ConvertToMp3Clicked(object sender, LibraryBook libraryBook)
|
||||
private void ProductsDisplay_ConvertToMp3Clicked(object sender, LibraryBook[] libraryBooks)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (libraryBook.Book.UserDefinedItem.BookStatus is LiberatedStatus.Liberated)
|
||||
var preLiberated = libraryBooks.Where(lb => lb.Book.UserDefinedItem.BookStatus is LiberatedStatus.Liberated).ToArray();
|
||||
if (preLiberated.Length > 0)
|
||||
{
|
||||
Serilog.Log.Logger.Information("Begin single pdf backup of {libraryBook}", libraryBook);
|
||||
Serilog.Log.Logger.Information("Begin convert {count} books to mp3", preLiberated.Length);
|
||||
SetQueueCollapseState(false);
|
||||
processBookQueue1.AddConvertMp3(libraryBook);
|
||||
processBookQueue1.AddConvertMp3(preLiberated);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Serilog.Log.Logger.Error(ex, "An error occurred while handling the stop light button click for {libraryBook}", libraryBook);
|
||||
Serilog.Log.Logger.Error(ex, "An error occurred while handling the stop light button click for {libraryBook}", libraryBooks);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,9 +20,9 @@ namespace LibationWinForms.GridView
|
|||
/// <summary>Number of visible rows has changed</summary>
|
||||
public event EventHandler<int> VisibleCountChanged;
|
||||
public event EventHandler<int> RemovableCountChanged;
|
||||
public event EventHandler<LibraryBook> LiberateClicked;
|
||||
public event EventHandler<LibraryBook[]> LiberateClicked;
|
||||
public event EventHandler<ISeriesEntry> LiberateSeriesClicked;
|
||||
public event EventHandler<LibraryBook> ConvertToMp3Clicked;
|
||||
public event EventHandler<LibraryBook[]> ConvertToMp3Clicked;
|
||||
public event EventHandler InitialLoaded;
|
||||
|
||||
private bool hasBeenDisplayed;
|
||||
|
|
@ -123,12 +123,12 @@ namespace LibationWinForms.GridView
|
|||
|
||||
#region Cell Context Menu
|
||||
|
||||
private void productsGrid_CellContextMenuStripNeeded(IGridEntry entry, ContextMenuStrip ctxMenu)
|
||||
private void productsGrid_CellContextMenuStripNeeded(IGridEntry[] entries, ContextMenuStrip ctxMenu)
|
||||
{
|
||||
var ctx = new GridContextMenu(entry, '&');
|
||||
#region Liberate all Episodes
|
||||
var ctx = new GridContextMenu(entries, '&');
|
||||
#region Liberate all Episodes (Single series only)
|
||||
|
||||
if (entry.Liberate.IsSeries)
|
||||
if (entries.Length == 1 && entries[0] is ISeriesEntry seriesEntry)
|
||||
{
|
||||
var liberateEpisodesMenuItem = new ToolStripMenuItem()
|
||||
{
|
||||
|
|
@ -136,7 +136,7 @@ namespace LibationWinForms.GridView
|
|||
Enabled = ctx.LiberateEpisodesEnabled
|
||||
};
|
||||
|
||||
liberateEpisodesMenuItem.Click += (_, _) => LiberateSeriesClicked?.Invoke(this, (ISeriesEntry)entry);
|
||||
liberateEpisodesMenuItem.Click += (_, _) => LiberateSeriesClicked?.Invoke(this, seriesEntry);
|
||||
ctxMenu.Items.Add(liberateEpisodesMenuItem);
|
||||
}
|
||||
|
||||
|
|
@ -163,17 +163,10 @@ namespace LibationWinForms.GridView
|
|||
ctxMenu.Items.Add(setNotDownloadMenuItem);
|
||||
|
||||
#endregion
|
||||
#region Remove from library
|
||||
#region Locate file (Single book only)
|
||||
|
||||
var removeMenuItem = new ToolStripMenuItem() { Text = ctx.RemoveText };
|
||||
removeMenuItem.Click += async (_, _) => await ctx.RemoveAsync();
|
||||
ctxMenu.Items.Add(removeMenuItem);
|
||||
|
||||
#endregion
|
||||
|
||||
if (!entry.Liberate.IsSeries)
|
||||
if (entries.Length == 1 && entries[0] is ILibraryBookEntry entry)
|
||||
{
|
||||
#region Locate file
|
||||
var locateFileMenuItem = new ToolStripMenuItem() { Text = ctx.LocateFileText };
|
||||
ctxMenu.Items.Add(locateFileMenuItem);
|
||||
locateFileMenuItem.Click += (_, _) =>
|
||||
|
|
@ -194,23 +187,49 @@ namespace LibationWinForms.GridView
|
|||
MessageBoxLib.ShowAdminAlert(this, ctx.LocateFileErrorMessage, ctx.LocateFileErrorMessage, ex);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region Convert to Mp3
|
||||
#endregion
|
||||
#region Remove from library
|
||||
|
||||
var removeMenuItem = new ToolStripMenuItem() { Text = ctx.RemoveText };
|
||||
removeMenuItem.Click += async (_, _) => await ctx.RemoveAsync();
|
||||
ctxMenu.Items.Add(removeMenuItem);
|
||||
|
||||
#endregion
|
||||
#region Liberate All (multiple books only)
|
||||
if (entries.OfType<ILibraryBookEntry>().Count() > 1)
|
||||
{
|
||||
var downloadSelectedMenuItem = new ToolStripMenuItem()
|
||||
{
|
||||
Text = ctx.DownloadSelectedText
|
||||
};
|
||||
ctxMenu.Items.Add(downloadSelectedMenuItem);
|
||||
downloadSelectedMenuItem.Click += (s, _) =>
|
||||
{
|
||||
LiberateClicked?.Invoke(s, ctx.LibraryBookEntries.Select(e => e.LibraryBook).ToArray());
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region Convert to Mp3
|
||||
|
||||
if (ctx.LibraryBookEntries.Length > 0)
|
||||
{
|
||||
var convertToMp3MenuItem = new ToolStripMenuItem
|
||||
{
|
||||
Text = ctx.ConvertToMp3Text,
|
||||
Enabled = ctx.ConvertToMp3Enabled
|
||||
};
|
||||
convertToMp3MenuItem.Click += (_, e) => ConvertToMp3Clicked?.Invoke(this, entry.LibraryBook);
|
||||
convertToMp3MenuItem.Click += (_, e) => ConvertToMp3Clicked?.Invoke(this, ctx.LibraryBookEntries.Select(e => e.LibraryBook).ToArray());
|
||||
ctxMenu.Items.Add(convertToMp3MenuItem);
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#region Force Re-Download
|
||||
if (!entry.Liberate.IsSeries)
|
||||
#endregion
|
||||
#region Force Re-Download (Single book only)
|
||||
|
||||
if (entries.Length == 1 && entries[0] is ILibraryBookEntry entry4)
|
||||
{
|
||||
var reDownloadMenuItem = new ToolStripMenuItem()
|
||||
{
|
||||
|
|
@ -220,13 +239,24 @@ namespace LibationWinForms.GridView
|
|||
ctxMenu.Items.Add(reDownloadMenuItem);
|
||||
reDownloadMenuItem.Click += (s, _) =>
|
||||
{
|
||||
//No need to persist this change. It only needs to last long for the file to start downloading
|
||||
entry.Book.UserDefinedItem.BookStatus = LiberatedStatus.NotLiberated;
|
||||
LiberateClicked?.Invoke(s, entry.LibraryBook);
|
||||
//No need to persist these changes. They only needs to last long for the files to start downloading
|
||||
entry4.Book.UserDefinedItem.BookStatus = LiberatedStatus.NotLiberated;
|
||||
if (entry4.Book.HasPdf())
|
||||
entry4.Book.UserDefinedItem.SetPdfStatus(LiberatedStatus.NotLiberated);
|
||||
|
||||
LiberateClicked?.Invoke(s, [entry4.LibraryBook]);
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region Edit Templates
|
||||
|
||||
if (entries.Length > 1)
|
||||
return;
|
||||
|
||||
ctxMenu.Items.Add(new ToolStripSeparator());
|
||||
|
||||
#region Edit Templates (Single book only)
|
||||
|
||||
void editTemplate<T>(LibraryBook libraryBook, string existingTemplate, Action<string> setNewTemplate)
|
||||
where T : Templates, LibationFileManager.ITemplate, new()
|
||||
{
|
||||
|
|
@ -238,14 +268,14 @@ namespace LibationWinForms.GridView
|
|||
}
|
||||
}
|
||||
|
||||
if (!entry.Liberate.IsSeries)
|
||||
if (entries.Length == 1 && entries[0] is ILibraryBookEntry entry2)
|
||||
{
|
||||
var folderTemplateMenuItem = new ToolStripMenuItem { Text = ctx.FolderTemplateText };
|
||||
var fileTemplateMenuItem = new ToolStripMenuItem { Text = ctx.FileTemplateText };
|
||||
var multiFileTemplateMenuItem = new ToolStripMenuItem { Text = ctx.MultipartTemplateText };
|
||||
folderTemplateMenuItem.Click += (s, _) => editTemplate<Templates.FolderTemplate>(entry.LibraryBook, Configuration.Instance.FolderTemplate, t => Configuration.Instance.FolderTemplate = t);
|
||||
fileTemplateMenuItem.Click += (s, _) => editTemplate<Templates.FileTemplate>(entry.LibraryBook, Configuration.Instance.FileTemplate, t => Configuration.Instance.FileTemplate = t);
|
||||
multiFileTemplateMenuItem.Click += (s, _) => editTemplate<Templates.ChapterFileTemplate>(entry.LibraryBook, Configuration.Instance.ChapterFileTemplate, t => Configuration.Instance.ChapterFileTemplate = t);
|
||||
folderTemplateMenuItem.Click += (s, _) => editTemplate<Templates.FolderTemplate>(entry2.LibraryBook, Configuration.Instance.FolderTemplate, t => Configuration.Instance.FolderTemplate = t);
|
||||
fileTemplateMenuItem.Click += (s, _) => editTemplate<Templates.FileTemplate>(entry2.LibraryBook, Configuration.Instance.FileTemplate, t => Configuration.Instance.FileTemplate = t);
|
||||
multiFileTemplateMenuItem.Click += (s, _) => editTemplate<Templates.ChapterFileTemplate>(entry2.LibraryBook, Configuration.Instance.ChapterFileTemplate, t => Configuration.Instance.ChapterFileTemplate = t);
|
||||
|
||||
var editTemplatesMenuItem = new ToolStripMenuItem { Text = ctx.EditTemplatesText };
|
||||
editTemplatesMenuItem.DropDownItems.AddRange(new[] { folderTemplateMenuItem, fileTemplateMenuItem, multiFileTemplateMenuItem });
|
||||
|
|
@ -255,25 +285,22 @@ namespace LibationWinForms.GridView
|
|||
}
|
||||
|
||||
#endregion
|
||||
#region View Bookmarks/Clips (Single book only)
|
||||
|
||||
ctxMenu.Items.Add(new ToolStripSeparator());
|
||||
|
||||
#region View Bookmarks/Clips
|
||||
|
||||
if (!entry.Liberate.IsSeries)
|
||||
if (entries.Length == 1 && entries[0] is ILibraryBookEntry entry3)
|
||||
{
|
||||
var bookRecordMenuItem = new ToolStripMenuItem { Text = ctx.ViewBookmarksText };
|
||||
bookRecordMenuItem.Click += (_, _) => new BookRecordsDialog(entry.LibraryBook).ShowDialog(this);
|
||||
bookRecordMenuItem.Click += (_, _) => new BookRecordsDialog(entry3.LibraryBook).ShowDialog(this);
|
||||
ctxMenu.Items.Add(bookRecordMenuItem);
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region View All Series
|
||||
#region View All Series (Single book only)
|
||||
|
||||
if (entry.Book.SeriesLink.Any())
|
||||
if (entries.Length == 1 && entries[0].Book.SeriesLink.Any())
|
||||
{
|
||||
var viewSeriesMenuItem = new ToolStripMenuItem { Text = ctx.ViewSeriesText };
|
||||
viewSeriesMenuItem.Click += (_, _) => new SeriesViewDialog(entry.LibraryBook).Show();
|
||||
viewSeriesMenuItem.Click += (_, _) => new SeriesViewDialog(entries[0].LibraryBook).Show();
|
||||
ctxMenu.Items.Add(viewSeriesMenuItem);
|
||||
}
|
||||
|
||||
|
|
@ -393,7 +420,7 @@ namespace LibationWinForms.GridView
|
|||
{
|
||||
if (liveGridEntry.LibraryBook.Book.UserDefinedItem.BookStatus is not LiberatedStatus.Error
|
||||
&& !liveGridEntry.Liberate.IsUnavailable)
|
||||
LiberateClicked?.Invoke(this, liveGridEntry.LibraryBook);
|
||||
LiberateClicked?.Invoke(this, [liveGridEntry.LibraryBook]);
|
||||
}
|
||||
|
||||
private void productsGrid_RemovableCountChanged(object sender, EventArgs e)
|
||||
|
|
|
|||
|
|
@ -11,33 +11,34 @@ using System.Drawing;
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
#nullable enable
|
||||
|
||||
namespace LibationWinForms.GridView
|
||||
{
|
||||
public delegate void GridEntryClickedEventHandler(IGridEntry liveGridEntry);
|
||||
public delegate void LibraryBookEntryClickedEventHandler(ILibraryBookEntry liveGridEntry);
|
||||
public delegate void GridEntryRectangleClickedEventHandler(IGridEntry liveGridEntry, Rectangle cellRectangle);
|
||||
public delegate void ProductsGridCellContextMenuStripNeededEventHandler(IGridEntry liveGridEntry, ContextMenuStrip ctxMenu);
|
||||
public delegate void ProductsGridCellContextMenuStripNeededEventHandler(IGridEntry[] liveGridEntry, ContextMenuStrip ctxMenu);
|
||||
|
||||
public partial class ProductsGrid : UserControl
|
||||
{
|
||||
/// <summary>Number of visible rows has changed</summary>
|
||||
public event EventHandler<int> VisibleCountChanged;
|
||||
public event LibraryBookEntryClickedEventHandler LiberateClicked;
|
||||
public event GridEntryClickedEventHandler CoverClicked;
|
||||
public event LibraryBookEntryClickedEventHandler DetailsClicked;
|
||||
public event GridEntryRectangleClickedEventHandler DescriptionClicked;
|
||||
public new event EventHandler<ScrollEventArgs> Scroll;
|
||||
public event EventHandler RemovableCountChanged;
|
||||
public event ProductsGridCellContextMenuStripNeededEventHandler LiberateContextMenuStripNeeded;
|
||||
public event EventHandler<int>? VisibleCountChanged;
|
||||
public event LibraryBookEntryClickedEventHandler? LiberateClicked;
|
||||
public event GridEntryClickedEventHandler? CoverClicked;
|
||||
public event LibraryBookEntryClickedEventHandler? DetailsClicked;
|
||||
public event GridEntryRectangleClickedEventHandler? DescriptionClicked;
|
||||
public new event EventHandler<ScrollEventArgs>? Scroll;
|
||||
public event EventHandler? RemovableCountChanged;
|
||||
public event ProductsGridCellContextMenuStripNeededEventHandler? LiberateContextMenuStripNeeded;
|
||||
|
||||
private GridEntryBindingList bindingList;
|
||||
private GridEntryBindingList? bindingList;
|
||||
internal IEnumerable<LibraryBook> GetVisibleBooks()
|
||||
=> bindingList
|
||||
.GetFilteredInItems()
|
||||
.Select(lbe => lbe.LibraryBook);
|
||||
?.GetFilteredInItems()
|
||||
.Select(lbe => lbe.LibraryBook) ?? Enumerable.Empty<LibraryBook>();
|
||||
internal IEnumerable<ILibraryBookEntry> GetAllBookEntries()
|
||||
=> bindingList.AllItems().BookEntries();
|
||||
=> bindingList?.AllItems().BookEntries() ?? Enumerable.Empty<ILibraryBookEntry>();
|
||||
|
||||
public ProductsGrid()
|
||||
{
|
||||
|
|
@ -64,11 +65,17 @@ namespace LibationWinForms.GridView
|
|||
|
||||
[PropertyChangeFilter(nameof(Configuration.GridFontScaleFactor))]
|
||||
private void Configuration_FontScaleChanged(object sender, PropertyChangedEventArgsEx e)
|
||||
=> setGridFontScale((float)e.NewValue);
|
||||
{
|
||||
if (e.NewValue is float v)
|
||||
setGridFontScale(v);
|
||||
}
|
||||
|
||||
[PropertyChangeFilter(nameof(Configuration.GridScaleFactor))]
|
||||
private void Configuration_ScaleChanged(object sender, PropertyChangedEventArgsEx e)
|
||||
=> setGridScale((float)e.NewValue);
|
||||
{
|
||||
if (e.NewValue is float v)
|
||||
setGridScale(v);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Keep track of the original dimensions for rescaling
|
||||
|
|
@ -106,10 +113,13 @@ namespace LibationWinForms.GridView
|
|||
|
||||
#endregion
|
||||
|
||||
private void GridEntryDataGridView_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
|
||||
private static string? RemoveLineBreaks(string? text)
|
||||
=> text?.Replace("\r\n", "").Replace('\r', ' ').Replace('\n', ' ');
|
||||
|
||||
private void GridEntryDataGridView_CellContextMenuStripNeeded(object? sender, DataGridViewCellContextMenuStripNeededEventArgs e)
|
||||
{
|
||||
// header
|
||||
if (e.RowIndex < 0)
|
||||
if (e.RowIndex < 0 || sender is not DataGridView dgv)
|
||||
return;
|
||||
|
||||
e.ContextMenuStrip = new ContextMenuStrip();
|
||||
|
|
@ -120,25 +130,89 @@ namespace LibationWinForms.GridView
|
|||
{
|
||||
try
|
||||
{
|
||||
var dgv = (DataGridView)sender;
|
||||
var text = dgv[e.ColumnIndex, e.RowIndex].FormattedValue.ToString();
|
||||
Clipboard.SetDataObject(text, false, 5, 150);
|
||||
string clipboardText;
|
||||
|
||||
if (dgv.SelectedCells.Count <= 1)
|
||||
{
|
||||
//Copy contents only of cell that was right-clicked on.
|
||||
clipboardText = dgv[e.ColumnIndex, e.RowIndex].FormattedValue?.ToString() ?? string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
//Copy contents of selected cells. Each row is a new line,
|
||||
//and columns are separated with tabs. Similar formatting to Microsoft Excel.
|
||||
var selectedCells
|
||||
= dgv.SelectedCells
|
||||
.OfType<DataGridViewCell>()
|
||||
.Where(c => c.OwningColumn is not null && c.OwningRow is not null)
|
||||
.OrderBy(c => c.RowIndex)
|
||||
.ThenBy(c => c.OwningColumn!.DisplayIndex)
|
||||
.ToList();
|
||||
|
||||
var headerText
|
||||
= string.Join("\t",
|
||||
selectedCells
|
||||
.Select(c => c.OwningColumn)
|
||||
.Distinct()
|
||||
.Select(c => RemoveLineBreaks(c?.HeaderText))
|
||||
.OfType<string>());
|
||||
|
||||
List<string> linesOfText = [headerText];
|
||||
foreach (var distinctRow in selectedCells.Select(c => c.RowIndex).Distinct())
|
||||
{
|
||||
linesOfText.Add(string.Join("\t",
|
||||
selectedCells
|
||||
.Where(c => c.RowIndex == distinctRow)
|
||||
.Select(c => RemoveLineBreaks(c.FormattedValue?.ToString()) ?? string.Empty)
|
||||
));
|
||||
}
|
||||
clipboardText = string.Join(Environment.NewLine, linesOfText);
|
||||
}
|
||||
Clipboard.SetDataObject(clipboardText, false, 5, 150);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Serilog.Log.Logger.Error(ex, "Error copying text to clipboard");
|
||||
}
|
||||
catch { }
|
||||
});
|
||||
e.ContextMenuStrip.Items.Add(new ToolStripSeparator());
|
||||
}
|
||||
|
||||
var entry = getGridEntry(e.RowIndex);
|
||||
var name = gridEntryDataGridView.Columns[e.ColumnIndex].DataPropertyName;
|
||||
LiberateContextMenuStripNeeded?.Invoke(entry, e.ContextMenuStrip);
|
||||
var clickedEntry = getGridEntry(e.RowIndex);
|
||||
|
||||
var allSelected
|
||||
= gridEntryDataGridView
|
||||
.SelectedCells
|
||||
.OfType<DataGridViewCell>()
|
||||
.Select(c => c.OwningRow)
|
||||
.OfType<DataGridViewRow>()
|
||||
.Distinct()
|
||||
.OrderBy(r => r.Index)
|
||||
.Select(r => r.DataBoundItem)
|
||||
.OfType<IGridEntry>()
|
||||
.ToArray();
|
||||
|
||||
var clickedIndex = Array.IndexOf(allSelected, clickedEntry);
|
||||
if (clickedIndex == -1)
|
||||
{
|
||||
//User didn't right-click on a selected cell
|
||||
gridEntryDataGridView.ClearSelection();
|
||||
gridEntryDataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
|
||||
allSelected = [clickedEntry];
|
||||
}
|
||||
else if (clickedIndex > 0)
|
||||
{
|
||||
//Ensure the clicked entry is first in the list
|
||||
(allSelected[0], allSelected[clickedIndex]) = (allSelected[clickedIndex], allSelected[0]);
|
||||
}
|
||||
LiberateContextMenuStripNeeded?.Invoke(allSelected, e.ContextMenuStrip);
|
||||
}
|
||||
|
||||
private void EnableDoubleBuffering()
|
||||
{
|
||||
var propertyInfo = gridEntryDataGridView.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
|
||||
|
||||
propertyInfo.SetValue(gridEntryDataGridView, true, null);
|
||||
propertyInfo?.SetValue(gridEntryDataGridView, true, null);
|
||||
}
|
||||
|
||||
#region Button controls
|
||||
|
|
@ -167,11 +241,11 @@ namespace LibationWinForms.GridView
|
|||
if (e.ColumnIndex == liberateGVColumn.Index)
|
||||
{
|
||||
if (sEntry.Liberate.Expanded)
|
||||
bindingList.CollapseItem(sEntry);
|
||||
bindingList?.CollapseItem(sEntry);
|
||||
else
|
||||
bindingList.ExpandItem(sEntry);
|
||||
bindingList?.ExpandItem(sEntry);
|
||||
|
||||
VisibleCountChanged?.Invoke(this, bindingList.GetFilteredInItems().Count());
|
||||
VisibleCountChanged?.Invoke(this, bindingList?.GetFilteredInItems().Count() ?? 0);
|
||||
}
|
||||
else if (e.ColumnIndex == descriptionGVColumn.Index)
|
||||
DescriptionClicked?.Invoke(sEntry, gridEntryDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false));
|
||||
|
|
@ -202,7 +276,7 @@ namespace LibationWinForms.GridView
|
|||
get => removeGVColumn.Visible;
|
||||
set
|
||||
{
|
||||
if (value)
|
||||
if (value && bindingList is not null)
|
||||
{
|
||||
foreach (var book in bindingList.AllItems())
|
||||
book.Remove = false;
|
||||
|
|
@ -248,13 +322,16 @@ namespace LibationWinForms.GridView
|
|||
|
||||
internal void UpdateGrid(List<LibraryBook> dbBooks)
|
||||
{
|
||||
if (bindingList == null)
|
||||
throw new InvalidOperationException($"Must call {nameof(BindToGridAsync)} before calling {nameof(UpdateGrid)}");
|
||||
|
||||
//First row that is in view in the DataGridView
|
||||
var topRow = gridEntryDataGridView.Rows.Cast<DataGridViewRow>().FirstOrDefault(r => r.Displayed)?.Index ?? 0;
|
||||
|
||||
#region Add new or update existing grid entries
|
||||
|
||||
//Remove filter prior to adding/updating boooks
|
||||
string existingFilter = syncBindingSource.Filter;
|
||||
//Remove filter prior to adding/updating books
|
||||
string? existingFilter = syncBindingSource.Filter;
|
||||
Filter(null);
|
||||
|
||||
//Add absent entries to grid, or update existing entry
|
||||
|
|
@ -308,6 +385,9 @@ namespace LibationWinForms.GridView
|
|||
|
||||
public void RemoveBooks(IEnumerable<ILibraryBookEntry> removedBooks)
|
||||
{
|
||||
if (bindingList == null)
|
||||
throw new InvalidOperationException($"Must call {nameof(BindToGridAsync)} before calling {nameof(RemoveBooks)}");
|
||||
|
||||
//Remove books in series from their parents' Children list
|
||||
foreach (var removed in removedBooks.Where(b => b.Liberate.IsEpisode))
|
||||
removed.Parent.RemoveChild(removed);
|
||||
|
|
@ -325,8 +405,11 @@ namespace LibationWinForms.GridView
|
|||
VisibleCountChanged?.Invoke(this, bindingList.GetFilteredInItems().Count());
|
||||
}
|
||||
|
||||
private void AddOrUpdateBook(LibraryBook book, ILibraryBookEntry existingBookEntry)
|
||||
private void AddOrUpdateBook(LibraryBook book, ILibraryBookEntry? existingBookEntry)
|
||||
{
|
||||
if (bindingList == null)
|
||||
throw new InvalidOperationException($"Must call {nameof(BindToGridAsync)} before calling {nameof(AddOrUpdateBook)}");
|
||||
|
||||
if (existingBookEntry is null)
|
||||
// Add the new product to top
|
||||
bindingList.Insert(0, new LibraryBookEntry<WinFormsEntryStatus>(book));
|
||||
|
|
@ -335,8 +418,11 @@ namespace LibationWinForms.GridView
|
|||
existingBookEntry.UpdateLibraryBook(book);
|
||||
}
|
||||
|
||||
private void AddOrUpdateEpisode(LibraryBook episodeBook, ILibraryBookEntry existingEpisodeEntry, List<ISeriesEntry> seriesEntries, IEnumerable<LibraryBook> dbBooks)
|
||||
private void AddOrUpdateEpisode(LibraryBook episodeBook, ILibraryBookEntry? existingEpisodeEntry, List<ISeriesEntry> seriesEntries, IEnumerable<LibraryBook> dbBooks)
|
||||
{
|
||||
if (bindingList == null)
|
||||
throw new InvalidOperationException($"Must call {nameof(BindToGridAsync)} before calling {nameof(AddOrUpdateEpisode)}");
|
||||
|
||||
if (existingEpisodeEntry is null)
|
||||
{
|
||||
ILibraryBookEntry episodeEntry;
|
||||
|
|
@ -397,7 +483,7 @@ namespace LibationWinForms.GridView
|
|||
|
||||
#region Filter
|
||||
|
||||
public void Filter(string searchString)
|
||||
public void Filter(string? searchString)
|
||||
{
|
||||
if (bindingList is null) return;
|
||||
|
||||
|
|
@ -485,16 +571,16 @@ namespace LibationWinForms.GridView
|
|||
removeGVColumn.IndeterminateValue = null;
|
||||
}
|
||||
|
||||
private void HideMenuItem_Click(object sender, EventArgs e)
|
||||
private void HideMenuItem_Click(object? sender, EventArgs e)
|
||||
{
|
||||
var menuItem = sender as ToolStripMenuItem;
|
||||
var propertyName = menuItem.Tag as string;
|
||||
var propertyName = menuItem?.Tag as string;
|
||||
|
||||
var column = gridEntryDataGridView.Columns
|
||||
.Cast<DataGridViewColumn>()
|
||||
.FirstOrDefault(c => c.DataPropertyName == propertyName);
|
||||
|
||||
if (column != null)
|
||||
if (column != null && menuItem != null && propertyName != null)
|
||||
{
|
||||
var visible = menuItem.Checked;
|
||||
menuItem.Checked = !visible;
|
||||
|
|
@ -508,7 +594,7 @@ namespace LibationWinForms.GridView
|
|||
}
|
||||
}
|
||||
|
||||
private void gridEntryDataGridView_ColumnDisplayIndexChanged(object sender, DataGridViewColumnEventArgs e)
|
||||
private void gridEntryDataGridView_ColumnDisplayIndexChanged(object? sender, DataGridViewColumnEventArgs e)
|
||||
{
|
||||
var config = Configuration.Instance;
|
||||
|
||||
|
|
@ -517,7 +603,7 @@ namespace LibationWinForms.GridView
|
|||
config.GridColumnsDisplayIndices = dictionary;
|
||||
}
|
||||
|
||||
private void gridEntryDataGridView_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
|
||||
private void gridEntryDataGridView_CellToolTipTextNeeded(object? sender, DataGridViewCellToolTipTextNeededEventArgs e)
|
||||
{
|
||||
if (e.ColumnIndex == descriptionGVColumn.Index)
|
||||
e.ToolTipText = "Click to see full description";
|
||||
|
|
@ -525,7 +611,7 @@ namespace LibationWinForms.GridView
|
|||
e.ToolTipText = "Click to see full size";
|
||||
}
|
||||
|
||||
private void gridEntryDataGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
|
||||
private void gridEntryDataGridView_ColumnWidthChanged(object? sender, DataGridViewColumnEventArgs e)
|
||||
{
|
||||
var config = Configuration.Instance;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue