PIbd-23-Radaev-A.V.-20/Catamaran/FormCatamaranCollection.cs
2023-12-13 11:50:29 +04:00

210 lines
7.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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