From 19054e4477eaca06e23b93558a1ddc6ef25f5697 Mon Sep 17 00:00:00 2001 From: Dominik Kopczynski Date: Wed, 25 Oct 2023 15:52:15 +0200 Subject: [PATCH] working on structure editor --- .../StructureEditor/LipidStructure.cs | 59 +++++-- .../StructureEditor.Designer.cs | 37 +++- .../StructureEditor/StructureEditor.cs | 164 ++++++++++++++---- 3 files changed, 202 insertions(+), 58 deletions(-) diff --git a/LipidCreator/StructureEditor/LipidStructure.cs b/LipidCreator/StructureEditor/LipidStructure.cs index 5ab248d..5862f40 100644 --- a/LipidCreator/StructureEditor/LipidStructure.cs +++ b/LipidCreator/StructureEditor/LipidStructure.cs @@ -193,7 +193,7 @@ public class LipidStructure public Dictionary nodeConnectionsHiddenCount = new Dictionary(); public HashSet specialAtoms = new HashSet(){"N", "O", "P", "S"}; public Dictionary freeElectrons = new Dictionary(){{"C", 4}, {"N", 3}, {"O", 2}, {"P", 3}, {"S", 2}}; - + public int projectionIds = 1; @@ -427,34 +427,59 @@ public LipidStructure(string file_name, Form form) - public void addPositiveFragment(string fragmentName) + public void addFragment(string fragmentName, bool isPositive) { - int newProjectionId = positiveFragments.Count + negativeFragments.Count + 1; - positiveFragments.Add(fragmentName, newProjectionId); + int newProjectionId = projectionIds++; + if (isPositive) positiveFragments.Add(fragmentName, newProjectionId); + else negativeFragments.Add(fragmentName, newProjectionId); + foreach (var node in nodes) { - node.nodeProjections.Add(newProjectionId, new NodeProjection()); - foreach (var decorator in node.decorators) decorator.nodeProjections.Add(newProjectionId, new NodeProjection()); + node.nodeProjections.Add(newProjectionId, new NodeProjection(node.nodeProjections[currentProjection])); + foreach (var decorator in node.decorators) decorator.nodeProjections.Add(newProjectionId, new NodeProjection(decorator.nodeProjections[currentProjection])); + } + foreach (var bond in bonds) bond.bondProjections.Add(newProjectionId, bond.bondProjections[currentProjection]); + foreach (var bond in additionalBonds) + { + if (bond.bondProjections.ContainsKey(currentProjection)) bond.bondProjections.Add(newProjectionId, bond.bondProjections[currentProjection]); } - foreach (var bond in bonds) bond.bondProjections.Add(newProjectionId, bond.isDoubleBond); countNodeConnections(); } - - public void addNegativeFragment(string fragmentName) + public void removeFragment(string fragmentName, bool isPositive) { - int newProjectionId = positiveFragments.Count + negativeFragments.Count + 1; - negativeFragments.Add(fragmentName, newProjectionId); + int projectionId = -1; + if (isPositive) + { + if (!positiveFragments.ContainsKey(fragmentName)) return; + projectionId = positiveFragments[fragmentName]; + positiveFragments.Remove(fragmentName); + } + else + { + if (!negativeFragments.ContainsKey(fragmentName)) return; + projectionId = negativeFragments[fragmentName]; + negativeFragments.Remove(fragmentName); + } + foreach (var node in nodes) { - node.nodeProjections.Add(newProjectionId, new NodeProjection()); - foreach (var decorator in node.decorators) decorator.nodeProjections.Add(newProjectionId, new NodeProjection()); + node.nodeProjections.Remove(projectionId); + foreach (var decorator in node.decorators) decorator.nodeProjections.Remove(projectionId); + } + foreach (var bond in bonds) bond.bondProjections.Remove(projectionId); + foreach (var bond in additionalBonds) + { + if (bond.bondProjections.ContainsKey(projectionId)) bond.bondProjections.Remove(projectionId); + } + if (currentProjection == projectionId) + { + currentProjection = 0; + countNodeConnections(); } - foreach (var bond in bonds) bond.bondProjections.Add(newProjectionId, bond.isDoubleBond); - countNodeConnections(); } @@ -543,8 +568,6 @@ public void addBond(StructureNode start, StructureNode end) } - - public Graphics setClipping(Graphics g, Form form) { // set up all clipping @@ -623,7 +646,7 @@ public void countNodeConnections() public void changeFragment(string fragmentName = "", bool charge = false) { currentProjection = (fragmentName.Length == 0) ? 0 : ((charge ? positiveFragments : negativeFragments)[fragmentName]); - countNodeConnections(); + computeBonds(); } diff --git a/LipidCreator/StructureEditor/StructureEditor.Designer.cs b/LipidCreator/StructureEditor/StructureEditor.Designer.cs index 656cd02..b89f946 100644 --- a/LipidCreator/StructureEditor/StructureEditor.Designer.cs +++ b/LipidCreator/StructureEditor/StructureEditor.Designer.cs @@ -99,6 +99,11 @@ partial class LipidCreatorStructureEditor public ComboBox adductComboBox = new ComboBox(); + public Button addPositiveFragmentButton = new Button(); + public Button removePositiveFragmentButton = new Button(); + public Button addNegativeFragmentButton = new Button(); + public Button removeNegativeFragmentButton = new Button(); + private void InitializeComponent() { this.Text = "LipidCreator Structure Editor"; @@ -153,17 +158,45 @@ private void InitializeComponent() positiveFragmentsListBox.Width = 130; positiveFragmentsListBox.Height = 300; positiveFragmentsListBox.Location = new Point(10, 210); - positiveFragmentsListBox.Click += positiveFragmentClicked; + positiveFragmentsListBox.KeyUp += fragmentKeyPressed; + positiveFragmentsListBox.SelectedIndexChanged += fragmentClicked; positiveFragmentsListBox.DoubleClick += positiveFragmentDoubleClicked; this.Controls.Add(negativeFragmentsListBox); negativeFragmentsListBox.Width = 130; negativeFragmentsListBox.Height = 300; negativeFragmentsListBox.Location = new Point(150, 210); - negativeFragmentsListBox.Click += negativeFragmentClicked; + negativeFragmentsListBox.KeyUp += fragmentKeyPressed; + negativeFragmentsListBox.SelectedIndexChanged += fragmentClicked; negativeFragmentsListBox.DoubleClick += negativeFragmentDoubleClicked; + this.Controls.Add(addPositiveFragmentButton); + addPositiveFragmentButton.Location = new Point(90, 505); + addPositiveFragmentButton.Size = new Size(25, 25); + addPositiveFragmentButton.Text = "+"; + addPositiveFragmentButton.Click += addPositiveFragment; + + this.Controls.Add(removePositiveFragmentButton); + removePositiveFragmentButton.Location = new Point(115, 505); + removePositiveFragmentButton.Size = new Size(25, 25); + removePositiveFragmentButton.Text = "-"; + removePositiveFragmentButton.Click += removePositiveFragment; + + this.Controls.Add(addNegativeFragmentButton); + addNegativeFragmentButton.Location = new Point(230, 505); + addNegativeFragmentButton.Size = new Size(25, 25); + addNegativeFragmentButton.Text = "+"; + addNegativeFragmentButton.Click += addNegativeFragment; + + this.Controls.Add(removeNegativeFragmentButton); + removeNegativeFragmentButton.Location = new Point(255, 505); + removeNegativeFragmentButton.Size = new Size(25, 25); + removeNegativeFragmentButton.Text = "-"; + removeNegativeFragmentButton.Click += removeNegativeFragment; + + + this.Controls.Add(bb1Carbon); bb1Carbon.Location = new Point(200, 10); diff --git a/LipidCreator/StructureEditor/StructureEditor.cs b/LipidCreator/StructureEditor/StructureEditor.cs index c5a5b10..f7451d9 100644 --- a/LipidCreator/StructureEditor/StructureEditor.cs +++ b/LipidCreator/StructureEditor/StructureEditor.cs @@ -22,13 +22,13 @@ public partial class LipidCreatorStructureEditor : Form public StructureNode drawStart = null; public Action action = Action.Idle; public StructureNode moveNode = null; - public int selectedIndexPositive = -1; - public int selectedIndexNegative = -1; + public int[] selectedIndexes = new int[]{-1, -1}; public Point startSelect; public PointF previousMousePosition; public List moveNodes = new List(); public static double ELECTRON_REST_MASS = 0.00054857990946; - + public const string FRAGMENT_LABEL = "fragment"; + public bool semaphore = false; public LipidCreatorStructureEditor() @@ -41,13 +41,13 @@ public LipidCreatorStructureEditor() lipidStructure = new LipidStructure(file, this); positiveFragmentsListBox.Items.Add("HG 164"); - lipidStructure.addPositiveFragment("HG 164"); + lipidStructure.addFragment("HG 164", true); positiveFragmentsListBox.Items.Add("HG 181"); - lipidStructure.addPositiveFragment("HG 181"); + lipidStructure.addFragment("HG 181", true); negativeFragmentsListBox.Items.Add("HG -OH"); - lipidStructure.addNegativeFragment("HG -OH"); + lipidStructure.addFragment("HG -OH", false); computeFragmentMass(null, null); } @@ -517,7 +517,7 @@ private void positiveFragmentDoubleClicked(Object sender, EventArgs e) string currentFragmentName = (string)positiveFragmentsListBox.Items[selectedIndex]; string newFragmentName = InputBox.Show("Please write a new fragment name:", "New fragment name", currentFragmentName); - if (newFragmentName.Length == 0 || newFragmentName.Equals(currentFragmentName)) return; + if (newFragmentName.Length == 0 || newFragmentName.Equals(currentFragmentName) || lipidStructure.positiveFragments.ContainsKey(newFragmentName)) return; lipidStructure.positiveFragments.Add(newFragmentName, lipidStructure.positiveFragments[currentFragmentName]); lipidStructure.positiveFragments.Remove(currentFragmentName); @@ -525,26 +525,7 @@ private void positiveFragmentDoubleClicked(Object sender, EventArgs e) } - private void positiveFragmentClicked(Object sender, EventArgs e) - { - moveNodes.Clear(); - int selectedIndex = positiveFragmentsListBox.SelectedIndex; - if (selectedIndex == selectedIndexPositive) - { - positiveFragmentsListBox.SelectedIndex = -1; - selectedIndexPositive = -1; - lipidStructure.changeFragment(); - } - else - { - selectedIndexPositive = selectedIndex; - lipidStructure.changeFragment((string)positiveFragmentsListBox.Items[selectedIndex], true); - } - negativeFragmentsListBox.SelectedIndex = -1; - selectedIndexNegative = -1; - lipidStructure.computeBonds(); - updateStructure(); - } + private void negativeFragmentDoubleClicked(Object sender, EventArgs e) @@ -554,7 +535,7 @@ private void negativeFragmentDoubleClicked(Object sender, EventArgs e) string currentFragmentName = (string)negativeFragmentsListBox.Items[selectedIndex]; string newFragmentName = InputBox.Show("Please write a new fragment name:", "New fragment name", currentFragmentName); - if (newFragmentName.Length == 0 || newFragmentName.Equals(currentFragmentName)) return; + if (newFragmentName.Length == 0 || newFragmentName.Equals(currentFragmentName) || lipidStructure.negativeFragments.ContainsKey(newFragmentName)) return; lipidStructure.negativeFragments.Add(newFragmentName, lipidStructure.negativeFragments[currentFragmentName]); lipidStructure.negativeFragments.Remove(currentFragmentName); @@ -562,26 +543,41 @@ private void negativeFragmentDoubleClicked(Object sender, EventArgs e) } - private void negativeFragmentClicked(Object sender, EventArgs e) + + + + + private void fragmentClicked(Object sender, EventArgs e) { + if (semaphore) return; + semaphore = true; + + ListBox fragmentListBox = (ListBox)sender; + + int indexIndex = (fragmentListBox == positiveFragmentsListBox) ? 1 : 0; + + if (indexIndex == 1) negativeFragmentsListBox.SelectedIndex = -1; + else positiveFragmentsListBox.SelectedIndex = -1; + moveNodes.Clear(); - int selectedIndex = negativeFragmentsListBox.SelectedIndex; - if (selectedIndex == selectedIndexNegative) + int selectedIndex = fragmentListBox.SelectedIndex; + + if (selectedIndex == selectedIndexes[indexIndex]) { - negativeFragmentsListBox.SelectedIndex = -1; - selectedIndexNegative = -1; + fragmentListBox.SelectedIndex = -1; + selectedIndexes[indexIndex] = -1; lipidStructure.changeFragment(); } else { - selectedIndexNegative = selectedIndex; - lipidStructure.changeFragment((string)negativeFragmentsListBox.Items[selectedIndex], false); - + selectedIndexes[indexIndex] = selectedIndex; + lipidStructure.changeFragment((string)fragmentListBox.Items[selectedIndex], indexIndex == 1); } - positiveFragmentsListBox.SelectedIndex = -1; - selectedIndexPositive = -1; + selectedIndexes[1 - indexIndex] = -1; lipidStructure.computeBonds(); updateStructure(); + + semaphore = false; } @@ -595,6 +591,98 @@ public void updateStructure() + private void addPositiveFragment(Object sender, EventArgs e) + { + if (!lipidStructure.positiveFragments.ContainsKey(FRAGMENT_LABEL)) + { + positiveFragmentsListBox.Items.Add(FRAGMENT_LABEL); + lipidStructure.addFragment(FRAGMENT_LABEL, true); + } + else + { + int i = 2; + while (true) + { + string fragmentName = FRAGMENT_LABEL + "." + (i++); + if (lipidStructure.positiveFragments.ContainsKey(fragmentName)) continue; + positiveFragmentsListBox.Items.Add(fragmentName); + lipidStructure.addFragment(fragmentName, true); + break; + } + } + } + + + + + public void fragmentKeyPressed(object sender, KeyEventArgs e) + { + if (e.KeyCode == Keys.Delete) removeFragment(sender, null); + } + + + + + private void addNegativeFragment(Object sender, EventArgs e) + { + if (!lipidStructure.negativeFragments.ContainsKey(FRAGMENT_LABEL)) + { + negativeFragmentsListBox.Items.Add(FRAGMENT_LABEL); + lipidStructure.addFragment(FRAGMENT_LABEL, false); + } + else + { + int i = 2; + while (true) + { + string fragmentName = FRAGMENT_LABEL + "." + (i++); + if (lipidStructure.negativeFragments.ContainsKey(fragmentName)) continue; + negativeFragmentsListBox.Items.Add(fragmentName); + lipidStructure.addFragment(fragmentName, false); + break; + } + } + } + + + + + + + public void removePositiveFragment(Object sender, EventArgs e) + { + removeFragment(positiveFragmentsListBox, e); + } + + + public void removeNegativeFragment(Object sender, EventArgs e) + { + removeFragment(negativeFragmentsListBox, e); + } + + + private void removeFragment(Object sender, EventArgs e) + { + ListBox fragmentListBox = (ListBox)sender; + + semaphore = true; + int selectedIndex = fragmentListBox.SelectedIndex; + if (selectedIndex == -1) return; + lipidStructure.removeFragment((string)fragmentListBox.Items[selectedIndex], false); + fragmentListBox.Items.RemoveAt(selectedIndex); + + selectedIndex = Math.Min(selectedIndex, fragmentListBox.Items.Count - 1); + fragmentListBox.SelectedIndex = selectedIndex; + selectedIndexes[0] = -1; + selectedIndexes[1] = -1; + semaphore = false; + fragmentClicked(sender, null); + } + + + + + private void computeFragmentMass(Object sender, EventArgs e) {