Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Default Styles #7

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Sources/DotNetGraph.Tests/DefaultStylesGraphTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Drawing;
using DotNetGraph.Extensions;
using DotNetGraph.Node;
using NFluent;
using Xunit;

namespace DotNetGraph.Tests
{
public class DefaultStylesGraphTests
{
[Fact]
public void NullNodeDefaultStyle()
{
var graph = new DotGraph("TestGraph")
.WithDefaultNodeStyleLayout(null)
.AddNode("TestNode");

var compiled = graph.Compile();

Check.That(compiled).HasSameValueAs("graph TestGraph { TestNode ; }");
}

[Fact]
public void DefaultColorNodeDefaultStyle()
{
var graph = new DotGraph("TestGraph")
.WithDefaultNodeStyleLayout(new DotNodeStyleLayout
{
Color = Color.Red
})
.AddNode("TestNode");

var compiled = graph.Compile();

Check.That(compiled).HasSameValueAs("graph TestGraph { TestNode [color=\"#FF0000\"]; }");
}

[Fact]
public void CompleteNodeDefaultStyle()
{
var graph = new DotGraph("TestGraph")
.WithDefaultNodeStyleLayout(new DotNodeStyleLayout
{
Color = Color.Red,
Height = 0.64f,
Shape = DotNodeShape.Box,
Style = DotNodeStyle.Dashed,
FillColor = Color.Blue,
FontColor = Color.Yellow
})
.AddNode("TestNode");

var compiled = graph.Compile();

Check.That(compiled).HasSameValueAs("graph TestGraph { TestNode [color=\"#FF0000\",height=0.64,shape=box,style=dashed,fillcolor=\"#0000FF\",fontcolor=\"#FFFF00\"]; }");
}
}
}
5 changes: 4 additions & 1 deletion Sources/DotNetGraph.Tests/Node/BasicNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public void NodeWithColor()
{
Elements =
{
new DotNode("TestNode", Color.Red)
new DotNode("TestNode")
{
Color = Color.Red
}
}
};

Expand Down
8 changes: 7 additions & 1 deletion Sources/DotNetGraph/Compiler/DotCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ public DotCompiler(DotGraph graph)
_graph = graph;
}

public string Compile()
public string Compile(bool applyDefaultStyles = true)
{
if (applyDefaultStyles)
{
var styleProcessor = new DotStyleProcessor(_graph);
styleProcessor.ApplyDefaultLayoutStyles(_graph);
}

var builder = new StringBuilder();

CompileGraph(builder);
Expand Down
27 changes: 27 additions & 0 deletions Sources/DotNetGraph/Compiler/DotStyleProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using DotNetGraph.Core;
using DotNetGraph.Node;

namespace DotNetGraph.Compiler
{
public class DotStyleProcessor
{
private readonly DotGraph _graph;

public DotStyleProcessor(DotGraph graph)
{
_graph = graph;
}

public void ApplyDefaultLayoutStyles(IElementWithChildren element)
{
foreach (var child in element.Elements)
{
if (child is DotNode dotNodeChild)
dotNodeChild.ApplyStyleLayout(_graph.DefaultNodeStyleLayout);

if (child is IElementWithChildren childWithChildren)
ApplyDefaultLayoutStyles(childWithChildren);
}
}
}
}
9 changes: 9 additions & 0 deletions Sources/DotNetGraph/Core/IElementWithChildren.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace DotNetGraph.Core
{
public interface IElementWithChildren
{
List<IDotElement> Elements { get; }
}
}
21 changes: 16 additions & 5 deletions Sources/DotNetGraph/DotGraph.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Collections.Generic;
using DotNetGraph.Core;
using DotNetGraph.Node;

namespace DotNetGraph
{
public class DotGraph : IDotElement
public class DotGraph : IDotElement, IElementWithChildren
{
public bool Directed { get; set; }

Expand All @@ -12,16 +13,26 @@ public class DotGraph : IDotElement
public bool Strict { get; set; }

public List<IDotElement> Elements { get; }

public DotNodeStyleLayout DefaultNodeStyleLayout { get; set; }

public DotGraph()
public DotGraph(string identifier, bool directed = false)
{
Elements = new List<IDotElement>();
Identifier = identifier;
Directed = directed;
}

public DotGraph(string identifier, bool directed = false) : this()
public DotGraph AddElement(IDotElement element)
{
Identifier = identifier;
Directed = directed;
Elements.Add(element);
return this;
}

public DotGraph WithDefaultNodeStyleLayout(DotNodeStyleLayout nodeStyleLayout)
{
DefaultNodeStyleLayout = nodeStyleLayout;
return this;
}
}
}
11 changes: 9 additions & 2 deletions Sources/DotNetGraph/Extensions/DotGraphExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
using DotNetGraph.Compiler;
using DotNetGraph.Node;

namespace DotNetGraph.Extensions
{
public static class DotGraphExtensions
{
public static string Compile(this DotGraph graph)
public static string Compile(this DotGraph graph, bool applyDefaultStyles = true)
{
return new DotCompiler(graph).Compile();
return new DotCompiler(graph).Compile(applyDefaultStyles);
}

public static DotGraph AddNode(this DotGraph graph, string identifier, DotNodeStyleLayout styleLayout = null)
{
var node = new DotNode(identifier, styleLayout);
return graph.AddElement(node);
}
}
}
33 changes: 31 additions & 2 deletions Sources/DotNetGraph/Node/DotNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,39 @@ public DotNodeHeightAttribute Height
set => SetAttribute(value);
}

public DotNode(string identifier = null, DotColorAttribute color = null)
public DotNode(string identifier)
{
Identifier = identifier;
Color = color;
}

public DotNode(string identifier, DotNodeStyleLayout styleLayout)
{
Identifier = identifier;
ApplyStyleLayout(styleLayout);
}

public void ApplyStyleLayout(DotNodeStyleLayout styleLayout)
{
if (styleLayout is null)
return;

if (styleLayout.Color != null)
Color = styleLayout.Color;

if (styleLayout.Height != null)
Height = styleLayout.Height;

if (styleLayout.Shape != null)
Shape = styleLayout.Shape;

if (styleLayout.Style != null)
Style = styleLayout.Style;

if (styleLayout.FillColor != null)
FillColor = styleLayout.FillColor;

if (styleLayout.FontColor != null)
FontColor = styleLayout.FontColor;
}
}
}
19 changes: 19 additions & 0 deletions Sources/DotNetGraph/Node/DotNodeStyleLayout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using DotNetGraph.Attributes;

namespace DotNetGraph.Node
{
public class DotNodeStyleLayout
{
public DotColorAttribute Color { get; set; }

public DotFontColorAttribute FontColor { get; set; }

public DotFillColorAttribute FillColor { get; set; }

public DotNodeShapeAttribute Shape { get; set; }

public DotNodeStyleAttribute Style { get; set; }

public DotNodeHeightAttribute Height { get; set; }
}
}
6 changes: 3 additions & 3 deletions Sources/DotNetGraph/SubGraph/DotSubGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace DotNetGraph.SubGraph
{
public class DotSubGraph : DotElementWithAttributes
public class DotSubGraph : DotElementWithAttributes, IElementWithChildren
{
public string Identifier { get; set; }

public List<IDotElement> Elements { get; }

public DotColorAttribute Color
{
get => GetAttribute<DotColorAttribute>();
Expand All @@ -26,8 +28,6 @@ public DotLabelAttribute Label
set => SetAttribute(value);
}

public List<IDotElement> Elements { get; }

public DotSubGraph(string identifier = null)
{
Elements = new List<IDotElement>();
Expand Down