Skip to content

Commit cf28ac4

Browse files
committed
Add column sorting to "Batch convert" file list view
1 parent dbdabec commit cf28ac4

File tree

4 files changed

+129
-47
lines changed

4 files changed

+129
-47
lines changed

libse/Utilities.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static bool IsHex(string s)
7777

7878
public static SubtitleFormat GetSubtitleFormatByFriendlyName(string friendlyName)
7979
{
80-
foreach (SubtitleFormat format in SubtitleFormat.AllSubtitleFormats)
80+
foreach (var format in SubtitleFormat.AllSubtitleFormats)
8181
{
8282
if (format.FriendlyName == friendlyName || format.Name == friendlyName)
8383
{
@@ -107,6 +107,43 @@ public static string FormatBytesToDisplayFileSize(long fileSize)
107107
return $"{(float)fileSize / (1024 * 1024 * 1024):0.0} gb";
108108
}
109109

110+
public static long DisplayFileSizeToBytes(string displayFileSize)
111+
{
112+
if (displayFileSize.Contains("bytes"))
113+
{
114+
if (double.TryParse(displayFileSize.Replace("bytes", string.Empty).Trim(), NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out var n))
115+
{
116+
return (int)Math.Round(n);
117+
}
118+
}
119+
120+
if (displayFileSize.Contains("kb"))
121+
{
122+
if (double.TryParse(displayFileSize.Replace("kb", string.Empty).Trim(), NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out var n))
123+
{
124+
return (int)Math.Round(n * 1024);
125+
}
126+
}
127+
128+
if (displayFileSize.Contains("mb"))
129+
{
130+
if (double.TryParse(displayFileSize.Replace("mb", string.Empty).Trim(), NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out var n))
131+
{
132+
return (int)Math.Round(n * 1024 * 1024);
133+
}
134+
}
135+
136+
if (displayFileSize.Contains("gb"))
137+
{
138+
if (double.TryParse(displayFileSize.Replace("gb", string.Empty).Trim(), NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out var n))
139+
{
140+
return (int)Math.Round(n * 1024 * 1024 * 1024);
141+
}
142+
}
143+
144+
return 0;
145+
}
146+
110147
/// <summary>
111148
/// Downloads the requested resource as a <see cref="String"/> using the configured <see cref="WebProxy"/>.
112149
/// </summary>

src/Forms/BatchConvert.Designer.cs

Lines changed: 37 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Forms/BatchConvert.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2436,5 +2436,38 @@ private void inverseSelectionToolStripMenuItem_Click(object sender, EventArgs e)
24362436
item.Checked = !item.Checked;
24372437
}
24382438
}
2439+
2440+
private void listViewInputFiles_ColumnClick(object sender, ColumnClickEventArgs e)
2441+
{
2442+
if (_converting)
2443+
{
2444+
return;
2445+
}
2446+
2447+
var sorter = (ListViewSorter)listViewInputFiles.ListViewItemSorter;
2448+
if (sorter == null)
2449+
{
2450+
sorter = new ListViewSorter
2451+
{
2452+
ColumnNumber = e.Column,
2453+
IsNumber = false,
2454+
IsDisplayFileSize = e.Column == columnHeaderSize.DisplayIndex
2455+
};
2456+
listViewInputFiles.ListViewItemSorter = sorter;
2457+
}
2458+
2459+
if (e.Column == sorter.ColumnNumber)
2460+
{
2461+
sorter.Descending = !sorter.Descending; // inverse sort direction
2462+
}
2463+
else
2464+
{
2465+
sorter.ColumnNumber = e.Column;
2466+
sorter.Descending = false;
2467+
sorter.IsNumber = false;
2468+
sorter.IsDisplayFileSize = e.Column == columnHeaderSize.DisplayIndex;
2469+
}
2470+
listViewInputFiles.Sort();
2471+
}
24392472
}
24402473
}

src/Logic/ListViewSorter.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
11
using System;
22
using System.Windows.Forms;
3+
using Nikse.SubtitleEdit.Core;
34

45
namespace Nikse.SubtitleEdit.Logic
56
{
67
public class ListViewSorter : System.Collections.IComparer
78
{
9+
public int ColumnNumber { get; set; }
10+
public bool IsNumber { get; set; }
11+
public bool IsDisplayFileSize { get; set; }
12+
public bool Descending { get; set; }
13+
814
public int Compare(object o1, object o2)
915
{
10-
var lvi1 = o1 as ListViewItem;
11-
var lvi2 = o2 as ListViewItem;
12-
if (lvi1 == null || lvi2 == null)
16+
if (!(o1 is ListViewItem lvi1) || !(o2 is ListViewItem lvi2))
1317
{
1418
return 0;
1519
}
1620

1721
if (Descending)
1822
{
19-
ListViewItem temp = lvi1;
23+
var temp = lvi1;
2024
lvi1 = lvi2;
2125
lvi2 = temp;
2226
}
2327

2428
if (IsNumber)
2529
{
26-
var i1 = int.Parse(lvi1.SubItems[ColumnNumber].Text);
27-
var i2 = int.Parse(lvi2.SubItems[ColumnNumber].Text);
28-
return (i1 > i2) ? 1 : (i1 == i2 ? 0 : -1);
30+
if (int.TryParse(lvi1.SubItems[ColumnNumber].Text, out var i1) &&
31+
int.TryParse(lvi2.SubItems[ColumnNumber].Text, out var i2))
32+
{
33+
return i1 > i2 ? 1 : i1 == i2 ? 0 : -1;
34+
}
2935
}
36+
37+
if (IsDisplayFileSize)
38+
{
39+
var i1 = Utilities.DisplayFileSizeToBytes(lvi1.SubItems[ColumnNumber].Text);
40+
var i2 = Utilities.DisplayFileSizeToBytes(lvi2.SubItems[ColumnNumber].Text);
41+
return i1 > i2 ? 1 : i1 == i2 ? 0 : -1;
42+
}
43+
3044
return string.Compare(lvi2.SubItems[ColumnNumber].Text, lvi1.SubItems[ColumnNumber].Text, StringComparison.Ordinal);
3145
}
32-
public int ColumnNumber { get; set; }
33-
public bool IsNumber { get; set; }
34-
public bool Descending { get; set; }
3546
}
3647
}

0 commit comments

Comments
 (0)