Newer
Older
Correlator / PipeGallery / View / SoundSpeedView.xaml.cs
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PipeGallery.Manage;
using PipeGallery.Model;
using PipeGallery.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PipeGallery.View
{
    /// <summary>
    /// SoundSpeedEditView.xaml 的交互逻辑
    /// </summary>
    public partial class SoundSpeedView : UserControl
    {
        private int pipeMaterialId;
        public SoundSpeedView(int id)
        {
            InitializeComponent();
            this.DataContext = SoundSpeedEditViewModel.Instance;
            DataFactory.InitalSoundSpeedData(id);

            this.pipeMaterialId = id;
            this.btnReturn.Click += btnReturn_Click;
            this.btnNew.Click += btnNew_Click;
            this.btnEdit.Click += btnEdit_Click;
            this.btnDelete.Click += btnDelete_Click;
        }

        void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            bool isSelect = false;
            foreach (var soundspeedinfo in SoundSpeedEditViewModel.Instance.SoundSpeedInfos)
            {
                if (soundspeedinfo.IsSelected)
                {
                    isSelect = true;
                    PopupWindow pw = new PopupWindow("是否确定删除?");
                    pw.Owner = App.Current.MainWindow;
                    bool? ret = pw.ShowDialog();
                    if (ret == true)
                    {
                        string josnString = File.ReadAllText(System.AppDomain.CurrentDomain.BaseDirectory + "config.json", Encoding.UTF8);
                        JObject jo = JObject.Parse(josnString);

                        jo["CustomMaterial"][pipeMaterialId]["range"][soundspeedinfo.Order - 1].Remove();
                        jo["CustomMaterial"][pipeMaterialId]["speed"][soundspeedinfo.Order - 1].Remove();
                        StreamWriter sw = new StreamWriter(System.AppDomain.CurrentDomain.BaseDirectory + "config.json");
                        sw.Write(JsonConvert.SerializeObject(jo));
                        sw.Flush();
                        sw.Close();
                    }
                }
            }
            DataFactory.InitalSoundSpeedData(pipeMaterialId);

            if (!isSelect)
            {
                PopupWindow pw = new PopupWindow("未选中删除项");
                pw.Owner = App.Current.MainWindow;
                pw.ShowDialog();
            }

        }

        void btnNew_Click(object sender, RoutedEventArgs e)
        {
            SoundSpeedEditView soundSpeedEditView = new SoundSpeedEditView(pipeMaterialId, true);
            Global.GetMainView().bdrPreposition.Child = soundSpeedEditView;
        }

        void btnReturn_Click(object sender, RoutedEventArgs e)
        {
            HomePageView homePageView = new HomePageView();
            Global.GetMainView().bdrMain.Child = homePageView;
            SelectPipeMaterialView selectPipeMaterialView = new SelectPipeMaterialView();
            Global.GetMainView().bdrPreposition.Child = selectPipeMaterialView;
        }

        void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            bool isSelect = false;
            foreach (var soundspeedinfo in SoundSpeedEditViewModel.Instance.SoundSpeedInfos)
            {
                if (soundspeedinfo.IsSelected)
                {
                    isSelect = true;
                    SoundSpeedEditView soundSpeedEditView = new SoundSpeedEditView(pipeMaterialId, false);
                    Global.GetMainView().bdrPreposition.Child = soundSpeedEditView;
                }
            }

            if (!isSelect)
            {
                PopupWindow pw = new PopupWindow("未选中编辑项");
                pw.Owner = App.Current.MainWindow;
                pw.ShowDialog();
            }
        }

        private void lbx_ManipulationBoundaryFeedback(object sender, ManipulationBoundaryFeedbackEventArgs e)
        {
            e.Handled = true;
        }
    }
}