2023-11-28 12:26:51 +04:00
|
|
|
|
using System.Windows.Forms;
|
2023-11-28 13:43:14 +04:00
|
|
|
|
using ElectricLocomotive.Exceptions;
|
|
|
|
|
using Serilog;
|
2023-11-28 12:26:51 +04:00
|
|
|
|
|
|
|
|
|
namespace ElectricLocomotive;
|
2023-10-17 13:49:31 +04:00
|
|
|
|
|
|
|
|
|
public partial class FormLocomotivCollection : Form {
|
2023-10-31 13:25:13 +04:00
|
|
|
|
private readonly LocosGenericStorage _storage;
|
2023-10-17 13:49:31 +04:00
|
|
|
|
public FormLocomotivCollection() {
|
|
|
|
|
InitializeComponent();
|
2023-10-31 13:25:13 +04:00
|
|
|
|
_storage = new(collectionPictureBox.Width, collectionPictureBox.Height);
|
|
|
|
|
}
|
|
|
|
|
private void ReloadObjects() {
|
|
|
|
|
int index = storageListBox.SelectedIndex;
|
|
|
|
|
storageListBox.Items.Clear();
|
|
|
|
|
for (int i = 0; i < _storage.Keys.Count; i++) {
|
|
|
|
|
storageListBox.Items.Add(_storage.Keys[i]);
|
|
|
|
|
}
|
|
|
|
|
if (storageListBox.Items.Count > 0 && (index == -1 || index
|
|
|
|
|
>= storageListBox.Items.Count)) {
|
|
|
|
|
storageListBox.SelectedIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (storageListBox.Items.Count > 0 && index > -1 &&
|
|
|
|
|
index < storageListBox.Items.Count) {
|
|
|
|
|
storageListBox.SelectedIndex = index;
|
|
|
|
|
}
|
2023-10-17 13:49:31 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addLocomotiv_Click(object sender, EventArgs e) {
|
2023-10-31 13:25:13 +04:00
|
|
|
|
if (storageListBox.SelectedIndex == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage[storageListBox.SelectedItem.ToString() ?? string.Empty];
|
|
|
|
|
if (obj == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-11-14 12:17:37 +04:00
|
|
|
|
FormLocoConfig form = new();
|
|
|
|
|
form.Show();
|
|
|
|
|
Action<DrawingLocomotiv>? monorailDelegate = new((m) => {
|
2023-11-28 13:43:14 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
bool q = obj + m;
|
2023-10-17 13:49:31 +04:00
|
|
|
|
MessageBox.Show("Объект добавлен");
|
2023-11-28 13:43:14 +04:00
|
|
|
|
Log.Information($"Добавлен объект в коллекцию {storageListBox.SelectedItem.ToString() ?? string.Empty}");
|
2023-11-14 12:17:37 +04:00
|
|
|
|
m.ChangePictureBoxSize(collectionPictureBox.Width, collectionPictureBox.Height);
|
2023-10-31 13:25:13 +04:00
|
|
|
|
collectionPictureBox.Image = obj.ShowLocos();
|
2023-10-17 13:49:31 +04:00
|
|
|
|
}
|
2023-11-28 13:43:14 +04:00
|
|
|
|
catch (StorageOverflowException ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Warning($"Коллекция {storageListBox.SelectedItem.ToString() ?? string.Empty} переполнена");
|
|
|
|
|
MessageBox.Show(ex.Message);
|
2023-10-17 13:49:31 +04:00
|
|
|
|
}
|
2023-11-14 12:17:37 +04:00
|
|
|
|
});
|
|
|
|
|
form.AddEvent(monorailDelegate);
|
2023-10-17 13:49:31 +04:00
|
|
|
|
}
|
|
|
|
|
private void deleteLoco_Click(object sender, EventArgs e) {
|
2023-10-31 13:25:13 +04:00
|
|
|
|
if (storageListBox.SelectedIndex == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage[storageListBox.SelectedItem.ToString() ??
|
|
|
|
|
string.Empty];
|
|
|
|
|
if (obj == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (MessageBox.Show("Удалить объект?", "Удаление",
|
|
|
|
|
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) {
|
2023-10-17 13:49:31 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-11-28 13:43:14 +04:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int pos = Convert.ToInt32(locoIndexInput.Text);
|
|
|
|
|
var q = obj - pos;
|
2023-10-17 13:49:31 +04:00
|
|
|
|
MessageBox.Show("Объект удален");
|
2023-11-28 13:43:14 +04:00
|
|
|
|
Log.Information($"Удален объект из коллекции {storageListBox.SelectedItem.ToString() ?? string.Empty} по номеру {pos}");
|
2023-10-31 13:25:13 +04:00
|
|
|
|
collectionPictureBox.Image = obj.ShowLocos();
|
2023-10-17 13:49:31 +04:00
|
|
|
|
}
|
2023-11-28 13:43:14 +04:00
|
|
|
|
catch(LocoNotFoundException ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Warning($"Не получилось удалить объект из коллекции {storageListBox.SelectedItem.ToString() ?? string.Empty}");
|
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
catch(FormatException ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Warning($"Было введено не число");
|
|
|
|
|
MessageBox.Show("Введите число");
|
2023-10-17 13:49:31 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void refreshCollection_Click(object sender, EventArgs e) {
|
2023-10-31 13:25:13 +04:00
|
|
|
|
if (storageListBox.SelectedIndex == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage[storageListBox.SelectedItem.ToString() ??
|
|
|
|
|
string.Empty];
|
|
|
|
|
if (obj == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
collectionPictureBox.Image = obj.ShowLocos();
|
|
|
|
|
}
|
|
|
|
|
private void storagesListBox_SelectedIndexChanged(object sender, EventArgs e) {
|
|
|
|
|
collectionPictureBox.Image =
|
|
|
|
|
_storage[storageListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowLocos();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void delStorageButton_Click(object sender, EventArgs e) {
|
|
|
|
|
if (storageListBox.SelectedIndex == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (MessageBox.Show($"Удалить объект{storageListBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
|
|
|
|
|
MessageBoxIcon.Question) == DialogResult.Yes) {
|
2023-11-28 13:43:14 +04:00
|
|
|
|
string name = storageListBox.SelectedItem.ToString() ?? string.Empty;
|
|
|
|
|
_storage.DelSet(name);
|
2023-10-31 13:25:13 +04:00
|
|
|
|
ReloadObjects();
|
2023-11-28 13:43:14 +04:00
|
|
|
|
Log.Information($"Удален набор: {name}");
|
2023-10-31 13:25:13 +04:00
|
|
|
|
}
|
2023-10-17 13:49:31 +04:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-31 13:25:13 +04:00
|
|
|
|
private void addStorageButton_Click(object sender, EventArgs e) {
|
|
|
|
|
if (string.IsNullOrEmpty(storageIndexInput.Text)) {
|
|
|
|
|
MessageBox.Show("Не все данные заполнены", "Ошибка",
|
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_storage.AddSet(storageIndexInput.Text);
|
|
|
|
|
ReloadObjects();
|
2023-11-28 13:43:14 +04:00
|
|
|
|
Log.Information($"Добавлен набор: {storageIndexInput.Text}");
|
2023-10-31 13:25:13 +04:00
|
|
|
|
}
|
2023-11-28 12:26:51 +04:00
|
|
|
|
|
|
|
|
|
private void SaveToolStripMenuItem_Click(object sender, EventArgs e) {
|
|
|
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
2023-11-28 13:43:14 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_storage.SaveData(saveFileDialog.FileName);
|
2023-11-28 12:26:51 +04:00
|
|
|
|
MessageBox.Show("Сохранение прошло успешно",
|
|
|
|
|
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
2023-11-28 13:43:14 +04:00
|
|
|
|
Log.Information($"Файл {saveFileDialog.FileName} успешно сохранен");
|
2023-11-28 12:26:51 +04:00
|
|
|
|
}
|
2023-11-28 13:43:14 +04:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Warning("Не удалось сохранить");
|
|
|
|
|
MessageBox.Show($"Не сохранилось: {ex.Message}",
|
|
|
|
|
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2023-11-28 12:26:51 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadToolStripMenuItem_Click(object sender, EventArgs e) {
|
|
|
|
|
if (loadFileDialog.ShowDialog() == DialogResult.OK) {
|
2023-11-28 13:43:14 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_storage.LoadData(loadFileDialog.FileName);
|
2023-11-28 12:26:51 +04:00
|
|
|
|
MessageBox.Show("Загрузка прошла успешно",
|
|
|
|
|
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
2023-11-28 13:43:14 +04:00
|
|
|
|
Log.Information($"Файл {loadFileDialog.FileName} успешно загружен");
|
|
|
|
|
foreach (var collection in _storage.Keys)
|
|
|
|
|
{
|
2023-11-28 12:26:51 +04:00
|
|
|
|
storageListBox.Items.Add(collection);
|
|
|
|
|
}
|
2023-11-28 13:43:14 +04:00
|
|
|
|
ReloadObjects();
|
2023-11-28 12:26:51 +04:00
|
|
|
|
}
|
2023-11-28 13:43:14 +04:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Warning("Не удалось загрузить");
|
|
|
|
|
MessageBox.Show($"Не загрузилось: {ex.Message}",
|
|
|
|
|
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2023-11-28 12:26:51 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-17 13:49:31 +04:00
|
|
|
|
}
|