private void ProcessUnitNameBox_LostFocus(object sender, RoutedEventArgs e) { // We should only be receiving this when the text box is visible if (Visibility.Visible != ProcessUnitNameBox.Visibility) { return; } ProcessUnitNameText.Visibility = System.Windows.Visibility.Visible; ProcessUnitNameBox.Visibility = System.Windows.Visibility.Collapsed; // Here's where we finalize the name change, so we need an undo if the name changed if (!m_pu.Label.Equals(m_labelOnEditStart)) { if (null == m_labelOnEditStart) { Core.App.Log(Core.App.LogItemType.Error, "Labeled process unit tried to create undo but had a null string for its original label"); } else { // Set the new text m_ignoreUnitPropertyChanges = true; m_pu.Label = ProcessUnitNameBox.Text; ProcessUnitNameText.Text = ProcessUnitNameBox.Text; m_ignoreUnitPropertyChanges = false; // Add the undo string undoText = "Undo renaming process unit from " + m_labelOnEditStart + " to " + m_pu.Label; m_canvas.GetWorkspace().AddUndo(new UndoRedoCollection(undoText, new PFD.Undos.SetProcessUnitLabel(m_pu, m_labelOnEditStart))); } } }
/// <summary> /// Deletes this sticky note from the workspace and adds an undo to bring it back. /// </summary> public void DeleteWithUndo(DrawingCanvas canvas) { // Get a reference to the workspace Workspace ws = canvas.GetWorkspace(); // Start by unsubscribing from events if (null != m_commentParent) { if (m_commentParent is ProcessUnitControl) { (m_commentParent as ProcessUnitControl).ProcessUnit.PropertyChanged -= ProcessUnitParentPropertyChanged; } else { (m_commentParent as PFD.Streams.StreamControl).Stream.PropertyChanged -= StreamParentPropertyChanged; } } // Get a reference to the relevant comment collection IList <StickyNote> comments; if (null == m_commentParent) { comments = ws.StickyNotes; } else if (m_commentParent is ProcessUnitControl) { comments = (m_commentParent as ProcessUnitControl).ProcessUnit.Comments; } else { comments = (m_commentParent as ChemProV.PFD.Streams.StreamControl).Stream.Comments; } // Find the index of this comment in the parent collection int commentIndex = -1; for (int i = 0; i < comments.Count; i++) { if (object.ReferenceEquals(m_note, comments[i])) { commentIndex = i; break; } } // This really should never occur, but if we didn't find the comment in the collection // then this implies that this control shouldn't be on the canvas anyway, so remove it. if (-1 == commentIndex) { canvas.RemoveChild(this); canvas.RemoveChild(m_lineToParent); return; } // Add the undo first ws.AddUndo(new UndoRedoCollection( "Undo deleting comment", new InsertComment(comments, m_note, commentIndex))); // Remove the comment from the collection. Event handlers will update the UI and remove // this control (and the line to the parent control) from the drawing canvas. comments.RemoveAt(commentIndex); }
public static MathCore.Vector ComputeNewCommentNoteLocation(DrawingCanvas canvas, object parentControl, double controlWidth = 100.0, double controlHeight = 100.0) { // First resolve the "center point" of the parent object. Also get a reference to the collection // of comments. Point location; IList <StickyNote> comments; ProcessUnitControl lpu = parentControl as ProcessUnitControl; PFD.Streams.StreamControl stream = parentControl as PFD.Streams.StreamControl; if (null != lpu) { location = lpu.Location; comments = lpu.ProcessUnit.Comments; } else if (null != stream) { location = stream.StreamLineMidpoint; comments = stream.Stream.Comments; } else { throw new ArgumentException( "Parent control for a comment sticky note must be a stream or process unit " + "control.\n Method: ComputeNewCommentNoteLocation"); } // Get a reference to the workspace. We will look at other sticky notes in this workspace to // try to avoid direct overlap. Workspace ws = canvas.GetWorkspace(); MathCore.Vector loc; int attempts = 0; while (true) { // Compute a location double radius = 150.0; double angle = (double)(comments.Count % 6) * 60.0 / 180.0 * Math.PI; loc = new MathCore.Vector( location.X + radius * Math.Cos(angle), location.Y + radius * Math.Sin(angle)); // Make sure this location wouldn't make the control go off the canvas if (loc.X - (controlWidth / 2.0) < 0.0 || loc.Y - (controlHeight / 2.0) < 0.0) { attempts++; } else if ((null != stream && stream.Stream.ContainsCommentWithLocation(loc.X, loc.Y)) || (null != lpu && lpu.ProcessUnit.ContainsCommentWithLocation(loc.X, loc.Y))) { attempts++; } else { // This means the location is ok and we can return it return(loc); } // Try cascading if radial position failed if (attempts > 6) { // Reset attempts because we're about to try another method of positioning attempts = 0; double offset = 10.0; while (true) { loc.X = location.X + radius + offset; loc.Y = location.Y + offset; // Make sure this location wouldn't make the control go off the canvas if (loc.X - (controlWidth / 2.0) < 0.0 || loc.Y - (controlHeight / 2.0) < 0.0) { attempts++; } else if ((null != stream && stream.Stream.ContainsCommentWithLocation(loc.X, loc.Y)) || (null != lpu && lpu.ProcessUnit.ContainsCommentWithLocation(loc.X, loc.Y))) { attempts++; } else { // This means the location is ok and we can return it return(loc); } attempts++; if (attempts > 50) { // Just give up and choose an arbitrary position return(new MathCore.Vector(location.X + radius, location.Y)); } // Increase the offset for the next attempt offset += 10.0; } } } }