From 328ebaf403b701106b18df3bdcb1b5bfa8539a24 Mon Sep 17 00:00:00 2001 From: yaosioc Date: Mon, 15 Dec 2025 21:55:43 +0800 Subject: [PATCH] make canvas size responsive to window resizing This PR addresses issue #38 by making the canvas size dynamically adjustable based on the main window dimensions, rather than using a fixed size. --- rdeditor/molViewWidget.py | 13 ++++++++++++- rdeditor/rdEditor.py | 7 ++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/rdeditor/molViewWidget.py b/rdeditor/molViewWidget.py index 651b447..29e77d0 100644 --- a/rdeditor/molViewWidget.py +++ b/rdeditor/molViewWidget.py @@ -66,7 +66,9 @@ def __init__(self, mol=None, parent=None, moldrawoptions: rdMolDraw2D.MolDrawOpt self.sanitizeSignal.connect(self.changeSanitizeStatus) # Initialize class with the mol passed + self.scale = 1 self.mol = mol + self.setMinimumSize(300, 300) ##Properties and their wrappers @property @@ -241,6 +243,14 @@ def changeSanitizeStatus(self, value): else: self.molecule_sanitizable = False + def resizeEvent(self, event): + super().resizeEvent(event) + self.draw() + + def setScale(self, scale): + self.scale = scale + self.draw() + def computeNewCoords(self, ignoreExisting=False, canonOrient=False): """Computes new coordinates for the molecule taking into account all existing positions (feeding these to the rdkit coordinate generation as @@ -333,7 +343,8 @@ def sanitizeDrawMol(self, kekulize=False, drawkekulize=False): finishedDrawing = QtCore.Signal(name="finishedDrawing") def getMolSvg(self): - self.drawer = rdMolDraw2D.MolDraw2DSVG(300, 300) + width, height = int(self.width() / self.scale), int(self.height() / self.scale) + self.drawer = rdMolDraw2D.MolDraw2DSVG(width, height) # TODO, what if self._drawmol doesn't exist? if self._drawmol is not None: # Chiral tags on R/S diff --git a/rdeditor/rdEditor.py b/rdeditor/rdEditor.py index f565a2b..f4ddaa1 100644 --- a/rdeditor/rdEditor.py +++ b/rdeditor/rdEditor.py @@ -77,7 +77,6 @@ def initGUI(self, fileName=None): self.setGeometry(100, 100, 200, 150) self.center = self.editor - self.center.setFixedSize(650, 650) self.setCentralWidget(self.center) self.fileName = fileName @@ -289,6 +288,12 @@ def CreateToolBars(self): self.mainToolBar.addAction(self.saveAction) self.mainToolBar.addAction(self.saveAsAction) self.mainToolBar.addSeparator() + self.scalewidget = QtWidgets.QComboBox() + self.scalewidget.addItems(["75%", "100%", "125%", "150%", "175%", "200%"]) + self.scalewidget.setCurrentIndex(1) + self.scalewidget.currentIndexChanged.connect(lambda: self.editor.setScale(float(self.scalewidget.currentText().strip('%'))/100)) + self.mainToolBar.addWidget(self.scalewidget) + self.mainToolBar.addSeparator() self.mainToolBar.addAction(self.selectAction) self.mainToolBar.addAction(self.addAction) # self.mainToolBar.addAction(self.addBondAction)