2023-11-25 14:55:27 +04:00
|
|
|
|
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 DoubleDeckerbus.Generic;
|
|
|
|
|
using DoubleDeckerbus.Drawing;
|
|
|
|
|
using DoubleDeckerbus.Move_Strategy;
|
2023-12-29 22:22:51 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using DoubleDeckerbus.Exceptions;
|
|
|
|
|
|
2023-11-25 14:55:27 +04:00
|
|
|
|
|
2023-12-15 20:07:22 +04:00
|
|
|
|
|
2023-11-25 14:55:27 +04:00
|
|
|
|
namespace DoubleDeckerbus
|
|
|
|
|
{
|
|
|
|
|
public partial class FormDoubleDeckerbusCollection : Form
|
|
|
|
|
{
|
|
|
|
|
private readonly BusesGenericStorage _storage;
|
2023-12-29 22:22:51 +04:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
public FormDoubleDeckerbusCollection(ILogger<FormDoubleDeckerbusCollection> logger)
|
2023-11-25 14:55:27 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
_storage = new BusesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
2023-12-29 22:22:51 +04:00
|
|
|
|
_logger = logger;
|
2023-11-25 14:55:27 +04:00
|
|
|
|
}
|
|
|
|
|
private void ReloadObjects()
|
|
|
|
|
{
|
|
|
|
|
int index = listBoxStorages.SelectedIndex;
|
|
|
|
|
listBoxStorages.Items.Clear();
|
|
|
|
|
for (int i = 0; i < _storage.Keys.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
listBoxStorages.Items.Add(_storage.Keys[i]);
|
|
|
|
|
}
|
|
|
|
|
if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count))
|
|
|
|
|
{
|
|
|
|
|
listBoxStorages.SelectedIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (listBoxStorages.Items.Count > 0 && index > -1 && index < listBoxStorages.Items.Count)
|
|
|
|
|
{
|
|
|
|
|
listBoxStorages.SelectedIndex = index;
|
|
|
|
|
}
|
2023-12-15 20:07:22 +04:00
|
|
|
|
|
2023-11-25 14:55:27 +04:00
|
|
|
|
}
|
|
|
|
|
private void buttonAddStorage_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxStorageName.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_storage.AddSet(textBoxStorageName.Text);
|
|
|
|
|
ReloadObjects();
|
2023-12-29 22:22:51 +04:00
|
|
|
|
_logger.LogInformation($"Добавлен набор: {textBoxStorageName.Text}");
|
2023-11-25 14:55:27 +04:00
|
|
|
|
}
|
|
|
|
|
private void listBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBus();
|
|
|
|
|
}
|
|
|
|
|
private void buttonDeleteStorage_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-29 22:22:51 +04:00
|
|
|
|
string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty;
|
2023-11-25 14:55:27 +04:00
|
|
|
|
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
|
|
|
|
{
|
2023-12-29 22:22:51 +04:00
|
|
|
|
_storage.DelSet(name);
|
2023-11-25 14:55:27 +04:00
|
|
|
|
ReloadObjects();
|
2023-12-29 22:22:51 +04:00
|
|
|
|
_logger.LogInformation($"Удалён набор: {name}");
|
2023-11-25 14:55:27 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-15 20:07:22 +04:00
|
|
|
|
|
2023-12-29 22:22:51 +04:00
|
|
|
|
|
2023-11-25 14:55:27 +04:00
|
|
|
|
private void buttonAddBus_Click(object sender, EventArgs e)
|
2023-11-25 15:23:25 +04:00
|
|
|
|
{
|
2023-12-15 20:07:22 +04:00
|
|
|
|
|
2023-11-25 15:23:25 +04:00
|
|
|
|
var formBusConfig = new FormBusConfig();
|
|
|
|
|
formBusConfig.AddEvent(AddBus);
|
|
|
|
|
formBusConfig.Show();
|
|
|
|
|
}
|
2023-12-15 20:07:22 +04:00
|
|
|
|
|
2023-11-25 15:23:25 +04:00
|
|
|
|
private void AddBus(DrawingBus selectedBus)
|
2023-11-25 14:55:27 +04:00
|
|
|
|
{
|
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не выбран набор");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-29 22:22:51 +04:00
|
|
|
|
selectedBus.ChangeBordersPicture(Width, Height);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (obj + selectedBus != -1)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Объект добавлен");
|
|
|
|
|
pictureBoxCollection.Image = obj.ShowBus();
|
|
|
|
|
_logger.LogInformation($"Добавлен объект: {selectedBus.EntityBus.BodyColor}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не удалось добавить объект");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (StorageOverflowException ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
_logger.LogWarning(ex.Message);
|
|
|
|
|
}
|
2023-11-25 14:55:27 +04:00
|
|
|
|
}
|
2023-12-15 20:07:22 +04:00
|
|
|
|
|
2023-11-25 14:55:27 +04:00
|
|
|
|
private void buttonDeleteBus_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-12-29 22:22:51 +04:00
|
|
|
|
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
|
|
|
|
if (obj - pos)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Объект удален");
|
|
|
|
|
pictureBoxCollection.Image = obj.ShowBus();
|
|
|
|
|
_logger.LogInformation($"Удалён объект по позиции : {pos}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не удалось удалить объект");
|
|
|
|
|
}
|
2023-11-25 14:55:27 +04:00
|
|
|
|
}
|
2023-12-29 22:22:51 +04:00
|
|
|
|
catch (FormatException ex)
|
2023-11-25 14:55:27 +04:00
|
|
|
|
{
|
2023-12-29 22:22:51 +04:00
|
|
|
|
MessageBox.Show("Неверный формат ввода");
|
|
|
|
|
_logger.LogWarning("Неверный формат ввода");
|
2023-11-25 14:55:27 +04:00
|
|
|
|
}
|
2023-12-29 22:22:51 +04:00
|
|
|
|
catch (Exception ex)
|
2023-11-25 14:55:27 +04:00
|
|
|
|
{
|
2023-12-29 22:22:51 +04:00
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
_logger.LogWarning(ex.Message);
|
2023-11-25 14:55:27 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void buttonUpdate_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
|
|
|
|
string.Empty];
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
pictureBoxCollection.Image = obj.ShowBus();
|
|
|
|
|
}
|
2023-12-29 22:22:51 +04:00
|
|
|
|
|
2023-12-15 20:07:22 +04:00
|
|
|
|
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
2023-12-29 22:22:51 +04:00
|
|
|
|
try
|
2023-12-15 20:07:22 +04:00
|
|
|
|
{
|
2023-12-29 22:22:51 +04:00
|
|
|
|
_storage.SaveData(saveFileDialog.FileName);
|
2023-12-15 20:07:22 +04:00
|
|
|
|
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
2023-12-29 22:22:51 +04:00
|
|
|
|
_logger.LogInformation($"Файл сохранён по пути: {saveFileDialog.FileName}");
|
2023-12-15 20:07:22 +04:00
|
|
|
|
}
|
2023-12-29 22:22:51 +04:00
|
|
|
|
catch (InvalidOperationException ex)
|
2023-12-15 20:07:22 +04:00
|
|
|
|
{
|
2023-12-29 22:22:51 +04:00
|
|
|
|
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
_logger.LogWarning(ex.Message);
|
2023-12-15 20:07:22 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
2023-12-29 22:22:51 +04:00
|
|
|
|
try
|
2023-12-15 20:07:22 +04:00
|
|
|
|
{
|
2023-12-29 22:22:51 +04:00
|
|
|
|
_storage.LoadData(openFileDialog.FileName);
|
2023-12-15 20:07:22 +04:00
|
|
|
|
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
2023-12-29 22:22:51 +04:00
|
|
|
|
_logger.LogInformation($"Файл загружен по пути: {openFileDialog.FileName}");
|
2023-12-15 20:07:22 +04:00
|
|
|
|
}
|
2023-12-29 22:22:51 +04:00
|
|
|
|
catch (Exception ex)
|
2023-12-15 20:07:22 +04:00
|
|
|
|
{
|
2023-12-29 22:22:51 +04:00
|
|
|
|
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
_logger.LogWarning(ex.Message);
|
2023-12-15 20:07:22 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-29 22:22:51 +04:00
|
|
|
|
ReloadObjects();
|
2023-12-15 20:07:22 +04:00
|
|
|
|
}
|
2023-11-25 14:55:27 +04:00
|
|
|
|
}
|
|
|
|
|
}
|