From 12149fbe73dc2bd5429daa5b85132a5c3a3eaa20 Mon Sep 17 00:00:00 2001 From: daHil Date: Wed, 24 Jan 2024 04:20:38 +0500 Subject: [PATCH 01/34] added file reader --- TagsCloudPainter/FileReader/IFileReader.cs | 7 ++++ TagsCloudPainter/FileReader/TextFileReader.cs | 35 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 TagsCloudPainter/FileReader/IFileReader.cs create mode 100644 TagsCloudPainter/FileReader/TextFileReader.cs diff --git a/TagsCloudPainter/FileReader/IFileReader.cs b/TagsCloudPainter/FileReader/IFileReader.cs new file mode 100644 index 000000000..26270cac6 --- /dev/null +++ b/TagsCloudPainter/FileReader/IFileReader.cs @@ -0,0 +1,7 @@ +namespace TagsCloudPainter.FileReader +{ + public interface IFileReader + { + public string ReadFile(string path); + } +} diff --git a/TagsCloudPainter/FileReader/TextFileReader.cs b/TagsCloudPainter/FileReader/TextFileReader.cs new file mode 100644 index 000000000..78a63c7c0 --- /dev/null +++ b/TagsCloudPainter/FileReader/TextFileReader.cs @@ -0,0 +1,35 @@ +using Spire.Doc; + +namespace TagsCloudPainter.FileReader +{ + public class TextFileReader : IFileReader + { + public string ReadFile(string path) + { + if (!File.Exists(path)) + throw new FileNotFoundException(); + + return Path.GetExtension(path) switch + { + ".txt" => ReadTxtFile(path), + ".doc" => ReadDocFile(path), + ".docx" => ReadDocFile(path), + _ => throw new ArgumentException("Incorrect file extension. Supported file extensions: txt, doc, docx"), + }; + } + + private static string ReadTxtFile(string path) + { + return File.ReadAllText(path).Trim(); + } + + private static string ReadDocFile(string path) + { + var doc = new Document(); + doc.LoadFromFile(path); + var text = doc.GetText(); + var lastIndexOfSpirePart = text.IndexOf(Environment.NewLine); + return text.Substring(lastIndexOfSpirePart + 2).Trim(); + } + } +} From 83d1aa6566ce71896233cbbb84c6331483f8493a Mon Sep 17 00:00:00 2001 From: daHil Date: Wed, 24 Jan 2024 04:22:53 +0500 Subject: [PATCH 02/34] added text parser --- TagsCloudPainter/Parser/BoringTextParser.cs | 33 +++++++++++++++++++++ TagsCloudPainter/Parser/ITextParser.cs | 7 +++++ TagsCloudPainter/Settings/TextSettings.cs | 7 +++++ 3 files changed, 47 insertions(+) create mode 100644 TagsCloudPainter/Parser/BoringTextParser.cs create mode 100644 TagsCloudPainter/Parser/ITextParser.cs create mode 100644 TagsCloudPainter/Settings/TextSettings.cs diff --git a/TagsCloudPainter/Parser/BoringTextParser.cs b/TagsCloudPainter/Parser/BoringTextParser.cs new file mode 100644 index 000000000..494f967ce --- /dev/null +++ b/TagsCloudPainter/Parser/BoringTextParser.cs @@ -0,0 +1,33 @@ +using TagsCloudPainter.Settings; + +namespace TagsCloudPainter.Parser +{ + public class BoringTextParser: ITextParser + { + private static readonly string[] _separators = [" ", ". ", ", ", "; ", "-", "—", Environment.NewLine]; + private readonly TextSettings textSettings; + + public BoringTextParser(TextSettings textSettings) + { + this.textSettings = textSettings; + } + + public HashSet GetBoringWords(string text) + { + var words = text.Split(Environment.NewLine); + var boringWords = new HashSet(); + + foreach (var word in words) + boringWords.Add(word.ToLower()); + + return boringWords; + } + + public List ParseText(string text) + { + var boringWords = GetBoringWords(textSettings.BoringText); + var words = text.Split(_separators, StringSplitOptions.RemoveEmptyEntries); + return words.Select(word => word.ToLower()).Where(word => !boringWords.Contains(word)).ToList(); + } + } +} diff --git a/TagsCloudPainter/Parser/ITextParser.cs b/TagsCloudPainter/Parser/ITextParser.cs new file mode 100644 index 000000000..dde53765e --- /dev/null +++ b/TagsCloudPainter/Parser/ITextParser.cs @@ -0,0 +1,7 @@ +namespace TagsCloudPainter.Parser +{ + public interface ITextParser + { + List ParseText(string text); + } +} diff --git a/TagsCloudPainter/Settings/TextSettings.cs b/TagsCloudPainter/Settings/TextSettings.cs new file mode 100644 index 000000000..7eb1ef324 --- /dev/null +++ b/TagsCloudPainter/Settings/TextSettings.cs @@ -0,0 +1,7 @@ +namespace TagsCloudPainter.Settings +{ + public class TextSettings + { + public string BoringText { get; set; } + } +} From ec0dca0f453fc97cfca203f53e82f1fbaf3464ac Mon Sep 17 00:00:00 2001 From: daHil Date: Wed, 24 Jan 2024 04:23:57 +0500 Subject: [PATCH 03/34] added tag builder --- TagsCloudPainter/Settings/TagSettings.cs | 11 ++++++ TagsCloudPainter/Tags/ITagsBuilder.cs | 7 ++++ TagsCloudPainter/Tags/Tag.cs | 16 +++++++++ TagsCloudPainter/Tags/TagsBuilder.cs | 45 ++++++++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 TagsCloudPainter/Settings/TagSettings.cs create mode 100644 TagsCloudPainter/Tags/ITagsBuilder.cs create mode 100644 TagsCloudPainter/Tags/Tag.cs create mode 100644 TagsCloudPainter/Tags/TagsBuilder.cs diff --git a/TagsCloudPainter/Settings/TagSettings.cs b/TagsCloudPainter/Settings/TagSettings.cs new file mode 100644 index 000000000..4290f3d0d --- /dev/null +++ b/TagsCloudPainter/Settings/TagSettings.cs @@ -0,0 +1,11 @@ +using System.Drawing; + +namespace TagsCloudPainter.Settings +{ + public class TagSettings + { + public int TagFontSize { get; set; } + public string TagFontName { get; set; } = "Arial"; + public Color TagColor { get; set; } + } +} diff --git a/TagsCloudPainter/Tags/ITagsBuilder.cs b/TagsCloudPainter/Tags/ITagsBuilder.cs new file mode 100644 index 000000000..6eca9ed7d --- /dev/null +++ b/TagsCloudPainter/Tags/ITagsBuilder.cs @@ -0,0 +1,7 @@ +namespace TagsCloudPainter.Tags +{ + public interface ITagsBuilder + { + public List GetTags(List words); + } +} diff --git a/TagsCloudPainter/Tags/Tag.cs b/TagsCloudPainter/Tags/Tag.cs new file mode 100644 index 000000000..e7e2e19b6 --- /dev/null +++ b/TagsCloudPainter/Tags/Tag.cs @@ -0,0 +1,16 @@ +namespace TagsCloudPainter.Tags +{ + public class Tag + { + public string Value { get; private set; } + public float FontSize { get; private set; } + public int Count { get; private set; } + + public Tag(string value, float fontSize, int count) + { + Value = value; + FontSize = fontSize; + Count = count; + } + } +} diff --git a/TagsCloudPainter/Tags/TagsBuilder.cs b/TagsCloudPainter/Tags/TagsBuilder.cs new file mode 100644 index 000000000..f07decb03 --- /dev/null +++ b/TagsCloudPainter/Tags/TagsBuilder.cs @@ -0,0 +1,45 @@ +using TagsCloudPainter.Settings; + +namespace TagsCloudPainter.Tags +{ + public class TagsBuilder: ITagsBuilder + { + private readonly TagSettings _settings; + + public TagsBuilder(TagSettings settings) + { + _settings = settings; + } + + public List GetTags(List words) + { + var countedWords = CountWords(words); + var tags = new List(); + + foreach (var wordWithCount in countedWords) + { + var tagFontSize = GetTagFontSize(_settings.TagFontSize, wordWithCount.Value, countedWords.Count); + var tag = new Tag(wordWithCount.Key, tagFontSize, wordWithCount.Value); + tags.Add(tag); + } + + return tags; + } + + private static Dictionary CountWords(List words) + { + var countedWords = new Dictionary(); + + foreach (var word in words) + { + if (!countedWords.TryAdd(word, 1)) + countedWords[word] += 1; + } + + return countedWords; + } + + private static float GetTagFontSize(int fontSize, int tagCount, int wordsAmount) => + (float)tagCount / wordsAmount * fontSize * 100; + } +} From 29f1d22f90745724be68c86e818821d5e87109bb Mon Sep 17 00:00:00 2001 From: daHil Date: Wed, 24 Jan 2024 04:26:06 +0500 Subject: [PATCH 04/34] added form pointer --- .../FormPointer/ArchimedeanSpiralPointer.cs | 40 +++++++++++++++++++ TagsCloudPainter/FormPointer/IFormPointer.cs | 9 +++++ TagsCloudPainter/IResetable.cs | 7 ++++ 3 files changed, 56 insertions(+) create mode 100644 TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs create mode 100644 TagsCloudPainter/FormPointer/IFormPointer.cs create mode 100644 TagsCloudPainter/IResetable.cs diff --git a/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs b/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs new file mode 100644 index 000000000..4d26158c9 --- /dev/null +++ b/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs @@ -0,0 +1,40 @@ +using System.Drawing; +using TagsCloudPainter.Settings; + +namespace TagsCloudPainter.FormPointer +{ + public class ArchimedeanSpiralPointer : IFormPointer + { + private double сurrentDifference; + private readonly CloudSettings cloudSettings; + private readonly SpiralPointerSettings spiralPointerSettings; + + public ArchimedeanSpiralPointer(CloudSettings cloudSettings, SpiralPointerSettings spiralPointerSettings) + { + if (spiralPointerSettings.Step <= 0 + || spiralPointerSettings.RadiusConst <= 0 + || spiralPointerSettings.AngleConst <= 0) + throw new ArgumentException("either step or radius or angle is not possitive"); + this.cloudSettings = cloudSettings; + this.spiralPointerSettings = spiralPointerSettings; + сurrentDifference = 0; + } + + private double Angle => сurrentDifference * spiralPointerSettings.AngleConst; + private double Radius => сurrentDifference * spiralPointerSettings.RadiusConst; + + public Point GetNextPoint() + { + сurrentDifference += spiralPointerSettings.Step; + var x = cloudSettings.CloudCenter.X + (int)(Radius * Math.Cos(Angle)); + var y = cloudSettings.CloudCenter.Y + (int)(Radius * Math.Sin(Angle)); + + return new Point(x, y); + } + + public void Reset() + { + сurrentDifference = 0; + } + } +} diff --git a/TagsCloudPainter/FormPointer/IFormPointer.cs b/TagsCloudPainter/FormPointer/IFormPointer.cs new file mode 100644 index 000000000..5db7c0c17 --- /dev/null +++ b/TagsCloudPainter/FormPointer/IFormPointer.cs @@ -0,0 +1,9 @@ +using System.Drawing; + +namespace TagsCloudPainter.FormPointer +{ + public interface IFormPointer: IResetable + { + Point GetNextPoint(); + } +} diff --git a/TagsCloudPainter/IResetable.cs b/TagsCloudPainter/IResetable.cs new file mode 100644 index 000000000..b74027d4f --- /dev/null +++ b/TagsCloudPainter/IResetable.cs @@ -0,0 +1,7 @@ +namespace TagsCloudPainter +{ + public interface IResetable + { + void Reset(); + } +} From b15fdc191037b480bfd70ece3de3cabb63887901 Mon Sep 17 00:00:00 2001 From: daHil Date: Wed, 24 Jan 2024 04:27:47 +0500 Subject: [PATCH 05/34] added cloud layouter --- .../CloudLayouter/ICloudLayouter.cs | 13 ++++ .../CloudLayouter/TagsCloudLayouter.cs | 66 +++++++++++++++++++ TagsCloudPainter/Settings/CloudSettings.cs | 10 +++ .../Settings/SpiralPointerSettings.cs | 15 +++++ TagsCloudPainter/TagsCloud.cs | 31 +++++++++ TagsCloudPainter/Utils/Utils.cs | 33 ++++++++++ 6 files changed, 168 insertions(+) create mode 100644 TagsCloudPainter/CloudLayouter/ICloudLayouter.cs create mode 100644 TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs create mode 100644 TagsCloudPainter/Settings/CloudSettings.cs create mode 100644 TagsCloudPainter/Settings/SpiralPointerSettings.cs create mode 100644 TagsCloudPainter/TagsCloud.cs create mode 100644 TagsCloudPainter/Utils/Utils.cs diff --git a/TagsCloudPainter/CloudLayouter/ICloudLayouter.cs b/TagsCloudPainter/CloudLayouter/ICloudLayouter.cs new file mode 100644 index 000000000..91ca175db --- /dev/null +++ b/TagsCloudPainter/CloudLayouter/ICloudLayouter.cs @@ -0,0 +1,13 @@ +using System.Drawing; +using TagsCloudPainter.Tags; + +namespace TagsCloudPainter.CloudLayouter +{ + public interface ICloudLayouter: IResetable + { + Rectangle PutNextTag(Tag tag); + TagsCloud GetCloud(); + void PutTags(List tags); + void InitializeCloud(); + } +} diff --git a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs new file mode 100644 index 000000000..2aad270b5 --- /dev/null +++ b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs @@ -0,0 +1,66 @@ +using System.Drawing; +using TagsCloudPainter.FormPointer; +using TagsCloudPainter.Settings; +using TagsCloudPainter.Tags; + +namespace TagsCloudPainter.CloudLayouter +{ + public class TagsCloudLayouter : ICloudLayouter + { + private readonly IFormPointer formPointer; + private TagsCloud cloud; + private readonly TagSettings tagSettings; + private CloudSettings cloudSettings; + + public TagsCloudLayouter(CloudSettings cloudSettings, IFormPointer formPointer, TagSettings tagSettings) + { + this.cloudSettings = cloudSettings; + this.formPointer = formPointer; + this.tagSettings = tagSettings; + } + + public Rectangle PutNextTag(Tag tag) + { + FailIfCloudNotInitialized(); + + var rectangleSize = Utils.Utils.GetStringSize(tag.Value, tagSettings.TagFontName, tag.FontSize); + if (rectangleSize.Height <= 0 || rectangleSize.Width <= 0) + throw new ArgumentException("either width or height of rectangle size is not possitive"); + + var nextRectangle = Utils.Utils.GetRectangleFromCenter(formPointer.GetNextPoint(), rectangleSize); + while (cloud.Tags.Values.Any(rectangle => rectangle.IntersectsWith(nextRectangle))) + nextRectangle = Utils.Utils.GetRectangleFromCenter(formPointer.GetNextPoint(), rectangleSize); + + cloud.AddTag(tag, nextRectangle); + + return nextRectangle; + } + + public void PutTags(List tags) + { + FailIfCloudNotInitialized(); + + if (tags.Count == 0) + throw new ArgumentException("пустые размеры"); + foreach (var tag in tags) + PutNextTag(tag); + } + + public TagsCloud GetCloud() => + new TagsCloud(cloud.Center, cloud.Tags); + + public void InitializeCloud() + => cloud = new TagsCloud(cloudSettings.CloudCenter, []); + + public void FailIfCloudNotInitialized() + { + if (cloud is null) + throw new InvalidOperationException("Initialize cloud before other method call!"); + } + + public void Reset() + { + formPointer.Reset(); + } + } +} diff --git a/TagsCloudPainter/Settings/CloudSettings.cs b/TagsCloudPainter/Settings/CloudSettings.cs new file mode 100644 index 000000000..8474db36b --- /dev/null +++ b/TagsCloudPainter/Settings/CloudSettings.cs @@ -0,0 +1,10 @@ +using System.Drawing; + +namespace TagsCloudPainter.Settings +{ + public class CloudSettings + { + public Point CloudCenter { get; set; } + public Color BackgroundColor { get; set; } + } +} diff --git a/TagsCloudPainter/Settings/SpiralPointerSettings.cs b/TagsCloudPainter/Settings/SpiralPointerSettings.cs new file mode 100644 index 000000000..560cad2e1 --- /dev/null +++ b/TagsCloudPainter/Settings/SpiralPointerSettings.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TagsCloudPainter.Settings +{ + public class SpiralPointerSettings + { + public double Step { get; set; } + public double RadiusConst { get; set; } + public double AngleConst { get; set; } + } +} diff --git a/TagsCloudPainter/TagsCloud.cs b/TagsCloudPainter/TagsCloud.cs new file mode 100644 index 000000000..fab816fc6 --- /dev/null +++ b/TagsCloudPainter/TagsCloud.cs @@ -0,0 +1,31 @@ +using System.Drawing; +using TagsCloudPainter.Tags; + +namespace TagsCloudPainter; + +public class TagsCloud +{ + public Point Center { get; private set; } + public Dictionary Tags { get; } + + public TagsCloud(Point center, Dictionary tags) + { + Center = center; + Tags = tags ?? []; + } + + public void AddTag(Tag tag, Rectangle rectangle) + { + Tags.Add(tag, rectangle); + } + + public int GetWidth() + { + return Tags.Values.Max(rect => rect.X) - Tags.Values.Min(rect => rect.X); + } + + public int GetHeight() + { + return Tags.Values.Max(rect => rect.Y) - Tags.Values.Min(rect => rect.Y); + } +} \ No newline at end of file diff --git a/TagsCloudPainter/Utils/Utils.cs b/TagsCloudPainter/Utils/Utils.cs new file mode 100644 index 000000000..46aa3d925 --- /dev/null +++ b/TagsCloudPainter/Utils/Utils.cs @@ -0,0 +1,33 @@ +using System.Drawing; +using TagsCloudPainter.Settings; + +namespace TagsCloudPainter.Utils +{ + public static class Utils + { + + public static Rectangle GetRectangleFromCenter(Point center, Size size) + { + var x = center.X - size.Width / 2; + var y = center.Y - size.Height / 2; + + return new Rectangle(new Point(x, y), size); + } + + public static Point GetRectangleCenter(Rectangle rectangle) + { + var x = rectangle.X; + var y = rectangle.Y; + return new Point(x + rectangle.Width / 2, y + rectangle.Height / 2); + } + + public static Size GetStringSize(string value, string fontName, float fontSize) + { + using var graphics = Graphics.FromHwnd(IntPtr.Zero); + var font = new Font(fontName, fontSize); + var size = graphics.MeasureString(value, font).ToSize(); + + return size; + } + } +} From ee647198ed1e45fadaa70809edc9d49428944f64 Mon Sep 17 00:00:00 2001 From: daHil Date: Wed, 24 Jan 2024 04:28:30 +0500 Subject: [PATCH 06/34] added cloud drawer --- TagsCloudPainter/CloudDrawer/CloudDrawer.cs | 56 +++++++++++++++++++++ TagsCloudPainter/TagsCloudPainter.csproj | 15 ++++++ 2 files changed, 71 insertions(+) create mode 100644 TagsCloudPainter/CloudDrawer/CloudDrawer.cs create mode 100644 TagsCloudPainter/TagsCloudPainter.csproj diff --git a/TagsCloudPainter/CloudDrawer/CloudDrawer.cs b/TagsCloudPainter/CloudDrawer/CloudDrawer.cs new file mode 100644 index 000000000..c03dbb219 --- /dev/null +++ b/TagsCloudPainter/CloudDrawer/CloudDrawer.cs @@ -0,0 +1,56 @@ +using System.Drawing; +using System.Drawing.Drawing2D; +using TagsCloudPainter.Settings; + +namespace TagsCloudPainter.Drawer +{ + public class CloudDrawer + { + private readonly TagSettings tagSettings; + private readonly CloudSettings cloudSettings; + + public CloudDrawer(TagSettings tagSettings, CloudSettings cloudSettings) + { + this.tagSettings = tagSettings; + this.cloudSettings = cloudSettings; + } + + public Bitmap DrawCloud(TagsCloud cloud, int imageWidth, int imageHeight) + { + if (cloud.Tags.Count == 0) + throw new ArgumentException("rectangles are empty"); + if (imageWidth <= 0 || imageHeight <= 0) + throw new ArgumentException("either width or height of rectangle size is not positive"); + + var drawingScale = CalculateObjectDrawingScale(cloud.GetWidth(), cloud.GetHeight(), imageWidth, imageHeight); + var bitmap = new Bitmap(imageWidth, imageHeight); + var graphics = Graphics.FromImage(bitmap); + var pen = new Pen(tagSettings.TagColor); + + graphics.TranslateTransform(-cloud.Center.X, -cloud.Center.Y); + graphics.ScaleTransform(drawingScale, drawingScale, MatrixOrder.Append); + graphics.TranslateTransform(cloud.Center.X, cloud.Center.Y, MatrixOrder.Append); + graphics.Clear(cloudSettings.BackgroundColor); + foreach(var tag in cloud.Tags) + { + var font = new Font(tagSettings.TagFontName, tag.Key.FontSize); + graphics.DrawString(tag.Key.Value, font, pen.Brush, tag.Value.Location); + } + + return bitmap; + } + + public static float CalculateObjectDrawingScale(float width, float height, float imageWidth, float imageHeight) + { + var scale = 1f; + var scaleAccuracy = 0.05f; + var widthScale = scale; + var heightScale = scale; + if (width * scale > imageWidth) + widthScale = imageWidth / width - scaleAccuracy; + if (height * scale > imageHeight) + heightScale = imageHeight / height - scaleAccuracy; + return Math.Min(widthScale, heightScale); + } + } +} diff --git a/TagsCloudPainter/TagsCloudPainter.csproj b/TagsCloudPainter/TagsCloudPainter.csproj new file mode 100644 index 000000000..1044eebe9 --- /dev/null +++ b/TagsCloudPainter/TagsCloudPainter.csproj @@ -0,0 +1,15 @@ + + + + Library + net8.0 + enable + enable + + + + + + + + From b0950ba1d100a91852dc2ed0d34591798ff79d1f Mon Sep 17 00:00:00 2001 From: daHil Date: Wed, 24 Jan 2024 04:28:46 +0500 Subject: [PATCH 07/34] added tests --- Tests/ArchimedeanSpiralTests.cs | 25 ++++++++ Tests/BoringTextParserTests.cs | 62 ++++++++++++++++++++ Tests/CloudDrawerTests.cs | 61 +++++++++++++++++++ Tests/GlobalUsings.cs | 1 + Tests/TagsBuilderTests.cs | 46 +++++++++++++++ Tests/TagsCloudLayouterTests.cs | 91 +++++++++++++++++++++++++++++ Tests/TagsCloudPainterTests.csproj | 25 ++++++++ Tests/TextFileReaderTests.cs | 42 +++++++++++++ Tests/TextFiles/boringWords.txt | 3 + Tests/TextFiles/testFile.docx | Bin 0 -> 14023 bytes Tests/TextFiles/testFile.txt | 3 + 11 files changed, 359 insertions(+) create mode 100644 Tests/ArchimedeanSpiralTests.cs create mode 100644 Tests/BoringTextParserTests.cs create mode 100644 Tests/CloudDrawerTests.cs create mode 100644 Tests/GlobalUsings.cs create mode 100644 Tests/TagsBuilderTests.cs create mode 100644 Tests/TagsCloudLayouterTests.cs create mode 100644 Tests/TagsCloudPainterTests.csproj create mode 100644 Tests/TextFileReaderTests.cs create mode 100644 Tests/TextFiles/boringWords.txt create mode 100644 Tests/TextFiles/testFile.docx create mode 100644 Tests/TextFiles/testFile.txt diff --git a/Tests/ArchimedeanSpiralTests.cs b/Tests/ArchimedeanSpiralTests.cs new file mode 100644 index 000000000..ccde7956b --- /dev/null +++ b/Tests/ArchimedeanSpiralTests.cs @@ -0,0 +1,25 @@ +using System.Drawing; +using TagsCloudPainter.FormPointer; +using TagsCloudPainter.Settings; + +namespace TagsCloudPainterTests +{ + [TestFixture] + public class ArchimedeanSpiralTests + { + private static IEnumerable ConstructorArgumentException => new[] + { + new TestCaseData(new Point(1, 1), 0, 1, 1).SetName("WhenGivenNotPositiveStep"), + new TestCaseData(new Point(1, 1), 1, 0, 1).SetName("WhenGivenNotPositiveRadius"), + new TestCaseData(new Point(1, 1), 1, 1, 0).SetName("WhenGivenNotPositiveAngle") + }; + + [TestCaseSource(nameof(ConstructorArgumentException))] + public void Constructor_ShouldThrowArgumentException(Point center, double step, double radius, double angle) + { + var cloudSettings = new CloudSettings() { CloudCenter = center }; + var pointerSettings = new SpiralPointerSettings() { AngleConst = angle, RadiusConst = radius, Step = step }; + Assert.Throws(() => new ArchimedeanSpiralPointer(cloudSettings, pointerSettings)); + } + } +} diff --git a/Tests/BoringTextParserTests.cs b/Tests/BoringTextParserTests.cs new file mode 100644 index 000000000..d6349abcc --- /dev/null +++ b/Tests/BoringTextParserTests.cs @@ -0,0 +1,62 @@ +using FluentAssertions; +using TagsCloudPainter.FileReader; +using TagsCloudPainter.Parser; +using TagsCloudPainter.Settings; + +namespace TagsCloudPainterTests +{ + [TestFixture] + public class BoringTextParserTests + { + private TextSettings textSettings; + private BoringTextParser boringTextParser; + private TextFileReader textFileReader; + + [SetUp] + public void Setup() + { + textFileReader = new TextFileReader(); + var boringText = textFileReader + .ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\boringWords.txt"); + textSettings = new TextSettings() { BoringText = boringText}; + boringTextParser = new BoringTextParser(textSettings); + } + + [Test] + public void ParseText_ShouldReturnWordsListWithoutBoringWords() + { + var boringWords = boringTextParser + .GetBoringWords(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\boringWords.txt")) + .ToHashSet(); + var parsedText = boringTextParser + .ParseText(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt")); + var isBoringWordsInParsedText = parsedText.Where(boringWords.Contains).Any(); + isBoringWordsInParsedText.Should().BeFalse(); + } + + [Test] + public void ParseText_ShouldReturnNotEmptyWordsList_WhenPassedNotEmptyText() + { + var parsedText = boringTextParser + .ParseText(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt")); + parsedText.Count.Should().BeGreaterThan(0); + } + + [Test] + public void ParseText_ShouldReturnWordsInLowerCase() + { + var parsedText = boringTextParser + .ParseText(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt")); + var isAnyWordNotLowered = parsedText.Where(word => word.ToLower() != word).Any(); + isAnyWordNotLowered.Should().BeFalse(); + } + + [Test] + public void ParseText_ShouldReturnWordsListWithTheSameAmountAsInText() + { + var parsedText = boringTextParser + .ParseText(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt")); + parsedText.Count.Should().Be(20); + } + } +} diff --git a/Tests/CloudDrawerTests.cs b/Tests/CloudDrawerTests.cs new file mode 100644 index 000000000..17ccbf910 --- /dev/null +++ b/Tests/CloudDrawerTests.cs @@ -0,0 +1,61 @@ +using System.Drawing; +using TagsCloudPainter; +using TagsCloudPainter.Drawer; +using TagsCloudPainter.Settings; +using TagsCloudPainter.Tags; + +namespace TagsCloudPainterTests +{ + [TestFixture] + public class CloudDrawerTests + { + private CloudDrawer drawer; + + [SetUp] + public void Setup() + { + var cloudSettings = new CloudSettings() { CloudCenter = new Point(0, 0), BackgroundColor = Color.White }; + var tagSettings = new TagSettings() { TagFontSize = 32, TagColor = Color.Black }; + drawer = new CloudDrawer(tagSettings, cloudSettings); + } + + private static IEnumerable DrawArgumentException => new[] + { + new TestCaseData(new TagsCloud(new Point(0, 0), + new Dictionary{ { new Tag("a", 1, 1), new (1, 1, 1, 1) } }), 0, 1) + .SetName("WhenGivenNotPositiveImageWidth"), + new TestCaseData(new TagsCloud(new Point(0, 0), + new Dictionary{ { new Tag("a", 1, 1), new (1, 1, 1, 1) } }), 1, 0) + .SetName("WhenGivenNotPositiveImageHeight"), + new TestCaseData(new TagsCloud(new Point(0, 0), + new Dictionary{ {new Tag("a", 1, 1), new(1, 1, 1, 1) } }), 0, 0) + .SetName("WhenGivenNotPositiveImageHeightAndWidth"), + new TestCaseData(new TagsCloud(new Point(0, 0), new Dictionary()), 1, 1) + .SetName("WhenGivenCloudWithEmptyTagsDictionary") + }; + + [TestCaseSource(nameof(DrawArgumentException))] + public void Draw_ShouldThrowArgumentException(TagsCloud cloud, int width, int height) + { + Assert.Throws(() => drawer.DrawCloud(cloud, width, height)); + } + private static IEnumerable DrawNoException => new[] + { + new TestCaseData(new TagsCloud(new Point(5, 5), + new Dictionary{ { new Tag("abcdadg", 10, 1), new(5, 5, 20, 3) } }), 10, 10) + .SetName("WhenCloudWidthIsGreaterThanImageWidth"), + new TestCaseData(new TagsCloud(new Point(5, 5), + new Dictionary{ { new Tag("abcdadg", 10, 1), new(5, 5, 3, 20) } }), 10, 10) + .SetName("WhenCloudHeightIsGreaterThanImageHeight"), + new TestCaseData(new TagsCloud(new Point(5, 5), + new Dictionary{ { new Tag("abcdadg", 10, 1), new(5, 5, 20, 20) } }), 10, 10) + .SetName("WhenCloudIsBiggerThanImage") + }; + + [TestCaseSource(nameof(DrawNoException))] + public void Draw_ShouldNotThrow(TagsCloud cloud, int width, int height) + { + Assert.DoesNotThrow(() => drawer.DrawCloud(cloud, width, height)); + } + } +} diff --git a/Tests/GlobalUsings.cs b/Tests/GlobalUsings.cs new file mode 100644 index 000000000..cefced496 --- /dev/null +++ b/Tests/GlobalUsings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file diff --git a/Tests/TagsBuilderTests.cs b/Tests/TagsBuilderTests.cs new file mode 100644 index 000000000..8c7a94d33 --- /dev/null +++ b/Tests/TagsBuilderTests.cs @@ -0,0 +1,46 @@ +using FluentAssertions; +using TagsCloudPainter.Settings; +using TagsCloudPainter.Tags; + +namespace TagsCloudPainterTests +{ + [TestFixture] + public class TagsBuilderTests + { + private TagsBuilder tagsBuilder; + + [SetUp] + public void Setup() + { + var tagSettings = new TagSettings() { TagFontSize = 32 }; + tagsBuilder = new TagsBuilder(tagSettings); + } + + [Test] + public void GetTags_ShouldReturnTagsWithGivenWords() + { + var words = new List() { "tag"}; + var tags = tagsBuilder.GetTags(words); + + tags[0].Value.Should().Be("tag"); + } + + [Test] + public void GetTags_ShouldReturnTagsWithDifferentValues() + { + var words = new List() { "tag", "tag" }; + var tags = tagsBuilder.GetTags(words); + + tags.Count.Should().Be(1); + } + + [Test] + public void GetTags_ShouldReturnTagsWithCorrectCount() + { + var words = new List() { "tag", "tag" }; + var tags = tagsBuilder.GetTags(words); + + tags[0].Count.Should().Be(2); + } + } +} diff --git a/Tests/TagsCloudLayouterTests.cs b/Tests/TagsCloudLayouterTests.cs new file mode 100644 index 000000000..3393908f9 --- /dev/null +++ b/Tests/TagsCloudLayouterTests.cs @@ -0,0 +1,91 @@ +using TagsCloudPainter.FormPointer; +using TagsCloudPainter.Settings; +using System.Drawing; +using TagsCloudPainter.CloudLayouter; +using TagsCloudPainter.Tags; +using TagsCloudPainter.Utils; +using FluentAssertions; + +namespace TagsCloudPainterTests +{ + [TestFixture] + public class TagsCloudLayouterTests + { + private TagsCloudLayouter tagsCloudLayouter; + private TagSettings tagSettings; + + [SetUp] + public void Setup() + { + var cloudSettings = new CloudSettings() { CloudCenter = new Point(0, 0) }; + tagSettings = new TagSettings() { TagFontSize = 32 }; + var pointerSettings = new SpiralPointerSettings() { AngleConst = 1, RadiusConst = 0.5, Step = 0.1 }; + var formPointer = new ArchimedeanSpiralPointer(cloudSettings, pointerSettings); + tagsCloudLayouter = new TagsCloudLayouter(cloudSettings, formPointer, tagSettings); + tagsCloudLayouter.InitializeCloud(); + } + + private static IEnumerable PutNextTagArgumentException => new[] + { + new TestCaseData(new Tag("", 10, 1)).SetName("WhenGivenTagWithEmptyValue"), + new TestCaseData(new Tag("das", 0, 1)).SetName("WhenGivenTagWithFontSizeLessThanOne"), + }; + + [TestCaseSource(nameof(PutNextTagArgumentException))] + public void PutNextRectangle_ShouldThrowArgumentException(Tag tag) + { + Assert.Throws(() => tagsCloudLayouter.PutNextTag(tag)); + } + + [Test] + public void PutNextTag_ShouldReturnRectangleOfTheTagValueSize() + { + var tag = new Tag("ads", 10, 5); + var tagRectangle = Utils.GetStringSize(tag.Value, tagSettings.TagFontName, tag.FontSize); + + var resultRectangle = tagsCloudLayouter.PutNextTag(tag); + + resultRectangle.Size.Should().Be(tagRectangle); + } + + [Test] + public void PutNextTag_ShouldReturnRectangleThatDoesNotIntersectWithAlreadyPutOnes() + { + var firstTag = new Tag("ads", 10, 5); + var secondTag = new Tag("ads", 10, 5); + var firstPutRectangle = tagsCloudLayouter.PutNextTag(firstTag); + var secondPutRectangle = tagsCloudLayouter.PutNextTag(secondTag); + + var doesRectanglesIntersect = firstPutRectangle.IntersectsWith(secondPutRectangle); + + doesRectanglesIntersect.Should().BeFalse(); + } + + [Test] + public void PutNextRectangle_ShouldPutRectangleWithCenterInTheCloudCenter() + { + var center = tagsCloudLayouter.GetCloud().Center; + var tag = new Tag("ads", 10, 5); + var firstRectangle = tagsCloudLayouter.PutNextTag(tag); + var firstRectangleCenter = Utils.GetRectangleCenter(firstRectangle); + + firstRectangleCenter.Should().Be(center); + } + + [Test] + public void PutTags_ThrowsArgumentNullException_WhenGivenEmptyDictionary() + { + Assert.Throws(() => tagsCloudLayouter.PutTags([])); + } + + [Test] + public void GetCloud_ReturnsAsManyTagsAsWasPut() + { + tagsCloudLayouter.PutNextTag(new Tag("ads", 10, 5)); + tagsCloudLayouter.PutNextTag(new Tag("ads", 10, 5)); + var rectanglesAmount = tagsCloudLayouter.GetCloud().Tags.Count; + + rectanglesAmount.Should().Be(2); + } + } +} diff --git a/Tests/TagsCloudPainterTests.csproj b/Tests/TagsCloudPainterTests.csproj new file mode 100644 index 000000000..01282a616 --- /dev/null +++ b/Tests/TagsCloudPainterTests.csproj @@ -0,0 +1,25 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + diff --git a/Tests/TextFileReaderTests.cs b/Tests/TextFileReaderTests.cs new file mode 100644 index 000000000..8537c81b2 --- /dev/null +++ b/Tests/TextFileReaderTests.cs @@ -0,0 +1,42 @@ +using TagsCloudPainter.FileReader; + +namespace TagsCloudPainterTests +{ + [TestFixture] + public class TextFileReaderTests + { + private TextFileReader fileReader; + + [SetUp] + public void Setup() + { + fileReader = new TextFileReader(); + } + + private static IEnumerable ReadTextFiles => new[] + { + new TestCaseData(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt") + .Returns("Товарищи! постоянное информационно-пропагандистское обеспечение нашей " + + $"деятельности играет важную роль в формировании форм развития.{Environment.NewLine}{Environment.NewLine}" + + "Значимость этих проблем настолько очевидна, что укрепление и развитие.") + .SetName("WhenPassedTxtFile"), + new TestCaseData(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.docx") + .Returns("Товарищи! постоянное информационно-пропагандистское обеспечение нашей " + + $"деятельности играет важную роль в формировании форм развития.{Environment.NewLine}{Environment.NewLine}" + + "Значимость этих проблем настолько очевидна, что укрепление и развитие.") + .SetName("WhenPassedDocxFile"), + }; + + [TestCaseSource(nameof(ReadTextFiles))] + public string ReadFile_ShouldReturnFileText(string path) + { + return fileReader.ReadFile(path); + } + + [Test] + public void ReadFile_ThrowsFileNotFoundExceptio_WhenPassedNonexistentPath() + { + Assert.Throws(() => fileReader.ReadFile("")); + } + } +} diff --git a/Tests/TextFiles/boringWords.txt b/Tests/TextFiles/boringWords.txt new file mode 100644 index 000000000..d655ac14c --- /dev/null +++ b/Tests/TextFiles/boringWords.txt @@ -0,0 +1,3 @@ +что +и +в \ No newline at end of file diff --git a/Tests/TextFiles/testFile.docx b/Tests/TextFiles/testFile.docx new file mode 100644 index 0000000000000000000000000000000000000000..0d1ed791b66034e2d6b9ada49f56b0d8c3b8c2ce GIT binary patch literal 14023 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 literal 0 HcmV?d00001 diff --git a/Tests/TextFiles/testFile.txt b/Tests/TextFiles/testFile.txt new file mode 100644 index 000000000..c96293518 --- /dev/null +++ b/Tests/TextFiles/testFile.txt @@ -0,0 +1,3 @@ +Товарищи! постоянное информационно-пропагандистское обеспечение нашей деятельности играет важную роль в формировании форм развития. + +Значимость этих проблем настолько очевидна, что укрепление и развитие. \ No newline at end of file From ceb1207b509f2bfc852b0b3ac520e56a754047f4 Mon Sep 17 00:00:00 2001 From: daHil Date: Wed, 24 Jan 2024 04:29:09 +0500 Subject: [PATCH 08/34] added application --- .../Actions/DrawTagCloudAction.cs | 103 ++++++++ .../Actions/FileSourceSettingsAction.cs | 23 ++ .../Actions/IUiAction.cs | 10 + .../Actions/ImageSettingsAction.cs | 23 ++ .../Actions/PaletteSettingsAction.cs | 24 ++ .../Actions/SaveImageAction.cs | 33 +++ .../Actions/UiActionExtensions.cs | 34 +++ TagsCloudPainterApplication/App.config | 18 ++ .../Data/BoringWords.txt | 238 ++++++++++++++++++ .../Infrastructure/IImageHolder.cs | 13 + .../Infrastructure/Palette.cs | 9 + .../Settings/FilesSourceSettings.cs | 7 + .../Infrastructure/Settings/ImageSettings.cs | 8 + .../Infrastructure/Settings/SettingsForm.cs | 36 +++ .../Settings/TagsCloudSettings.cs | 61 +++++ .../MainForm.Designer.cs | 39 +++ TagsCloudPainterApplication/MainForm.cs | 21 ++ TagsCloudPainterApplication/MainForm.resx | 120 +++++++++ .../PictureBoxImageHolder.cs | 45 ++++ TagsCloudPainterApplication/Program.cs | 53 ++++ .../Properties/Resources.Designer.cs | 63 +++++ .../Properties/Resources.resx | 117 +++++++++ .../Properties/Settings.Designer.cs | 26 ++ .../Properties/Settings.settings | 7 + .../TagsCloudPainterApplication.csproj | 42 ++++ TagsCloudPainterApplication/packages.config | 11 + di.sln | 29 ++- 27 files changed, 1212 insertions(+), 1 deletion(-) create mode 100644 TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs create mode 100644 TagsCloudPainterApplication/Actions/FileSourceSettingsAction.cs create mode 100644 TagsCloudPainterApplication/Actions/IUiAction.cs create mode 100644 TagsCloudPainterApplication/Actions/ImageSettingsAction.cs create mode 100644 TagsCloudPainterApplication/Actions/PaletteSettingsAction.cs create mode 100644 TagsCloudPainterApplication/Actions/SaveImageAction.cs create mode 100644 TagsCloudPainterApplication/Actions/UiActionExtensions.cs create mode 100644 TagsCloudPainterApplication/App.config create mode 100644 TagsCloudPainterApplication/Data/BoringWords.txt create mode 100644 TagsCloudPainterApplication/Infrastructure/IImageHolder.cs create mode 100644 TagsCloudPainterApplication/Infrastructure/Palette.cs create mode 100644 TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs create mode 100644 TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs create mode 100644 TagsCloudPainterApplication/Infrastructure/Settings/SettingsForm.cs create mode 100644 TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs create mode 100644 TagsCloudPainterApplication/MainForm.Designer.cs create mode 100644 TagsCloudPainterApplication/MainForm.cs create mode 100644 TagsCloudPainterApplication/MainForm.resx create mode 100644 TagsCloudPainterApplication/PictureBoxImageHolder.cs create mode 100644 TagsCloudPainterApplication/Program.cs create mode 100644 TagsCloudPainterApplication/Properties/Resources.Designer.cs create mode 100644 TagsCloudPainterApplication/Properties/Resources.resx create mode 100644 TagsCloudPainterApplication/Properties/Settings.Designer.cs create mode 100644 TagsCloudPainterApplication/Properties/Settings.settings create mode 100644 TagsCloudPainterApplication/TagsCloudPainterApplication.csproj create mode 100644 TagsCloudPainterApplication/packages.config diff --git a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs new file mode 100644 index 000000000..71aa4646d --- /dev/null +++ b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs @@ -0,0 +1,103 @@ +using TagsCloudPainter; +using TagsCloudPainter.CloudLayouter; +using TagsCloudPainter.Drawer; +using TagsCloudPainter.FileReader; +using TagsCloudPainter.Parser; +using TagsCloudPainter.Tags; +using TagsCloudPainterApplication.Infrastructure; +using TagsCloudPainterApplication.Infrastructure.Settings; + + +namespace TagsCloudPainterApplication.Actions +{ + public class DrawTagCloudAction : IUiAction + { + private readonly CloudDrawer cloudDrawer; + private readonly ImageSettings imageSettings; + private readonly TagsCloudSettings tagsCloudSettings; + private readonly FilesSourceSettings filesSourceSettings; + private readonly Palette palette; + private readonly IImageHolder imageHolder; + private readonly ICloudLayouter cloudLayouter; + private readonly ITagsBuilder tagsBuilder; + private readonly ITextParser textParser; + private readonly IFileReader fileReader; + public DrawTagCloudAction( + ImageSettings imageSettings, + TagsCloudSettings tagsCloudSettings, + FilesSourceSettings filesSourceSettings, + IImageHolder imageHolder, + CloudDrawer cloudDrawer, + ICloudLayouter cloudLayouter, + ITagsBuilder tagsBuilder, + ITextParser textParser, + IFileReader fileReader, + Palette palette) + { + this.cloudDrawer = cloudDrawer; + this.cloudLayouter = cloudLayouter; + this.tagsBuilder = tagsBuilder; + this.textParser = textParser; + this.fileReader = fileReader; + this.imageSettings = imageSettings; + this.tagsCloudSettings = tagsCloudSettings; + this.imageHolder = imageHolder; + this.filesSourceSettings = filesSourceSettings; + this.palette = palette; + } + public string Category => "Облако тэгов"; + + public string Name => "Нарисовать"; + + public string Description => "Нарисовать облако тэгов"; + + public void Perform() + { + var wordsFilePath = GetFilePath(); + SettingsForm.For(tagsCloudSettings).ShowDialog(); + tagsCloudSettings.CloudSettings.BackgroundColor = palette.BackgroundColor; + tagsCloudSettings.TagSettings.TagColor = palette.PrimaryColor; + + var wordsText = fileReader.ReadFile(wordsFilePath); + tagsCloudSettings.TextSettings.BoringText = fileReader.ReadFile(filesSourceSettings.BoringTextFilePath); + var parsedWords = textParser.ParseText(wordsText); + var cloud = GetCloud(parsedWords); + DrawCloud(cloud); + } + + private static string GetFilePath() + { + OpenFileDialog fileDialog = new() + { + InitialDirectory = Environment.CurrentDirectory, + Filter = "Текстовый файл txt (*.txt)|*.txt|Текстовый файл doc (*.doc)|*.doc|Текстовый файл docx (*.docx)|*.docx", + FilterIndex = 0, + RestoreDirectory = true + }; + fileDialog.ShowDialog(); + return fileDialog.FileName; + } + + private TagsCloud GetCloud(List words) + { + var tags = tagsBuilder.GetTags(words); + cloudLayouter.Reset(); + cloudLayouter.InitializeCloud(); + cloudLayouter.PutTags(tags); + var cloud = cloudLayouter.GetCloud(); + return cloud; + } + + private void DrawCloud(TagsCloud cloud) + { + var bitmap = cloudDrawer.DrawCloud(cloud, imageSettings.Width, imageSettings.Height); + imageHolder.RecreateImage(imageSettings); + + using (var graphics = imageHolder.StartDrawing()) + { + graphics.DrawImage(bitmap, new Point(0, 0)); + } + imageHolder.UpdateUi(); + } + } +} diff --git a/TagsCloudPainterApplication/Actions/FileSourceSettingsAction.cs b/TagsCloudPainterApplication/Actions/FileSourceSettingsAction.cs new file mode 100644 index 000000000..b3448f953 --- /dev/null +++ b/TagsCloudPainterApplication/Actions/FileSourceSettingsAction.cs @@ -0,0 +1,23 @@ +using TagsCloudPainterApplication.Infrastructure.Settings; + +namespace TagsCloudPainterApplication.Actions +{ + public class FileSourceSettingsAction: IUiAction + { + private readonly FilesSourceSettings filesSourceSettings; + + public FileSourceSettingsAction(FilesSourceSettings filesSourceSettings) + { + this.filesSourceSettings = filesSourceSettings; + } + + public string Category => "Настройки"; + public string Name => "Ресурсы"; + public string Description => "Укажите ресурсы"; + + public void Perform() + { + SettingsForm.For(filesSourceSettings).ShowDialog(); + } + } +} diff --git a/TagsCloudPainterApplication/Actions/IUiAction.cs b/TagsCloudPainterApplication/Actions/IUiAction.cs new file mode 100644 index 000000000..3128eb94f --- /dev/null +++ b/TagsCloudPainterApplication/Actions/IUiAction.cs @@ -0,0 +1,10 @@ +namespace TagsCloudPainterApplication.Actions +{ + public interface IUiAction + { + string Category { get; } + string Name { get; } + string Description { get; } + void Perform(); + } +} diff --git a/TagsCloudPainterApplication/Actions/ImageSettingsAction.cs b/TagsCloudPainterApplication/Actions/ImageSettingsAction.cs new file mode 100644 index 000000000..d58822523 --- /dev/null +++ b/TagsCloudPainterApplication/Actions/ImageSettingsAction.cs @@ -0,0 +1,23 @@ +using TagsCloudPainterApplication.Infrastructure.Settings; + +namespace TagsCloudPainterApplication.Actions +{ + public class ImageSettingsAction: IUiAction + { + private readonly ImageSettings imageSettings; + + public ImageSettingsAction(ImageSettings imageSettings) + { + this.imageSettings = imageSettings; + } + + public string Category => "Настройки"; + public string Name => "Изображение"; + public string Description => "Укажите размер изображения"; + + public void Perform() + { + SettingsForm.For(imageSettings).ShowDialog(); + } + } +} diff --git a/TagsCloudPainterApplication/Actions/PaletteSettingsAction.cs b/TagsCloudPainterApplication/Actions/PaletteSettingsAction.cs new file mode 100644 index 000000000..0849fd495 --- /dev/null +++ b/TagsCloudPainterApplication/Actions/PaletteSettingsAction.cs @@ -0,0 +1,24 @@ +using TagsCloudPainterApplication.Infrastructure; +using TagsCloudPainterApplication.Infrastructure.Settings; + +namespace TagsCloudPainterApplication.Actions +{ + public class PaletteSettingsAction : IUiAction + { + private readonly Palette palette; + + public PaletteSettingsAction(Palette palette) + { + this.palette = palette; + } + + public string Category => "Настройки"; + public string Name => "Палитра..."; + public string Description => "Цвета для рисования облака тэгов"; + + public void Perform() + { + SettingsForm.For(palette).ShowDialog(); + } + } +} diff --git a/TagsCloudPainterApplication/Actions/SaveImageAction.cs b/TagsCloudPainterApplication/Actions/SaveImageAction.cs new file mode 100644 index 000000000..62c4add7c --- /dev/null +++ b/TagsCloudPainterApplication/Actions/SaveImageAction.cs @@ -0,0 +1,33 @@ +using TagsCloudPainterApplication.Infrastructure; + +namespace TagsCloudPainterApplication.Actions +{ + public class SaveImageAction : IUiAction + { + private readonly IImageHolder imageHolder; + + public SaveImageAction(IImageHolder imageHolder) + { + this.imageHolder = imageHolder; + } + + public string Category => "Файл"; + public string Name => "Сохранить..."; + public string Description => "Сохранить изображение в файл"; + + public void Perform() + { + var dialog = new SaveFileDialog + { + CheckFileExists = false, + InitialDirectory = Path.GetFullPath(Environment.CurrentDirectory), + DefaultExt = "png", + FileName = "image.png", + Filter = "Изображения (*.png)|*.png|Изображения (*.jpeg)|*.jpeg" + }; + var res = dialog.ShowDialog(); + if (res == DialogResult.OK) + imageHolder.SaveImage(dialog.FileName); + } + } +} diff --git a/TagsCloudPainterApplication/Actions/UiActionExtensions.cs b/TagsCloudPainterApplication/Actions/UiActionExtensions.cs new file mode 100644 index 000000000..4027f925f --- /dev/null +++ b/TagsCloudPainterApplication/Actions/UiActionExtensions.cs @@ -0,0 +1,34 @@ +using System.Collections.Generic; +using System.Linq; +using System.Windows.Forms; + +namespace TagsCloudPainterApplication.Actions +{ + public static class UiActionExtensions + { + public static ToolStripItem[] ToMenuItems(this IUiAction[] actions) + { + var items = actions.GroupBy(a => a.Category) + .Select(g => CreateTopLevelMenuItem(g.Key, g.ToList())) + .Cast() + .ToArray(); + return items; + } + + private static ToolStripMenuItem CreateTopLevelMenuItem(string name, IList items) + { + var menuItems = items.Select(a => a.ToMenuItem()).ToArray(); + return new ToolStripMenuItem(name, null, menuItems); + } + + public static ToolStripItem ToMenuItem(this IUiAction action) + { + return + new ToolStripMenuItem(action.Name, null, (sender, args) => action.Perform()) + { + ToolTipText = action.Description, + Tag = action + }; + } + } +} diff --git a/TagsCloudPainterApplication/App.config b/TagsCloudPainterApplication/App.config new file mode 100644 index 000000000..b46b4d3a1 --- /dev/null +++ b/TagsCloudPainterApplication/App.config @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/TagsCloudPainterApplication/Data/BoringWords.txt b/TagsCloudPainterApplication/Data/BoringWords.txt new file mode 100644 index 000000000..30d46d215 --- /dev/null +++ b/TagsCloudPainterApplication/Data/BoringWords.txt @@ -0,0 +1,238 @@ +к +в +на +по +с +из +у +от +за +до +перед +через +со +о +об +при +под +как +если +что +чтобы +когда +где +потому что +поскольку +или +либо +да +нет +также +тоже +но +однако +а +и +да +ли +же +ибо +то +что +как +чтобы +если +пока +после +пока +после +так +также +тоже +потому что +поэтому +поэтому что +вот +вон +вроде +как будто +как будто бы +как бы +как бы то ни было +и так далее +так и +так что +так как +так что +то есть +даже +еще +бы +лишь бы +раз +однако +все-таки +все же +тем более +более того +по-прежнему +потому что +поэтому +следовательно +таким образом +в результате +так что +к тому же +благодаря +несмотря на +вопреки +вследствие +напротив +несмотря на то что +в силу +в виду +кроме +как +ради +из-за +без +для +против +в обмен на +в замен +наряду с +среди +помимо +в течение +на протяжении +в течение +вплоть до +во время +в преддверии +ввиду +вслед за +вдоль +возле +в глубь +в глубину +в направлении +в направлении к +в направлении от +вокруг +вокруг по периметру +в отличие от +в соответствии с +в связи с +в пределах +в предприятие +в случае +в случае если +в сравнении с +в среде +в условиях +в ходе +в честь +в память о +в отношении +в разрезе +в поддержку +в свете +в зависимости от +в результате +в целях +в интересах +в глазах +в поисках +вместо +вопреки +вдоль +внутри +вне +впереди +внизу +вверху +возле +вокруг +внутри +впереди +в сравнении с +в отличие от +вслед за +внутрь +вперед +ввысь +вдоль +вовне +вглубь +впереди +вокруг +внутрь +вниз +вверх +вдоль +возле +впереди +во имя +в пользу +в случае +в следствие +в связи с +в целях +в зависимости от +в отношении +в память о +в виду +в наказание +в пользу +в противном случае +в пользу +в результате +в служение +в свою очередь +в отличие от +в сравнении с +в целях +в следствие +в свете +в целях +вперед +вниз +вверх +вдоль +вне +вокруг +впереди +вперед +внутри +внутри +в сравнении с +в отличие от +вопреки +вслед за +внутрь +вверх +впереди +внутрь +вслед за +внутрь +вверх +вокруг +вглубь +внутрь +вперед +впереди +внутрь +вниз +вверх +вокруг +внутри +вверх +вниз +вглубь +вдоль +вперед +вокруг +вокруг +впереди +вниз +вверх \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/IImageHolder.cs b/TagsCloudPainterApplication/Infrastructure/IImageHolder.cs new file mode 100644 index 000000000..a9ee2c23f --- /dev/null +++ b/TagsCloudPainterApplication/Infrastructure/IImageHolder.cs @@ -0,0 +1,13 @@ +using TagsCloudPainterApplication.Infrastructure.Settings; + +namespace TagsCloudPainterApplication.Infrastructure +{ + public interface IImageHolder + { + Size GetImageSize(); + Graphics StartDrawing(); + void UpdateUi(); + void RecreateImage(ImageSettings settings); + void SaveImage(string fileName); + } +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/Palette.cs b/TagsCloudPainterApplication/Infrastructure/Palette.cs new file mode 100644 index 000000000..ad6c6603b --- /dev/null +++ b/TagsCloudPainterApplication/Infrastructure/Palette.cs @@ -0,0 +1,9 @@ +namespace TagsCloudPainterApplication.Infrastructure +{ + public class Palette + { + public Color PrimaryColor { get; set; } = Color.Yellow; + public Color SecondaryColor { get; set; } = Color.Red; + public Color BackgroundColor { get; set; } = Color.DarkBlue; + } +} diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs new file mode 100644 index 000000000..ddbdab326 --- /dev/null +++ b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs @@ -0,0 +1,7 @@ +namespace TagsCloudPainterApplication.Infrastructure.Settings +{ + public class FilesSourceSettings + { + public string BoringTextFilePath { get; set; } = @$"{Environment.CurrentDirectory}..\..\..\..\Data\BoringWords.txt"; + } +} diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs new file mode 100644 index 000000000..0f499238f --- /dev/null +++ b/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs @@ -0,0 +1,8 @@ +namespace TagsCloudPainterApplication.Infrastructure.Settings +{ + public class ImageSettings + { + public int Width { get; set; } = 800; + public int Height { get; set; } = 600; + } +} diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/SettingsForm.cs b/TagsCloudPainterApplication/Infrastructure/Settings/SettingsForm.cs new file mode 100644 index 000000000..1d8e6303d --- /dev/null +++ b/TagsCloudPainterApplication/Infrastructure/Settings/SettingsForm.cs @@ -0,0 +1,36 @@ +namespace TagsCloudPainterApplication.Infrastructure.Settings +{ + public static class SettingsForm + { + public static SettingsForm For(TSettings settings) + { + return new SettingsForm(settings); + } + } + + public class SettingsForm : Form + { + public SettingsForm(TSettings settings) + { + var okButton = new Button + { + Text = "OK", + DialogResult = DialogResult.OK, + Dock = DockStyle.Bottom, + }; + Controls.Add(okButton); + Controls.Add(new PropertyGrid + { + SelectedObject = settings, + Dock = DockStyle.Fill + }); + AcceptButton = okButton; + } + + protected override void OnLoad(EventArgs e) + { + base.OnLoad(e); + Text = "Настройки"; + } + } +} diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs new file mode 100644 index 000000000..9df3ac543 --- /dev/null +++ b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs @@ -0,0 +1,61 @@ +using System.ComponentModel; +using TagsCloudPainter.Settings; + +namespace TagsCloudPainterApplication.Infrastructure.Settings +{ + public class TagsCloudSettings + { + [Browsable(false)] public CloudSettings CloudSettings { get; private set; } + [Browsable(false)] public TagSettings TagSettings { get; private set; } + [Browsable(false)] public SpiralPointerSettings SpiralPointerSettings { get; private set; } + [Browsable(false)] public TextSettings TextSettings { get; private set; } + public int TagFontSize + { + get => TagSettings.TagFontSize; + set => TagSettings.TagFontSize = value; + } + public string TagFontName + { + get => TagSettings.TagFontName; + set => TagSettings.TagFontName = value; + } + public Point CloudCenter + { + get => CloudSettings.CloudCenter; + set => CloudSettings.CloudCenter = value; + } + public double PointerStep + { + get => SpiralPointerSettings.Step; + set => SpiralPointerSettings.Step = value; + } + public double PointerRadiusConst + { + get => SpiralPointerSettings.RadiusConst; + set => SpiralPointerSettings.RadiusConst = value; + } + public double PointerAngleConst + { + get => SpiralPointerSettings.AngleConst; + set => SpiralPointerSettings.AngleConst = value; + } + + public TagsCloudSettings( + CloudSettings cloudSettings, + TagSettings tagSettings, + SpiralPointerSettings spiralPointerSettings, + TextSettings textSettings) + { + CloudSettings = cloudSettings; + TagSettings = tagSettings; + SpiralPointerSettings = spiralPointerSettings; + TextSettings = textSettings; + TagFontSize = 32; + TagFontName = "Arial"; + CloudCenter = new Point(0, 0); + PointerStep = 0.1; + PointerRadiusConst = 0.5; + PointerAngleConst = 1; + } + } +} diff --git a/TagsCloudPainterApplication/MainForm.Designer.cs b/TagsCloudPainterApplication/MainForm.Designer.cs new file mode 100644 index 000000000..4362dc1e9 --- /dev/null +++ b/TagsCloudPainterApplication/MainForm.Designer.cs @@ -0,0 +1,39 @@ +namespace TagsCloudPainterApplication +{ + partial class MainForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// 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 = "Form1"; + } + + #endregion + } +} diff --git a/TagsCloudPainterApplication/MainForm.cs b/TagsCloudPainterApplication/MainForm.cs new file mode 100644 index 000000000..eeb702b94 --- /dev/null +++ b/TagsCloudPainterApplication/MainForm.cs @@ -0,0 +1,21 @@ +using TagsCloudPainterApplication.Actions; +using TagsCloudPainterApplication.Infrastructure.Settings; + +namespace TagsCloudPainterApplication +{ + public partial class MainForm : Form + { + public MainForm(IUiAction[] actions, ImageSettings imageSettings, PictureBoxImageHolder pictureBox) + { + ClientSize = new Size(imageSettings.Width, imageSettings.Height); + + var mainMenu = new MenuStrip(); + mainMenu.Items.AddRange(actions.ToMenuItems()); + Controls.Add(mainMenu); + + pictureBox.RecreateImage(imageSettings); + pictureBox.Dock = DockStyle.Fill; + Controls.Add(pictureBox); + } + } +} diff --git a/TagsCloudPainterApplication/MainForm.resx b/TagsCloudPainterApplication/MainForm.resx new file mode 100644 index 000000000..1af7de150 --- /dev/null +++ b/TagsCloudPainterApplication/MainForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/TagsCloudPainterApplication/PictureBoxImageHolder.cs b/TagsCloudPainterApplication/PictureBoxImageHolder.cs new file mode 100644 index 000000000..745a195be --- /dev/null +++ b/TagsCloudPainterApplication/PictureBoxImageHolder.cs @@ -0,0 +1,45 @@ +using System.Drawing.Imaging; +using TagsCloudPainterApplication.Infrastructure; +using TagsCloudPainterApplication.Infrastructure.Settings; + +namespace TagsCloudPainterApplication + +{ + public class PictureBoxImageHolder : PictureBox, IImageHolder + { + public Size GetImageSize() + { + FailIfNotInitialized(); + return Image.Size; + } + + public Graphics StartDrawing() + { + FailIfNotInitialized(); + return Graphics.FromImage(Image); + } + + private void FailIfNotInitialized() + { + if (Image == null) + throw new InvalidOperationException("Call PictureBoxImageHolder.RecreateImage before other method call!"); + } + + public void UpdateUi() + { + Refresh(); + Application.DoEvents(); + } + + public void RecreateImage(ImageSettings imageSettings) + { + Image = new Bitmap(imageSettings.Width, imageSettings.Height, PixelFormat.Format24bppRgb); + } + + public void SaveImage(string fileName) + { + FailIfNotInitialized(); + Image.Save(fileName); + } + } +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Program.cs b/TagsCloudPainterApplication/Program.cs new file mode 100644 index 000000000..165b4a9f0 --- /dev/null +++ b/TagsCloudPainterApplication/Program.cs @@ -0,0 +1,53 @@ +using Autofac; +using TagsCloudPainter.CloudLayouter; +using TagsCloudPainter.Drawer; +using TagsCloudPainter.FileReader; +using TagsCloudPainter.FormPointer; +using TagsCloudPainter.Parser; +using TagsCloudPainter.Settings; +using TagsCloudPainter.Tags; +using TagsCloudPainterApplication.Actions; +using TagsCloudPainterApplication.Infrastructure; +using TagsCloudPainterApplication.Infrastructure.Settings; + +namespace TagsCloudPainterApplication +{ + internal static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + var builder = new ContainerBuilder(); + + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().AsSelf(); + + var container = builder.Build(); + Application.Run(container.Resolve()); + } + } +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Properties/Resources.Designer.cs b/TagsCloudPainterApplication/Properties/Resources.Designer.cs new file mode 100644 index 000000000..876edd598 --- /dev/null +++ b/TagsCloudPainterApplication/Properties/Resources.Designer.cs @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// Этот код создан программой. +// Исполняемая версия:4.0.30319.42000 +// +// Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае +// повторной генерации кода. +// +//------------------------------------------------------------------------------ + +namespace TagsCloudPainterApplication.Properties { + using System; + + + /// + /// Класс ресурса со строгой типизацией для поиска локализованных строк и т.д. + /// + // Этот класс создан автоматически классом StronglyTypedResourceBuilder + // с помощью такого средства, как ResGen или Visual Studio. + // Чтобы добавить или удалить член, измените файл .ResX и снова запустите ResGen + // с параметром /str или перестройте свой проект VS. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Возвращает кэшированный экземпляр ResourceManager, использованный этим классом. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TagsCloudPainterApplication.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Перезаписывает свойство CurrentUICulture текущего потока для всех + /// обращений к ресурсу с помощью этого класса ресурса со строгой типизацией. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + } +} diff --git a/TagsCloudPainterApplication/Properties/Resources.resx b/TagsCloudPainterApplication/Properties/Resources.resx new file mode 100644 index 000000000..af7dbebba --- /dev/null +++ b/TagsCloudPainterApplication/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/TagsCloudPainterApplication/Properties/Settings.Designer.cs b/TagsCloudPainterApplication/Properties/Settings.Designer.cs new file mode 100644 index 000000000..80a3c58a6 --- /dev/null +++ b/TagsCloudPainterApplication/Properties/Settings.Designer.cs @@ -0,0 +1,26 @@ +//------------------------------------------------------------------------------ +// +// Этот код создан программой. +// Исполняемая версия:4.0.30319.42000 +// +// Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае +// повторной генерации кода. +// +//------------------------------------------------------------------------------ + +namespace TagsCloudPainterApplication.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.8.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + } +} diff --git a/TagsCloudPainterApplication/Properties/Settings.settings b/TagsCloudPainterApplication/Properties/Settings.settings new file mode 100644 index 000000000..39645652a --- /dev/null +++ b/TagsCloudPainterApplication/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj b/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj new file mode 100644 index 000000000..6e9991cc3 --- /dev/null +++ b/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj @@ -0,0 +1,42 @@ + + + + WinExe + net8.0-windows + enable + true + enable + false + + + + + + + + + + + + + Component + + + True + True + Settings.settings + + + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + + + + \ No newline at end of file diff --git a/TagsCloudPainterApplication/packages.config b/TagsCloudPainterApplication/packages.config new file mode 100644 index 000000000..8411f2587 --- /dev/null +++ b/TagsCloudPainterApplication/packages.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/di.sln b/di.sln index b27b7c05d..ec7ec4d50 100644 --- a/di.sln +++ b/di.sln @@ -1,6 +1,15 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FractalPainter", "FractalPainter\FractalPainter.csproj", "{4D70883B-6F8B-4166-802F-8EDC9BE93199}" +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34330.188 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FractalPainter", "FractalPainter\FractalPainter.csproj", "{4D70883B-6F8B-4166-802F-8EDC9BE93199}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TagsCloudPainter", "TagsCloudPainter\TagsCloudPainter.csproj", "{BE869D12-2ABD-4F38-BCC8-AD8B808E0F9A}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TagsCloudPainterApplication", "TagsCloudPainterApplication\TagsCloudPainterApplication.csproj", "{BE46FCA0-8EE5-40F4-9FB5-E9ABFA0200EE}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TagsCloudPainterTests", "Tests\TagsCloudPainterTests.csproj", "{61D758B1-A9F4-4E48-B1F9-0CA1637BA9A0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -12,5 +21,23 @@ Global {4D70883B-6F8B-4166-802F-8EDC9BE93199}.Debug|Any CPU.Build.0 = Debug|Any CPU {4D70883B-6F8B-4166-802F-8EDC9BE93199}.Release|Any CPU.ActiveCfg = Release|Any CPU {4D70883B-6F8B-4166-802F-8EDC9BE93199}.Release|Any CPU.Build.0 = Release|Any CPU + {BE869D12-2ABD-4F38-BCC8-AD8B808E0F9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BE869D12-2ABD-4F38-BCC8-AD8B808E0F9A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BE869D12-2ABD-4F38-BCC8-AD8B808E0F9A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BE869D12-2ABD-4F38-BCC8-AD8B808E0F9A}.Release|Any CPU.Build.0 = Release|Any CPU + {BE46FCA0-8EE5-40F4-9FB5-E9ABFA0200EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BE46FCA0-8EE5-40F4-9FB5-E9ABFA0200EE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BE46FCA0-8EE5-40F4-9FB5-E9ABFA0200EE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BE46FCA0-8EE5-40F4-9FB5-E9ABFA0200EE}.Release|Any CPU.Build.0 = Release|Any CPU + {61D758B1-A9F4-4E48-B1F9-0CA1637BA9A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {61D758B1-A9F4-4E48-B1F9-0CA1637BA9A0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {61D758B1-A9F4-4E48-B1F9-0CA1637BA9A0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {61D758B1-A9F4-4E48-B1F9-0CA1637BA9A0}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F33C482A-B97D-48B7-A2A3-020D3D102334} EndGlobalSection EndGlobal From 45c5d7f50377fda28af42bd0e3f30504c31a124b Mon Sep 17 00:00:00 2001 From: daHil Date: Wed, 24 Jan 2024 04:38:43 +0500 Subject: [PATCH 09/34] refactored code --- TagsCloudPainter/CloudDrawer/CloudDrawer.cs | 87 ++++--- .../CloudLayouter/ICloudLayouter.cs | 17 +- .../CloudLayouter/TagsCloudLayouter.cs | 97 ++++---- TagsCloudPainter/FileReader/IFileReader.cs | 11 +- TagsCloudPainter/FileReader/TextFileReader.cs | 53 ++-- .../FormPointer/ArchimedeanSpiralPointer.cs | 59 +++-- TagsCloudPainter/FormPointer/IFormPointer.cs | 11 +- TagsCloudPainter/IResetable.cs | 11 +- TagsCloudPainter/Parser/BoringTextParser.cs | 47 ++-- TagsCloudPainter/Parser/ITextParser.cs | 11 +- TagsCloudPainter/Settings/CloudSettings.cs | 13 +- .../Settings/SpiralPointerSettings.cs | 19 +- TagsCloudPainter/Settings/TagSettings.cs | 15 +- TagsCloudPainter/Settings/TextSettings.cs | 11 +- TagsCloudPainter/Tags/ITagsBuilder.cs | 11 +- TagsCloudPainter/Tags/Tag.cs | 25 +- TagsCloudPainter/Tags/TagsBuilder.cs | 63 +++-- TagsCloudPainter/TagsCloud.cs | 6 +- TagsCloudPainter/TagsCloudPainter.csproj | 20 +- TagsCloudPainter/Utils/Utils.cs | 45 ++-- .../Actions/DrawTagCloudAction.cs | 162 ++++++------ .../Actions/FileSourceSettingsAction.cs | 31 ++- .../Actions/IUiAction.cs | 17 +- .../Actions/ImageSettingsAction.cs | 31 ++- .../Actions/PaletteSettingsAction.cs | 31 ++- .../Actions/SaveImageAction.cs | 49 ++-- .../Actions/UiActionExtensions.cs | 51 ++-- TagsCloudPainterApplication/App.config | 32 +-- .../Infrastructure/IImageHolder.cs | 17 +- .../Infrastructure/Palette.cs | 15 +- .../Settings/FilesSourceSettings.cs | 11 +- .../Infrastructure/Settings/ImageSettings.cs | 13 +- .../Infrastructure/Settings/SettingsForm.cs | 55 ++-- .../Settings/TagsCloudSettings.cs | 115 +++++---- TagsCloudPainterApplication/MainForm.cs | 25 +- TagsCloudPainterApplication/MainForm.resx | 235 +++++++++--------- .../PictureBoxImageHolder.cs | 60 +++-- TagsCloudPainterApplication/Program.cs | 69 +++-- .../Properties/Resources.resx | 229 ++++++++--------- .../Properties/Settings.Designer.cs | 9 +- .../Properties/Settings.settings | 8 +- .../TagsCloudPainterApplication.csproj | 66 ++--- TagsCloudPainterApplication/packages.config | 16 +- Tests/ArchimedeanSpiralTests.cs | 27 +- Tests/BoringTextParserTests.cs | 98 ++++---- Tests/CloudDrawerTests.cs | 78 +++--- Tests/TagsBuilderTests.cs | 77 +++--- Tests/TagsCloudLayouterTests.cs | 165 ++++++------ Tests/TagsCloudPainterTests.csproj | 36 +-- Tests/TextFileReaderTests.cs | 59 +++-- 50 files changed, 1245 insertions(+), 1274 deletions(-) diff --git a/TagsCloudPainter/CloudDrawer/CloudDrawer.cs b/TagsCloudPainter/CloudDrawer/CloudDrawer.cs index c03dbb219..914b50d3b 100644 --- a/TagsCloudPainter/CloudDrawer/CloudDrawer.cs +++ b/TagsCloudPainter/CloudDrawer/CloudDrawer.cs @@ -2,55 +2,54 @@ using System.Drawing.Drawing2D; using TagsCloudPainter.Settings; -namespace TagsCloudPainter.Drawer +namespace TagsCloudPainter.Drawer; + +public class CloudDrawer { - public class CloudDrawer + private readonly CloudSettings cloudSettings; + private readonly TagSettings tagSettings; + + public CloudDrawer(TagSettings tagSettings, CloudSettings cloudSettings) { - private readonly TagSettings tagSettings; - private readonly CloudSettings cloudSettings; + this.tagSettings = tagSettings; + this.cloudSettings = cloudSettings; + } - public CloudDrawer(TagSettings tagSettings, CloudSettings cloudSettings) - { - this.tagSettings = tagSettings; - this.cloudSettings = cloudSettings; - } + public Bitmap DrawCloud(TagsCloud cloud, int imageWidth, int imageHeight) + { + if (cloud.Tags.Count == 0) + throw new ArgumentException("rectangles are empty"); + if (imageWidth <= 0 || imageHeight <= 0) + throw new ArgumentException("either width or height of rectangle size is not positive"); - public Bitmap DrawCloud(TagsCloud cloud, int imageWidth, int imageHeight) - { - if (cloud.Tags.Count == 0) - throw new ArgumentException("rectangles are empty"); - if (imageWidth <= 0 || imageHeight <= 0) - throw new ArgumentException("either width or height of rectangle size is not positive"); - - var drawingScale = CalculateObjectDrawingScale(cloud.GetWidth(), cloud.GetHeight(), imageWidth, imageHeight); - var bitmap = new Bitmap(imageWidth, imageHeight); - var graphics = Graphics.FromImage(bitmap); - var pen = new Pen(tagSettings.TagColor); - - graphics.TranslateTransform(-cloud.Center.X, -cloud.Center.Y); - graphics.ScaleTransform(drawingScale, drawingScale, MatrixOrder.Append); - graphics.TranslateTransform(cloud.Center.X, cloud.Center.Y, MatrixOrder.Append); - graphics.Clear(cloudSettings.BackgroundColor); - foreach(var tag in cloud.Tags) - { - var font = new Font(tagSettings.TagFontName, tag.Key.FontSize); - graphics.DrawString(tag.Key.Value, font, pen.Brush, tag.Value.Location); - } - - return bitmap; - } + var drawingScale = CalculateObjectDrawingScale(cloud.GetWidth(), cloud.GetHeight(), imageWidth, imageHeight); + var bitmap = new Bitmap(imageWidth, imageHeight); + var graphics = Graphics.FromImage(bitmap); + var pen = new Pen(tagSettings.TagColor); - public static float CalculateObjectDrawingScale(float width, float height, float imageWidth, float imageHeight) + graphics.TranslateTransform(-cloud.Center.X, -cloud.Center.Y); + graphics.ScaleTransform(drawingScale, drawingScale, MatrixOrder.Append); + graphics.TranslateTransform(cloud.Center.X, cloud.Center.Y, MatrixOrder.Append); + graphics.Clear(cloudSettings.BackgroundColor); + foreach (var tag in cloud.Tags) { - var scale = 1f; - var scaleAccuracy = 0.05f; - var widthScale = scale; - var heightScale = scale; - if (width * scale > imageWidth) - widthScale = imageWidth / width - scaleAccuracy; - if (height * scale > imageHeight) - heightScale = imageHeight / height - scaleAccuracy; - return Math.Min(widthScale, heightScale); + var font = new Font(tagSettings.TagFontName, tag.Key.FontSize); + graphics.DrawString(tag.Key.Value, font, pen.Brush, tag.Value.Location); } + + return bitmap; + } + + public static float CalculateObjectDrawingScale(float width, float height, float imageWidth, float imageHeight) + { + var scale = 1f; + var scaleAccuracy = 0.05f; + var widthScale = scale; + var heightScale = scale; + if (width * scale > imageWidth) + widthScale = imageWidth / width - scaleAccuracy; + if (height * scale > imageHeight) + heightScale = imageHeight / height - scaleAccuracy; + return Math.Min(widthScale, heightScale); } -} +} \ No newline at end of file diff --git a/TagsCloudPainter/CloudLayouter/ICloudLayouter.cs b/TagsCloudPainter/CloudLayouter/ICloudLayouter.cs index 91ca175db..e06fc1efb 100644 --- a/TagsCloudPainter/CloudLayouter/ICloudLayouter.cs +++ b/TagsCloudPainter/CloudLayouter/ICloudLayouter.cs @@ -1,13 +1,12 @@ using System.Drawing; using TagsCloudPainter.Tags; -namespace TagsCloudPainter.CloudLayouter +namespace TagsCloudPainter.CloudLayouter; + +public interface ICloudLayouter : IResetable { - public interface ICloudLayouter: IResetable - { - Rectangle PutNextTag(Tag tag); - TagsCloud GetCloud(); - void PutTags(List tags); - void InitializeCloud(); - } -} + Rectangle PutNextTag(Tag tag); + TagsCloud GetCloud(); + void PutTags(List tags); + void InitializeCloud(); +} \ No newline at end of file diff --git a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs index 2aad270b5..bab1b3b10 100644 --- a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs +++ b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs @@ -3,64 +3,67 @@ using TagsCloudPainter.Settings; using TagsCloudPainter.Tags; -namespace TagsCloudPainter.CloudLayouter +namespace TagsCloudPainter.CloudLayouter; + +public class TagsCloudLayouter : ICloudLayouter { - public class TagsCloudLayouter : ICloudLayouter - { - private readonly IFormPointer formPointer; - private TagsCloud cloud; - private readonly TagSettings tagSettings; - private CloudSettings cloudSettings; + private readonly IFormPointer formPointer; + private readonly TagSettings tagSettings; + private TagsCloud cloud; + private readonly CloudSettings cloudSettings; - public TagsCloudLayouter(CloudSettings cloudSettings, IFormPointer formPointer, TagSettings tagSettings) - { - this.cloudSettings = cloudSettings; - this.formPointer = formPointer; - this.tagSettings = tagSettings; - } + public TagsCloudLayouter(CloudSettings cloudSettings, IFormPointer formPointer, TagSettings tagSettings) + { + this.cloudSettings = cloudSettings; + this.formPointer = formPointer; + this.tagSettings = tagSettings; + } - public Rectangle PutNextTag(Tag tag) - { - FailIfCloudNotInitialized(); + public Rectangle PutNextTag(Tag tag) + { + FailIfCloudNotInitialized(); - var rectangleSize = Utils.Utils.GetStringSize(tag.Value, tagSettings.TagFontName, tag.FontSize); - if (rectangleSize.Height <= 0 || rectangleSize.Width <= 0) - throw new ArgumentException("either width or height of rectangle size is not possitive"); + var rectangleSize = Utils.Utils.GetStringSize(tag.Value, tagSettings.TagFontName, tag.FontSize); + if (rectangleSize.Height <= 0 || rectangleSize.Width <= 0) + throw new ArgumentException("either width or height of rectangle size is not possitive"); - var nextRectangle = Utils.Utils.GetRectangleFromCenter(formPointer.GetNextPoint(), rectangleSize); - while (cloud.Tags.Values.Any(rectangle => rectangle.IntersectsWith(nextRectangle))) - nextRectangle = Utils.Utils.GetRectangleFromCenter(formPointer.GetNextPoint(), rectangleSize); + var nextRectangle = Utils.Utils.GetRectangleFromCenter(formPointer.GetNextPoint(), rectangleSize); + while (cloud.Tags.Values.Any(rectangle => rectangle.IntersectsWith(nextRectangle))) + nextRectangle = Utils.Utils.GetRectangleFromCenter(formPointer.GetNextPoint(), rectangleSize); - cloud.AddTag(tag, nextRectangle); + cloud.AddTag(tag, nextRectangle); - return nextRectangle; - } + return nextRectangle; + } - public void PutTags(List tags) - { - FailIfCloudNotInitialized(); + public void PutTags(List tags) + { + FailIfCloudNotInitialized(); - if (tags.Count == 0) - throw new ArgumentException("пустые размеры"); - foreach (var tag in tags) - PutNextTag(tag); - } + if (tags.Count == 0) + throw new ArgumentException("пустые размеры"); + foreach (var tag in tags) + PutNextTag(tag); + } - public TagsCloud GetCloud() => - new TagsCloud(cloud.Center, cloud.Tags); + public TagsCloud GetCloud() + { + return new TagsCloud(cloud.Center, cloud.Tags); + } - public void InitializeCloud() - => cloud = new TagsCloud(cloudSettings.CloudCenter, []); + public void InitializeCloud() + { + cloud = new TagsCloud(cloudSettings.CloudCenter, []); + } - public void FailIfCloudNotInitialized() - { - if (cloud is null) - throw new InvalidOperationException("Initialize cloud before other method call!"); - } + public void Reset() + { + formPointer.Reset(); + } - public void Reset() - { - formPointer.Reset(); - } + public void FailIfCloudNotInitialized() + { + if (cloud is null) + throw new InvalidOperationException("Initialize cloud before other method call!"); } -} +} \ No newline at end of file diff --git a/TagsCloudPainter/FileReader/IFileReader.cs b/TagsCloudPainter/FileReader/IFileReader.cs index 26270cac6..5d1c6819f 100644 --- a/TagsCloudPainter/FileReader/IFileReader.cs +++ b/TagsCloudPainter/FileReader/IFileReader.cs @@ -1,7 +1,6 @@ -namespace TagsCloudPainter.FileReader +namespace TagsCloudPainter.FileReader; + +public interface IFileReader { - public interface IFileReader - { - public string ReadFile(string path); - } -} + public string ReadFile(string path); +} \ No newline at end of file diff --git a/TagsCloudPainter/FileReader/TextFileReader.cs b/TagsCloudPainter/FileReader/TextFileReader.cs index 78a63c7c0..c62379ab9 100644 --- a/TagsCloudPainter/FileReader/TextFileReader.cs +++ b/TagsCloudPainter/FileReader/TextFileReader.cs @@ -1,35 +1,34 @@ using Spire.Doc; -namespace TagsCloudPainter.FileReader +namespace TagsCloudPainter.FileReader; + +public class TextFileReader : IFileReader { - public class TextFileReader : IFileReader + public string ReadFile(string path) { - public string ReadFile(string path) - { - if (!File.Exists(path)) - throw new FileNotFoundException(); - - return Path.GetExtension(path) switch - { - ".txt" => ReadTxtFile(path), - ".doc" => ReadDocFile(path), - ".docx" => ReadDocFile(path), - _ => throw new ArgumentException("Incorrect file extension. Supported file extensions: txt, doc, docx"), - }; - } + if (!File.Exists(path)) + throw new FileNotFoundException(); - private static string ReadTxtFile(string path) + return Path.GetExtension(path) switch { - return File.ReadAllText(path).Trim(); - } + ".txt" => ReadTxtFile(path), + ".doc" => ReadDocFile(path), + ".docx" => ReadDocFile(path), + _ => throw new ArgumentException("Incorrect file extension. Supported file extensions: txt, doc, docx") + }; + } - private static string ReadDocFile(string path) - { - var doc = new Document(); - doc.LoadFromFile(path); - var text = doc.GetText(); - var lastIndexOfSpirePart = text.IndexOf(Environment.NewLine); - return text.Substring(lastIndexOfSpirePart + 2).Trim(); - } + private static string ReadTxtFile(string path) + { + return File.ReadAllText(path).Trim(); + } + + private static string ReadDocFile(string path) + { + var doc = new Document(); + doc.LoadFromFile(path); + var text = doc.GetText(); + var lastIndexOfSpirePart = text.IndexOf(Environment.NewLine); + return text.Substring(lastIndexOfSpirePart + 2).Trim(); } -} +} \ No newline at end of file diff --git a/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs b/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs index 4d26158c9..138c4075f 100644 --- a/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs +++ b/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs @@ -1,40 +1,39 @@ using System.Drawing; using TagsCloudPainter.Settings; -namespace TagsCloudPainter.FormPointer +namespace TagsCloudPainter.FormPointer; + +public class ArchimedeanSpiralPointer : IFormPointer { - public class ArchimedeanSpiralPointer : IFormPointer - { - private double сurrentDifference; - private readonly CloudSettings cloudSettings; - private readonly SpiralPointerSettings spiralPointerSettings; + private readonly CloudSettings cloudSettings; + private readonly SpiralPointerSettings spiralPointerSettings; + private double сurrentDifference; - public ArchimedeanSpiralPointer(CloudSettings cloudSettings, SpiralPointerSettings spiralPointerSettings) - { - if (spiralPointerSettings.Step <= 0 - || spiralPointerSettings.RadiusConst <= 0 - || spiralPointerSettings.AngleConst <= 0) - throw new ArgumentException("either step or radius or angle is not possitive"); - this.cloudSettings = cloudSettings; - this.spiralPointerSettings = spiralPointerSettings; - сurrentDifference = 0; - } + public ArchimedeanSpiralPointer(CloudSettings cloudSettings, SpiralPointerSettings spiralPointerSettings) + { + if (spiralPointerSettings.Step <= 0 + || spiralPointerSettings.RadiusConst <= 0 + || spiralPointerSettings.AngleConst <= 0) + throw new ArgumentException("either step or radius or angle is not possitive"); + this.cloudSettings = cloudSettings; + this.spiralPointerSettings = spiralPointerSettings; + сurrentDifference = 0; + } - private double Angle => сurrentDifference * spiralPointerSettings.AngleConst; - private double Radius => сurrentDifference * spiralPointerSettings.RadiusConst; + private double Angle => сurrentDifference * spiralPointerSettings.AngleConst; + private double Radius => сurrentDifference * spiralPointerSettings.RadiusConst; - public Point GetNextPoint() - { - сurrentDifference += spiralPointerSettings.Step; - var x = cloudSettings.CloudCenter.X + (int)(Radius * Math.Cos(Angle)); - var y = cloudSettings.CloudCenter.Y + (int)(Radius * Math.Sin(Angle)); + public Point GetNextPoint() + { + сurrentDifference += spiralPointerSettings.Step; + var x = cloudSettings.CloudCenter.X + (int)(Radius * Math.Cos(Angle)); + var y = cloudSettings.CloudCenter.Y + (int)(Radius * Math.Sin(Angle)); - return new Point(x, y); - } + return new Point(x, y); + } - public void Reset() - { - сurrentDifference = 0; - } + public void Reset() + { + сurrentDifference = 0; } -} +} \ No newline at end of file diff --git a/TagsCloudPainter/FormPointer/IFormPointer.cs b/TagsCloudPainter/FormPointer/IFormPointer.cs index 5db7c0c17..ca3120df2 100644 --- a/TagsCloudPainter/FormPointer/IFormPointer.cs +++ b/TagsCloudPainter/FormPointer/IFormPointer.cs @@ -1,9 +1,8 @@ using System.Drawing; -namespace TagsCloudPainter.FormPointer +namespace TagsCloudPainter.FormPointer; + +public interface IFormPointer : IResetable { - public interface IFormPointer: IResetable - { - Point GetNextPoint(); - } -} + Point GetNextPoint(); +} \ No newline at end of file diff --git a/TagsCloudPainter/IResetable.cs b/TagsCloudPainter/IResetable.cs index b74027d4f..0bb4cef54 100644 --- a/TagsCloudPainter/IResetable.cs +++ b/TagsCloudPainter/IResetable.cs @@ -1,7 +1,6 @@ -namespace TagsCloudPainter +namespace TagsCloudPainter; + +public interface IResetable { - public interface IResetable - { - void Reset(); - } -} + void Reset(); +} \ No newline at end of file diff --git a/TagsCloudPainter/Parser/BoringTextParser.cs b/TagsCloudPainter/Parser/BoringTextParser.cs index 494f967ce..cb958d9a0 100644 --- a/TagsCloudPainter/Parser/BoringTextParser.cs +++ b/TagsCloudPainter/Parser/BoringTextParser.cs @@ -1,33 +1,32 @@ using TagsCloudPainter.Settings; -namespace TagsCloudPainter.Parser +namespace TagsCloudPainter.Parser; + +public class BoringTextParser : ITextParser { - public class BoringTextParser: ITextParser - { - private static readonly string[] _separators = [" ", ". ", ", ", "; ", "-", "—", Environment.NewLine]; - private readonly TextSettings textSettings; + private static readonly string[] _separators = [" ", ". ", ", ", "; ", "-", "—", Environment.NewLine]; + private readonly TextSettings textSettings; - public BoringTextParser(TextSettings textSettings) - { - this.textSettings = textSettings; - } + public BoringTextParser(TextSettings textSettings) + { + this.textSettings = textSettings; + } - public HashSet GetBoringWords(string text) - { - var words = text.Split(Environment.NewLine); - var boringWords = new HashSet(); + public List ParseText(string text) + { + var boringWords = GetBoringWords(textSettings.BoringText); + var words = text.Split(_separators, StringSplitOptions.RemoveEmptyEntries); + return words.Select(word => word.ToLower()).Where(word => !boringWords.Contains(word)).ToList(); + } - foreach (var word in words) - boringWords.Add(word.ToLower()); + public HashSet GetBoringWords(string text) + { + var words = text.Split(Environment.NewLine); + var boringWords = new HashSet(); - return boringWords; - } + foreach (var word in words) + boringWords.Add(word.ToLower()); - public List ParseText(string text) - { - var boringWords = GetBoringWords(textSettings.BoringText); - var words = text.Split(_separators, StringSplitOptions.RemoveEmptyEntries); - return words.Select(word => word.ToLower()).Where(word => !boringWords.Contains(word)).ToList(); - } + return boringWords; } -} +} \ No newline at end of file diff --git a/TagsCloudPainter/Parser/ITextParser.cs b/TagsCloudPainter/Parser/ITextParser.cs index dde53765e..8f5f3bf96 100644 --- a/TagsCloudPainter/Parser/ITextParser.cs +++ b/TagsCloudPainter/Parser/ITextParser.cs @@ -1,7 +1,6 @@ -namespace TagsCloudPainter.Parser +namespace TagsCloudPainter.Parser; + +public interface ITextParser { - public interface ITextParser - { - List ParseText(string text); - } -} + List ParseText(string text); +} \ No newline at end of file diff --git a/TagsCloudPainter/Settings/CloudSettings.cs b/TagsCloudPainter/Settings/CloudSettings.cs index 8474db36b..9e7a181d7 100644 --- a/TagsCloudPainter/Settings/CloudSettings.cs +++ b/TagsCloudPainter/Settings/CloudSettings.cs @@ -1,10 +1,9 @@ using System.Drawing; -namespace TagsCloudPainter.Settings +namespace TagsCloudPainter.Settings; + +public class CloudSettings { - public class CloudSettings - { - public Point CloudCenter { get; set; } - public Color BackgroundColor { get; set; } - } -} + public Point CloudCenter { get; set; } + public Color BackgroundColor { get; set; } +} \ No newline at end of file diff --git a/TagsCloudPainter/Settings/SpiralPointerSettings.cs b/TagsCloudPainter/Settings/SpiralPointerSettings.cs index 560cad2e1..b67b48512 100644 --- a/TagsCloudPainter/Settings/SpiralPointerSettings.cs +++ b/TagsCloudPainter/Settings/SpiralPointerSettings.cs @@ -1,15 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace TagsCloudPainter.Settings; -namespace TagsCloudPainter.Settings +public class SpiralPointerSettings { - public class SpiralPointerSettings - { - public double Step { get; set; } - public double RadiusConst { get; set; } - public double AngleConst { get; set; } - } -} + public double Step { get; set; } + public double RadiusConst { get; set; } + public double AngleConst { get; set; } +} \ No newline at end of file diff --git a/TagsCloudPainter/Settings/TagSettings.cs b/TagsCloudPainter/Settings/TagSettings.cs index 4290f3d0d..c0090cd1e 100644 --- a/TagsCloudPainter/Settings/TagSettings.cs +++ b/TagsCloudPainter/Settings/TagSettings.cs @@ -1,11 +1,10 @@ using System.Drawing; -namespace TagsCloudPainter.Settings +namespace TagsCloudPainter.Settings; + +public class TagSettings { - public class TagSettings - { - public int TagFontSize { get; set; } - public string TagFontName { get; set; } = "Arial"; - public Color TagColor { get; set; } - } -} + public int TagFontSize { get; set; } + public string TagFontName { get; set; } = "Arial"; + public Color TagColor { get; set; } +} \ No newline at end of file diff --git a/TagsCloudPainter/Settings/TextSettings.cs b/TagsCloudPainter/Settings/TextSettings.cs index 7eb1ef324..97eae450a 100644 --- a/TagsCloudPainter/Settings/TextSettings.cs +++ b/TagsCloudPainter/Settings/TextSettings.cs @@ -1,7 +1,6 @@ -namespace TagsCloudPainter.Settings +namespace TagsCloudPainter.Settings; + +public class TextSettings { - public class TextSettings - { - public string BoringText { get; set; } - } -} + public string BoringText { get; set; } +} \ No newline at end of file diff --git a/TagsCloudPainter/Tags/ITagsBuilder.cs b/TagsCloudPainter/Tags/ITagsBuilder.cs index 6eca9ed7d..dab92a984 100644 --- a/TagsCloudPainter/Tags/ITagsBuilder.cs +++ b/TagsCloudPainter/Tags/ITagsBuilder.cs @@ -1,7 +1,6 @@ -namespace TagsCloudPainter.Tags +namespace TagsCloudPainter.Tags; + +public interface ITagsBuilder { - public interface ITagsBuilder - { - public List GetTags(List words); - } -} + public List GetTags(List words); +} \ No newline at end of file diff --git a/TagsCloudPainter/Tags/Tag.cs b/TagsCloudPainter/Tags/Tag.cs index e7e2e19b6..f90b75314 100644 --- a/TagsCloudPainter/Tags/Tag.cs +++ b/TagsCloudPainter/Tags/Tag.cs @@ -1,16 +1,15 @@ -namespace TagsCloudPainter.Tags +namespace TagsCloudPainter.Tags; + +public class Tag { - public class Tag + public Tag(string value, float fontSize, int count) { - public string Value { get; private set; } - public float FontSize { get; private set; } - public int Count { get; private set; } - - public Tag(string value, float fontSize, int count) - { - Value = value; - FontSize = fontSize; - Count = count; - } + Value = value; + FontSize = fontSize; + Count = count; } -} + + public string Value { get; private set; } + public float FontSize { get; private set; } + public int Count { get; private set; } +} \ No newline at end of file diff --git a/TagsCloudPainter/Tags/TagsBuilder.cs b/TagsCloudPainter/Tags/TagsBuilder.cs index f07decb03..dfedeba43 100644 --- a/TagsCloudPainter/Tags/TagsBuilder.cs +++ b/TagsCloudPainter/Tags/TagsBuilder.cs @@ -1,45 +1,44 @@ using TagsCloudPainter.Settings; -namespace TagsCloudPainter.Tags +namespace TagsCloudPainter.Tags; + +public class TagsBuilder : ITagsBuilder { - public class TagsBuilder: ITagsBuilder + private readonly TagSettings _settings; + + public TagsBuilder(TagSettings settings) { - private readonly TagSettings _settings; + _settings = settings; + } - public TagsBuilder(TagSettings settings) - { - _settings = settings; - } + public List GetTags(List words) + { + var countedWords = CountWords(words); + var tags = new List(); - public List GetTags(List words) + foreach (var wordWithCount in countedWords) { - var countedWords = CountWords(words); - var tags = new List(); - - foreach (var wordWithCount in countedWords) - { - var tagFontSize = GetTagFontSize(_settings.TagFontSize, wordWithCount.Value, countedWords.Count); - var tag = new Tag(wordWithCount.Key, tagFontSize, wordWithCount.Value); - tags.Add(tag); - } - - return tags; + var tagFontSize = GetTagFontSize(_settings.TagFontSize, wordWithCount.Value, countedWords.Count); + var tag = new Tag(wordWithCount.Key, tagFontSize, wordWithCount.Value); + tags.Add(tag); } - private static Dictionary CountWords(List words) - { - var countedWords = new Dictionary(); + return tags; + } - foreach (var word in words) - { - if (!countedWords.TryAdd(word, 1)) - countedWords[word] += 1; - } + private static Dictionary CountWords(List words) + { + var countedWords = new Dictionary(); - return countedWords; - } + foreach (var word in words) + if (!countedWords.TryAdd(word, 1)) + countedWords[word] += 1; - private static float GetTagFontSize(int fontSize, int tagCount, int wordsAmount) => - (float)tagCount / wordsAmount * fontSize * 100; + return countedWords; + } + + private static float GetTagFontSize(int fontSize, int tagCount, int wordsAmount) + { + return (float)tagCount / wordsAmount * fontSize * 100; } -} +} \ No newline at end of file diff --git a/TagsCloudPainter/TagsCloud.cs b/TagsCloudPainter/TagsCloud.cs index fab816fc6..db85b7ed7 100644 --- a/TagsCloudPainter/TagsCloud.cs +++ b/TagsCloudPainter/TagsCloud.cs @@ -5,15 +5,15 @@ namespace TagsCloudPainter; public class TagsCloud { - public Point Center { get; private set; } - public Dictionary Tags { get; } - public TagsCloud(Point center, Dictionary tags) { Center = center; Tags = tags ?? []; } + public Point Center { get; private set; } + public Dictionary Tags { get; } + public void AddTag(Tag tag, Rectangle rectangle) { Tags.Add(tag, rectangle); diff --git a/TagsCloudPainter/TagsCloudPainter.csproj b/TagsCloudPainter/TagsCloudPainter.csproj index 1044eebe9..5d7a5062b 100644 --- a/TagsCloudPainter/TagsCloudPainter.csproj +++ b/TagsCloudPainter/TagsCloudPainter.csproj @@ -1,15 +1,15 @@  - - Library - net8.0 - enable - enable - + + Library + net8.0 + enable + enable + - - - - + + + + diff --git a/TagsCloudPainter/Utils/Utils.cs b/TagsCloudPainter/Utils/Utils.cs index 46aa3d925..2bf8c3d22 100644 --- a/TagsCloudPainter/Utils/Utils.cs +++ b/TagsCloudPainter/Utils/Utils.cs @@ -1,33 +1,30 @@ using System.Drawing; -using TagsCloudPainter.Settings; -namespace TagsCloudPainter.Utils +namespace TagsCloudPainter.Utils; + +public static class Utils { - public static class Utils + public static Rectangle GetRectangleFromCenter(Point center, Size size) { + var x = center.X - size.Width / 2; + var y = center.Y - size.Height / 2; - public static Rectangle GetRectangleFromCenter(Point center, Size size) - { - var x = center.X - size.Width / 2; - var y = center.Y - size.Height / 2; - - return new Rectangle(new Point(x, y), size); - } + return new Rectangle(new Point(x, y), size); + } - public static Point GetRectangleCenter(Rectangle rectangle) - { - var x = rectangle.X; - var y = rectangle.Y; - return new Point(x + rectangle.Width / 2, y + rectangle.Height / 2); - } + public static Point GetRectangleCenter(Rectangle rectangle) + { + var x = rectangle.X; + var y = rectangle.Y; + return new Point(x + rectangle.Width / 2, y + rectangle.Height / 2); + } - public static Size GetStringSize(string value, string fontName, float fontSize) - { - using var graphics = Graphics.FromHwnd(IntPtr.Zero); - var font = new Font(fontName, fontSize); - var size = graphics.MeasureString(value, font).ToSize(); + public static Size GetStringSize(string value, string fontName, float fontSize) + { + using var graphics = Graphics.FromHwnd(IntPtr.Zero); + var font = new Font(fontName, fontSize); + var size = graphics.MeasureString(value, font).ToSize(); - return size; - } + return size; } -} +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs index 71aa4646d..7099753dc 100644 --- a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs +++ b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs @@ -7,97 +7,99 @@ using TagsCloudPainterApplication.Infrastructure; using TagsCloudPainterApplication.Infrastructure.Settings; +namespace TagsCloudPainterApplication.Actions; -namespace TagsCloudPainterApplication.Actions +public class DrawTagCloudAction : IUiAction { - public class DrawTagCloudAction : IUiAction + private readonly CloudDrawer cloudDrawer; + private readonly ICloudLayouter cloudLayouter; + private readonly IFileReader fileReader; + private readonly FilesSourceSettings filesSourceSettings; + private readonly IImageHolder imageHolder; + private readonly ImageSettings imageSettings; + private readonly Palette palette; + private readonly ITagsBuilder tagsBuilder; + private readonly TagsCloudSettings tagsCloudSettings; + private readonly ITextParser textParser; + + public DrawTagCloudAction( + ImageSettings imageSettings, + TagsCloudSettings tagsCloudSettings, + FilesSourceSettings filesSourceSettings, + IImageHolder imageHolder, + CloudDrawer cloudDrawer, + ICloudLayouter cloudLayouter, + ITagsBuilder tagsBuilder, + ITextParser textParser, + IFileReader fileReader, + Palette palette) { - private readonly CloudDrawer cloudDrawer; - private readonly ImageSettings imageSettings; - private readonly TagsCloudSettings tagsCloudSettings; - private readonly FilesSourceSettings filesSourceSettings; - private readonly Palette palette; - private readonly IImageHolder imageHolder; - private readonly ICloudLayouter cloudLayouter; - private readonly ITagsBuilder tagsBuilder; - private readonly ITextParser textParser; - private readonly IFileReader fileReader; - public DrawTagCloudAction( - ImageSettings imageSettings, - TagsCloudSettings tagsCloudSettings, - FilesSourceSettings filesSourceSettings, - IImageHolder imageHolder, - CloudDrawer cloudDrawer, - ICloudLayouter cloudLayouter, - ITagsBuilder tagsBuilder, - ITextParser textParser, - IFileReader fileReader, - Palette palette) - { - this.cloudDrawer = cloudDrawer; - this.cloudLayouter = cloudLayouter; - this.tagsBuilder = tagsBuilder; - this.textParser = textParser; - this.fileReader = fileReader; - this.imageSettings = imageSettings; - this.tagsCloudSettings = tagsCloudSettings; - this.imageHolder = imageHolder; - this.filesSourceSettings = filesSourceSettings; - this.palette = palette; - } - public string Category => "Облако тэгов"; + this.cloudDrawer = cloudDrawer; + this.cloudLayouter = cloudLayouter; + this.tagsBuilder = tagsBuilder; + this.textParser = textParser; + this.fileReader = fileReader; + this.imageSettings = imageSettings; + this.tagsCloudSettings = tagsCloudSettings; + this.imageHolder = imageHolder; + this.filesSourceSettings = filesSourceSettings; + this.palette = palette; + } - public string Name => "Нарисовать"; + public string Category => "Облако тэгов"; - public string Description => "Нарисовать облако тэгов"; + public string Name => "Нарисовать"; - public void Perform() - { - var wordsFilePath = GetFilePath(); - SettingsForm.For(tagsCloudSettings).ShowDialog(); - tagsCloudSettings.CloudSettings.BackgroundColor = palette.BackgroundColor; - tagsCloudSettings.TagSettings.TagColor = palette.PrimaryColor; + public string Description => "Нарисовать облако тэгов"; - var wordsText = fileReader.ReadFile(wordsFilePath); - tagsCloudSettings.TextSettings.BoringText = fileReader.ReadFile(filesSourceSettings.BoringTextFilePath); - var parsedWords = textParser.ParseText(wordsText); - var cloud = GetCloud(parsedWords); - DrawCloud(cloud); - } + public void Perform() + { + var wordsFilePath = GetFilePath(); + SettingsForm.For(tagsCloudSettings).ShowDialog(); + tagsCloudSettings.CloudSettings.BackgroundColor = palette.BackgroundColor; + tagsCloudSettings.TagSettings.TagColor = palette.PrimaryColor; - private static string GetFilePath() - { - OpenFileDialog fileDialog = new() - { - InitialDirectory = Environment.CurrentDirectory, - Filter = "Текстовый файл txt (*.txt)|*.txt|Текстовый файл doc (*.doc)|*.doc|Текстовый файл docx (*.docx)|*.docx", - FilterIndex = 0, - RestoreDirectory = true - }; - fileDialog.ShowDialog(); - return fileDialog.FileName; - } + var wordsText = fileReader.ReadFile(wordsFilePath); + tagsCloudSettings.TextSettings.BoringText = fileReader.ReadFile(filesSourceSettings.BoringTextFilePath); + var parsedWords = textParser.ParseText(wordsText); + var cloud = GetCloud(parsedWords); + DrawCloud(cloud); + } - private TagsCloud GetCloud(List words) + private static string GetFilePath() + { + OpenFileDialog fileDialog = new() { - var tags = tagsBuilder.GetTags(words); - cloudLayouter.Reset(); - cloudLayouter.InitializeCloud(); - cloudLayouter.PutTags(tags); - var cloud = cloudLayouter.GetCloud(); - return cloud; - } + InitialDirectory = Environment.CurrentDirectory, + Filter = + "Текстовый файл txt (*.txt)|*.txt|Текстовый файл doc (*.doc)|*.doc|Текстовый файл docx (*.docx)|*.docx", + FilterIndex = 0, + RestoreDirectory = true + }; + fileDialog.ShowDialog(); + return fileDialog.FileName; + } - private void DrawCloud(TagsCloud cloud) - { - var bitmap = cloudDrawer.DrawCloud(cloud, imageSettings.Width, imageSettings.Height); - imageHolder.RecreateImage(imageSettings); + private TagsCloud GetCloud(List words) + { + var tags = tagsBuilder.GetTags(words); + cloudLayouter.Reset(); + cloudLayouter.InitializeCloud(); + cloudLayouter.PutTags(tags); + var cloud = cloudLayouter.GetCloud(); + return cloud; + } + + private void DrawCloud(TagsCloud cloud) + { + var bitmap = cloudDrawer.DrawCloud(cloud, imageSettings.Width, imageSettings.Height); + imageHolder.RecreateImage(imageSettings); - using (var graphics = imageHolder.StartDrawing()) - { - graphics.DrawImage(bitmap, new Point(0, 0)); - } - imageHolder.UpdateUi(); + using (var graphics = imageHolder.StartDrawing()) + { + graphics.DrawImage(bitmap, new Point(0, 0)); } + + imageHolder.UpdateUi(); } -} +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Actions/FileSourceSettingsAction.cs b/TagsCloudPainterApplication/Actions/FileSourceSettingsAction.cs index b3448f953..9e12051bb 100644 --- a/TagsCloudPainterApplication/Actions/FileSourceSettingsAction.cs +++ b/TagsCloudPainterApplication/Actions/FileSourceSettingsAction.cs @@ -1,23 +1,22 @@ using TagsCloudPainterApplication.Infrastructure.Settings; -namespace TagsCloudPainterApplication.Actions +namespace TagsCloudPainterApplication.Actions; + +public class FileSourceSettingsAction : IUiAction { - public class FileSourceSettingsAction: IUiAction - { - private readonly FilesSourceSettings filesSourceSettings; + private readonly FilesSourceSettings filesSourceSettings; - public FileSourceSettingsAction(FilesSourceSettings filesSourceSettings) - { - this.filesSourceSettings = filesSourceSettings; - } + public FileSourceSettingsAction(FilesSourceSettings filesSourceSettings) + { + this.filesSourceSettings = filesSourceSettings; + } - public string Category => "Настройки"; - public string Name => "Ресурсы"; - public string Description => "Укажите ресурсы"; + public string Category => "Настройки"; + public string Name => "Ресурсы"; + public string Description => "Укажите ресурсы"; - public void Perform() - { - SettingsForm.For(filesSourceSettings).ShowDialog(); - } + public void Perform() + { + SettingsForm.For(filesSourceSettings).ShowDialog(); } -} +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Actions/IUiAction.cs b/TagsCloudPainterApplication/Actions/IUiAction.cs index 3128eb94f..870b077fd 100644 --- a/TagsCloudPainterApplication/Actions/IUiAction.cs +++ b/TagsCloudPainterApplication/Actions/IUiAction.cs @@ -1,10 +1,9 @@ -namespace TagsCloudPainterApplication.Actions +namespace TagsCloudPainterApplication.Actions; + +public interface IUiAction { - public interface IUiAction - { - string Category { get; } - string Name { get; } - string Description { get; } - void Perform(); - } -} + string Category { get; } + string Name { get; } + string Description { get; } + void Perform(); +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Actions/ImageSettingsAction.cs b/TagsCloudPainterApplication/Actions/ImageSettingsAction.cs index d58822523..59c3a270e 100644 --- a/TagsCloudPainterApplication/Actions/ImageSettingsAction.cs +++ b/TagsCloudPainterApplication/Actions/ImageSettingsAction.cs @@ -1,23 +1,22 @@ using TagsCloudPainterApplication.Infrastructure.Settings; -namespace TagsCloudPainterApplication.Actions +namespace TagsCloudPainterApplication.Actions; + +public class ImageSettingsAction : IUiAction { - public class ImageSettingsAction: IUiAction - { - private readonly ImageSettings imageSettings; + private readonly ImageSettings imageSettings; - public ImageSettingsAction(ImageSettings imageSettings) - { - this.imageSettings = imageSettings; - } + public ImageSettingsAction(ImageSettings imageSettings) + { + this.imageSettings = imageSettings; + } - public string Category => "Настройки"; - public string Name => "Изображение"; - public string Description => "Укажите размер изображения"; + public string Category => "Настройки"; + public string Name => "Изображение"; + public string Description => "Укажите размер изображения"; - public void Perform() - { - SettingsForm.For(imageSettings).ShowDialog(); - } + public void Perform() + { + SettingsForm.For(imageSettings).ShowDialog(); } -} +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Actions/PaletteSettingsAction.cs b/TagsCloudPainterApplication/Actions/PaletteSettingsAction.cs index 0849fd495..30fc4ba19 100644 --- a/TagsCloudPainterApplication/Actions/PaletteSettingsAction.cs +++ b/TagsCloudPainterApplication/Actions/PaletteSettingsAction.cs @@ -1,24 +1,23 @@ using TagsCloudPainterApplication.Infrastructure; using TagsCloudPainterApplication.Infrastructure.Settings; -namespace TagsCloudPainterApplication.Actions +namespace TagsCloudPainterApplication.Actions; + +public class PaletteSettingsAction : IUiAction { - public class PaletteSettingsAction : IUiAction - { - private readonly Palette palette; + private readonly Palette palette; - public PaletteSettingsAction(Palette palette) - { - this.palette = palette; - } + public PaletteSettingsAction(Palette palette) + { + this.palette = palette; + } - public string Category => "Настройки"; - public string Name => "Палитра..."; - public string Description => "Цвета для рисования облака тэгов"; + public string Category => "Настройки"; + public string Name => "Палитра..."; + public string Description => "Цвета для рисования облака тэгов"; - public void Perform() - { - SettingsForm.For(palette).ShowDialog(); - } + public void Perform() + { + SettingsForm.For(palette).ShowDialog(); } -} +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Actions/SaveImageAction.cs b/TagsCloudPainterApplication/Actions/SaveImageAction.cs index 62c4add7c..ccef3a1e4 100644 --- a/TagsCloudPainterApplication/Actions/SaveImageAction.cs +++ b/TagsCloudPainterApplication/Actions/SaveImageAction.cs @@ -1,33 +1,32 @@ using TagsCloudPainterApplication.Infrastructure; -namespace TagsCloudPainterApplication.Actions +namespace TagsCloudPainterApplication.Actions; + +public class SaveImageAction : IUiAction { - public class SaveImageAction : IUiAction - { - private readonly IImageHolder imageHolder; + private readonly IImageHolder imageHolder; - public SaveImageAction(IImageHolder imageHolder) - { - this.imageHolder = imageHolder; - } + public SaveImageAction(IImageHolder imageHolder) + { + this.imageHolder = imageHolder; + } - public string Category => "Файл"; - public string Name => "Сохранить..."; - public string Description => "Сохранить изображение в файл"; + public string Category => "Файл"; + public string Name => "Сохранить..."; + public string Description => "Сохранить изображение в файл"; - public void Perform() + public void Perform() + { + var dialog = new SaveFileDialog { - var dialog = new SaveFileDialog - { - CheckFileExists = false, - InitialDirectory = Path.GetFullPath(Environment.CurrentDirectory), - DefaultExt = "png", - FileName = "image.png", - Filter = "Изображения (*.png)|*.png|Изображения (*.jpeg)|*.jpeg" - }; - var res = dialog.ShowDialog(); - if (res == DialogResult.OK) - imageHolder.SaveImage(dialog.FileName); - } + CheckFileExists = false, + InitialDirectory = Path.GetFullPath(Environment.CurrentDirectory), + DefaultExt = "png", + FileName = "image.png", + Filter = "Изображения (*.png)|*.png|Изображения (*.jpeg)|*.jpeg" + }; + var res = dialog.ShowDialog(); + if (res == DialogResult.OK) + imageHolder.SaveImage(dialog.FileName); } -} +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Actions/UiActionExtensions.cs b/TagsCloudPainterApplication/Actions/UiActionExtensions.cs index 4027f925f..93c3f1e27 100644 --- a/TagsCloudPainterApplication/Actions/UiActionExtensions.cs +++ b/TagsCloudPainterApplication/Actions/UiActionExtensions.cs @@ -1,34 +1,29 @@ -using System.Collections.Generic; -using System.Linq; -using System.Windows.Forms; +namespace TagsCloudPainterApplication.Actions; -namespace TagsCloudPainterApplication.Actions +public static class UiActionExtensions { - public static class UiActionExtensions + public static ToolStripItem[] ToMenuItems(this IUiAction[] actions) { - public static ToolStripItem[] ToMenuItems(this IUiAction[] actions) - { - var items = actions.GroupBy(a => a.Category) - .Select(g => CreateTopLevelMenuItem(g.Key, g.ToList())) - .Cast() - .ToArray(); - return items; - } + var items = actions.GroupBy(a => a.Category) + .Select(g => CreateTopLevelMenuItem(g.Key, g.ToList())) + .Cast() + .ToArray(); + return items; + } - private static ToolStripMenuItem CreateTopLevelMenuItem(string name, IList items) - { - var menuItems = items.Select(a => a.ToMenuItem()).ToArray(); - return new ToolStripMenuItem(name, null, menuItems); - } + private static ToolStripMenuItem CreateTopLevelMenuItem(string name, IList items) + { + var menuItems = items.Select(a => a.ToMenuItem()).ToArray(); + return new ToolStripMenuItem(name, null, menuItems); + } - public static ToolStripItem ToMenuItem(this IUiAction action) - { - return - new ToolStripMenuItem(action.Name, null, (sender, args) => action.Perform()) - { - ToolTipText = action.Description, - Tag = action - }; - } + public static ToolStripItem ToMenuItem(this IUiAction action) + { + return + new ToolStripMenuItem(action.Name, null, (sender, args) => action.Perform()) + { + ToolTipText = action.Description, + Tag = action + }; } -} +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/App.config b/TagsCloudPainterApplication/App.config index b46b4d3a1..9ec7d364a 100644 --- a/TagsCloudPainterApplication/App.config +++ b/TagsCloudPainterApplication/App.config @@ -1,18 +1,18 @@ - + - - + + - - - - - - - - - - - - - + + + + + + + + + + + + + \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/IImageHolder.cs b/TagsCloudPainterApplication/Infrastructure/IImageHolder.cs index a9ee2c23f..cebf9c09a 100644 --- a/TagsCloudPainterApplication/Infrastructure/IImageHolder.cs +++ b/TagsCloudPainterApplication/Infrastructure/IImageHolder.cs @@ -1,13 +1,12 @@ using TagsCloudPainterApplication.Infrastructure.Settings; -namespace TagsCloudPainterApplication.Infrastructure +namespace TagsCloudPainterApplication.Infrastructure; + +public interface IImageHolder { - public interface IImageHolder - { - Size GetImageSize(); - Graphics StartDrawing(); - void UpdateUi(); - void RecreateImage(ImageSettings settings); - void SaveImage(string fileName); - } + Size GetImageSize(); + Graphics StartDrawing(); + void UpdateUi(); + void RecreateImage(ImageSettings settings); + void SaveImage(string fileName); } \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/Palette.cs b/TagsCloudPainterApplication/Infrastructure/Palette.cs index ad6c6603b..c5974ca60 100644 --- a/TagsCloudPainterApplication/Infrastructure/Palette.cs +++ b/TagsCloudPainterApplication/Infrastructure/Palette.cs @@ -1,9 +1,8 @@ -namespace TagsCloudPainterApplication.Infrastructure +namespace TagsCloudPainterApplication.Infrastructure; + +public class Palette { - public class Palette - { - public Color PrimaryColor { get; set; } = Color.Yellow; - public Color SecondaryColor { get; set; } = Color.Red; - public Color BackgroundColor { get; set; } = Color.DarkBlue; - } -} + public Color PrimaryColor { get; set; } = Color.Yellow; + public Color SecondaryColor { get; set; } = Color.Red; + public Color BackgroundColor { get; set; } = Color.DarkBlue; +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs index ddbdab326..4ce3643f9 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs @@ -1,7 +1,6 @@ -namespace TagsCloudPainterApplication.Infrastructure.Settings +namespace TagsCloudPainterApplication.Infrastructure.Settings; + +public class FilesSourceSettings { - public class FilesSourceSettings - { - public string BoringTextFilePath { get; set; } = @$"{Environment.CurrentDirectory}..\..\..\..\Data\BoringWords.txt"; - } -} + public string BoringTextFilePath { get; set; } = @$"{Environment.CurrentDirectory}..\..\..\..\Data\BoringWords.txt"; +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs index 0f499238f..cd795ac58 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs @@ -1,8 +1,7 @@ -namespace TagsCloudPainterApplication.Infrastructure.Settings +namespace TagsCloudPainterApplication.Infrastructure.Settings; + +public class ImageSettings { - public class ImageSettings - { - public int Width { get; set; } = 800; - public int Height { get; set; } = 600; - } -} + public int Width { get; set; } = 800; + public int Height { get; set; } = 600; +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/SettingsForm.cs b/TagsCloudPainterApplication/Infrastructure/Settings/SettingsForm.cs index 1d8e6303d..c49ec7b21 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/SettingsForm.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/SettingsForm.cs @@ -1,36 +1,35 @@ -namespace TagsCloudPainterApplication.Infrastructure.Settings +namespace TagsCloudPainterApplication.Infrastructure.Settings; + +public static class SettingsForm { - public static class SettingsForm + public static SettingsForm For(TSettings settings) { - public static SettingsForm For(TSettings settings) - { - return new SettingsForm(settings); - } + return new SettingsForm(settings); } +} - public class SettingsForm : Form +public class SettingsForm : Form +{ + public SettingsForm(TSettings settings) { - public SettingsForm(TSettings settings) + var okButton = new Button { - var okButton = new Button - { - Text = "OK", - DialogResult = DialogResult.OK, - Dock = DockStyle.Bottom, - }; - Controls.Add(okButton); - Controls.Add(new PropertyGrid - { - SelectedObject = settings, - Dock = DockStyle.Fill - }); - AcceptButton = okButton; - } - - protected override void OnLoad(EventArgs e) + Text = "OK", + DialogResult = DialogResult.OK, + Dock = DockStyle.Bottom + }; + Controls.Add(okButton); + Controls.Add(new PropertyGrid { - base.OnLoad(e); - Text = "Настройки"; - } + SelectedObject = settings, + Dock = DockStyle.Fill + }); + AcceptButton = okButton; } -} + + protected override void OnLoad(EventArgs e) + { + base.OnLoad(e); + Text = "Настройки"; + } +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs index 9df3ac543..1c2760f98 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs @@ -1,61 +1,66 @@ using System.ComponentModel; using TagsCloudPainter.Settings; -namespace TagsCloudPainterApplication.Infrastructure.Settings +namespace TagsCloudPainterApplication.Infrastructure.Settings; + +public class TagsCloudSettings { - public class TagsCloudSettings + public TagsCloudSettings( + CloudSettings cloudSettings, + TagSettings tagSettings, + SpiralPointerSettings spiralPointerSettings, + TextSettings textSettings) + { + CloudSettings = cloudSettings; + TagSettings = tagSettings; + SpiralPointerSettings = spiralPointerSettings; + TextSettings = textSettings; + TagFontSize = 32; + TagFontName = "Arial"; + CloudCenter = new Point(0, 0); + PointerStep = 0.1; + PointerRadiusConst = 0.5; + PointerAngleConst = 1; + } + + [Browsable(false)] public CloudSettings CloudSettings { get; } + [Browsable(false)] public TagSettings TagSettings { get; } + [Browsable(false)] public SpiralPointerSettings SpiralPointerSettings { get; } + [Browsable(false)] public TextSettings TextSettings { get; private set; } + + public int TagFontSize + { + get => TagSettings.TagFontSize; + set => TagSettings.TagFontSize = value; + } + + public string TagFontName + { + get => TagSettings.TagFontName; + set => TagSettings.TagFontName = value; + } + + public Point CloudCenter + { + get => CloudSettings.CloudCenter; + set => CloudSettings.CloudCenter = value; + } + + public double PointerStep + { + get => SpiralPointerSettings.Step; + set => SpiralPointerSettings.Step = value; + } + + public double PointerRadiusConst + { + get => SpiralPointerSettings.RadiusConst; + set => SpiralPointerSettings.RadiusConst = value; + } + + public double PointerAngleConst { - [Browsable(false)] public CloudSettings CloudSettings { get; private set; } - [Browsable(false)] public TagSettings TagSettings { get; private set; } - [Browsable(false)] public SpiralPointerSettings SpiralPointerSettings { get; private set; } - [Browsable(false)] public TextSettings TextSettings { get; private set; } - public int TagFontSize - { - get => TagSettings.TagFontSize; - set => TagSettings.TagFontSize = value; - } - public string TagFontName - { - get => TagSettings.TagFontName; - set => TagSettings.TagFontName = value; - } - public Point CloudCenter - { - get => CloudSettings.CloudCenter; - set => CloudSettings.CloudCenter = value; - } - public double PointerStep - { - get => SpiralPointerSettings.Step; - set => SpiralPointerSettings.Step = value; - } - public double PointerRadiusConst - { - get => SpiralPointerSettings.RadiusConst; - set => SpiralPointerSettings.RadiusConst = value; - } - public double PointerAngleConst - { - get => SpiralPointerSettings.AngleConst; - set => SpiralPointerSettings.AngleConst = value; - } - - public TagsCloudSettings( - CloudSettings cloudSettings, - TagSettings tagSettings, - SpiralPointerSettings spiralPointerSettings, - TextSettings textSettings) - { - CloudSettings = cloudSettings; - TagSettings = tagSettings; - SpiralPointerSettings = spiralPointerSettings; - TextSettings = textSettings; - TagFontSize = 32; - TagFontName = "Arial"; - CloudCenter = new Point(0, 0); - PointerStep = 0.1; - PointerRadiusConst = 0.5; - PointerAngleConst = 1; - } + get => SpiralPointerSettings.AngleConst; + set => SpiralPointerSettings.AngleConst = value; } -} +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/MainForm.cs b/TagsCloudPainterApplication/MainForm.cs index eeb702b94..09302f7ed 100644 --- a/TagsCloudPainterApplication/MainForm.cs +++ b/TagsCloudPainterApplication/MainForm.cs @@ -1,21 +1,20 @@ using TagsCloudPainterApplication.Actions; using TagsCloudPainterApplication.Infrastructure.Settings; -namespace TagsCloudPainterApplication +namespace TagsCloudPainterApplication; + +public partial class MainForm : Form { - public partial class MainForm : Form + public MainForm(IUiAction[] actions, ImageSettings imageSettings, PictureBoxImageHolder pictureBox) { - public MainForm(IUiAction[] actions, ImageSettings imageSettings, PictureBoxImageHolder pictureBox) - { - ClientSize = new Size(imageSettings.Width, imageSettings.Height); + ClientSize = new Size(imageSettings.Width, imageSettings.Height); - var mainMenu = new MenuStrip(); - mainMenu.Items.AddRange(actions.ToMenuItems()); - Controls.Add(mainMenu); + var mainMenu = new MenuStrip(); + mainMenu.Items.AddRange(actions.ToMenuItems()); + Controls.Add(mainMenu); - pictureBox.RecreateImage(imageSettings); - pictureBox.Dock = DockStyle.Fill; - Controls.Add(pictureBox); - } + pictureBox.RecreateImage(imageSettings); + pictureBox.Dock = DockStyle.Fill; + Controls.Add(pictureBox); } -} +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/MainForm.resx b/TagsCloudPainterApplication/MainForm.resx index 1af7de150..0ff5d7eaa 100644 --- a/TagsCloudPainterApplication/MainForm.resx +++ b/TagsCloudPainterApplication/MainForm.resx @@ -1,120 +1,125 @@  - - - - - - - + + + + - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/TagsCloudPainterApplication/PictureBoxImageHolder.cs b/TagsCloudPainterApplication/PictureBoxImageHolder.cs index 745a195be..6fe98af37 100644 --- a/TagsCloudPainterApplication/PictureBoxImageHolder.cs +++ b/TagsCloudPainterApplication/PictureBoxImageHolder.cs @@ -2,44 +2,42 @@ using TagsCloudPainterApplication.Infrastructure; using TagsCloudPainterApplication.Infrastructure.Settings; -namespace TagsCloudPainterApplication +namespace TagsCloudPainterApplication; +public class PictureBoxImageHolder : PictureBox, IImageHolder { - public class PictureBoxImageHolder : PictureBox, IImageHolder + public Size GetImageSize() { - public Size GetImageSize() - { - FailIfNotInitialized(); - return Image.Size; - } + FailIfNotInitialized(); + return Image.Size; + } - public Graphics StartDrawing() - { - FailIfNotInitialized(); - return Graphics.FromImage(Image); - } + public Graphics StartDrawing() + { + FailIfNotInitialized(); + return Graphics.FromImage(Image); + } - private void FailIfNotInitialized() - { - if (Image == null) - throw new InvalidOperationException("Call PictureBoxImageHolder.RecreateImage before other method call!"); - } + public void UpdateUi() + { + Refresh(); + Application.DoEvents(); + } - public void UpdateUi() - { - Refresh(); - Application.DoEvents(); - } + public void RecreateImage(ImageSettings imageSettings) + { + Image = new Bitmap(imageSettings.Width, imageSettings.Height, PixelFormat.Format24bppRgb); + } - public void RecreateImage(ImageSettings imageSettings) - { - Image = new Bitmap(imageSettings.Width, imageSettings.Height, PixelFormat.Format24bppRgb); - } + public void SaveImage(string fileName) + { + FailIfNotInitialized(); + Image.Save(fileName); + } - public void SaveImage(string fileName) - { - FailIfNotInitialized(); - Image.Save(fileName); - } + private void FailIfNotInitialized() + { + if (Image == null) + throw new InvalidOperationException("Call PictureBoxImageHolder.RecreateImage before other method call!"); } } \ No newline at end of file diff --git a/TagsCloudPainterApplication/Program.cs b/TagsCloudPainterApplication/Program.cs index 165b4a9f0..98abab68b 100644 --- a/TagsCloudPainterApplication/Program.cs +++ b/TagsCloudPainterApplication/Program.cs @@ -10,44 +10,43 @@ using TagsCloudPainterApplication.Infrastructure; using TagsCloudPainterApplication.Infrastructure.Settings; -namespace TagsCloudPainterApplication +namespace TagsCloudPainterApplication; + +internal static class Program { - internal static class Program + /// + /// The main entry point for the application. + /// + [STAThread] + private static void Main() { - /// - /// The main entry point for the application. - /// - [STAThread] - static void Main() - { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - var builder = new ContainerBuilder(); + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + var builder = new ContainerBuilder(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().As().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().As(); - builder.RegisterType().As(); - builder.RegisterType().As().SingleInstance(); - builder.RegisterType().As().SingleInstance(); - builder.RegisterType().As().SingleInstance(); - builder.RegisterType().As(); - builder.RegisterType().As(); - builder.RegisterType().As(); - builder.RegisterType().As(); - builder.RegisterType().As(); - builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().AsSelf(); - var container = builder.Build(); - Application.Run(container.Resolve()); - } + var container = builder.Build(); + Application.Run(container.Resolve()); } } \ No newline at end of file diff --git a/TagsCloudPainterApplication/Properties/Resources.resx b/TagsCloudPainterApplication/Properties/Resources.resx index af7dbebba..668cdff93 100644 --- a/TagsCloudPainterApplication/Properties/Resources.resx +++ b/TagsCloudPainterApplication/Properties/Resources.resx @@ -1,117 +1,122 @@  - - - - - - + + + - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/TagsCloudPainterApplication/Properties/Settings.Designer.cs b/TagsCloudPainterApplication/Properties/Settings.Designer.cs index 80a3c58a6..4d692949b 100644 --- a/TagsCloudPainterApplication/Properties/Settings.Designer.cs +++ b/TagsCloudPainterApplication/Properties/Settings.Designer.cs @@ -1,10 +1,9 @@ //------------------------------------------------------------------------------ // -// Этот код создан программой. -// Исполняемая версия:4.0.30319.42000 +// This code was generated by a tool. // -// Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае -// повторной генерации кода. +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. // //------------------------------------------------------------------------------ @@ -12,7 +11,7 @@ namespace TagsCloudPainterApplication.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.8.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.9.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); diff --git a/TagsCloudPainterApplication/Properties/Settings.settings b/TagsCloudPainterApplication/Properties/Settings.settings index 39645652a..796d34b5e 100644 --- a/TagsCloudPainterApplication/Properties/Settings.settings +++ b/TagsCloudPainterApplication/Properties/Settings.settings @@ -1,7 +1,7 @@  - - - - + + + + diff --git a/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj b/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj index 6e9991cc3..014a2d8b2 100644 --- a/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj +++ b/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj @@ -1,42 +1,42 @@  - - WinExe - net8.0-windows - enable - true - enable - false - + + WinExe + net8.0-windows + enable + true + enable + false + - - - + + + - - - + + + - - - Component - - - True - True - Settings.settings - - + + + Component + + + True + True + Settings.settings + + - - - SettingsSingleFileGenerator - Settings.Designer.cs - - + + + SettingsSingleFileGenerator + Settings.Designer.cs + + - - - + + + \ No newline at end of file diff --git a/TagsCloudPainterApplication/packages.config b/TagsCloudPainterApplication/packages.config index 8411f2587..c2bcb1ec2 100644 --- a/TagsCloudPainterApplication/packages.config +++ b/TagsCloudPainterApplication/packages.config @@ -1,11 +1,11 @@  - - - - - - - - + + + + + + + + \ No newline at end of file diff --git a/Tests/ArchimedeanSpiralTests.cs b/Tests/ArchimedeanSpiralTests.cs index ccde7956b..dd70be92e 100644 --- a/Tests/ArchimedeanSpiralTests.cs +++ b/Tests/ArchimedeanSpiralTests.cs @@ -2,24 +2,23 @@ using TagsCloudPainter.FormPointer; using TagsCloudPainter.Settings; -namespace TagsCloudPainterTests +namespace TagsCloudPainterTests; + +[TestFixture] +public class ArchimedeanSpiralTests { - [TestFixture] - public class ArchimedeanSpiralTests + private static IEnumerable ConstructorArgumentException => new[] { - private static IEnumerable ConstructorArgumentException => new[] - { new TestCaseData(new Point(1, 1), 0, 1, 1).SetName("WhenGivenNotPositiveStep"), new TestCaseData(new Point(1, 1), 1, 0, 1).SetName("WhenGivenNotPositiveRadius"), new TestCaseData(new Point(1, 1), 1, 1, 0).SetName("WhenGivenNotPositiveAngle") - }; + }; - [TestCaseSource(nameof(ConstructorArgumentException))] - public void Constructor_ShouldThrowArgumentException(Point center, double step, double radius, double angle) - { - var cloudSettings = new CloudSettings() { CloudCenter = center }; - var pointerSettings = new SpiralPointerSettings() { AngleConst = angle, RadiusConst = radius, Step = step }; - Assert.Throws(() => new ArchimedeanSpiralPointer(cloudSettings, pointerSettings)); - } + [TestCaseSource(nameof(ConstructorArgumentException))] + public void Constructor_ShouldThrowArgumentException(Point center, double step, double radius, double angle) + { + var cloudSettings = new CloudSettings { CloudCenter = center }; + var pointerSettings = new SpiralPointerSettings { AngleConst = angle, RadiusConst = radius, Step = step }; + Assert.Throws(() => new ArchimedeanSpiralPointer(cloudSettings, pointerSettings)); } -} +} \ No newline at end of file diff --git a/Tests/BoringTextParserTests.cs b/Tests/BoringTextParserTests.cs index d6349abcc..6d589a222 100644 --- a/Tests/BoringTextParserTests.cs +++ b/Tests/BoringTextParserTests.cs @@ -3,60 +3,60 @@ using TagsCloudPainter.Parser; using TagsCloudPainter.Settings; -namespace TagsCloudPainterTests +namespace TagsCloudPainterTests; + +[TestFixture] +public class BoringTextParserTests { - [TestFixture] - public class BoringTextParserTests + [SetUp] + public void Setup() { - private TextSettings textSettings; - private BoringTextParser boringTextParser; - private TextFileReader textFileReader; + textFileReader = new TextFileReader(); + var boringText = textFileReader + .ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\boringWords.txt"); + textSettings = new TextSettings { BoringText = boringText }; + boringTextParser = new BoringTextParser(textSettings); + } - [SetUp] - public void Setup() - { - textFileReader = new TextFileReader(); - var boringText = textFileReader - .ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\boringWords.txt"); - textSettings = new TextSettings() { BoringText = boringText}; - boringTextParser = new BoringTextParser(textSettings); - } + private TextSettings textSettings; + private BoringTextParser boringTextParser; + private TextFileReader textFileReader; - [Test] - public void ParseText_ShouldReturnWordsListWithoutBoringWords() - { - var boringWords = boringTextParser - .GetBoringWords(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\boringWords.txt")) - .ToHashSet(); - var parsedText = boringTextParser - .ParseText(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt")); - var isBoringWordsInParsedText = parsedText.Where(boringWords.Contains).Any(); - isBoringWordsInParsedText.Should().BeFalse(); - } + [Test] + public void ParseText_ShouldReturnWordsListWithoutBoringWords() + { + var boringWords = boringTextParser + .GetBoringWords( + textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\boringWords.txt")) + .ToHashSet(); + var parsedText = boringTextParser + .ParseText(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt")); + var isBoringWordsInParsedText = parsedText.Where(boringWords.Contains).Any(); + isBoringWordsInParsedText.Should().BeFalse(); + } - [Test] - public void ParseText_ShouldReturnNotEmptyWordsList_WhenPassedNotEmptyText() - { - var parsedText = boringTextParser - .ParseText(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt")); - parsedText.Count.Should().BeGreaterThan(0); - } + [Test] + public void ParseText_ShouldReturnNotEmptyWordsList_WhenPassedNotEmptyText() + { + var parsedText = boringTextParser + .ParseText(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt")); + parsedText.Count.Should().BeGreaterThan(0); + } - [Test] - public void ParseText_ShouldReturnWordsInLowerCase() - { - var parsedText = boringTextParser - .ParseText(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt")); - var isAnyWordNotLowered = parsedText.Where(word => word.ToLower() != word).Any(); - isAnyWordNotLowered.Should().BeFalse(); - } + [Test] + public void ParseText_ShouldReturnWordsInLowerCase() + { + var parsedText = boringTextParser + .ParseText(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt")); + var isAnyWordNotLowered = parsedText.Where(word => word.ToLower() != word).Any(); + isAnyWordNotLowered.Should().BeFalse(); + } - [Test] - public void ParseText_ShouldReturnWordsListWithTheSameAmountAsInText() - { - var parsedText = boringTextParser - .ParseText(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt")); - parsedText.Count.Should().Be(20); - } + [Test] + public void ParseText_ShouldReturnWordsListWithTheSameAmountAsInText() + { + var parsedText = boringTextParser + .ParseText(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt")); + parsedText.Count.Should().Be(20); } -} +} \ No newline at end of file diff --git a/Tests/CloudDrawerTests.cs b/Tests/CloudDrawerTests.cs index 17ccbf910..82c18c706 100644 --- a/Tests/CloudDrawerTests.cs +++ b/Tests/CloudDrawerTests.cs @@ -4,58 +4,58 @@ using TagsCloudPainter.Settings; using TagsCloudPainter.Tags; -namespace TagsCloudPainterTests +namespace TagsCloudPainterTests; + +[TestFixture] +public class CloudDrawerTests { - [TestFixture] - public class CloudDrawerTests + [SetUp] + public void Setup() { - private CloudDrawer drawer; + var cloudSettings = new CloudSettings { CloudCenter = new Point(0, 0), BackgroundColor = Color.White }; + var tagSettings = new TagSettings { TagFontSize = 32, TagColor = Color.Black }; + drawer = new CloudDrawer(tagSettings, cloudSettings); + } - [SetUp] - public void Setup() - { - var cloudSettings = new CloudSettings() { CloudCenter = new Point(0, 0), BackgroundColor = Color.White }; - var tagSettings = new TagSettings() { TagFontSize = 32, TagColor = Color.Black }; - drawer = new CloudDrawer(tagSettings, cloudSettings); - } + private CloudDrawer drawer; - private static IEnumerable DrawArgumentException => new[] - { + private static IEnumerable DrawArgumentException => new[] + { new TestCaseData(new TagsCloud(new Point(0, 0), - new Dictionary{ { new Tag("a", 1, 1), new (1, 1, 1, 1) } }), 0, 1) + new Dictionary { { new Tag("a", 1, 1), new(1, 1, 1, 1) } }), 0, 1) .SetName("WhenGivenNotPositiveImageWidth"), - new TestCaseData(new TagsCloud(new Point(0, 0), - new Dictionary{ { new Tag("a", 1, 1), new (1, 1, 1, 1) } }), 1, 0) + new TestCaseData(new TagsCloud(new Point(0, 0), + new Dictionary { { new Tag("a", 1, 1), new(1, 1, 1, 1) } }), 1, 0) .SetName("WhenGivenNotPositiveImageHeight"), - new TestCaseData(new TagsCloud(new Point(0, 0), - new Dictionary{ {new Tag("a", 1, 1), new(1, 1, 1, 1) } }), 0, 0) + new TestCaseData(new TagsCloud(new Point(0, 0), + new Dictionary { { new Tag("a", 1, 1), new(1, 1, 1, 1) } }), 0, 0) .SetName("WhenGivenNotPositiveImageHeightAndWidth"), new TestCaseData(new TagsCloud(new Point(0, 0), new Dictionary()), 1, 1) .SetName("WhenGivenCloudWithEmptyTagsDictionary") - }; + }; - [TestCaseSource(nameof(DrawArgumentException))] - public void Draw_ShouldThrowArgumentException(TagsCloud cloud, int width, int height) - { - Assert.Throws(() => drawer.DrawCloud(cloud, width, height)); - } - private static IEnumerable DrawNoException => new[] - { - new TestCaseData(new TagsCloud(new Point(5, 5), - new Dictionary{ { new Tag("abcdadg", 10, 1), new(5, 5, 20, 3) } }), 10, 10) + [TestCaseSource(nameof(DrawArgumentException))] + public void Draw_ShouldThrowArgumentException(TagsCloud cloud, int width, int height) + { + Assert.Throws(() => drawer.DrawCloud(cloud, width, height)); + } + + private static IEnumerable DrawNoException => new[] + { + new TestCaseData(new TagsCloud(new Point(5, 5), + new Dictionary { { new Tag("abcdadg", 10, 1), new(5, 5, 20, 3) } }), 10, 10) .SetName("WhenCloudWidthIsGreaterThanImageWidth"), - new TestCaseData(new TagsCloud(new Point(5, 5), - new Dictionary{ { new Tag("abcdadg", 10, 1), new(5, 5, 3, 20) } }), 10, 10) + new TestCaseData(new TagsCloud(new Point(5, 5), + new Dictionary { { new Tag("abcdadg", 10, 1), new(5, 5, 3, 20) } }), 10, 10) .SetName("WhenCloudHeightIsGreaterThanImageHeight"), - new TestCaseData(new TagsCloud(new Point(5, 5), - new Dictionary{ { new Tag("abcdadg", 10, 1), new(5, 5, 20, 20) } }), 10, 10) + new TestCaseData(new TagsCloud(new Point(5, 5), + new Dictionary { { new Tag("abcdadg", 10, 1), new(5, 5, 20, 20) } }), 10, 10) .SetName("WhenCloudIsBiggerThanImage") - }; + }; - [TestCaseSource(nameof(DrawNoException))] - public void Draw_ShouldNotThrow(TagsCloud cloud, int width, int height) - { - Assert.DoesNotThrow(() => drawer.DrawCloud(cloud, width, height)); - } + [TestCaseSource(nameof(DrawNoException))] + public void Draw_ShouldNotThrow(TagsCloud cloud, int width, int height) + { + Assert.DoesNotThrow(() => drawer.DrawCloud(cloud, width, height)); } -} +} \ No newline at end of file diff --git a/Tests/TagsBuilderTests.cs b/Tests/TagsBuilderTests.cs index 8c7a94d33..606f3bb1c 100644 --- a/Tests/TagsBuilderTests.cs +++ b/Tests/TagsBuilderTests.cs @@ -2,45 +2,44 @@ using TagsCloudPainter.Settings; using TagsCloudPainter.Tags; -namespace TagsCloudPainterTests +namespace TagsCloudPainterTests; + +[TestFixture] +public class TagsBuilderTests { - [TestFixture] - public class TagsBuilderTests + [SetUp] + public void Setup() + { + var tagSettings = new TagSettings { TagFontSize = 32 }; + tagsBuilder = new TagsBuilder(tagSettings); + } + + private TagsBuilder tagsBuilder; + + [Test] + public void GetTags_ShouldReturnTagsWithGivenWords() + { + var words = new List { "tag" }; + var tags = tagsBuilder.GetTags(words); + + tags[0].Value.Should().Be("tag"); + } + + [Test] + public void GetTags_ShouldReturnTagsWithDifferentValues() { - private TagsBuilder tagsBuilder; - - [SetUp] - public void Setup() - { - var tagSettings = new TagSettings() { TagFontSize = 32 }; - tagsBuilder = new TagsBuilder(tagSettings); - } - - [Test] - public void GetTags_ShouldReturnTagsWithGivenWords() - { - var words = new List() { "tag"}; - var tags = tagsBuilder.GetTags(words); - - tags[0].Value.Should().Be("tag"); - } - - [Test] - public void GetTags_ShouldReturnTagsWithDifferentValues() - { - var words = new List() { "tag", "tag" }; - var tags = tagsBuilder.GetTags(words); - - tags.Count.Should().Be(1); - } - - [Test] - public void GetTags_ShouldReturnTagsWithCorrectCount() - { - var words = new List() { "tag", "tag" }; - var tags = tagsBuilder.GetTags(words); - - tags[0].Count.Should().Be(2); - } + var words = new List { "tag", "tag" }; + var tags = tagsBuilder.GetTags(words); + + tags.Count.Should().Be(1); + } + + [Test] + public void GetTags_ShouldReturnTagsWithCorrectCount() + { + var words = new List { "tag", "tag" }; + var tags = tagsBuilder.GetTags(words); + + tags[0].Count.Should().Be(2); } -} +} \ No newline at end of file diff --git a/Tests/TagsCloudLayouterTests.cs b/Tests/TagsCloudLayouterTests.cs index 3393908f9..bd89b2690 100644 --- a/Tests/TagsCloudLayouterTests.cs +++ b/Tests/TagsCloudLayouterTests.cs @@ -1,91 +1,90 @@ -using TagsCloudPainter.FormPointer; -using TagsCloudPainter.Settings; -using System.Drawing; +using System.Drawing; +using FluentAssertions; using TagsCloudPainter.CloudLayouter; +using TagsCloudPainter.FormPointer; +using TagsCloudPainter.Settings; using TagsCloudPainter.Tags; using TagsCloudPainter.Utils; -using FluentAssertions; -namespace TagsCloudPainterTests +namespace TagsCloudPainterTests; + +[TestFixture] +public class TagsCloudLayouterTests { - [TestFixture] - public class TagsCloudLayouterTests + [SetUp] + public void Setup() + { + var cloudSettings = new CloudSettings { CloudCenter = new Point(0, 0) }; + tagSettings = new TagSettings { TagFontSize = 32 }; + var pointerSettings = new SpiralPointerSettings { AngleConst = 1, RadiusConst = 0.5, Step = 0.1 }; + var formPointer = new ArchimedeanSpiralPointer(cloudSettings, pointerSettings); + tagsCloudLayouter = new TagsCloudLayouter(cloudSettings, formPointer, tagSettings); + tagsCloudLayouter.InitializeCloud(); + } + + private TagsCloudLayouter tagsCloudLayouter; + private TagSettings tagSettings; + + private static IEnumerable PutNextTagArgumentException => new[] { - private TagsCloudLayouter tagsCloudLayouter; - private TagSettings tagSettings; - - [SetUp] - public void Setup() - { - var cloudSettings = new CloudSettings() { CloudCenter = new Point(0, 0) }; - tagSettings = new TagSettings() { TagFontSize = 32 }; - var pointerSettings = new SpiralPointerSettings() { AngleConst = 1, RadiusConst = 0.5, Step = 0.1 }; - var formPointer = new ArchimedeanSpiralPointer(cloudSettings, pointerSettings); - tagsCloudLayouter = new TagsCloudLayouter(cloudSettings, formPointer, tagSettings); - tagsCloudLayouter.InitializeCloud(); - } - - private static IEnumerable PutNextTagArgumentException => new[] - { new TestCaseData(new Tag("", 10, 1)).SetName("WhenGivenTagWithEmptyValue"), - new TestCaseData(new Tag("das", 0, 1)).SetName("WhenGivenTagWithFontSizeLessThanOne"), - }; - - [TestCaseSource(nameof(PutNextTagArgumentException))] - public void PutNextRectangle_ShouldThrowArgumentException(Tag tag) - { - Assert.Throws(() => tagsCloudLayouter.PutNextTag(tag)); - } - - [Test] - public void PutNextTag_ShouldReturnRectangleOfTheTagValueSize() - { - var tag = new Tag("ads", 10, 5); - var tagRectangle = Utils.GetStringSize(tag.Value, tagSettings.TagFontName, tag.FontSize); - - var resultRectangle = tagsCloudLayouter.PutNextTag(tag); - - resultRectangle.Size.Should().Be(tagRectangle); - } - - [Test] - public void PutNextTag_ShouldReturnRectangleThatDoesNotIntersectWithAlreadyPutOnes() - { - var firstTag = new Tag("ads", 10, 5); - var secondTag = new Tag("ads", 10, 5); - var firstPutRectangle = tagsCloudLayouter.PutNextTag(firstTag); - var secondPutRectangle = tagsCloudLayouter.PutNextTag(secondTag); - - var doesRectanglesIntersect = firstPutRectangle.IntersectsWith(secondPutRectangle); - - doesRectanglesIntersect.Should().BeFalse(); - } - - [Test] - public void PutNextRectangle_ShouldPutRectangleWithCenterInTheCloudCenter() - { - var center = tagsCloudLayouter.GetCloud().Center; - var tag = new Tag("ads", 10, 5); - var firstRectangle = tagsCloudLayouter.PutNextTag(tag); - var firstRectangleCenter = Utils.GetRectangleCenter(firstRectangle); - - firstRectangleCenter.Should().Be(center); - } - - [Test] - public void PutTags_ThrowsArgumentNullException_WhenGivenEmptyDictionary() - { - Assert.Throws(() => tagsCloudLayouter.PutTags([])); - } - - [Test] - public void GetCloud_ReturnsAsManyTagsAsWasPut() - { - tagsCloudLayouter.PutNextTag(new Tag("ads", 10, 5)); - tagsCloudLayouter.PutNextTag(new Tag("ads", 10, 5)); - var rectanglesAmount = tagsCloudLayouter.GetCloud().Tags.Count; - - rectanglesAmount.Should().Be(2); - } + new TestCaseData(new Tag("das", 0, 1)).SetName("WhenGivenTagWithFontSizeLessThanOne") + }; + + [TestCaseSource(nameof(PutNextTagArgumentException))] + public void PutNextRectangle_ShouldThrowArgumentException(Tag tag) + { + Assert.Throws(() => tagsCloudLayouter.PutNextTag(tag)); + } + + [Test] + public void PutNextTag_ShouldReturnRectangleOfTheTagValueSize() + { + var tag = new Tag("ads", 10, 5); + var tagRectangle = Utils.GetStringSize(tag.Value, tagSettings.TagFontName, tag.FontSize); + + var resultRectangle = tagsCloudLayouter.PutNextTag(tag); + + resultRectangle.Size.Should().Be(tagRectangle); + } + + [Test] + public void PutNextTag_ShouldReturnRectangleThatDoesNotIntersectWithAlreadyPutOnes() + { + var firstTag = new Tag("ads", 10, 5); + var secondTag = new Tag("ads", 10, 5); + var firstPutRectangle = tagsCloudLayouter.PutNextTag(firstTag); + var secondPutRectangle = tagsCloudLayouter.PutNextTag(secondTag); + + var doesRectanglesIntersect = firstPutRectangle.IntersectsWith(secondPutRectangle); + + doesRectanglesIntersect.Should().BeFalse(); + } + + [Test] + public void PutNextRectangle_ShouldPutRectangleWithCenterInTheCloudCenter() + { + var center = tagsCloudLayouter.GetCloud().Center; + var tag = new Tag("ads", 10, 5); + var firstRectangle = tagsCloudLayouter.PutNextTag(tag); + var firstRectangleCenter = Utils.GetRectangleCenter(firstRectangle); + + firstRectangleCenter.Should().Be(center); + } + + [Test] + public void PutTags_ThrowsArgumentNullException_WhenGivenEmptyDictionary() + { + Assert.Throws(() => tagsCloudLayouter.PutTags([])); + } + + [Test] + public void GetCloud_ReturnsAsManyTagsAsWasPut() + { + tagsCloudLayouter.PutNextTag(new Tag("ads", 10, 5)); + tagsCloudLayouter.PutNextTag(new Tag("ads", 10, 5)); + var rectanglesAmount = tagsCloudLayouter.GetCloud().Tags.Count; + + rectanglesAmount.Should().Be(2); } -} +} \ No newline at end of file diff --git a/Tests/TagsCloudPainterTests.csproj b/Tests/TagsCloudPainterTests.csproj index 01282a616..cd1cdc579 100644 --- a/Tests/TagsCloudPainterTests.csproj +++ b/Tests/TagsCloudPainterTests.csproj @@ -1,25 +1,25 @@ - - net8.0 - enable - enable + + net8.0 + enable + enable - false - true - + false + true + - - - - - - - - + + + + + + + + - - - + + + diff --git a/Tests/TextFileReaderTests.cs b/Tests/TextFileReaderTests.cs index 8537c81b2..3944b46a9 100644 --- a/Tests/TextFileReaderTests.cs +++ b/Tests/TextFileReaderTests.cs @@ -1,42 +1,41 @@ using TagsCloudPainter.FileReader; -namespace TagsCloudPainterTests +namespace TagsCloudPainterTests; + +[TestFixture] +public class TextFileReaderTests { - [TestFixture] - public class TextFileReaderTests + [SetUp] + public void Setup() { - private TextFileReader fileReader; + fileReader = new TextFileReader(); + } - [SetUp] - public void Setup() - { - fileReader = new TextFileReader(); - } + private TextFileReader fileReader; - private static IEnumerable ReadTextFiles => new[] - { - new TestCaseData(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt") + private static IEnumerable ReadTextFiles => new[] + { + new TestCaseData(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt") .Returns("Товарищи! постоянное информационно-пропагандистское обеспечение нашей " + - $"деятельности играет важную роль в формировании форм развития.{Environment.NewLine}{Environment.NewLine}" + - "Значимость этих проблем настолько очевидна, что укрепление и развитие.") + $"деятельности играет важную роль в формировании форм развития.{Environment.NewLine}{Environment.NewLine}" + + "Значимость этих проблем настолько очевидна, что укрепление и развитие.") .SetName("WhenPassedTxtFile"), - new TestCaseData(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.docx") + new TestCaseData(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.docx") .Returns("Товарищи! постоянное информационно-пропагандистское обеспечение нашей " + - $"деятельности играет важную роль в формировании форм развития.{Environment.NewLine}{Environment.NewLine}" + - "Значимость этих проблем настолько очевидна, что укрепление и развитие.") - .SetName("WhenPassedDocxFile"), - }; + $"деятельности играет важную роль в формировании форм развития.{Environment.NewLine}{Environment.NewLine}" + + "Значимость этих проблем настолько очевидна, что укрепление и развитие.") + .SetName("WhenPassedDocxFile") + }; - [TestCaseSource(nameof(ReadTextFiles))] - public string ReadFile_ShouldReturnFileText(string path) - { - return fileReader.ReadFile(path); - } + [TestCaseSource(nameof(ReadTextFiles))] + public string ReadFile_ShouldReturnFileText(string path) + { + return fileReader.ReadFile(path); + } - [Test] - public void ReadFile_ThrowsFileNotFoundExceptio_WhenPassedNonexistentPath() - { - Assert.Throws(() => fileReader.ReadFile("")); - } + [Test] + public void ReadFile_ThrowsFileNotFoundExceptio_WhenPassedNonexistentPath() + { + Assert.Throws(() => fileReader.ReadFile("")); } -} +} \ No newline at end of file From b385a926216e3793c295683ca0126548829fc6f7 Mon Sep 17 00:00:00 2001 From: daHil Date: Fri, 26 Jan 2024 18:41:35 +0500 Subject: [PATCH 10/34] TagsCloudSettings now can't have nullable fields --- .../Infrastructure/Settings/TagsCloudSettings.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs index 1c2760f98..66a2f37ea 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs @@ -23,10 +23,10 @@ public TagsCloudSettings( PointerAngleConst = 1; } - [Browsable(false)] public CloudSettings CloudSettings { get; } - [Browsable(false)] public TagSettings TagSettings { get; } - [Browsable(false)] public SpiralPointerSettings SpiralPointerSettings { get; } - [Browsable(false)] public TextSettings TextSettings { get; private set; } + [Browsable(false)] public CloudSettings CloudSettings { get; } = null!; + [Browsable(false)] public TagSettings TagSettings { get; } = null!; + [Browsable(false)] public SpiralPointerSettings SpiralPointerSettings { get; } = null!; + [Browsable(false)] public TextSettings TextSettings { get; private set; } = null!; public int TagFontSize { From afa5144d7b6916cf7e691875f01f8d191b51beb3 Mon Sep 17 00:00:00 2001 From: daHil Date: Fri, 26 Jan 2024 22:07:24 +0500 Subject: [PATCH 11/34] added laziness instead of initialization --- .../CloudLayouter/ICloudLayouter.cs | 1 - .../CloudLayouter/TagsCloudLayouter.cs | 33 +++++++------------ .../Actions/DrawTagCloudAction.cs | 2 -- .../Infrastructure/IImageHolder.cs | 1 - TagsCloudPainterApplication/MainForm.cs | 1 - .../PictureBoxImageHolder.cs | 27 +++++++-------- 6 files changed, 25 insertions(+), 40 deletions(-) diff --git a/TagsCloudPainter/CloudLayouter/ICloudLayouter.cs b/TagsCloudPainter/CloudLayouter/ICloudLayouter.cs index e06fc1efb..659f25e1d 100644 --- a/TagsCloudPainter/CloudLayouter/ICloudLayouter.cs +++ b/TagsCloudPainter/CloudLayouter/ICloudLayouter.cs @@ -8,5 +8,4 @@ public interface ICloudLayouter : IResetable Rectangle PutNextTag(Tag tag); TagsCloud GetCloud(); void PutTags(List tags); - void InitializeCloud(); } \ No newline at end of file diff --git a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs index bab1b3b10..205648388 100644 --- a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs +++ b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs @@ -7,39 +7,39 @@ namespace TagsCloudPainter.CloudLayouter; public class TagsCloudLayouter : ICloudLayouter { - private readonly IFormPointer formPointer; + private readonly IFormPointer formPointer = null!; private readonly TagSettings tagSettings; + private readonly Lazy cloudSettings; private TagsCloud cloud; - private readonly CloudSettings cloudSettings; + private TagsCloud Cloud + { + get => cloud ??= new TagsCloud(cloudSettings.Value.CloudCenter, []); + } - public TagsCloudLayouter(CloudSettings cloudSettings, IFormPointer formPointer, TagSettings tagSettings) + public TagsCloudLayouter(Lazy cloudSettings, IFormPointer formPointer, TagSettings tagSettings) { this.cloudSettings = cloudSettings; - this.formPointer = formPointer; + this.formPointer = null; this.tagSettings = tagSettings; } public Rectangle PutNextTag(Tag tag) { - FailIfCloudNotInitialized(); - var rectangleSize = Utils.Utils.GetStringSize(tag.Value, tagSettings.TagFontName, tag.FontSize); if (rectangleSize.Height <= 0 || rectangleSize.Width <= 0) throw new ArgumentException("either width or height of rectangle size is not possitive"); var nextRectangle = Utils.Utils.GetRectangleFromCenter(formPointer.GetNextPoint(), rectangleSize); - while (cloud.Tags.Values.Any(rectangle => rectangle.IntersectsWith(nextRectangle))) + while (Cloud.Tags.Values.Any(rectangle => rectangle.IntersectsWith(nextRectangle))) nextRectangle = Utils.Utils.GetRectangleFromCenter(formPointer.GetNextPoint(), rectangleSize); - cloud.AddTag(tag, nextRectangle); + Cloud.AddTag(tag, nextRectangle); return nextRectangle; } public void PutTags(List tags) { - FailIfCloudNotInitialized(); - if (tags.Count == 0) throw new ArgumentException("пустые размеры"); foreach (var tag in tags) @@ -48,22 +48,11 @@ public void PutTags(List tags) public TagsCloud GetCloud() { - return new TagsCloud(cloud.Center, cloud.Tags); - } - - public void InitializeCloud() - { - cloud = new TagsCloud(cloudSettings.CloudCenter, []); + return new TagsCloud(Cloud.Center, Cloud.Tags); } public void Reset() { formPointer.Reset(); } - - public void FailIfCloudNotInitialized() - { - if (cloud is null) - throw new InvalidOperationException("Initialize cloud before other method call!"); - } } \ No newline at end of file diff --git a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs index 7099753dc..62b830028 100644 --- a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs +++ b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs @@ -84,7 +84,6 @@ private TagsCloud GetCloud(List words) { var tags = tagsBuilder.GetTags(words); cloudLayouter.Reset(); - cloudLayouter.InitializeCloud(); cloudLayouter.PutTags(tags); var cloud = cloudLayouter.GetCloud(); return cloud; @@ -93,7 +92,6 @@ private TagsCloud GetCloud(List words) private void DrawCloud(TagsCloud cloud) { var bitmap = cloudDrawer.DrawCloud(cloud, imageSettings.Width, imageSettings.Height); - imageHolder.RecreateImage(imageSettings); using (var graphics = imageHolder.StartDrawing()) { diff --git a/TagsCloudPainterApplication/Infrastructure/IImageHolder.cs b/TagsCloudPainterApplication/Infrastructure/IImageHolder.cs index cebf9c09a..4614c3386 100644 --- a/TagsCloudPainterApplication/Infrastructure/IImageHolder.cs +++ b/TagsCloudPainterApplication/Infrastructure/IImageHolder.cs @@ -7,6 +7,5 @@ public interface IImageHolder Size GetImageSize(); Graphics StartDrawing(); void UpdateUi(); - void RecreateImage(ImageSettings settings); void SaveImage(string fileName); } \ No newline at end of file diff --git a/TagsCloudPainterApplication/MainForm.cs b/TagsCloudPainterApplication/MainForm.cs index 09302f7ed..43ffe92c4 100644 --- a/TagsCloudPainterApplication/MainForm.cs +++ b/TagsCloudPainterApplication/MainForm.cs @@ -13,7 +13,6 @@ public MainForm(IUiAction[] actions, ImageSettings imageSettings, PictureBoxImag mainMenu.Items.AddRange(actions.ToMenuItems()); Controls.Add(mainMenu); - pictureBox.RecreateImage(imageSettings); pictureBox.Dock = DockStyle.Fill; Controls.Add(pictureBox); } diff --git a/TagsCloudPainterApplication/PictureBoxImageHolder.cs b/TagsCloudPainterApplication/PictureBoxImageHolder.cs index 6fe98af37..c7ff3b94e 100644 --- a/TagsCloudPainterApplication/PictureBoxImageHolder.cs +++ b/TagsCloudPainterApplication/PictureBoxImageHolder.cs @@ -6,16 +6,21 @@ namespace TagsCloudPainterApplication; public class PictureBoxImageHolder : PictureBox, IImageHolder { + private readonly Lazy imageSettings = null!; + + public PictureBoxImageHolder(Lazy imageSettings) + { + this.imageSettings = imageSettings; + } + public Size GetImageSize() { - FailIfNotInitialized(); - return Image.Size; + return GetImage().Size; } public Graphics StartDrawing() { - FailIfNotInitialized(); - return Graphics.FromImage(Image); + return Graphics.FromImage(GetImage()); } public void UpdateUi() @@ -24,20 +29,16 @@ public void UpdateUi() Application.DoEvents(); } - public void RecreateImage(ImageSettings imageSettings) - { - Image = new Bitmap(imageSettings.Width, imageSettings.Height, PixelFormat.Format24bppRgb); - } - public void SaveImage(string fileName) { - FailIfNotInitialized(); - Image.Save(fileName); + GetImage().Save(fileName); } - private void FailIfNotInitialized() + public Image GetImage() { if (Image == null) - throw new InvalidOperationException("Call PictureBoxImageHolder.RecreateImage before other method call!"); + Image = new Bitmap(imageSettings.Value.Width, imageSettings.Value.Height, PixelFormat.Format24bppRgb); + + return Image; } } \ No newline at end of file From 2c1e9fe1a7e0dda82d38a813dac740794dfad45a Mon Sep 17 00:00:00 2001 From: daHil Date: Fri, 26 Jan 2024 22:25:52 +0500 Subject: [PATCH 12/34] now every constructor throws null exception if null reference was passed --- TagsCloudPainter/CloudDrawer/CloudDrawer.cs | 4 ++-- .../CloudLayouter/TagsCloudLayouter.cs | 8 +++---- .../FormPointer/ArchimedeanSpiralPointer.cs | 4 ++-- TagsCloudPainter/Parser/BoringTextParser.cs | 2 +- TagsCloudPainter/Tags/TagsBuilder.cs | 2 +- .../Actions/DrawTagCloudAction.cs | 21 ++++++++++--------- .../Actions/FileSourceSettingsAction.cs | 2 +- .../Actions/ImageSettingsAction.cs | 2 +- .../Actions/PaletteSettingsAction.cs | 2 +- .../Actions/SaveImageAction.cs | 2 +- .../Settings/TagsCloudSettings.cs | 17 ++++++++------- 11 files changed, 34 insertions(+), 32 deletions(-) diff --git a/TagsCloudPainter/CloudDrawer/CloudDrawer.cs b/TagsCloudPainter/CloudDrawer/CloudDrawer.cs index 914b50d3b..d34310ca7 100644 --- a/TagsCloudPainter/CloudDrawer/CloudDrawer.cs +++ b/TagsCloudPainter/CloudDrawer/CloudDrawer.cs @@ -11,8 +11,8 @@ public class CloudDrawer public CloudDrawer(TagSettings tagSettings, CloudSettings cloudSettings) { - this.tagSettings = tagSettings; - this.cloudSettings = cloudSettings; + this.tagSettings = tagSettings ?? throw new ArgumentNullException(nameof(tagSettings)); + this.cloudSettings = cloudSettings ?? throw new ArgumentNullException(nameof(cloudSettings)); } public Bitmap DrawCloud(TagsCloud cloud, int imageWidth, int imageHeight) diff --git a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs index 205648388..41b5fd828 100644 --- a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs +++ b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs @@ -7,7 +7,7 @@ namespace TagsCloudPainter.CloudLayouter; public class TagsCloudLayouter : ICloudLayouter { - private readonly IFormPointer formPointer = null!; + private readonly IFormPointer formPointer; private readonly TagSettings tagSettings; private readonly Lazy cloudSettings; private TagsCloud cloud; @@ -18,9 +18,9 @@ private TagsCloud Cloud public TagsCloudLayouter(Lazy cloudSettings, IFormPointer formPointer, TagSettings tagSettings) { - this.cloudSettings = cloudSettings; - this.formPointer = null; - this.tagSettings = tagSettings; + this.cloudSettings = cloudSettings ?? throw new ArgumentNullException(nameof(cloudSettings)); + this.formPointer = formPointer ?? throw new ArgumentNullException(nameof(formPointer)); + this.tagSettings = tagSettings ?? throw new ArgumentNullException(nameof(tagSettings)); } public Rectangle PutNextTag(Tag tag) diff --git a/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs b/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs index 138c4075f..3bd4b11a5 100644 --- a/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs +++ b/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs @@ -15,8 +15,8 @@ public ArchimedeanSpiralPointer(CloudSettings cloudSettings, SpiralPointerSettin || spiralPointerSettings.RadiusConst <= 0 || spiralPointerSettings.AngleConst <= 0) throw new ArgumentException("either step or radius or angle is not possitive"); - this.cloudSettings = cloudSettings; - this.spiralPointerSettings = spiralPointerSettings; + this.cloudSettings = cloudSettings ?? throw new ArgumentNullException(nameof(cloudSettings)); + this.spiralPointerSettings = spiralPointerSettings ?? throw new ArgumentNullException(nameof(spiralPointerSettings)); сurrentDifference = 0; } diff --git a/TagsCloudPainter/Parser/BoringTextParser.cs b/TagsCloudPainter/Parser/BoringTextParser.cs index cb958d9a0..62c7e9741 100644 --- a/TagsCloudPainter/Parser/BoringTextParser.cs +++ b/TagsCloudPainter/Parser/BoringTextParser.cs @@ -9,7 +9,7 @@ public class BoringTextParser : ITextParser public BoringTextParser(TextSettings textSettings) { - this.textSettings = textSettings; + this.textSettings = textSettings ?? throw new ArgumentNullException(nameof(textSettings)); } public List ParseText(string text) diff --git a/TagsCloudPainter/Tags/TagsBuilder.cs b/TagsCloudPainter/Tags/TagsBuilder.cs index dfedeba43..6df7907fa 100644 --- a/TagsCloudPainter/Tags/TagsBuilder.cs +++ b/TagsCloudPainter/Tags/TagsBuilder.cs @@ -8,7 +8,7 @@ public class TagsBuilder : ITagsBuilder public TagsBuilder(TagSettings settings) { - _settings = settings; + _settings = settings ?? throw new ArgumentNullException(nameof(settings)); } public List GetTags(List words) diff --git a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs index 62b830028..fbb4f6a8b 100644 --- a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs +++ b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs @@ -3,6 +3,7 @@ using TagsCloudPainter.Drawer; using TagsCloudPainter.FileReader; using TagsCloudPainter.Parser; +using TagsCloudPainter.Settings; using TagsCloudPainter.Tags; using TagsCloudPainterApplication.Infrastructure; using TagsCloudPainterApplication.Infrastructure.Settings; @@ -34,16 +35,16 @@ public DrawTagCloudAction( IFileReader fileReader, Palette palette) { - this.cloudDrawer = cloudDrawer; - this.cloudLayouter = cloudLayouter; - this.tagsBuilder = tagsBuilder; - this.textParser = textParser; - this.fileReader = fileReader; - this.imageSettings = imageSettings; - this.tagsCloudSettings = tagsCloudSettings; - this.imageHolder = imageHolder; - this.filesSourceSettings = filesSourceSettings; - this.palette = palette; + this.cloudDrawer = cloudDrawer ?? throw new ArgumentNullException(nameof(cloudDrawer)); + this.cloudLayouter = cloudLayouter ?? throw new ArgumentNullException(nameof(cloudLayouter)); + this.tagsBuilder = tagsBuilder ?? throw new ArgumentNullException(nameof(tagsBuilder)); + this.textParser = textParser ?? throw new ArgumentNullException(nameof(textParser)); + this.fileReader = fileReader ?? throw new ArgumentNullException(nameof(fileReader)); + this.imageSettings = imageSettings ?? throw new ArgumentNullException(nameof(imageSettings)); + this.tagsCloudSettings = tagsCloudSettings ?? throw new ArgumentNullException(nameof(tagsCloudSettings)); + this.imageHolder = imageHolder ?? throw new ArgumentNullException(nameof(imageHolder)); + this.filesSourceSettings = filesSourceSettings ?? throw new ArgumentNullException(nameof(filesSourceSettings)); + this.palette = palette ?? throw new ArgumentNullException(nameof(palette)); } public string Category => "Облако тэгов"; diff --git a/TagsCloudPainterApplication/Actions/FileSourceSettingsAction.cs b/TagsCloudPainterApplication/Actions/FileSourceSettingsAction.cs index 9e12051bb..02b4fd70c 100644 --- a/TagsCloudPainterApplication/Actions/FileSourceSettingsAction.cs +++ b/TagsCloudPainterApplication/Actions/FileSourceSettingsAction.cs @@ -8,7 +8,7 @@ public class FileSourceSettingsAction : IUiAction public FileSourceSettingsAction(FilesSourceSettings filesSourceSettings) { - this.filesSourceSettings = filesSourceSettings; + this.filesSourceSettings = filesSourceSettings ?? throw new ArgumentNullException(nameof(filesSourceSettings)); } public string Category => "Настройки"; diff --git a/TagsCloudPainterApplication/Actions/ImageSettingsAction.cs b/TagsCloudPainterApplication/Actions/ImageSettingsAction.cs index 59c3a270e..cdcc84801 100644 --- a/TagsCloudPainterApplication/Actions/ImageSettingsAction.cs +++ b/TagsCloudPainterApplication/Actions/ImageSettingsAction.cs @@ -8,7 +8,7 @@ public class ImageSettingsAction : IUiAction public ImageSettingsAction(ImageSettings imageSettings) { - this.imageSettings = imageSettings; + this.imageSettings = imageSettings ?? throw new ArgumentNullException(nameof(imageSettings)); } public string Category => "Настройки"; diff --git a/TagsCloudPainterApplication/Actions/PaletteSettingsAction.cs b/TagsCloudPainterApplication/Actions/PaletteSettingsAction.cs index 30fc4ba19..6f4909449 100644 --- a/TagsCloudPainterApplication/Actions/PaletteSettingsAction.cs +++ b/TagsCloudPainterApplication/Actions/PaletteSettingsAction.cs @@ -9,7 +9,7 @@ public class PaletteSettingsAction : IUiAction public PaletteSettingsAction(Palette palette) { - this.palette = palette; + this.palette = palette ?? throw new ArgumentNullException(nameof(palette)); } public string Category => "Настройки"; diff --git a/TagsCloudPainterApplication/Actions/SaveImageAction.cs b/TagsCloudPainterApplication/Actions/SaveImageAction.cs index ccef3a1e4..a4876ba7d 100644 --- a/TagsCloudPainterApplication/Actions/SaveImageAction.cs +++ b/TagsCloudPainterApplication/Actions/SaveImageAction.cs @@ -8,7 +8,7 @@ public class SaveImageAction : IUiAction public SaveImageAction(IImageHolder imageHolder) { - this.imageHolder = imageHolder; + this.imageHolder = imageHolder ?? throw new ArgumentNullException(nameof(imageHolder)); } public string Category => "Файл"; diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs index 66a2f37ea..fcb602200 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs @@ -1,4 +1,5 @@ using System.ComponentModel; +using System.Xml.Linq; using TagsCloudPainter.Settings; namespace TagsCloudPainterApplication.Infrastructure.Settings; @@ -11,10 +12,10 @@ public TagsCloudSettings( SpiralPointerSettings spiralPointerSettings, TextSettings textSettings) { - CloudSettings = cloudSettings; - TagSettings = tagSettings; - SpiralPointerSettings = spiralPointerSettings; - TextSettings = textSettings; + CloudSettings = cloudSettings ?? throw new ArgumentNullException(nameof(cloudSettings)); + TagSettings = tagSettings ?? throw new ArgumentNullException(nameof(tagSettings)); + SpiralPointerSettings = spiralPointerSettings ?? throw new ArgumentNullException(nameof(spiralPointerSettings)); + TextSettings = textSettings ?? throw new ArgumentNullException(nameof(textSettings)); TagFontSize = 32; TagFontName = "Arial"; CloudCenter = new Point(0, 0); @@ -23,10 +24,10 @@ public TagsCloudSettings( PointerAngleConst = 1; } - [Browsable(false)] public CloudSettings CloudSettings { get; } = null!; - [Browsable(false)] public TagSettings TagSettings { get; } = null!; - [Browsable(false)] public SpiralPointerSettings SpiralPointerSettings { get; } = null!; - [Browsable(false)] public TextSettings TextSettings { get; private set; } = null!; + [Browsable(false)] public CloudSettings CloudSettings { get; } + [Browsable(false)] public TagSettings TagSettings { get; } + [Browsable(false)] public SpiralPointerSettings SpiralPointerSettings { get; } + [Browsable(false)] public TextSettings TextSettings { get; } public int TagFontSize { From b0c310a68819fc2931b055a7f6a11902b964f659 Mon Sep 17 00:00:00 2001 From: daHil Date: Fri, 26 Jan 2024 22:30:19 +0500 Subject: [PATCH 13/34] now every constructor throws null exception if null reference was pas --- TagsCloudPainterApplication/MainForm.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/TagsCloudPainterApplication/MainForm.cs b/TagsCloudPainterApplication/MainForm.cs index 43ffe92c4..48e405612 100644 --- a/TagsCloudPainterApplication/MainForm.cs +++ b/TagsCloudPainterApplication/MainForm.cs @@ -1,3 +1,4 @@ +using TagsCloudPainter.Settings; using TagsCloudPainterApplication.Actions; using TagsCloudPainterApplication.Infrastructure.Settings; @@ -7,6 +8,9 @@ public partial class MainForm : Form { public MainForm(IUiAction[] actions, ImageSettings imageSettings, PictureBoxImageHolder pictureBox) { + if(actions is null || actions.Length == 0 || imageSettings is null || pictureBox is null) + throw new NullReferenceException("MainForm cann't be injected with nullable reference"); + ClientSize = new Size(imageSettings.Width, imageSettings.Height); var mainMenu = new MenuStrip(); From 7193c8504e659babd2ae4051cd7e0d03b2408a63 Mon Sep 17 00:00:00 2001 From: daHil Date: Fri, 26 Jan 2024 23:57:34 +0500 Subject: [PATCH 14/34] refactored --- TagsCloudPainterApplication/MainForm.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TagsCloudPainterApplication/MainForm.cs b/TagsCloudPainterApplication/MainForm.cs index 48e405612..2c35e7d4f 100644 --- a/TagsCloudPainterApplication/MainForm.cs +++ b/TagsCloudPainterApplication/MainForm.cs @@ -9,7 +9,7 @@ public partial class MainForm : Form public MainForm(IUiAction[] actions, ImageSettings imageSettings, PictureBoxImageHolder pictureBox) { if(actions is null || actions.Length == 0 || imageSettings is null || pictureBox is null) - throw new NullReferenceException("MainForm cann't be injected with nullable reference"); + throw new ArgumentNullException("MainForm cann't be injected with nullable reference"); ClientSize = new Size(imageSettings.Width, imageSettings.Height); From 1d5b6ebe94cad6ce91d99595a22f6306ce609016 Mon Sep 17 00:00:00 2001 From: daHil Date: Sat, 27 Jan 2024 04:52:51 +0500 Subject: [PATCH 15/34] settings now loaded from settings file --- .../Settings/FilesSourceSettings.cs | 8 ++++- .../Infrastructure/Settings/ImageSettings.cs | 4 +-- .../Settings/TagsCloudSettings.cs | 11 +++--- .../Properties/Settings.settings | 35 +++++++++++++++---- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs index 4ce3643f9..9590562f0 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs @@ -2,5 +2,11 @@ public class FilesSourceSettings { - public string BoringTextFilePath { get; set; } = @$"{Environment.CurrentDirectory}..\..\..\..\Data\BoringWords.txt"; + public string BoringTextFilePath { get; set; } + + public FilesSourceSettings() + { + BoringTextFilePath = @$"{Environment.CurrentDirectory}..\..\..\..\" + Properties.Settings.Default.boringTextFilePath; + } + } \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs index cd795ac58..9b7abf90e 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs @@ -2,6 +2,6 @@ public class ImageSettings { - public int Width { get; set; } = 800; - public int Height { get; set; } = 600; + public int Width { get; set; } = Properties.Settings.Default.imageWidth; + public int Height { get; set; } = Properties.Settings.Default.imageHeight; } \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs index fcb602200..32c0ad2b9 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs @@ -16,12 +16,11 @@ public TagsCloudSettings( TagSettings = tagSettings ?? throw new ArgumentNullException(nameof(tagSettings)); SpiralPointerSettings = spiralPointerSettings ?? throw new ArgumentNullException(nameof(spiralPointerSettings)); TextSettings = textSettings ?? throw new ArgumentNullException(nameof(textSettings)); - TagFontSize = 32; - TagFontName = "Arial"; - CloudCenter = new Point(0, 0); - PointerStep = 0.1; - PointerRadiusConst = 0.5; - PointerAngleConst = 1; + TagFontSize = Properties.Settings.Default.tagFontSize; + TagFontName = Properties.Settings.Default.tagFontName; + PointerStep = Properties.Settings.Default.pointerStep; + PointerRadiusConst = Properties.Settings.Default.pointerRadiusConst; + PointerAngleConst = Properties.Settings.Default.pointerAngleConst; } [Browsable(false)] public CloudSettings CloudSettings { get; } diff --git a/TagsCloudPainterApplication/Properties/Settings.settings b/TagsCloudPainterApplication/Properties/Settings.settings index 796d34b5e..db14197f7 100644 --- a/TagsCloudPainterApplication/Properties/Settings.settings +++ b/TagsCloudPainterApplication/Properties/Settings.settings @@ -1,7 +1,30 @@  - - - - - - + + + + + Data\BoringWords.txt + + + 800 + + + 600 + + + 32 + + + Arial + + + 0.1 + + + 0.5 + + + 1 + + + \ No newline at end of file From b74c843df27ed486799611a3bf21c629683fa24f Mon Sep 17 00:00:00 2001 From: daHil Date: Sat, 27 Jan 2024 04:53:30 +0500 Subject: [PATCH 16/34] refactored --- .../CloudLayouter/TagsCloudLayouter.cs | 4 +- TagsCloudPainter/Utils/Utils.cs | 9 +- .../PictureBoxImageHolder.cs | 6 +- .../Properties/Settings.Designer.cs | 105 +++++++++++++++++- 4 files changed, 112 insertions(+), 12 deletions(-) diff --git a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs index 41b5fd828..c43427c9c 100644 --- a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs +++ b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs @@ -5,7 +5,7 @@ namespace TagsCloudPainter.CloudLayouter; -public class TagsCloudLayouter : ICloudLayouter +public class TagsCloudLayouter : ICloudLayouter, IResetable { private readonly IFormPointer formPointer; private readonly TagSettings tagSettings; @@ -14,6 +14,7 @@ public class TagsCloudLayouter : ICloudLayouter private TagsCloud Cloud { get => cloud ??= new TagsCloud(cloudSettings.Value.CloudCenter, []); + set => cloud = value; } public TagsCloudLayouter(Lazy cloudSettings, IFormPointer formPointer, TagSettings tagSettings) @@ -54,5 +55,6 @@ public TagsCloud GetCloud() public void Reset() { formPointer.Reset(); + Cloud = new TagsCloud(cloudSettings.Value.CloudCenter, []); } } \ No newline at end of file diff --git a/TagsCloudPainter/Utils/Utils.cs b/TagsCloudPainter/Utils/Utils.cs index 2bf8c3d22..40d2cdcfd 100644 --- a/TagsCloudPainter/Utils/Utils.cs +++ b/TagsCloudPainter/Utils/Utils.cs @@ -22,9 +22,10 @@ public static Point GetRectangleCenter(Rectangle rectangle) public static Size GetStringSize(string value, string fontName, float fontSize) { using var graphics = Graphics.FromHwnd(IntPtr.Zero); - var font = new Font(fontName, fontSize); - var size = graphics.MeasureString(value, font).ToSize(); - - return size; + using var font = new Font(fontName, fontSize); + { + var size = graphics.MeasureString(value, font).ToSize(); + return size; + } } } \ No newline at end of file diff --git a/TagsCloudPainterApplication/PictureBoxImageHolder.cs b/TagsCloudPainterApplication/PictureBoxImageHolder.cs index c7ff3b94e..8d283e796 100644 --- a/TagsCloudPainterApplication/PictureBoxImageHolder.cs +++ b/TagsCloudPainterApplication/PictureBoxImageHolder.cs @@ -6,11 +6,11 @@ namespace TagsCloudPainterApplication; public class PictureBoxImageHolder : PictureBox, IImageHolder { - private readonly Lazy imageSettings = null!; + private readonly Lazy imageSettings; public PictureBoxImageHolder(Lazy imageSettings) { - this.imageSettings = imageSettings; + this.imageSettings = imageSettings ?? throw new ArgumentNullException(nameof(imageSettings)); } public Size GetImageSize() @@ -36,7 +36,7 @@ public void SaveImage(string fileName) public Image GetImage() { - if (Image == null) + if (Image == null || Image.Width != imageSettings.Value.Width || Image.Height != imageSettings.Value.Height) Image = new Bitmap(imageSettings.Value.Width, imageSettings.Value.Height, PixelFormat.Format24bppRgb); return Image; diff --git a/TagsCloudPainterApplication/Properties/Settings.Designer.cs b/TagsCloudPainterApplication/Properties/Settings.Designer.cs index 4d692949b..bc0ee6bdb 100644 --- a/TagsCloudPainterApplication/Properties/Settings.Designer.cs +++ b/TagsCloudPainterApplication/Properties/Settings.Designer.cs @@ -1,9 +1,10 @@ //------------------------------------------------------------------------------ // -// This code was generated by a tool. +// Этот код создан программой. +// Исполняемая версия:4.0.30319.42000 // -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. +// Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае +// повторной генерации кода. // //------------------------------------------------------------------------------ @@ -11,7 +12,7 @@ namespace TagsCloudPainterApplication.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.9.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.8.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -21,5 +22,101 @@ public static Settings Default { return defaultInstance; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("Data\\BoringWords.txt")] + public string boringTextFilePath { + get { + return ((string)(this["boringTextFilePath"])); + } + set { + this["boringTextFilePath"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("800")] + public int imageWidth { + get { + return ((int)(this["imageWidth"])); + } + set { + this["imageWidth"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("600")] + public int imageHeight { + get { + return ((int)(this["imageHeight"])); + } + set { + this["imageHeight"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("32")] + public int tagFontSize { + get { + return ((int)(this["tagFontSize"])); + } + set { + this["tagFontSize"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("Arial")] + public string tagFontName { + get { + return ((string)(this["tagFontName"])); + } + set { + this["tagFontName"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("0.1")] + public double pointerStep { + get { + return ((double)(this["pointerStep"])); + } + set { + this["pointerStep"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("0.5")] + public double pointerRadiusConst { + get { + return ((double)(this["pointerRadiusConst"])); + } + set { + this["pointerRadiusConst"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("1")] + public double pointerAngleConst { + get { + return ((double)(this["pointerAngleConst"])); + } + set { + this["pointerAngleConst"] = value; + } + } } } From dad1000e0453dd610c195527e6c896ea00cb8af2 Mon Sep 17 00:00:00 2001 From: daHil Date: Sat, 27 Jan 2024 05:13:11 +0500 Subject: [PATCH 17/34] settings added to config --- TagsCloudPainterApplication/App.config | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/TagsCloudPainterApplication/App.config b/TagsCloudPainterApplication/App.config index 9ec7d364a..9ec0eb6bc 100644 --- a/TagsCloudPainterApplication/App.config +++ b/TagsCloudPainterApplication/App.config @@ -1,5 +1,10 @@  + + +
+ + @@ -15,4 +20,32 @@ + + + + Data\BoringWords.txt + + + 800 + + + 600 + + + 32 + + + Arial + + + 0.1 + + + 0.5 + + + 1 + + + \ No newline at end of file From b7438026705c806ddf61ce2396591b7672a3accc Mon Sep 17 00:00:00 2001 From: daHil Date: Sat, 27 Jan 2024 06:15:09 +0500 Subject: [PATCH 18/34] refactored --- TagsCloudPainterApplication/Program.cs | 41 +++++++++++++------ .../TagsCloudPainterApplication.csproj | 6 +-- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/TagsCloudPainterApplication/Program.cs b/TagsCloudPainterApplication/Program.cs index 98abab68b..9f4d5edfe 100644 --- a/TagsCloudPainterApplication/Program.cs +++ b/TagsCloudPainterApplication/Program.cs @@ -1,4 +1,6 @@ using Autofac; +using Autofac.Core; +using Autofac.Core.Registration; using TagsCloudPainter.CloudLayouter; using TagsCloudPainter.Drawer; using TagsCloudPainter.FileReader; @@ -23,30 +25,45 @@ private static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var builder = new ContainerBuilder(); + builder.RegisterModule(new TagsCloudPainterModule()); + builder.RegisterModule(new ApplicationModule()); + var container = builder.Build(); + Application.Run(container.Resolve()); + } +} +public class ApplicationModule: Module +{ + protected override void Load(ContainerBuilder builder) + { builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().As().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().As(); - builder.RegisterType().As(); - builder.RegisterType().As().SingleInstance(); - builder.RegisterType().As().SingleInstance(); - builder.RegisterType().As().SingleInstance(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().AsSelf(); + } +} - var container = builder.Build(); - Application.Run(container.Resolve()); +public class TagsCloudPainterModule : Module +{ + protected override void Load(ContainerBuilder builder) + { + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().As(); + builder.RegisterType().As(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); } } \ No newline at end of file diff --git a/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj b/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj index 014a2d8b2..5131bf9cc 100644 --- a/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj +++ b/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj @@ -10,11 +10,11 @@ - + - + @@ -36,7 +36,7 @@ - + \ No newline at end of file From af175576577553344024508cec8ba017f560d196 Mon Sep 17 00:00:00 2001 From: daHil Date: Sat, 27 Jan 2024 08:56:14 +0500 Subject: [PATCH 19/34] added di tests --- .../CloudLayouter/TagsCloudLayouter.cs | 2 +- .../Settings/SpiralPointerSettings.cs | 6 +- TagsCloudPainterApplication/Program.cs | 7 +- .../TagsCloudPainterApplication.csproj | 2 +- .../DependencyInjectionTests.cs | 98 +++++++++++++++++++ .../GlobalUsings.cs | 1 + .../TagsCloudPainterApplicationTests.csproj | 25 +++++ Tests/TagsCloudLayouterTests.cs | 5 +- di.sln | 6 ++ 9 files changed, 140 insertions(+), 12 deletions(-) create mode 100644 TagsCloudPainterApplicationTests/DependencyInjectionTests.cs create mode 100644 TagsCloudPainterApplicationTests/GlobalUsings.cs create mode 100644 TagsCloudPainterApplicationTests/TagsCloudPainterApplicationTests.csproj diff --git a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs index c43427c9c..acaee0c60 100644 --- a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs +++ b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs @@ -5,7 +5,7 @@ namespace TagsCloudPainter.CloudLayouter; -public class TagsCloudLayouter : ICloudLayouter, IResetable +public class TagsCloudLayouter : ICloudLayouter { private readonly IFormPointer formPointer; private readonly TagSettings tagSettings; diff --git a/TagsCloudPainter/Settings/SpiralPointerSettings.cs b/TagsCloudPainter/Settings/SpiralPointerSettings.cs index b67b48512..a1d3f1a57 100644 --- a/TagsCloudPainter/Settings/SpiralPointerSettings.cs +++ b/TagsCloudPainter/Settings/SpiralPointerSettings.cs @@ -2,7 +2,7 @@ public class SpiralPointerSettings { - public double Step { get; set; } - public double RadiusConst { get; set; } - public double AngleConst { get; set; } + public double Step { get; set; } = 1; + public double RadiusConst { get; set; } = 1; + public double AngleConst { get; set; } = 1; } \ No newline at end of file diff --git a/TagsCloudPainterApplication/Program.cs b/TagsCloudPainterApplication/Program.cs index 9f4d5edfe..32967aa81 100644 --- a/TagsCloudPainterApplication/Program.cs +++ b/TagsCloudPainterApplication/Program.cs @@ -1,6 +1,4 @@ using Autofac; -using Autofac.Core; -using Autofac.Core.Registration; using TagsCloudPainter.CloudLayouter; using TagsCloudPainter.Drawer; using TagsCloudPainter.FileReader; @@ -24,8 +22,9 @@ private static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); + var builder = new ContainerBuilder(); - builder.RegisterModule(new TagsCloudPainterModule()); + builder.RegisterModule(new TagsCloudPainterLibModule()); builder.RegisterModule(new ApplicationModule()); var container = builder.Build(); Application.Run(container.Resolve()); @@ -50,7 +49,7 @@ protected override void Load(ContainerBuilder builder) } } -public class TagsCloudPainterModule : Module +public class TagsCloudPainterLibModule : Module { protected override void Load(ContainerBuilder builder) { diff --git a/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj b/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj index 5131bf9cc..3af7f7ce3 100644 --- a/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj +++ b/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj @@ -2,7 +2,7 @@ WinExe - net8.0-windows + net8.0-windows7.0 enable true enable diff --git a/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs b/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs new file mode 100644 index 000000000..3daf0bbcc --- /dev/null +++ b/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs @@ -0,0 +1,98 @@ +using Autofac; +using TagsCloudPainter.CloudLayouter; +using TagsCloudPainter.Drawer; +using TagsCloudPainter.FileReader; +using TagsCloudPainter.FormPointer; +using TagsCloudPainter.Parser; +using TagsCloudPainter.Settings; +using TagsCloudPainter.Tags; +using TagsCloudPainterApplication; +using TagsCloudPainterApplication.Actions; +using TagsCloudPainterApplication.Infrastructure; +using TagsCloudPainterApplication.Infrastructure.Settings; + +namespace TagsCloudPainterApplicationTests +{ + [TestFixture] + public class DependencyInjectionTests + { + private ILifetimeScope scope; + + [SetUp] + public void Setup() + { + var builder = new ContainerBuilder(); + builder.RegisterModule(); + builder.RegisterModule(); + var container = builder.Build(); + scope = container.BeginLifetimeScope(); + } + + [TearDown] + public void TearDown() + { + scope.Dispose(); + } + + private static IEnumerable InstancePerLifetimeScopeDependencesTypes => new[] + { + new TestCaseData(typeof(IFormPointer)), + new TestCaseData(typeof(ICloudLayouter)), + new TestCaseData(typeof(IUiAction)), + new TestCaseData(typeof(MainForm)), + }; + + private static IEnumerable SingleInstanceDependencesTypes => new[] + { + new TestCaseData(typeof(TagSettings)), + new TestCaseData(typeof(CloudDrawer)), + new TestCaseData(typeof(IFileReader)), + new TestCaseData(typeof(ITextParser)), + new TestCaseData(typeof(ITagsBuilder)), + new TestCaseData(typeof(TextSettings)), + new TestCaseData(typeof(CloudSettings)), + new TestCaseData(typeof(SpiralPointerSettings)), + new TestCaseData(typeof(Palette)), + new TestCaseData(typeof(ImageSettings)), + new TestCaseData(typeof(FilesSourceSettings)), + new TestCaseData(typeof(TagsCloudSettings)), + new TestCaseData(typeof(PictureBoxImageHolder)), + new TestCaseData(typeof(IImageHolder)), + }; + + [TestCaseSource(nameof(SingleInstanceDependencesTypes))] + [TestCaseSource(nameof(InstancePerLifetimeScopeDependencesTypes))] + public void Dependence_SouldResolve(Type dependenceType) + { + Assert.DoesNotThrow(() => scope.Resolve(dependenceType)); + } + + [TestCaseSource(nameof(SingleInstanceDependencesTypes))] + [TestCaseSource(nameof(InstancePerLifetimeScopeDependencesTypes))] + public void Dependence_SouldBeNotNull(Type dependenceType) + { + var dependence = scope.Resolve(dependenceType); + Assert.That(dependence, Is.Not.Null); + } + + [TestCaseSource(nameof(SingleInstanceDependencesTypes))] + public void SingleInstanceDependence_ShouldResolveSameReferenceInDifferentScopes(Type dependenceType) + { + using var childScope = scope.BeginLifetimeScope(); + var dependence1 = scope.Resolve(dependenceType); + var dependence2 = childScope.Resolve(dependenceType); + + Assert.That(dependence2, Is.SameAs(dependence1)); + } + + [TestCaseSource(nameof(InstancePerLifetimeScopeDependencesTypes))] + public void InstancePerLifetimeScopeDependence_ShouldResolveDifferentReferencesInDifferentScopes(Type dependenceType) + { + using var childScope = scope.BeginLifetimeScope(); + var dependence1 = scope.Resolve(dependenceType); + var dependence2 = childScope.Resolve(dependenceType); + + Assert.That(dependence2, Is.Not.EqualTo(dependence1)); + } + } +} \ No newline at end of file diff --git a/TagsCloudPainterApplicationTests/GlobalUsings.cs b/TagsCloudPainterApplicationTests/GlobalUsings.cs new file mode 100644 index 000000000..cefced496 --- /dev/null +++ b/TagsCloudPainterApplicationTests/GlobalUsings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file diff --git a/TagsCloudPainterApplicationTests/TagsCloudPainterApplicationTests.csproj b/TagsCloudPainterApplicationTests/TagsCloudPainterApplicationTests.csproj new file mode 100644 index 000000000..47deafbe2 --- /dev/null +++ b/TagsCloudPainterApplicationTests/TagsCloudPainterApplicationTests.csproj @@ -0,0 +1,25 @@ + + + + net8.0-windows + enable + enable + + false + true + Library + + + + + + + + + + + + + + + diff --git a/Tests/TagsCloudLayouterTests.cs b/Tests/TagsCloudLayouterTests.cs index bd89b2690..67317a122 100644 --- a/Tests/TagsCloudLayouterTests.cs +++ b/Tests/TagsCloudLayouterTests.cs @@ -14,12 +14,11 @@ public class TagsCloudLayouterTests [SetUp] public void Setup() { - var cloudSettings = new CloudSettings { CloudCenter = new Point(0, 0) }; + var cloudSettings = new Lazy (new CloudSettings { CloudCenter = new Point(0, 0) }); tagSettings = new TagSettings { TagFontSize = 32 }; var pointerSettings = new SpiralPointerSettings { AngleConst = 1, RadiusConst = 0.5, Step = 0.1 }; - var formPointer = new ArchimedeanSpiralPointer(cloudSettings, pointerSettings); + var formPointer = new ArchimedeanSpiralPointer(cloudSettings.Value, pointerSettings); tagsCloudLayouter = new TagsCloudLayouter(cloudSettings, formPointer, tagSettings); - tagsCloudLayouter.InitializeCloud(); } private TagsCloudLayouter tagsCloudLayouter; diff --git a/di.sln b/di.sln index ec7ec4d50..0d1add06c 100644 --- a/di.sln +++ b/di.sln @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TagsCloudPainterApplication EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TagsCloudPainterTests", "Tests\TagsCloudPainterTests.csproj", "{61D758B1-A9F4-4E48-B1F9-0CA1637BA9A0}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TagsCloudPainterApplicationTests", "TagsCloudPainterApplicationTests\TagsCloudPainterApplicationTests.csproj", "{506CF64F-0A6E-4055-A905-537BE33FB146}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {61D758B1-A9F4-4E48-B1F9-0CA1637BA9A0}.Debug|Any CPU.Build.0 = Debug|Any CPU {61D758B1-A9F4-4E48-B1F9-0CA1637BA9A0}.Release|Any CPU.ActiveCfg = Release|Any CPU {61D758B1-A9F4-4E48-B1F9-0CA1637BA9A0}.Release|Any CPU.Build.0 = Release|Any CPU + {506CF64F-0A6E-4055-A905-537BE33FB146}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {506CF64F-0A6E-4055-A905-537BE33FB146}.Debug|Any CPU.Build.0 = Debug|Any CPU + {506CF64F-0A6E-4055-A905-537BE33FB146}.Release|Any CPU.ActiveCfg = Release|Any CPU + {506CF64F-0A6E-4055-A905-537BE33FB146}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 6dd0cd7d08e4a87acb15f5007f8f5a758ebb4d30 Mon Sep 17 00:00:00 2001 From: daHil Date: Sat, 27 Jan 2024 09:08:20 +0500 Subject: [PATCH 20/34] refactored --- .../CloudLayouter/TagsCloudLayouter.cs | 13 +- .../FormPointer/ArchimedeanSpiralPointer.cs | 3 +- TagsCloudPainter/Settings/TextSettings.cs | 2 +- TagsCloudPainter/Tags/Tag.cs | 2 +- .../Actions/DrawTagCloudAction.cs | 1 - TagsCloudPainterApplication/App.config | 2 +- .../Infrastructure/IImageHolder.cs | 2 - .../Settings/FilesSourceSettings.cs | 10 +- .../Settings/TagsCloudSettings.cs | 3 +- TagsCloudPainterApplication/MainForm.cs | 3 +- TagsCloudPainterApplication/MainForm.resx | 3 +- TagsCloudPainterApplication/Program.cs | 2 +- .../Properties/Resources.resx | 3 +- .../Properties/Settings.Designer.cs | 9 +- .../Properties/Settings.settings | 57 +++---- .../TagsCloudPainterApplication.csproj | 6 +- .../DependencyInjectionTests.cs | 142 +++++++++--------- .../TagsCloudPainterApplicationTests.csproj | 36 ++--- Tests/TagsCloudLayouterTests.cs | 2 +- 19 files changed, 153 insertions(+), 148 deletions(-) diff --git a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs index acaee0c60..5b8b7f8d3 100644 --- a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs +++ b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs @@ -7,15 +7,10 @@ namespace TagsCloudPainter.CloudLayouter; public class TagsCloudLayouter : ICloudLayouter { + private readonly Lazy cloudSettings; private readonly IFormPointer formPointer; private readonly TagSettings tagSettings; - private readonly Lazy cloudSettings; private TagsCloud cloud; - private TagsCloud Cloud - { - get => cloud ??= new TagsCloud(cloudSettings.Value.CloudCenter, []); - set => cloud = value; - } public TagsCloudLayouter(Lazy cloudSettings, IFormPointer formPointer, TagSettings tagSettings) { @@ -24,6 +19,12 @@ public TagsCloudLayouter(Lazy cloudSettings, IFormPointer formPoi this.tagSettings = tagSettings ?? throw new ArgumentNullException(nameof(tagSettings)); } + private TagsCloud Cloud + { + get => cloud ??= new TagsCloud(cloudSettings.Value.CloudCenter, []); + set => cloud = value; + } + public Rectangle PutNextTag(Tag tag) { var rectangleSize = Utils.Utils.GetStringSize(tag.Value, tagSettings.TagFontName, tag.FontSize); diff --git a/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs b/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs index 3bd4b11a5..fb1ab7d06 100644 --- a/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs +++ b/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs @@ -16,7 +16,8 @@ public ArchimedeanSpiralPointer(CloudSettings cloudSettings, SpiralPointerSettin || spiralPointerSettings.AngleConst <= 0) throw new ArgumentException("either step or radius or angle is not possitive"); this.cloudSettings = cloudSettings ?? throw new ArgumentNullException(nameof(cloudSettings)); - this.spiralPointerSettings = spiralPointerSettings ?? throw new ArgumentNullException(nameof(spiralPointerSettings)); + this.spiralPointerSettings = + spiralPointerSettings ?? throw new ArgumentNullException(nameof(spiralPointerSettings)); сurrentDifference = 0; } diff --git a/TagsCloudPainter/Settings/TextSettings.cs b/TagsCloudPainter/Settings/TextSettings.cs index 97eae450a..26290a24d 100644 --- a/TagsCloudPainter/Settings/TextSettings.cs +++ b/TagsCloudPainter/Settings/TextSettings.cs @@ -2,5 +2,5 @@ public class TextSettings { - public string BoringText { get; set; } + public string BoringText { get; set; } = string.Empty; } \ No newline at end of file diff --git a/TagsCloudPainter/Tags/Tag.cs b/TagsCloudPainter/Tags/Tag.cs index f90b75314..f867a89b9 100644 --- a/TagsCloudPainter/Tags/Tag.cs +++ b/TagsCloudPainter/Tags/Tag.cs @@ -4,7 +4,7 @@ public class Tag { public Tag(string value, float fontSize, int count) { - Value = value; + Value = value ?? ""; FontSize = fontSize; Count = count; } diff --git a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs index fbb4f6a8b..2fd823f01 100644 --- a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs +++ b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs @@ -3,7 +3,6 @@ using TagsCloudPainter.Drawer; using TagsCloudPainter.FileReader; using TagsCloudPainter.Parser; -using TagsCloudPainter.Settings; using TagsCloudPainter.Tags; using TagsCloudPainterApplication.Infrastructure; using TagsCloudPainterApplication.Infrastructure.Settings; diff --git a/TagsCloudPainterApplication/App.config b/TagsCloudPainterApplication/App.config index 9ec0eb6bc..d521fe458 100644 --- a/TagsCloudPainterApplication/App.config +++ b/TagsCloudPainterApplication/App.config @@ -1,7 +1,7 @@  - +
diff --git a/TagsCloudPainterApplication/Infrastructure/IImageHolder.cs b/TagsCloudPainterApplication/Infrastructure/IImageHolder.cs index 4614c3386..d9ae5826e 100644 --- a/TagsCloudPainterApplication/Infrastructure/IImageHolder.cs +++ b/TagsCloudPainterApplication/Infrastructure/IImageHolder.cs @@ -1,5 +1,3 @@ -using TagsCloudPainterApplication.Infrastructure.Settings; - namespace TagsCloudPainterApplication.Infrastructure; public interface IImageHolder diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs index 9590562f0..2f42b46b8 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs @@ -2,11 +2,17 @@ public class FilesSourceSettings { - public string BoringTextFilePath { get; set; } + private string boringTextFilePath; public FilesSourceSettings() { - BoringTextFilePath = @$"{Environment.CurrentDirectory}..\..\..\..\" + Properties.Settings.Default.boringTextFilePath; + BoringTextFilePath = @$"{Environment.CurrentDirectory}..\..\..\..\" + + Properties.Settings.Default.boringTextFilePath; } + public string BoringTextFilePath + { + get => boringTextFilePath; + set => boringTextFilePath = value ?? boringTextFilePath; + } } \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs index 32c0ad2b9..7e4eb915e 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs @@ -1,5 +1,4 @@ using System.ComponentModel; -using System.Xml.Linq; using TagsCloudPainter.Settings; namespace TagsCloudPainterApplication.Infrastructure.Settings; @@ -37,7 +36,7 @@ public int TagFontSize public string TagFontName { get => TagSettings.TagFontName; - set => TagSettings.TagFontName = value; + set => TagSettings.TagFontName = value ?? TagSettings.TagFontName; } public Point CloudCenter diff --git a/TagsCloudPainterApplication/MainForm.cs b/TagsCloudPainterApplication/MainForm.cs index 2c35e7d4f..48174ba74 100644 --- a/TagsCloudPainterApplication/MainForm.cs +++ b/TagsCloudPainterApplication/MainForm.cs @@ -1,4 +1,3 @@ -using TagsCloudPainter.Settings; using TagsCloudPainterApplication.Actions; using TagsCloudPainterApplication.Infrastructure.Settings; @@ -8,7 +7,7 @@ public partial class MainForm : Form { public MainForm(IUiAction[] actions, ImageSettings imageSettings, PictureBoxImageHolder pictureBox) { - if(actions is null || actions.Length == 0 || imageSettings is null || pictureBox is null) + if (actions is null || actions.Length == 0 || imageSettings is null || pictureBox is null) throw new ArgumentNullException("MainForm cann't be injected with nullable reference"); ClientSize = new Size(imageSettings.Width, imageSettings.Height); diff --git a/TagsCloudPainterApplication/MainForm.resx b/TagsCloudPainterApplication/MainForm.resx index 0ff5d7eaa..d4ad6f224 100644 --- a/TagsCloudPainterApplication/MainForm.resx +++ b/TagsCloudPainterApplication/MainForm.resx @@ -59,7 +59,8 @@ : using a System.ComponentModel.TypeConverter : and then encoded with base64 encoding. --> - diff --git a/TagsCloudPainterApplication/Program.cs b/TagsCloudPainterApplication/Program.cs index 32967aa81..d568e32cc 100644 --- a/TagsCloudPainterApplication/Program.cs +++ b/TagsCloudPainterApplication/Program.cs @@ -31,7 +31,7 @@ private static void Main() } } -public class ApplicationModule: Module +public class ApplicationModule : Module { protected override void Load(ContainerBuilder builder) { diff --git a/TagsCloudPainterApplication/Properties/Resources.resx b/TagsCloudPainterApplication/Properties/Resources.resx index 668cdff93..3627ee652 100644 --- a/TagsCloudPainterApplication/Properties/Resources.resx +++ b/TagsCloudPainterApplication/Properties/Resources.resx @@ -59,7 +59,8 @@ : using a System.ComponentModel.TypeConverter : and then encoded with base64 encoding. --> - diff --git a/TagsCloudPainterApplication/Properties/Settings.Designer.cs b/TagsCloudPainterApplication/Properties/Settings.Designer.cs index bc0ee6bdb..75d28af2d 100644 --- a/TagsCloudPainterApplication/Properties/Settings.Designer.cs +++ b/TagsCloudPainterApplication/Properties/Settings.Designer.cs @@ -1,10 +1,9 @@ //------------------------------------------------------------------------------ // -// Этот код создан программой. -// Исполняемая версия:4.0.30319.42000 +// This code was generated by a tool. // -// Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае -// повторной генерации кода. +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. // //------------------------------------------------------------------------------ @@ -12,7 +11,7 @@ namespace TagsCloudPainterApplication.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.8.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.9.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); diff --git a/TagsCloudPainterApplication/Properties/Settings.settings b/TagsCloudPainterApplication/Properties/Settings.settings index db14197f7..6a5822734 100644 --- a/TagsCloudPainterApplication/Properties/Settings.settings +++ b/TagsCloudPainterApplication/Properties/Settings.settings @@ -1,30 +1,31 @@  - - - - - Data\BoringWords.txt - - - 800 - - - 600 - - - 32 - - - Arial - - - 0.1 - - - 0.5 - - - 1 - - + + + + + Data\BoringWords.txt + + + 800 + + + 600 + + + 32 + + + Arial + + + 0.1 + + + 0.5 + + + 1 + + \ No newline at end of file diff --git a/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj b/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj index 3af7f7ce3..bf172d2dd 100644 --- a/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj +++ b/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj @@ -10,11 +10,11 @@ - + - + @@ -36,7 +36,7 @@ - + \ No newline at end of file diff --git a/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs b/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs index 3daf0bbcc..ae0946fd4 100644 --- a/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs +++ b/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs @@ -11,88 +11,88 @@ using TagsCloudPainterApplication.Infrastructure; using TagsCloudPainterApplication.Infrastructure.Settings; -namespace TagsCloudPainterApplicationTests +namespace TagsCloudPainterApplicationTests; + +[TestFixture] +public class DependencyInjectionTests { - [TestFixture] - public class DependencyInjectionTests + [SetUp] + public void Setup() { - private ILifetimeScope scope; + var builder = new ContainerBuilder(); + builder.RegisterModule(); + builder.RegisterModule(); + var container = builder.Build(); + scope = container.BeginLifetimeScope(); + } - [SetUp] - public void Setup() - { - var builder = new ContainerBuilder(); - builder.RegisterModule(); - builder.RegisterModule(); - var container = builder.Build(); - scope = container.BeginLifetimeScope(); - } + [TearDown] + public void TearDown() + { + scope.Dispose(); + } - [TearDown] - public void TearDown() - { - scope.Dispose(); - } + private ILifetimeScope scope; - private static IEnumerable InstancePerLifetimeScopeDependencesTypes => new[] - { - new TestCaseData(typeof(IFormPointer)), - new TestCaseData(typeof(ICloudLayouter)), - new TestCaseData(typeof(IUiAction)), - new TestCaseData(typeof(MainForm)), - }; + private static IEnumerable InstancePerLifetimeScopeDependencesTypes => new[] + { + new TestCaseData(typeof(IFormPointer)), + new TestCaseData(typeof(ICloudLayouter)), + new TestCaseData(typeof(IUiAction)), + new TestCaseData(typeof(MainForm)) + }; - private static IEnumerable SingleInstanceDependencesTypes => new[] - { - new TestCaseData(typeof(TagSettings)), - new TestCaseData(typeof(CloudDrawer)), - new TestCaseData(typeof(IFileReader)), - new TestCaseData(typeof(ITextParser)), - new TestCaseData(typeof(ITagsBuilder)), - new TestCaseData(typeof(TextSettings)), - new TestCaseData(typeof(CloudSettings)), - new TestCaseData(typeof(SpiralPointerSettings)), - new TestCaseData(typeof(Palette)), - new TestCaseData(typeof(ImageSettings)), - new TestCaseData(typeof(FilesSourceSettings)), - new TestCaseData(typeof(TagsCloudSettings)), - new TestCaseData(typeof(PictureBoxImageHolder)), - new TestCaseData(typeof(IImageHolder)), - }; + private static IEnumerable SingleInstanceDependencesTypes => new[] + { + new TestCaseData(typeof(TagSettings)), + new TestCaseData(typeof(CloudDrawer)), + new TestCaseData(typeof(IFileReader)), + new TestCaseData(typeof(ITextParser)), + new TestCaseData(typeof(ITagsBuilder)), + new TestCaseData(typeof(TextSettings)), + new TestCaseData(typeof(CloudSettings)), + new TestCaseData(typeof(SpiralPointerSettings)), + new TestCaseData(typeof(Palette)), + new TestCaseData(typeof(ImageSettings)), + new TestCaseData(typeof(FilesSourceSettings)), + new TestCaseData(typeof(TagsCloudSettings)), + new TestCaseData(typeof(PictureBoxImageHolder)), + new TestCaseData(typeof(IImageHolder)) + }; - [TestCaseSource(nameof(SingleInstanceDependencesTypes))] - [TestCaseSource(nameof(InstancePerLifetimeScopeDependencesTypes))] - public void Dependence_SouldResolve(Type dependenceType) - { - Assert.DoesNotThrow(() => scope.Resolve(dependenceType)); - } + [TestCaseSource(nameof(SingleInstanceDependencesTypes))] + [TestCaseSource(nameof(InstancePerLifetimeScopeDependencesTypes))] + public void Dependence_SouldResolve(Type dependenceType) + { + Assert.DoesNotThrow(() => scope.Resolve(dependenceType)); + } - [TestCaseSource(nameof(SingleInstanceDependencesTypes))] - [TestCaseSource(nameof(InstancePerLifetimeScopeDependencesTypes))] - public void Dependence_SouldBeNotNull(Type dependenceType) - { - var dependence = scope.Resolve(dependenceType); - Assert.That(dependence, Is.Not.Null); - } + [TestCaseSource(nameof(SingleInstanceDependencesTypes))] + [TestCaseSource(nameof(InstancePerLifetimeScopeDependencesTypes))] + public void Dependence_SouldBeNotNull(Type dependenceType) + { + var dependence = scope.Resolve(dependenceType); + Assert.That(dependence, Is.Not.Null); + } - [TestCaseSource(nameof(SingleInstanceDependencesTypes))] - public void SingleInstanceDependence_ShouldResolveSameReferenceInDifferentScopes(Type dependenceType) - { - using var childScope = scope.BeginLifetimeScope(); - var dependence1 = scope.Resolve(dependenceType); - var dependence2 = childScope.Resolve(dependenceType); + [TestCaseSource(nameof(SingleInstanceDependencesTypes))] + public void SingleInstanceDependence_ShouldResolveSameReferenceInDifferentScopes(Type dependenceType) + { + using var childScope = scope.BeginLifetimeScope(); + var dependence1 = scope.Resolve(dependenceType); + var dependence2 = childScope.Resolve(dependenceType); - Assert.That(dependence2, Is.SameAs(dependence1)); - } + Assert.That(dependence2, Is.SameAs(dependence1)); + } - [TestCaseSource(nameof(InstancePerLifetimeScopeDependencesTypes))] - public void InstancePerLifetimeScopeDependence_ShouldResolveDifferentReferencesInDifferentScopes(Type dependenceType) - { - using var childScope = scope.BeginLifetimeScope(); - var dependence1 = scope.Resolve(dependenceType); - var dependence2 = childScope.Resolve(dependenceType); + [TestCaseSource(nameof(InstancePerLifetimeScopeDependencesTypes))] + public void InstancePerLifetimeScopeDependence_ShouldResolveDifferentReferencesInDifferentScopes( + Type dependenceType) + { + using var childScope = scope.BeginLifetimeScope(); + var dependence1 = scope.Resolve(dependenceType); + var dependence2 = childScope.Resolve(dependenceType); - Assert.That(dependence2, Is.Not.EqualTo(dependence1)); - } + Assert.That(dependence2, Is.Not.EqualTo(dependence1)); } } \ No newline at end of file diff --git a/TagsCloudPainterApplicationTests/TagsCloudPainterApplicationTests.csproj b/TagsCloudPainterApplicationTests/TagsCloudPainterApplicationTests.csproj index 47deafbe2..51017c60d 100644 --- a/TagsCloudPainterApplicationTests/TagsCloudPainterApplicationTests.csproj +++ b/TagsCloudPainterApplicationTests/TagsCloudPainterApplicationTests.csproj @@ -1,25 +1,25 @@ - - net8.0-windows - enable - enable + + net8.0-windows + enable + enable - false - true - Library - + false + true + Library + - - - - - - - + + + + + + + - - - + + + diff --git a/Tests/TagsCloudLayouterTests.cs b/Tests/TagsCloudLayouterTests.cs index 67317a122..3eeece0f0 100644 --- a/Tests/TagsCloudLayouterTests.cs +++ b/Tests/TagsCloudLayouterTests.cs @@ -14,7 +14,7 @@ public class TagsCloudLayouterTests [SetUp] public void Setup() { - var cloudSettings = new Lazy (new CloudSettings { CloudCenter = new Point(0, 0) }); + var cloudSettings = new Lazy(new CloudSettings { CloudCenter = new Point(0, 0) }); tagSettings = new TagSettings { TagFontSize = 32 }; var pointerSettings = new SpiralPointerSettings { AngleConst = 1, RadiusConst = 0.5, Step = 0.1 }; var formPointer = new ArchimedeanSpiralPointer(cloudSettings.Value, pointerSettings); From 3a801dda7a3194e17c1c7752a817e0a45f7470ff Mon Sep 17 00:00:00 2001 From: daHil Date: Sun, 28 Jan 2024 01:15:56 +0500 Subject: [PATCH 21/34] app settings now passed through container --- .../Infrastructure/Settings/FilesSourceSettings.cs | 9 +++++---- .../Infrastructure/Settings/ImageSettings.cs | 13 ++++++++++--- .../Infrastructure/Settings/TagsCloudSettings.cs | 10 +++++----- TagsCloudPainterApplication/Program.cs | 2 ++ .../Properties/Settings.Designer.cs | 6 +++--- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs index 2f42b46b8..559d02155 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs @@ -1,13 +1,14 @@ -namespace TagsCloudPainterApplication.Infrastructure.Settings; +using TagsCloudPainterApplication.Properties; + +namespace TagsCloudPainterApplication.Infrastructure.Settings; public class FilesSourceSettings { private string boringTextFilePath; - public FilesSourceSettings() + public FilesSourceSettings(AppSettings settings) { - BoringTextFilePath = @$"{Environment.CurrentDirectory}..\..\..\..\" + - Properties.Settings.Default.boringTextFilePath; + BoringTextFilePath = @$"{Environment.CurrentDirectory}..\..\..\..\" + settings.boringTextFilePath; } public string BoringTextFilePath diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs index 9b7abf90e..106e29019 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs @@ -1,7 +1,14 @@ -namespace TagsCloudPainterApplication.Infrastructure.Settings; +using TagsCloudPainterApplication.Properties; + +namespace TagsCloudPainterApplication.Infrastructure.Settings; public class ImageSettings { - public int Width { get; set; } = Properties.Settings.Default.imageWidth; - public int Height { get; set; } = Properties.Settings.Default.imageHeight; + public ImageSettings(AppSettings settings) + { + Width = settings.imageWidth; + Height = settings.imageHeight; + } + public int Width { get; set; } + public int Height { get; set; } } \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs index 7e4eb915e..c29b86bbd 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs @@ -15,11 +15,11 @@ public TagsCloudSettings( TagSettings = tagSettings ?? throw new ArgumentNullException(nameof(tagSettings)); SpiralPointerSettings = spiralPointerSettings ?? throw new ArgumentNullException(nameof(spiralPointerSettings)); TextSettings = textSettings ?? throw new ArgumentNullException(nameof(textSettings)); - TagFontSize = Properties.Settings.Default.tagFontSize; - TagFontName = Properties.Settings.Default.tagFontName; - PointerStep = Properties.Settings.Default.pointerStep; - PointerRadiusConst = Properties.Settings.Default.pointerRadiusConst; - PointerAngleConst = Properties.Settings.Default.pointerAngleConst; + TagFontSize = Properties.AppSettings.Default.tagFontSize; + TagFontName = Properties.AppSettings.Default.tagFontName; + PointerStep = Properties.AppSettings.Default.pointerStep; + PointerRadiusConst = Properties.AppSettings.Default.pointerRadiusConst; + PointerAngleConst = Properties.AppSettings.Default.pointerAngleConst; } [Browsable(false)] public CloudSettings CloudSettings { get; } diff --git a/TagsCloudPainterApplication/Program.cs b/TagsCloudPainterApplication/Program.cs index d568e32cc..c7946f92d 100644 --- a/TagsCloudPainterApplication/Program.cs +++ b/TagsCloudPainterApplication/Program.cs @@ -9,6 +9,7 @@ using TagsCloudPainterApplication.Actions; using TagsCloudPainterApplication.Infrastructure; using TagsCloudPainterApplication.Infrastructure.Settings; +using TagsCloudPainterApplication.Properties; namespace TagsCloudPainterApplication; @@ -45,6 +46,7 @@ protected override void Load(ContainerBuilder builder) builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); + builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); } } diff --git a/TagsCloudPainterApplication/Properties/Settings.Designer.cs b/TagsCloudPainterApplication/Properties/Settings.Designer.cs index 75d28af2d..8b1faef14 100644 --- a/TagsCloudPainterApplication/Properties/Settings.Designer.cs +++ b/TagsCloudPainterApplication/Properties/Settings.Designer.cs @@ -12,11 +12,11 @@ namespace TagsCloudPainterApplication.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.9.0.0")] - internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + public sealed partial class AppSettings : global::System.Configuration.ApplicationSettingsBase { - private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + private static AppSettings defaultInstance = ((AppSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new AppSettings()))); - public static Settings Default { + public static AppSettings Default { get { return defaultInstance; } From 49959c5009398f35189ab35cdbd1515ab412708b Mon Sep 17 00:00:00 2001 From: daHil Date: Sun, 28 Jan 2024 05:59:02 +0500 Subject: [PATCH 22/34] refactored exceptions throwing --- TagsCloudPainterApplication/MainForm.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/TagsCloudPainterApplication/MainForm.cs b/TagsCloudPainterApplication/MainForm.cs index 48174ba74..3d33fecfc 100644 --- a/TagsCloudPainterApplication/MainForm.cs +++ b/TagsCloudPainterApplication/MainForm.cs @@ -7,8 +7,11 @@ public partial class MainForm : Form { public MainForm(IUiAction[] actions, ImageSettings imageSettings, PictureBoxImageHolder pictureBox) { - if (actions is null || actions.Length == 0 || imageSettings is null || pictureBox is null) - throw new ArgumentNullException("MainForm cann't be injected with nullable reference"); + ArgumentNullException.ThrowIfNull(actions); + if (actions.Count() == 0) + throw new ArgumentException(nameof(actions)); + ArgumentNullException.ThrowIfNull(imageSettings); + ArgumentNullException.ThrowIfNull(pictureBox); ClientSize = new Size(imageSettings.Width, imageSettings.Height); From 74c891e09e527aeb309845c5dbf5a96f87874e08 Mon Sep 17 00:00:00 2001 From: daHil Date: Sun, 28 Jan 2024 06:01:43 +0500 Subject: [PATCH 23/34] added app settings into di tests --- TagsCloudPainterApplication/Program.cs | 2 +- TagsCloudPainterApplicationTests/DependencyInjectionTests.cs | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/TagsCloudPainterApplication/Program.cs b/TagsCloudPainterApplication/Program.cs index c7946f92d..fec83a187 100644 --- a/TagsCloudPainterApplication/Program.cs +++ b/TagsCloudPainterApplication/Program.cs @@ -41,12 +41,12 @@ protected override void Load(ContainerBuilder builder) builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().As().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); - builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); } } diff --git a/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs b/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs index ae0946fd4..9dc651d67 100644 --- a/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs +++ b/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs @@ -10,6 +10,7 @@ using TagsCloudPainterApplication.Actions; using TagsCloudPainterApplication.Infrastructure; using TagsCloudPainterApplication.Infrastructure.Settings; +using TagsCloudPainterApplication.Properties; namespace TagsCloudPainterApplicationTests; @@ -57,7 +58,8 @@ public void TearDown() new TestCaseData(typeof(FilesSourceSettings)), new TestCaseData(typeof(TagsCloudSettings)), new TestCaseData(typeof(PictureBoxImageHolder)), - new TestCaseData(typeof(IImageHolder)) + new TestCaseData(typeof(IImageHolder)), + new TestCaseData(typeof(AppSettings)) }; [TestCaseSource(nameof(SingleInstanceDependencesTypes))] From 47ec8ee52d30b9ec472ac20089f1afd149696bb7 Mon Sep 17 00:00:00 2001 From: daHil Date: Sun, 28 Jan 2024 06:03:57 +0500 Subject: [PATCH 24/34] now actions can be empty --- TagsCloudPainterApplication/MainForm.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/TagsCloudPainterApplication/MainForm.cs b/TagsCloudPainterApplication/MainForm.cs index 3d33fecfc..407ffe90c 100644 --- a/TagsCloudPainterApplication/MainForm.cs +++ b/TagsCloudPainterApplication/MainForm.cs @@ -8,8 +8,6 @@ public partial class MainForm : Form public MainForm(IUiAction[] actions, ImageSettings imageSettings, PictureBoxImageHolder pictureBox) { ArgumentNullException.ThrowIfNull(actions); - if (actions.Count() == 0) - throw new ArgumentException(nameof(actions)); ArgumentNullException.ThrowIfNull(imageSettings); ArgumentNullException.ThrowIfNull(pictureBox); From 1b9fe40583509b8a8f36ca02c1ca38f36a9e0b10 Mon Sep 17 00:00:00 2001 From: daHil Date: Wed, 31 Jan 2024 07:39:06 +0500 Subject: [PATCH 25/34] refactored after feedback --- TagsCloudPainter/CloudDrawer/CloudDrawer.cs | 23 ++++++------- .../CloudLayouter/TagsCloudLayouter.cs | 18 +++++++--- TagsCloudPainter/Extensions/PointExtension.cs | 14 ++++++++ .../Extensions/RectangleExtensions.cs | 13 ++++++++ TagsCloudPainter/FileReader/DocFileReader.cs | 15 +++++++++ TagsCloudPainter/FileReader/TextFileReader.cs | 27 ++++----------- TagsCloudPainter/FileReader/TxtFileReader.cs | 9 +++++ TagsCloudPainter/Settings/TagSettings.cs | 3 +- TagsCloudPainter/Sizer/IStringSizer.cs | 8 +++++ TagsCloudPainter/Sizer/StringSizer.cs | 16 +++++++++ TagsCloudPainter/Tags/Tag.cs | 3 +- TagsCloudPainter/Utils/Utils.cs | 31 ------------------ .../Actions/DrawTagCloudAction.cs | 6 ++-- TagsCloudPainterApplication/App.config | 19 +++++++---- .../Infrastructure/Settings/ImageSettings.cs | 3 +- .../Settings/TagsCloudSettings.cs | 11 ++++--- TagsCloudPainterApplication/Program.cs | 2 ++ Tests/BoringTextParserTests.cs | 25 +++++--------- Tests/TagsCloudLayouterTests.cs | 14 ++++---- Tests/TextFileReaderTests.cs | 7 +++- Tests/TextFiles/boringWords.txt | 3 -- Tests/TextFiles/testFile.doc | Bin 0 -> 25600 bytes 22 files changed, 160 insertions(+), 110 deletions(-) create mode 100644 TagsCloudPainter/Extensions/PointExtension.cs create mode 100644 TagsCloudPainter/Extensions/RectangleExtensions.cs create mode 100644 TagsCloudPainter/FileReader/DocFileReader.cs create mode 100644 TagsCloudPainter/FileReader/TxtFileReader.cs create mode 100644 TagsCloudPainter/Sizer/IStringSizer.cs create mode 100644 TagsCloudPainter/Sizer/StringSizer.cs delete mode 100644 TagsCloudPainter/Utils/Utils.cs delete mode 100644 Tests/TextFiles/boringWords.txt create mode 100644 Tests/TextFiles/testFile.doc diff --git a/TagsCloudPainter/CloudDrawer/CloudDrawer.cs b/TagsCloudPainter/CloudDrawer/CloudDrawer.cs index d34310ca7..70e6041c7 100644 --- a/TagsCloudPainter/CloudDrawer/CloudDrawer.cs +++ b/TagsCloudPainter/CloudDrawer/CloudDrawer.cs @@ -24,19 +24,20 @@ public Bitmap DrawCloud(TagsCloud cloud, int imageWidth, int imageHeight) var drawingScale = CalculateObjectDrawingScale(cloud.GetWidth(), cloud.GetHeight(), imageWidth, imageHeight); var bitmap = new Bitmap(imageWidth, imageHeight); - var graphics = Graphics.FromImage(bitmap); - var pen = new Pen(tagSettings.TagColor); - - graphics.TranslateTransform(-cloud.Center.X, -cloud.Center.Y); - graphics.ScaleTransform(drawingScale, drawingScale, MatrixOrder.Append); - graphics.TranslateTransform(cloud.Center.X, cloud.Center.Y, MatrixOrder.Append); - graphics.Clear(cloudSettings.BackgroundColor); - foreach (var tag in cloud.Tags) + using var graphics = Graphics.FromImage(bitmap); + using var pen = new Pen(tagSettings.TagColor); { - var font = new Font(tagSettings.TagFontName, tag.Key.FontSize); - graphics.DrawString(tag.Key.Value, font, pen.Brush, tag.Value.Location); + graphics.TranslateTransform(-cloud.Center.X, -cloud.Center.Y); + graphics.ScaleTransform(drawingScale, drawingScale, MatrixOrder.Append); + graphics.TranslateTransform(cloud.Center.X, cloud.Center.Y, MatrixOrder.Append); + graphics.Clear(cloudSettings.BackgroundColor); + foreach (var tag in cloud.Tags) + { + var font = new Font(tagSettings.TagFontName, tag.Key.FontSize); + graphics.DrawString(tag.Key.Value, font, pen.Brush, tag.Value.Location); + } } - + ; return bitmap; } diff --git a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs index 5b8b7f8d3..7c4cf4e8f 100644 --- a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs +++ b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs @@ -1,6 +1,8 @@ using System.Drawing; +using TagsCloudPainter.Extensions; using TagsCloudPainter.FormPointer; using TagsCloudPainter.Settings; +using TagsCloudPainter.Sizer; using TagsCloudPainter.Tags; namespace TagsCloudPainter.CloudLayouter; @@ -9,14 +11,20 @@ public class TagsCloudLayouter : ICloudLayouter { private readonly Lazy cloudSettings; private readonly IFormPointer formPointer; + private readonly IStringSizer stringSizer; private readonly TagSettings tagSettings; private TagsCloud cloud; - public TagsCloudLayouter(Lazy cloudSettings, IFormPointer formPointer, TagSettings tagSettings) + public TagsCloudLayouter( + Lazy cloudSettings, + IFormPointer formPointer, + TagSettings tagSettings, + IStringSizer stringSizer) { this.cloudSettings = cloudSettings ?? throw new ArgumentNullException(nameof(cloudSettings)); this.formPointer = formPointer ?? throw new ArgumentNullException(nameof(formPointer)); this.tagSettings = tagSettings ?? throw new ArgumentNullException(nameof(tagSettings)); + this.stringSizer = stringSizer ?? throw new ArgumentNullException(); } private TagsCloud Cloud @@ -27,13 +35,13 @@ private TagsCloud Cloud public Rectangle PutNextTag(Tag tag) { - var rectangleSize = Utils.Utils.GetStringSize(tag.Value, tagSettings.TagFontName, tag.FontSize); - if (rectangleSize.Height <= 0 || rectangleSize.Width <= 0) + var tagSize = stringSizer.GetStringSize(tag.Value, tagSettings.TagFontName, tag.FontSize); + if (tagSize.Height <= 0 || tagSize.Width <= 0) throw new ArgumentException("either width or height of rectangle size is not possitive"); - var nextRectangle = Utils.Utils.GetRectangleFromCenter(formPointer.GetNextPoint(), rectangleSize); + var nextRectangle = formPointer.GetNextPoint().GetRectangle(tagSize); while (Cloud.Tags.Values.Any(rectangle => rectangle.IntersectsWith(nextRectangle))) - nextRectangle = Utils.Utils.GetRectangleFromCenter(formPointer.GetNextPoint(), rectangleSize); + nextRectangle = formPointer.GetNextPoint().GetRectangle(tagSize); Cloud.AddTag(tag, nextRectangle); diff --git a/TagsCloudPainter/Extensions/PointExtension.cs b/TagsCloudPainter/Extensions/PointExtension.cs new file mode 100644 index 000000000..97cb37bc1 --- /dev/null +++ b/TagsCloudPainter/Extensions/PointExtension.cs @@ -0,0 +1,14 @@ +using System.Drawing; + +namespace TagsCloudPainter.Extensions; + +public static class PointExtension +{ + public static Rectangle GetRectangle(this Point center, Size size) + { + var x = center.X - size.Width / 2; + var y = center.Y - size.Height / 2; + + return new Rectangle(new Point(x, y), size); + } +} \ No newline at end of file diff --git a/TagsCloudPainter/Extensions/RectangleExtensions.cs b/TagsCloudPainter/Extensions/RectangleExtensions.cs new file mode 100644 index 000000000..11939e807 --- /dev/null +++ b/TagsCloudPainter/Extensions/RectangleExtensions.cs @@ -0,0 +1,13 @@ +using System.Drawing; + +namespace TagsCloudPainter.Extensions; + +public static class RectangleExtensions +{ + public static Point GetCenter(this Rectangle rectangle) + { + var x = rectangle.X; + var y = rectangle.Y; + return new Point(x + rectangle.Width / 2, y + rectangle.Height / 2); + } +} \ No newline at end of file diff --git a/TagsCloudPainter/FileReader/DocFileReader.cs b/TagsCloudPainter/FileReader/DocFileReader.cs new file mode 100644 index 000000000..db5fc6d50 --- /dev/null +++ b/TagsCloudPainter/FileReader/DocFileReader.cs @@ -0,0 +1,15 @@ +using Spire.Doc; + +namespace TagsCloudPainter.FileReader; + +internal class DocFileReader : IFileReader +{ + public string ReadFile(string path) + { + var doc = new Document(); + doc.LoadFromFile(path); + var text = doc.GetText(); + var lastIndexOfSpirePart = text.IndexOf(Environment.NewLine); + return text.Substring(lastIndexOfSpirePart + 2).Trim(); + } +} \ No newline at end of file diff --git a/TagsCloudPainter/FileReader/TextFileReader.cs b/TagsCloudPainter/FileReader/TextFileReader.cs index c62379ab9..7a03b1f5f 100644 --- a/TagsCloudPainter/FileReader/TextFileReader.cs +++ b/TagsCloudPainter/FileReader/TextFileReader.cs @@ -1,9 +1,10 @@ -using Spire.Doc; - -namespace TagsCloudPainter.FileReader; +namespace TagsCloudPainter.FileReader; public class TextFileReader : IFileReader { + private readonly DocFileReader docFileReader = new(); + private readonly TxtFileReader txtFileReader = new(); + public string ReadFile(string path) { if (!File.Exists(path)) @@ -11,24 +12,10 @@ public string ReadFile(string path) return Path.GetExtension(path) switch { - ".txt" => ReadTxtFile(path), - ".doc" => ReadDocFile(path), - ".docx" => ReadDocFile(path), + ".txt" => txtFileReader.ReadFile(path), + ".doc" => docFileReader.ReadFile(path), + ".docx" => docFileReader.ReadFile(path), _ => throw new ArgumentException("Incorrect file extension. Supported file extensions: txt, doc, docx") }; } - - private static string ReadTxtFile(string path) - { - return File.ReadAllText(path).Trim(); - } - - private static string ReadDocFile(string path) - { - var doc = new Document(); - doc.LoadFromFile(path); - var text = doc.GetText(); - var lastIndexOfSpirePart = text.IndexOf(Environment.NewLine); - return text.Substring(lastIndexOfSpirePart + 2).Trim(); - } } \ No newline at end of file diff --git a/TagsCloudPainter/FileReader/TxtFileReader.cs b/TagsCloudPainter/FileReader/TxtFileReader.cs new file mode 100644 index 000000000..3377dba3f --- /dev/null +++ b/TagsCloudPainter/FileReader/TxtFileReader.cs @@ -0,0 +1,9 @@ +namespace TagsCloudPainter.FileReader; + +internal class TxtFileReader : IFileReader +{ + public string ReadFile(string path) + { + return File.ReadAllText(path).Trim(); + } +} \ No newline at end of file diff --git a/TagsCloudPainter/Settings/TagSettings.cs b/TagsCloudPainter/Settings/TagSettings.cs index c0090cd1e..278c05008 100644 --- a/TagsCloudPainter/Settings/TagSettings.cs +++ b/TagsCloudPainter/Settings/TagSettings.cs @@ -1,10 +1,11 @@ using System.Drawing; +using System.Drawing.Text; namespace TagsCloudPainter.Settings; public class TagSettings { public int TagFontSize { get; set; } - public string TagFontName { get; set; } = "Arial"; + public string TagFontName { get; set; } = new InstalledFontCollection().Families.FirstOrDefault().Name; public Color TagColor { get; set; } } \ No newline at end of file diff --git a/TagsCloudPainter/Sizer/IStringSizer.cs b/TagsCloudPainter/Sizer/IStringSizer.cs new file mode 100644 index 000000000..4cca958b4 --- /dev/null +++ b/TagsCloudPainter/Sizer/IStringSizer.cs @@ -0,0 +1,8 @@ +using System.Drawing; + +namespace TagsCloudPainter.Sizer; + +public interface IStringSizer +{ + Size GetStringSize(string value, string fontName, float fontSize); +} \ No newline at end of file diff --git a/TagsCloudPainter/Sizer/StringSizer.cs b/TagsCloudPainter/Sizer/StringSizer.cs new file mode 100644 index 000000000..76dffbb66 --- /dev/null +++ b/TagsCloudPainter/Sizer/StringSizer.cs @@ -0,0 +1,16 @@ +using System.Drawing; + +namespace TagsCloudPainter.Sizer; + +public class StringSizer : IStringSizer +{ + public Size GetStringSize(string value, string fontName, float fontSize) + { + using var graphics = Graphics.FromHwnd(IntPtr.Zero); + using var font = new Font(fontName, fontSize); + { + var size = graphics.MeasureString(value, font).ToSize(); + return size; + } + } +} \ No newline at end of file diff --git a/TagsCloudPainter/Tags/Tag.cs b/TagsCloudPainter/Tags/Tag.cs index f867a89b9..bdf1617c8 100644 --- a/TagsCloudPainter/Tags/Tag.cs +++ b/TagsCloudPainter/Tags/Tag.cs @@ -4,7 +4,8 @@ public class Tag { public Tag(string value, float fontSize, int count) { - Value = value ?? ""; + ArgumentException.ThrowIfNullOrEmpty(value, nameof(value)); + Value = value; FontSize = fontSize; Count = count; } diff --git a/TagsCloudPainter/Utils/Utils.cs b/TagsCloudPainter/Utils/Utils.cs deleted file mode 100644 index 40d2cdcfd..000000000 --- a/TagsCloudPainter/Utils/Utils.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System.Drawing; - -namespace TagsCloudPainter.Utils; - -public static class Utils -{ - public static Rectangle GetRectangleFromCenter(Point center, Size size) - { - var x = center.X - size.Width / 2; - var y = center.Y - size.Height / 2; - - return new Rectangle(new Point(x, y), size); - } - - public static Point GetRectangleCenter(Rectangle rectangle) - { - var x = rectangle.X; - var y = rectangle.Y; - return new Point(x + rectangle.Width / 2, y + rectangle.Height / 2); - } - - public static Size GetStringSize(string value, string fontName, float fontSize) - { - using var graphics = Graphics.FromHwnd(IntPtr.Zero); - using var font = new Font(fontName, fontSize); - { - var size = graphics.MeasureString(value, font).ToSize(); - return size; - } - } -} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs index 2fd823f01..2af2dbd74 100644 --- a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs +++ b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs @@ -55,6 +55,9 @@ public DrawTagCloudAction( public void Perform() { var wordsFilePath = GetFilePath(); + if (string.IsNullOrEmpty(wordsFilePath)) + return; + SettingsForm.For(tagsCloudSettings).ShowDialog(); tagsCloudSettings.CloudSettings.BackgroundColor = palette.BackgroundColor; tagsCloudSettings.TagSettings.TagColor = palette.PrimaryColor; @@ -91,8 +94,7 @@ private TagsCloud GetCloud(List words) private void DrawCloud(TagsCloud cloud) { - var bitmap = cloudDrawer.DrawCloud(cloud, imageSettings.Width, imageSettings.Height); - + using var bitmap = cloudDrawer.DrawCloud(cloud, imageSettings.Width, imageSettings.Height); using (var graphics = imageHolder.StartDrawing()) { graphics.DrawImage(bitmap, new Point(0, 0)); diff --git a/TagsCloudPainterApplication/App.config b/TagsCloudPainterApplication/App.config index d521fe458..4ea15faf7 100644 --- a/TagsCloudPainterApplication/App.config +++ b/TagsCloudPainterApplication/App.config @@ -1,22 +1,27 @@  - -
+ +
- + - - + + - - + + diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs index 106e29019..a097f57d4 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs @@ -4,11 +4,12 @@ namespace TagsCloudPainterApplication.Infrastructure.Settings; public class ImageSettings { - public ImageSettings(AppSettings settings) + public ImageSettings(AppSettings settings) { Width = settings.imageWidth; Height = settings.imageHeight; } + public int Width { get; set; } public int Height { get; set; } } \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs index c29b86bbd..cc0c5793e 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs @@ -1,5 +1,6 @@ using System.ComponentModel; using TagsCloudPainter.Settings; +using TagsCloudPainterApplication.Properties; namespace TagsCloudPainterApplication.Infrastructure.Settings; @@ -15,11 +16,11 @@ public TagsCloudSettings( TagSettings = tagSettings ?? throw new ArgumentNullException(nameof(tagSettings)); SpiralPointerSettings = spiralPointerSettings ?? throw new ArgumentNullException(nameof(spiralPointerSettings)); TextSettings = textSettings ?? throw new ArgumentNullException(nameof(textSettings)); - TagFontSize = Properties.AppSettings.Default.tagFontSize; - TagFontName = Properties.AppSettings.Default.tagFontName; - PointerStep = Properties.AppSettings.Default.pointerStep; - PointerRadiusConst = Properties.AppSettings.Default.pointerRadiusConst; - PointerAngleConst = Properties.AppSettings.Default.pointerAngleConst; + TagFontSize = AppSettings.Default.tagFontSize; + TagFontName = AppSettings.Default.tagFontName; + PointerStep = AppSettings.Default.pointerStep; + PointerRadiusConst = AppSettings.Default.pointerRadiusConst; + PointerAngleConst = AppSettings.Default.pointerAngleConst; } [Browsable(false)] public CloudSettings CloudSettings { get; } diff --git a/TagsCloudPainterApplication/Program.cs b/TagsCloudPainterApplication/Program.cs index fec83a187..3fd100e6c 100644 --- a/TagsCloudPainterApplication/Program.cs +++ b/TagsCloudPainterApplication/Program.cs @@ -5,6 +5,7 @@ using TagsCloudPainter.FormPointer; using TagsCloudPainter.Parser; using TagsCloudPainter.Settings; +using TagsCloudPainter.Sizer; using TagsCloudPainter.Tags; using TagsCloudPainterApplication.Actions; using TagsCloudPainterApplication.Infrastructure; @@ -66,5 +67,6 @@ protected override void Load(ContainerBuilder builder) builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); } } \ No newline at end of file diff --git a/Tests/BoringTextParserTests.cs b/Tests/BoringTextParserTests.cs index 6d589a222..462e9e534 100644 --- a/Tests/BoringTextParserTests.cs +++ b/Tests/BoringTextParserTests.cs @@ -1,5 +1,4 @@ using FluentAssertions; -using TagsCloudPainter.FileReader; using TagsCloudPainter.Parser; using TagsCloudPainter.Settings; @@ -11,26 +10,23 @@ public class BoringTextParserTests [SetUp] public void Setup() { - textFileReader = new TextFileReader(); - var boringText = textFileReader - .ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\boringWords.txt"); + boringText = $"что{Environment.NewLine}и{Environment.NewLine}в"; textSettings = new TextSettings { BoringText = boringText }; boringTextParser = new BoringTextParser(textSettings); } private TextSettings textSettings; private BoringTextParser boringTextParser; - private TextFileReader textFileReader; + private string boringText; [Test] public void ParseText_ShouldReturnWordsListWithoutBoringWords() { var boringWords = boringTextParser .GetBoringWords( - textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\boringWords.txt")) + boringText) .ToHashSet(); - var parsedText = boringTextParser - .ParseText(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt")); + var parsedText = boringTextParser.ParseText("Скучные Слова что в и"); var isBoringWordsInParsedText = parsedText.Where(boringWords.Contains).Any(); isBoringWordsInParsedText.Should().BeFalse(); } @@ -38,25 +34,22 @@ public void ParseText_ShouldReturnWordsListWithoutBoringWords() [Test] public void ParseText_ShouldReturnNotEmptyWordsList_WhenPassedNotEmptyText() { - var parsedText = boringTextParser - .ParseText(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt")); + var parsedText = boringTextParser.ParseText("Скучные Слова что в и"); parsedText.Count.Should().BeGreaterThan(0); } [Test] public void ParseText_ShouldReturnWordsInLowerCase() { - var parsedText = boringTextParser - .ParseText(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt")); - var isAnyWordNotLowered = parsedText.Where(word => word.ToLower() != word).Any(); + var parsedText = boringTextParser.ParseText("Скучные Слова что в и"); + var isAnyWordNotLowered = parsedText.Any(word => !word.Equals(word, StringComparison.CurrentCultureIgnoreCase)); isAnyWordNotLowered.Should().BeFalse(); } [Test] public void ParseText_ShouldReturnWordsListWithTheSameAmountAsInText() { - var parsedText = boringTextParser - .ParseText(textFileReader.ReadFile(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.txt")); - parsedText.Count.Should().Be(20); + var parsedText = boringTextParser.ParseText("Скучные Слова что в и"); + parsedText.Count.Should().Be(2); } } \ No newline at end of file diff --git a/Tests/TagsCloudLayouterTests.cs b/Tests/TagsCloudLayouterTests.cs index 3eeece0f0..b7b672550 100644 --- a/Tests/TagsCloudLayouterTests.cs +++ b/Tests/TagsCloudLayouterTests.cs @@ -1,10 +1,11 @@ using System.Drawing; using FluentAssertions; using TagsCloudPainter.CloudLayouter; +using TagsCloudPainter.Extensions; using TagsCloudPainter.FormPointer; using TagsCloudPainter.Settings; +using TagsCloudPainter.Sizer; using TagsCloudPainter.Tags; -using TagsCloudPainter.Utils; namespace TagsCloudPainterTests; @@ -18,15 +19,16 @@ public void Setup() tagSettings = new TagSettings { TagFontSize = 32 }; var pointerSettings = new SpiralPointerSettings { AngleConst = 1, RadiusConst = 0.5, Step = 0.1 }; var formPointer = new ArchimedeanSpiralPointer(cloudSettings.Value, pointerSettings); - tagsCloudLayouter = new TagsCloudLayouter(cloudSettings, formPointer, tagSettings); + stringSizer = new StringSizer(); + tagsCloudLayouter = new TagsCloudLayouter(cloudSettings, formPointer, tagSettings, stringSizer); } private TagsCloudLayouter tagsCloudLayouter; private TagSettings tagSettings; + private IStringSizer stringSizer; private static IEnumerable PutNextTagArgumentException => new[] { - new TestCaseData(new Tag("", 10, 1)).SetName("WhenGivenTagWithEmptyValue"), new TestCaseData(new Tag("das", 0, 1)).SetName("WhenGivenTagWithFontSizeLessThanOne") }; @@ -40,11 +42,11 @@ public void PutNextRectangle_ShouldThrowArgumentException(Tag tag) public void PutNextTag_ShouldReturnRectangleOfTheTagValueSize() { var tag = new Tag("ads", 10, 5); - var tagRectangle = Utils.GetStringSize(tag.Value, tagSettings.TagFontName, tag.FontSize); + var tagSize = stringSizer.GetStringSize(tag.Value, tagSettings.TagFontName, tag.FontSize); var resultRectangle = tagsCloudLayouter.PutNextTag(tag); - resultRectangle.Size.Should().Be(tagRectangle); + resultRectangle.Size.Should().Be(tagSize); } [Test] @@ -66,7 +68,7 @@ public void PutNextRectangle_ShouldPutRectangleWithCenterInTheCloudCenter() var center = tagsCloudLayouter.GetCloud().Center; var tag = new Tag("ads", 10, 5); var firstRectangle = tagsCloudLayouter.PutNextTag(tag); - var firstRectangleCenter = Utils.GetRectangleCenter(firstRectangle); + var firstRectangleCenter = firstRectangle.GetCenter(); firstRectangleCenter.Should().Be(center); } diff --git a/Tests/TextFileReaderTests.cs b/Tests/TextFileReaderTests.cs index 3944b46a9..d5cdc0e9c 100644 --- a/Tests/TextFileReaderTests.cs +++ b/Tests/TextFileReaderTests.cs @@ -24,7 +24,12 @@ public void Setup() .Returns("Товарищи! постоянное информационно-пропагандистское обеспечение нашей " + $"деятельности играет важную роль в формировании форм развития.{Environment.NewLine}{Environment.NewLine}" + "Значимость этих проблем настолько очевидна, что укрепление и развитие.") - .SetName("WhenPassedDocxFile") + .SetName("WhenPassedDocxFile"), + new TestCaseData(@$"{Environment.CurrentDirectory}..\..\..\..\TextFiles\testFile.doc") + .Returns("Товарищи! постоянное информационно-пропагандистское обеспечение нашей " + + $"деятельности играет важную роль в формировании форм развития.{Environment.NewLine}{Environment.NewLine}" + + "Значимость этих проблем настолько очевидна, что укрепление и развитие.") + .SetName("WhenPassedDocFile") }; [TestCaseSource(nameof(ReadTextFiles))] diff --git a/Tests/TextFiles/boringWords.txt b/Tests/TextFiles/boringWords.txt deleted file mode 100644 index d655ac14c..000000000 --- a/Tests/TextFiles/boringWords.txt +++ /dev/null @@ -1,3 +0,0 @@ -что -и -в \ No newline at end of file diff --git a/Tests/TextFiles/testFile.doc b/Tests/TextFiles/testFile.doc new file mode 100644 index 0000000000000000000000000000000000000000..c7408256d037cbd42299bc5caf8216e3c59243b3 GIT binary patch literal 25600 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 BtxW&` literal 0 HcmV?d00001 From eae835c11f42740d6ff25d82a31fa7fe96ae26c4 Mon Sep 17 00:00:00 2001 From: daHil Date: Thu, 1 Feb 2024 21:50:15 +0500 Subject: [PATCH 26/34] files now copied to solution directory --- .../Infrastructure/Settings/FilesSourceSettings.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs index 559d02155..64ea31368 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs @@ -8,7 +8,7 @@ public class FilesSourceSettings public FilesSourceSettings(AppSettings settings) { - BoringTextFilePath = @$"{Environment.CurrentDirectory}..\..\..\..\" + settings.boringTextFilePath; + BoringTextFilePath = settings.boringTextFilePath; } public string BoringTextFilePath From 5a12d342cc49396c64a6241d6440823b914ac84e Mon Sep 17 00:00:00 2001 From: daHil Date: Thu, 1 Feb 2024 21:51:38 +0500 Subject: [PATCH 27/34] images are now saved with format --- .../PictureBoxImageHolder.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/TagsCloudPainterApplication/PictureBoxImageHolder.cs b/TagsCloudPainterApplication/PictureBoxImageHolder.cs index 8d283e796..8df8ee9e2 100644 --- a/TagsCloudPainterApplication/PictureBoxImageHolder.cs +++ b/TagsCloudPainterApplication/PictureBoxImageHolder.cs @@ -1,4 +1,5 @@ using System.Drawing.Imaging; +using System.Reflection; using TagsCloudPainterApplication.Infrastructure; using TagsCloudPainterApplication.Infrastructure.Settings; @@ -31,7 +32,8 @@ public void UpdateUi() public void SaveImage(string fileName) { - GetImage().Save(fileName); + var imageFormat = GetImageFormat(Path.GetExtension(fileName)); + GetImage().Save(fileName, imageFormat); } public Image GetImage() @@ -41,4 +43,15 @@ public Image GetImage() return Image; } + + private static ImageFormat GetImageFormat(string extension) + { + PropertyInfo prop = typeof(ImageFormat) + .GetProperties().Where(p => p.Name.Equals(extension.Replace(".", ""), StringComparison.InvariantCultureIgnoreCase)) + .FirstOrDefault(); + + return prop is not null + ? prop.GetValue(prop) as ImageFormat + : throw new ArgumentException($"there is no image format with {extension} extension"); + } } \ No newline at end of file From 52e1d874900713706f64c24f6ecf27ba603be25d Mon Sep 17 00:00:00 2001 From: daHil Date: Thu, 1 Feb 2024 21:55:17 +0500 Subject: [PATCH 28/34] cloud now stores list of tuples and layouter now doesn't have laziness --- TagsCloudPainter/CloudDrawer/CloudDrawer.cs | 4 ++-- .../CloudLayouter/TagsCloudLayouter.cs | 19 +++++++------------ TagsCloudPainter/TagsCloud.cs | 10 +++++----- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/TagsCloudPainter/CloudDrawer/CloudDrawer.cs b/TagsCloudPainter/CloudDrawer/CloudDrawer.cs index 70e6041c7..134bf5ac4 100644 --- a/TagsCloudPainter/CloudDrawer/CloudDrawer.cs +++ b/TagsCloudPainter/CloudDrawer/CloudDrawer.cs @@ -33,8 +33,8 @@ public Bitmap DrawCloud(TagsCloud cloud, int imageWidth, int imageHeight) graphics.Clear(cloudSettings.BackgroundColor); foreach (var tag in cloud.Tags) { - var font = new Font(tagSettings.TagFontName, tag.Key.FontSize); - graphics.DrawString(tag.Key.Value, font, pen.Brush, tag.Value.Location); + var font = new Font(tagSettings.TagFontName, tag.Item1.FontSize); + graphics.DrawString(tag.Item1.Value, font, pen.Brush, tag.Item2.Location); } } ; diff --git a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs index 7c4cf4e8f..2d12c51f9 100644 --- a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs +++ b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs @@ -9,14 +9,14 @@ namespace TagsCloudPainter.CloudLayouter; public class TagsCloudLayouter : ICloudLayouter { - private readonly Lazy cloudSettings; + private readonly CloudSettings cloudSettings; private readonly IFormPointer formPointer; private readonly IStringSizer stringSizer; private readonly TagSettings tagSettings; private TagsCloud cloud; public TagsCloudLayouter( - Lazy cloudSettings, + CloudSettings cloudSettings, IFormPointer formPointer, TagSettings tagSettings, IStringSizer stringSizer) @@ -25,12 +25,7 @@ public TagsCloudLayouter( this.formPointer = formPointer ?? throw new ArgumentNullException(nameof(formPointer)); this.tagSettings = tagSettings ?? throw new ArgumentNullException(nameof(tagSettings)); this.stringSizer = stringSizer ?? throw new ArgumentNullException(); - } - - private TagsCloud Cloud - { - get => cloud ??= new TagsCloud(cloudSettings.Value.CloudCenter, []); - set => cloud = value; + cloud = new TagsCloud(cloudSettings.CloudCenter, []); } public Rectangle PutNextTag(Tag tag) @@ -40,10 +35,10 @@ public Rectangle PutNextTag(Tag tag) throw new ArgumentException("either width or height of rectangle size is not possitive"); var nextRectangle = formPointer.GetNextPoint().GetRectangle(tagSize); - while (Cloud.Tags.Values.Any(rectangle => rectangle.IntersectsWith(nextRectangle))) + while (cloud.Tags.Any(pair => pair.Item2.IntersectsWith(nextRectangle))) nextRectangle = formPointer.GetNextPoint().GetRectangle(tagSize); - Cloud.AddTag(tag, nextRectangle); + cloud.AddTag(tag, nextRectangle); return nextRectangle; } @@ -58,12 +53,12 @@ public void PutTags(List tags) public TagsCloud GetCloud() { - return new TagsCloud(Cloud.Center, Cloud.Tags); + return new TagsCloud(cloud.Center, cloud.Tags); } public void Reset() { formPointer.Reset(); - Cloud = new TagsCloud(cloudSettings.Value.CloudCenter, []); + cloud = new TagsCloud(cloudSettings.CloudCenter, []); } } \ No newline at end of file diff --git a/TagsCloudPainter/TagsCloud.cs b/TagsCloudPainter/TagsCloud.cs index db85b7ed7..a46fe130a 100644 --- a/TagsCloudPainter/TagsCloud.cs +++ b/TagsCloudPainter/TagsCloud.cs @@ -5,27 +5,27 @@ namespace TagsCloudPainter; public class TagsCloud { - public TagsCloud(Point center, Dictionary tags) + public TagsCloud(Point center, List<(Tag, Rectangle)> tags) { Center = center; Tags = tags ?? []; } public Point Center { get; private set; } - public Dictionary Tags { get; } + public List<(Tag, Rectangle)> Tags { get; } public void AddTag(Tag tag, Rectangle rectangle) { - Tags.Add(tag, rectangle); + Tags.Add((tag, rectangle)); } public int GetWidth() { - return Tags.Values.Max(rect => rect.X) - Tags.Values.Min(rect => rect.X); + return Tags.Max(pair => pair.Item2.X) - Tags.Min(pair => pair.Item2.X); } public int GetHeight() { - return Tags.Values.Max(rect => rect.Y) - Tags.Values.Min(rect => rect.Y); + return Tags.Max(pair => pair.Item2.Y) - Tags.Min(pair => pair.Item2.Y); } } \ No newline at end of file From 6c8155cb5c3a13850817937a94017e18df171f06 Mon Sep 17 00:00:00 2001 From: daHil Date: Thu, 1 Feb 2024 21:56:58 +0500 Subject: [PATCH 29/34] cloud settings now use app settings --- .../Infrastructure/Settings/TagsCloudSettings.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs index cc0c5793e..72e6c1afc 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs @@ -10,17 +10,18 @@ public TagsCloudSettings( CloudSettings cloudSettings, TagSettings tagSettings, SpiralPointerSettings spiralPointerSettings, - TextSettings textSettings) + TextSettings textSettings, + AppSettings appSettings) { CloudSettings = cloudSettings ?? throw new ArgumentNullException(nameof(cloudSettings)); TagSettings = tagSettings ?? throw new ArgumentNullException(nameof(tagSettings)); SpiralPointerSettings = spiralPointerSettings ?? throw new ArgumentNullException(nameof(spiralPointerSettings)); TextSettings = textSettings ?? throw new ArgumentNullException(nameof(textSettings)); - TagFontSize = AppSettings.Default.tagFontSize; - TagFontName = AppSettings.Default.tagFontName; - PointerStep = AppSettings.Default.pointerStep; - PointerRadiusConst = AppSettings.Default.pointerRadiusConst; - PointerAngleConst = AppSettings.Default.pointerAngleConst; + TagFontSize = appSettings.tagFontSize; + TagFontName = appSettings.tagFontName; + PointerStep = appSettings.pointerStep; + PointerRadiusConst = appSettings.pointerRadiusConst; + PointerAngleConst = appSettings.pointerAngleConst; } [Browsable(false)] public CloudSettings CloudSettings { get; } From f41c88d6e5868228ef307c7365e7fb8dc7d70eaa Mon Sep 17 00:00:00 2001 From: daHil Date: Thu, 1 Feb 2024 21:57:21 +0500 Subject: [PATCH 30/34] changed file readers logic --- TagsCloudPainter/FileReader/DocFileReader.cs | 4 +++- TagsCloudPainter/FileReader/IFileReader.cs | 12 ++++++---- .../FileReader/IFormatFileReader.cs | 6 +++++ TagsCloudPainter/FileReader/TextFileReader.cs | 24 +++++++++++-------- TagsCloudPainter/FileReader/TxtFileReader.cs | 7 ++++-- .../Actions/DrawTagCloudAction.cs | 10 ++++---- TagsCloudPainterApplication/Program.cs | 4 +++- .../TagsCloudPainterApplication.csproj | 9 ++++--- .../DependencyInjectionTests.cs | 2 +- Tests/CloudDrawerTests.cs | 14 +++++------ Tests/TagsCloudLayouterTests.cs | 19 ++++++++++----- Tests/TagsCloudPainterTests.csproj | 15 ++++++------ Tests/TextFileReaderTests.cs | 9 +++---- 13 files changed, 83 insertions(+), 52 deletions(-) create mode 100644 TagsCloudPainter/FileReader/IFormatFileReader.cs diff --git a/TagsCloudPainter/FileReader/DocFileReader.cs b/TagsCloudPainter/FileReader/DocFileReader.cs index db5fc6d50..4eb8c4597 100644 --- a/TagsCloudPainter/FileReader/DocFileReader.cs +++ b/TagsCloudPainter/FileReader/DocFileReader.cs @@ -2,8 +2,10 @@ namespace TagsCloudPainter.FileReader; -internal class DocFileReader : IFileReader +public class DocFileReader : IFileReader { + public HashSet SupportedExtensions => new() { ".doc", ".docx" }; + public string ReadFile(string path) { var doc = new Document(); diff --git a/TagsCloudPainter/FileReader/IFileReader.cs b/TagsCloudPainter/FileReader/IFileReader.cs index 5d1c6819f..47571f915 100644 --- a/TagsCloudPainter/FileReader/IFileReader.cs +++ b/TagsCloudPainter/FileReader/IFileReader.cs @@ -1,6 +1,8 @@ -namespace TagsCloudPainter.FileReader; - -public interface IFileReader +namespace TagsCloudPainter.FileReader { - public string ReadFile(string path); -} \ No newline at end of file + public interface IFileReader + { + public HashSet SupportedExtensions { get; } + public string ReadFile(string path); + } +} diff --git a/TagsCloudPainter/FileReader/IFormatFileReader.cs b/TagsCloudPainter/FileReader/IFormatFileReader.cs new file mode 100644 index 000000000..c5ccab016 --- /dev/null +++ b/TagsCloudPainter/FileReader/IFormatFileReader.cs @@ -0,0 +1,6 @@ +namespace TagsCloudPainter.FileReader; + +public interface IFormatFileReader +{ + public TFormat ReadFile(string path); +} \ No newline at end of file diff --git a/TagsCloudPainter/FileReader/TextFileReader.cs b/TagsCloudPainter/FileReader/TextFileReader.cs index 7a03b1f5f..c22366804 100644 --- a/TagsCloudPainter/FileReader/TextFileReader.cs +++ b/TagsCloudPainter/FileReader/TextFileReader.cs @@ -1,21 +1,25 @@ namespace TagsCloudPainter.FileReader; -public class TextFileReader : IFileReader +public class TextFileReader : IFormatFileReader { - private readonly DocFileReader docFileReader = new(); - private readonly TxtFileReader txtFileReader = new(); + private readonly IEnumerable fileReaders; + + public TextFileReader(IEnumerable fileReaders) + { + this.fileReaders = fileReaders; + } public string ReadFile(string path) { if (!File.Exists(path)) throw new FileNotFoundException(); - return Path.GetExtension(path) switch - { - ".txt" => txtFileReader.ReadFile(path), - ".doc" => docFileReader.ReadFile(path), - ".docx" => docFileReader.ReadFile(path), - _ => throw new ArgumentException("Incorrect file extension. Supported file extensions: txt, doc, docx") - }; + var fileExtension = Path.GetExtension(path); + var fileReader = fileReaders.FirstOrDefault(fileReader => fileReader.SupportedExtensions.Contains(fileExtension)); + + return fileReader is not null + ? fileReader.ReadFile(path) + : throw new ArgumentException($"Incorrect file extension {fileExtension}. " + + $"Supported file extensions: txt, doc, docx"); } } \ No newline at end of file diff --git a/TagsCloudPainter/FileReader/TxtFileReader.cs b/TagsCloudPainter/FileReader/TxtFileReader.cs index 3377dba3f..b0fe705cb 100644 --- a/TagsCloudPainter/FileReader/TxtFileReader.cs +++ b/TagsCloudPainter/FileReader/TxtFileReader.cs @@ -1,7 +1,10 @@ -namespace TagsCloudPainter.FileReader; + +namespace TagsCloudPainter.FileReader; -internal class TxtFileReader : IFileReader +public class TxtFileReader : IFileReader { + public HashSet SupportedExtensions => new() { ".txt" }; + public string ReadFile(string path) { return File.ReadAllText(path).Trim(); diff --git a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs index 2af2dbd74..d9ac1c3fa 100644 --- a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs +++ b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs @@ -13,7 +13,7 @@ public class DrawTagCloudAction : IUiAction { private readonly CloudDrawer cloudDrawer; private readonly ICloudLayouter cloudLayouter; - private readonly IFileReader fileReader; + private readonly IFormatFileReader textFileReader; private readonly FilesSourceSettings filesSourceSettings; private readonly IImageHolder imageHolder; private readonly ImageSettings imageSettings; @@ -31,14 +31,14 @@ public DrawTagCloudAction( ICloudLayouter cloudLayouter, ITagsBuilder tagsBuilder, ITextParser textParser, - IFileReader fileReader, + IFormatFileReader textFileReader, Palette palette) { this.cloudDrawer = cloudDrawer ?? throw new ArgumentNullException(nameof(cloudDrawer)); this.cloudLayouter = cloudLayouter ?? throw new ArgumentNullException(nameof(cloudLayouter)); this.tagsBuilder = tagsBuilder ?? throw new ArgumentNullException(nameof(tagsBuilder)); this.textParser = textParser ?? throw new ArgumentNullException(nameof(textParser)); - this.fileReader = fileReader ?? throw new ArgumentNullException(nameof(fileReader)); + this.textFileReader = textFileReader ?? throw new ArgumentNullException(nameof(textFileReader)); this.imageSettings = imageSettings ?? throw new ArgumentNullException(nameof(imageSettings)); this.tagsCloudSettings = tagsCloudSettings ?? throw new ArgumentNullException(nameof(tagsCloudSettings)); this.imageHolder = imageHolder ?? throw new ArgumentNullException(nameof(imageHolder)); @@ -62,8 +62,8 @@ public void Perform() tagsCloudSettings.CloudSettings.BackgroundColor = palette.BackgroundColor; tagsCloudSettings.TagSettings.TagColor = palette.PrimaryColor; - var wordsText = fileReader.ReadFile(wordsFilePath); - tagsCloudSettings.TextSettings.BoringText = fileReader.ReadFile(filesSourceSettings.BoringTextFilePath); + var wordsText = textFileReader.ReadFile(wordsFilePath); + tagsCloudSettings.TextSettings.BoringText = textFileReader.ReadFile(filesSourceSettings.BoringTextFilePath); var parsedWords = textParser.ParseText(wordsText); var cloud = GetCloud(parsedWords); DrawCloud(cloud); diff --git a/TagsCloudPainterApplication/Program.cs b/TagsCloudPainterApplication/Program.cs index 3fd100e6c..3e25b1dff 100644 --- a/TagsCloudPainterApplication/Program.cs +++ b/TagsCloudPainterApplication/Program.cs @@ -66,7 +66,9 @@ protected override void Load(ContainerBuilder builder) builder.RegisterType().As(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); - builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As>().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); } } \ No newline at end of file diff --git a/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj b/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj index bf172d2dd..b191bda07 100644 --- a/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj +++ b/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj @@ -10,11 +10,11 @@ - + - + @@ -29,6 +29,9 @@ + + Always + SettingsSingleFileGenerator Settings.Designer.cs @@ -36,7 +39,7 @@ - + \ No newline at end of file diff --git a/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs b/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs index 9dc651d67..1db74ba32 100644 --- a/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs +++ b/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs @@ -47,7 +47,7 @@ public void TearDown() { new TestCaseData(typeof(TagSettings)), new TestCaseData(typeof(CloudDrawer)), - new TestCaseData(typeof(IFileReader)), + new TestCaseData(typeof(IFormatFileReader)), new TestCaseData(typeof(ITextParser)), new TestCaseData(typeof(ITagsBuilder)), new TestCaseData(typeof(TextSettings)), diff --git a/Tests/CloudDrawerTests.cs b/Tests/CloudDrawerTests.cs index 82c18c706..749f450ce 100644 --- a/Tests/CloudDrawerTests.cs +++ b/Tests/CloudDrawerTests.cs @@ -22,15 +22,15 @@ public void Setup() private static IEnumerable DrawArgumentException => new[] { new TestCaseData(new TagsCloud(new Point(0, 0), - new Dictionary { { new Tag("a", 1, 1), new(1, 1, 1, 1) } }), 0, 1) + new List<(Tag, Rectangle)> { (new Tag("a", 1, 1), new(1, 1, 1, 1) )} ), 0, 1) .SetName("WhenGivenNotPositiveImageWidth"), new TestCaseData(new TagsCloud(new Point(0, 0), - new Dictionary { { new Tag("a", 1, 1), new(1, 1, 1, 1) } }), 1, 0) + new List <(Tag, Rectangle) > {(new Tag("a", 1, 1), new(1, 1, 1, 1)) }), 1, 0) .SetName("WhenGivenNotPositiveImageHeight"), new TestCaseData(new TagsCloud(new Point(0, 0), - new Dictionary { { new Tag("a", 1, 1), new(1, 1, 1, 1) } }), 0, 0) + new List <(Tag, Rectangle) > {(new Tag("a", 1, 1), new(1, 1, 1, 1)) }), 0, 0) .SetName("WhenGivenNotPositiveImageHeightAndWidth"), - new TestCaseData(new TagsCloud(new Point(0, 0), new Dictionary()), 1, 1) + new TestCaseData(new TagsCloud(new Point(0, 0), new List<(Tag, Rectangle)>()), 1, 1) .SetName("WhenGivenCloudWithEmptyTagsDictionary") }; @@ -43,13 +43,13 @@ public void Draw_ShouldThrowArgumentException(TagsCloud cloud, int width, int he private static IEnumerable DrawNoException => new[] { new TestCaseData(new TagsCloud(new Point(5, 5), - new Dictionary { { new Tag("abcdadg", 10, 1), new(5, 5, 20, 3) } }), 10, 10) + new List<(Tag, Rectangle)> { ( new Tag("abcdadg", 10, 1), new(5, 5, 20, 3) ) }), 10, 10) .SetName("WhenCloudWidthIsGreaterThanImageWidth"), new TestCaseData(new TagsCloud(new Point(5, 5), - new Dictionary { { new Tag("abcdadg", 10, 1), new(5, 5, 3, 20) } }), 10, 10) + new List <(Tag, Rectangle) > {(new Tag("abcdadg", 10, 1), new(5, 5, 3, 20)) }), 10, 10) .SetName("WhenCloudHeightIsGreaterThanImageHeight"), new TestCaseData(new TagsCloud(new Point(5, 5), - new Dictionary { { new Tag("abcdadg", 10, 1), new(5, 5, 20, 20) } }), 10, 10) + new List <(Tag, Rectangle) > {(new Tag("abcdadg", 10, 1), new(5, 5, 20, 20)) }), 10, 10) .SetName("WhenCloudIsBiggerThanImage") }; diff --git a/Tests/TagsCloudLayouterTests.cs b/Tests/TagsCloudLayouterTests.cs index b7b672550..548ec3052 100644 --- a/Tests/TagsCloudLayouterTests.cs +++ b/Tests/TagsCloudLayouterTests.cs @@ -1,4 +1,5 @@ using System.Drawing; +using FakeItEasy; using FluentAssertions; using TagsCloudPainter.CloudLayouter; using TagsCloudPainter.Extensions; @@ -15,11 +16,13 @@ public class TagsCloudLayouterTests [SetUp] public void Setup() { - var cloudSettings = new Lazy(new CloudSettings { CloudCenter = new Point(0, 0) }); + var cloudSettings = new CloudSettings { CloudCenter = new Point(0, 0) }; tagSettings = new TagSettings { TagFontSize = 32 }; var pointerSettings = new SpiralPointerSettings { AngleConst = 1, RadiusConst = 0.5, Step = 0.1 }; - var formPointer = new ArchimedeanSpiralPointer(cloudSettings.Value, pointerSettings); - stringSizer = new StringSizer(); + var formPointer = new ArchimedeanSpiralPointer(cloudSettings, pointerSettings); + stringSizer = A.Fake(); + A.CallTo(() => stringSizer.GetStringSize(A.Ignored, A.Ignored, A.Ignored)) + .Returns(new Size(10,10)); tagsCloudLayouter = new TagsCloudLayouter(cloudSettings, formPointer, tagSettings, stringSizer); } @@ -29,13 +32,17 @@ public void Setup() private static IEnumerable PutNextTagArgumentException => new[] { - new TestCaseData(new Tag("das", 0, 1)).SetName("WhenGivenTagWithFontSizeLessThanOne") + new TestCaseData(new Size(0,10)).SetName("WidthNotPossitive"), + new TestCaseData(new Size(10,0)).SetName("HeightNotPossitive"), + new TestCaseData(new Size(0,0)).SetName("HeightAndWidthNotPossitive"), }; [TestCaseSource(nameof(PutNextTagArgumentException))] - public void PutNextRectangle_ShouldThrowArgumentException(Tag tag) + public void PutNextRectangle_ShouldThrowArgumentException_WhenGivenTagWith(Size size) { - Assert.Throws(() => tagsCloudLayouter.PutNextTag(tag)); + A.CallTo(() => stringSizer.GetStringSize(A.Ignored, A.Ignored, A.Ignored)) + .Returns(size); + Assert.Throws(() => tagsCloudLayouter.PutNextTag(new Tag("a", 2, 1))); } [Test] diff --git a/Tests/TagsCloudPainterTests.csproj b/Tests/TagsCloudPainterTests.csproj index cd1cdc579..d5e419e07 100644 --- a/Tests/TagsCloudPainterTests.csproj +++ b/Tests/TagsCloudPainterTests.csproj @@ -10,16 +10,17 @@ - - - - - - + + + + + + + - + diff --git a/Tests/TextFileReaderTests.cs b/Tests/TextFileReaderTests.cs index d5cdc0e9c..84b736ac6 100644 --- a/Tests/TextFileReaderTests.cs +++ b/Tests/TextFileReaderTests.cs @@ -8,10 +8,11 @@ public class TextFileReaderTests [SetUp] public void Setup() { - fileReader = new TextFileReader(); + var fileReaders = new List() { new TxtFileReader(), new DocFileReader()}; + textFileReader = new TextFileReader(fileReaders); } - private TextFileReader fileReader; + private TextFileReader textFileReader; private static IEnumerable ReadTextFiles => new[] { @@ -35,12 +36,12 @@ public void Setup() [TestCaseSource(nameof(ReadTextFiles))] public string ReadFile_ShouldReturnFileText(string path) { - return fileReader.ReadFile(path); + return textFileReader.ReadFile(path); } [Test] public void ReadFile_ThrowsFileNotFoundExceptio_WhenPassedNonexistentPath() { - Assert.Throws(() => fileReader.ReadFile("")); + Assert.Throws(() => textFileReader.ReadFile("")); } } \ No newline at end of file From 43d00cd3354e06b41d9984f5123f45d7f16c42b4 Mon Sep 17 00:00:00 2001 From: daHil Date: Thu, 1 Feb 2024 22:36:02 +0500 Subject: [PATCH 31/34] added interfaces for settings --- TagsCloudPainter/CloudDrawer/CloudDrawer.cs | 9 +- .../CloudLayouter/TagsCloudLayouter.cs | 11 +- .../FormPointer/ArchimedeanSpiralPointer.cs | 9 +- TagsCloudPainter/Parser/BoringTextParser.cs | 4 +- .../Settings/{ => Cloud}/CloudSettings.cs | 4 +- .../Settings/Cloud/ICloudSettings.cs | 10 ++ .../FormPointer/ISpiralPointerSettings.cs | 9 ++ .../SpiralPointerSettings.cs | 4 +- TagsCloudPainter/Settings/Tag/ITagSettings.cs | 11 ++ .../Settings/{ => Tag}/TagSettings.cs | 4 +- .../Settings/Text/ITextSettings.cs | 7 ++ .../Settings/{ => Text}/TextSettings.cs | 2 +- TagsCloudPainter/Tags/TagsBuilder.cs | 6 +- TagsCloudPainter/TagsCloudPainter.csproj | 4 +- .../Actions/DrawTagCloudAction.cs | 15 ++- .../Actions/FileSourceSettingsAction.cs | 5 +- .../Actions/ImageSettingsAction.cs | 5 +- .../{ => FilesSource}/FilesSourceSettings.cs | 8 +- .../FilesSource/IFilesSourceSettings.cs | 7 ++ .../Settings/Image/IImageSettings.cs | 8 ++ .../Settings/Image/ImageSettings.cs | 15 +++ .../Infrastructure/Settings/ImageSettings.cs | 15 --- .../Settings/TagsCloud/ITagsCloudSettings.cs | 27 +++++ .../{ => TagsCloud}/TagsCloudSettings.cs | 35 +++--- TagsCloudPainterApplication/MainForm.cs | 4 +- .../PictureBoxImageHolder.cs | 6 +- TagsCloudPainterApplication/Program.cs | 23 ++-- .../Properties/IAppSettings.cs | 14 +++ .../Properties/Settings.Designer.cs | 108 +++++++++++------- .../DependencyInjectionTests.cs | 23 ++-- Tests/ArchimedeanSpiralTests.cs | 3 +- Tests/BoringTextParserTests.cs | 2 +- Tests/CloudDrawerTests.cs | 3 +- Tests/TagsBuilderTests.cs | 2 +- Tests/TagsCloudLayouterTests.cs | 6 +- 35 files changed, 287 insertions(+), 141 deletions(-) rename TagsCloudPainter/Settings/{ => Cloud}/CloudSettings.cs (57%) create mode 100644 TagsCloudPainter/Settings/Cloud/ICloudSettings.cs create mode 100644 TagsCloudPainter/Settings/FormPointer/ISpiralPointerSettings.cs rename TagsCloudPainter/Settings/{ => FormPointer}/SpiralPointerSettings.cs (56%) create mode 100644 TagsCloudPainter/Settings/Tag/ITagSettings.cs rename TagsCloudPainter/Settings/{ => Tag}/TagSettings.cs (75%) create mode 100644 TagsCloudPainter/Settings/Text/ITextSettings.cs rename TagsCloudPainter/Settings/{ => Text}/TextSettings.cs (71%) rename TagsCloudPainterApplication/Infrastructure/Settings/{ => FilesSource}/FilesSourceSettings.cs (51%) create mode 100644 TagsCloudPainterApplication/Infrastructure/Settings/FilesSource/IFilesSourceSettings.cs create mode 100644 TagsCloudPainterApplication/Infrastructure/Settings/Image/IImageSettings.cs create mode 100644 TagsCloudPainterApplication/Infrastructure/Settings/Image/ImageSettings.cs delete mode 100644 TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs create mode 100644 TagsCloudPainterApplication/Infrastructure/Settings/TagsCloud/ITagsCloudSettings.cs rename TagsCloudPainterApplication/Infrastructure/Settings/{ => TagsCloud}/TagsCloudSettings.cs (59%) create mode 100644 TagsCloudPainterApplication/Properties/IAppSettings.cs diff --git a/TagsCloudPainter/CloudDrawer/CloudDrawer.cs b/TagsCloudPainter/CloudDrawer/CloudDrawer.cs index 134bf5ac4..6bf95cb46 100644 --- a/TagsCloudPainter/CloudDrawer/CloudDrawer.cs +++ b/TagsCloudPainter/CloudDrawer/CloudDrawer.cs @@ -1,15 +1,16 @@ using System.Drawing; using System.Drawing.Drawing2D; -using TagsCloudPainter.Settings; +using TagsCloudPainter.Settings.Cloud; +using TagsCloudPainter.Settings.Tag; namespace TagsCloudPainter.Drawer; public class CloudDrawer { - private readonly CloudSettings cloudSettings; - private readonly TagSettings tagSettings; + private readonly ICloudSettings cloudSettings; + private readonly ITagSettings tagSettings; - public CloudDrawer(TagSettings tagSettings, CloudSettings cloudSettings) + public CloudDrawer(ITagSettings tagSettings, ICloudSettings cloudSettings) { this.tagSettings = tagSettings ?? throw new ArgumentNullException(nameof(tagSettings)); this.cloudSettings = cloudSettings ?? throw new ArgumentNullException(nameof(cloudSettings)); diff --git a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs index 2d12c51f9..65b8f7791 100644 --- a/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs +++ b/TagsCloudPainter/CloudLayouter/TagsCloudLayouter.cs @@ -1,7 +1,8 @@ using System.Drawing; using TagsCloudPainter.Extensions; using TagsCloudPainter.FormPointer; -using TagsCloudPainter.Settings; +using TagsCloudPainter.Settings.Cloud; +using TagsCloudPainter.Settings.Tag; using TagsCloudPainter.Sizer; using TagsCloudPainter.Tags; @@ -9,16 +10,16 @@ namespace TagsCloudPainter.CloudLayouter; public class TagsCloudLayouter : ICloudLayouter { - private readonly CloudSettings cloudSettings; + private readonly ICloudSettings cloudSettings; private readonly IFormPointer formPointer; private readonly IStringSizer stringSizer; - private readonly TagSettings tagSettings; + private readonly ITagSettings tagSettings; private TagsCloud cloud; public TagsCloudLayouter( - CloudSettings cloudSettings, + ICloudSettings cloudSettings, IFormPointer formPointer, - TagSettings tagSettings, + ITagSettings tagSettings, IStringSizer stringSizer) { this.cloudSettings = cloudSettings ?? throw new ArgumentNullException(nameof(cloudSettings)); diff --git a/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs b/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs index fb1ab7d06..943189c69 100644 --- a/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs +++ b/TagsCloudPainter/FormPointer/ArchimedeanSpiralPointer.cs @@ -1,15 +1,16 @@ using System.Drawing; -using TagsCloudPainter.Settings; +using TagsCloudPainter.Settings.Cloud; +using TagsCloudPainter.Settings.FormPointer; namespace TagsCloudPainter.FormPointer; public class ArchimedeanSpiralPointer : IFormPointer { - private readonly CloudSettings cloudSettings; - private readonly SpiralPointerSettings spiralPointerSettings; + private readonly ICloudSettings cloudSettings; + private readonly ISpiralPointerSettings spiralPointerSettings; private double сurrentDifference; - public ArchimedeanSpiralPointer(CloudSettings cloudSettings, SpiralPointerSettings spiralPointerSettings) + public ArchimedeanSpiralPointer(ICloudSettings cloudSettings, ISpiralPointerSettings spiralPointerSettings) { if (spiralPointerSettings.Step <= 0 || spiralPointerSettings.RadiusConst <= 0 diff --git a/TagsCloudPainter/Parser/BoringTextParser.cs b/TagsCloudPainter/Parser/BoringTextParser.cs index 62c7e9741..1e0e8372d 100644 --- a/TagsCloudPainter/Parser/BoringTextParser.cs +++ b/TagsCloudPainter/Parser/BoringTextParser.cs @@ -5,9 +5,9 @@ namespace TagsCloudPainter.Parser; public class BoringTextParser : ITextParser { private static readonly string[] _separators = [" ", ". ", ", ", "; ", "-", "—", Environment.NewLine]; - private readonly TextSettings textSettings; + private readonly ITextSettings textSettings; - public BoringTextParser(TextSettings textSettings) + public BoringTextParser(ITextSettings textSettings) { this.textSettings = textSettings ?? throw new ArgumentNullException(nameof(textSettings)); } diff --git a/TagsCloudPainter/Settings/CloudSettings.cs b/TagsCloudPainter/Settings/Cloud/CloudSettings.cs similarity index 57% rename from TagsCloudPainter/Settings/CloudSettings.cs rename to TagsCloudPainter/Settings/Cloud/CloudSettings.cs index 9e7a181d7..0303aaa9d 100644 --- a/TagsCloudPainter/Settings/CloudSettings.cs +++ b/TagsCloudPainter/Settings/Cloud/CloudSettings.cs @@ -1,8 +1,8 @@ using System.Drawing; -namespace TagsCloudPainter.Settings; +namespace TagsCloudPainter.Settings.Cloud; -public class CloudSettings +public class CloudSettings : ICloudSettings { public Point CloudCenter { get; set; } public Color BackgroundColor { get; set; } diff --git a/TagsCloudPainter/Settings/Cloud/ICloudSettings.cs b/TagsCloudPainter/Settings/Cloud/ICloudSettings.cs new file mode 100644 index 000000000..21a262b3d --- /dev/null +++ b/TagsCloudPainter/Settings/Cloud/ICloudSettings.cs @@ -0,0 +1,10 @@ +using System.Drawing; + +namespace TagsCloudPainter.Settings.Cloud +{ + public interface ICloudSettings + { + public Point CloudCenter { get; set; } + public Color BackgroundColor { get; set; } + } +} diff --git a/TagsCloudPainter/Settings/FormPointer/ISpiralPointerSettings.cs b/TagsCloudPainter/Settings/FormPointer/ISpiralPointerSettings.cs new file mode 100644 index 000000000..54e2170e2 --- /dev/null +++ b/TagsCloudPainter/Settings/FormPointer/ISpiralPointerSettings.cs @@ -0,0 +1,9 @@ +namespace TagsCloudPainter.Settings.FormPointer +{ + public interface ISpiralPointerSettings + { + public double Step { get; set; } + public double RadiusConst { get; set; } + public double AngleConst { get; set; } + } +} diff --git a/TagsCloudPainter/Settings/SpiralPointerSettings.cs b/TagsCloudPainter/Settings/FormPointer/SpiralPointerSettings.cs similarity index 56% rename from TagsCloudPainter/Settings/SpiralPointerSettings.cs rename to TagsCloudPainter/Settings/FormPointer/SpiralPointerSettings.cs index a1d3f1a57..54bb6105a 100644 --- a/TagsCloudPainter/Settings/SpiralPointerSettings.cs +++ b/TagsCloudPainter/Settings/FormPointer/SpiralPointerSettings.cs @@ -1,6 +1,6 @@ -namespace TagsCloudPainter.Settings; +namespace TagsCloudPainter.Settings.FormPointer; -public class SpiralPointerSettings +public class SpiralPointerSettings : ISpiralPointerSettings { public double Step { get; set; } = 1; public double RadiusConst { get; set; } = 1; diff --git a/TagsCloudPainter/Settings/Tag/ITagSettings.cs b/TagsCloudPainter/Settings/Tag/ITagSettings.cs new file mode 100644 index 000000000..227f085d9 --- /dev/null +++ b/TagsCloudPainter/Settings/Tag/ITagSettings.cs @@ -0,0 +1,11 @@ +using System.Drawing; + +namespace TagsCloudPainter.Settings.Tag +{ + public interface ITagSettings + { + public int TagFontSize { get; set; } + public string TagFontName { get; set; } + public Color TagColor { get; set; } + } +} diff --git a/TagsCloudPainter/Settings/TagSettings.cs b/TagsCloudPainter/Settings/Tag/TagSettings.cs similarity index 75% rename from TagsCloudPainter/Settings/TagSettings.cs rename to TagsCloudPainter/Settings/Tag/TagSettings.cs index 278c05008..fc1d91244 100644 --- a/TagsCloudPainter/Settings/TagSettings.cs +++ b/TagsCloudPainter/Settings/Tag/TagSettings.cs @@ -1,9 +1,9 @@ using System.Drawing; using System.Drawing.Text; -namespace TagsCloudPainter.Settings; +namespace TagsCloudPainter.Settings.Tag; -public class TagSettings +public class TagSettings : ITagSettings { public int TagFontSize { get; set; } public string TagFontName { get; set; } = new InstalledFontCollection().Families.FirstOrDefault().Name; diff --git a/TagsCloudPainter/Settings/Text/ITextSettings.cs b/TagsCloudPainter/Settings/Text/ITextSettings.cs new file mode 100644 index 000000000..cc1e114bd --- /dev/null +++ b/TagsCloudPainter/Settings/Text/ITextSettings.cs @@ -0,0 +1,7 @@ +namespace TagsCloudPainter.Settings +{ + public interface ITextSettings + { + public string BoringText { get; set; } + } +} diff --git a/TagsCloudPainter/Settings/TextSettings.cs b/TagsCloudPainter/Settings/Text/TextSettings.cs similarity index 71% rename from TagsCloudPainter/Settings/TextSettings.cs rename to TagsCloudPainter/Settings/Text/TextSettings.cs index 26290a24d..ed514c209 100644 --- a/TagsCloudPainter/Settings/TextSettings.cs +++ b/TagsCloudPainter/Settings/Text/TextSettings.cs @@ -1,6 +1,6 @@ namespace TagsCloudPainter.Settings; -public class TextSettings +public class TextSettings : ITextSettings { public string BoringText { get; set; } = string.Empty; } \ No newline at end of file diff --git a/TagsCloudPainter/Tags/TagsBuilder.cs b/TagsCloudPainter/Tags/TagsBuilder.cs index 6df7907fa..57632c48e 100644 --- a/TagsCloudPainter/Tags/TagsBuilder.cs +++ b/TagsCloudPainter/Tags/TagsBuilder.cs @@ -1,12 +1,12 @@ -using TagsCloudPainter.Settings; +using TagsCloudPainter.Settings.Tag; namespace TagsCloudPainter.Tags; public class TagsBuilder : ITagsBuilder { - private readonly TagSettings _settings; + private readonly ITagSettings _settings; - public TagsBuilder(TagSettings settings) + public TagsBuilder(ITagSettings settings) { _settings = settings ?? throw new ArgumentNullException(nameof(settings)); } diff --git a/TagsCloudPainter/TagsCloudPainter.csproj b/TagsCloudPainter/TagsCloudPainter.csproj index 5d7a5062b..10e1428c6 100644 --- a/TagsCloudPainter/TagsCloudPainter.csproj +++ b/TagsCloudPainter/TagsCloudPainter.csproj @@ -8,8 +8,8 @@ - - + + diff --git a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs index d9ac1c3fa..80106cdac 100644 --- a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs +++ b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs @@ -6,6 +6,9 @@ using TagsCloudPainter.Tags; using TagsCloudPainterApplication.Infrastructure; using TagsCloudPainterApplication.Infrastructure.Settings; +using TagsCloudPainterApplication.Infrastructure.Settings.FilesSource; +using TagsCloudPainterApplication.Infrastructure.Settings.Image; +using TagsCloudPainterApplication.Infrastructure.Settings.TagsCloud; namespace TagsCloudPainterApplication.Actions; @@ -14,18 +17,18 @@ public class DrawTagCloudAction : IUiAction private readonly CloudDrawer cloudDrawer; private readonly ICloudLayouter cloudLayouter; private readonly IFormatFileReader textFileReader; - private readonly FilesSourceSettings filesSourceSettings; + private readonly IFilesSourceSettings filesSourceSettings; private readonly IImageHolder imageHolder; - private readonly ImageSettings imageSettings; + private readonly IImageSettings imageSettings; private readonly Palette palette; private readonly ITagsBuilder tagsBuilder; - private readonly TagsCloudSettings tagsCloudSettings; + private readonly ITagsCloudSettings tagsCloudSettings; private readonly ITextParser textParser; public DrawTagCloudAction( - ImageSettings imageSettings, - TagsCloudSettings tagsCloudSettings, - FilesSourceSettings filesSourceSettings, + IImageSettings imageSettings, + ITagsCloudSettings tagsCloudSettings, + IFilesSourceSettings filesSourceSettings, IImageHolder imageHolder, CloudDrawer cloudDrawer, ICloudLayouter cloudLayouter, diff --git a/TagsCloudPainterApplication/Actions/FileSourceSettingsAction.cs b/TagsCloudPainterApplication/Actions/FileSourceSettingsAction.cs index 02b4fd70c..c8a5d9c61 100644 --- a/TagsCloudPainterApplication/Actions/FileSourceSettingsAction.cs +++ b/TagsCloudPainterApplication/Actions/FileSourceSettingsAction.cs @@ -1,12 +1,13 @@ using TagsCloudPainterApplication.Infrastructure.Settings; +using TagsCloudPainterApplication.Infrastructure.Settings.FilesSource; namespace TagsCloudPainterApplication.Actions; public class FileSourceSettingsAction : IUiAction { - private readonly FilesSourceSettings filesSourceSettings; + private readonly IFilesSourceSettings filesSourceSettings; - public FileSourceSettingsAction(FilesSourceSettings filesSourceSettings) + public FileSourceSettingsAction(IFilesSourceSettings filesSourceSettings) { this.filesSourceSettings = filesSourceSettings ?? throw new ArgumentNullException(nameof(filesSourceSettings)); } diff --git a/TagsCloudPainterApplication/Actions/ImageSettingsAction.cs b/TagsCloudPainterApplication/Actions/ImageSettingsAction.cs index cdcc84801..d79b96ef5 100644 --- a/TagsCloudPainterApplication/Actions/ImageSettingsAction.cs +++ b/TagsCloudPainterApplication/Actions/ImageSettingsAction.cs @@ -1,12 +1,13 @@ using TagsCloudPainterApplication.Infrastructure.Settings; +using TagsCloudPainterApplication.Infrastructure.Settings.Image; namespace TagsCloudPainterApplication.Actions; public class ImageSettingsAction : IUiAction { - private readonly ImageSettings imageSettings; + private readonly IImageSettings imageSettings; - public ImageSettingsAction(ImageSettings imageSettings) + public ImageSettingsAction(IImageSettings imageSettings) { this.imageSettings = imageSettings ?? throw new ArgumentNullException(nameof(imageSettings)); } diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSource/FilesSourceSettings.cs similarity index 51% rename from TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs rename to TagsCloudPainterApplication/Infrastructure/Settings/FilesSource/FilesSourceSettings.cs index 64ea31368..a882ab112 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/FilesSourceSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSource/FilesSourceSettings.cs @@ -1,14 +1,14 @@ using TagsCloudPainterApplication.Properties; -namespace TagsCloudPainterApplication.Infrastructure.Settings; +namespace TagsCloudPainterApplication.Infrastructure.Settings.FilesSource; -public class FilesSourceSettings +public class FilesSourceSettings : IFilesSourceSettings { private string boringTextFilePath; - public FilesSourceSettings(AppSettings settings) + public FilesSourceSettings(IAppSettings settings) { - BoringTextFilePath = settings.boringTextFilePath; + BoringTextFilePath = settings.BoringTextFilePath; } public string BoringTextFilePath diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/FilesSource/IFilesSourceSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSource/IFilesSourceSettings.cs new file mode 100644 index 000000000..5171dfbfd --- /dev/null +++ b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSource/IFilesSourceSettings.cs @@ -0,0 +1,7 @@ +namespace TagsCloudPainterApplication.Infrastructure.Settings.FilesSource +{ + public interface IFilesSourceSettings + { + public string BoringTextFilePath { get; set; } + } +} diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/Image/IImageSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/Image/IImageSettings.cs new file mode 100644 index 000000000..479e7a213 --- /dev/null +++ b/TagsCloudPainterApplication/Infrastructure/Settings/Image/IImageSettings.cs @@ -0,0 +1,8 @@ +namespace TagsCloudPainterApplication.Infrastructure.Settings.Image +{ + public interface IImageSettings + { + public int Width { get; set; } + public int Height { get; set; } + } +} diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/Image/ImageSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/Image/ImageSettings.cs new file mode 100644 index 000000000..7501ae41f --- /dev/null +++ b/TagsCloudPainterApplication/Infrastructure/Settings/Image/ImageSettings.cs @@ -0,0 +1,15 @@ +using TagsCloudPainterApplication.Properties; + +namespace TagsCloudPainterApplication.Infrastructure.Settings.Image; + +public class ImageSettings : IImageSettings +{ + public ImageSettings(IAppSettings settings) + { + Width = settings.ImageWidth; + Height = settings.ImageHeight; + } + + public int Width { get; set; } + public int Height { get; set; } +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs deleted file mode 100644 index a097f57d4..000000000 --- a/TagsCloudPainterApplication/Infrastructure/Settings/ImageSettings.cs +++ /dev/null @@ -1,15 +0,0 @@ -using TagsCloudPainterApplication.Properties; - -namespace TagsCloudPainterApplication.Infrastructure.Settings; - -public class ImageSettings -{ - public ImageSettings(AppSettings settings) - { - Width = settings.imageWidth; - Height = settings.imageHeight; - } - - public int Width { get; set; } - public int Height { get; set; } -} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloud/ITagsCloudSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloud/ITagsCloudSettings.cs new file mode 100644 index 000000000..a16322539 --- /dev/null +++ b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloud/ITagsCloudSettings.cs @@ -0,0 +1,27 @@ +using TagsCloudPainter.Settings.Cloud; +using TagsCloudPainter.Settings.FormPointer; +using TagsCloudPainter.Settings.Tag; +using TagsCloudPainter.Settings; + +namespace TagsCloudPainterApplication.Infrastructure.Settings.TagsCloud +{ + public interface ITagsCloudSettings + { + public ICloudSettings CloudSettings { get; } + public ITagSettings TagSettings { get; } + public ISpiralPointerSettings SpiralPointerSettings { get; } + public ITextSettings TextSettings { get; } + + public int TagFontSize { get; set; } + + public string TagFontName { get; set; } + + public Point CloudCenter { get; set; } + + public double PointerStep { get; set; } + + public double PointerRadiusConst { get; set; } + + public double PointerAngleConst { get; set; } + } +} diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloud/TagsCloudSettings.cs similarity index 59% rename from TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs rename to TagsCloudPainterApplication/Infrastructure/Settings/TagsCloud/TagsCloudSettings.cs index 72e6c1afc..6ff3c07d4 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloudSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloud/TagsCloudSettings.cs @@ -1,33 +1,36 @@ using System.ComponentModel; using TagsCloudPainter.Settings; +using TagsCloudPainter.Settings.Cloud; +using TagsCloudPainter.Settings.FormPointer; +using TagsCloudPainter.Settings.Tag; using TagsCloudPainterApplication.Properties; -namespace TagsCloudPainterApplication.Infrastructure.Settings; +namespace TagsCloudPainterApplication.Infrastructure.Settings.TagsCloud; -public class TagsCloudSettings +public class TagsCloudSettings : ITagsCloudSettings { public TagsCloudSettings( - CloudSettings cloudSettings, - TagSettings tagSettings, - SpiralPointerSettings spiralPointerSettings, - TextSettings textSettings, - AppSettings appSettings) + ICloudSettings cloudSettings, + ITagSettings tagSettings, + ISpiralPointerSettings spiralPointerSettings, + ITextSettings textSettings, + IAppSettings appSettings) { CloudSettings = cloudSettings ?? throw new ArgumentNullException(nameof(cloudSettings)); TagSettings = tagSettings ?? throw new ArgumentNullException(nameof(tagSettings)); SpiralPointerSettings = spiralPointerSettings ?? throw new ArgumentNullException(nameof(spiralPointerSettings)); TextSettings = textSettings ?? throw new ArgumentNullException(nameof(textSettings)); - TagFontSize = appSettings.tagFontSize; - TagFontName = appSettings.tagFontName; - PointerStep = appSettings.pointerStep; - PointerRadiusConst = appSettings.pointerRadiusConst; - PointerAngleConst = appSettings.pointerAngleConst; + TagFontSize = appSettings.TagFontSize; + TagFontName = appSettings.TagFontName; + PointerStep = appSettings.PointerStep; + PointerRadiusConst = appSettings.PointerRadiusConst; + PointerAngleConst = appSettings.PointerAngleConst; } - [Browsable(false)] public CloudSettings CloudSettings { get; } - [Browsable(false)] public TagSettings TagSettings { get; } - [Browsable(false)] public SpiralPointerSettings SpiralPointerSettings { get; } - [Browsable(false)] public TextSettings TextSettings { get; } + [Browsable(false)] public ICloudSettings CloudSettings { get; } + [Browsable(false)] public ITagSettings TagSettings { get; } + [Browsable(false)] public ISpiralPointerSettings SpiralPointerSettings { get; } + [Browsable(false)] public ITextSettings TextSettings { get; } public int TagFontSize { diff --git a/TagsCloudPainterApplication/MainForm.cs b/TagsCloudPainterApplication/MainForm.cs index 407ffe90c..243f2b555 100644 --- a/TagsCloudPainterApplication/MainForm.cs +++ b/TagsCloudPainterApplication/MainForm.cs @@ -1,11 +1,11 @@ using TagsCloudPainterApplication.Actions; -using TagsCloudPainterApplication.Infrastructure.Settings; +using TagsCloudPainterApplication.Infrastructure.Settings.Image; namespace TagsCloudPainterApplication; public partial class MainForm : Form { - public MainForm(IUiAction[] actions, ImageSettings imageSettings, PictureBoxImageHolder pictureBox) + public MainForm(IUiAction[] actions, IImageSettings imageSettings, PictureBoxImageHolder pictureBox) { ArgumentNullException.ThrowIfNull(actions); ArgumentNullException.ThrowIfNull(imageSettings); diff --git a/TagsCloudPainterApplication/PictureBoxImageHolder.cs b/TagsCloudPainterApplication/PictureBoxImageHolder.cs index 8df8ee9e2..ef17d6678 100644 --- a/TagsCloudPainterApplication/PictureBoxImageHolder.cs +++ b/TagsCloudPainterApplication/PictureBoxImageHolder.cs @@ -1,15 +1,15 @@ using System.Drawing.Imaging; using System.Reflection; using TagsCloudPainterApplication.Infrastructure; -using TagsCloudPainterApplication.Infrastructure.Settings; +using TagsCloudPainterApplication.Infrastructure.Settings.Image; namespace TagsCloudPainterApplication; public class PictureBoxImageHolder : PictureBox, IImageHolder { - private readonly Lazy imageSettings; + private readonly Lazy imageSettings; - public PictureBoxImageHolder(Lazy imageSettings) + public PictureBoxImageHolder(Lazy imageSettings) { this.imageSettings = imageSettings ?? throw new ArgumentNullException(nameof(imageSettings)); } diff --git a/TagsCloudPainterApplication/Program.cs b/TagsCloudPainterApplication/Program.cs index 3e25b1dff..f114b6d8f 100644 --- a/TagsCloudPainterApplication/Program.cs +++ b/TagsCloudPainterApplication/Program.cs @@ -5,11 +5,16 @@ using TagsCloudPainter.FormPointer; using TagsCloudPainter.Parser; using TagsCloudPainter.Settings; +using TagsCloudPainter.Settings.Cloud; +using TagsCloudPainter.Settings.FormPointer; +using TagsCloudPainter.Settings.Tag; using TagsCloudPainter.Sizer; using TagsCloudPainter.Tags; using TagsCloudPainterApplication.Actions; using TagsCloudPainterApplication.Infrastructure; -using TagsCloudPainterApplication.Infrastructure.Settings; +using TagsCloudPainterApplication.Infrastructure.Settings.FilesSource; +using TagsCloudPainterApplication.Infrastructure.Settings.Image; +using TagsCloudPainterApplication.Infrastructure.Settings.TagsCloud; using TagsCloudPainterApplication.Properties; namespace TagsCloudPainterApplication; @@ -38,11 +43,11 @@ public class ApplicationModule : Module protected override void Load(ContainerBuilder builder) { builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().As().SingleInstance(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); @@ -56,10 +61,10 @@ public class TagsCloudPainterLibModule : Module { protected override void Load(ContainerBuilder builder) { - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().As(); diff --git a/TagsCloudPainterApplication/Properties/IAppSettings.cs b/TagsCloudPainterApplication/Properties/IAppSettings.cs new file mode 100644 index 000000000..4c8fcc840 --- /dev/null +++ b/TagsCloudPainterApplication/Properties/IAppSettings.cs @@ -0,0 +1,14 @@ +namespace TagsCloudPainterApplication.Properties +{ + public interface IAppSettings + { + public string BoringTextFilePath { get; set; } + public int ImageWidth { get; set; } + public int ImageHeight { get; set; } + public int TagFontSize { get; set; } + public string TagFontName { get; set; } + public double PointerStep { get; set; } + public double PointerRadiusConst { get; set; } + public double PointerAngleConst { get; set; } + } +} diff --git a/TagsCloudPainterApplication/Properties/Settings.Designer.cs b/TagsCloudPainterApplication/Properties/Settings.Designer.cs index 8b1faef14..d00b43c13 100644 --- a/TagsCloudPainterApplication/Properties/Settings.Designer.cs +++ b/TagsCloudPainterApplication/Properties/Settings.Designer.cs @@ -7,113 +7,141 @@ // //------------------------------------------------------------------------------ -namespace TagsCloudPainterApplication.Properties { - - +namespace TagsCloudPainterApplication.Properties +{ + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.9.0.0")] - public sealed partial class AppSettings : global::System.Configuration.ApplicationSettingsBase { - + public sealed partial class AppSettings : global::System.Configuration.ApplicationSettingsBase, IAppSettings + { + private static AppSettings defaultInstance = ((AppSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new AppSettings()))); - - public static AppSettings Default { - get { + + public static AppSettings Default + { + get + { return defaultInstance; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("Data\\BoringWords.txt")] - public string boringTextFilePath { - get { + public string BoringTextFilePath + { + get + { return ((string)(this["boringTextFilePath"])); } - set { + set + { this["boringTextFilePath"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("800")] - public int imageWidth { - get { + public int ImageWidth + { + get + { return ((int)(this["imageWidth"])); } - set { + set + { this["imageWidth"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("600")] - public int imageHeight { - get { + public int ImageHeight + { + get + { return ((int)(this["imageHeight"])); } - set { + set + { this["imageHeight"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("32")] - public int tagFontSize { - get { + public int TagFontSize + { + get + { return ((int)(this["tagFontSize"])); } - set { + set + { this["tagFontSize"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("Arial")] - public string tagFontName { - get { + public string TagFontName + { + get + { return ((string)(this["tagFontName"])); } - set { + set + { this["tagFontName"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("0.1")] - public double pointerStep { - get { + public double PointerStep + { + get + { return ((double)(this["pointerStep"])); } - set { + set + { this["pointerStep"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("0.5")] - public double pointerRadiusConst { - get { + public double PointerRadiusConst + { + get + { return ((double)(this["pointerRadiusConst"])); } - set { + set + { this["pointerRadiusConst"] = value; } } - + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("1")] - public double pointerAngleConst { - get { + public double PointerAngleConst + { + get + { return ((double)(this["pointerAngleConst"])); } - set { + set + { this["pointerAngleConst"] = value; } } diff --git a/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs b/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs index 1db74ba32..b92e3830e 100644 --- a/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs +++ b/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs @@ -5,11 +5,16 @@ using TagsCloudPainter.FormPointer; using TagsCloudPainter.Parser; using TagsCloudPainter.Settings; +using TagsCloudPainter.Settings.Cloud; +using TagsCloudPainter.Settings.FormPointer; +using TagsCloudPainter.Settings.Tag; using TagsCloudPainter.Tags; using TagsCloudPainterApplication; using TagsCloudPainterApplication.Actions; using TagsCloudPainterApplication.Infrastructure; -using TagsCloudPainterApplication.Infrastructure.Settings; +using TagsCloudPainterApplication.Infrastructure.Settings.FilesSource; +using TagsCloudPainterApplication.Infrastructure.Settings.Image; +using TagsCloudPainterApplication.Infrastructure.Settings.TagsCloud; using TagsCloudPainterApplication.Properties; namespace TagsCloudPainterApplicationTests; @@ -45,21 +50,21 @@ public void TearDown() private static IEnumerable SingleInstanceDependencesTypes => new[] { - new TestCaseData(typeof(TagSettings)), + new TestCaseData(typeof(ITagSettings)), new TestCaseData(typeof(CloudDrawer)), new TestCaseData(typeof(IFormatFileReader)), new TestCaseData(typeof(ITextParser)), new TestCaseData(typeof(ITagsBuilder)), - new TestCaseData(typeof(TextSettings)), - new TestCaseData(typeof(CloudSettings)), - new TestCaseData(typeof(SpiralPointerSettings)), + new TestCaseData(typeof(ITextSettings)), + new TestCaseData(typeof(ICloudSettings)), + new TestCaseData(typeof(ISpiralPointerSettings)), new TestCaseData(typeof(Palette)), - new TestCaseData(typeof(ImageSettings)), - new TestCaseData(typeof(FilesSourceSettings)), - new TestCaseData(typeof(TagsCloudSettings)), + new TestCaseData(typeof(IImageSettings)), + new TestCaseData(typeof(IFilesSourceSettings)), + new TestCaseData(typeof(ITagsCloudSettings)), new TestCaseData(typeof(PictureBoxImageHolder)), new TestCaseData(typeof(IImageHolder)), - new TestCaseData(typeof(AppSettings)) + new TestCaseData(typeof(IAppSettings)) }; [TestCaseSource(nameof(SingleInstanceDependencesTypes))] diff --git a/Tests/ArchimedeanSpiralTests.cs b/Tests/ArchimedeanSpiralTests.cs index dd70be92e..4ac3ab3f4 100644 --- a/Tests/ArchimedeanSpiralTests.cs +++ b/Tests/ArchimedeanSpiralTests.cs @@ -1,6 +1,7 @@ using System.Drawing; using TagsCloudPainter.FormPointer; -using TagsCloudPainter.Settings; +using TagsCloudPainter.Settings.Cloud; +using TagsCloudPainter.Settings.FormPointer; namespace TagsCloudPainterTests; diff --git a/Tests/BoringTextParserTests.cs b/Tests/BoringTextParserTests.cs index 462e9e534..d27b35f62 100644 --- a/Tests/BoringTextParserTests.cs +++ b/Tests/BoringTextParserTests.cs @@ -15,7 +15,7 @@ public void Setup() boringTextParser = new BoringTextParser(textSettings); } - private TextSettings textSettings; + private ITextSettings textSettings; private BoringTextParser boringTextParser; private string boringText; diff --git a/Tests/CloudDrawerTests.cs b/Tests/CloudDrawerTests.cs index 749f450ce..161fae9e3 100644 --- a/Tests/CloudDrawerTests.cs +++ b/Tests/CloudDrawerTests.cs @@ -1,7 +1,8 @@ using System.Drawing; using TagsCloudPainter; using TagsCloudPainter.Drawer; -using TagsCloudPainter.Settings; +using TagsCloudPainter.Settings.Cloud; +using TagsCloudPainter.Settings.Tag; using TagsCloudPainter.Tags; namespace TagsCloudPainterTests; diff --git a/Tests/TagsBuilderTests.cs b/Tests/TagsBuilderTests.cs index 606f3bb1c..4ec57e7c8 100644 --- a/Tests/TagsBuilderTests.cs +++ b/Tests/TagsBuilderTests.cs @@ -1,5 +1,5 @@ using FluentAssertions; -using TagsCloudPainter.Settings; +using TagsCloudPainter.Settings.Tag; using TagsCloudPainter.Tags; namespace TagsCloudPainterTests; diff --git a/Tests/TagsCloudLayouterTests.cs b/Tests/TagsCloudLayouterTests.cs index 548ec3052..f20ae42c7 100644 --- a/Tests/TagsCloudLayouterTests.cs +++ b/Tests/TagsCloudLayouterTests.cs @@ -4,7 +4,9 @@ using TagsCloudPainter.CloudLayouter; using TagsCloudPainter.Extensions; using TagsCloudPainter.FormPointer; -using TagsCloudPainter.Settings; +using TagsCloudPainter.Settings.Cloud; +using TagsCloudPainter.Settings.FormPointer; +using TagsCloudPainter.Settings.Tag; using TagsCloudPainter.Sizer; using TagsCloudPainter.Tags; @@ -27,7 +29,7 @@ public void Setup() } private TagsCloudLayouter tagsCloudLayouter; - private TagSettings tagSettings; + private ITagSettings tagSettings; private IStringSizer stringSizer; private static IEnumerable PutNextTagArgumentException => new[] From 4f024d3e3b87896afa5753a8c1c2b4287885237e Mon Sep 17 00:00:00 2001 From: daHil Date: Thu, 1 Feb 2024 22:40:01 +0500 Subject: [PATCH 32/34] added interface for cloud drawer --- TagsCloudPainter/CloudDrawer/CloudDrawer.cs | 2 +- TagsCloudPainter/CloudDrawer/ICloudDrawer.cs | 9 +++++++++ .../Actions/DrawTagCloudAction.cs | 4 ++-- TagsCloudPainterApplication/Program.cs | 2 +- .../DependencyInjectionTests.cs | 2 +- Tests/CloudDrawerTests.cs | 2 +- 6 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 TagsCloudPainter/CloudDrawer/ICloudDrawer.cs diff --git a/TagsCloudPainter/CloudDrawer/CloudDrawer.cs b/TagsCloudPainter/CloudDrawer/CloudDrawer.cs index 6bf95cb46..470537fbe 100644 --- a/TagsCloudPainter/CloudDrawer/CloudDrawer.cs +++ b/TagsCloudPainter/CloudDrawer/CloudDrawer.cs @@ -5,7 +5,7 @@ namespace TagsCloudPainter.Drawer; -public class CloudDrawer +public class CloudDrawer : ICloudDrawer { private readonly ICloudSettings cloudSettings; private readonly ITagSettings tagSettings; diff --git a/TagsCloudPainter/CloudDrawer/ICloudDrawer.cs b/TagsCloudPainter/CloudDrawer/ICloudDrawer.cs new file mode 100644 index 000000000..945ee26fd --- /dev/null +++ b/TagsCloudPainter/CloudDrawer/ICloudDrawer.cs @@ -0,0 +1,9 @@ +using System.Drawing; + +namespace TagsCloudPainter.Drawer +{ + public interface ICloudDrawer + { + public Bitmap DrawCloud(TagsCloud cloud, int imageWidth, int imageHeight); + } +} diff --git a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs index 80106cdac..16aab8d94 100644 --- a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs +++ b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs @@ -14,7 +14,7 @@ namespace TagsCloudPainterApplication.Actions; public class DrawTagCloudAction : IUiAction { - private readonly CloudDrawer cloudDrawer; + private readonly ICloudDrawer cloudDrawer; private readonly ICloudLayouter cloudLayouter; private readonly IFormatFileReader textFileReader; private readonly IFilesSourceSettings filesSourceSettings; @@ -30,7 +30,7 @@ public DrawTagCloudAction( ITagsCloudSettings tagsCloudSettings, IFilesSourceSettings filesSourceSettings, IImageHolder imageHolder, - CloudDrawer cloudDrawer, + ICloudDrawer cloudDrawer, ICloudLayouter cloudLayouter, ITagsBuilder tagsBuilder, ITextParser textParser, diff --git a/TagsCloudPainterApplication/Program.cs b/TagsCloudPainterApplication/Program.cs index f114b6d8f..cb24aed79 100644 --- a/TagsCloudPainterApplication/Program.cs +++ b/TagsCloudPainterApplication/Program.cs @@ -66,7 +66,7 @@ protected override void Load(ContainerBuilder builder) builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().As().SingleInstance(); builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As().SingleInstance(); diff --git a/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs b/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs index b92e3830e..767b57b2b 100644 --- a/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs +++ b/TagsCloudPainterApplicationTests/DependencyInjectionTests.cs @@ -51,7 +51,7 @@ public void TearDown() private static IEnumerable SingleInstanceDependencesTypes => new[] { new TestCaseData(typeof(ITagSettings)), - new TestCaseData(typeof(CloudDrawer)), + new TestCaseData(typeof(ICloudDrawer)), new TestCaseData(typeof(IFormatFileReader)), new TestCaseData(typeof(ITextParser)), new TestCaseData(typeof(ITagsBuilder)), diff --git a/Tests/CloudDrawerTests.cs b/Tests/CloudDrawerTests.cs index 161fae9e3..1106934e2 100644 --- a/Tests/CloudDrawerTests.cs +++ b/Tests/CloudDrawerTests.cs @@ -18,7 +18,7 @@ public void Setup() drawer = new CloudDrawer(tagSettings, cloudSettings); } - private CloudDrawer drawer; + private ICloudDrawer drawer; private static IEnumerable DrawArgumentException => new[] { From e463cc47ec6042d51cd074909014d772f5fcd4f8 Mon Sep 17 00:00:00 2001 From: daHil Date: Thu, 1 Feb 2024 22:42:04 +0500 Subject: [PATCH 33/34] cleaned up --- TagsCloudPainter/CloudDrawer/ICloudDrawer.cs | 11 +++---- TagsCloudPainter/FileReader/IFileReader.cs | 13 ++++---- TagsCloudPainter/FileReader/TextFileReader.cs | 9 ++--- TagsCloudPainter/FileReader/TxtFileReader.cs | 3 +- .../Settings/Cloud/ICloudSettings.cs | 13 ++++---- .../FormPointer/ISpiralPointerSettings.cs | 15 ++++----- TagsCloudPainter/Settings/Tag/ITagSettings.cs | 15 ++++----- .../Settings/Text/ITextSettings.cs | 11 +++---- TagsCloudPainter/TagsCloudPainter.csproj | 4 +-- .../Actions/DrawTagCloudAction.cs | 2 +- .../FilesSource/IFilesSourceSettings.cs | 11 +++---- .../Settings/Image/IImageSettings.cs | 13 ++++---- .../Settings/TagsCloud/ITagsCloudSettings.cs | 33 +++++++++---------- .../PictureBoxImageHolder.cs | 6 ++-- .../Properties/IAppSettings.cs | 25 +++++++------- .../TagsCloudPainterApplication.csproj | 8 ++--- Tests/CloudDrawerTests.cs | 12 +++---- Tests/TagsCloudLayouterTests.cs | 8 ++--- Tests/TagsCloudPainterTests.csproj | 16 ++++----- Tests/TextFileReaderTests.cs | 2 +- 20 files changed, 110 insertions(+), 120 deletions(-) diff --git a/TagsCloudPainter/CloudDrawer/ICloudDrawer.cs b/TagsCloudPainter/CloudDrawer/ICloudDrawer.cs index 945ee26fd..6a8365cdb 100644 --- a/TagsCloudPainter/CloudDrawer/ICloudDrawer.cs +++ b/TagsCloudPainter/CloudDrawer/ICloudDrawer.cs @@ -1,9 +1,8 @@ using System.Drawing; -namespace TagsCloudPainter.Drawer +namespace TagsCloudPainter.Drawer; + +public interface ICloudDrawer { - public interface ICloudDrawer - { - public Bitmap DrawCloud(TagsCloud cloud, int imageWidth, int imageHeight); - } -} + public Bitmap DrawCloud(TagsCloud cloud, int imageWidth, int imageHeight); +} \ No newline at end of file diff --git a/TagsCloudPainter/FileReader/IFileReader.cs b/TagsCloudPainter/FileReader/IFileReader.cs index 47571f915..62f7f12c7 100644 --- a/TagsCloudPainter/FileReader/IFileReader.cs +++ b/TagsCloudPainter/FileReader/IFileReader.cs @@ -1,8 +1,7 @@ -namespace TagsCloudPainter.FileReader +namespace TagsCloudPainter.FileReader; + +public interface IFileReader { - public interface IFileReader - { - public HashSet SupportedExtensions { get; } - public string ReadFile(string path); - } -} + public HashSet SupportedExtensions { get; } + public string ReadFile(string path); +} \ No newline at end of file diff --git a/TagsCloudPainter/FileReader/TextFileReader.cs b/TagsCloudPainter/FileReader/TextFileReader.cs index c22366804..677976f97 100644 --- a/TagsCloudPainter/FileReader/TextFileReader.cs +++ b/TagsCloudPainter/FileReader/TextFileReader.cs @@ -15,11 +15,12 @@ public string ReadFile(string path) throw new FileNotFoundException(); var fileExtension = Path.GetExtension(path); - var fileReader = fileReaders.FirstOrDefault(fileReader => fileReader.SupportedExtensions.Contains(fileExtension)); + var fileReader = + fileReaders.FirstOrDefault(fileReader => fileReader.SupportedExtensions.Contains(fileExtension)); - return fileReader is not null - ? fileReader.ReadFile(path) + return fileReader is not null + ? fileReader.ReadFile(path) : throw new ArgumentException($"Incorrect file extension {fileExtension}. " + - $"Supported file extensions: txt, doc, docx"); + $"Supported file extensions: txt, doc, docx"); } } \ No newline at end of file diff --git a/TagsCloudPainter/FileReader/TxtFileReader.cs b/TagsCloudPainter/FileReader/TxtFileReader.cs index b0fe705cb..9e9c3450e 100644 --- a/TagsCloudPainter/FileReader/TxtFileReader.cs +++ b/TagsCloudPainter/FileReader/TxtFileReader.cs @@ -1,5 +1,4 @@ - -namespace TagsCloudPainter.FileReader; +namespace TagsCloudPainter.FileReader; public class TxtFileReader : IFileReader { diff --git a/TagsCloudPainter/Settings/Cloud/ICloudSettings.cs b/TagsCloudPainter/Settings/Cloud/ICloudSettings.cs index 21a262b3d..5f003a91f 100644 --- a/TagsCloudPainter/Settings/Cloud/ICloudSettings.cs +++ b/TagsCloudPainter/Settings/Cloud/ICloudSettings.cs @@ -1,10 +1,9 @@ using System.Drawing; -namespace TagsCloudPainter.Settings.Cloud +namespace TagsCloudPainter.Settings.Cloud; + +public interface ICloudSettings { - public interface ICloudSettings - { - public Point CloudCenter { get; set; } - public Color BackgroundColor { get; set; } - } -} + public Point CloudCenter { get; set; } + public Color BackgroundColor { get; set; } +} \ No newline at end of file diff --git a/TagsCloudPainter/Settings/FormPointer/ISpiralPointerSettings.cs b/TagsCloudPainter/Settings/FormPointer/ISpiralPointerSettings.cs index 54e2170e2..0f7761089 100644 --- a/TagsCloudPainter/Settings/FormPointer/ISpiralPointerSettings.cs +++ b/TagsCloudPainter/Settings/FormPointer/ISpiralPointerSettings.cs @@ -1,9 +1,8 @@ -namespace TagsCloudPainter.Settings.FormPointer +namespace TagsCloudPainter.Settings.FormPointer; + +public interface ISpiralPointerSettings { - public interface ISpiralPointerSettings - { - public double Step { get; set; } - public double RadiusConst { get; set; } - public double AngleConst { get; set; } - } -} + public double Step { get; set; } + public double RadiusConst { get; set; } + public double AngleConst { get; set; } +} \ No newline at end of file diff --git a/TagsCloudPainter/Settings/Tag/ITagSettings.cs b/TagsCloudPainter/Settings/Tag/ITagSettings.cs index 227f085d9..3670f6362 100644 --- a/TagsCloudPainter/Settings/Tag/ITagSettings.cs +++ b/TagsCloudPainter/Settings/Tag/ITagSettings.cs @@ -1,11 +1,10 @@ using System.Drawing; -namespace TagsCloudPainter.Settings.Tag +namespace TagsCloudPainter.Settings.Tag; + +public interface ITagSettings { - public interface ITagSettings - { - public int TagFontSize { get; set; } - public string TagFontName { get; set; } - public Color TagColor { get; set; } - } -} + public int TagFontSize { get; set; } + public string TagFontName { get; set; } + public Color TagColor { get; set; } +} \ No newline at end of file diff --git a/TagsCloudPainter/Settings/Text/ITextSettings.cs b/TagsCloudPainter/Settings/Text/ITextSettings.cs index cc1e114bd..a7c852cbf 100644 --- a/TagsCloudPainter/Settings/Text/ITextSettings.cs +++ b/TagsCloudPainter/Settings/Text/ITextSettings.cs @@ -1,7 +1,6 @@ -namespace TagsCloudPainter.Settings +namespace TagsCloudPainter.Settings; + +public interface ITextSettings { - public interface ITextSettings - { - public string BoringText { get; set; } - } -} + public string BoringText { get; set; } +} \ No newline at end of file diff --git a/TagsCloudPainter/TagsCloudPainter.csproj b/TagsCloudPainter/TagsCloudPainter.csproj index 10e1428c6..5d7a5062b 100644 --- a/TagsCloudPainter/TagsCloudPainter.csproj +++ b/TagsCloudPainter/TagsCloudPainter.csproj @@ -8,8 +8,8 @@ - - + + diff --git a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs index 16aab8d94..adbb28b2c 100644 --- a/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs +++ b/TagsCloudPainterApplication/Actions/DrawTagCloudAction.cs @@ -16,13 +16,13 @@ public class DrawTagCloudAction : IUiAction { private readonly ICloudDrawer cloudDrawer; private readonly ICloudLayouter cloudLayouter; - private readonly IFormatFileReader textFileReader; private readonly IFilesSourceSettings filesSourceSettings; private readonly IImageHolder imageHolder; private readonly IImageSettings imageSettings; private readonly Palette palette; private readonly ITagsBuilder tagsBuilder; private readonly ITagsCloudSettings tagsCloudSettings; + private readonly IFormatFileReader textFileReader; private readonly ITextParser textParser; public DrawTagCloudAction( diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/FilesSource/IFilesSourceSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSource/IFilesSourceSettings.cs index 5171dfbfd..76955e9a7 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/FilesSource/IFilesSourceSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/FilesSource/IFilesSourceSettings.cs @@ -1,7 +1,6 @@ -namespace TagsCloudPainterApplication.Infrastructure.Settings.FilesSource +namespace TagsCloudPainterApplication.Infrastructure.Settings.FilesSource; + +public interface IFilesSourceSettings { - public interface IFilesSourceSettings - { - public string BoringTextFilePath { get; set; } - } -} + public string BoringTextFilePath { get; set; } +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/Image/IImageSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/Image/IImageSettings.cs index 479e7a213..1e34d23b3 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/Image/IImageSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/Image/IImageSettings.cs @@ -1,8 +1,7 @@ -namespace TagsCloudPainterApplication.Infrastructure.Settings.Image +namespace TagsCloudPainterApplication.Infrastructure.Settings.Image; + +public interface IImageSettings { - public interface IImageSettings - { - public int Width { get; set; } - public int Height { get; set; } - } -} + public int Width { get; set; } + public int Height { get; set; } +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloud/ITagsCloudSettings.cs b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloud/ITagsCloudSettings.cs index a16322539..7629e792e 100644 --- a/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloud/ITagsCloudSettings.cs +++ b/TagsCloudPainterApplication/Infrastructure/Settings/TagsCloud/ITagsCloudSettings.cs @@ -1,27 +1,26 @@ -using TagsCloudPainter.Settings.Cloud; +using TagsCloudPainter.Settings; +using TagsCloudPainter.Settings.Cloud; using TagsCloudPainter.Settings.FormPointer; using TagsCloudPainter.Settings.Tag; -using TagsCloudPainter.Settings; -namespace TagsCloudPainterApplication.Infrastructure.Settings.TagsCloud +namespace TagsCloudPainterApplication.Infrastructure.Settings.TagsCloud; + +public interface ITagsCloudSettings { - public interface ITagsCloudSettings - { - public ICloudSettings CloudSettings { get; } - public ITagSettings TagSettings { get; } - public ISpiralPointerSettings SpiralPointerSettings { get; } - public ITextSettings TextSettings { get; } + public ICloudSettings CloudSettings { get; } + public ITagSettings TagSettings { get; } + public ISpiralPointerSettings SpiralPointerSettings { get; } + public ITextSettings TextSettings { get; } - public int TagFontSize { get; set; } + public int TagFontSize { get; set; } - public string TagFontName { get; set; } + public string TagFontName { get; set; } - public Point CloudCenter { get; set; } + public Point CloudCenter { get; set; } - public double PointerStep { get; set; } + public double PointerStep { get; set; } - public double PointerRadiusConst { get; set; } + public double PointerRadiusConst { get; set; } - public double PointerAngleConst { get; set; } - } -} + public double PointerAngleConst { get; set; } +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/PictureBoxImageHolder.cs b/TagsCloudPainterApplication/PictureBoxImageHolder.cs index ef17d6678..bce6eb567 100644 --- a/TagsCloudPainterApplication/PictureBoxImageHolder.cs +++ b/TagsCloudPainterApplication/PictureBoxImageHolder.cs @@ -1,5 +1,4 @@ using System.Drawing.Imaging; -using System.Reflection; using TagsCloudPainterApplication.Infrastructure; using TagsCloudPainterApplication.Infrastructure.Settings.Image; @@ -46,8 +45,9 @@ public Image GetImage() private static ImageFormat GetImageFormat(string extension) { - PropertyInfo prop = typeof(ImageFormat) - .GetProperties().Where(p => p.Name.Equals(extension.Replace(".", ""), StringComparison.InvariantCultureIgnoreCase)) + var prop = typeof(ImageFormat) + .GetProperties().Where(p => + p.Name.Equals(extension.Replace(".", ""), StringComparison.InvariantCultureIgnoreCase)) .FirstOrDefault(); return prop is not null diff --git a/TagsCloudPainterApplication/Properties/IAppSettings.cs b/TagsCloudPainterApplication/Properties/IAppSettings.cs index 4c8fcc840..6808ef473 100644 --- a/TagsCloudPainterApplication/Properties/IAppSettings.cs +++ b/TagsCloudPainterApplication/Properties/IAppSettings.cs @@ -1,14 +1,13 @@ -namespace TagsCloudPainterApplication.Properties +namespace TagsCloudPainterApplication.Properties; + +public interface IAppSettings { - public interface IAppSettings - { - public string BoringTextFilePath { get; set; } - public int ImageWidth { get; set; } - public int ImageHeight { get; set; } - public int TagFontSize { get; set; } - public string TagFontName { get; set; } - public double PointerStep { get; set; } - public double PointerRadiusConst { get; set; } - public double PointerAngleConst { get; set; } - } -} + public string BoringTextFilePath { get; set; } + public int ImageWidth { get; set; } + public int ImageHeight { get; set; } + public int TagFontSize { get; set; } + public string TagFontName { get; set; } + public double PointerStep { get; set; } + public double PointerRadiusConst { get; set; } + public double PointerAngleConst { get; set; } +} \ No newline at end of file diff --git a/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj b/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj index b191bda07..6af042650 100644 --- a/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj +++ b/TagsCloudPainterApplication/TagsCloudPainterApplication.csproj @@ -10,11 +10,11 @@ - + - + @@ -30,7 +30,7 @@ - Always + Always SettingsSingleFileGenerator @@ -39,7 +39,7 @@ - + \ No newline at end of file diff --git a/Tests/CloudDrawerTests.cs b/Tests/CloudDrawerTests.cs index 1106934e2..a3c092e7d 100644 --- a/Tests/CloudDrawerTests.cs +++ b/Tests/CloudDrawerTests.cs @@ -23,13 +23,13 @@ public void Setup() private static IEnumerable DrawArgumentException => new[] { new TestCaseData(new TagsCloud(new Point(0, 0), - new List<(Tag, Rectangle)> { (new Tag("a", 1, 1), new(1, 1, 1, 1) )} ), 0, 1) + new List<(Tag, Rectangle)> { (new Tag("a", 1, 1), new Rectangle(1, 1, 1, 1)) }), 0, 1) .SetName("WhenGivenNotPositiveImageWidth"), new TestCaseData(new TagsCloud(new Point(0, 0), - new List <(Tag, Rectangle) > {(new Tag("a", 1, 1), new(1, 1, 1, 1)) }), 1, 0) + new List<(Tag, Rectangle)> { (new Tag("a", 1, 1), new Rectangle(1, 1, 1, 1)) }), 1, 0) .SetName("WhenGivenNotPositiveImageHeight"), new TestCaseData(new TagsCloud(new Point(0, 0), - new List <(Tag, Rectangle) > {(new Tag("a", 1, 1), new(1, 1, 1, 1)) }), 0, 0) + new List<(Tag, Rectangle)> { (new Tag("a", 1, 1), new Rectangle(1, 1, 1, 1)) }), 0, 0) .SetName("WhenGivenNotPositiveImageHeightAndWidth"), new TestCaseData(new TagsCloud(new Point(0, 0), new List<(Tag, Rectangle)>()), 1, 1) .SetName("WhenGivenCloudWithEmptyTagsDictionary") @@ -44,13 +44,13 @@ public void Draw_ShouldThrowArgumentException(TagsCloud cloud, int width, int he private static IEnumerable DrawNoException => new[] { new TestCaseData(new TagsCloud(new Point(5, 5), - new List<(Tag, Rectangle)> { ( new Tag("abcdadg", 10, 1), new(5, 5, 20, 3) ) }), 10, 10) + new List<(Tag, Rectangle)> { (new Tag("abcdadg", 10, 1), new Rectangle(5, 5, 20, 3)) }), 10, 10) .SetName("WhenCloudWidthIsGreaterThanImageWidth"), new TestCaseData(new TagsCloud(new Point(5, 5), - new List <(Tag, Rectangle) > {(new Tag("abcdadg", 10, 1), new(5, 5, 3, 20)) }), 10, 10) + new List<(Tag, Rectangle)> { (new Tag("abcdadg", 10, 1), new Rectangle(5, 5, 3, 20)) }), 10, 10) .SetName("WhenCloudHeightIsGreaterThanImageHeight"), new TestCaseData(new TagsCloud(new Point(5, 5), - new List <(Tag, Rectangle) > {(new Tag("abcdadg", 10, 1), new(5, 5, 20, 20)) }), 10, 10) + new List<(Tag, Rectangle)> { (new Tag("abcdadg", 10, 1), new Rectangle(5, 5, 20, 20)) }), 10, 10) .SetName("WhenCloudIsBiggerThanImage") }; diff --git a/Tests/TagsCloudLayouterTests.cs b/Tests/TagsCloudLayouterTests.cs index f20ae42c7..2ecb25dfd 100644 --- a/Tests/TagsCloudLayouterTests.cs +++ b/Tests/TagsCloudLayouterTests.cs @@ -24,7 +24,7 @@ public void Setup() var formPointer = new ArchimedeanSpiralPointer(cloudSettings, pointerSettings); stringSizer = A.Fake(); A.CallTo(() => stringSizer.GetStringSize(A.Ignored, A.Ignored, A.Ignored)) - .Returns(new Size(10,10)); + .Returns(new Size(10, 10)); tagsCloudLayouter = new TagsCloudLayouter(cloudSettings, formPointer, tagSettings, stringSizer); } @@ -34,9 +34,9 @@ public void Setup() private static IEnumerable PutNextTagArgumentException => new[] { - new TestCaseData(new Size(0,10)).SetName("WidthNotPossitive"), - new TestCaseData(new Size(10,0)).SetName("HeightNotPossitive"), - new TestCaseData(new Size(0,0)).SetName("HeightAndWidthNotPossitive"), + new TestCaseData(new Size(0, 10)).SetName("WidthNotPossitive"), + new TestCaseData(new Size(10, 0)).SetName("HeightNotPossitive"), + new TestCaseData(new Size(0, 0)).SetName("HeightAndWidthNotPossitive") }; [TestCaseSource(nameof(PutNextTagArgumentException))] diff --git a/Tests/TagsCloudPainterTests.csproj b/Tests/TagsCloudPainterTests.csproj index d5e419e07..1db4569f2 100644 --- a/Tests/TagsCloudPainterTests.csproj +++ b/Tests/TagsCloudPainterTests.csproj @@ -10,17 +10,17 @@ - - - - - - - + + + + + + + - + diff --git a/Tests/TextFileReaderTests.cs b/Tests/TextFileReaderTests.cs index 84b736ac6..1f92d3278 100644 --- a/Tests/TextFileReaderTests.cs +++ b/Tests/TextFileReaderTests.cs @@ -8,7 +8,7 @@ public class TextFileReaderTests [SetUp] public void Setup() { - var fileReaders = new List() { new TxtFileReader(), new DocFileReader()}; + var fileReaders = new List { new TxtFileReader(), new DocFileReader() }; textFileReader = new TextFileReader(fileReaders); } From 944d472f405d32036093a8f8a55aed4d44d21924 Mon Sep 17 00:00:00 2001 From: daHil Date: Thu, 1 Feb 2024 22:57:01 +0500 Subject: [PATCH 34/34] added gif and bmp formats for image saving --- TagsCloudPainterApplication/Actions/SaveImageAction.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TagsCloudPainterApplication/Actions/SaveImageAction.cs b/TagsCloudPainterApplication/Actions/SaveImageAction.cs index a4876ba7d..1e905de5f 100644 --- a/TagsCloudPainterApplication/Actions/SaveImageAction.cs +++ b/TagsCloudPainterApplication/Actions/SaveImageAction.cs @@ -23,7 +23,7 @@ public void Perform() InitialDirectory = Path.GetFullPath(Environment.CurrentDirectory), DefaultExt = "png", FileName = "image.png", - Filter = "Изображения (*.png)|*.png|Изображения (*.jpeg)|*.jpeg" + Filter = "Изображения (*.png)|*.png|Изображения (*.jpeg)|*.jpeg|Изображения (*.bmp)|*.bmp|Изображения (*.gif)|*.gif" }; var res = dialog.ShowDialog(); if (res == DialogResult.OK)