PKHeX/PKHeX.WinForms/Subforms/ReportGrid.cs
Kurt 3e7775fc44
Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately (#3222)
* Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately

Don't store within the object, track the slot origin data separately.

Batch editing now pre-filters if using Box/Slot/Identifier logic; split up mods/filters as they're starting to get pretty hefty.

- Requesting a Box Data report now shows all slots in the save file (party, misc)
- Can now exclude backup saves from database search via toggle (separate from settings preventing load entirely)
- Replace some linq usages with direct code

* Remove WasLink virtual in PKM

Inline any logic, since we now have encounter objects to indicate matching, rather than the proto-legality logic checking properties of a PKM.

* Use Fateful to directly check gen5 mysterygift origins

No other encounter types in gen5 apply Fateful

* Simplify double ball comparison

Used to be separate for deferral cases, now no longer needed to be separate.

* Grab move/relearn reference and update locally

Fix relearn move identifier

* Inline defog HM transfer preference check

HasMove is faster than getting moves & checking contains. Skips allocation by setting values directly.

* Extract more met location metadata checks: WasBredEgg

* Replace Console.Write* with Debug.Write*

There's no console output UI, so don't include them in release builds.

* Inline WasGiftEgg, WasEvent, and WasEventEgg logic

Adios legality tags that aren't entirely correct for the specific format. Just put the computations in EncounterFinder.
2021-06-22 20:23:48 -07:00

162 lines
6.1 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using PKHeX.Core;
using PKHeX.Drawing;
using static PKHeX.Core.MessageStrings;
namespace PKHeX.WinForms
{
public partial class ReportGrid : Form
{
public ReportGrid()
{
InitializeComponent();
dgData.DoubleBuffered(true);
CenterToParent();
GetContextMenu();
}
private void GetContextMenu()
{
var mnuHide = new ToolStripMenuItem { Name = "mnuHide", Text = MsgReportColumnHide, };
mnuHide.Click += (sender, e) =>
{
int c = dgData.SelectedCells.Count;
if (c == 0)
{ WinFormsUtil.Alert(MsgReportColumnHideFail); return; }
for (int i = 0; i < c; i++)
dgData.Columns[dgData.SelectedCells[i].ColumnIndex].Visible = false;
};
var mnuRestore = new ToolStripMenuItem { Name = "mnuRestore", Text = MsgReportColumnRestore, };
mnuRestore.Click += (sender, e) =>
{
int c = dgData.ColumnCount;
for (int i = 0; i < c; i++)
dgData.Columns[i].Visible = true;
WinFormsUtil.Alert(MsgReportColumnRestoreSuccess);
};
ContextMenuStrip mnu = new();
mnu.Items.Add(mnuHide);
mnu.Items.Add(mnuRestore);
dgData.ContextMenuStrip = mnu;
}
private sealed class PokemonList<T> : SortableBindingList<T> where T : class { }
public void PopulateData(IList<SlotCache> Data)
{
SuspendLayout();
BoxBar.Step = 1;
var PL = new PokemonList<EntitySummaryImage>();
var strings = GameInfo.Strings;
foreach (var entry in Data)
{
var pkm = entry.Entity;
pkm.Stat_Level = pkm.CurrentLevel; // recalc Level
PL.Add(new EntitySummaryImage(pkm, strings, entry.Identify()));
BoxBar.PerformStep();
}
dgData.DataSource = PL;
dgData.AutoGenerateColumns = true;
BoxBar.Maximum = Data.Count + dgData.Columns.Count;
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;
// 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;
Data_Sorted(this, EventArgs.Empty); // trigger row resizing
ResumeLayout();
}
private void Data_Sorted(object sender, EventArgs e)
{
int height = SpriteUtil.GetSprite(1, 0, 0, 0, 0, false, false).Height + 1; // dummy sprite, max height of a row
for (int i = 0; i < dgData.Rows.Count; i++)
dgData.Rows[i].Height = height;
}
private async void PromptSaveCSV(object sender, FormClosingEventArgs e)
{
if (WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgReportExportCSV) != DialogResult.Yes)
return;
using var savecsv = new SaveFileDialog
{
Filter = "Spreadsheet|*.csv",
FileName = "Box Data Dump.csv"
};
if (savecsv.ShowDialog() == DialogResult.OK)
await Export_CSV(savecsv.FileName).ConfigureAwait(false);
}
private async Task Export_CSV(string path)
{
using var fs = new FileStream(path, FileMode.Create);
using var s = new StreamWriter(fs);
var headers = dgData.Columns.Cast<DataGridViewColumn>();
await s.WriteLineAsync(string.Join(",", headers.Skip(1).Select(column => $"\"{column.HeaderText}\""))).ConfigureAwait(false);
foreach (var cells in from DataGridViewRow row in dgData.Rows select row.Cells.Cast<DataGridViewCell>())
await s.WriteLineAsync(string.Join(",", cells.Skip(1).Select(cell => $"\"{cell.Value}\""))).ConfigureAwait(false);
}
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);
var content = dgData.GetClipboardContent();
if (content == null)
return base.ProcessCmdKey(ref msg, keyData);
string data = content.GetText();
var dr = WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgReportExportTable);
if (dr != DialogResult.Yes)
{
WinFormsUtil.SetClipboardText(data);
return true;
}
// Reformat datagrid clipboard content
string[] lines = data.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
string[] newlines = ConvertTabbedToRedditTable(lines);
WinFormsUtil.SetClipboardText(string.Join(Environment.NewLine, newlines));
return true;
}
private static string[] ConvertTabbedToRedditTable(string[] lines)
{
string[] newlines = new string[lines.Length + 1];
int tabcount = lines[0].Count(c => c == '\t');
newlines[0] = lines[0].Replace('\t', '|');
newlines[1] = string.Join(":--:", Enumerable.Repeat('|', tabcount + 2)); // 2 pipes for each end
for (int i = 1; i < lines.Length; i++)
newlines[i + 1] = lines[i].Replace('\t', '|');
return newlines;
}
}
}