PIbd-21_MasenkinMS_LabWork04

This commit is contained in:
parent 1b3e1d0826
commit e40550d7a2
5 changed files with 316 additions and 67 deletions

View File

@ -30,7 +30,11 @@
{
pictureBoxCollection = new PictureBox();
panelTools = new Panel();
maskedTextBox = new MaskedTextBox();
buttonDeleteObject = new Button();
listBoxStorages = new ListBox();
maskedTextBoxStorageName = new MaskedTextBox();
buttonAddObject = new Button();
maskedTextBoxNumber = new MaskedTextBox();
buttonRefreshCollection = new Button();
buttonRemoveBus = new Button();
buttonAddBus = new Button();
@ -49,7 +53,11 @@
//
// panelTools
//
panelTools.Controls.Add(maskedTextBox);
panelTools.Controls.Add(buttonDeleteObject);
panelTools.Controls.Add(listBoxStorages);
panelTools.Controls.Add(maskedTextBoxStorageName);
panelTools.Controls.Add(buttonAddObject);
panelTools.Controls.Add(maskedTextBoxNumber);
panelTools.Controls.Add(buttonRefreshCollection);
panelTools.Controls.Add(buttonRemoveBus);
panelTools.Controls.Add(buttonAddBus);
@ -59,16 +67,53 @@
panelTools.Size = new Size(174, 461);
panelTools.TabIndex = 1;
//
// maskedTextBox
// buttonDeleteObject
//
maskedTextBox.Location = new Point(30, 160);
maskedTextBox.Name = "maskedTextBox";
maskedTextBox.Size = new Size(120, 23);
maskedTextBox.TabIndex = 4;
buttonDeleteObject.Location = new Point(30, 180);
buttonDeleteObject.Name = "buttonDeleteObject";
buttonDeleteObject.Size = new Size(120, 40);
buttonDeleteObject.TabIndex = 8;
buttonDeleteObject.Text = "Удалить набор";
buttonDeleteObject.UseVisualStyleBackColor = true;
buttonDeleteObject.Click += buttonDeleteObject_Click;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(30, 110);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(120, 64);
listBoxStorages.TabIndex = 7;
listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged;
//
// maskedTextBoxStorageName
//
maskedTextBoxStorageName.Location = new Point(30, 30);
maskedTextBoxStorageName.Name = "maskedTextBoxStorageName";
maskedTextBoxStorageName.Size = new Size(120, 23);
maskedTextBoxStorageName.TabIndex = 6;
//
// buttonAddObject
//
buttonAddObject.Location = new Point(30, 60);
buttonAddObject.Name = "buttonAddObject";
buttonAddObject.Size = new Size(120, 40);
buttonAddObject.TabIndex = 5;
buttonAddObject.Text = "Добавить набор";
buttonAddObject.UseVisualStyleBackColor = true;
buttonAddObject.Click += buttonAddObject_Click;
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(30, 300);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(120, 23);
maskedTextBoxNumber.TabIndex = 4;
//
// buttonRefreshCollection
//
buttonRefreshCollection.Location = new Point(30, 290);
buttonRefreshCollection.Location = new Point(30, 400);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(120, 40);
buttonRefreshCollection.TabIndex = 3;
@ -78,7 +123,7 @@
//
// buttonRemoveBus
//
buttonRemoveBus.Location = new Point(30, 190);
buttonRemoveBus.Location = new Point(30, 330);
buttonRemoveBus.Name = "buttonRemoveBus";
buttonRemoveBus.Size = new Size(120, 40);
buttonRemoveBus.TabIndex = 2;
@ -88,7 +133,7 @@
//
// buttonAddBus
//
buttonAddBus.Location = new Point(30, 60);
buttonAddBus.Location = new Point(30, 250);
buttonAddBus.Name = "buttonAddBus";
buttonAddBus.Size = new Size(120, 40);
buttonAddBus.TabIndex = 1;
@ -125,9 +170,13 @@
private PictureBox pictureBoxCollection;
private Panel panelTools;
private Label labelTools;
private MaskedTextBox maskedTextBox;
private MaskedTextBox maskedTextBoxNumber;
private Button buttonRefreshCollection;
private Button buttonRemoveBus;
private Button buttonAddBus;
private MaskedTextBox maskedTextBoxStorageName;
private Button buttonAddObject;
private Button buttonDeleteObject;
private ListBox listBoxStorages;
}
}

View File

@ -22,7 +22,7 @@ namespace AccordionBus
/// <summary>
/// Набор объектов
/// </summary>
private readonly BusGenericCollection<DrawingBus, DrawingObjectBus> _buses;
private readonly BusGenericStorage _storage;
/// <summary>
/// Конструктор
@ -30,7 +30,76 @@ namespace AccordionBus
public BusCollectionForm()
{
InitializeComponent();
_buses = new BusGenericCollection<DrawingBus, DrawingObjectBus>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new BusGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
/// <summary>
/// Заполнение ListBoxObject
/// </summary>
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;
}
}
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(buttonAddObject.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(maskedTextBoxStorageName.Text);
ReloadObjects();
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBuses();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonDeleteObject_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
}
/// <summary>
@ -40,13 +109,24 @@ namespace AccordionBus
/// <param name="e"></param>
private void buttonAddBus_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
AccordionBusForm form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_buses + form.SelectedBus > -1)
if (obj + form.SelectedBus > -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _buses.ShowBuses();
pictureBoxCollection.Image = obj.ShowBuses();
}
else
{
@ -62,16 +142,27 @@ namespace AccordionBus
/// <param name="e"></param>
private void buttonRemoveBus_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;
}
int pos = Convert.ToInt32(maskedTextBox.Text);
if (_buses - pos)
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _buses.ShowBuses();
pictureBoxCollection.Image = obj.ShowBuses();
}
else
{
@ -86,7 +177,18 @@ namespace AccordionBus
/// <param name="e"></param>
private void buttonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _buses.ShowBuses();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowBuses();
}
}
}

View File

@ -81,11 +81,11 @@ namespace AccordionBus.Generics
/// <returns></returns>
public static bool operator -(BusGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
return collect._collection.Remove(pos);
collect._collection.Remove(pos);
}
return false;
@ -98,7 +98,7 @@ namespace AccordionBus.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
@ -139,7 +139,7 @@ namespace AccordionBus.Generics
for (int i = 0; i < _collection.Count; i++)
{
// Получение объекта
var obj = _collection.Get(i);
var obj = _collection[i];
// Установка позиции
obj?.SetPosition(
(int)((width - 1) * _placeSizeWidth - (i % width * _placeSizeWidth)),

View File

@ -0,0 +1,92 @@
using AccordionBus.Drawings;
using AccordionBus.Generics;
using AccordionBus.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus
{
/// <summary>
/// Класс для хранения коллекции
/// </summary>
internal class BusGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, BusGenericCollection<DrawingBus, DrawingObjectBus>> _busStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _busStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth">Ширина картинки</param>
/// <param name="pictureHeight">Высота картинки</param>
public BusGenericStorage(int pictureWidth, int pictureHeight)
{
_busStorages = new Dictionary<string, BusGenericCollection<DrawingBus, DrawingObjectBus>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
_busStorages.Add(name, new BusGenericCollection<DrawingBus, DrawingObjectBus>(_pictureWidth, _pictureHeight));
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
// Проверка наличия ключа
if (!_busStorages.ContainsKey(name))
{
return;
}
_busStorages.Remove(name);
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public BusGenericCollection<DrawingBus, DrawingObjectBus>? this[string ind]
{
get
{
// Проверка наличия ключа
if (_busStorages.ContainsKey(ind))
{
return _busStorages[ind];
}
return null;
}
}
}
}

View File

@ -13,14 +13,19 @@ namespace AccordionBus.Generics
internal class SetGeneric<T> where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// Список объектов, которые храним
/// </summary>
private readonly T?[] _places;
private readonly List<T?> _places;
/// <summary>
/// Количество объектов в массиве
/// Количество объектов в списке
/// </summary>
public int Count => _places.Length;
public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
/// <summary>
/// Конструктор
@ -28,7 +33,8 @@ namespace AccordionBus.Generics
/// <param name="count"></param>
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(count);
}
/// <summary>
@ -50,42 +56,13 @@ namespace AccordionBus.Generics
public int Insert(T bus, int position)
{
// Проверка позиции
if (position < 0 || position >= Count)
if (position < 0 || position >= _maxCount)
{
return -1;
}
// Проверка, что элемент массива по этой позиции пустой
if (_places[position] != null)
{
// Проверка, что после вставляемого элемента в массиве есть пустой элемент
int index = -1;
for (int i = position + 1; i < Count; i++)
{
if (_places[i] == null)
{
index = i;
break;
}
}
// Проверка, если пустого элемента нет
if (index == -1)
{
return -1;
}
// Сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
int j = index - 1;
while (j >= position)
{
_places[j + 1] = _places[j];
j--;
}
}
// Вставка по позиции
_places[position] = bus;
_places.Insert(position, bus);
return position;
}
@ -97,13 +74,13 @@ namespace AccordionBus.Generics
public bool Remove(int position)
{
// Проверка позиции
if (position < 0 || position >= Count)
if (position < 0 || position >= _maxCount)
{
return false;
}
// Удаление объекта из массива, присвоив элементу массива значение null
_places[position] = null;
// Удаление объекта из списка
_places.RemoveAt(position);
return true;
}
@ -112,15 +89,44 @@ namespace AccordionBus.Generics
/// </summary>
/// <param name="position">Позиция</param>
/// <returns></returns>
public T? Get(int position)
public T? this[int position]
{
// Проверка позиции
if (position < 0 || position >= Count)
get
{
return null;
// Проверка позиции
if (position < 0 || position >= _maxCount)
{
return null;
}
return _places[position];
}
set
{
// Проверка позиции
if (position < 0 || position >= _maxCount)
{
return;
}
return _places[position];
_places.Insert(position, value);
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <param name="maxBuses"></param>
/// <returns></returns>
public IEnumerable<T?> GetBuses(int? maxBuses = null)
{
for (int i = 0; i < _maxCount; i++)
{
yield return _places[i];
if (maxBuses.HasValue && i == maxBuses.Value)
{
yield break;
}
}
}
}
}