PIbd-22. Stroev V.M. Lab Work 06 #8

Closed
StroevVladimir wants to merge 2 commits from Lab06 into Lab05
7 changed files with 301 additions and 62 deletions

View File

@ -0,0 +1,52 @@
using Monorail.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail.DrawningObjects
{
public static class ExtentionDrawningMonorail
{
public static DrawningMonorail? CreateDrawningMonorail(this string info,
char separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if(strs.Length == 3)
{
return new DrawningMonorail(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]),
Color.FromName(strs[2]), width, height);
}
if (strs.Length == 7)
{
return new DrawningSecondMonorail(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]),
Color.FromName(strs[2]),
Color.FromName(strs[3]),
Convert.ToBoolean(strs[4]),
Convert.ToBoolean(strs[5]),
width, height);
}
return null;
}
public static string GetDataForSave(this DrawningMonorail drawningMonorail,
char separatorForObject)
{
var monorail = drawningMonorail.EntityMonorail;
if (monorail == null)
{
return string.Empty;
}
var str =
$"{monorail.Speed}{separatorForObject}{monorail.Weight}{separatorForObject}{monorail.BodyColor.Name}";
if (monorail is not EntitySecondMonorail secondMonorail)
{
return str;
}
return
$"{str}{separatorForObject}{secondMonorail.AdditionalColor.Name}{separatorForObject}" +
$"{secondMonorail.Monorails}{separatorForObject}{secondMonorail.SecondCabin}{separatorForObject}";
}
}
}

View File

@ -41,16 +41,23 @@
buttonDelMonorail = new Button(); buttonDelMonorail = new Button();
buttonAddMonorail = new Button(); buttonAddMonorail = new Button();
label1 = new Label(); label1 = new Label();
menuStrip1 = new MenuStrip();
файлToolStripMenuItem = new ToolStripMenuItem();
сохранитьToolStripMenuItem = new ToolStripMenuItem();
загрузитьToolStripMenuItem = new ToolStripMenuItem();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
panel1.SuspendLayout(); panel1.SuspendLayout();
panel2.SuspendLayout(); panel2.SuspendLayout();
menuStrip1.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
// pictureBoxCollection // pictureBoxCollection
// //
pictureBoxCollection.Location = new Point(5, -1); pictureBoxCollection.Location = new Point(5, 31);
pictureBoxCollection.Name = "pictureBoxCollection"; pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(1017, 517); pictureBoxCollection.Size = new Size(1017, 485);
pictureBoxCollection.TabIndex = 5; pictureBoxCollection.TabIndex = 5;
pictureBoxCollection.TabStop = false; pictureBoxCollection.TabStop = false;
// //
@ -97,6 +104,7 @@
listBoxStorages.Name = "listBoxStorages"; listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(144, 104); listBoxStorages.Size = new Size(144, 104);
listBoxStorages.TabIndex = 9; listBoxStorages.TabIndex = 9;
listBoxStorages.Click += ListBoxObjects_SelectedIndexChanged;
// //
// textBoxStorageName // textBoxStorageName
// //
@ -176,6 +184,47 @@
label1.TabIndex = 0; label1.TabIndex = 0;
label1.Text = "Инструменты"; label1.Text = "Инструменты";
// //
// menuStrip1
//
menuStrip1.ImageScalingSize = new Size(20, 20);
menuStrip1.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
menuStrip1.Location = new Point(0, 0);
menuStrip1.Name = "menuStrip1";
menuStrip1.Size = new Size(1228, 28);
menuStrip1.TabIndex = 6;
menuStrip1.Text = "menuStrip1";
//
// файлToolStripMenuItem
//
файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { сохранитьToolStripMenuItem, загрузитьToolStripMenuItem });
файлToolStripMenuItem.Name = айлToolStripMenuItem";
файлToolStripMenuItem.Size = new Size(59, 24);
файлToolStripMenuItem.Text = "Файл";
//
// сохранитьToolStripMenuItem
//
сохранитьToolStripMenuItem.Name = "сохранитьToolStripMenuItem";
сохранитьToolStripMenuItem.Size = new Size(166, 26);
сохранитьToolStripMenuItem.Text = "Сохранить";
сохранитьToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// загрузитьToolStripMenuItem
//
загрузитьToolStripMenuItem.Name = агрузитьToolStripMenuItem";
загрузитьToolStripMenuItem.Size = new Size(166, 26);
загрузитьToolStripMenuItem.Text = "Загрузить";
загрузитьToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
// openFileDialog
//
openFileDialog.FileName = "openFileDialog";
openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
saveFileDialog.FileName = "saveFileDialog";
saveFileDialog.Filter = "txt file | *.txt";
//
// FormMonorailCollection // FormMonorailCollection
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
@ -184,6 +233,8 @@
ClientSize = new Size(1228, 518); ClientSize = new Size(1228, 518);
Controls.Add(pictureBoxCollection); Controls.Add(pictureBoxCollection);
Controls.Add(panel1); Controls.Add(panel1);
Controls.Add(menuStrip1);
MainMenuStrip = menuStrip1;
Name = "FormMonorailCollection"; Name = "FormMonorailCollection";
Text = "Набор Монорельсов"; Text = "Набор Монорельсов";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
@ -191,7 +242,10 @@
panel1.PerformLayout(); panel1.PerformLayout();
panel2.ResumeLayout(false); panel2.ResumeLayout(false);
panel2.PerformLayout(); panel2.PerformLayout();
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();
ResumeLayout(false); ResumeLayout(false);
PerformLayout();
} }
#endregion #endregion
@ -209,5 +263,11 @@
private ListBox listBoxStorages; private ListBox listBoxStorages;
private TextBox textBoxStorageName; private TextBox textBoxStorageName;
private Button ButtonAddObject; private Button ButtonAddObject;
private MenuStrip menuStrip1;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem сохранитьToolStripMenuItem;
private ToolStripMenuItem загрузитьToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
} }
} }

View File

@ -168,5 +168,38 @@ EventArgs e)
} }
pictureBoxCollection.Image = obj.ShowMonorails(); pictureBoxCollection.Image = obj.ShowMonorails();
} }
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storage.SaveData(saveFileDialog.FileName))
{
MessageBox.Show("Сохранение прошло успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Не сохранилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storage.LoadData(openFileDialog.FileName))
{
MessageBox.Show("Загрузка прошло успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Не загрузилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
ReloadObjects();
}
}
} }
} }

View File

@ -117,4 +117,13 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>330, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>176, 17</value>
</metadata>
</root> </root>

View File

@ -29,6 +29,10 @@
private void InitializeComponent() private void InitializeComponent()
{ {
groupBox1 = new GroupBox(); groupBox1 = new GroupBox();
ButtonOk = new Button();
ButtonCancel = new Button();
label_body_color = new Label();
label_additional_color = new Label();
labelModifiedObject = new Label(); labelModifiedObject = new Label();
labelSimpleObject = new Label(); labelSimpleObject = new Label();
GroupColor = new GroupBox(); GroupColor = new GroupBox();
@ -48,11 +52,7 @@
numericUpDownSpeed = new NumericUpDown(); numericUpDownSpeed = new NumericUpDown();
pictureBoxObject = new PictureBox(); pictureBoxObject = new PictureBox();
label4 = new Label(); label4 = new Label();
ButtonOk = new Button();
ButtonCancel = new Button();
PanelObject = new Panel(); PanelObject = new Panel();
label_body_color = new Label();
label_additional_color = new Label();
groupBox1.SuspendLayout(); groupBox1.SuspendLayout();
GroupColor.SuspendLayout(); GroupColor.SuspendLayout();
((System.ComponentModel.ISupportInitialize)numericUpDownWeight).BeginInit(); ((System.ComponentModel.ISupportInitialize)numericUpDownWeight).BeginInit();
@ -84,6 +84,59 @@
groupBox1.TabStop = false; groupBox1.TabStop = false;
groupBox1.Text = "Параметры"; groupBox1.Text = "Параметры";
// //
// ButtonOk
//
ButtonOk.BackColor = SystemColors.Info;
ButtonOk.ForeColor = SystemColors.ControlText;
ButtonOk.Location = new Point(313, 480);
ButtonOk.Name = "ButtonOk";
ButtonOk.Size = new Size(101, 46);
ButtonOk.TabIndex = 12;
ButtonOk.Text = "Добавить";
ButtonOk.UseVisualStyleBackColor = false;
ButtonOk.Click += ButtonOk_Click;
//
// ButtonCancel
//
ButtonCancel.BackColor = SystemColors.Info;
ButtonCancel.ForeColor = SystemColors.ControlText;
ButtonCancel.Location = new Point(6, 480);
ButtonCancel.Name = "ButtonCancel";
ButtonCancel.Size = new Size(106, 46);
ButtonCancel.TabIndex = 13;
ButtonCancel.Text = "Отмена";
ButtonCancel.UseVisualStyleBackColor = false;
//
// label_body_color
//
label_body_color.AllowDrop = true;
label_body_color.BackColor = SystemColors.Info;
label_body_color.BorderStyle = BorderStyle.FixedSingle;
label_body_color.ForeColor = SystemColors.ControlText;
label_body_color.Location = new Point(6, 382);
label_body_color.Name = "label_body_color";
label_body_color.Size = new Size(106, 56);
label_body_color.TabIndex = 10;
label_body_color.Text = "Осн. цвет";
label_body_color.TextAlign = ContentAlignment.MiddleCenter;
label_body_color.DragDrop += PanelColor_DragDrop;
label_body_color.DragEnter += PanelColor_DragEnter;
//
// label_additional_color
//
label_additional_color.AllowDrop = true;
label_additional_color.BackColor = SystemColors.Info;
label_additional_color.BorderStyle = BorderStyle.FixedSingle;
label_additional_color.ForeColor = SystemColors.ControlText;
label_additional_color.Location = new Point(250, 382);
label_additional_color.Name = "label_additional_color";
label_additional_color.Size = new Size(101, 56);
label_additional_color.TabIndex = 10;
label_additional_color.Text = "Доп. цвет";
label_additional_color.TextAlign = ContentAlignment.MiddleCenter;
label_additional_color.DragDrop += PanelColor_DragDrop;
label_additional_color.DragEnter += PanelColor_DragEnter;
//
// labelModifiedObject // labelModifiedObject
// //
labelModifiedObject.AllowDrop = true; labelModifiedObject.AllowDrop = true;
@ -159,7 +212,7 @@
// //
// panelBlue // panelBlue
// //
panelBlue.BackColor = Color.FromArgb(0, 0, 192); panelBlue.BackColor = Color.Blue;
panelBlue.Location = new Point(174, 32); panelBlue.Location = new Point(174, 32);
panelBlue.Name = "panelBlue"; panelBlue.Name = "panelBlue";
panelBlue.Size = new Size(78, 55); panelBlue.Size = new Size(78, 55);
@ -195,7 +248,7 @@
// //
// panelRed // panelRed
// //
panelRed.BackColor = Color.FromArgb(192, 0, 0); panelRed.BackColor = Color.Red;
panelRed.Location = new Point(6, 32); panelRed.Location = new Point(6, 32);
panelRed.Name = "panelRed"; panelRed.Name = "panelRed";
panelRed.Size = new Size(78, 55); panelRed.Size = new Size(78, 55);
@ -282,29 +335,6 @@
label4.Size = new Size(0, 20); label4.Size = new Size(0, 20);
label4.TabIndex = 2; label4.TabIndex = 2;
// //
// ButtonOk
//
ButtonOk.BackColor = SystemColors.Info;
ButtonOk.ForeColor = SystemColors.ControlText;
ButtonOk.Location = new Point(313, 480);
ButtonOk.Name = "ButtonOk";
ButtonOk.Size = new Size(101, 46);
ButtonOk.TabIndex = 12;
ButtonOk.Text = "Добавить";
ButtonOk.UseVisualStyleBackColor = false;
ButtonOk.Click += ButtonOk_Click;
//
// ButtonCancel
//
ButtonCancel.BackColor = SystemColors.Info;
ButtonCancel.ForeColor = SystemColors.ControlText;
ButtonCancel.Location = new Point(6, 480);
ButtonCancel.Name = "ButtonCancel";
ButtonCancel.Size = new Size(106, 46);
ButtonCancel.TabIndex = 13;
ButtonCancel.Text = "Отмена";
ButtonCancel.UseVisualStyleBackColor = false;
//
// PanelObject // PanelObject
// //
PanelObject.AllowDrop = true; PanelObject.AllowDrop = true;
@ -316,36 +346,6 @@
PanelObject.DragDrop += PanelObject_DragDrop; PanelObject.DragDrop += PanelObject_DragDrop;
PanelObject.DragEnter += PanelObject_DragEnter; PanelObject.DragEnter += PanelObject_DragEnter;
// //
// label_body_color
//
label_body_color.AllowDrop = true;
label_body_color.BackColor = SystemColors.Info;
label_body_color.BorderStyle = BorderStyle.FixedSingle;
label_body_color.ForeColor = SystemColors.ControlText;
label_body_color.Location = new Point(6, 382);
label_body_color.Name = "label_body_color";
label_body_color.Size = new Size(106, 56);
label_body_color.TabIndex = 10;
label_body_color.Text = "Осн. цвет";
label_body_color.TextAlign = ContentAlignment.MiddleCenter;
label_body_color.DragDrop += PanelColor_DragDrop;
label_body_color.DragEnter += PanelColor_DragEnter;
//
// label_additional_color
//
label_additional_color.AllowDrop = true;
label_additional_color.BackColor = SystemColors.Info;
label_additional_color.BorderStyle = BorderStyle.FixedSingle;
label_additional_color.ForeColor = SystemColors.ControlText;
label_additional_color.Location = new Point(250, 382);
label_additional_color.Name = "label_additional_color";
label_additional_color.Size = new Size(101, 56);
label_additional_color.TabIndex = 10;
label_additional_color.Text = "Доп. цвет";
label_additional_color.TextAlign = ContentAlignment.MiddleCenter;
label_additional_color.DragDrop += PanelColor_DragDrop;
label_additional_color.DragEnter += PanelColor_DragEnter;
//
// FormMonorailConfig // FormMonorailConfig
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);

View File

@ -12,6 +12,7 @@ namespace Monorail.Generics
where T: DrawningMonorail where T: DrawningMonorail
where U : IMoveableObject where U : IMoveableObject
{ {
public IEnumerable<T?> GetMonorails => _collection.GetMonorails();
private readonly int _pictureWidth; private readonly int _pictureWidth;
private readonly int _pictureHeight; private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 295; private readonly int _placeSizeWidth = 295;

View File

@ -10,6 +10,10 @@ namespace Monorail.Generics
{ {
internal class MonorailsGenericStorage internal class MonorailsGenericStorage
{ {
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
readonly Dictionary<string, MonorailsGenericCollection<DrawningMonorail, readonly Dictionary<string, MonorailsGenericCollection<DrawningMonorail,
DrawningObjectMonorail>> _monorailsStorages; DrawningObjectMonorail>> _monorailsStorages;
public List<string> Keys => _monorailsStorages.Keys.ToList(); public List<string> Keys => _monorailsStorages.Keys.ToList();
@ -48,5 +52,85 @@ namespace Monorail.Generics
return _monorailsStorages[ind]; return _monorailsStorages[ind];
} }
} }
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,MonorailsGenericCollection<DrawningMonorail, DrawningObjectMonorail>>
record in _monorailsStorages)
{
StringBuilder records = new();
foreach (DrawningMonorail? elem in record.Value.GetMonorails)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
using StreamWriter sw = new(filename);
sw.Write($"MonorailStorage{Environment.NewLine}{data}");
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader sr = File.OpenText(filename))
{
string str = sr.ReadLine();
if (str == null || str.Length == 0)
{
return false;
}
if (!str.StartsWith("MonorailStorage"))
{
return false;
}
_monorailsStorages.Clear();
string strs = "";
while ((strs = sr.ReadLine()) != null)
{
if (strs == null)
{
return false;
}
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
continue;
}
MonorailsGenericCollection<DrawningMonorail, DrawningObjectMonorail> collection =
new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawningMonorail? monorail = elem?.CreateDrawningMonorail(_separatorForObject,
_pictureWidth, _pictureHeight);
if (monorail != null)
{
if ((collection + monorail) == -1)
{
return false;
}
}
}
_monorailsStorages.Add(record[0], collection);
}
return true;
}
}
} }
} }