Skip to content

Commit

Permalink
fix: hydrogen highlight reset
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Jul 25, 2024
1 parent 8a8185f commit b64aca6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 76 deletions.
2 changes: 1 addition & 1 deletion openchemlib
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@
import com.actelion.research.gui.hidpi.HiDPIHelper;
import com.actelion.research.util.ColorHelper;

import java.awt.*;
import java.awt.geom.Point2D;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.TreeMap;

public class GenericEditorArea implements GenericEventListener {
Expand Down Expand Up @@ -148,11 +146,13 @@ public class GenericEditorArea implements GenericEventListener {

private static IReactionMapper sMapper;
private static String[][] sReactionQueryTemplates;
private final int mMaxAVBL;
private int mMode, mChainAtoms, mCurrentTool, mCustomAtomicNo, mCustomAtomMass, mCustomAtomValence, mCustomAtomRadical,
mCurrentHiliteAtom, mCurrentHiliteBond, mPendingRequest, mEventsScheduled, mFirstAtomKey,
mCurrentCursor, mReactantCount, mUpdateMode, mDisplayMode, mAtom1, mAtom2, mMaxAVBL, mAllowedPseudoAtoms;
mCurrentCursor, mReactantCount, mUpdateMode, mDisplayMode, mAtom1, mAtom2, mAllowedPseudoAtoms;
private int[] mChainAtom, mFragmentNo, mHiliteBondSet;
private double mX1, mY1, mX2, mY2, mWidth, mHeight, mUIScaling, mTextSizeFactor;
private final double mUIScaling;
private double mX1, mY1, mX2, mY2, mWidth, mHeight, mTextSizeFactor;
private double[] mX, mY, mChainAtomX, mChainAtomY;
private boolean mAltIsDown, mShiftIsDown, mMouseIsDown, mIsAddingToSelection, mAtomColorSupported, mAllowQueryFeatures,
mAllowFragmentChangeOnPasteOrDrop;
Expand All @@ -169,9 +169,9 @@ public class GenericEditorArea implements GenericEventListener {
private GenericPolygon mLassoRegion;
private ArrayList<GenericEventListener> mListeners;
private IClipboardHandler mClipboardHandler;
private StringBuilder mAtomKeyStrokeBuffer;
private GenericUIHelper mUIHelper;
private GenericCanvas mCanvas;
private final StringBuilder mAtomKeyStrokeBuffer;
private final GenericUIHelper mUIHelper;
private final GenericCanvas mCanvas;

/**
* @param mol an empty or valid stereo molecule
Expand Down Expand Up @@ -1010,8 +1010,8 @@ private void eventHappened(GenericMouseEvent e) {
s2 = -s2;
}
for (int i = 0; i<mChainAtoms; i++) {
mChainAtomX[i] = mX1 + ((i + 1) / 2) * s1;
mChainAtomY[i] = mY1 + ((i + 1) / 2) * (s0 + s2);
mChainAtomX[i] = mX1 + 0.5 * (i + 1) * s1;
mChainAtomY[i] = mY1 + 0.5 * (i + 1) * (s0 + s2);
if ((i & 1) == 0) {
mChainAtomY[i] += s0;
}
Expand Down Expand Up @@ -1235,14 +1235,14 @@ else if (e.getKey() == 'v') {
} else {
boolean bondChanged =
(ch == '0') ? changeHighlightedBond(Molecule.cBondTypeMetalLigand)
: (ch == '1') ? changeHighlightedBond(Molecule.cBondTypeSingle)
: (ch == '2') ? changeHighlightedBond(Molecule.cBondTypeDouble)
: (ch == '3') ? changeHighlightedBond(Molecule.cBondTypeTriple)
: (ch == 'u') ? changeHighlightedBond(Molecule.cBondTypeUp)
: (ch == 'd') ? changeHighlightedBond(Molecule.cBondTypeDown)
: (ch == 'c') ? changeHighlightedBond(Molecule.cBondTypeCross)
: (ch == 'm') ? changeHighlightedBond(Molecule.cBondTypeMetalLigand)
: false;
: (ch == '1') ? changeHighlightedBond(Molecule.cBondTypeSingle)
: (ch == '2') ? changeHighlightedBond(Molecule.cBondTypeDouble)
: (ch == '3') ? changeHighlightedBond(Molecule.cBondTypeTriple)
: (ch == 'u') ? changeHighlightedBond(Molecule.cBondTypeUp)
: (ch == 'd') ? changeHighlightedBond(Molecule.cBondTypeDown)
: (ch == 'c') ? changeHighlightedBond(Molecule.cBondTypeCross)
: (ch == 'm') ? changeHighlightedBond(Molecule.cBondTypeMetalLigand)
: false;
if (bondChanged)
updateAndFireEvent(UPDATE_REDRAW);
}
Expand Down Expand Up @@ -1324,7 +1324,7 @@ else if (e.getKey() == 'v') {
} else if (ch == '\n' || ch == '\r') {
expandAtomKeyStrokes(mAtomKeyStrokeBuffer.toString());
}
} else if (mCurrentHiliteAtom == -1 && mCurrentHiliteBond == -1) {
} else {
if ((mMode & (MODE_REACTION | MODE_MARKUSH_STRUCTURE | MODE_MULTIPLE_FRAGMENTS)) == 0) {
int ch = e.getKey();
if (ch == 'h')
Expand Down Expand Up @@ -1810,59 +1810,37 @@ private void mousePressedButton1(GenericMouseEvent gme) {
updateAndFireEvent(UPDATE_CHECK_COORDS);
break;
case GenericEditorToolbar.cToolAtomH:
storeState();
if (mMol.addOrChangeAtom(mX1, mY1, 1, 0, -1, 0, null))
updateAndFireEvent(UPDATE_CHECK_COORDS);
changeAtomicNo(1);
break;
case GenericEditorToolbar.cToolAtomC:
storeState();
if (mMol.addOrChangeAtom(mX1, mY1, 6, 0, -1, 0, null))
updateAndFireEvent(UPDATE_CHECK_COORDS);
changeAtomicNo(6);
break;
case GenericEditorToolbar.cToolAtomN:
storeState();
if (mMol.addOrChangeAtom(mX1, mY1, 7, 0, -1, 0, null))
updateAndFireEvent(UPDATE_CHECK_COORDS);
changeAtomicNo(7);
break;
case GenericEditorToolbar.cToolAtomO:
storeState();
if (mMol.addOrChangeAtom(mX1, mY1, 8, 0, -1, 0, null))
updateAndFireEvent(UPDATE_CHECK_COORDS);
changeAtomicNo(8);
break;
case GenericEditorToolbar.cToolAtomSi:
storeState();
if (mMol.addOrChangeAtom(mX1, mY1, 14, 0, -1, 0, null))
updateAndFireEvent(UPDATE_CHECK_COORDS);
changeAtomicNo(14);
break;
case GenericEditorToolbar.cToolAtomP:
storeState();
if (mMol.addOrChangeAtom(mX1, mY1, 15, 0, -1, 0, null))
updateAndFireEvent(UPDATE_CHECK_COORDS);
changeAtomicNo(15);
break;
case GenericEditorToolbar.cToolAtomS:
storeState();
if (mMol.addOrChangeAtom(mX1, mY1, 16, 0, -1, 0, null))
updateAndFireEvent(UPDATE_CHECK_COORDS);
changeAtomicNo(16);
break;
case GenericEditorToolbar.cToolAtomF:
storeState();
if (mMol.addOrChangeAtom(mX1, mY1, 9, 0, -1, 0, null))
updateAndFireEvent(UPDATE_CHECK_COORDS);
changeAtomicNo(9);
break;
case GenericEditorToolbar.cToolAtomCl:
storeState();
if (mMol.addOrChangeAtom(mX1, mY1, 17, 0, -1, 0, null))
updateAndFireEvent(UPDATE_CHECK_COORDS);
changeAtomicNo(17);
break;
case GenericEditorToolbar.cToolAtomBr:
storeState();
if (mMol.addOrChangeAtom(mX1, mY1, 35, 0, -1, 0, null))
updateAndFireEvent(UPDATE_CHECK_COORDS);
changeAtomicNo(35);
break;
case GenericEditorToolbar.cToolAtomI:
storeState();
if (mMol.addOrChangeAtom(mX1, mY1, 53, 0, -1, 0, null))
updateAndFireEvent(UPDATE_CHECK_COORDS);
changeAtomicNo(53);
break;
case GenericEditorToolbar.cToolCustomAtom:
if (gme.isControlDown()) {
Expand All @@ -1872,8 +1850,10 @@ private void mousePressedButton1(GenericMouseEvent gme) {
}
} else {
storeState();
if (mMol.addOrChangeAtom(mX1, mY1, mCustomAtomicNo, mCustomAtomMass, mCustomAtomValence, mCustomAtomRadical, mCustomAtomLabel))
if (mMol.addOrChangeAtom(mX1, mY1, mCustomAtomicNo, mCustomAtomMass, mCustomAtomValence, mCustomAtomRadical, mCustomAtomLabel)) {
mCurrentHiliteAtom = -1;
updateAndFireEvent(UPDATE_CHECK_COORDS);
}
}
break;
case GenericEditorToolbar.cToolMapper:
Expand All @@ -1900,6 +1880,14 @@ private void mousePressedButton1(GenericMouseEvent gme) {
}
}

private void changeAtomicNo(int atomicNo) {
storeState();
if (mMol.addOrChangeAtom(mX1, mY1, atomicNo, 0, -1, 0, null)) {
mCurrentHiliteAtom = -1;
updateAndFireEvent(UPDATE_CHECK_COORDS);
}
}

private void mouseReleasedButton1() {
int pendingRequest = mPendingRequest;
mPendingRequest = cRequestNone;
Expand Down Expand Up @@ -2569,7 +2557,7 @@ private int getAtomKeyStrokeValidity(String s){
mAtomKeyStrokeSuggestion = NamedSubstituents.identify(s);
if (mAtomKeyStrokeSuggestion == null)
return isValidAtomKeyStrokeStart(s) ? KEY_IS_VALID_START : KEY_IS_INVALID;
if (mAtomKeyStrokeSuggestion.length() == 0)
if (mAtomKeyStrokeSuggestion.isEmpty())
return KEY_IS_VALID_START;
else
return KEY_IS_SUBSTITUENT;
Expand Down Expand Up @@ -2600,7 +2588,7 @@ private void expandAtomKeyStrokes(String keyStrokes){
}
}

if (mAtomKeyStrokeSuggestion != null && mAtomKeyStrokeSuggestion.length() != 0)
if (mAtomKeyStrokeSuggestion != null && !mAtomKeyStrokeSuggestion.isEmpty())
keyStrokes = mAtomKeyStrokeSuggestion;

StereoMolecule substituent = NamedSubstituents.getSubstituent(keyStrokes);
Expand Down Expand Up @@ -2678,8 +2666,8 @@ private void editTextObject(TextDrawingObject object){

private boolean shareSameReactionSide(int atom1, int atom2){
ReactionArrow arrow = (ReactionArrow)mDrawingObjectList.get(0);
return !(arrow.isOnProductSide(mMol.getAtomX(atom1), mMol.getAtomY(atom1))
^ arrow.isOnProductSide(mMol.getAtomX(atom2), mMol.getAtomY(atom2)));
return arrow.isOnProductSide(mMol.getAtomX(atom1), mMol.getAtomY(atom1))
== arrow.isOnProductSide(mMol.getAtomX(atom2), mMol.getAtomY(atom2));
}

protected void restoreState() {
Expand Down Expand Up @@ -3256,7 +3244,7 @@ private void sortFragmentsByPosition(int[] fragmentNo, int fragments){
fragmentDescriptor[fragment][0] = fragment;
}

Point[] fragmentCOG = calculateFragmentCenterOfGravity(fragmentNo, fragments);
Point2D.Double[] fragmentCOG = calculateFragmentCenterOfGravity(fragmentNo, fragments);

if ((mMode & MODE_REACTION) != 0) {
mReactantCount = 0;
Expand All @@ -3278,41 +3266,34 @@ private void sortFragmentsByPosition(int[] fragmentNo, int fragments){
}
}

final Point[] cog = fragmentCOG;
Arrays.sort(fragmentDescriptor, new Comparator<int[]>() {
public int compare(int[] fragmentDescriptor1, int[] fragmentDescriptor2) {
if ((mMode & (MODE_REACTION | MODE_MARKUSH_STRUCTURE)) != 0) {
if (fragmentDescriptor1[1] != fragmentDescriptor2[1]) {
return (fragmentDescriptor1[1] == 0) ? -1 : 1;
}
final Point2D.Double[] cog = fragmentCOG;
Arrays.sort(fragmentDescriptor, (fragmentDescriptor1, fragmentDescriptor2) -> {
if ((mMode & (MODE_REACTION | MODE_MARKUSH_STRUCTURE)) != 0) {
if (fragmentDescriptor1[1] != fragmentDescriptor2[1]) {
return (fragmentDescriptor1[1] == 0) ? -1 : 1;
}

return (cog[fragmentDescriptor1[0]].x
+ cog[fragmentDescriptor1[0]].y
<cog[fragmentDescriptor2[0]].x
+ cog[fragmentDescriptor2[0]].y) ? -1 : 1;
}

return Double.compare(cog[fragmentDescriptor1[0]].x + cog[fragmentDescriptor1[0]].y,
cog[fragmentDescriptor2[0]].x + cog[fragmentDescriptor2[0]].y);
});

int[] newFragmentIndex = new int[fragments];
Point[] centerOfGravity = new Point[fragments];
for (int fragment = 0; fragment<fragments; fragment++) {
int oldIndex = ((int[])fragmentDescriptor[fragment])[0];
newFragmentIndex[oldIndex] = fragment;
centerOfGravity[fragment] = fragmentCOG[oldIndex];
}

fragmentCOG = centerOfGravity;
for (int atom1 = 0; atom1<mMol.getAllAtoms(); atom1++) {
fragmentNo[atom1] = newFragmentIndex[fragmentNo[atom1]];
}
}

private Point[] calculateFragmentCenterOfGravity(int[] fragmentNo, int fragments){
Point[] fragmentCOG = new Point[fragments];
private Point2D.Double[] calculateFragmentCenterOfGravity(int[] fragmentNo, int fragments){
Point2D.Double[] fragmentCOG = new Point2D.Double[fragments];
int[] fragmentAtoms = new int[fragments];
for (int fragment = 0; fragment<fragments; fragment++) {
fragmentCOG[fragment] = new Point(0, 0);
fragmentCOG[fragment] = new Point2D.Double();
}
for (int atom = 0; atom<mMol.getAllAtoms(); atom++) {
fragmentCOG[fragmentNo[atom]].x += mMol.getAtomX(atom);
Expand Down

0 comments on commit b64aca6

Please sign in to comment.