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

Овечкин Илья #193

Open
wants to merge 10 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
26 changes: 26 additions & 0 deletions TagsCloudContainer/Actions/AlgorithmSettingsAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using TagsCloudContainer.Infrastucture.Settings;
using TagsCloudContainer.Infrastucture.UiActions;

namespace TagsCloudContainer.Actions
{
public class AlgorithmSettingsAction : IUiAction
{
private AlgorithmSettings algorithmSettings;

public AlgorithmSettingsAction(AlgorithmSettings settings)
{
this.algorithmSettings = settings;
}

public string Category => "Настроить";

public string Name => "Алгоритм";

public string Description => "Изменить настройки алгоритма";

public void Perform()
{
SettingsForm.For(algorithmSettings).ShowDialog();
}
}
}
30 changes: 30 additions & 0 deletions TagsCloudContainer/Actions/DrawTagCloudAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using TagsCloudContainer.Client;
using TagsCloudContainer.Infrastucture.Settings;
using TagsCloudContainer.Infrastucture.UiActions;

namespace TagsCloudContainer.Actions
{
public class DrawTagCloudAction : IUiAction
{
private FileSettings fileSettings;
private ITagCloudClient tagCloudClient;

public DrawTagCloudAction(FileSettings fileSettings, ITagCloudClient tagCloudClient)
{
this.tagCloudClient = tagCloudClient;
this.fileSettings = fileSettings;
}

public string Category => "Изображение";

public string Name => "Отрисовать изображение";

public string Description => "Отрисовать изображение облака тегов";

public void Perform()
{
tagCloudClient.DrawImage(fileSettings.SourceFilePath,
fileSettings.BoringFilePath);
}
}
}
30 changes: 30 additions & 0 deletions TagsCloudContainer/Actions/ImageSettingsAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using TagsCloudContainer.Infrastucture.Extensions;
using TagsCloudContainer.Infrastucture.Settings;
using TagsCloudContainer.Infrastucture.UiActions;

namespace TagsCloudContainer.Actions
{
public class ImageSettingsAction : IUiAction
{
private ImageSettings imageSettings;
private PictureBox pictureBox;

public ImageSettingsAction(ImageSettings settings, PictureBox pictureBox)
{
this.imageSettings = settings;
this.pictureBox = pictureBox;
}

public string Category => "Настроить";

public string Name => "Изображение";

public string Description => "Изменить настройки изображения";

public void Perform()
{
SettingsForm.For(imageSettings).ShowDialog();
pictureBox.RecreateImage(imageSettings);
}
}
}
40 changes: 40 additions & 0 deletions TagsCloudContainer/Actions/SaveImageAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using TagsCloudContainer.Client;
using TagsCloudContainer.Infrastucture.Settings;
using TagsCloudContainer.Infrastucture.UiActions;

namespace TagsCloudContainer.Actions
{
public class SaveImageAction : IUiAction
{
private FileSettings fileSettings;
private ITagCloudClient tagCloudClient;

public SaveImageAction(FileSettings settings, ITagCloudClient tagCloudClient)
{
this.fileSettings = settings;
this.tagCloudClient = tagCloudClient;
}

public string Category => "Изображение";

public string Name => "Сохранить";

public string Description => "Сохранить изображение";

public void Perform()
{
var dialog = new SaveFileDialog
{
CheckFileExists = false,
InitialDirectory = Path.GetFullPath(fileSettings.ImagePath),
DefaultExt = "png",
FileName = "image.png",
Filter = "Изображения (*.png)|*.png|Изображения (*.jpg)|*.jpg|Изображения (*.bmp)|*.bmp"
};
var res = dialog.ShowDialog();

if (res == DialogResult.OK)
tagCloudClient.SaveImage(dialog.FileName);
}
}
}
40 changes: 40 additions & 0 deletions TagsCloudContainer/Actions/SelectBoringWordsFileAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using TagsCloudContainer.Infrastucture.Settings;
using TagsCloudContainer.Infrastucture.UiActions;

namespace TagsCloudContainer.Actions
{
public class SelectBoringWordsFileAction : IUiAction
{
private FileSettings fileSetting;

public SelectBoringWordsFileAction(FileSettings settings)
{
this.fileSetting = settings;
}

public string Category => "Файлы";

public string Name => "Файл со скучными словами";

public string Description => "Выбрать файл со списком скучных слов";

public void Perform()
{
var filePath = fileSetting.BoringFilePath;
var dialog = new OpenFileDialog()
{
CheckFileExists = true,
InitialDirectory = Path.GetFullPath(filePath),
DefaultExt = "txt",
FileName = "boring.txt",
Filter = "Текстовые файлы (*.txt)|*.txt"
};
var res = dialog.ShowDialog();

if (res == DialogResult.OK)
filePath = dialog.FileName;

fileSetting.BoringFilePath = filePath;
}
}
}
40 changes: 40 additions & 0 deletions TagsCloudContainer/Actions/SelectSourceFileAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using TagsCloudContainer.Infrastucture.Settings;
using TagsCloudContainer.Infrastucture.UiActions;

namespace TagsCloudContainer.Actions
{
public class SelectSourceFileAction : IUiAction
{
private FileSettings fileSettings;

public SelectSourceFileAction(FileSettings settings)
{
this.fileSettings = settings;
}

public string Category => "Файлы";

public string Name => "Файл cо словами";

public string Description => "Выбрать файл со словами для облака тегов";

public void Perform()
{
var filePath = fileSettings.SourceFilePath;
var dialog = new OpenFileDialog()
{
CheckFileExists = true,
InitialDirectory = Path.GetFullPath(filePath),
DefaultExt = "txt",
FileName = "source.txt",
Filter = "Текстовые файлы (*.txt)|*.txt"
};
var res = dialog.ShowDialog();

if (res == DialogResult.OK)
filePath = dialog.FileName;

fileSettings.SourceFilePath = filePath;
}
}
}
41 changes: 41 additions & 0 deletions TagsCloudContainer/Algorithm/CircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using TagsCloudContainer.Infrastucture;
using TagsCloudContainer.Infrastucture.Settings;

namespace TagsCloudContainer.Algorithm
{
public class CircularCloudLayouter : ICloudLayouter
{
private readonly ImageSettings imageSettings;
private readonly IRectanglePlacer rectanglePlacer;


public CircularCloudLayouter(ImageSettings imageSettings, IRectanglePlacer rectanglePlacer)
{
this.imageSettings = imageSettings;
this.rectanglePlacer = rectanglePlacer;
}

public List<TextRectangle> GetRectangles(Dictionary<string, int> wordFrequencies)
{
var rectangles = new List<TextRectangle>();
var bitmap = new Bitmap(imageSettings.Width, imageSettings.Height);
var graphics = Graphics.FromImage(bitmap);

foreach (var word in wordFrequencies.Keys)
{
var fontSize = CalculateFontSize(wordFrequencies, word, imageSettings.Font.Size);
var font = new Font(imageSettings.Font.FontFamily, fontSize, imageSettings.Font.Style, imageSettings.Font.Unit);
var textSize = graphics.MeasureString(word, font);
var rectangle = rectanglePlacer.GetPossibleNextRectangle(rectangles, textSize);
rectangles.Add(new TextRectangle(rectangle, word, font));
}

return rectangles;
}

private float CalculateFontSize(Dictionary<string, int> wordFrequencies, string word, float fontSize)

This comment was marked as resolved.

{
return fontSize + (wordFrequencies[word] - wordFrequencies.Values.Min()) * 20 / (wordFrequencies.Values.Max());
}
}
}
24 changes: 24 additions & 0 deletions TagsCloudContainer/Algorithm/FileParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace TagsCloudContainer.Algorithm
{
public class FileParser : IFileParser
{
public List<string> ReadWordsInFile(string filePath)
{
var words = new List<string>();
var lines = File.ReadAllLines(filePath);

foreach (var line in lines)
{
var lineWords = line.ToLower().Trim().Split(' ');
if (lineWords.Length > 1)
{
throw new Exception("There is more than one word in a line");
}
words.Add(lineWords[0]);
}

return words;
}

}
}
9 changes: 9 additions & 0 deletions TagsCloudContainer/Algorithm/ICloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using TagsCloudContainer.Infrastucture;

namespace TagsCloudContainer.Algorithm
{
public interface ICloudLayouter
{
List<TextRectangle> GetRectangles(Dictionary<string, int> wordFrequencies);
}
}
7 changes: 7 additions & 0 deletions TagsCloudContainer/Algorithm/IFileParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TagsCloudContainer.Algorithm
{
public interface IFileParser
{
List<string> ReadWordsInFile(string filePath);
}
}
9 changes: 9 additions & 0 deletions TagsCloudContainer/Algorithm/IRectanglePlacer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using TagsCloudContainer.Infrastucture;

namespace TagsCloudContainer.Algorithm
{
public interface IRectanglePlacer
{
RectangleF GetPossibleNextRectangle(List<TextRectangle> cloudRectangles, SizeF rectangleSize);
}
}
7 changes: 7 additions & 0 deletions TagsCloudContainer/Algorithm/IWordProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TagsCloudContainer.Algorithm
{
public interface IWordProcessor
{
Dictionary<string, int> CalculateFrequencyInterestingWords(string sourceFilePath, string boringFilePath);
}
}
54 changes: 54 additions & 0 deletions TagsCloudContainer/Algorithm/RectanglePlacer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Diagnostics;
using TagsCloudContainer.Infrastucture;
using TagsCloudContainer.Infrastucture.Settings;

namespace TagsCloudContainer.Algorithm
{
public sealed class RectanglePlacer : IRectanglePlacer
{
private readonly AlgorithmSettings algorithmSettings;
private readonly ImageSettings imageSettings;

public RectanglePlacer(AlgorithmSettings algorithmSettings, ImageSettings imageSettings)
{
if (imageSettings.Width / 2 < 0 || imageSettings.Height / 2 < 0)
throw new ArgumentException("the coordinates of the center must be positive numbers");

this.algorithmSettings = algorithmSettings;
this.imageSettings = imageSettings;
}

public RectangleF GetPossibleNextRectangle(List<TextRectangle> cloudRectangles, SizeF rectangleSize)
{
if (rectangleSize.Width <= 0 || rectangleSize.Height <= 0)
throw new ArgumentException("the width and height of the rectangle must be positive numbers");

return FindPossibleNextRectangle(cloudRectangles, rectangleSize);
}

private RectangleF FindPossibleNextRectangle(List<TextRectangle> cloudRectangles, SizeF rectangleSize)
{
var radius = algorithmSettings.Radius;
var angle = algorithmSettings.Angle;
var center = new Point(imageSettings.Width / 2, imageSettings.Height / 2);

while (true)
{
var point = new Point(
(int)(center.X + radius * Math.Cos(angle)),
(int)(center.Y + radius * Math.Sin(angle))
);
var possibleRectangle = new RectangleF(point, rectangleSize);

if (!cloudRectangles.Any(textRectangle => textRectangle.Rectangle.IntersectsWith(possibleRectangle)))
{
return possibleRectangle;
}

angle += algorithmSettings.DeltaAngle;
radius += algorithmSettings.DeltaRadius;
}
}

}
}
Loading