using DevExpress.XtraEditors; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Text; using System.Windows.Forms; namespace IStation.WinFrmUI.Basic { public partial class ProjectMgrDlg : DevExpress.XtraBars.Ribbon.RibbonForm { public ProjectMgrDlg() { InitializeComponent(); this.IconOptions.Icon = WinFrmUI.Properties.Resources.App; this.gridView1.SetNormalView(); this.gridView1.OptionsView.EnableAppearanceOddRow = false; this.gridView1.OptionsView.EnableAppearanceEvenRow = false; this.gridView1.OptionsView.ShowIndicator = true; } public class CurrentViewModel : Model.Project { public CurrentViewModel() { } public CurrentViewModel(Model.Project rhs) : base(rhs) { } } private BLL.Project _bll = new BLL.Project(); private List _allBindList = null; public event Func AddEvent; public event Action EditEvent; public event Func SwitchEvent; /// /// 初始化数据 /// public void SetBindingData() { _allBindList = new List(); var list = _bll.GetAll(); if (list != null) { foreach (var item in list) { var vm = new CurrentViewModel(item); _allBindList.Add(vm); } } this.projectBindingSource.DataSource = _allBindList; } //切换项目 private void barBtnSwitch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var vm = this.gridView1.GetCurrentViewModel(_allBindList); if (vm == null) return; if (vm.ID == Settings.Project.ID) { XtraMessageBox.Show("无法打开正在使用的项目!"); return; } var bol = this.SwitchEvent?.Invoke(vm); if (bol != null && bol.HasValue) { this.gridView1.RefreshData(); } } //添加 private void barBtnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var dlg = new AddProjectDlg(); dlg.SetBindingData(); dlg.ReloadDataEvent += (rhs) => { var vm = new CurrentViewModel(rhs); _allBindList.Add(vm); this.projectBindingSource.ResetBindings(false); var bol = this.AddEvent?.Invoke(vm); if (bol != null && bol.HasValue) { this.gridView1.RefreshData(); } }; dlg.ShowDialog(); } //编辑 private void barBtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var vm = this.gridView1.GetCurrentViewModel(_allBindList); if (vm == null) return; var dlg = new EditProjectDlg(); dlg.SetBindingData(vm); dlg.ReloadDataEvent += (rhs) => { vm.Reset(rhs); this.projectBindingSource.ResetBindings(false); this.EditEvent?.Invoke(rhs); }; dlg.ShowDialog(); } //删除 private void barBtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var vm = this.gridView1.GetCurrentViewModel(_allBindList); if (vm == null) return; if (vm.ID == Settings.Project.ID) { XtraMessageBox.Show("项目已打开,不能移除!"); return; } if (XtraMessageBox.Show("是否要删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) return; if (!_bll.DeleteByID(vm.ID, out string msg)) { XtraMessageBox.Show("项目移除失败!"); return; } _allBindList.Remove(vm); this.projectBindingSource.ResetBindings(false); XtraMessageBox.Show("项目移除成功!"); } //刷新 private void barBtnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { SetBindingData(); } //表格行样式 private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e) { var vm = this.gridView1.GetRow(e.RowHandle) as CurrentViewModel; if (vm == null) return; if (vm.ID == Settings.Project.ID) { e.Appearance.BackColor = Color.PapayaWhip; } else { e.Appearance.BackColor = Color.White; } } //导入项目 private void barBtnImportPorject_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var filePath = ProjectHelper.GetOpenProjectPath(); if (string.IsNullOrEmpty(filePath)) return; var projecId = ProjectHelper.ImportProject(filePath); if (projecId < 1) return; var project = _bll.GetByID(projecId); var vm = new CurrentViewModel(project); _allBindList.Add(vm); this.projectBindingSource.ResetBindings(false); var bol = this.SwitchEvent?.Invoke(vm); if (bol != null && bol.HasValue) { this.gridView1.RefreshData(); } } //导入曲线分析 private void barBtnImportCurveAnalysis_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var filePath = ProjectHelper.GetOpenProjectPath(); if (string.IsNullOrEmpty(filePath)) return; var projecId = ProjectHelper.ImportProject(filePath); if (projecId < 1) { XtraMessageBox.Show("导入失败!"); return; } var project = _bll.GetByID(projecId); var vm = new CurrentViewModel(project); _allBindList.Add(vm); this.projectBindingSource.ResetBindings(false); XtraMessageBox.Show("导入成功"); } //导入测试系统 private void barBtnImportTProduct_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var dlg = new OpenFileDialog(); dlg.Title = "TProduct"; dlg.Filter = "bin文件 (*.bin)|*.bin"; if (dlg.ShowDialog() != DialogResult.OK) return; var enCode = Encoding.GetEncoding("GB2312"); if (enCode == null) { XtraMessageBox.Show("GB2312:解析失败!"); return; } var json = File.ReadAllText(dlg.FileName, enCode); var dataPacking = JsonHelper.Json2Object(json); var result = ConvertHelper.ConvertTProduct(dataPacking); if (!result) { XtraMessageBox.Show("JSON:解析失败!"); return; } XtraMessageBox.Show("导入成功!"); } //导出项目 private void barBtnExportProject_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var vm = this.gridView1.GetCurrentViewModel(_allBindList); if (vm == null) return; if (XtraMessageBox.Show($"是否导出[{vm.Name}]?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) return; ProjectHelper.OtherSaveProject(vm.ID); } } }