Skip to content

Commit

Permalink
Make label sizes configurable with the mouse wheel
Browse files Browse the repository at this point in the history
  • Loading branch information
dkratzert committed Sep 26, 2024
1 parent d1fab1e commit f99abf4
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions finalcif/displaymol/molecule2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QPen, QBrush, QColor, QMouseEvent, QPalette, QImage, QResizeEvent
from PyQt5.QtGui import QPainter, QPen, QBrush, QColor, QMouseEvent, QPalette, QImage, QResizeEvent, QWheelEvent

from finalcif.cif.atoms import get_radius_from_element, element2color
from finalcif.cif.cif_file_io import CifContainer
Expand All @@ -29,6 +29,7 @@ def __init__(self, parent):
super().__init__(parent=parent)
self.factor = 1.0
self.atoms_size = 12
self.fontsize = 13
self.bond_width = 2
self.labels = False
self.molecule_center = np.array([0, 0, 0])
Expand Down Expand Up @@ -56,6 +57,12 @@ def __init__(self, parent):
self.projected_points = []
self.zoom = 1.1

def setLabelFont(self, font_size: int):
if font_size < 0:
font_size = 1
self.fontsize = font_size
self.update()

def clear(self):
self.open_molecule(atoms=[])

Expand Down Expand Up @@ -84,7 +91,7 @@ def paintEvent(self, event):
self.painter = QPainter(self)
self.painter.setRenderHint(QPainter.Antialiasing)
font = self.painter.font()
font.setPixelSize(13)
font.setPixelSize(self.fontsize)
self.painter.setFont(font)
try:
self.draw()
Expand All @@ -95,6 +102,12 @@ def paintEvent(self, event):
def mousePressEvent(self, event: QMouseEvent) -> None:
self.lastPos = event.pos()

def wheelEvent(self, event: QWheelEvent):
if event.angleDelta().y() > 0:
self.setLabelFont(self.fontsize + 2)
elif event.angleDelta().y() < 0:
self.setLabelFont(self.fontsize - 2)

def save_image(self, filename: Path, image_scale=1.5):
image = QImage(self.size() * image_scale, QImage.Format_RGB32)
image.fill(Qt.white)
Expand Down Expand Up @@ -285,6 +298,7 @@ def display(atoms: List[Atomtuple]):
render_widget = MoleculeWidget(None)
t1 = time.perf_counter()
render_widget.open_molecule(atoms, labels=False)
render_widget.labels = True
print(f'Time to display molecule: {time.perf_counter() - t1:5.4} s')
# add and show
window.setCentralWidget(render_widget)
Expand Down

0 comments on commit f99abf4

Please sign in to comment.