You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

576 lines
17 KiB
C#

9 months ago
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using NLog;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Metro;
using System.Linq;
using System.ComponentModel;
using JwKdsV.Entity.Normal;
using JwKdsV.Core;
using JwKdsV.Core.Bean;
namespace JwKdsV.Component
{
[ToolboxItem(true)]
public partial class Category : BaseUserControl
{
private Logger logger = NLog.LogManager.GetCurrentClassLogger();
/// <summary>
/// 左右分页默认的宽度
/// </summary>
private const int ArrowWidth = 40;
public Category()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
//base.OnPaint(e);
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
ControlPaint.DrawBorder(e.Graphics, rect, Color.Transparent, ButtonBorderStyle.Solid);//画个边框
//ControlPaint.DrawBorder(e.Graphics , rect ,
// Color.Transparent , 1 , ButtonBorderStyle.Solid ,
// Color.Transparent , 1 , ButtonBorderStyle.Solid ,
// Color.Transparent , 1 , ButtonBorderStyle.Solid ,
// Color.Transparent , 1 , ButtonBorderStyle.Solid
//);
}
public event CategoryEventHandler CategoryCheckedChanged;
protected virtual void OnCategoryCheckedChanged(CategoryEventArgs e)
{
CategoryCheckedChanged?.Invoke(this, e);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.DesignMode) return;
//左分页
this.ButtonLeftArrow.Visible = true;
//右分页
this.ButtonRightArrow.Visible = true;
//当前页码
this.PageNumber = 1;
this.RefreshUi();
}
private void RefreshUi()
{
this.Height = (this.Visible ? this.Rows * this.ItemHeight : 0);
this.BindDataSource(this._dataSource);
}
/// <summary>
/// 每页的大小
/// </summary>
public int PageSize { get; private set; }
/// <summary>
/// 总页数
/// </summary>
public int PageCount => this.PageSize <= 0 ? 0 : ((this.TotalCount + this.PageSize - 1) / this.PageSize);
/// <summary>
/// 当前页码
/// </summary>
public int PageNumber { get; set; }
/// <summary>
/// 总数量
/// </summary>
public int TotalCount { get; private set; }
private int _columns = 1;
/// <summary>
/// 每一行显示的数量
/// </summary>
public int Columns
{
get
{
return _columns;
}
set
{
_columns = (value <= 0 ? 1 : value);
//当前页码
this.PageNumber = 1;
RefreshUi();
}
}
/// <summary>
/// 分页List数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public List<ProductType> ListPager(List<ProductType> data)
{
var result = new List<ProductType>();
result.AddRange(data.Skip((this.PageNumber - 1) * this.PageSize).Take(this.PageSize));
return result;
}
private MetroTileItem _selectedItem = null;
public MetroTileItem SelectedItem
{
get { return this._selectedItem; }
private set { this._selectedItem = value; }
}
/// <summary>
/// 绑定的数据源
/// </summary>
private List<ProductType> _dataSource = null;
/// <summary>
/// 绑定品类数据源
/// </summary>
/// <param name="dataSource"></param>
public void BindDataSource(List<ProductType> dataSource)
{
try
{
if (dataSource == null)
{
return;
}
this._dataSource = dataSource;
//总记录
this.TotalCount = dataSource.Count;
//每页的显示数量
this.PageSize = this.Rows * this.Columns;
//是否需要分页
this.DisplayPager = this._autoPager ? this.PageCount > 1 : true;
this.ButtonLeftArrow.Visible = this.DisplayPager;
this.ButtonRightArrow.Visible = this.DisplayPager;
//清空历史数据
itemPanel.Items.Clear();
this.itemPanel.Font = GetFont(this._fontSize);
var pager = ListPager(dataSource);
ItemContainer ic = new ItemContainer();
ic.MultiLine = (this.Rows > 1);
ic.ItemSpacing = 0;
ic.LayoutOrientation = eOrientation.Horizontal;
foreach (ProductType entity in pager)
{
MetroTileItem item = this.GetItemTemplate();
item.Checked = false;
//绑定品类ID到Tag属性
item.Tag = entity;
//设置编码显示位置
if (this._displayCategoryCode)
{
//绑定品类编码到TitleText属性
item.TitleText = entity.No;
}
//绑定品类名称到Text属性
item.Text = entity.Name;
ic.SubItems.Add(item);
}
this.itemPanel.Items.Add(ic);
this.itemPanel.Invalidate();
}
catch (Exception ex)
{
logger.Error(ex, "绑定品类数据源异常");
}
}
private Font GetFont(SystemFont fontSize)
{
Font result = Constant.DEFAULT_FONT;
switch (fontSize)
{
case SystemFont.:
{
result = Constant.DEFAULT_FONT;
}
break;
case SystemFont.:
{
result = Constant.NORMAL_FONT;
}
break;
case SystemFont.:
{
result = Constant.SMALL_FONT;
}
break;
case SystemFont.:
{
result = Constant.BIG_FONT;
}
break;
}
return result;
}
private MetroTileItem GetItemTemplate()
{
MetroTileItem template = new MetroTileItem();
template.TileStyle.PaddingLeft = template.TileStyle.PaddingRight = -2;
template.EnableMarkup = true;
template.OptionGroup = "DishType";
template.TileStyle.Font = itemPanel.Font;
//显示编码后,调整呈现位置
if (this._displayCategoryCode)
{
template.TileStyle.TextAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center;
template.TileStyle.TextLineAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Far;
template.TitleTextAlignment = System.Drawing.ContentAlignment.TopCenter;
}
else
{
template.TileStyle.TextAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center;
template.TileStyle.TextLineAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center;
}
template.TileStyle.TextColor = ColorTranslator.FromHtml(this._textColor);
template.TitleTextColor = ColorTranslator.FromHtml(this._textColor);
template.TileStyle.BackColor = ColorTranslator.FromHtml(this._backColor1);
template.TileStyle.BackColor2 = ColorTranslator.FromHtml(this._backColor2);
template.TileStyle.BorderColor = ColorTranslator.FromHtml(this._backColor1);
template.TileStyle.BorderColor2 = ColorTranslator.FromHtml(this._backColor2);
template.TileStyle.BorderWidth = 1;
template.TileStyle.CornerDiameter = 4;
switch (this.CategoryCornerType)
{
case CornerType.:
{
template.TileStyle.CornerType = eCornerType.Square;
}
break;
case CornerType.:
{
template.TileStyle.CornerType = eCornerType.Rounded;
template.TileStyle.BorderColor = ColorTranslator.FromHtml(this._backColor1);
template.TileStyle.BorderColor2 = ColorTranslator.FromHtml(this._backColor2);
}
break;
case CornerType.:
{
template.TileStyle.CornerType = eCornerType.Diagonal;
}
break;
}
//显示大小
template.TileSize = new Size(this.ItemWidth, this.ItemHeight);
template.MouseDown += OnMouseDown;
template.CheckedChanged += OnCheckedChanged;
return template;
}
private void OnMouseDown(object sender, MouseEventArgs e)
{
MetroTileItem item = (MetroTileItem)sender;
item.Checked = false;
item.Checked = true;
}
private void OnCheckedChanged(object sender, EventArgs e)
{
MetroTileItem item = (MetroTileItem)sender;
if (item.Checked)
{
this._selectedItem = item;
item.TileStyle.BackColor = ColorTranslator.FromHtml(this._selectedColor);
item.TileStyle.BackColor2 = ColorTranslator.FromHtml(this._selectedColor);
item.TileStyle.BorderColor = ColorTranslator.FromHtml(this._selectedColor);
item.TileStyle.BorderColor2 = ColorTranslator.FromHtml(this._selectedColor);
OnCategoryCheckedChanged(new CategoryEventArgs((ProductType)item.Tag));
}
else
{
item.TileStyle.BackColor = ColorTranslator.FromHtml(this._backColor1);
item.TileStyle.BackColor2 = ColorTranslator.FromHtml(this._backColor2);
item.TileStyle.BorderColor = ColorTranslator.FromHtml(this._backColor1);
item.TileStyle.BorderColor2 = ColorTranslator.FromHtml(this._backColor2);
}
}
private bool _displayPager = true;
/// <summary>
/// 是否显示分页导航按钮
/// </summary>
public bool DisplayPager
{
get
{
return _displayPager;
}
set
{
_displayPager = value;
}
}
private SystemFont _fontSize = SystemFont.;
/// <summary>
/// 字体大小
/// </summary>
public SystemFont FontSize
{
get { return this._fontSize; }
set { this._fontSize = value; }
}
private int _rows = 1;
/// <summary>
///品类默认行数
/// </summary>
public int Rows
{
get { return _rows; }
set
{
_rows = (value <= 0 ? 1 : value);
this.PageNumber = 1;
this.PageSize = _rows * _columns;
//刷新
RefreshUi();
}
}
/// <summary>
/// 有效区域的宽度,不包含左右分页宽度
/// </summary>
public int ContentWidth => this.Width - (this.DisplayPager ? ArrowWidth * 2 : 0);// - (this.Columns + 1)
private int _itemHeight = 45;
/// <summary>
///品类显示的高度
/// </summary>
public int ItemHeight
{
get { return _itemHeight; }
set
{
_itemHeight = (value <= 0 ? 45 : value);
this.RefreshUi();
}
}
private bool _autoItemWidth = true;
/// <summary>
/// 是否自动计算宽度
/// </summary>
public bool AutoItemWidth
{
get
{
return this._autoItemWidth;
}
set
{
this._autoItemWidth = value;
}
}
private int _itemWidth;
/// <summary>
/// 品类的宽度
/// </summary>
public int ItemWidth
{
get
{
if (this._autoItemWidth)
{
this._itemWidth = this.ContentWidth / this._columns;
}
return this._itemWidth;
}
set
{
if (this._autoItemWidth)
{
this._itemWidth = this.ContentWidth / this._columns;
}
else
{
this._itemWidth = value;
}
}
}
private bool _autoPager = true;
/// <summary>
/// 是否自动隐藏分页
/// </summary>
public bool AutoPager
{
get
{
return this._autoPager;
}
set
{
this._autoPager = value;
}
}
private bool _displayCategoryCode = false;
/// <summary>
/// 是否显示品类编码
/// </summary>
public bool DisplayCategoryCode
{
get { return this._displayCategoryCode; }
set { this._displayCategoryCode = value; }
}
private string _backColor1 = ColorTranslator.ToHtml(Color.DimGray);
/// <summary>
/// 品类显示颜色
/// </summary>
public string BackColor1
{
get { return this._backColor1; }
set { this._backColor1 = value; }
}
private string _backColor2 = ColorTranslator.ToHtml(Color.DimGray);
/// <summary>
/// 品类显示颜色
/// </summary>
public string BackColor2
{
get { return this._backColor2; }
set { this._backColor2 = value; }
}
private string _textColor = ColorTranslator.ToHtml(Color.White);
/// <summary>
/// 品类显示颜色
/// </summary>
public string TextColor
{
get { return this._textColor; }
set { this._textColor = value; }
}
private string _selectedColor = ColorTranslator.ToHtml(Color.DimGray);
/// <summary>
/// 品类选中的颜色
/// </summary>
public string SelectedColor
{
get { return this._selectedColor; }
set { this._selectedColor = value; }
}
/// <summary>
/// 显示样式
/// </summary>
public CornerType CategoryCornerType { get; set; }
private void ButtonRightArrowClick(object sender, EventArgs e)
{
if (this.HasNextPage)
{
this.PageNumber++;
BindDataSource(this._dataSource);
}
}
private void ButtonLeftArrowClick(object sender, EventArgs e)
{
if (this.HasPreviousPage)
{
this.PageNumber--;
BindDataSource(this._dataSource);
}
}
//是否有上一页
public bool HasPreviousPage
{
get { return PageNumber > 1; }
}
//是否有下一页
public bool HasNextPage
{
get { return PageNumber < this.PageCount; }
}
}
public delegate void CategoryEventHandler(object sender, CategoryEventArgs e);
public class CategoryEventArgs : EventArgs
{
public ProductType Data;
public CategoryEventArgs(ProductType data)
{
this.Data = data;
}
}
}