From 454452c84b6a4bfac8761100878d650d55db9b98 Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Fri, 1 Dec 2023 00:26:39 +0400 Subject: [PATCH] Lab done --- .../AccountAppPlugins.csproj | 10 + .../AccountAppPlugins/FormMain.Designer.cs | 155 ++++++++++++- AbazovApp/AccountAppPlugins/FormMain.cs | 218 +++++++++++++++++- AbazovApp/AccountAppPlugins/FormMain.resx | 65 +----- AbazovApp/AccountsApp/FormMain.cs | 22 +- AbazovApp/AccountsApp/FormMain.resx | 2 +- AbazovApp/AccountsApp/PluginsConvention.cs | 20 +- .../BindingModels/AccountBindingModel.cs | 5 - 8 files changed, 413 insertions(+), 84 deletions(-) diff --git a/AbazovApp/AccountAppPlugins/AccountAppPlugins.csproj b/AbazovApp/AccountAppPlugins/AccountAppPlugins.csproj index b57c89e..177d96d 100644 --- a/AbazovApp/AccountAppPlugins/AccountAppPlugins.csproj +++ b/AbazovApp/AccountAppPlugins/AccountAppPlugins.csproj @@ -8,4 +8,14 @@ enable + + + + + + + + + + \ No newline at end of file diff --git a/AbazovApp/AccountAppPlugins/FormMain.Designer.cs b/AbazovApp/AccountAppPlugins/FormMain.Designer.cs index af0909e..ade69db 100644 --- a/AbazovApp/AccountAppPlugins/FormMain.Designer.cs +++ b/AbazovApp/AccountAppPlugins/FormMain.Designer.cs @@ -1,14 +1,17 @@ -namespace AccountAppPlugins +using static System.Net.Mime.MediaTypeNames; +using System.Windows.Forms; + +namespace AccountAppPlugins { partial class FormMain { /// - /// Required designer variable. + /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// - /// Clean up any resources being used. + /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) @@ -23,17 +26,151 @@ #region Windows Form Designer generated code /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "FormMain"; + menuStrip = new MenuStrip(); + ControlsStripMenuItem = new ToolStripMenuItem(); + ActionsToolStripMenuItem = new ToolStripMenuItem(); + ThesaurusToolStripMenuItem = new ToolStripMenuItem(); + AddElementToolStripMenuItem = new ToolStripMenuItem(); + UpdElementToolStripMenuItem = new ToolStripMenuItem(); + DelElementToolStripMenuItem = new ToolStripMenuItem(); + DocsToolStripMenuItem = new ToolStripMenuItem(); + SimpleDocToolStripMenuItem = new ToolStripMenuItem(); + TableDocToolStripMenuItem = new ToolStripMenuItem(); + ChartDocToolStripMenuItem = new ToolStripMenuItem(); + panelControl = new Panel(); + menuStrip.SuspendLayout(); + SuspendLayout(); + // + // menuStrip + // + menuStrip.Items.AddRange(new ToolStripItem[] { ControlsStripMenuItem, ActionsToolStripMenuItem, DocsToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(800, 24); + menuStrip.TabIndex = 0; + menuStrip.Text = "Меню"; + // + // ControlsStripMenuItem + // + ControlsStripMenuItem.Name = "ControlsStripMenuItem"; + ControlsStripMenuItem.Size = new Size(90, 20); + ControlsStripMenuItem.Text = "Компоненты"; + // + // ActionsToolStripMenuItem + // + ActionsToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { ThesaurusToolStripMenuItem, AddElementToolStripMenuItem, UpdElementToolStripMenuItem, DelElementToolStripMenuItem }); + ActionsToolStripMenuItem.Name = "ActionsToolStripMenuItem"; + ActionsToolStripMenuItem.Size = new Size(70, 20); + ActionsToolStripMenuItem.Text = "Действия"; + // + // ThesaurusToolStripMenuItem + // + ThesaurusToolStripMenuItem.Name = "ThesaurusToolStripMenuItem"; + ThesaurusToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.I; + ThesaurusToolStripMenuItem.Size = new Size(180, 22); + ThesaurusToolStripMenuItem.Text = "Справочник"; + ThesaurusToolStripMenuItem.Click += ThesaurusToolStripMenuItem_Click; + // + // AddElementToolStripMenuItem + // + AddElementToolStripMenuItem.Name = "AddElementToolStripMenuItem"; + AddElementToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.A; + AddElementToolStripMenuItem.Size = new Size(180, 22); + AddElementToolStripMenuItem.Text = "Добавить"; + AddElementToolStripMenuItem.Click += AddElementToolStripMenuItem_Click; + // + // UpdElementToolStripMenuItem + // + UpdElementToolStripMenuItem.Name = "UpdElementToolStripMenuItem"; + UpdElementToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.U; + UpdElementToolStripMenuItem.Size = new Size(180, 22); + UpdElementToolStripMenuItem.Text = "Изменить"; + UpdElementToolStripMenuItem.Click += UpdElementToolStripMenuItem_Click; + // + // DelElementToolStripMenuItem + // + DelElementToolStripMenuItem.Name = "DelElementToolStripMenuItem"; + DelElementToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.D; + DelElementToolStripMenuItem.Size = new Size(180, 22); + DelElementToolStripMenuItem.Text = "Удалить"; + DelElementToolStripMenuItem.Click += DelElementToolStripMenuItem_Click; + // + // DocsToolStripMenuItem + // + DocsToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SimpleDocToolStripMenuItem, TableDocToolStripMenuItem, ChartDocToolStripMenuItem }); + DocsToolStripMenuItem.Name = "DocsToolStripMenuItem"; + DocsToolStripMenuItem.Size = new Size(82, 20); + DocsToolStripMenuItem.Text = "Документы"; + // + // SimpleDocToolStripMenuItem + // + SimpleDocToolStripMenuItem.Name = "SimpleDocToolStripMenuItem"; + SimpleDocToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S; + SimpleDocToolStripMenuItem.Size = new Size(233, 22); + SimpleDocToolStripMenuItem.Text = "Простой документ"; + SimpleDocToolStripMenuItem.Click += SimpleDocToolStripMenuItem_Click; + // + // TableDocToolStripMenuItem + // + TableDocToolStripMenuItem.Name = "TableDocToolStripMenuItem"; + TableDocToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.T; + TableDocToolStripMenuItem.Size = new Size(233, 22); + TableDocToolStripMenuItem.Text = "Документ с таблицой"; + TableDocToolStripMenuItem.Click += TableDocToolStripMenuItem_Click; + // + // ChartDocToolStripMenuItem + // + ChartDocToolStripMenuItem.Name = "ChartDocToolStripMenuItem"; + ChartDocToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.C; + ChartDocToolStripMenuItem.Size = new Size(233, 22); + ChartDocToolStripMenuItem.Text = "Документ с диаграммой"; + ChartDocToolStripMenuItem.Click += ChartDocToolStripMenuItem_Click; + // + // panelControl + // + panelControl.Dock = DockStyle.Fill; + panelControl.Location = new Point(0, 24); + panelControl.Name = "panelControl"; + panelControl.Size = new Size(800, 426); + panelControl.TabIndex = 1; + // + // FormMain + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(panelControl); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; + Name = "FormMain"; + StartPosition = FormStartPosition.CenterScreen; + Text = "Главная форма"; + WindowState = FormWindowState.Maximized; + KeyDown += FormMain_KeyDown; + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); + ResumeLayout(false); + PerformLayout(); } #endregion + + private System.Windows.Forms.MenuStrip menuStrip; + private System.Windows.Forms.ToolStripMenuItem ControlsStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem DocsToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem SimpleDocToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem TableDocToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem ChartDocToolStripMenuItem; + private System.Windows.Forms.Panel panelControl; + private System.Windows.Forms.ToolStripMenuItem ActionsToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem ThesaurusToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem AddElementToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem UpdElementToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem DelElementToolStripMenuItem; } } \ No newline at end of file diff --git a/AbazovApp/AccountAppPlugins/FormMain.cs b/AbazovApp/AccountAppPlugins/FormMain.cs index 7c60377..2f23297 100644 --- a/AbazovApp/AccountAppPlugins/FormMain.cs +++ b/AbazovApp/AccountAppPlugins/FormMain.cs @@ -1,9 +1,11 @@ -using System; +using PluginsConventionLibrary; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; @@ -12,9 +14,223 @@ namespace AccountAppPlugins { public partial class FormMain : Form { + private readonly Dictionary _plugins; + private string _selectedPlugin; + public FormMain() { InitializeComponent(); + _plugins = new(); + LoadPlugins(); + _selectedPlugin = string.Empty; } + + private void LoadPlugins() + { + List pluginsList = GetPlugins(); + + foreach (var plugin in pluginsList) + { + _plugins[plugin.PluginName] = plugin; + CreateMenuItem(plugin.PluginName); + } + } + + private List GetPlugins() + { + string currentDir = Environment.CurrentDirectory; + string pluginsDir = Directory.GetParent(currentDir).Parent.Parent.Parent.FullName + "\\Plugins"; + string[] dllFiles = Directory.GetFiles( + pluginsDir, + "*.dll", + SearchOption.AllDirectories + ); + List plugins = new(); + foreach (string dllFile in dllFiles) + { + try + { + Assembly assembly = Assembly.LoadFrom(dllFile); + Type[] types = assembly.GetTypes(); + foreach (Type type in types) + { + if (typeof(IPluginsConvention).IsAssignableFrom(type) && !type.IsInterface) + { + if (Activator.CreateInstance(type) is IPluginsConvention plugin) + { + plugins.Add(plugin); + } + } + } + } + catch (Exception ex) + { + MessageBox.Show( + ex.Message + ); + } + } + return plugins; + } + + private void CreateMenuItem(string pluginName) + { + ToolStripMenuItem menuItem = new(pluginName); + menuItem.Click += (object? sender, EventArgs e) => + { + UserControl userControl = _plugins[pluginName].GetControl; + if (userControl != null) + { + panelControl.Controls.Clear(); + userControl.Dock = DockStyle.Fill; + _plugins[pluginName].ReloadData(); + _selectedPlugin = pluginName; + panelControl.Controls.Add(userControl); + } + }; + ControlsStripMenuItem.DropDownItems.Add(menuItem); + } + + private void FormMain_KeyDown(object sender, KeyEventArgs e) + { + if (string.IsNullOrEmpty(_selectedPlugin) || + !_plugins.ContainsKey(_selectedPlugin)) + { + return; + } + if (!e.Control) + { + return; + } + switch (e.KeyCode) + { + case Keys.I: + ShowThesaurus(); + break; + case Keys.A: + AddNewElement(); + break; + case Keys.U: + UpdateElement(); + break; + case Keys.D: + DeleteElement(); + break; + case Keys.S: + CreateSimpleDoc(); + break; + case Keys.T: + CreateTableDoc(); + break; + case Keys.C: + CreateChartDoc(); + break; + } + } + + private void ShowThesaurus() + { + _plugins[_selectedPlugin].GetThesaurus()?.Show(); + } + + private void AddNewElement() + { + var form = _plugins[_selectedPlugin].GetForm(null); + if (form != null && form.ShowDialog() == DialogResult.OK) + { + _plugins[_selectedPlugin].ReloadData(); + } + } + + private void UpdateElement() + { + var element = _plugins[_selectedPlugin].GetElement; + if (element == null) + { + MessageBox.Show( + "Не выбран элемент для обновления", + "Ошибка", + MessageBoxButtons.OK, + MessageBoxIcon.Error + ); + return; + } + var form = _plugins[_selectedPlugin].GetForm(element); + if (form != null && form.ShowDialog() == DialogResult.OK) + { + _plugins[_selectedPlugin].ReloadData(); + } + } + + private void DeleteElement() + { + if (MessageBox.Show( + "Удалить выбранный элемент?", + "Удаление", + MessageBoxButtons.YesNo, + MessageBoxIcon.Question) != DialogResult.Yes) + { + return; + } + var element = _plugins[_selectedPlugin].GetElement; + if (element == null) + { + MessageBox.Show( + "Не выбран элемент для удаления", + "Ошибка", + MessageBoxButtons.OK, + MessageBoxIcon.Error + ); + return; + } + if (_plugins[_selectedPlugin].DeleteElement(element)) + { + _plugins[_selectedPlugin].ReloadData(); + } + } + + private void CreateSimpleDoc() + { + SaveFileDialog saveFileDialog = new() + { + Filter = "Excel Files|*.xlsx" + }; + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + _plugins[_selectedPlugin].CreateSimpleDocument(new PluginsConventionSaveDocument() { FileName = saveFileDialog.FileName }); + + } + } + private void CreateTableDoc() + { + SaveFileDialog saveFileDialog = new() + { + Filter = "Word Files|*.docx" + }; + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + _plugins[_selectedPlugin].CreateTableDocument(new PluginsConventionSaveDocument() { FileName = saveFileDialog.FileName }); + + } + } + private void CreateChartDoc() + { + SaveFileDialog saveFileDialog = new() + { + Filter = "PDF Files|*.pdf" + }; + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + _plugins[_selectedPlugin].CreateChartDocument(new PluginsConventionSaveDocument() { FileName = saveFileDialog.FileName }); + } + } + + private void ThesaurusToolStripMenuItem_Click(object sender, EventArgs e) => ShowThesaurus(); + private void AddElementToolStripMenuItem_Click(object sender, EventArgs e) => AddNewElement(); + private void UpdElementToolStripMenuItem_Click(object sender, EventArgs e) => UpdateElement(); + private void DelElementToolStripMenuItem_Click(object sender, EventArgs e) => DeleteElement(); + private void SimpleDocToolStripMenuItem_Click(object sender, EventArgs e) => CreateSimpleDoc(); + private void TableDocToolStripMenuItem_Click(object sender, EventArgs e) => CreateTableDoc(); + private void ChartDocToolStripMenuItem_Click(object sender, EventArgs e) => CreateChartDoc(); } } diff --git a/AbazovApp/AccountAppPlugins/FormMain.resx b/AbazovApp/AccountAppPlugins/FormMain.resx index 1af7de1..81a9e3d 100644 --- a/AbazovApp/AccountAppPlugins/FormMain.resx +++ b/AbazovApp/AccountAppPlugins/FormMain.resx @@ -1,64 +1,4 @@ - - - + @@ -117,4 +57,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + \ No newline at end of file diff --git a/AbazovApp/AccountsApp/FormMain.cs b/AbazovApp/AccountsApp/FormMain.cs index 19cbb4e..9560987 100644 --- a/AbazovApp/AccountsApp/FormMain.cs +++ b/AbazovApp/AccountsApp/FormMain.cs @@ -96,18 +96,28 @@ namespace AccountsApp private void документToolStripMenuItem_Click(object sender, EventArgs e) { + SaveFileDialog saveFileDialog = new() + { + Filter = "Excel Files|*.xlsx" + }; + if (saveFileDialog.ShowDialog() != DialogResult.OK) return; List avatars = new List(); foreach (var account in _logic.ReadList(null)) { avatars.Add(account.Avatar); } - string path = AppDomain.CurrentDomain.BaseDirectory + "Аватары.xlsx"; + string path = saveFileDialog.FileName; if (excelImagesComponent.createWithImages(new ExcelImageInfo(path, "Аватары", avatars.ToArray()))) MessageBox.Show("Документ создан"); } private void документСДиаграммойToolStripMenuItem_Click(object sender, EventArgs e) { - string path = AppDomain.CurrentDomain.BaseDirectory + "Интересы.pdf"; + SaveFileDialog saveFileDialog = new() + { + Filter = "PDF Files|*.pdf" + }; + if (saveFileDialog.ShowDialog() != DialogResult.OK) return; + string path = saveFileDialog.FileName; Dictionary> data = new Dictionary>(); data = _logic @@ -129,7 +139,12 @@ namespace AccountsApp private void документСТаблицейToolStripMenuItem_Click(object sender, EventArgs e) { - string path = AppDomain.CurrentDomain.BaseDirectory + "Аккаунты.docx"; + SaveFileDialog saveFileDialog = new() + { + Filter = "Word Files|*.docx" + }; + if (saveFileDialog.ShowDialog() != DialogResult.OK) return; + string path = saveFileDialog.FileName; List<(int, int)> merges = new List<(int, int)> { (1, 2) }; List widths = new List { 100, 100, 100, 100 }; List<(string, string)> headers = new List<(string, string)> { @@ -141,6 +156,7 @@ namespace AccountsApp }; wordTableComponentAccount.createWithTable(path, "Список аккаунтов", merges, widths, headers, _logic.ReadList(null)); + MessageBox.Show("Успех"); } } } diff --git a/AbazovApp/AccountsApp/FormMain.resx b/AbazovApp/AccountsApp/FormMain.resx index 3206035..62434ef 100644 --- a/AbazovApp/AccountsApp/FormMain.resx +++ b/AbazovApp/AccountsApp/FormMain.resx @@ -70,6 +70,6 @@ 639, 27 - 90 + 137 \ No newline at end of file diff --git a/AbazovApp/AccountsApp/PluginsConvention.cs b/AbazovApp/AccountsApp/PluginsConvention.cs index aa16346..3e96755 100644 --- a/AbazovApp/AccountsApp/PluginsConvention.cs +++ b/AbazovApp/AccountsApp/PluginsConvention.cs @@ -1,7 +1,9 @@ using AbazovViewComponents.LogicalComponents; +using AccountsBusinessLogic.BusinessLogics; using AccountsContracts.BusinessLogicContracts; using AccountsContracts.StorageContracts; using AccountsContracts.ViewModels; +using AccountsDataBaseImplement.Implements; using ComponentsLibraryNet60.DocumentWithChart; using ComponentsLibraryNet60.Models; using ControlsLibraryNet60.Core; @@ -32,6 +34,16 @@ namespace AccountsApp get { return _controlDataTable; } } + public PluginsConvention() + { + _accountLogic = new AccountLogic(new AccountStorage()); + _interestLogic = new InterestLogic(new InterestStorage()); + _excelImagesComponent = new(); + _wordTableComponent = new(); + _chartBar = new(); + _controlDataTable = new(); + } + public PluginsConventionElement GetElement { get @@ -66,7 +78,7 @@ namespace AccountsApp public bool DeleteElement(PluginsConventionElement element) { _accountLogic.Delete( - new(element.Id.GetHashCode()) + new AccountsContracts.BindingModels.AccountBindingModel { Id = element.Id.GetHashCode()} ); return true; } @@ -108,7 +120,7 @@ namespace AccountsApp { avatars.Add(account.Avatar); } - string path = AppDomain.CurrentDomain.BaseDirectory + "Аватары.xlsx"; + string path = saveDocument.FileName; if (_excelImagesComponent.createWithImages(new ExcelImageInfo(path, "Аватары", avatars.ToArray()))) { MessageBox.Show("Документ создан"); @@ -119,7 +131,7 @@ namespace AccountsApp public bool CreateTableDocument(PluginsConventionSaveDocument saveDocument) { - string path = AppDomain.CurrentDomain.BaseDirectory + "Аккаунты.docx"; + string path = saveDocument.FileName; List<(int, int)> merges = new List<(int, int)> { (1, 2) }; List widths = new List { 100, 100, 100, 100 }; List<(string, string)> headers = new List<(string, string)> { @@ -139,7 +151,7 @@ namespace AccountsApp { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); - string path = AppDomain.CurrentDomain.BaseDirectory + "Интересы.pdf"; + string path = saveDocument.FileName; Dictionary> data = new Dictionary>(); data = _accountLogic diff --git a/AbazovApp/AccountsContracts/BindingModels/AccountBindingModel.cs b/AbazovApp/AccountsContracts/BindingModels/AccountBindingModel.cs index d05fb83..3d904ea 100644 --- a/AbazovApp/AccountsContracts/BindingModels/AccountBindingModel.cs +++ b/AbazovApp/AccountsContracts/BindingModels/AccountBindingModel.cs @@ -20,10 +20,5 @@ namespace AccountsContracts.BindingModels public int InterestId { get; set; } public string Avatar { get; set; } = string.Empty; - - public AccountBindingModel(int id) - { - Id = id; - } } }