PKHeX/PKHeX.WinForms/Subforms/ReportGrid.cs

158 lines
6 KiB
C#
Raw Normal View History

2014-07-22 05:47:18 +00:00
using System;
2014-10-11 07:22:22 +00:00
using System.Collections.Generic;
using System.IO;
2014-07-22 05:47:18 +00:00
using System.Linq;
using System.Threading.Tasks;
2014-10-11 07:22:22 +00:00
using System.Windows.Forms;
using PKHeX.Core;
using PKHeX.Drawing;
using static PKHeX.Core.MessageStrings;
2014-12-14 04:02:15 +00:00
namespace PKHeX.WinForms
2014-07-22 05:47:18 +00:00
{
public partial class ReportGrid : Form
2014-07-22 05:47:18 +00:00
{
public ReportGrid()
2014-07-22 05:47:18 +00:00
{
InitializeComponent();
2014-07-24 16:20:08 +00:00
dgData.DoubleBuffered(true);
CenterToParent();
GetContextMenu();
}
2018-08-04 17:06:06 +00:00
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;
2014-07-22 05:47:18 +00:00
}
private sealed class PokemonList<T> : SortableBindingList<T> where T : class { }
public void PopulateData(IList<PKM> Data)
{
2016-07-24 08:31:02 +00:00
SuspendLayout();
BoxBar.Step = 1;
2019-07-14 00:43:05 +00:00
var PL = new PokemonList<PKMSummaryImage>();
2018-08-04 17:06:06 +00:00
var strings = GameInfo.Strings;
foreach (PKM pkm in Data.Where(pkm => pkm.ChecksumValid && pkm.Species != 0))
{
2019-11-16 01:34:18 +00:00
pkm.Stat_Level = Experience.GetLevel(pkm.EXP, pkm.PersonalInfo.EXPGrowth); // recalc Level
2019-07-14 00:43:05 +00:00
PL.Add(new PKMSummaryImage(pkm, strings));
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
2016-07-24 08:31:02 +00:00
ResumeLayout();
}
2018-08-04 17:06:06 +00:00
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;
}
2018-08-04 17:06:06 +00:00
private async void PromptSaveCSV(object sender, FormClosingEventArgs e)
2014-12-14 04:02:15 +00:00
{
if (WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgReportExportCSV) != DialogResult.Yes)
return;
using var savecsv = new SaveFileDialog
2014-12-14 04:02:15 +00:00
{
Filter = "Spreadsheet|*.csv",
FileName = "Box Data Dump.csv"
};
if (savecsv.ShowDialog() == DialogResult.OK)
await Export_CSV(savecsv.FileName).ConfigureAwait(false);
2014-12-14 04:02:15 +00:00
}
2018-08-04 17:06:06 +00:00
private async Task Export_CSV(string path)
2014-12-14 04:02:15 +00:00
{
using var fs = new FileStream(path, FileMode.Create);
using var s = new StreamWriter(fs);
2014-12-14 04:02:15 +00:00
var headers = dgData.Columns.Cast<DataGridViewColumn>();
await s.WriteLineAsync(string.Join(",", headers.Skip(1).Select(column => $"\"{column.HeaderText}\""))).ConfigureAwait(false);
2016-07-24 08:31:02 +00:00
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);
2014-12-14 04:02:15 +00:00
}
2018-05-12 15:13:39 +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();
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;
}
2014-07-22 05:47:18 +00:00
}
}