Add column sorting to "Batch convert" file list view

This commit is contained in:
Nikolaj Olsson 2020-03-08 08:31:41 +01:00
parent dbdabec9c3
commit cf28ac4ff7
4 changed files with 129 additions and 47 deletions

View File

@ -77,7 +77,7 @@ namespace Nikse.SubtitleEdit.Core
public static SubtitleFormat GetSubtitleFormatByFriendlyName(string friendlyName)
{
foreach (SubtitleFormat format in SubtitleFormat.AllSubtitleFormats)
foreach (var format in SubtitleFormat.AllSubtitleFormats)
{
if (format.FriendlyName == friendlyName || format.Name == friendlyName)
{
@ -107,6 +107,43 @@ namespace Nikse.SubtitleEdit.Core
return $"{(float)fileSize / (1024 * 1024 * 1024):0.0} gb";
}
public static long DisplayFileSizeToBytes(string displayFileSize)
{
if (displayFileSize.Contains("bytes"))
{
if (double.TryParse(displayFileSize.Replace("bytes", string.Empty).Trim(), NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out var n))
{
return (int)Math.Round(n);
}
}
if (displayFileSize.Contains("kb"))
{
if (double.TryParse(displayFileSize.Replace("kb", string.Empty).Trim(), NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out var n))
{
return (int)Math.Round(n * 1024);
}
}
if (displayFileSize.Contains("mb"))
{
if (double.TryParse(displayFileSize.Replace("mb", string.Empty).Trim(), NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out var n))
{
return (int)Math.Round(n * 1024 * 1024);
}
}
if (displayFileSize.Contains("gb"))
{
if (double.TryParse(displayFileSize.Replace("gb", string.Empty).Trim(), NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out var n))
{
return (int)Math.Round(n * 1024 * 1024 * 1024);
}
}
return 0;
}
/// <summary>
/// Downloads the requested resource as a <see cref="String"/> using the configured <see cref="WebProxy"/>.
/// </summary>

View File

@ -29,7 +29,7 @@
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
Nikse.SubtitleEdit.Core.TimeCode timeCode2 = new Nikse.SubtitleEdit.Core.TimeCode();
Nikse.SubtitleEdit.Core.TimeCode timeCode1 = new Nikse.SubtitleEdit.Core.TimeCode();
this.buttonConvert = new System.Windows.Forms.Button();
this.buttonCancel = new System.Windows.Forms.Button();
this.groupBoxConvertOptions = new System.Windows.Forms.GroupBox();
@ -37,6 +37,9 @@
this.listViewConvertOptions = new System.Windows.Forms.ListView();
this.ActionCheckBox = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.Action = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.contextMenuStripOptions = new System.Windows.Forms.ContextMenuStrip(this.components);
this.toolStripMenuItemSelectAll = new System.Windows.Forms.ToolStripMenuItem();
this.inverseSelectionToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.groupBoxChangeFrameRate = new System.Windows.Forms.GroupBox();
this.buttonSwapFrameRate = new System.Windows.Forms.Button();
this.comboBoxFrameRateTo = new System.Windows.Forms.ComboBox();
@ -103,10 +106,8 @@
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.labelStatus = new System.Windows.Forms.Label();
this.contextMenuStripOptions = new System.Windows.Forms.ContextMenuStrip(this.components);
this.toolStripMenuItemSelectAll = new System.Windows.Forms.ToolStripMenuItem();
this.inverseSelectionToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.groupBoxConvertOptions.SuspendLayout();
this.contextMenuStripOptions.SuspendLayout();
this.groupBoxChangeFrameRate.SuspendLayout();
this.groupBoxOffsetTimeCodes.SuspendLayout();
this.groupBoxFixRtl.SuspendLayout();
@ -119,7 +120,6 @@
this.groupBoxOutput.SuspendLayout();
this.groupBoxInput.SuspendLayout();
this.contextMenuStripFiles.SuspendLayout();
this.contextMenuStripOptions.SuspendLayout();
this.SuspendLayout();
//
// buttonConvert
@ -204,6 +204,28 @@
//
this.Action.Width = 400;
//
// contextMenuStripOptions
//
this.contextMenuStripOptions.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripMenuItemSelectAll,
this.inverseSelectionToolStripMenuItem});
this.contextMenuStripOptions.Name = "contextMenuStripOptions";
this.contextMenuStripOptions.Size = new System.Drawing.Size(162, 48);
//
// toolStripMenuItemSelectAll
//
this.toolStripMenuItemSelectAll.Name = "toolStripMenuItemSelectAll";
this.toolStripMenuItemSelectAll.Size = new System.Drawing.Size(161, 22);
this.toolStripMenuItemSelectAll.Text = "Select all";
this.toolStripMenuItemSelectAll.Click += new System.EventHandler(this.toolStripMenuItemSelectAll_Click);
//
// inverseSelectionToolStripMenuItem
//
this.inverseSelectionToolStripMenuItem.Name = "inverseSelectionToolStripMenuItem";
this.inverseSelectionToolStripMenuItem.Size = new System.Drawing.Size(161, 22);
this.inverseSelectionToolStripMenuItem.Text = "Inverse selection";
this.inverseSelectionToolStripMenuItem.Click += new System.EventHandler(this.inverseSelectionToolStripMenuItem_Click);
//
// groupBoxChangeFrameRate
//
this.groupBoxChangeFrameRate.Controls.Add(this.buttonSwapFrameRate);
@ -310,14 +332,14 @@
this.timeUpDownAdjust.Name = "timeUpDownAdjust";
this.timeUpDownAdjust.Size = new System.Drawing.Size(111, 27);
this.timeUpDownAdjust.TabIndex = 1;
timeCode2.Hours = 0;
timeCode2.Milliseconds = 0;
timeCode2.Minutes = 0;
timeCode2.Seconds = 0;
timeCode2.TimeSpan = System.TimeSpan.Parse("00:00:00");
timeCode2.TotalMilliseconds = 0D;
timeCode2.TotalSeconds = 0D;
this.timeUpDownAdjust.TimeCode = timeCode2;
timeCode1.Hours = 0;
timeCode1.Milliseconds = 0;
timeCode1.Minutes = 0;
timeCode1.Seconds = 0;
timeCode1.TimeSpan = System.TimeSpan.Parse("00:00:00");
timeCode1.TotalMilliseconds = 0D;
timeCode1.TotalSeconds = 0D;
this.timeUpDownAdjust.TimeCode = timeCode1;
this.timeUpDownAdjust.UseVideoOffset = false;
//
// labelHourMinSecMilliSecond
@ -886,6 +908,7 @@
this.listViewInputFiles.TabIndex = 2;
this.listViewInputFiles.UseCompatibleStateImageBehavior = false;
this.listViewInputFiles.View = System.Windows.Forms.View.Details;
this.listViewInputFiles.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listViewInputFiles_ColumnClick);
this.listViewInputFiles.DragDrop += new System.Windows.Forms.DragEventHandler(this.listViewInputFiles_DragDrop);
this.listViewInputFiles.DragEnter += new System.Windows.Forms.DragEventHandler(this.listViewInputFiles_DragEnter);
this.listViewInputFiles.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ListViewInputFilesKeyDown);
@ -956,28 +979,6 @@
this.labelStatus.TabIndex = 9;
this.labelStatus.Text = "labelStatus";
//
// contextMenuStripOptions
//
this.contextMenuStripOptions.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripMenuItemSelectAll,
this.inverseSelectionToolStripMenuItem});
this.contextMenuStripOptions.Name = "contextMenuStripOptions";
this.contextMenuStripOptions.Size = new System.Drawing.Size(181, 70);
//
// toolStripMenuItemSelectAll
//
this.toolStripMenuItemSelectAll.Name = "toolStripMenuItemSelectAll";
this.toolStripMenuItemSelectAll.Size = new System.Drawing.Size(180, 22);
this.toolStripMenuItemSelectAll.Text = "Select all";
this.toolStripMenuItemSelectAll.Click += new System.EventHandler(this.toolStripMenuItemSelectAll_Click);
//
// inverseSelectionToolStripMenuItem
//
this.inverseSelectionToolStripMenuItem.Name = "inverseSelectionToolStripMenuItem";
this.inverseSelectionToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.inverseSelectionToolStripMenuItem.Text = "Inverse selection";
this.inverseSelectionToolStripMenuItem.Click += new System.EventHandler(this.inverseSelectionToolStripMenuItem_Click);
//
// BatchConvert
//
this.AllowDrop = true;
@ -998,6 +999,7 @@
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.BatchConvert_FormClosing);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.BatchConvert_KeyDown);
this.groupBoxConvertOptions.ResumeLayout(false);
this.contextMenuStripOptions.ResumeLayout(false);
this.groupBoxChangeFrameRate.ResumeLayout(false);
this.groupBoxChangeFrameRate.PerformLayout();
this.groupBoxOffsetTimeCodes.ResumeLayout(false);
@ -1018,7 +1020,6 @@
this.groupBoxInput.ResumeLayout(false);
this.groupBoxInput.PerformLayout();
this.contextMenuStripFiles.ResumeLayout(false);
this.contextMenuStripOptions.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();

View File

@ -2436,5 +2436,38 @@ namespace Nikse.SubtitleEdit.Forms
item.Checked = !item.Checked;
}
}
private void listViewInputFiles_ColumnClick(object sender, ColumnClickEventArgs e)
{
if (_converting)
{
return;
}
var sorter = (ListViewSorter)listViewInputFiles.ListViewItemSorter;
if (sorter == null)
{
sorter = new ListViewSorter
{
ColumnNumber = e.Column,
IsNumber = false,
IsDisplayFileSize = e.Column == columnHeaderSize.DisplayIndex
};
listViewInputFiles.ListViewItemSorter = sorter;
}
if (e.Column == sorter.ColumnNumber)
{
sorter.Descending = !sorter.Descending; // inverse sort direction
}
else
{
sorter.ColumnNumber = e.Column;
sorter.Descending = false;
sorter.IsNumber = false;
sorter.IsDisplayFileSize = e.Column == columnHeaderSize.DisplayIndex;
}
listViewInputFiles.Sort();
}
}
}

View File

@ -1,36 +1,47 @@
using System;
using System.Windows.Forms;
using Nikse.SubtitleEdit.Core;
namespace Nikse.SubtitleEdit.Logic
{
public class ListViewSorter : System.Collections.IComparer
{
public int ColumnNumber { get; set; }
public bool IsNumber { get; set; }
public bool IsDisplayFileSize { get; set; }
public bool Descending { get; set; }
public int Compare(object o1, object o2)
{
var lvi1 = o1 as ListViewItem;
var lvi2 = o2 as ListViewItem;
if (lvi1 == null || lvi2 == null)
if (!(o1 is ListViewItem lvi1) || !(o2 is ListViewItem lvi2))
{
return 0;
}
if (Descending)
{
ListViewItem temp = lvi1;
var temp = lvi1;
lvi1 = lvi2;
lvi2 = temp;
}
if (IsNumber)
{
var i1 = int.Parse(lvi1.SubItems[ColumnNumber].Text);
var i2 = int.Parse(lvi2.SubItems[ColumnNumber].Text);
return (i1 > i2) ? 1 : (i1 == i2 ? 0 : -1);
if (int.TryParse(lvi1.SubItems[ColumnNumber].Text, out var i1) &&
int.TryParse(lvi2.SubItems[ColumnNumber].Text, out var i2))
{
return i1 > i2 ? 1 : i1 == i2 ? 0 : -1;
}
}
if (IsDisplayFileSize)
{
var i1 = Utilities.DisplayFileSizeToBytes(lvi1.SubItems[ColumnNumber].Text);
var i2 = Utilities.DisplayFileSizeToBytes(lvi2.SubItems[ColumnNumber].Text);
return i1 > i2 ? 1 : i1 == i2 ? 0 : -1;
}
return string.Compare(lvi2.SubItems[ColumnNumber].Text, lvi1.SubItems[ColumnNumber].Text, StringComparison.Ordinal);
}
public int ColumnNumber { get; set; }
public bool IsNumber { get; set; }
public bool Descending { get; set; }
}
}