UsrMacro - 副本.cs 6.2 KB
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DocumentFormat.OpenXml.Wordprocessing;
using Model;

namespace SmartScan
{
    public partial class UsrMacro : UserControl, ISetMenu
    {
        private readonly List<string> keyCopy;
        private readonly List<AutoGenRule> autoGenRulesCopy;
        AutoGenRule genRule;
        public UsrMacro()
        {
            InitializeComponent();
            keyCopy = new(Common.macroKey);
            autoGenRulesCopy = new(Common.AutoGenRules);
            LstKey.Items.AddRange(keyCopy.ToArray());
            setRIkey();
            if (autoGenRulesCopy.Count > 0)
            {
                genRule = autoGenRulesCopy[0];
            }
            else
            {
                genRule = new AutoGenRule();
            }
            TxtReelIDMatch.Text = genRule.GenRule;
            TxtPrefix.Text = genRule.Prefix;
            TxtPostfix.Text = genRule.Postfix;
            NumReelIDPlaces.Value = genRule.NumLength;
            ChkReelIDFillZero.Checked = genRule.IsFillZero;
            ChkResetidbydate.Checked = genRule.NumAutoResetByDate;
            Asa.FaceControl.Language.SetLanguage(this);
        }

        public Asa.FaceControl.FacePanel GetPanel()
        {
            return facePanel1;
        }

        public void Save()
        {
            Common.macroKey.Clear();
            Common.macroKey.AddRange(keyCopy);
            System.IO.File.WriteAllLines(FilePath.CONFIG_MACRO_KEY, Common.macroKey.ToArray());

            Common.config.ReelIDMatch = TxtReelIDMatch.Text;
            Common.config.ReelIDPrefix = TxtPrefix.Text;
            Common.config.ReelIDPostfix = TxtPostfix.Text;
            Common.config.ReelIDPlaces = (int)NumReelIDPlaces.Value;
            Common.config.ReelIDFillZero = ChkReelIDFillZero.Checked;
            Common.config.ReelIDAutoResetByDate = ChkResetidbydate.Checked;
            Common.AutoGenRules.Clear();
            Common.AutoGenRules.AddRange(autoGenRulesCopy);
            AutoGenRule.Save(Common.AutoGenRules);
        }
        void setRIkey()
        {
            LstKey.Items.Clear();
            LstKey.Items.AddRange(keyCopy.ToArray());
            for (int i = 0; i < LstKey.Items.Count; i++)
            {
                var rule = autoGenRulesCopy.Find(s => s.Keyword.Equals(LstKey.Items[i]));
                if (rule != null)
                {
                    LstKey.Items[i] = "[Auto]" + rule.Keyword;
                }
            }
        }


        private void LstKey_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LstKey.SelectedIndex >= 0)
            {
                TxtKey.Text = keyCopy[LstKey.SelectedIndex];
                genRule = autoGenRulesCopy.Find(s=>s.Keyword.Equals(TxtKey.Text));
            }

        }

        private void BtnAddKey_Click(object sender, EventArgs e)
        {
            string text = TxtKey.Text;
            int index = keyCopy.FindIndex(match => match == text);
            if (index == -1)
            {
                keyCopy.Add(text);
                LstKey.Items.Add(text);
                autoGenRulesCopy.Add(new AutoGenRule() { Keyword= text); 
            }
            else
            {
                string hint = Asa.FaceControl.Language.Dialog("KeyExists");
                hint = hint.Replace("[name]", text);
                new Asa.FaceControl.FaceMessageBox("", hint, MessageBoxButtons.OK).ShowDialog();
            }
        }

        private void BtnDelKey_Click(object sender, EventArgs e)
        {
            if (LstKey.SelectedIndex == -1) return;
            keyCopy.RemoveAt(LstKey.SelectedIndex);
            LstKey.Items.RemoveAt(LstKey.SelectedIndex);
            var rule = autoGenRulesCopy.Find(s => s.Keyword.Equals(keyCopy[LstKey.SelectedIndex]));
            if (rule != null) { }
            autoGenRulesCopy.Remove(rule);
        }

        private void BtnUpdateKey_Click(object sender, EventArgs e)
        {
            if (LstKey.SelectedIndex == -1) return;
            string text = TxtKey.Text;
            int index = keyCopy.FindIndex(match => match == text);
            if (index == -1)
            {
                keyCopy[LstKey.SelectedIndex] = text;
                LstKey.Items[LstKey.SelectedIndex] = text;
                setRIkey();
            }
            else
            {
                string hint = Asa.FaceControl.Language.Dialog("KeyExists");
                hint = hint.Replace("[name]", text);
                new Asa.FaceControl.FaceMessageBox("", hint, MessageBoxButtons.OK).ShowDialog();
            }

        }

        private void BtnAppendKey_Click(object sender, EventArgs e)
        {
            if (LstKey.SelectedIndex == -1) return;
            string key = "[" + LstKey.Text + "]";
            TxtReelIDMatch.AppendText(key);
        }

        private void btn_up_Click(object sender, EventArgs e)
        {
            if (LstKey.SelectedIndex == -1 || LstKey.SelectedIndex == 0) return;
            keyCopy.Insert(LstKey.SelectedIndex - 1, keyCopy[LstKey.SelectedIndex]);
            keyCopy.RemoveAt(LstKey.SelectedIndex + 1);
            LstKey.Items.Clear();
            LstKey.Items.AddRange(keyCopy.ToArray());
            LstKey.SelectedIndex = LstKey.SelectedIndex - 1;

        }

        private void btn_down_Click(object sender, EventArgs e)
        {
            if (LstKey.SelectedIndex == -1 || LstKey.SelectedIndex == keyCopy.Count - 1) return;
            keyCopy.Insert(LstKey.SelectedIndex + 2, keyCopy[LstKey.SelectedIndex]);
            keyCopy.RemoveAt(LstKey.SelectedIndex);
            LstKey.Items.Clear();
            LstKey.Items.AddRange(keyCopy.ToArray());
            LstKey.SelectedIndex = LstKey.SelectedIndex + 1;
        }

        private void btn_setriid_Click(object sender, EventArgs e)
        {
            if (LstKey.SelectedIndex == -1) return;
            if (keyCopy[LstKey.SelectedIndex] == Common.config.ReelIDKeyWord)
                Common.config.ReelIDKeyWord = "";
            else
                Common.config.ReelIDKeyWord = keyCopy[LstKey.SelectedIndex];
            setRIkey();
        }

        private void btn_adddatetime_Click(object sender, EventArgs e)
        {
            string key = "[datetime:yyyyMMddHHmmss]";
            TxtReelIDMatch.AppendText(key);
        }
    }
}