using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using DevExpress.XtraEditors.Controls;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Tile;
using IStation.Extensions;
namespace IStation.WinFormUI.Project
{
public partial class ProjectTileViewCtrl : DevExpress.XtraEditors.XtraUserControl
{
public ProjectTileViewCtrl()
{
InitializeComponent();
}
#region 内部视图类
public class CurrentViewModel
{
public CurrentViewModel(Model.Project rhs)
{
this.Id = rhs.Id;
this.Name = rhs.Name;
this.IsExist = true;
this.LastSaveTime = rhs.UpdateTime;
if (this.IsExist)
{
if (this.LastSaveTime > DateTime.Now.Date)
this.TimeDescription = " 今天";
else if (this.LastSaveTime > DateTime.Now.GetWeekFirst())
this.TimeDescription = " 本周";
else if (this.LastSaveTime > DateTime.Now.GetMonthFirst())
this.TimeDescription = " 本月";
else
this.TimeDescription = " 历史";
}
else
{
this.TimeDescription = " 不存在";
}
this.Model = rhs;
}
///
/// 标识
///
public long Id { get; set; }
///
/// 名称
///
public string Name { get; set; }
///
/// 状态
///
public string Status { get; set; }
///
/// 文件全路径
///
public string FileFullName { get; set; }
///
/// 说明
///
public string Description { get; set; }
///
/// 是否存在
///
public bool IsExist { get; set; }
///
/// 最后保存时间
///
public DateTime? LastSaveTime { get; set; }
///
/// 时间说明
///
public string TimeDescription { get; set; }
///
/// Model
///
public Model.Project Model { get; set; }
}
#endregion
///
/// 回调事件
///
public event Action ReloadDataEvent;
private List _allBindList = null;//所有绑定列表
///
/// 绑定数据
///
public void SetBindingData()
{
_allBindList = new List();
var allProjects = new BLL.Project().QueryAll();
if (allProjects != null && allProjects.Count > 0)
allProjects.ForEach(x => _allBindList.Add(new CurrentViewModel(x)));
_allBindList = _allBindList.OrderByDescending(t => t.LastSaveTime).ToList();
this.bindingSource1.DataSource = _allBindList;
this.bindingSource1.ResetBindings(false);
}
///
/// 绑定数据
///
public void SetBindingData(List allProjects)
{
_allBindList = new List();
if (allProjects != null && allProjects.Count > 0)
allProjects.ForEach(x => _allBindList.Add(new CurrentViewModel(x)));
_allBindList = _allBindList.OrderBy(t => t.LastSaveTime).ToList();
this.bindingSource1.DataSource = _allBindList;
this.bindingSource1.ResetBindings(false);
}
///
/// 获取项目
///
public Model.Project GetProject()
{
var row = this.tileView1.GetFocusedRow() as CurrentViewModel;
if (row == null)
{
XtraMessageBox.Show("未检索到项目信息!");
return default;
}
if (!File.Exists(row.FileFullName))
{
XtraMessageBox.Show("项目文件不存在!");
return default;
}
if (row.Model.Id == GlobalParas.ProjectId)
{
XtraMessageBox.Show("该项目已经打开!");
return default;
}
return row.Model;
}
///
/// 是否显示边框
///
public bool BorderVisible
{
get
{
if (this.tileView1.BorderStyle == BorderStyles.NoBorder)
return false;
return true;
}
set
{
if (value)
this.tileView1.BorderStyle = BorderStyles.Simple;
else
this.tileView1.BorderStyle = BorderStyles.NoBorder;
}
}
//显示提示信息
private void toolTipController1_GetActiveObjectInfo(object sender, DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs e)
{
if (e.SelectedControl != this.gridControl1)
return;
var view = this.gridControl1.GetViewAt(e.ControlMousePosition) as TileView;
if (view == null)
return;
var hi = view.CalcHitInfo(e.ControlMousePosition);
ToolTipControlInfo info = null;
if (hi.InItem)
{
var row = view.GetRow(hi.RowHandle) as CurrentViewModel;
if (row != null)
{
if (row.IsExist)
{
info = new ToolTipControlInfo(row, row.Description);
}
else
{
info = new ToolTipControlInfo(row, "项目不存在");
}
}
}
if (info != null)
{
e.Info = info;
}
}
//点击事件
private void tileView1_ContextButtonClick(object sender, ContextItemClickEventArgs e)
{
var tileViewItem = e.DataItem as TileViewItem;
var row = this.tileView1.GetRow(tileViewItem.RowHandle) as CurrentViewModel;
if (row == null)
return;
//判断项目状态
if (row.Id == GlobalParas.ProjectId)
{
XtraMessageBox.Show("项目已打开,不能移除!");
return;
}
/*//如果不存在直接删除
if (!File.Exists(row.FileFullName))
{
if (!new BLL.Project().DeleteById(row.Id))
{
XtraMessageBox.Show("项目移除失败!");
return;
}
return;
}*/
//提示用户是否移除项目
if (XtraMessageBox.Show("是否要删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
return;
if (!new BLL.Project().DeleteById(row.Id,out string msg))
{
XtraMessageBox.Show("项目移除失败!");
return;
}
_allBindList.Remove(row);
this.bindingSource1.ResetBindings(false);
XtraMessageBox.Show("项目移除成功!");
}
//双击
private void tileView1_ItemDoubleClick(object sender, TileViewItemClickEventArgs e)
{
var row = this.tileView1.GetRow(e.Item.RowHandle) as CurrentViewModel;
if (row == null)
return;
this.ReloadDataEvent?.Invoke(row.Model);
}
}
}