FrmCodeExtract.cs 13.8 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Asa.FaceControl;
using BLL;
using CameraVisionLib.Model;
using Model;

namespace SmartScan
{
    public partial class FrmCodeExtract : FaceFormNormal
    {
        private readonly int codeID;
        private readonly string codeText;
        private readonly string codeType;
        public readonly Dictionary<FaceButton, UsrCodeExtractList> matchButton = new();

        int windowHeight = 0;
        MaterialCodeMatch matchShared = new MaterialCodeMatch();
        public FrmCodeExtract(string codeText, int codeID, string codeType, List<MaterialCodeMatch> match)
        {
            InitializeComponent();
            windowHeight = this.Height;
            codeText = codeText.Replace("\r", "");
            this.codeText = codeText.Replace("\n", "");
            this.codeID = codeID;
            this.codeType = codeType;
            TxtMatchingEndText.Tag = "not";
            TxtMatchingMiddleText.Tag = "not";
            TxtMatchingStartText.Tag = "not";
            Language.SetLanguage(this);
            ChoMatchMiddleType.Items.Add(Language.Dialog("MatchType_max"));
            ChoMatchMiddleType.Items.Add(Language.Dialog("MatchType_equals"));
            ChoMatchMiddleType.Items.Add(Language.Dialog("MatchType_min"));
            if (match.Count > 0)
            {
                matchShared = match[0];
            }
            ChkMatchingStart.Checked = matchShared.MatchStart;
            TxtMatchingStartText.Text = matchShared.StartText;
            ChkMatchingEnd.Checked = matchShared.MatchEnd;
            TxtMatchingEndText.Text = matchShared.EndText;
            ChkMatchingMiddle.Checked = matchShared.MatchMiddle;
            TxtMatchingMiddleText.Text = matchShared.MiddleText;
            NudMiddleTextCount.Value = matchShared.MiddleTextCount;
            ChkCaseSensitivity.Checked = matchShared.CaseSensitive;
            ChoMatchMiddleType.SelectedIndex = matchShared.MatchMiddleType + 1;
            ChkCheckCodeType.Checked = matchShared.CheckCodeType;
            LblCodeType.Text = codeType;

            for (int i = 0; i < match.Count; i++)
                AddMatch(match[i]);
            if (matchButton.Count > 0)
                Btn_Click(matchButton.ElementAt(0).Key, EventArgs.Empty);
            else
                BtnAddMatch_Click(null, EventArgs.Empty);
        }

        public List<MaterialCodeMatch> CodeMatch { get; private set; }



        private void AddMatch(MaterialCodeMatch match)
        {
            FaceButton btn = new() { Text = string.Format("[{0}]", match.Keyword), Margin = new Padding(0, 0, 6, 6), Width = 100, Height = 50 };
            btn.Click += Btn_Click;
            btn.Tag = "not";
            flowLayoutPanel1.Controls.Add(btn);


            UsrCodeExtractList list = new(codeText, codeType, codeText.Length, match);
            list.DelClick += List_DelClick;
            list.AddClick += List_AddClick;
            list.KeyChanged += List_KeyChanged;
            matchButton.Add(btn, list);

            FacePanel pnl = list.GetPanel();
            pnl.Left = PnlTemp.Left;
            pnl.Top = PnlTemp.Top;
            pnl.Width = PnlTemp.Width;
            pnl.Height = PnlTemp.Height;
            pnl.Anchor = PnlTemp.Anchor;
            pnl.Visible = false;
            //Controls.Add(pnl);
            this.tableLayoutPanel3.Controls.Add(pnl, 0, 1);
            Maximize();
            clacWindowHeight();
        }

        private void List_AddClick(object sender, EventArgs e)
        {
            BtnAddMatch_Click(sender, e);
        }

        void clacWindowHeight()
        {
            //if (flowLayoutPanel1.Controls.Count > 14)
            //{
            //    matchButton.Values.ToList().ForEach((x) => { x.GetPanel().Top = 418 + 56 * 2; });//184

            //    flowLayoutPanel1.Height = 56 * 3;
            //    this.Height = windowHeight + 56 * 2 - 4;
            //}
            //else if (flowLayoutPanel1.Controls.Count > 7)
            //{
            //    matchButton.Values.ToList().ForEach((x) => { x.GetPanel().Top = 418 + 56 * 1; });
            //    flowLayoutPanel1.Height = 56 * 2 - 4;
            //    this.Height = windowHeight + 56;
            //}
            //else
            //{
            //    matchButton.Values.ToList().ForEach((x) => { x.GetPanel().Top = 418; });
            //    flowLayoutPanel1.Height = 56 - 4;
            //    this.Height = windowHeight;
            //}
        }
        private void List_KeyChanged(object sender, string key)
        {
            UsrCodeExtractList ctl = sender as UsrCodeExtractList;
            foreach (FaceButton btn in matchButton.Keys)
            {
                if (matchButton[btn] != ctl) continue;
                btn.Text = string.Format("[{0}]", key);
                break;
            }
        }

        private void List_DelClick(object sender, EventArgs e)
        {
            UsrCodeExtractList ctl = sender as UsrCodeExtractList;
            foreach (FaceButton btn in matchButton.Keys)
            {
                if (matchButton[btn] != ctl) continue;
                flowLayoutPanel1.Controls.Remove(btn);
                Controls.Remove(matchButton[btn].GetPanel());
                matchButton.Remove(btn);
                break;
            }
            if (matchButton.Count > 0)
            {
                matchButton.Last().Key.HoldPress = true;
                matchButton.Last().Value.GetPanel().Visible = true;
            }
            clacWindowHeight();
        }

        private void BtnAddMatch_Click(object sender, EventArgs e)
        {
            MaterialCodeMatch temp = new() { SubstringLength = -1 };
            AddMatch(temp);
            Btn_Click(matchButton.ElementAt(matchButton.Count - 1).Key, EventArgs.Empty);
            //foreach (FaceButton button in matchButton.Keys)
            //{
            //    bool rtn = button == matchButton.ElementAt(matchButton.Count - 1).Key;
            //    matchButton[button].GetPanel().Visible = rtn;
            //}
        }

        private void Btn_Click(object sender, EventArgs e)
        {
            foreach (FaceButton button in matchButton.Keys)
            {
                bool rtn = button == sender;
                button.HoldPress = rtn;
                matchButton[button].GetPanel().Visible = rtn;
            }
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            Dictionary<string, int> keyTemp = new();
            foreach (FaceButton btn in matchButton.Keys)
            {
                string key = matchButton[btn].GetMatchKey();
                if (string.IsNullOrWhiteSpace(key))
                {
                    string text = Language.Dialog("KeyEmpty");
                    new FaceMessageBox("", text, MessageBoxButtons.OK).ShowDialog();

                    Btn_Click(btn, null);
                    return;
                }
                if (keyTemp.ContainsKey(key))
                    keyTemp[key]++;
                else
                    keyTemp.Add(key, 1);
            }
            //if (matchButton.Keys.Count == 0) {
            //    var mateCopy = BLLCommon.mateEdit.ToCopy();
            //    var findocrm = mateCopy.Find(m => m.Ocr.Find(o => o.CodeID == this.codeID) != null);
            //    if (findocrm != null) {
            //        string text = Language.Dialog("ThisMatchHasOcrCantdelete");
            //        new FaceMessageBox("", text, MessageBoxButtons.OK).ShowDialog();
            //        return;
            //    }
            //}

            foreach (string key in keyTemp.Keys)
            {
                if (keyTemp[key] > 1)
                {
                    string text = Language.Dialog("KeyRepeat") + "\r\n";
                    text += string.Format("{0} * {1}", key, keyTemp[key]);
                    new FaceMessageBox("", text, MessageBoxButtons.OK).ShowDialog();
                    return;
                }
            }

            CodeMatch = new();
            foreach (FaceButton btn in matchButton.Keys)
            {
                MaterialCodeMatch match = matchButton[btn].GetMatch();
                match.MatchStart = ChkMatchingStart.Checked;
                match.StartText = TxtMatchingStartText.Text;
                match.MatchEnd = ChkMatchingEnd.Checked;
                match.EndText = TxtMatchingEndText.Text;
                match.MatchMiddle = ChkMatchingMiddle.Checked;
                match.MiddleText = TxtMatchingMiddleText.Text;
                match.MiddleTextCount = Convert.ToInt32(NudMiddleTextCount.Value);
                match.CaseSensitive = ChkCaseSensitivity.Checked;
                match.CheckCodeType = ChkCheckCodeType.Checked;
                match.CodeType = LblCodeType.Text;
                match.MatchMiddleType = ChoMatchMiddleType.SelectedIndex - 1;
                match.CodeID = codeID;

                var hasmatchselect = match.MatchISNumber || match.CheckCodeType || match.MatchEnd || match.MatchMaxLength || match.MatchMiddle || match.MatchMinLength || match.MatchSplit || match.MatchStart;
                if (!hasmatchselect)
                {
                    string text = Language.Dialog("NoMatchSelect");
                    new FaceMessageBox("", text, MessageBoxButtons.OK).ShowDialog();
                    Btn_Click(btn, null);
                    return;
                }
                bool emptyMatch = false;
                if (match.MatchEnd)
                {
                    if (string.IsNullOrEmpty(match.EndText))
                    {
                        emptyMatch = true;
                    }
                }
                else if (match.MatchMiddle)
                {
                    if (string.IsNullOrEmpty(match.MiddleText))
                    {
                        emptyMatch = true;
                    }
                }
                //else if (match.MatchSplit)
                //{
                //    if(string.IsNullOrEmpty(match.MiddleText))
                //    {
                //        emptyMatch = true;
                //    }
                //}
                else if (match.MatchStart)
                {
                    if(string.IsNullOrEmpty(match.StartText))
                    {
                        emptyMatch = true;
                    }
                }
                else if(match.MatchMaxLength)
                {
                    if(match.MaxLength==0)
                    {
                        emptyMatch = true;
                    }
                }
                if(emptyMatch)
                {
                    new Asa.FaceControl.FaceMessageBox("", Language.Dialog("MatchEmpty"), MessageBoxButtons.OK).ShowDialog();
                    Btn_Click(btn, null);
                    return;
                }
                if(match.MatchMinLength &&  match.MatchMaxLength)
                {
                    if(match.MinLength>match.MaxLength)
                    {
                        new Asa.FaceControl.FaceMessageBox("", Language.Dialog("ConflictLength"), MessageBoxButtons.OK).ShowDialog();
                        Btn_Click(btn, null);
                        return;
                    }
                }
                CodeMatch.Add(match);
            }
            DialogResult = DialogResult.OK;
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void FrmCodeExtract_Load(object sender, EventArgs e)
        {
            LblCode.Text = codeText;

        }
        private void matchChanged(object sender, EventArgs e)
        {
            lblMatchRes.Invoke(new Action(() =>
            {
                if (CheckMatch())
                {
                    lblMatchRes.Text = "";
                    //lblMatchRes.ForeColor = Color.Green;
                }
                else
                {
                    lblMatchRes.Text = "NG";
                    lblMatchRes.ForeColor = Color.Red;
                }
            }));
        }
        private bool CheckMatch()
        {
            string code = ChkCaseSensitivity.Checked ? codeText : codeText.ToUpper();
            bool ismatch = true;

            string text = "";
            //开头
            if (ChkMatchingStart.Checked)
            {
                text = ChkCaseSensitivity.Checked ? TxtMatchingStartText.Text : TxtMatchingStartText.Text.ToUpper();
                if (!code.StartsWith(text))
                    ismatch = false;
            }

            //结尾
            if (ChkMatchingEnd.Checked)
            {

                text = ChkCaseSensitivity.Checked ? TxtMatchingEndText.Text : TxtMatchingEndText.Text.ToUpper();
                if (!code.EndsWith(text))
                    ismatch = false;
            }

            //中间
            if (ChkMatchingMiddle.Checked)
            {
                int index, count;
                text = ChkCaseSensitivity.Checked ? TxtMatchingMiddleText.Text : TxtMatchingMiddleText.Text.ToUpper();
                if (text == "") return false;
                index = count = 0;
                while ((index = code.IndexOf(text, index)) != -1)
                {
                    count++;
                    index += text.Length;
                }
                if (count == 0)
                    ismatch = false;
                if (ChoMatchMiddleType.SelectedIndex == -1 && count > Convert.ToInt32(NudMiddleTextCount.Value))
                    ismatch = false;
                if (ChoMatchMiddleType.SelectedIndex == 0 && count != Convert.ToInt32(NudMiddleTextCount.Value))
                    ismatch = false;
                if (ChoMatchMiddleType.SelectedIndex == 1 && count < Convert.ToInt32(NudMiddleTextCount.Value))
                    ismatch = false;
            }
            return ismatch;
        }
    }
}