rdkit.Chem.Draw.qtCanvas

Package rdkit :: Package Chem :: Package Draw :: Module qtCanvas |
[hide private]
[frames] | no frames]

# Source Code for Module rdkit.Chem.Draw.qtCanvas

      1  #
      2  #  Copyright (C) 2014 Seiji Matsuoka
      3  #
      4  #   @@ All Rights Reserved @@
      5  #  This file is part of the RDKit.
      6  #  The contents are covered by the terms of the BSD license
      7  #  which is included in the file license.txt, found at the root
      8  #  of the RDKit source tree.
      9  #
     10
     11  from rdkit.Chem.Draw.canvasbase import CanvasBase
     12  try:
     13    from PySide import QtGui, QtCore
     14  except ImportError:
     15    from PyQt5 import QtGui, QtCore
     16
     17
     18 -class Canvas(CanvasBase):
     19
     20 -  def __init__(self, size):
     21      self.size = size
     22      self.qsize = QtCore.QSize(*size)
     23      self.pixmap = QtGui.QPixmap(self.qsize)
     24      self.painter = QtGui.QPainter(self.pixmap)
     25      self.painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
     26      self.painter.setRenderHint(QtGui.QPainter.SmoothPixmapTransform, True)
     27      self.painter.fillRect(0, 0, size[0], size[1], QtCore.Qt.white)
     28
     29 -  def addCanvasLine(self, p1, p2, color=(0, 0, 0), color2=None, **kwargs):
     30      if 'dash' in kwargs:
     31        line_type = QtCore.Qt.DashLine
     32      else:
     33        line_type = QtCore.Qt.SolidLine
     34      qp1 = QtCore.QPointF(*p1)
     35      qp2 = QtCore.QPointF(*p2)
     36      qpm = QtCore.QPointF((p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2)
     37      if color2 and color2 != color:
     38        rgb = [int(c * 255) for c in color]
     39        pen = QtGui.QPen(QtGui.QColor(*rgb), 1, line_type)
     40        self.painter.setPen(pen)
     41        self.painter.drawLine(qp1, qpm)
     42        rgb2 = [int(c * 255) for c in color2]
     43        pen.setColor(QtGui.QColor(*rgb2))
     44        self.painter.setPen(pen)
     45        self.painter.drawLine(qpm, qp2)
     46      else:
     47        rgb = [int(c * 255) for c in color]
     48        pen = QtGui.QPen(QtGui.QColor(*rgb), 1, line_type)
     49        self.painter.setPen(pen)
     50        self.painter.drawLine(qp1, qp2)
     51
     52 -  def addCanvasText(self, text, pos, font, color=(0, 0, 0), **kwargs):
     53      orientation = kwargs.get('orientation', 'E')
     54      qfont = QtGui.QFont("Helvetica", font.size * 1.5)
     55      qtext = QtGui.QTextDocument()
     56      qtext.setDefaultFont(qfont)
     57      colored = [int(c * 255) for c in color]
     58      colored.append(text)
     59      html_format = "<span style='color:rgb({},{},{})'>{}</span>"
     60      formatted = html_format.format(*colored)
     61      qtext.setHtml(formatted)
     62      if orientation == 'N':
     63        qpos = QtCore.QPointF(pos[0] - qtext.idealWidth() / 2, pos[1] - font.size)
     64      elif orientation == 'W':
     65        qpos = QtCore.QPointF(pos[0] - qtext.idealWidth() + font.size, pos[1] - font.size)
     66      else:
     67        qpos = QtCore.QPointF(pos[0] - font.size, pos[1] - font.size)
     68      self.painter.save()
     69      self.painter.translate(qpos)
     70      qtext.drawContents(self.painter)
     71      self.painter.restore()
     72      return font.size * 1.8, font.size * 1.8, 0
     73
     74 -  def addCanvasPolygon(self, ps, color=(0, 0, 0), fill=True, stroke=False, **kwargs):
     75      polygon = QtGui.QPolygonF()
     76      for ver in ps:
     77        polygon.append(QtCore.QPointF(*ver))
     78      pen = QtGui.QPen(QtGui.QColor(*color), 1, QtCore.Qt.SolidLine)
     79      self.painter.setPen(pen)
     80      self.painter.setBrush(QtGui.QColor(0, 0, 0))
     81      self.painter.drawPolygon(polygon)
     82
     83 -  def addCanvasDashedWedge(self, p1, p2, p3, dash=(2, 2), color=(0, 0, 0), color2=None, **kwargs):
     84      rgb = [int(c * 255) for c in color]
     85      pen = QtGui.QPen(QtGui.QColor(*rgb), 1, QtCore.Qt.SolidLine)
     86      self.painter.setPen(pen)
     87      dash = (4, 4)
     88      pts1 = self._getLinePoints(p1, p2, dash)
     89      pts2 = self._getLinePoints(p1, p3, dash)
     90      if len(pts2) < len(pts1):
     91        pts2, pts1 = pts1, pts2
     92      for i in range(len(pts1)):
     93        qp1 = QtCore.QPointF(pts1[i][0], pts1[i][1])
     94        qp2 = QtCore.QPointF(pts2[i][0], pts2[i][1])
     95        self.painter.drawLine(qp1, qp2)
     96
     97 -  def flush(self):
     98      self.painter.end()
     99