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