using WorkCameraExport.Models; using WorkCameraExport.Services; using WorkCameraExport.Services.Interfaces; namespace WorkCameraExport.Forms { /// /// 设置窗体 - 用户可配置项设置 /// public partial class SettingsForm : Form { private readonly IConfigService _configService; private readonly ILogService? _logService; private AppConfig _config; public SettingsForm(IConfigService configService, ILogService? logService = null) { InitializeComponent(); _configService = configService; _logService = logService; _config = _configService.LoadConfig(); LoadSettings(); } /// /// 加载设置到界面 /// private void LoadSettings() { txtDefaultSavePath.Text = _config.DefaultSavePath; numConcurrency.Value = _config.ImageDownloadConcurrency; trackQuality.Value = _config.ImageCompressQuality; lblQualityValue.Text = $"{_config.ImageCompressQuality}%"; chkAutoCleanTemp.Checked = _config.AutoCleanTempFiles; } /// /// 浏览默认保存路径 /// private void btnBrowsePath_Click(object sender, EventArgs e) { using var dialog = new FolderBrowserDialog { Description = "选择默认保存目录", UseDescriptionForTitle = true, SelectedPath = txtDefaultSavePath.Text }; if (dialog.ShowDialog() == DialogResult.OK) { txtDefaultSavePath.Text = dialog.SelectedPath; } } /// /// 压缩质量滑块值变化 /// private void trackQuality_Scroll(object sender, EventArgs e) { lblQualityValue.Text = $"{trackQuality.Value}%"; } /// /// 保存按钮点击 /// private void btnSave_Click(object sender, EventArgs e) { try { // 验证保存路径 if (!string.IsNullOrWhiteSpace(txtDefaultSavePath.Text) && !Directory.Exists(txtDefaultSavePath.Text)) { var result = MessageBox.Show( "指定的保存路径不存在,是否创建?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { Directory.CreateDirectory(txtDefaultSavePath.Text); } else { txtDefaultSavePath.Focus(); return; } } // 更新配置 _config.DefaultSavePath = txtDefaultSavePath.Text; _config.ImageDownloadConcurrency = (int)numConcurrency.Value; _config.ImageCompressQuality = trackQuality.Value; _config.AutoCleanTempFiles = chkAutoCleanTemp.Checked; // 保存配置 _configService.SaveConfig(_config); _logService?.Info("设置已保存"); MessageBox.Show("设置已保存", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; Close(); } catch (Exception ex) { _logService?.Error("保存设置失败", ex); MessageBox.Show($"保存设置失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } /// /// 取消按钮点击 /// private void btnCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } /// /// 重置为默认值按钮点击 /// private void btnReset_Click(object sender, EventArgs e) { var result = MessageBox.Show( "确定要重置所有设置为默认值吗?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { _config = AppConfig.CreateDefault(); LoadSettings(); _logService?.Info("设置已重置为默认值"); } } /// /// 清理临时文件按钮点击 /// private void btnCleanTemp_Click(object sender, EventArgs e) { try { _configService.CleanTempFiles(); _logService?.Info("临时文件已清理"); MessageBox.Show("临时文件已清理", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { _logService?.Error("清理临时文件失败", ex); MessageBox.Show($"清理临时文件失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } /// /// 导出日志按钮点击 /// private void btnExportLogs_Click(object sender, EventArgs e) { using var dialog = new SaveFileDialog { Title = "导出日志文件", Filter = "ZIP 文件|*.zip", FileName = $"WorkCameraExport_Logs_{DateTime.Now:yyyyMMdd_HHmmss}.zip", DefaultExt = "zip" }; if (dialog.ShowDialog() == DialogResult.OK) { try { // 打包日志目录 var logPath = _configService.LogPath; if (Directory.Exists(logPath)) { System.IO.Compression.ZipFile.CreateFromDirectory(logPath, dialog.FileName); _logService?.Info($"日志已导出到: {dialog.FileName}"); var result = MessageBox.Show( "日志导出成功!是否打开文件所在目录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (result == DialogResult.Yes) { System.Diagnostics.Process.Start("explorer.exe", $"/select,\"{dialog.FileName}\""); } } else { MessageBox.Show("日志目录不存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception ex) { _logService?.Error("导出日志失败", ex); MessageBox.Show($"导出日志失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } }