PIbd-23-Radaev-A.V.-20/Catamaran/FormCatamaranCollection.cs

210 lines
7.7 KiB
C#
Raw Normal View History

using Microsoft.Extensions.Logging;
using System;
2023-11-12 11:28:15 +04:00
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;
using Serilog;
2023-11-12 11:28:15 +04:00
namespace Catamaran
{
public partial class FormCatamaranCollection : Form
{
2023-11-19 12:36:59 +04:00
private readonly CatamaransGenericStorage _storage;
2023-11-28 13:45:08 +04:00
2023-11-12 11:28:15 +04:00
public FormCatamaranCollection()
{
InitializeComponent();
2023-11-19 12:36:59 +04:00
_storage = new CatamaransGenericStorage(pictureBoxCollection.Width,
pictureBoxCollection.Height);
2023-11-12 11:28:15 +04:00
}
2023-11-28 13:45:08 +04:00
2023-11-19 12:36:59 +04:00
private void ReloadObjects()
{
int index = SetslistBox.SelectedIndex;
SetslistBox.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
SetslistBox.Items.Add(_storage.Keys[i]);
}
if (SetslistBox.Items.Count > 0 && (index == -1 || index
>= SetslistBox.Items.Count))
{
SetslistBox.SelectedIndex = 0;
}
else if (SetslistBox.Items.Count > 0 && index > -1 &&
index < SetslistBox.Items.Count)
{
SetslistBox.SelectedIndex = index;
}
}
private void ButtonAddStorage_Click(object sender, EventArgs e)
{
string storname = setAddBox.Text;
if (string.IsNullOrEmpty(setAddBox.Text) || _storage.Keys.Contains(storname))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(setAddBox.Text);
ReloadObjects();
}
2023-11-28 13:45:08 +04:00
2023-11-19 12:36:59 +04:00
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image =
_storage[SetslistBox.SelectedItem?.ToString() ?? string.Empty]?.ShowCats();
}
2023-11-28 13:45:08 +04:00
2023-11-19 12:36:59 +04:00
private void ButtonDeleteStorage_Click(object sender, EventArgs e)
{
if (SetslistBox.SelectedIndex == -1)
{
return;
}
2023-11-28 13:45:08 +04:00
if (MessageBox.Show($"Удалить набор {SetslistBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
2023-11-19 12:36:59 +04:00
_storage.DelSet(SetslistBox.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
}
2023-11-28 13:45:08 +04:00
2023-11-19 12:36:59 +04:00
private void ButtonAddCatamaran_Click(object sender, EventArgs e)
2023-11-12 11:28:15 +04:00
{
2023-11-19 12:36:59 +04:00
if (SetslistBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[SetslistBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
2023-11-25 20:02:11 +04:00
FormCatamaranConfig form = new();
form.Show();
2023-11-28 13:45:08 +04:00
Action<DrawningCatamaran>? catamaranDelegate = new((c) =>
{
try
2023-11-12 11:28:15 +04:00
{
bool q = obj + c;
2023-11-12 11:28:15 +04:00
MessageBox.Show("Объект добавлен");
Log.Information($"Добавлен объект в коллекцию {SetslistBox.SelectedItem.ToString() ?? string.Empty}");
2023-11-25 20:02:11 +04:00
c.ChangePictureBoxSize(pictureBoxCollection.Width, pictureBoxCollection.Height);
2023-11-19 12:36:59 +04:00
pictureBoxCollection.Image = obj.ShowCats();
2023-11-12 11:28:15 +04:00
}
catch (StorageOverflowException ex)
2023-11-12 11:28:15 +04:00
{
Log.Warning($"Коллекция {SetslistBox.SelectedItem.ToString() ?? string.Empty} переполнена");
MessageBox.Show(ex.Message);
2023-11-12 11:28:15 +04:00
}
2023-11-25 20:02:11 +04:00
});
form.AddEvent(catamaranDelegate);
2023-11-12 11:28:15 +04:00
}
2023-11-28 13:45:08 +04:00
2023-11-19 12:36:59 +04:00
private void ButtonRemoveCatamaran_Click(object sender, EventArgs e)
2023-11-12 11:28:15 +04:00
{
2023-11-19 12:36:59 +04:00
if (SetslistBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[SetslistBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
2023-11-12 11:28:15 +04:00
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
try
2023-11-12 11:28:15 +04:00
{
int pos = Convert.ToInt32(maskedTextBox.Text);
var q = obj - pos;
2023-11-12 11:28:15 +04:00
MessageBox.Show("Объект удален");
Log.Information($"Удален объект из коллекции {SetslistBox.SelectedItem.ToString() ?? string.Empty} по номеру {pos}");
2023-11-19 12:36:59 +04:00
pictureBoxCollection.Image = obj.ShowCats();
2023-11-12 11:28:15 +04:00
}
catch (CatamaranNotFoundException ex)
{
Log.Warning($"Не получилось удалить объект из коллекции {SetslistBox.SelectedItem.ToString() ?? string.Empty}");
MessageBox.Show(ex.Message);
}
catch (FormatException ex)
2023-11-12 11:28:15 +04:00
{
Log.Warning($"Было введено не число");
MessageBox.Show("Введите число");
2023-11-12 11:28:15 +04:00
}
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
2023-11-19 12:36:59 +04:00
if (SetslistBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[SetslistBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowCats();
2023-11-12 11:28:15 +04:00
}
2023-11-19 12:36:59 +04:00
2023-11-28 13:45:08 +04:00
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
try
2023-11-28 13:45:08 +04:00
{
_storage.SaveData(saveFileDialog.FileName);
2023-11-28 13:45:08 +04:00
MessageBox.Show("Сохранение прошло успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
Log.Information($"Файл {saveFileDialog.FileName} успешно сохранен");
2023-11-28 13:45:08 +04:00
}
catch (Exception ex)
2023-11-28 13:45:08 +04:00
{
Log.Warning("Не удалось сохранить");
MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
2023-11-28 13:45:08 +04:00
}
}
}
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
2023-11-28 13:45:08 +04:00
{
_storage.LoadData(openFileDialog.FileName);
2023-11-28 13:45:08 +04:00
MessageBox.Show("Загрузка прошла успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
Log.Information($"Файл {openFileDialog.FileName} успешно загружен");
2023-11-28 13:45:08 +04:00
foreach (var collection in _storage.Keys)
{
SetslistBox.Items.Add(collection);
}
ReloadObjects();
2023-11-28 13:45:08 +04:00
}
catch (Exception ex)
2023-11-28 13:45:08 +04:00
{
Log.Warning("Не удалось загрузить");
MessageBox.Show($"Не загрузилось: {ex.Message}","Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
2023-11-28 13:45:08 +04:00
}
}
}
2023-11-12 11:28:15 +04:00
}
}