From c3e86a75c117ce29c22086558fe0583dabd8d03c Mon Sep 17 00:00:00 2001 From: MojitoJoe1813 <71935206+MojitoJoe1813@users.noreply.github.com> Date: Sun, 1 Dec 2024 22:39:18 +0100 Subject: [PATCH 1/2] Adding sorting by columns --- TeddyBench/TeddyBench.csproj | 2 +- TeddyBench/TrackSortDialog.Designer.cs | 10 ++- TeddyBench/TrackSortDialog.cs | 100 ++++++++++++++++++++++++- 3 files changed, 108 insertions(+), 4 deletions(-) diff --git a/TeddyBench/TeddyBench.csproj b/TeddyBench/TeddyBench.csproj index 49d2fa4..8f5eff6 100644 --- a/TeddyBench/TeddyBench.csproj +++ b/TeddyBench/TeddyBench.csproj @@ -4,7 +4,7 @@ true - net48 + net481 WinExe false true diff --git a/TeddyBench/TrackSortDialog.Designer.cs b/TeddyBench/TrackSortDialog.Designer.cs index 2985057..4278434 100644 --- a/TeddyBench/TrackSortDialog.Designer.cs +++ b/TeddyBench/TrackSortDialog.Designer.cs @@ -51,6 +51,7 @@ private void InitializeComponent() this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.columnHeader4 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout(); @@ -149,7 +150,8 @@ private void InitializeComponent() this.lstTracks.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { this.columnHeader1, this.columnHeader2, - this.columnHeader3}); + this.columnHeader3, + this.columnHeader4}); this.lstTracks.Dock = System.Windows.Forms.DockStyle.Fill; this.lstTracks.FullRowSelect = true; this.lstTracks.HideSelection = false; @@ -164,6 +166,7 @@ private void InitializeComponent() this.lstTracks.TabIndex = 0; this.lstTracks.UseCompatibleStateImageBehavior = false; this.lstTracks.View = System.Windows.Forms.View.Details; + this.lstTracks.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.lstTracks_ColumnClick); // // columnHeader1 // @@ -179,6 +182,10 @@ private void InitializeComponent() this.columnHeader3.Text = "ID3"; this.columnHeader3.Width = 219; // + // columnHeader4 + // + this.columnHeader4.Text = "Track No."; + // // TrackSortDialog // this.AcceptButton = this.btnEncode; @@ -216,5 +223,6 @@ private void InitializeComponent() private System.Windows.Forms.ColumnHeader columnHeader1; private System.Windows.Forms.ColumnHeader columnHeader2; private System.Windows.Forms.ColumnHeader columnHeader3; + private System.Windows.Forms.ColumnHeader columnHeader4; } } \ No newline at end of file diff --git a/TeddyBench/TrackSortDialog.cs b/TeddyBench/TrackSortDialog.cs index 7c35377..a051a83 100644 --- a/TeddyBench/TrackSortDialog.cs +++ b/TeddyBench/TrackSortDialog.cs @@ -1,19 +1,25 @@ using Id3; using System; +using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; namespace TeddyBench { + + public partial class TrackSortDialog : Form { private string[] FileNames; + private bool isAscending = false; + private string[] originalColumnHeaders; private List> FileList = new List>(); public TrackSortDialog() @@ -38,6 +44,13 @@ protected override void OnLoad(EventArgs e) } UpdateView(); + + // Store original column headers + originalColumnHeaders = new string[lstTracks.Columns.Count]; + for (int i = 0; i < lstTracks.Columns.Count; i++) + { + originalColumnHeaders[i] = lstTracks.Columns[i].Text; + } } private Id3Tag GetTag(string f) @@ -46,7 +59,8 @@ private Id3Tag GetTag(string f) { Mp3 mp3 = new Mp3(f, Mp3Permissions.Read); - Id3Tag ret = mp3.GetAllTags().Where(t => t.Track.IsAssigned).FirstOrDefault(); + //Id3Tag ret = mp3.GetAllTags().Where(t => t.Track.IsAssigned).FirstOrDefault(); + Id3Tag ret = mp3.GetAllTags().FirstOrDefault(); return ret; } @@ -68,13 +82,33 @@ private void UpdateView() lvi.Text = track.ToString(); string id3 = ""; + string storedTrack = ""; if (item.Item2 != null) { - id3 = item.Item2.Artists + " - " + item.Item2.Title; + string artist; + if (item.Item2.Artists == null) + { + artist = Regex.Replace(item.Item2.Band, @"\0+$", ""); + } else + { + artist = Regex.Replace(item.Item2.Artists, @"\0+$", ""); + } + + id3 = artist + " - " + Regex.Replace(item.Item2.Title, @"\0+$", ""); + + if (item.Item2.Track.IsAssigned) + { + storedTrack = item.Item2.Track.ToString(); + } + } + lvi.SubItems.Add(item.Item1); lvi.SubItems.Add(id3); + lvi.SubItems.Add(item.Item2.Track); + // Set the ToolTip text to the full ID3 information + lvi.ToolTipText = id3; lstTracks.Items.Add(lvi); @@ -111,6 +145,8 @@ private void btnUp_Click(object sender, EventArgs e) int numberOfItems = lstTracks.Items.Count; List selectedItems = new List(); + lstTracks.ListViewItemSorter = null; // remove the sorting by column + isAscending = false; foreach (var item in lstTracks.SelectedItems) { @@ -155,8 +191,11 @@ private void btnDown_Click(object sender, EventArgs e) { return; } + int numberOfItems = lstTracks.Items.Count; List selectedItems = new List(); + lstTracks.ListViewItemSorter = null; // remove the sorting by column + isAscending = false; foreach (var item in lstTracks.SelectedItems) { @@ -197,5 +236,62 @@ private void RebuildFileList() FileList.Add((Tuple)item.Tag); } } + + private void lstTracks_ColumnClick(object sender, ColumnClickEventArgs e) + { + if (e.Column != 0) SortListView(e.Column); + } + + private void SortListView(int columnIndex) + { + // Use ListViewItemComparer for sorting + lstTracks.ListViewItemSorter = new ListViewItemComparer(columnIndex, !isAscending); + lstTracks.Sort(); + isAscending = !isAscending; + RebuildFileList(); + } + + } + + public class ListViewItemComparer : IComparer + { + private int columnIndex; + private bool ascending; + + public ListViewItemComparer(int columnIndex, bool ascending) + { + this.columnIndex = columnIndex; + this.ascending = ascending; + } + + public int Compare(object x, object y) + { + ListViewItem item1 = (ListViewItem)x; + ListViewItem item2 = (ListViewItem)y; + + // Get the values based on the specified column + string value1 = item1.SubItems[columnIndex].Text; + string value2 = item2.SubItems[columnIndex].Text; + + // Handle potential null values + if (value1 == null && value2 == null) + { + return 0; // Both values are null, consider them equal + } + else if (value1 == null) + { + return ascending ? 1 : -1; // Null value comes after non-null in ascending order + } + else if (value2 == null) + { + return ascending ? -1 : 1; // Null value comes before non-null in descending order + } + + // Compare strings based on chosen order + int comparison = string.Compare(value1, value2, StringComparison.OrdinalIgnoreCase); + return ascending ? comparison : -comparison; + } + } + } From 6c376c8d5f7891ab99b8379a1fa71b0fa7b70d55 Mon Sep 17 00:00:00 2001 From: MojitoJoe1813 <71935206+MojitoJoe1813@users.noreply.github.com> Date: Sun, 1 Dec 2024 23:03:15 +0100 Subject: [PATCH 2/2] Removed unused code --- TeddyBench/TrackSortDialog.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/TeddyBench/TrackSortDialog.cs b/TeddyBench/TrackSortDialog.cs index a051a83..8b60265 100644 --- a/TeddyBench/TrackSortDialog.cs +++ b/TeddyBench/TrackSortDialog.cs @@ -43,16 +43,11 @@ protected override void OnLoad(EventArgs e) FileList.Add(item); } - UpdateView(); - - // Store original column headers - originalColumnHeaders = new string[lstTracks.Columns.Count]; - for (int i = 0; i < lstTracks.Columns.Count; i++) - { - originalColumnHeaders[i] = lstTracks.Columns[i].Text; - } + UpdateView(); + } + private Id3Tag GetTag(string f) { try