Skip to content

Commit

Permalink
working on structure editor
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-kopczynski committed Oct 25, 2023
1 parent d277179 commit 19054e4
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 58 deletions.
59 changes: 41 additions & 18 deletions LipidCreator/StructureEditor/LipidStructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public class LipidStructure
public Dictionary<int, int> nodeConnectionsHiddenCount = new Dictionary<int, int>();
public HashSet<string> specialAtoms = new HashSet<string>(){"N", "O", "P", "S"};
public Dictionary<string, int> freeElectrons = new Dictionary<string, int>(){{"C", 4}, {"N", 3}, {"O", 2}, {"P", 3}, {"S", 2}};
public int projectionIds = 1;



Expand Down Expand Up @@ -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();
}


Expand Down Expand Up @@ -543,8 +568,6 @@ public void addBond(StructureNode start, StructureNode end)
}




public Graphics setClipping(Graphics g, Form form)
{
// set up all clipping
Expand Down Expand Up @@ -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();
}


Expand Down
37 changes: 35 additions & 2 deletions LipidCreator/StructureEditor/StructureEditor.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

164 changes: 126 additions & 38 deletions LipidCreator/StructureEditor/StructureEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StructureNode> moveNodes = new List<StructureNode>();
public static double ELECTRON_REST_MASS = 0.00054857990946;

public const string FRAGMENT_LABEL = "fragment";
public bool semaphore = false;


public LipidCreatorStructureEditor()
Expand All @@ -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);
}
Expand Down Expand Up @@ -517,34 +517,15 @@ 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);
positiveFragmentsListBox.Items[selectedIndex] = newFragmentName;
}


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)
Expand All @@ -554,34 +535,49 @@ 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);
negativeFragmentsListBox.Items[selectedIndex] = newFragmentName;
}


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;
}


Expand All @@ -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)
{
Expand Down

0 comments on commit 19054e4

Please sign in to comment.