PIAPS_CW/WinFormsApp/FormMediaFiles.cs

138 lines
4.7 KiB
C#
Raw Normal View History

2024-06-24 16:23:42 +04:00
using BusinessLogic.BusinessLogic;
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormsApp
{
public partial class FormMediaFiles : Form
{
2024-06-24 16:23:42 +04:00
private readonly ILogger _logger;
private readonly IMediaFileLogic _mediafileLogic;
private readonly IProductLogic _productLogic;
private string? _filename;
public FormMediaFiles(ILogger<FormMediaFiles> logger, IMediaFileLogic mediaFileLogic, IProductLogic productLogic)
{
InitializeComponent();
2024-06-24 16:23:42 +04:00
_logger = logger;
_mediafileLogic = mediaFileLogic;
_productLogic = productLogic;
_filename = null;
}
private void LoadData()
{
if (comboBoxProduct.SelectedIndex < 0) return;
2024-06-24 17:09:03 +04:00
_logger.LogInformation("Загрузка файлов");
try
{
var filelist = _mediafileLogic.ReadList(null);
if (filelist != null)
{
dataGridView.DataSource = filelist;
}
}
catch (Exception ex)
{
}
2024-06-24 16:23:42 +04:00
var list = _mediafileLogic.ReadList(new MediaFileSearchModel()
{
ProductId = (Guid?)comboBoxProduct.SelectedValue ?? Guid.Empty,
});
panelFiles.Controls.Clear();
foreach (var item in list)
{
var imageBox = new PictureBox();
imageBox.Dock = DockStyle.Top;
imageBox.Image = new Bitmap(item.Location);
imageBox.Width = groupBox1.Width;
panelFiles.Controls.Add(imageBox);
imageBox.SizeMode = PictureBoxSizeMode.Zoom;
imageBox.Height = 400;
2024-06-24 17:09:03 +04:00
imageBox.BorderStyle = BorderStyle.FixedSingle;
var imageLabel = new Label();
imageLabel.Text = item.Name;
imageLabel.Dock = DockStyle.Top;
imageLabel.TextAlign = ContentAlignment.MiddleCenter;
panelFiles.Controls.Add(imageLabel);
2024-06-24 16:23:42 +04:00
panelFiles.Refresh();
}
}
private void FormMediaFiles_Load(object sender, EventArgs e)
{
try
{
var list = _productLogic.ReadList(null);
if (list != null)
{
comboBoxProduct.DisplayMember = "Name";
comboBoxProduct.ValueMember = "Id";
comboBoxProduct.DataSource = list;
comboBoxProduct.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки списка продуктов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void comboBoxProduct_SelectedIndexChanged(object sender, EventArgs e)
{
LoadData();
}
private void buttonAdd_Click(object sender, EventArgs e)
{
if (comboBoxProduct.SelectedIndex != -1 && !string.IsNullOrEmpty(_filename))
{
_mediafileLogic.Create(new MediaFileBindingModel()
{
Id = Guid.NewGuid(),
Name = System.IO.Path.GetFileNameWithoutExtension(_filename),
Location = _filename,
ProductId = (Guid)comboBoxProduct.SelectedValue,
});
}
}
private void buttonSelectFile_Click(object sender, EventArgs e)
{
var dialog = new OpenFileDialog();
dialog.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg;*.png;*.jpeg;*.gif;*.bmp";
if (dialog.ShowDialog() == DialogResult.OK)
{
_filename = dialog.FileName;
2024-06-24 17:09:03 +04:00
labelFile.Text = System.IO.Path.GetFileName(_filename);
2024-06-24 16:23:42 +04:00
}
}
private void FormMediaFiles_Resize(object sender, EventArgs e)
{
2024-06-24 17:09:03 +04:00
groupBoxFiles.Width = Convert.ToInt32(this.Width * 0.4);
2024-06-24 16:23:42 +04:00
panelFiles.Height = this.Height;
foreach (var control in this.Controls.OfType<PictureBox>())
{
control.Width = panelFiles.Width;
control.Height = Convert.ToInt32(panelFiles.Height * 0.3);
}
}
}
}