Обновление отображения

This commit is contained in:
2025-04-27 12:22:52 +04:00
parent 76167f1b24
commit 06d154eccc
9 changed files with 523 additions and 402 deletions
+143
View File
@@ -0,0 +1,143 @@
using Pf2eModel;
using Pf2eModel.Model.Base;
using Pf2eModel.Model.Common.I18n;
using Pf2eModel.Model.Enum;
namespace Pf2eViewer;
public partial class DatabaseViewerControl : UserControl
{
public readonly Pf2eDbContext Context = new();
private int _perPage = 30;
private int _page = 1;
private EntityType _type;
public int PerPage
{
get => _perPage;
set
{
_perPage = value;
UpdateView();
}
}
public int Page
{
get => _page;
set
{
_page = value;
UpdateView();
}
}
public int Count { get; private set; }
public int MaxPage => Convert.ToInt32(Math.Ceiling(1.0 * Count / PerPage));
public EntityView CurrentEntity { get; private set; } = null!;
public EntityType Type
{
get => _type;
set
{
_type = value;
UpdateView();
}
}
internal bool _updating = false;
public DatabaseViewerControl()
{
InitializeComponent();
BindingNavigator.BindingSource = DbBindingSource;
DataGridView.AutoGenerateColumns = true;
DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
private void DatabaseViewerControl_Load(object sender, EventArgs e)
{
UpdateView();
}
private void BindingNavigator_RefreshItems(object sender, EventArgs e)
{
if (_updating)
{
return;
}
Page = (int)(BindingNavigator?.BindingSource?.Current ?? 1);
UpdateView();
}
public void UpdateView()
{
_updating = true;
IQueryable<Entity> query = Context.Entities.Where(e =>
e.Type == Type
&& (
// ReSharper disable once EntityFramework.UnsupportedServerSideFunctionCall
SearchTextBox.Text == ""
|| Pf2eDbContext.LocaleSearch(e.Name, SearchTextBox.Text)
)
);
Count = query.Count();
PagesBindingSource.DataSource = Enumerable.Range(1, MaxPage).ToList();
BindingNavigator.BindingSource = PagesBindingSource;
DbBindingSource.DataSource = query
.Skip((Page - 1) * PerPage)
.Take(PerPage)
.Select(e => new EntityGridRow(e))
.ToList();
if (DataGridView.Rows.Count > 0)
{
DataGridView.Rows[0].Selected = true;
}
DataGridView.Update();
_updating = false;
}
private void DataGridView_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
if (DataGridView.SelectedRows.Count == 0)
{
return;
}
EntityGridRow data = (EntityGridRow)DataGridView.SelectedRows[0].DataBoundItem;
CurrentEntity = new(Context, Type, data.Id);
EntityChange?.Invoke(CurrentEntity);
}
public delegate void EntityChangeHandler(EntityView newEntity);
public event EntityChangeHandler? EntityChange;
internal class EntityGridRow(Entity e)
{
public int Id { get; set; } = e.Id;
public string Name { get; set; } = e.Name[LocaleType.English];
public EntityRarity Rarity { get; set; } = e.Rarity;
private readonly Entity _e = e;
public Entity GetEntity() => _e;
}
}