using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace AGVMapEditor.Models
{
///
/// PropertyGrid에서 이미지 파일 경로를 선택하기 위한 커스텀 UITypeEditor
/// PropertyGrid에 "..." 버튼을 표시하고, 클릭 시 파일 열기 대화상자를 표시
///
public class ImagePathEditor : UITypeEditor
{
///
/// PropertyGrid에서 이 에디터의 UI 스타일 반환
/// DropDown 스타일을 사용하여 "..." 버튼을 표시
///
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
///
/// 사용자가 "..." 버튼을 클릭할 때 호출되는 메서드
///
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
// IWindowsFormsEditorService를 얻어서 대화상자를 표시
var editorService = provider?.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
if (editorService == null)
return value;
// 파일 열기 대화상자 생성
using (var ofd = new OpenFileDialog())
{
ofd.Title = "이미지 파일 선택";
ofd.Filter = "이미지 파일|*.jpg;*.jpeg;*.png;*.bmp;*.gif|모든 파일|*.*";
ofd.CheckFileExists = true;
// 현재 경로가 있으면 해당 위치에서 시작
if (!string.IsNullOrEmpty(value?.ToString()))
{
try
{
string currentPath = value.ToString();
if (System.IO.File.Exists(currentPath))
{
ofd.InitialDirectory = System.IO.Path.GetDirectoryName(currentPath);
ofd.FileName = System.IO.Path.GetFileName(currentPath);
}
}
catch { }
}
// 대화상자 표시
if (ofd.ShowDialog() == DialogResult.OK)
{
// 선택된 파일 경로를 Base64로 변환하고 반환
string filePath = ofd.FileName;
return filePath; // MapNode의 ConvertImageToBase64는 setter에서 호출됨
}
}
return value;
}
///
/// PropertyGrid에서 이 타입의 값을 표시하는 방법
/// 파일 경로를 파일명만 표시하도록 처리
///
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return false;
}
}
}