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.

374 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.Metro;
using DevComponents.DotNetBar;
using POSV.Entity;
using POSV.Utils;
using POSV.Bean;
using POSV.MessageEvent;
namespace POSV.Template.Component
{
[ToolboxItem(true)]
public partial class SpecControl : BaseUserControl
{
/// <summary>
/// 数据源
/// </summary>
private List<ProductSpec> _dataSource = null;
public SpecControl()
{
InitializeComponent();
this.BackColor = Color.Transparent;
//订购界面变更通知事件
MsgEvent.RemoveListener(Constant.SPEC_CHANGED_NOTIFY, this.SpecChangedEventNotify);
MsgEvent.Receive(Constant.SPEC_CHANGED_NOTIFY, this.SpecChangedEventNotify);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.DesignMode) return;
var productJsonTemplate = Global.Instance.GlobalConfigStringValue(ConfigConstant.SPEC_JSON_TEMPLATE);
if (string.IsNullOrEmpty(productJsonTemplate))
{
productJsonTemplate = JsonUtils.Serialize(new ProductSpecItem());
}
this.RefreshUi(JsonUtils.Deserialize<ProductSpecItem>(productJsonTemplate));
//当前页码
this.PageNumber = 1;
}
protected void SpecChangedEventNotify(object sender, MsgEventArgs args)
{
if (args.Data is ProductSpecItem)
{
var data = args.Data as ProductSpecItem;
Global.Instance.ReloadConfig(ConfigConstant.SPECMAKE_GROUP);
this.RefreshUi(data);
this.BindDataSource(this._dataSource);
}
}
public void RefreshUi(ProductSpecItem item)
{
this.FontSize = item.SpecFont;
this.ItemHeight = item.ItemHeight;
this.ItemWidth = item.ItemWidth;
this.Cols = item.Cols;
this.BackColor1 = item.BackColor1;
this.BackColor2 = item.BackColor2;
this.TextColor = item.TxtColor;
UpdateTemplate();
}
private void UpdateTemplate()
{
}
private MetroTileItem GetItemTemplate()
{
MetroTileItem template = new MetroTileItem();
template.TileStyle.PaddingLeft = template.TileStyle.PaddingRight = -2;
template.EnableMarkup = true;
template.OptionGroup = "Spec";
template.TileStyle.Font = this.itemPanel1.Font;
this.itemPanel1.ResizeItemsToFit = false;
this.itemPanel1.BackgroundStyle.Class = "MetroTileGroupTitle";//很关键
template.TileStyle.TextAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center;
template.TileStyle.TextLineAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Far;
if (this.ShowPrice)
{
template.TitleTextAlignment = System.Drawing.ContentAlignment.TopCenter;
}
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.TileSize = new Size(this.ItemWidth, this.Height);
//圆角
template.TileStyle.CornerType = eCornerType.Rounded;
template.MouseDown += OnItemMouseDown;
return template;
}
private void OnItemMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MetroTileItem item = (MetroTileItem)sender;
var productSpec = item.Tag as ProductSpec;
this.OnProductSpecCheckedChanged(new ProductSpecEventArgs(productSpec));
}
}
public event ProductSpecEventHandler ProductSpecCheckedChanged;
protected virtual void OnProductSpecCheckedChanged(ProductSpecEventArgs e)
{
ProductSpecCheckedChanged?.Invoke(this, e);
}
public void BindDataSource(List<ProductSpec> dataSource)
{
this._dataSource = dataSource;
if (this._dataSource == null)
{
this._dataSource = new List<ProductSpec>();
}
if(this._dataSource.Count == 1)
{
//清空历史数据
this.itemPanel1.Items.Clear();
this.itemPanel1.Invalidate();
return;
}
//总数量
this.TotalCount = dataSource.Count;
//每页的显示数量
if(this.Width == 0)
{
this.PageSize = this.Cols;
}
else
{
this.PageSize = this.Width / this.ItemWidth;
}
//清空历史数据
this.itemPanel1.Items.Clear();
this.itemPanel1.Font = this.GetSystemFont(this._fontSize);
var pager = ListPager(dataSource);
ItemContainer ic = new ItemContainer();
ic.MultiLine = true;
ic.ItemSpacing = 0;
ic.LayoutOrientation = eOrientation.Horizontal;
foreach (ProductSpec entity in pager)
{
MetroTileItem item = this.GetItemTemplate();
item.Checked = false;
//绑定品类ID到Tag属性
item.Tag = entity;
item.TitleText = entity.Name;
if (this.ShowPrice)
{
if (entity.Price != 0)
{
item.Text = StringUtils.FormatDataNoDigit(entity.Price);
}
}
ic.SubItems.Add(item);
}
this.itemPanel1.Items.Add(ic);
this.itemPanel1.Invalidate();
}
/// <summary>
/// 分页List数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public List<ProductSpec> ListPager(List<ProductSpec> data)
{
var result = new List<ProductSpec>();
result.AddRange(data.Skip((this.PageNumber - 1) * this.PageSize).Take(this.PageSize));
return result;
}
private int _itemHeight = 70;
/// <summary>
///品类显示的高度
/// </summary>
public int ItemHeight
{
get { return _itemHeight; }
set
{
_itemHeight = (value <= 0 ? 70 : value);
}
}
private int _itemWidth = 70;
/// <summary>
///规格显示的宽度
/// </summary>
public int ItemWidth
{
get
{
if(_itemWidth == 0)
{
return this.Width / this.Cols;
}
return _itemWidth;
}
set
{
_itemWidth = (value < 0 ? 70 : value);
}
}
private SystemFont _fontSize = SystemFont.;
/// <summary>
/// 字体大小
/// </summary>
public SystemFont FontSize
{
get { return this._fontSize; }
set { this._fontSize = 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; }
}
/// <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; }
/// <summary>
///规格默认列数
/// </summary>
public int Cols { get; private set; }
//是否有上一页
public bool HasPreviousPage
{
get { return PageNumber > 1; }
}
//是否有下一页
public bool HasNextPage
{
get { return PageNumber < this.PageCount; }
}
/// <summary>
/// 显示零售价
/// </summary>
private bool _showPrice = true;
public bool ShowPrice
{
get
{
return _showPrice;
}
set
{
_showPrice = value;
}
}
}
public delegate void ProductSpecEventHandler(object sender, ProductSpecEventArgs e);
public class ProductSpecEventArgs : EventArgs
{
public ProductSpec ProductSpec;
public ProductSpecEventArgs(ProductSpec data)
{
this.ProductSpec = data;
}
}
}