PIAPS_CW/WinFormsApp/FormMediaFiles.cs

138 lines
4.7 KiB
C#

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
{
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();
_logger = logger;
_mediafileLogic = mediaFileLogic;
_productLogic = productLogic;
_filename = null;
}
private void LoadData()
{
if (comboBoxProduct.SelectedIndex < 0) return;
_logger.LogInformation("Загрузка файлов");
try
{
var filelist = _mediafileLogic.ReadList(null);
if (filelist != null)
{
dataGridView.DataSource = filelist;
}
}
catch (Exception ex)
{
}
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;
imageBox.BorderStyle = BorderStyle.FixedSingle;
var imageLabel = new Label();
imageLabel.Text = item.Name;
imageLabel.Dock = DockStyle.Top;
imageLabel.TextAlign = ContentAlignment.MiddleCenter;
panelFiles.Controls.Add(imageLabel);
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;
labelFile.Text = System.IO.Path.GetFileName(_filename);
}
}
private void FormMediaFiles_Resize(object sender, EventArgs e)
{
groupBoxFiles.Width = Convert.ToInt32(this.Width * 0.4);
panelFiles.Height = this.Height;
foreach (var control in this.Controls.OfType<PictureBox>())
{
control.Width = panelFiles.Width;
control.Height = Convert.ToInt32(panelFiles.Height * 0.3);
}
}
}
}