2014-07-22 05:47:18 +00:00
|
|
|
|
using System;
|
2014-12-14 03:32:51 +00:00
|
|
|
|
using System.Collections;
|
2014-10-11 07:22:22 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
2015-09-24 03:29:31 +00:00
|
|
|
|
using System.Drawing;
|
2015-09-21 03:34:09 +00:00
|
|
|
|
using System.IO;
|
2014-07-22 05:47:18 +00:00
|
|
|
|
using System.Linq;
|
2015-03-11 01:44:51 +00:00
|
|
|
|
using System.Reflection;
|
2014-07-22 05:47:18 +00:00
|
|
|
|
using System.Text;
|
2014-10-11 07:22:22 +00:00
|
|
|
|
using System.Windows.Forms;
|
2017-01-08 07:54:09 +00:00
|
|
|
|
using PKHeX.Core;
|
2018-04-07 04:23:09 +00:00
|
|
|
|
using static PKHeX.Core.MessageStrings;
|
2014-12-14 04:02:15 +00:00
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.WinForms
|
2014-07-22 05:47:18 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public partial class ReportGrid : Form
|
2014-07-22 05:47:18 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private struct Preview
|
2015-09-24 03:29:31 +00:00
|
|
|
|
{
|
2016-06-20 04:22:43 +00:00
|
|
|
|
private readonly PKM pkm;
|
2017-04-15 21:38:43 +00:00
|
|
|
|
private readonly ushort[] Stats;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
public string Position => pkm.Identifier;
|
2017-01-12 01:55:42 +00:00
|
|
|
|
public Image Sprite => pkm.Sprite();
|
2016-06-20 04:22:43 +00:00
|
|
|
|
public string Nickname => pkm.Nickname;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public string Species => Get(GameInfo.Strings.specieslist, pkm.Species);
|
|
|
|
|
public string Nature => Get(GameInfo.Strings.natures, pkm.Nature);
|
|
|
|
|
public string Gender => Get(Main.GenderSymbols, pkm.Gender);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
public string ESV => pkm.PSV.ToString("0000");
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public string HP_Type => Get(GameInfo.Strings.types, pkm.HPType+1);
|
|
|
|
|
public string Ability => Get(GameInfo.Strings.abilitylist, pkm.Ability);
|
|
|
|
|
public string Move1 => Get(GameInfo.Strings.movelist, pkm.Move1);
|
|
|
|
|
public string Move2 => Get(GameInfo.Strings.movelist, pkm.Move2);
|
|
|
|
|
public string Move3 => Get(GameInfo.Strings.movelist, pkm.Move3);
|
|
|
|
|
public string Move4 => Get(GameInfo.Strings.movelist, pkm.Move4);
|
|
|
|
|
public string HeldItem => Get(GameInfo.Strings.itemlist, pkm.HeldItem);
|
2017-04-15 21:38:43 +00:00
|
|
|
|
public string HP => Stats[0].ToString();
|
|
|
|
|
public string ATK => Stats[1].ToString();
|
|
|
|
|
public string DEF => Stats[2].ToString();
|
|
|
|
|
public string SPA => Stats[4].ToString();
|
|
|
|
|
public string SPD => Stats[5].ToString();
|
|
|
|
|
public string SPE => Stats[3].ToString();
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public string MetLoc => pkm.GetLocationString(eggmet: false);
|
|
|
|
|
public string EggLoc => pkm.GetLocationString(eggmet: true);
|
|
|
|
|
public string Ball => Get(GameInfo.Strings.balllist, pkm.Ball);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
public string OT => pkm.OT_Name;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public string Version => Get(GameInfo.Strings.gamelist, pkm.Version);
|
|
|
|
|
public string OTLang => Get(GameInfo.Strings.gamelanguages, pkm.Language) ?? $"UNK {pkm.Language}";
|
2017-02-28 02:25:22 +00:00
|
|
|
|
public string Legal { get { var la = new LegalityAnalysis(pkm); return la.Parsed ? la.Valid.ToString() : "-"; } }
|
2016-11-14 15:08:54 +00:00
|
|
|
|
public string CountryID => pkm.Format > 5 ? pkm.Country.ToString() : "N/A";
|
|
|
|
|
public string RegionID => pkm.Format > 5 ? pkm.Region.ToString() : "N/A";
|
|
|
|
|
public string DSRegionID => pkm.Format > 5 ? pkm.ConsoleRegion.ToString() : "N/A";
|
2015-09-24 03:29:31 +00:00
|
|
|
|
|
|
|
|
|
#region Extraneous
|
2016-06-20 04:22:43 +00:00
|
|
|
|
public string EC => pkm.EncryptionConstant.ToString("X8");
|
|
|
|
|
public string PID => pkm.PID.ToString("X8");
|
|
|
|
|
public int HP_IV => pkm.IV_HP;
|
|
|
|
|
public int ATK_IV => pkm.IV_ATK;
|
|
|
|
|
public int DEF_IV => pkm.IV_DEF;
|
|
|
|
|
public int SPA_IV => pkm.IV_SPA;
|
|
|
|
|
public int SPD_IV => pkm.IV_SPD;
|
|
|
|
|
public int SPE_IV => pkm.IV_SPE;
|
|
|
|
|
public uint EXP => pkm.EXP;
|
|
|
|
|
public int Level => pkm.CurrentLevel;
|
|
|
|
|
public int HP_EV => pkm.EV_HP;
|
|
|
|
|
public int ATK_EV => pkm.EV_ATK;
|
|
|
|
|
public int DEF_EV => pkm.EV_DEF;
|
|
|
|
|
public int SPA_EV => pkm.EV_SPA;
|
|
|
|
|
public int SPD_EV => pkm.EV_SPD;
|
|
|
|
|
public int SPE_EV => pkm.EV_SPE;
|
2018-06-03 04:19:03 +00:00
|
|
|
|
public int Cool => pkm is IContestStats s ? s.CNT_Cool : 0;
|
|
|
|
|
public int Beauty => pkm is IContestStats s ? s.CNT_Beauty : 0;
|
|
|
|
|
public int Cute => pkm is IContestStats s ? s.CNT_Cute : 0;
|
|
|
|
|
public int Smart => pkm is IContestStats s ? s.CNT_Smart : 0;
|
|
|
|
|
public int Tough => pkm is IContestStats s ? s.CNT_Tough : 0;
|
|
|
|
|
public int Sheen => pkm is IContestStats s ? s.CNT_Sheen : 0;
|
2016-10-31 02:15:48 +00:00
|
|
|
|
public int Markings => pkm.MarkValue;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
2016-11-14 15:08:54 +00:00
|
|
|
|
public string NotOT => pkm.Format > 5 ? pkm.HT_Name : "N/A";
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
2016-11-14 15:08:54 +00:00
|
|
|
|
public int AbilityNum => pkm.Format > 5 ? pkm.AbilityNumber : -1;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
public int GenderFlag => pkm.Gender;
|
|
|
|
|
public int AltForms => pkm.AltForm;
|
|
|
|
|
public int PKRS_Strain => pkm.PKRS_Strain;
|
|
|
|
|
public int PKRS_Days => pkm.PKRS_Days;
|
|
|
|
|
public int MetLevel => pkm.Met_Level;
|
|
|
|
|
public int OT_Gender => pkm.OT_Gender;
|
|
|
|
|
|
|
|
|
|
public bool FatefulFlag => pkm.FatefulEncounter;
|
|
|
|
|
public bool IsEgg => pkm.IsEgg;
|
|
|
|
|
public bool IsNicknamed => pkm.IsNicknamed;
|
|
|
|
|
public bool IsShiny => pkm.IsShiny;
|
|
|
|
|
|
|
|
|
|
public int TID => pkm.TID;
|
|
|
|
|
public int SID => pkm.SID;
|
|
|
|
|
public int TSV => pkm.TSV;
|
|
|
|
|
public int Move1_PP => pkm.Move1_PP;
|
|
|
|
|
public int Move2_PP => pkm.Move2_PP;
|
|
|
|
|
public int Move3_PP => pkm.Move3_PP;
|
|
|
|
|
public int Move4_PP => pkm.Move4_PP;
|
|
|
|
|
public int Move1_PPUp => pkm.Move1_PPUps;
|
|
|
|
|
public int Move2_PPUp => pkm.Move2_PPUps;
|
|
|
|
|
public int Move3_PPUp => pkm.Move3_PPUps;
|
|
|
|
|
public int Move4_PPUp => pkm.Move4_PPUps;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public string Relearn1 => Get(GameInfo.Strings.movelist, pkm.RelearnMove1);
|
|
|
|
|
public string Relearn2 => Get(GameInfo.Strings.movelist, pkm.RelearnMove2);
|
|
|
|
|
public string Relearn3 => Get(GameInfo.Strings.movelist, pkm.RelearnMove3);
|
|
|
|
|
public string Relearn4 => Get(GameInfo.Strings.movelist, pkm.RelearnMove4);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
public ushort Checksum => pkm.Checksum;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public int Friendship => pkm.OT_Friendship;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
public int OT_Affection => pkm.OT_Affection;
|
2016-08-10 14:02:31 +00:00
|
|
|
|
public int Egg_Year => pkm.EggMetDate.GetValueOrDefault().Year;
|
|
|
|
|
public int Egg_Month => pkm.EggMetDate.GetValueOrDefault().Month;
|
|
|
|
|
public int Egg_Day => pkm.EggMetDate.GetValueOrDefault().Day;
|
|
|
|
|
public int Met_Year => pkm.MetDate.GetValueOrDefault().Year;
|
|
|
|
|
public int Met_Month => pkm.MetDate.GetValueOrDefault().Month;
|
|
|
|
|
public int Met_Day => pkm.MetDate.GetValueOrDefault().Day;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
public int Encounter => pkm.EncounterType;
|
2015-09-24 03:29:31 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
2017-04-15 21:38:43 +00:00
|
|
|
|
|
|
|
|
|
public Preview(PKM p)
|
|
|
|
|
{
|
|
|
|
|
pkm = p;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
Stats = pkm.GetStats(pkm.PersonalInfo);
|
2017-04-15 21:38:43 +00:00
|
|
|
|
}
|
2017-02-24 00:39:03 +00:00
|
|
|
|
|
2018-06-11 13:36:43 +00:00
|
|
|
|
private static string Get(IReadOnlyList<string> arr, int val) => arr?.Count > val && val >= 0 ? arr[val] : null;
|
2015-09-24 03:29:31 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public ReportGrid()
|
2014-07-22 05:47:18 +00:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2014-07-24 16:20:08 +00:00
|
|
|
|
dgData.DoubleBuffered(true);
|
2016-04-15 23:11:46 +00:00
|
|
|
|
CenterToParent();
|
2017-06-18 01:37:19 +00:00
|
|
|
|
GetContextMenu();
|
2016-12-28 19:31:32 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void GetContextMenu()
|
2016-12-28 19:31:32 +00:00
|
|
|
|
{
|
2018-04-07 04:23:09 +00:00
|
|
|
|
var mnuHide = new ToolStripMenuItem { Name = "mnuHide", Text = MsgReportColumnHide, };
|
2016-12-28 19:31:32 +00:00
|
|
|
|
mnuHide.Click += (sender, e) =>
|
|
|
|
|
{
|
|
|
|
|
int c = dgData.SelectedCells.Count;
|
|
|
|
|
if (c == 0)
|
2018-04-07 04:23:09 +00:00
|
|
|
|
{ WinFormsUtil.Alert(MsgReportColumnHideFail); return; }
|
2016-12-28 19:31:32 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < c; i++)
|
|
|
|
|
dgData.Columns[dgData.SelectedCells[i].ColumnIndex].Visible = false;
|
|
|
|
|
};
|
2018-04-07 04:23:09 +00:00
|
|
|
|
var mnuRestore = new ToolStripMenuItem { Name = "mnuRestore", Text = MsgReportColumnRestore, };
|
2016-12-28 19:31:32 +00:00
|
|
|
|
mnuRestore.Click += (sender, e) =>
|
|
|
|
|
{
|
|
|
|
|
int c = dgData.ColumnCount;
|
|
|
|
|
for (int i = 0; i < c; i++)
|
|
|
|
|
dgData.Columns[i].Visible = true;
|
|
|
|
|
|
2018-04-07 04:23:09 +00:00
|
|
|
|
WinFormsUtil.Alert(MsgReportColumnRestoreSuccess);
|
2016-12-28 19:31:32 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ContextMenuStrip mnu = new ContextMenuStrip();
|
|
|
|
|
mnu.Items.Add(mnuHide);
|
|
|
|
|
mnu.Items.Add(mnuRestore);
|
|
|
|
|
|
|
|
|
|
dgData.ContextMenuStrip = mnu;
|
2014-07-22 05:47:18 +00:00
|
|
|
|
}
|
2017-09-29 05:20:27 +00:00
|
|
|
|
public void PopulateData(IList<PKM> Data)
|
2015-10-11 01:29:02 +00:00
|
|
|
|
{
|
2016-07-24 08:31:02 +00:00
|
|
|
|
SuspendLayout();
|
2015-10-11 01:29:02 +00:00
|
|
|
|
BoxBar.Step = 1;
|
|
|
|
|
PokemonList PL = new PokemonList();
|
2016-06-20 04:22:43 +00:00
|
|
|
|
foreach (PKM pkm in Data.Where(pkm => pkm.ChecksumValid && pkm.Species != 0))
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
pkm.Stat_Level = PKX.GetLevel(pkm.Species, pkm.EXP); // recalc Level
|
2016-06-20 04:22:43 +00:00
|
|
|
|
PL.Add(new Preview(pkm));
|
|
|
|
|
BoxBar.PerformStep();
|
|
|
|
|
}
|
2015-10-11 01:29:02 +00:00
|
|
|
|
|
|
|
|
|
dgData.DataSource = PL;
|
|
|
|
|
dgData.AutoGenerateColumns = true;
|
2017-09-29 05:20:27 +00:00
|
|
|
|
BoxBar.Maximum = Data.Count + dgData.Columns.Count;
|
2015-10-11 01:29:02 +00:00
|
|
|
|
for (int i = 0; i < dgData.Columns.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
BoxBar.PerformStep();
|
|
|
|
|
if (dgData.Columns[i] is DataGridViewImageColumn) continue; // Don't add sorting for Sprites
|
|
|
|
|
dgData.Columns[i].SortMode = DataGridViewColumnSortMode.Automatic;
|
|
|
|
|
}
|
|
|
|
|
BoxBar.Visible = false;
|
2016-12-28 19:31:32 +00:00
|
|
|
|
|
|
|
|
|
// Trigger Resizing
|
|
|
|
|
dgData.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
|
|
|
|
|
for (int i = 0; i < dgData.Columns.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
int w = dgData.Columns[i].Width;
|
|
|
|
|
dgData.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
|
|
|
|
|
dgData.Columns[i].Width = w;
|
|
|
|
|
}
|
|
|
|
|
dgData.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
Data_Sorted(null, null); // trigger row resizing
|
2016-12-28 19:31:32 +00:00
|
|
|
|
|
2016-07-24 08:31:02 +00:00
|
|
|
|
ResumeLayout();
|
2015-10-11 01:29:02 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void Data_Sorted(object sender, EventArgs e)
|
2016-12-28 19:31:32 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
int height = PKMUtil.GetSprite(1, 0, 0, 0, false, false).Height + 1; // dummy sprite, max height of a row
|
2016-12-28 19:31:32 +00:00
|
|
|
|
for (int i = 0; i < dgData.Rows.Count; i++)
|
|
|
|
|
dgData.Rows[i].Height = height;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void PromptSaveCSV(object sender, FormClosingEventArgs e)
|
2014-12-14 04:02:15 +00:00
|
|
|
|
{
|
2018-04-07 04:23:09 +00:00
|
|
|
|
if (WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgReportExportCSV) != DialogResult.Yes)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return;
|
|
|
|
|
SaveFileDialog savecsv = new SaveFileDialog
|
2014-12-14 04:02:15 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
Filter = "Spreadsheet|*.csv",
|
|
|
|
|
FileName = "Box Data Dump.csv"
|
|
|
|
|
};
|
|
|
|
|
if (savecsv.ShowDialog() == DialogResult.OK)
|
|
|
|
|
Export_CSV(savecsv.FileName);
|
2014-12-14 04:02:15 +00:00
|
|
|
|
}
|
|
|
|
|
private void Export_CSV(string path)
|
|
|
|
|
{
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
var headers = dgData.Columns.Cast<DataGridViewColumn>();
|
2016-07-24 08:31:02 +00:00
|
|
|
|
sb.AppendLine(string.Join(",", headers.Select(column => $"\"{column.HeaderText}\"")));
|
|
|
|
|
|
|
|
|
|
foreach (var cells in from DataGridViewRow row in dgData.Rows select row.Cells.Cast<DataGridViewCell>())
|
|
|
|
|
sb.AppendLine(string.Join(",", cells.Select(cell => $"\"{cell.Value}\"")));
|
2014-12-14 04:02:15 +00:00
|
|
|
|
|
2015-09-21 03:34:09 +00:00
|
|
|
|
File.WriteAllText(path, sb.ToString(), Encoding.UTF8);
|
2014-12-14 04:02:15 +00:00
|
|
|
|
}
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2016-12-08 01:48:45 +00:00
|
|
|
|
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
|
|
|
|
|
{
|
|
|
|
|
bool cp = keyData == (Keys.Control | Keys.C) && ActiveControl is DataGridView;
|
|
|
|
|
if (!cp)
|
|
|
|
|
return base.ProcessCmdKey(ref msg, keyData);
|
|
|
|
|
|
|
|
|
|
string data = dgData.GetClipboardContent().GetText();
|
2018-04-07 04:23:09 +00:00
|
|
|
|
var dr = WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgReportExportTable);
|
2016-12-08 01:48:45 +00:00
|
|
|
|
if (dr != DialogResult.Yes)
|
|
|
|
|
{
|
|
|
|
|
Clipboard.SetText(data);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reformat datagrid clipboard content
|
|
|
|
|
string[] lines = data.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
|
|
|
|
|
int tabcount = lines[0].Count(c => c == '\t');
|
|
|
|
|
|
|
|
|
|
string[] newlines = new string[lines.Length + 1];
|
|
|
|
|
newlines[0] = lines[0].Replace('\t', '|');
|
2018-05-13 02:07:32 +00:00
|
|
|
|
newlines[1] = string.Join(":--:", new int[tabcount + 2].Select(_ => '|')); // 2 pipes for each end
|
2016-12-08 01:48:45 +00:00
|
|
|
|
for (int i = 1; i < lines.Length; i++)
|
|
|
|
|
newlines[i + 1] = lines[i].Replace('\t', '|');
|
|
|
|
|
|
|
|
|
|
Clipboard.SetText(string.Join(Environment.NewLine, newlines));
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-12-14 04:02:15 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private sealed class PokemonList : SortableBindingList<Preview> { }
|
2014-07-22 05:47:18 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static class ExtensionMethods
|
2014-07-24 16:20:08 +00:00
|
|
|
|
{
|
|
|
|
|
public static void DoubleBuffered(this DataGridView dgv, bool setting)
|
|
|
|
|
{
|
|
|
|
|
Type dgvType = dgv.GetType();
|
|
|
|
|
PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
|
|
|
|
|
pi.SetValue(dgv, setting, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-14 03:32:51 +00:00
|
|
|
|
public class SortableBindingList<T> : BindingList<T>
|
|
|
|
|
{
|
|
|
|
|
private readonly Dictionary<Type, PropertyComparer<T>> comparers;
|
|
|
|
|
private bool isSorted;
|
|
|
|
|
private ListSortDirection listSortDirection;
|
|
|
|
|
private PropertyDescriptor propertyDescriptor;
|
|
|
|
|
|
2016-06-20 04:22:43 +00:00
|
|
|
|
protected SortableBindingList() : base(new List<T>())
|
2014-12-14 03:32:51 +00:00
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
comparers = new Dictionary<Type, PropertyComparer<T>>();
|
2014-12-14 03:32:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-17 21:27:24 +00:00
|
|
|
|
protected override bool SupportsSortingCore => true;
|
2014-12-14 03:32:51 +00:00
|
|
|
|
|
2016-01-17 21:27:24 +00:00
|
|
|
|
protected override bool IsSortedCore => isSorted;
|
2014-12-14 03:32:51 +00:00
|
|
|
|
|
2016-01-17 21:27:24 +00:00
|
|
|
|
protected override PropertyDescriptor SortPropertyCore => propertyDescriptor;
|
2014-12-14 03:32:51 +00:00
|
|
|
|
|
2016-01-17 21:27:24 +00:00
|
|
|
|
protected override ListSortDirection SortDirectionCore => listSortDirection;
|
2014-12-14 03:32:51 +00:00
|
|
|
|
|
2016-01-17 21:27:24 +00:00
|
|
|
|
protected override bool SupportsSearchingCore => true;
|
2014-12-14 03:32:51 +00:00
|
|
|
|
|
2018-05-13 02:07:32 +00:00
|
|
|
|
protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
|
2014-12-14 03:32:51 +00:00
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
List<T> itemsList = (List<T>)Items;
|
2014-12-14 03:32:51 +00:00
|
|
|
|
|
2018-05-13 02:07:32 +00:00
|
|
|
|
Type propertyType = prop.PropertyType;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (!comparers.TryGetValue(propertyType, out PropertyComparer<T> comparer))
|
2014-12-14 03:32:51 +00:00
|
|
|
|
{
|
2018-05-13 02:07:32 +00:00
|
|
|
|
comparer = new PropertyComparer<T>(prop, direction);
|
2015-03-11 01:44:51 +00:00
|
|
|
|
comparers.Add(propertyType, comparer);
|
2014-12-14 03:32:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-13 02:07:32 +00:00
|
|
|
|
comparer.SetPropertyAndDirection(prop, direction);
|
2014-12-14 03:32:51 +00:00
|
|
|
|
itemsList.Sort(comparer);
|
|
|
|
|
|
2018-05-13 02:07:32 +00:00
|
|
|
|
propertyDescriptor = prop;
|
2015-03-11 01:44:51 +00:00
|
|
|
|
listSortDirection = direction;
|
|
|
|
|
isSorted = true;
|
2014-12-14 03:32:51 +00:00
|
|
|
|
|
2015-03-11 01:44:51 +00:00
|
|
|
|
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
|
2014-12-14 03:32:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void RemoveSortCore()
|
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
isSorted = false;
|
|
|
|
|
propertyDescriptor = base.SortPropertyCore;
|
|
|
|
|
listSortDirection = base.SortDirectionCore;
|
2014-12-14 03:32:51 +00:00
|
|
|
|
|
2015-03-11 01:44:51 +00:00
|
|
|
|
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
|
2014-12-14 03:32:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-13 02:07:32 +00:00
|
|
|
|
protected override int FindCore(PropertyDescriptor prop, object key)
|
2014-12-14 03:32:51 +00:00
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
int count = Count;
|
2014-12-14 03:32:51 +00:00
|
|
|
|
for (int i = 0; i < count; ++i)
|
2018-05-13 02:07:32 +00:00
|
|
|
|
if (prop.GetValue(this[i]).Equals(key))
|
2014-12-14 03:32:51 +00:00
|
|
|
|
return i;
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public class PropertyComparer<T> : IComparer<T>
|
|
|
|
|
{
|
|
|
|
|
private readonly IComparer comparer;
|
|
|
|
|
private PropertyDescriptor propertyDescriptor;
|
|
|
|
|
private int reverse;
|
|
|
|
|
|
|
|
|
|
public PropertyComparer(PropertyDescriptor property, ListSortDirection direction)
|
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
propertyDescriptor = property;
|
2014-12-14 03:32:51 +00:00
|
|
|
|
Type comparerForPropertyType = typeof(Comparer<>).MakeGenericType(property.PropertyType);
|
2015-03-11 01:44:51 +00:00
|
|
|
|
comparer = (IComparer)comparerForPropertyType.InvokeMember("Default", BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.Public, null, null, null);
|
|
|
|
|
SetListSortDirection(direction);
|
2014-12-14 03:32:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region IComparer<T> Members
|
|
|
|
|
|
|
|
|
|
public int Compare(T x, T y)
|
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
return reverse * comparer.Compare(propertyDescriptor.GetValue(x), propertyDescriptor.GetValue(y));
|
2014-12-14 03:32:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
private void SetPropertyDescriptor(PropertyDescriptor descriptor)
|
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
propertyDescriptor = descriptor;
|
2014-12-14 03:32:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetListSortDirection(ListSortDirection direction)
|
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
reverse = direction == ListSortDirection.Ascending ? 1 : -1;
|
2014-12-14 03:32:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetPropertyAndDirection(PropertyDescriptor descriptor, ListSortDirection direction)
|
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
SetPropertyDescriptor(descriptor);
|
|
|
|
|
SetListSortDirection(direction);
|
2014-12-14 03:32:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-14 15:08:54 +00:00
|
|
|
|
}
|