зеркало из https://github.com/stride3d/GraphX.git
Refactored file handling
Refactored file handling, removing IFileServiceProvider and changing GraphArea so that it simply provides the data for serialization and accepts data for deserialization. This makes it easier to handle serialization with other storage types. (Still need mods for METRO)
This commit is contained in:
Родитель
ba1243cd33
Коммит
1f08a3cfd9
|
@ -1,14 +1,9 @@
|
|||
using GraphX.PCL.Logic.Models;
|
||||
using QuickGraph;
|
||||
using ShowcaseApp.WPF.FileSerialization;
|
||||
|
||||
namespace ShowcaseApp.WPF
|
||||
{
|
||||
public class LogicCoreExample : GXLogicCore<DataVertex, DataEdge, BidirectionalGraph<DataVertex, DataEdge>>
|
||||
{
|
||||
public LogicCoreExample()
|
||||
{
|
||||
FileServiceProvider = new FileServiceProviderWpf();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,16 +8,16 @@ using YAXLib;
|
|||
namespace ShowcaseApp.WPF.FileSerialization
|
||||
{
|
||||
/// <summary>
|
||||
/// WPF implementation of IFileServiceProvider
|
||||
/// WPF implementation of file serialization and deserialization
|
||||
/// </summary>
|
||||
public class FileServiceProviderWpf: IFileServiceProvider
|
||||
public static class FileServiceProviderWpf
|
||||
{
|
||||
/// <summary>
|
||||
/// Serializes data classes list to file
|
||||
/// </summary>
|
||||
/// <param name="filename">File name</param>
|
||||
/// <param name="modelsList">Data classes list</param>
|
||||
public void SerializeDataToFile(string filename, List<GraphSerializationData> modelsList)
|
||||
public static void SerializeDataToFile(string filename, List<GraphSerializationData> modelsList)
|
||||
{
|
||||
var serializer = new YAXSerializer(typeof(List<GraphSerializationData>));
|
||||
using (var textWriter = new StreamWriter(filename))
|
||||
|
@ -30,7 +30,7 @@ namespace ShowcaseApp.WPF.FileSerialization
|
|||
/// Deserializes data classes list from file
|
||||
/// </summary>
|
||||
/// <param name="filename">File name</param>
|
||||
public List<GraphSerializationData> DeserializeDataFromFile(string filename)
|
||||
public static List<GraphSerializationData> DeserializeDataFromFile(string filename)
|
||||
{
|
||||
var deserializer = new YAXSerializer(typeof(List<GraphSerializationData>));
|
||||
using (var textReader = new StreamReader(filename))
|
||||
|
|
|
@ -8,6 +8,7 @@ using GraphX.PCL.Common.Enums;
|
|||
using GraphX.WPF.Controls;
|
||||
using Microsoft.Win32;
|
||||
using QuickGraph;
|
||||
using ShowcaseApp.WPF.FileSerialization;
|
||||
using ShowcaseApp.WPF.Models;
|
||||
using Rect = GraphX.Measure.Rect;
|
||||
|
||||
|
@ -116,7 +117,7 @@ namespace ShowcaseApp.WPF.Pages
|
|||
var dlg = new SaveFileDialog { Filter = "All files|*.*", Title = "Select layout file name", FileName = "laytest.xml" };
|
||||
if (dlg.ShowDialog() == true)
|
||||
{
|
||||
gg_Area.SerializeToFile(dlg.FileName);
|
||||
FileServiceProviderWpf.SerializeDataToFile(dlg.FileName, gg_Area.ExtractSerializationData());
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
@ -135,7 +136,7 @@ namespace ShowcaseApp.WPF.Pages
|
|||
if (dlg.ShowDialog() != true) return;
|
||||
try
|
||||
{
|
||||
gg_Area.DeserializeFromFile(dlg.FileName);
|
||||
gg_Area.RebuildFromSerializationData(FileServiceProviderWpf.DeserializeDataFromFile(dlg.FileName));
|
||||
gg_Area.SetVerticesDrag(true, true);
|
||||
gg_Area.UpdateAllEdges();
|
||||
gg_zoomctrl.ZoomToFill();
|
||||
|
|
|
@ -1434,20 +1434,16 @@ namespace GraphX.WPF.Controls
|
|||
}
|
||||
#endregion
|
||||
|
||||
#region Save
|
||||
#region Serialization support
|
||||
|
||||
/// <summary>
|
||||
/// Serialize graph layout to file using custom FileServiceProvider
|
||||
/// Obtain graph layout data, which can then be used with a serializer.
|
||||
/// </summary>
|
||||
/// <param name="filename">Output filename</param>
|
||||
/// <exception cref="GX_InvalidDataException">Occures when LogicCore or object Id isn't set</exception>
|
||||
/// <exception cref="GX_ObjectNotFoundException">Occures when FileServiceProvider not set</exception>
|
||||
public void SerializeToFile(string filename)
|
||||
/// <exception cref="GX_InvalidDataException">Occurs when LogicCore or object Id isn't set</exception>
|
||||
public List<GraphSerializationData> ExtractSerializationData()
|
||||
{
|
||||
if (LogicCore == null)
|
||||
throw new GX_InvalidDataException("LogicCore -> Not initialized!");
|
||||
if (LogicCore.FileServiceProvider == null)
|
||||
throw new GX_ObjectNotFoundException("LogicCore::FileServiceProvider must be set before using file operation methods!");
|
||||
|
||||
if (AutoAssignMissingDataId)
|
||||
AutoresolveIds();
|
||||
|
@ -1456,40 +1452,32 @@ namespace GraphX.WPF.Controls
|
|||
foreach (var item in VertexList) //ALWAYS serialize vertices first
|
||||
{
|
||||
dlist.Add(new GraphSerializationData { Position = item.Value.GetPositionGraphX(), Data = item.Key });
|
||||
if (item.Key.ID == -1) throw new GX_InvalidDataException("SerializeToFile() -> All vertex datas must have positive unique ID!");
|
||||
if (item.Key.ID == -1) throw new GX_InvalidDataException("ExtractSerializationData() -> All vertex datas must have positive unique ID!");
|
||||
}
|
||||
foreach (var item in EdgesList)
|
||||
{
|
||||
// item.Key.RoutingPoints = new Point[] { new Point(0, 123), new Point(12, 12), new Point(10, 234.5) };
|
||||
// item.Key.RoutingPoints = new Point[] { new Point(0, 123), new Point(12, 12), new Point(10, 234.5) };
|
||||
dlist.Add(new GraphSerializationData { Position = new Measure.Point(), Data = item.Key });
|
||||
if (item.Key.ID == -1) throw new GX_InvalidDataException("SerializeToFile() -> All edge datas must have positive unique ID!");
|
||||
if (item.Key.ID == -1) throw new GX_InvalidDataException("ExtractSerializationData() -> All edge datas must have positive unique ID!");
|
||||
}
|
||||
|
||||
LogicCore.FileServiceProvider.SerializeDataToFile(filename, dlist);
|
||||
|
||||
return dlist;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes previously serialized graph layout from file
|
||||
/// Rebuilds the graph layout from serialization data.
|
||||
/// </summary>
|
||||
/// <param name="filename">Input filename</param>
|
||||
/// <exception cref="GX_InvalidDataException"></exception>
|
||||
/// <exception cref="GX_ObjectNotFoundException"></exception>
|
||||
/// <exception cref="GX_SerializationException"></exception>
|
||||
public void DeserializeFromFile(string filename)
|
||||
/// <param name="data">The serialization data</param>
|
||||
/// <exception cref="GX_InvalidDataException">Occurs when LogicCore isn't set</exception>
|
||||
/// <exception cref="GX_SerializationException">Occurs when edge source or target isn't set</exception>
|
||||
public void RebuildFromSerializationData(IEnumerable<GraphSerializationData> data)
|
||||
{
|
||||
if (LogicCore == null)
|
||||
throw new GX_InvalidDataException("LogicCore -> Not initialized!");
|
||||
if (LogicCore.FileServiceProvider == null)
|
||||
throw new GX_ObjectNotFoundException("LogicCore::FileServiceProvider must be set before using file operation methods!");
|
||||
|
||||
RemoveAllEdges();
|
||||
RemoveAllVertices();
|
||||
|
||||
|
||||
var data = LogicCore.FileServiceProvider.DeserializeDataFromFile(filename);
|
||||
|
||||
if (LogicCore.Graph == null) LogicCore.Graph = Activator.CreateInstance<TGraph>();
|
||||
if (LogicCore.Graph == null) LogicCore.Graph = Activator.CreateInstance<TGraph>();
|
||||
else LogicCore.Graph.Clear();
|
||||
|
||||
var vlist = data.Where(a => a.Data is TVertex);
|
||||
|
@ -1523,7 +1511,7 @@ namespace GraphX.WPF.Controls
|
|||
//to correctly draw arrows in any case except they are manually disabled
|
||||
UpdateLayout();
|
||||
foreach (var item in EdgesList.Values)
|
||||
item.OnApplyTemplate();
|
||||
item.OnApplyTemplate();
|
||||
|
||||
RestoreAlgorithmStorage();
|
||||
}
|
||||
|
|
|
@ -78,7 +78,6 @@
|
|||
<Compile Include="Interfaces\IExternalEdgeRouting.cs" />
|
||||
<Compile Include="Interfaces\IExternalLayout.cs" />
|
||||
<Compile Include="Interfaces\IExternalOverlapRemoval.cs" />
|
||||
<Compile Include="Interfaces\IFileServiceProvider.cs" />
|
||||
<Compile Include="Interfaces\IGraphXEdge.cs" />
|
||||
<Compile Include="Interfaces\IGraphXVertex.cs" />
|
||||
<Compile Include="Interfaces\IGXLogicCore.cs" />
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
using System.Collections.Generic;
|
||||
using GraphX.PCL.Common.Models;
|
||||
|
||||
namespace GraphX.PCL.Common.Interfaces
|
||||
{
|
||||
public interface IFileServiceProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Serializes specified list of data classes into file
|
||||
/// </summary>
|
||||
/// <param name="filename">File name</param>
|
||||
/// <param name="modelsList">Data classes list</param>
|
||||
void SerializeDataToFile(string filename, List<GraphSerializationData> modelsList);
|
||||
/// <summary>
|
||||
/// Deserializes specified file data into data classes list
|
||||
/// </summary>
|
||||
/// <param name="filename"></param>
|
||||
/// <returns></returns>
|
||||
List<GraphSerializationData> DeserializeDataFromFile(string filename);
|
||||
}
|
||||
}
|
|
@ -28,11 +28,6 @@ namespace GraphX.PCL.Common.Interfaces
|
|||
bool EnableEdgeLabelsOverlapRemoval { get; set; }
|
||||
bool IsCustomLayout { get; }
|
||||
|
||||
/// <summary>
|
||||
/// File service provider for graph serialization
|
||||
/// </summary>
|
||||
IFileServiceProvider FileServiceProvider { get; set; }
|
||||
|
||||
LayoutAlgorithmTypeEnum DefaultLayoutAlgorithm { get; set; }
|
||||
OverlapRemovalAlgorithmTypeEnum DefaultOverlapRemovalAlgorithm { get; set; }
|
||||
EdgeRoutingAlgorithmTypeEnum DefaultEdgeRoutingAlgorithm { get; set; }
|
||||
|
|
|
@ -30,11 +30,6 @@ namespace GraphX.PCL.Logic.Models
|
|||
public IAlgorithmStorage<TVertex, TEdge> AlgorithmStorage { get; private set; }
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets file operations provider for GXCore
|
||||
/// </summary>
|
||||
public IFileServiceProvider FileServiceProvider { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets if if edge label overlap removal enabled
|
||||
/// </summary>
|
||||
|
|
Загрузка…
Ссылка в новой задаче