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 2 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(App app, ISettingsManager settingsManager) : IClient
{
public void Run()
{
Parser.Default.ParseArguments<ConsoleSettings>(Environment.GetCommandLineArgs())
.WithParsed(settingsManager.Set);
app.Run();
}
}
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>
21 changes: 21 additions & 0 deletions ConsoleClient/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Autofac;
using ConsoleClient.Settings;
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>();
builder.RegisterType<ConsoleSettingsManager>().As<ISettingsManager>();
var container = builder.Build();
var app = container.Resolve<IClient>();

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

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; }
}
29 changes: 29 additions & 0 deletions ConsoleClient/Settings/ConsoleSettingsManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using TagCloud.Settings;

namespace ConsoleClient.Settings;

public class ConsoleSettingsManager(
IImageSettingsProvider imageSettingsProvider,
IAppSettingsProvider appSettingsProvider)
: ISettingsManager
{
public void Set(ConsoleSettings settings)
{
imageSettingsProvider.ImageSettings = new ImageSettings
{
Width = settings.Width,
Height = settings.Height,
BackgroundColor = settings.BackgroundColor,
FontFamily = settings.FontFamily,
FontSizeMax = settings.FontSizeMax,
FontSizeMin = settings.FontSizeMin,
};

appSettingsProvider.AppSettings = new AppSettings
{
SourcePath = settings.SourcePath,
SavePath = settings.SavePath,
BoringWordsPath = settings.BoringWordsPath
};
}
}
6 changes: 6 additions & 0 deletions ConsoleClient/Settings/ISettingsManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ConsoleClient.Settings;

public interface ISettingsManager
{
void Set(ConsoleSettings settings);
}
202 changes: 202 additions & 0 deletions GuiClient/Forms/MainForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
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)
{
_lifetimeScope.Resolve<IImageSettingsProvider>()
.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
};
_lifetimeScope.Resolve<IAppSettingsProvider>()
.AppSettings = new AppSettings
{
SourcePath = _sourceFilePath,
BoringWordsPath = _boringWordsFilePath,
SavePath = "output.png"
};

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

namespace GuiClient.Forms;

public class ResultForm : Form
{
private readonly ILifetimeScope _lifetimeScope;

public ResultForm(ILifetimeScope lifetimeScope, Form mainForm)
{
_lifetimeScope = lifetimeScope;

using var scope = _lifetimeScope.BeginLifetimeScope();
var imageSettingsProvider = scope.Resolve<IImageSettingsProvider>();

Text = "Результат";
Size = new Size(imageSettingsProvider.ImageSettings.Width, imageSettingsProvider.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();
Controls.Add(regenerateButton);
}

private void ShowCloudImage()
{
_lifetimeScope.Resolve<App>().Run();
var appSettingsProvider = _lifetimeScope.Resolve<IAppSettingsProvider>();
var picture = new PictureBox
{
SizeMode = PictureBoxSizeMode.AutoSize,
ImageLocation = appSettingsProvider.AppSettings.SavePath,
Dock = DockStyle.Top,
};
picture.Load();
Controls.Add(picture);
}
}
Loading