Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Кашин Александр #19

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions ConsoleClient/ConsoleClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using CommandLine;
using ConsoleClient.Settings;
using TagCloud;
using TagCloud.Client;

namespace ConsoleClient;

public class ConsoleClient(IApp app) : IClient
{
public void Run()
{
Parser.Default.ParseArguments<ConsoleSettings>(Environment.GetCommandLineArgs())
.WithParsed(settings => app.Run(settings.GetAppSettings(), settings.GetImageSettings()))
.WithNotParsed(_ => throw new ArgumentException("Invalid command line arguments"));
}
}
19 changes: 19 additions & 0 deletions ConsoleClient/ConsoleClient.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="8.2.0"/>
<PackageReference Include="CommandLineParser" Version="2.9.1"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TagCloud\TagCloud.csproj"/>
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions ConsoleClient/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Autofac;
using TagCloud;
using TagCloud.Client;

namespace ConsoleClient;

class Program
{
static void Main()
{
var builder = new ContainerBuilder();
builder.RegisterModule(new TagCloudModule());
builder.RegisterType<ConsoleClient>().As<IClient>();
var container = builder.Build();
var app = container.Resolve<IClient>();

app.Run();
}
}
53 changes: 53 additions & 0 deletions ConsoleClient/Settings/ConsoleSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using CommandLine;
using TagCloud.Settings;

namespace ConsoleClient.Settings;

public class ConsoleSettings
{
[Option('w', "wight", Default = 800, HelpText = "Ширина изображения.")]
public int Width { get; set; }

[Option('h', "height", Default = 800, HelpText = "Высота изображения.")]
public int Height { get; set; }

[Option('b', "background", Default = "white", HelpText = "Цвет фона.")]
public string BackgroundColor { get; set; } = string.Empty;

[Option('f', "font", Default = "arial", HelpText = "Шрифт.")]
public string FontFamily { get; set; } = string.Empty;

[Option('p', "path", Default = "words.txt", HelpText = "Путь до источника слов.")]
public string SourcePath { get; set; } = string.Empty;

[Option('o', "output", Default = "output.png", HelpText = "Путь сохранения результата.")]
public string SavePath { get; set; } = string.Empty;

[Option("boringWords", HelpText = "Путь до списка скучный слов.")]
public string BoringWordsPath { get; set; } = string.Empty;

[Option("minFont", Default = 15, HelpText = "Минимальный шрифт.")]
public int FontSizeMin { get; set; }

[Option("maxFont", Default = 40, HelpText = "Максимальный шрифт.")]
public int FontSizeMax { get; set; }

public AppSettings GetAppSettings() =>
new()
{
SourcePath = SourcePath,
SavePath = SavePath,
BoringWordsPath = BoringWordsPath
};

public ImageSettings GetImageSettings() =>
new()
{
Width = Width,
Height = Height,
BackgroundColor = BackgroundColor,
FontFamily = FontFamily,
FontSizeMax = FontSizeMax,
FontSizeMin = FontSizeMin
};
}
201 changes: 201 additions & 0 deletions GuiClient/Forms/MainForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
using Autofac;
using TagCloud.Settings;

namespace GuiClient.Forms;

public class MainForm : Form
{
private readonly ILifetimeScope _lifetimeScope;

private NumericUpDown? _widthInput;
private NumericUpDown? _heightInput;
private Button? _colorButton;
private TextBox? _sourceFilePathInput;
private TextBox? _boringWordsPathFileInput;
private Button? _browseSourceFileButton;
private Button? _browseBoringWordsPathFileButton;
private Button? _fontButton;
private NumericUpDown? _minFontSizeInput;
private NumericUpDown? _maxFontSizeInput;
private Button? _generateButton;
private Color _selectedColor = Color.White;
private Font _selectedFont = new("Arial", 10);

private const int LabelX = 20;
private const int InputX = 130;
private string _sourceFilePath = string.Empty;
private string _boringWordsFilePath = string.Empty;

public MainForm(ILifetimeScope lifetimeScope)
{
_lifetimeScope = lifetimeScope;
InitForm();
AddWidthHeightControl();
AddColorControl();
AddSourceFilePathSelector();
AddBoringWordsFilePathSelector();
AddFontSelector();
AddMinMaxFontSizeControls();
AddGenerateButton();
}

private void InitForm()
{
Text = "Tag Cloud";
Size = new Size(480, 420);
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
StartPosition = FormStartPosition.CenterScreen;
}

private void AddWidthHeightControl()
{
var widthLabel = new Label { Text = "Ширина:", Location = new Point(LabelX, 20), AutoSize = true };
_widthInput = new NumericUpDown
{ Location = new Point(InputX, 20), Minimum = 100, Maximum = 1920, Value = 400 };

var heightLabel = new Label { Text = "Высота:", Location = new Point(LabelX, 60), AutoSize = true };
_heightInput = new NumericUpDown
{ Location = new Point(InputX, 60), Minimum = 100, Maximum = 1080, Value = 300 };

Controls.Add(widthLabel);
Controls.Add(_widthInput);
Controls.Add(heightLabel);
Controls.Add(_heightInput);
}

private void AddColorControl()
{
var colorLabel = new Label { Text = "Цвет:", Location = new Point(LabelX, 100), AutoSize = true };
_colorButton = new Button { Text = "Выбрать цвет", Location = new Point(250, 100) };
var colorPicture = new PictureBox
{
Location = new Point(InputX, 100),
Width = 100,
Height = 20,
BackColor = _selectedColor
};

_colorButton.Click += (_, _) =>
{
using var colorDialog = new ColorDialog();
if (colorDialog.ShowDialog() == DialogResult.OK)
{
_selectedColor = colorDialog.Color;
colorPicture.BackColor = colorDialog.Color;
}
};

Controls.Add(colorLabel);
Controls.Add(_colorButton);
Controls.Add(colorPicture);
}

private void AddSourceFilePathSelector()
{
var sourceFilePathLabel = new Label { Text = "Источник:", Location = new Point(LabelX, 140), AutoSize = true };
_sourceFilePathInput = new TextBox { Location = new Point(InputX, 140), Width = 200 };
_browseSourceFileButton = new Button { Text = "Обзор...", Location = new Point(360, 140) };
_browseSourceFileButton.Click += (_, _) =>
{
using var openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Text Files (*.txt)|*.txt;";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
_sourceFilePathInput.Text = openFileDialog.FileName;
_sourceFilePath = openFileDialog.FileName;
}
};

Controls.Add(sourceFilePathLabel);
Controls.Add(_sourceFilePathInput);
Controls.Add(_browseSourceFileButton);
}

private void AddBoringWordsFilePathSelector()
{
var additionalFilePathLabel = new Label
{ Text = "Скучные слова:", Location = new Point(LabelX, 180), AutoSize = true };
_boringWordsPathFileInput = new TextBox { Location = new Point(InputX, 180), Width = 200 };
_browseBoringWordsPathFileButton = new Button { Text = "Обзор...", Location = new Point(360, 180) };
_browseBoringWordsPathFileButton.Click += (_, _) =>
{
using var openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Text Files (*.txt)|*.txt";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
_boringWordsPathFileInput.Text = openFileDialog.FileName;
_boringWordsFilePath = openFileDialog.FileName;
}
};
Controls.Add(additionalFilePathLabel);
Controls.Add(_boringWordsPathFileInput);
Controls.Add(_browseBoringWordsPathFileButton);
}

private void AddFontSelector()
{
var fontLabel = new Label { Text = "Шрифт:", Location = new Point(LabelX, 220), AutoSize = true };
_fontButton = new Button { Text = "Выбрать шрифт", Location = new Point(InputX, 220) };
_fontButton.Click += (_, _) =>
{
using var fontDialog = new FontDialog();
if (fontDialog.ShowDialog() == DialogResult.OK)
{
_selectedFont = fontDialog.Font;
}
};

Controls.Add(fontLabel);
Controls.Add(_fontButton);
}

private void AddMinMaxFontSizeControls()
{
var minFontSizeLabel = new Label { Text = "Мин. шрифт:", Location = new Point(LabelX, 260), AutoSize = true };
_minFontSizeInput = new NumericUpDown
{ Location = new Point(InputX, 260), Minimum = 8, Maximum = 72, Value = 8 };

var maxFontSizeLabel = new Label { Text = "Макс. шрифт:", Location = new Point(LabelX, 300), AutoSize = true };
_maxFontSizeInput = new NumericUpDown
{ Location = new Point(InputX, 300), Minimum = 8, Maximum = 72, Value = 24 };

Controls.Add(minFontSizeLabel);
Controls.Add(_minFontSizeInput);
Controls.Add(maxFontSizeLabel);
Controls.Add(_maxFontSizeInput);
}

private void AddGenerateButton()
{
_generateButton = new Button { Text = "Сгенерировать", Location = new Point(200, 340), AutoSize = true };
_generateButton.Click += GenerateButton_Click!;

Controls.Add(_generateButton);
}

private void GenerateButton_Click(object sender, EventArgs e)
{
var imageSettings = new ImageSettings
{
Width = (int)_widthInput!.Value,
Height = (int)_heightInput!.Value,
BackgroundColor = _selectedColor.Name,
FontFamily = _selectedFont.FontFamily.Name,
FontSizeMax = (int)_minFontSizeInput!.Value,
FontSizeMin = (int)_maxFontSizeInput!.Value
};

var appSettings = new AppSettings
{
SourcePath = _sourceFilePath,
BoringWordsPath = _boringWordsFilePath,
SavePath = "output.png"
};

var resultForm = new ResultForm(this, _lifetimeScope, appSettings, imageSettings);
resultForm.Show();
Hide();
}
}
47 changes: 47 additions & 0 deletions GuiClient/Forms/ResultForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Autofac;
using TagCloud;
using TagCloud.Settings;

namespace GuiClient.Forms;

public class ResultForm : Form
{
private readonly ILifetimeScope _lifetimeScope;

public ResultForm(Form mainForm, ILifetimeScope lifetimeScope, AppSettings appSettings, ImageSettings imageSettings)
{
_lifetimeScope = lifetimeScope;

using var scope = _lifetimeScope.BeginLifetimeScope();

Text = "Результат";
Size = new Size(imageSettings.Width, imageSettings.Height + 70);
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
StartPosition = FormStartPosition.CenterScreen;

var regenerateButton = new Button { Text = "Сгенерировать заново", Dock = DockStyle.Bottom, };
regenerateButton.Click += (_, _) =>
{
mainForm.Show();
Close();
};

ShowCloudImage(appSettings, imageSettings);
Controls.Add(regenerateButton);
}

private void ShowCloudImage(AppSettings appSettings, ImageSettings imageSettings)
{
_lifetimeScope.Resolve<IApp>().Run(appSettings, imageSettings);
var picture = new PictureBox
{
SizeMode = PictureBoxSizeMode.AutoSize,
ImageLocation = appSettings.SavePath,
Dock = DockStyle.Top,
};
picture.Load();
Controls.Add(picture);
}
}
17 changes: 17 additions & 0 deletions GuiClient/GuiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Autofac;
using GuiClient.Forms;
using TagCloud.Client;

namespace GuiClient;

public class GuiClient(ILifetimeScope lifetimeScope) : IClient
{
public void Run()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

var mainForm = lifetimeScope.Resolve<MainForm>();
Application.Run(mainForm);
}
}
Loading