/// <summary> /// Builds the properties for the labels's <see cref="ILabelModel"/> type. /// </summary> protected virtual void BuildModelProperties(IPropertyBuildContext <ILabel> context) { ValueGetterDelegate <Type> labelModelGetter = new ValueGetterDelegate <Type>( delegate { var type = context.CurrentInstance.LayoutParameter.Model.GetType(); while (!type.IsPublic) { type = type.BaseType; } return(type); }); ValueSetterDelegate <Type> labelModelSetter = new ValueSetterDelegate <Type>( delegate(Type value) { IGraph graph = context.Lookup(typeof(IGraph)) as IGraph; if (graph != null) { ILabelModel model = Activator.CreateInstance(value) as ILabelModel; if (model != null) { ILabelModelParameterFinder finder = model.Lookup(typeof(ILabelModelParameterFinder)) as ILabelModelParameterFinder; ILabelModelParameter parameter; ILabel subject = context.CurrentInstance; if (finder != null) { parameter = finder.FindBestParameter(subject, model, subject.GetLayout()); } else { parameter = model.CreateDefaultParameter(); } graph.SetLabelLayoutParameter(subject, parameter); } } }); ILabel currentLabel; currentLabel = context.CurrentInstance; if (currentLabel == null) { return; } if (currentLabel.Owner is IEdge) { context.AddEntry(EdgeLabelModelProperty, labelModelGetter, labelModelSetter, null); } if (currentLabel.Owner is INode) { context.AddEntry(NodeLabelModelProperty, labelModelGetter, labelModelSetter, null); } if (currentLabel.Owner is IPort) { context.AddEntry(PortLabelModelProperty, labelModelGetter, labelModelSetter, null); } }
/// <summary> /// Does the label placement using the generic labeling algorithm. Before this, the model and size of the labels is /// set according to the option handlers settings. /// </summary> private async Task DoLabelPlacement() { if (inLayout) { return; } inLayout = true; toolBar.IsEnabled = false; editorControl.IsEnabled = false; //desired label model ILabelModel labelModel = LabelModels[(string)handler[LABEL_MODEL].Value]; int size = (int)handler[LABEL_SIZE].Value; foreach (var label in graphControl.Graph.Labels) { if (label.Owner is INode) { // only update the label model parameter if the label model changed if (labelModel != graphControl.Graph.NodeDefaults.Labels.LayoutParameter.Model) { graphControl.Graph.SetLabelLayoutParameter(label, labelModel.CreateDefaultParameter()); } var cityLabelStyle = label.Style as CityLabelStyle; if (cityLabelStyle != null && cityLabelStyle.InnerLabelStyle is DefaultLabelStyle) { ((DefaultLabelStyle)cityLabelStyle.InnerLabelStyle).TextSize = size; } graphControl.Graph.AdjustLabelPreferredSize(label); } } { // set as default label model parameter graphControl.Graph.NodeDefaults.Labels.LayoutParameter = labelModel.CreateDefaultParameter(); var cityLabelStyle = graphControl.Graph.NodeDefaults.Labels.Style as CityLabelStyle; if (cityLabelStyle != null && cityLabelStyle.InnerLabelStyle is DefaultLabelStyle) { ((DefaultLabelStyle)cityLabelStyle.InnerLabelStyle).TextSize = size; } } graphControl.Invalidate(); // configure and run the layout algorithm var labelingAlgorithm = new GenericLabeling { MaximumDuration = 0, OptimizationStrategy = OptimizationStrategy.Balanced, PlaceEdgeLabels = false, PlaceNodeLabels = true, ReduceLabelOverlaps = true, ProfitModel = new ExtendedLabelCandidateProfitModel(), }; var layoutExecutor = new LayoutExecutor(graphControl, graphControl.Graph, labelingAlgorithm) { Duration = TimeSpan.FromMilliseconds(500), EasedAnimation = true, AnimateViewport = false, UpdateContentRect = true }; await layoutExecutor.Start(); toolBar.IsEnabled = true; editorControl.IsEnabled = true; inLayout = false; }
/// <summary> /// Does the label placement using the generic labeling algorithm. Before this, the model and size of the labels is /// set according to the option handlers settings. /// </summary> private async Task DoLabelPlacement() { if (inLayout) { return; } inLayout = true; toolStrip.Enabled = false; //desired label model ILabelModel labelModel = LabelModels[labelModelComboBox.SelectedItem.ToString()]; //desired label size int size = Convert.ToInt32(sizeNumericUpDown.NumericUpDownControl.Text); foreach (var label in graphControl.Graph.Labels) { if (label.Owner is INode) { // only update the label model parameter if the label model changed if (labelModel != graphControl.Graph.NodeDefaults.Labels.LayoutParameter.Model) { graphControl.Graph.SetLabelLayoutParameter(label, labelModel.CreateDefaultParameter()); } var cityLabelStyle = label.Style as CityLabelStyle; if (cityLabelStyle != null && cityLabelStyle.InnerLabelStyle is DefaultLabelStyle) { var font = ((DefaultLabelStyle)cityLabelStyle.InnerLabelStyle).Font; ((DefaultLabelStyle)cityLabelStyle.InnerLabelStyle).Font = new Font(font.FontFamily, size); } graphControl.Graph.AdjustLabelPreferredSize(label); } } { // set as default label model parameter graphControl.Graph.NodeDefaults.Labels.LayoutParameter = labelModel.CreateDefaultParameter(); var cityLabelStyle = graphControl.Graph.NodeDefaults.Labels.Style as CityLabelStyle; if (cityLabelStyle != null && cityLabelStyle.InnerLabelStyle is DefaultLabelStyle) { var font = ((DefaultLabelStyle)cityLabelStyle.InnerLabelStyle).Font; ((DefaultLabelStyle)cityLabelStyle.InnerLabelStyle).Font = new Font(font.FontFamily, size); } } graphControl.Invalidate(); // configure and run the layout algorithm var labelingAlgorithm = new GenericLabeling { MaximumDuration = 0, OptimizationStrategy = OptimizationStrategy.Balanced, PlaceEdgeLabels = false, PlaceNodeLabels = true, ReduceLabelOverlaps = true, ProfitModel = new ExtendedLabelCandidateProfitModel(), }; var layoutExecutor = new LayoutExecutor(graphControl, graphControl.Graph, labelingAlgorithm) { Duration = TimeSpan.FromMilliseconds(500), EasedAnimation = true, AnimateViewport = false, UpdateContentRect = true }; await layoutExecutor.Start(); toolStrip.Enabled = true; inLayout = false; }