2023-11-04 16:22:00 +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 DumpTruck.Generics;
|
|
|
|
|
using DumpTruck.DrawingObjects;
|
|
|
|
|
using DumpTruck.MovementStrategy;
|
2023-12-15 21:24:09 +04:00
|
|
|
|
using DumpTruck.Exceptions;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-11-04 16:22:00 +04:00
|
|
|
|
|
|
|
|
|
namespace DumpTruck
|
|
|
|
|
{
|
2023-11-04 16:38:42 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Форма для работы с набором объектов класса DrawingTruck
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class FormTruckCollection : Form
|
|
|
|
|
{
|
2023-11-04 16:22:00 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Набор объектов
|
|
|
|
|
/// </summary>
|
2023-11-04 16:38:42 +04:00
|
|
|
|
private readonly TrucksGenericStorage _storage;
|
|
|
|
|
|
2023-12-15 21:24:09 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Логер
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2023-11-04 16:22:00 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
2023-12-15 21:24:09 +04:00
|
|
|
|
public FormTruckCollection(ILogger<FormTruckCollection> logger)
|
2023-11-04 16:38:42 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
_storage = new TrucksGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
2023-12-15 21:24:09 +04:00
|
|
|
|
_logger = logger;
|
2023-11-04 16:22:00 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-04 16:38:42 +04:00
|
|
|
|
private void ReloadObjects()
|
2023-11-04 16:22:00 +04:00
|
|
|
|
{
|
2023-11-04 16:38:42 +04:00
|
|
|
|
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))
|
2023-11-04 16:22:00 +04:00
|
|
|
|
{
|
2023-11-04 16:38:42 +04:00
|
|
|
|
listBoxStorages.SelectedIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (listBoxStorages.Items.Count > 0 && index > -1 &&
|
|
|
|
|
index < listBoxStorages.Items.Count)
|
|
|
|
|
{
|
|
|
|
|
listBoxStorages.SelectedIndex = index;
|
2023-11-04 16:22:00 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-04 16:38:42 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление набора в коллекцию
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void buttonAddObject_Click(object sender, EventArgs e)
|
2023-11-04 16:22:00 +04:00
|
|
|
|
{
|
2023-11-04 16:38:42 +04:00
|
|
|
|
if (string.IsNullOrEmpty(textBoxStorageName.Text))
|
2023-11-04 16:22:00 +04:00
|
|
|
|
{
|
2023-11-04 16:38:42 +04:00
|
|
|
|
MessageBox.Show("Не все данные заполнены", "Ошибка",
|
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2023-11-04 16:22:00 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-11-04 16:38:42 +04:00
|
|
|
|
_storage.AddSet(textBoxStorageName.Text);
|
|
|
|
|
ReloadObjects();
|
2023-12-15 21:24:09 +04:00
|
|
|
|
_logger.LogInformation($"Добавлен набор: {textBoxStorageName.Text}");
|
2023-11-04 16:38:42 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Выбор набора
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowTrucks();
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление набора
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void buttonDelObject_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
2023-11-04 16:22:00 +04:00
|
|
|
|
{
|
2023-11-04 16:38:42 +04:00
|
|
|
|
return;
|
2023-11-04 16:22:00 +04:00
|
|
|
|
}
|
2023-12-15 21:24:09 +04:00
|
|
|
|
string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty;
|
|
|
|
|
if (MessageBox.Show($"Удалить объект {name}?", "Удаление",
|
|
|
|
|
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
2023-11-04 16:22:00 +04:00
|
|
|
|
{
|
2023-12-15 21:24:09 +04:00
|
|
|
|
_storage.DelSet(name);
|
2023-11-04 16:38:42 +04:00
|
|
|
|
ReloadObjects();
|
2023-12-15 21:24:09 +04:00
|
|
|
|
_logger.LogInformation($"Удален набор: {name}");
|
2023-11-04 16:22:00 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-24 20:27:04 +04:00
|
|
|
|
private void AddTruck(DrawingTruck truck)
|
|
|
|
|
{
|
2023-12-01 19:22:27 +04:00
|
|
|
|
truck._pictureWidth = pictureBoxCollection.Image.Width;
|
|
|
|
|
truck._pictureHeight = pictureBoxCollection.Image.Height;
|
2023-11-04 16:38:42 +04:00
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
2023-12-15 21:24:09 +04:00
|
|
|
|
_logger.LogWarning($"Не удалось добавить объект: набор пуст");
|
2023-11-04 16:38:42 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-15 21:24:09 +04:00
|
|
|
|
try
|
2023-11-24 20:27:04 +04:00
|
|
|
|
{
|
2023-12-15 21:24:09 +04:00
|
|
|
|
if (obj + truck != -1)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Объект добавлен");
|
|
|
|
|
pictureBoxCollection.Image = obj.ShowTrucks();
|
|
|
|
|
_logger.LogInformation($"Объект добавлен {truck}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не удалось добавить объект");
|
|
|
|
|
}
|
2023-11-24 20:27:04 +04:00
|
|
|
|
}
|
2023-12-15 21:24:09 +04:00
|
|
|
|
catch (ApplicationException ex)
|
2023-11-24 20:27:04 +04:00
|
|
|
|
{
|
2023-12-15 21:24:09 +04:00
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
_logger.LogWarning($"Не удалось добавить объект: {ex.Message}");
|
2023-11-24 20:27:04 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonAddTruck_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
|
|
|
|
{
|
2023-12-15 21:24:09 +04:00
|
|
|
|
_logger.LogWarning($"Не удалось добавить объект: не выбран набор");
|
2023-11-24 20:27:04 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var formTruckConfig = new FormTruckConfig();
|
|
|
|
|
formTruckConfig.AddEvent(AddTruck);
|
|
|
|
|
formTruckConfig.Show();
|
|
|
|
|
}
|
2023-11-04 16:22:00 +04:00
|
|
|
|
|
2023-11-24 20:27:04 +04:00
|
|
|
|
private void buttonRemoveTruck_Click(object sender, EventArgs e)
|
2023-11-04 16:38:42 +04:00
|
|
|
|
{
|
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
|
|
|
|
{
|
2023-12-15 21:24:09 +04:00
|
|
|
|
_logger.LogWarning($"Не удалось удалить объект: не выбран набор");
|
2023-11-04 16:38:42 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
2023-12-15 21:24:09 +04:00
|
|
|
|
_logger.LogWarning($"Не удалось удалить объект: набор пуст");
|
2023-11-04 16:38:42 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (MessageBox.Show("Удалить объект?", "Удаление",
|
|
|
|
|
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
2023-12-15 21:24:09 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (obj - pos)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Объект удален");
|
|
|
|
|
pictureBoxCollection.Image = obj.ShowTrucks();
|
|
|
|
|
_logger.LogInformation($"Удален объект на позиции: {pos}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не удалось удалить объект");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (TruckNotFoundException ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
_logger.LogWarning($"Не удалось удалить объект: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-04 16:38:42 +04:00
|
|
|
|
|
2023-12-15 21:24:09 +04:00
|
|
|
|
private void buttonRefreshCollection_Click(object sender, EventArgs e)
|
2023-11-04 16:38:42 +04:00
|
|
|
|
{
|
|
|
|
|
if (listBoxStorages.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
pictureBoxCollection.Image = obj.ShowTrucks();
|
|
|
|
|
}
|
2023-12-01 19:22:27 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обработка нажатия "Сохранение"
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
2023-12-15 21:24:09 +04:00
|
|
|
|
try
|
2023-12-01 19:22:27 +04:00
|
|
|
|
{
|
2023-12-15 21:24:09 +04:00
|
|
|
|
_storage.SaveData(saveFileDialog.FileName);
|
2023-12-01 19:22:27 +04:00
|
|
|
|
MessageBox.Show("Сохранение прошло успешно",
|
|
|
|
|
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
2023-12-15 21:24:09 +04:00
|
|
|
|
_logger.LogInformation($"Сохранение прошло успешно: {saveFileDialog.FileName}");
|
2023-12-01 19:22:27 +04:00
|
|
|
|
}
|
2023-12-15 21:24:09 +04:00
|
|
|
|
catch (Exception ex)
|
2023-12-01 19:22:27 +04:00
|
|
|
|
{
|
2023-12-15 21:24:09 +04:00
|
|
|
|
MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат",
|
2023-12-01 19:22:27 +04:00
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обработка нажатия "Загрузка"
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
2023-12-15 21:24:09 +04:00
|
|
|
|
try
|
2023-12-01 19:22:27 +04:00
|
|
|
|
{
|
2023-12-15 21:24:09 +04:00
|
|
|
|
_storage.LoadData(openFileDialog.FileName);
|
2023-12-01 19:22:27 +04:00
|
|
|
|
MessageBox.Show("Загрузка прошла успешно",
|
|
|
|
|
"Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
ReloadObjects();
|
2023-12-15 21:24:09 +04:00
|
|
|
|
_logger.LogInformation($"Загрузка прошла успешно: {openFileDialog.FileName}");
|
2023-12-01 19:22:27 +04:00
|
|
|
|
}
|
2023-12-15 21:24:09 +04:00
|
|
|
|
catch (Exception ex)
|
2023-12-01 19:22:27 +04:00
|
|
|
|
{
|
2023-12-15 21:24:09 +04:00
|
|
|
|
MessageBox.Show($"Не загрузилось {ex.Message}", "Результат",
|
2023-12-01 19:22:27 +04:00
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2023-12-15 21:24:09 +04:00
|
|
|
|
_logger.LogWarning($"Не загрузилось: {ex.Message}");
|
2023-12-01 19:22:27 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-04 16:22:00 +04:00
|
|
|
|
}
|