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