Add row sort in ASSA styles - thx tormento :)

Work on  #7345
This commit is contained in:
niksedk 2023-09-08 06:18:34 +02:00
parent 762f1883cb
commit 94f789d088
3 changed files with 78 additions and 6 deletions

View File

@ -207,6 +207,7 @@
this.listViewStyles.TabIndex = 0;
this.listViewStyles.UseCompatibleStateImageBehavior = false;
this.listViewStyles.View = System.Windows.Forms.View.Details;
this.listViewStyles.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.ListViewStylesSortColumnClick);
this.listViewStyles.SelectedIndexChanged += new System.EventHandler(this.listViewStyles_SelectedIndexChanged);
this.listViewStyles.ClientSizeChanged += new System.EventHandler(this.listViewStyles_ClientSizeChanged);
this.listViewStyles.Enter += new System.EventHandler(this.listViewStyles_Enter);
@ -1606,6 +1607,7 @@
this.listViewStorage.TabIndex = 6;
this.listViewStorage.UseCompatibleStateImageBehavior = false;
this.listViewStorage.View = System.Windows.Forms.View.Details;
this.listViewStorage.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listViewStorage_ColumnClick);
this.listViewStorage.SelectedIndexChanged += new System.EventHandler(this.listViewStorage_SelectedIndexChanged);
this.listViewStorage.ClientSizeChanged += new System.EventHandler(this.listViewStorage_ClientSizeChanged);
this.listViewStorage.Enter += new System.EventHandler(this.listViewStorage_Enter);

View File

@ -446,7 +446,7 @@ namespace Nikse.SubtitleEdit.Forms.Assa
}
}
private List<string> GetFontNames(byte[] fontBytes)
private static List<string> GetFontNames(byte[] fontBytes)
{
var privateFontCollection = new PrivateFontCollection();
var handle = GCHandle.Alloc(fontBytes, GCHandleType.Pinned);
@ -2925,5 +2925,63 @@ namespace Nikse.SubtitleEdit.Forms.Assa
GeneratePreview();
}
}
private void ListViewStylesSortColumnClick(object sender, ColumnClickEventArgs e)
{
var lv = (ListView)sender;
if (!(lv.ListViewItemSorter is ListViewSorter sorter))
{
sorter = new ListViewSorter
{
ColumnNumber = e.Column,
IsNumber = e.Column == 2 || e.Column == 3,
IsDisplayFileSize = false,
};
lv.ListViewItemSorter = sorter;
}
if (e.Column == sorter.ColumnNumber)
{
sorter.Descending = !sorter.Descending; // inverse sort direction
}
else
{
sorter.ColumnNumber = e.Column;
sorter.Descending = false;
sorter.IsNumber = e.Column == 2 || e.Column == 3;
sorter.IsDisplayFileSize = false;
}
lv.Sort();
}
private void listViewStorage_ColumnClick(object sender, ColumnClickEventArgs e)
{
var lv = (ListView)sender;
if (!(lv.ListViewItemSorter is ListViewSorter sorter))
{
sorter = new ListViewSorter
{
ColumnNumber = e.Column,
IsNumber = e.Column == 2,
IsDisplayFileSize = false,
};
lv.ListViewItemSorter = sorter;
}
if (e.Column == sorter.ColumnNumber)
{
sorter.Descending = !sorter.Descending; // inverse sort direction
}
else
{
sorter.ColumnNumber = e.Column;
sorter.Descending = false;
sorter.IsNumber = e.Column == 2;
sorter.IsDisplayFileSize = false;
}
lv.Sort();
}
}
}

View File

@ -1,5 +1,7 @@
using Nikse.SubtitleEdit.Core.Common;
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace Nikse.SubtitleEdit.Logic
@ -11,6 +13,8 @@ namespace Nikse.SubtitleEdit.Logic
public bool IsDisplayFileSize { get; set; }
public bool Descending { get; set; }
private Regex _invariantNumber = new Regex(@"\d+\.{1,2}", RegexOptions.Compiled);
public int Compare(object o1, object o2)
{
if (!(o1 is ListViewItem lvi1) || !(o2 is ListViewItem lvi2))
@ -20,18 +24,26 @@ namespace Nikse.SubtitleEdit.Logic
if (Descending)
{
var temp = lvi1;
lvi1 = lvi2;
lvi2 = temp;
(lvi1, lvi2) = (lvi2, lvi1);
}
if (IsNumber)
{
if (int.TryParse(lvi1.SubItems[ColumnNumber].Text, out var i1) &&
int.TryParse(lvi2.SubItems[ColumnNumber].Text, out var i2))
var s1 = lvi1.SubItems[ColumnNumber].Text;
var s2 = lvi2.SubItems[ColumnNumber].Text;
if (_invariantNumber.IsMatch(s1) && _invariantNumber.IsMatch(s2) &&
int.TryParse(s1, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var i1) &&
int.TryParse(s2, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var i2))
{
return i1 > i2 ? 1 : i1 == i2 ? 0 : -1;
}
if (int.TryParse(s1, out var ii1) &&
int.TryParse(s2, out var ii2))
{
return ii1 > ii2 ? 1 : ii1 == ii2 ? 0 : -1;
}
}
if (IsDisplayFileSize)