Skip to content

Commit cc46c84

Browse files
authored
Merge pull request #36 from EBjerrum/drawing_settings
Drawing settings
2 parents d0124fc + 7f5bbc6 commit cc46c84

2 files changed

Lines changed: 80 additions & 3 deletions

File tree

rdeditor/molViewWidget.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
# The Viewer Class
2323
class MolWidget(QtSvgWidgets.QSvgWidget):
24-
def __init__(self, mol=None, parent=None):
24+
def __init__(self, mol=None, parent=None, moldrawoptions: rdMolDraw2D.MolDrawOptions = None):
2525
# Also init the super class
2626
super(MolWidget, self).__init__(parent)
2727

@@ -49,6 +49,16 @@ def __init__(self, mol=None, parent=None):
4949
self._sanitize = False
5050
self._updatepropertycache = False
5151

52+
# Draw options
53+
if moldrawoptions is None:
54+
self._moldrawoptions = rdMolDraw2D.MolDraw2DSVG(300, 300).drawOptions()
55+
self._moldrawoptions.prepareMolsBeforeDrawing = True
56+
self._moldrawoptions.addStereoAnnotation = True
57+
self._moldrawoptions.unspecifiedStereoIsUnknown = False
58+
self._moldrawoptions.fixedBondLength = 25
59+
else:
60+
self._moldrawoptions = moldrawoptions
61+
5262
# Bind signales to slots for automatic actions
5363
self.molChanged.connect(self.sanitize_draw)
5464
self.selectionChanged.connect(self.draw)
@@ -76,6 +86,25 @@ def darkmode(self, value: bool):
7686
self._darkmode = bool(value)
7787
self.draw()
7888

89+
@property
90+
def moldrawoptions(self):
91+
"""Returns the current drawing options.
92+
If settings aremanipulated directly, a drawSettingsChanged signal is not emitted,
93+
consider using setDrawOption instead."""
94+
return self._moldrawoptions
95+
96+
@moldrawoptions.setter
97+
def moldrawoptions(self, value):
98+
self._moldrawoptions = value
99+
self.drawSettingsChanged.emit()
100+
101+
def getDrawOption(self, attribute):
102+
return getattr(self._moldrawoptions, attribute)
103+
104+
def setDrawOption(self, attribute, value):
105+
setattr(self._moldrawoptions, attribute, value)
106+
self.drawSettingsChanged.emit()
107+
79108
# Getter and setter for mol
80109
molChanged = QtCore.Signal(name="molChanged")
81110

@@ -309,13 +338,15 @@ def getMolSvg(self):
309338
if self._drawmol is not None:
310339
# Chiral tags on R/S
311340
# chiraltags = Chem.FindMolChiralCenters(self._drawmol)
341+
self.drawer.SetDrawOptions(self._moldrawoptions)
312342
opts = self.drawer.drawOptions()
313343
if self._darkmode:
314344
rdMolDraw2D.SetDarkMode(opts)
315345
if (not self.molecule_sanitizable) and self.unsanitizable_background_colour:
316346
opts.setBackgroundColour(self.unsanitizable_background_colour)
317-
opts.prepareMolsBeforeDrawing = False
318-
opts.addStereoAnnotation = True # Show R/S and E/Z
347+
# opts.prepareMolsBeforeDrawing = True
348+
# opts.addStereoAnnotation = True # Show R/S and E/Z
349+
# opts.unspecifiedStereoIsUnknown = True # Show wiggly bond at undefined stereo centre
319350
# for tag in chiraltags:
320351
# idx = tag[0]
321352
# opts.atomLabels[idx] = self._drawmol.GetAtomWithIdx(idx).GetSymbol() + ":" + tag[1]

rdeditor/rdEditor.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ def __init__(self, fileName=None, loglevel="WARNING"):
3333
[os.path.abspath(os.path.dirname(__file__)) + "/icon_themes/"]
3434
)
3535
self.loglevels = ["Critical", "Error", "Warning", "Info", "Debug", "Notset"]
36+
# RDKit draw options, tooltip, default value is read from molViewWidget
37+
self._drawopts_actions = [
38+
(
39+
"prepareMolsBeforeDrawing",
40+
"Prepare molecules before drawing (i.e. fix stereochemistry and annotations)",
41+
),
42+
(
43+
"addStereoAnnotation",
44+
"Add stereo annotation (R/S and E/Z)",
45+
),
46+
(
47+
"unspecifiedStereoIsUnknown",
48+
"Show wiggly bond at potential undefined chiral stereo centres "
49+
+ "and cross bonds for undefined doublebonds",
50+
),
51+
]
52+
3653
self.editor = MolEditWidget()
3754
self.chemEntityActionGroup = QtGui.QActionGroup(self, exclusive=True)
3855
self.ptable = PTable(self.chemEntityActionGroup)
@@ -133,6 +150,18 @@ def applySettings(self):
133150
self.editor.kekulize_on_cleanup = kekulize_on_cleanup
134151
self.cleanupSettingActions["kekulize_on_cleanup"].setChecked(kekulize_on_cleanup)
135152

153+
# Draw options
154+
for key, statusTip in self._drawopts_actions:
155+
viewer_value = self.editor.getDrawOption(key)
156+
settings_value = self.settings.value(f"drawoptions/{key}", viewer_value, type=bool)
157+
if settings_value != viewer_value:
158+
self.editor.setDrawOption(key, settings_value)
159+
self.drawOptionsActions[key].setChecked(settings_value)
160+
161+
if self.settings.contains("drawoptions/fixedBondLength"):
162+
fixedBondLength = self.settings.value("drawoptions/fixedBondLength", 15, type=int)
163+
self.editor.setDrawOption("fixedBondLength", fixedBondLength)
164+
136165
# Function to setup status bar, central widget, menu bar, tool bar
137166
def SetupComponents(self):
138167
self.myStatusBar = QStatusBar()
@@ -218,6 +247,9 @@ def CreateMenus(self):
218247
self.cleanupMenu = self.settingsMenu.addMenu("Cleanup")
219248
for key, action in self.cleanupSettingActions.items():
220249
self.cleanupMenu.addAction(action)
250+
self.drawOptionsMenu = self.settingsMenu.addMenu("Drawing Options")
251+
for key, statusTip in self._drawopts_actions:
252+
self.drawOptionsMenu.addAction(self.drawOptionsActions[key])
221253

222254
# Help menu
223255
self.helpMenu.addAction(self.aboutAction)
@@ -452,6 +484,13 @@ def setLogLevel(self):
452484
self.settings.setValue("loglevel", loglevel)
453485
self.settings.sync()
454486

487+
def setDrawOption(self):
488+
sender = self.sender()
489+
option = sender.objectName()
490+
self.editor.setDrawOption(option, sender.isChecked())
491+
self.settings.setValue(f"drawoptions/{option}", sender.isChecked())
492+
self.settings.sync()
493+
455494
def setTheme(self):
456495
sender = self.sender()
457496
theme_name = sender.objectName()
@@ -914,6 +953,13 @@ def CreateActions(self):
914953
)
915954
self.loglevelActionGroup.addAction(self.loglevelactions[key])
916955

956+
self.drawOptionsActions = {}
957+
for key, statusTip in self._drawopts_actions:
958+
self.drawOptionsActions[key] = QAction(
959+
key, self, statusTip=statusTip, triggered=self.setDrawOption, objectName=key, checkable=True
960+
)
961+
# self.drawOptionsActionGroup.addAction(self.drawOptionsActions[key])
962+
917963
self.openChemRxiv = QAction(
918964
QIcon.fromTheme("icons8-Exit"),
919965
"ChemRxiv Preprint",

0 commit comments

Comments
 (0)