2022-09-15 02:34:23 +00:00
|
|
|
using System;
|
2014-10-11 07:22:22 +00:00
|
|
|
using System.Collections.Generic;
|
2015-09-21 03:34:09 +00:00
|
|
|
using System.IO;
|
2014-07-22 05:47:18 +00:00
|
|
|
using System.Linq;
|
2021-01-17 01:25:55 +00:00
|
|
|
using System.Threading.Tasks;
|
2014-10-11 07:22:22 +00:00
|
|
|
using System.Windows.Forms;
|
2017-01-08 07:54:09 +00:00
|
|
|
using PKHeX.Core;
|
2021-11-27 23:48:08 +00:00
|
|
|
using PKHeX.Drawing.PokeSprite;
|
2018-04-07 04:23:09 +00:00
|
|
|
using static PKHeX.Core.MessageStrings;
|
2014-12-14 04:02:15 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.WinForms;
|
|
|
|
|
|
|
|
public partial class ReportGrid : Form
|
2014-07-22 05:47:18 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
public ReportGrid()
|
2014-07-22 05:47:18 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
InitializeComponent();
|
|
|
|
CenterToParent();
|
|
|
|
GetContextMenu();
|
|
|
|
}
|
2018-08-04 17:06:06 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void GetContextMenu()
|
|
|
|
{
|
|
|
|
var mnuHide = new ToolStripMenuItem { Name = "mnuHide", Text = MsgReportColumnHide };
|
|
|
|
mnuHide.Click += (sender, e) =>
|
2016-12-28 19:31:32 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
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;
|
2016-12-28 19:31:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
WinFormsUtil.Alert(MsgReportColumnRestoreSuccess);
|
|
|
|
};
|
2016-12-28 19:31:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
ContextMenuStrip mnu = new();
|
|
|
|
mnu.Items.Add(mnuHide);
|
|
|
|
mnu.Items.Add(mnuRestore);
|
2016-12-28 19:31:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
dgData.ContextMenuStrip = mnu;
|
|
|
|
}
|
2018-07-15 18:12:02 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private sealed class PokemonList<T> : SortableBindingList<T> where T : class { }
|
2018-07-15 18:12:02 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public void PopulateData(IList<SlotCache> Data)
|
|
|
|
{
|
|
|
|
SuspendLayout();
|
|
|
|
var PL = new PokemonList<EntitySummaryImage>();
|
|
|
|
var strings = GameInfo.Strings;
|
|
|
|
foreach (var entry in Data)
|
2015-10-11 01:29:02 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var pk = entry.Entity;
|
|
|
|
if ((uint)(pk.Species - 1) >= pk.MaxSpeciesID)
|
2016-06-20 04:22:43 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
continue;
|
2016-06-20 04:22:43 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.Stat_Level = pk.CurrentLevel; // recalc Level
|
|
|
|
PL.Add(new EntitySummaryImage(pk, strings, entry.Identify()));
|
2015-10-11 01:29:02 +00:00
|
|
|
}
|
2018-08-04 17:06:06 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
dgData.DataSource = PL;
|
|
|
|
dgData.AutoGenerateColumns = true;
|
|
|
|
for (int i = 0; i < dgData.Columns.Count; i++)
|
2016-12-28 19:31:32 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var col = dgData.Columns[i];
|
|
|
|
if (col is DataGridViewImageColumn)
|
|
|
|
continue; // Don't add sorting for Sprites
|
|
|
|
col.SortMode = DataGridViewColumnSortMode.Automatic;
|
2016-12-28 19:31:32 +00:00
|
|
|
}
|
2018-08-04 17:06:06 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Trigger Resizing
|
|
|
|
dgData.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
|
|
|
|
for (int i = 0; i < dgData.Columns.Count; i++)
|
2014-12-14 04:02:15 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int w = dgData.Columns[i].Width;
|
|
|
|
dgData.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
|
|
|
|
dgData.Columns[i].Width = w;
|
2014-12-14 04:02:15 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
dgData.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
|
|
|
|
Data_Sorted(this, EventArgs.Empty); // trigger row resizing
|
2018-08-04 17:06:06 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
ResumeLayout();
|
|
|
|
}
|
2014-12-14 04:02:15 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void Data_Sorted(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
int height = SpriteUtil.Spriter.Height + 1; // max height of a row, +1px
|
|
|
|
for (int i = 0; i < dgData.Rows.Count; i++)
|
|
|
|
dgData.Rows[i].Height = height;
|
|
|
|
}
|
2016-07-24 08:31:02 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private 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)
|
|
|
|
{
|
|
|
|
Hide();
|
|
|
|
var path = savecsv.FileName;
|
|
|
|
var t = Task.Run(() => Export_CSV(path));
|
|
|
|
t.Wait(); // don't start disposing until the saving is complete
|
2014-12-14 04:02:15 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-05-12 15:13:39 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private async Task Export_CSV(string path)
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
await using var fs = new FileStream(path, FileMode.Create);
|
|
|
|
await using var s = new StreamWriter(fs, System.Text.Encoding.Unicode);
|
2016-12-08 01:48:45 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var headers = dgData.Columns.Cast<DataGridViewColumn>();
|
|
|
|
await s.WriteLineAsync(string.Join(",", headers.Skip(1).Select(column => $"\"{column.HeaderText}\""))).ConfigureAwait(false);
|
2021-05-29 22:31:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
foreach (var cells in dgData.Rows.Cast<DataGridViewRow>().Select(row => row.Cells.Cast<DataGridViewCell>()))
|
|
|
|
await s.WriteLineAsync(string.Join(",", cells.Skip(1).Select(cell => $"\"{cell.Value}\""))).ConfigureAwait(false);
|
|
|
|
}
|
2016-12-08 01:48:45 +00:00
|
|
|
|
2022-06-18 18:04:24 +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);
|
|
|
|
|
|
|
|
var content = dgData.GetClipboardContent();
|
|
|
|
if (content == null)
|
|
|
|
return base.ProcessCmdKey(ref msg, keyData);
|
2016-12-08 01:48:45 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
string data = content.GetText();
|
|
|
|
var dr = WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgReportExportTable);
|
|
|
|
if (dr != DialogResult.Yes)
|
2018-07-15 18:12:02 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
WinFormsUtil.SetClipboardText(data);
|
|
|
|
return true;
|
2016-12-08 01:48:45 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
// 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;
|
2014-07-22 05:47:18 +00:00
|
|
|
}
|
2016-11-14 15:08:54 +00:00
|
|
|
}
|