FrmCodeExtract.cs 16.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
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();

        string[] myStringArray = null;
        public FrmCodeExtract(string codeText, int codeID, string codeType, List<MaterialCodeMatch> match,string[] strings)
        {
            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;
            Characteristicbarcode.Checked = matchShared.Characteristic;
            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);

            myStringArray = strings;

            //if (BLL.Config.Backgrounder)
            //{
            //    Characteristicbarcode.Visible = true;
            //}

            Characteristicbarcode.Visible = true;
        }

        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 (!btn.HoldPress) 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;
                match.Characteristic = Characteristicbarcode.Checked;
                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;
            richTextBox1.Text = codeText;
            //ColorizeText(myStringArray, Color.Red);
            ColorizeText(myStringArray);
        }
        //修改richTextBox1 字体颜色
        private void ColorizeText(string[] keywords,Color color)
        {
            // 清除已经存在的颜色标记
            //richTextBox1.SelectAll();
            //richTextBox1.SelectionColor = Color.Black;

            foreach (string keyword in keywords)
            {
                int startIndex = 0;

                while (startIndex < richTextBox1.TextLength)
                {
                    int wordStartIndex = richTextBox1.Find(keyword, startIndex, RichTextBoxFinds.None);

                    if (wordStartIndex == -1)
                    {
                        break;
                    }

                    richTextBox1.SelectionStart = wordStartIndex;
                    richTextBox1.SelectionLength = keyword.Length;
                    richTextBox1.SelectionColor = color; // 设置关键字的颜色

                    startIndex = wordStartIndex + keyword.Length;
                }
            }
        }
        //将匹配的字符修改为白底黑字
        private void ColorizeText(string[] keywords)
        {
            foreach (string keyword in keywords)
            {
                int startIndex = 0;

                while (startIndex < richTextBox1.TextLength)
                {
                    int wordStartIndex = richTextBox1.Find(keyword, startIndex, RichTextBoxFinds.None);

                    if (wordStartIndex == -1)
                    {
                        break;
                    }

                    richTextBox1.SelectionStart = wordStartIndex;
                    richTextBox1.SelectionLength = keyword.Length;
                    richTextBox1.SelectionBackColor = Color.AliceBlue; // 设置关键字的背景颜色
                                                                   // 使用稍浅的白色作为背景颜色
                    //Color slightlyLighterWhite = ControlPaint.Light(Color.White);
                    //richTextBox1.SelectionBackColor = slightlyLighterWhite;
                    richTextBox1.SelectionColor = Color.Black;     // 设置关键字的字体颜色

                    startIndex = wordStartIndex + keyword.Length;
                }
            }
        }


        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;
        }
    }
}