Example #1
0
 def findIcons(self):
     if hou.applicationVersion()[0] < 15:
         for category in os.listdir(self.path):
             for ico in os.listdir(os.path.join(self.path, category)):
                 iconPath = os.path.join(
                     os.path.join(self.path, category, ico))
                 iconName = '_'.join([category, os.path.splitext(ico)[0]])
                 self.icons[iconName] = QPixmap(iconPath)
     else:
         zf = zipfile.ZipFile(self.path, 'r')
         for f in zf.namelist():
             if f.startswith('old'): continue
             if os.path.splitext(f)[-1] == '.svg':
                 svg = QSvgRenderer(QByteArray(zf.read(f)))
                 if not svg.isValid():
                     continue
                 pixmap = QPixmap(iconSize, iconSize)
                 painter = QPainter()
                 painter.begin(pixmap)
                 pixmap.fill(QColor(Qt.black))
                 svg.render(painter)
                 painter.end()
                 category, ico = f.split('/')
                 iconName = '_'.join([category, os.path.splitext(ico)[0]])
                 self.icons[iconName] = pixmap
         zf.close()
Example #2
0
 def __load_svg(self, svg_file: str) -> QPixmap:
     """Loads a SVG file and scales it correctly for High-DPI screens"""
     svg_renderer = QSvgRenderer(svg_file)
     pixmap = QPixmap(svg_renderer.defaultSize() * self.devicePixelRatio())
     pixmap.fill(Qt.transparent)
     painter = QPainter()
     painter.begin(pixmap)
     svg_renderer.render(painter)
     painter.end()
     pixmap.setDevicePixelRatio(self.devicePixelRatio())
     return pixmap
Example #3
0
    def paint(self, painter, option, index):
        row = index.row()
        column = index.column()
        cell_data = self.grid_data[row][column]
        is_word_cell = cell_data[0]
        clue_index = cell_data[1] or cell_data[2]

        painter.save()
        if is_word_cell:
            if clue_index:
                icon_path = os.path.join(icons_folder,
                                         '{}.svg'.format(clue_index))
                renderer = QSvgRenderer(icon_path)
                #pixmap = QPixmap(QSize(grid_cell_size, grid_cell_size))
                #pixmap.fill(Qt.transparent)
                renderer.render(painter, option.rect())
        painter.restore()
Example #4
0
    def data(self, index, role=Qt.DisplayRole):
        row = index.row()
        column = index.column()
        cell_data = self.grid_data[row][column]
        is_word_cell = cell_data[0]
        clue_index = cell_data[1] or cell_data[2]

        if role == Qt.DisplayRole:
            return self.solution_data[row][column]
        if role == Qt.EditRole:
            return self.solution_data[row][column]
        #elif role == Qt.DecorationRole:
        #  if clue_index:
        #    icon_path = os.path.join(icons_folder, '{}.svg'.format(clue_index))
        #    return QIcon(icon_path)
        #  else:
        #    return None
        elif role == Qt.BackgroundRole:
            if is_word_cell:
                if clue_index:
                    icon_path = os.path.join(icons_folder,
                                             '{}.svg'.format(clue_index))
                    renderer = QSvgRenderer(icon_path)
                    pixmap = QPixmap(QSize(grid_cell_size, grid_cell_size))
                    pixmap.fill(Qt.transparent)
                    painter = QPainter()
                    renderer.render(painter, pixmap.rect())
                    brush = QBrush()
                    brush.setTexture(pixmap)
                    return brush
                else:
                    return QColor(Qt.white)
            else:
                return QColor(Qt.black)
        elif role == Qt.FontRole:
            font = QFont(font_name, font_size)
            return font
        elif role == Qt.TextAlignmentRole:
            return Qt.AlignCenter
        return None
Example #5
0
class LoadingBar(QWidget):
    def __init__(self, parent, fn, *args):
        super(LoadingBar, self).__init__(parent)

        self.fn = fn
        self.args = args
        self.worker = Worker(fn, *args)
        self.worker.signals.result.connect(
            main_window.MainWindow.get_instance().calc_done)

        main_window.MainWindow.get_instance().threadpool.start(self.worker)

        # overlay
        # make the window frameless
        self.loading_icon = QSvgRenderer('resources/images/loading_icon.svg')
        self.qp = QPainter()

        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)

        self.fillColor = QColor(30, 30, 30, 120)
        self.penColor = QColor("#333333")

        self.close_btn = QPushButton(self)
        self.close_btn.setText("Abbrechen")

        font = QFont()
        font.setPixelSize(18)
        font.setBold(True)
        self.close_btn.setFont(font)
        self.close_btn.setStyleSheet("QPushButton {"
                                     "background-color: #EAEAEA;"
                                     "border: None;"
                                     "padding-top: 12px;"
                                     "padding-bottom: 12px;"
                                     "padding-left: 20px;"
                                     "padding-right: 20px;"
                                     "}"
                                     "QPushButton:hover {"
                                     "background-color: #DFDFDF;"
                                     "}")
        #self.close_btn.setFixedSize(30, 30)
        self.close_btn.clicked.connect(self._onclose)

        self.signals = LoadingBarSignals()

    def resizeEvent(self, event=None):
        button_x = (self.width() - self.close_btn.width()) / 2
        button_y = (self.height() - self.close_btn.height()) / 2 + 60
        self.close_btn.move(button_x, button_y)

    def paintEvent(self, event):
        # This method is, in practice, drawing the contents of
        # your window.

        # get current window size
        s = self.size()
        self.qp.begin(self)
        self.qp.setRenderHint(QPainter.Antialiasing, True)
        self.qp.setPen(self.penColor)
        self.qp.setBrush(self.fillColor)
        self.qp.drawRect(0, 0, s.width(), s.height())

        icon_size = 180
        self.loading_icon.render(
            self.qp,
            QRect((self.width() - icon_size) / 2,
                  (self.height() - icon_size) / 2 - 60, icon_size, icon_size))
        self.qp.end()

    def _onclose(self):
        self.signals.close.emit()
Example #6
0
def svg_to_outlines(root, width_mm=None, height_mm=None, pixels_per_mm=5.0):
    """
    Given an SVG as a Python ElementTree, return a set of straight line
    segments which approximate the outlines in that SVG when rendered.

    Occlusion is not accounted for in the returned list of outlines. Even if
    one shape is completely occluded by another, both of their outlines will be
    reported. Simillarly, overlapping lines will also be passed through.

    .. note::

        This function internally uses the QSvg library from Qt to render the
        provided SVG to a special painting backend which captures the output
        lines. As a consequence, SVG feature support is as good or as bad as
        that library. QSvg is generally regarded as a high quality and
        relatively complete SVG implementation and so most SVG features should
        be supported.

    .. note::

        Due to its internal use of Qt, a PySide2.QtGui.QGuiApplication will be
        created if one has not already been created. Non-Qt users and most Qt
        users should not be affected by this.

    Parameters
    ----------
    root : ElementTree
        The SVG whose outlines should be extracted.
    width_mm, height_mm : float or None
        The page size to render the SVG at (in milimeters). If omitted, this
        will be determined automatically from the SVG's width and height
        attributes. These arguments are mandatory for SVGs lacking these
        attributes.
    pixels_per_mm : float
        Curved outlines will be approximated by straight lines in the output.
        This parameter controls how exactly curves will be approximated.
        Specifically, the curve approximation will be at least fine enough for
        rasterised versions of the lines to 'look right' at the specified pixel
        density.

    Returns
    -------
    [((r, g, b, a) or None, width, [(x, y), ...]), ...]
        A list of polylines described by (colour, line) pairs.

        The 'colour' values define the colour used to draw the line (if a solid
        colour was used) or None (if a gradient or patterned stroke was used).
        Colours are given as four-tuples in RGBA order with values from 0.0 to
        1.0.

        The 'width' value gives the line width used to draw the line (given in
        mm). If the shape being drawn has a non-uniform scaling applied, this
        value may not be meaningful and its actual value should be considered
        undefined.

        The 'line' part of each tuple defines the outline as a series of (x, y)
        coordinates (given in mm) which describe a continuous polyline. These
        polylines may be considered open. Closed lines in the input SVG will
        result in polylines where the first and last coordinate are identical.
        Lines may go beyond the bounds of the designated page size (as in the
        input SVG).
    """
    # This method internally uses various parts of Qt which require that a Qt
    # application exists. If one does not exist, one will be created.
    if QGuiApplication.instance() is None:
        QGuiApplication()

    # Determine the page size from the document if necessary
    if width_mm is None or height_mm is None:
        width_mm, height_mm = get_svg_page_size(root)

    # Load the SVG into QSvg
    xml_stream_reader = QXmlStreamReader()
    xml_stream_reader.addData(ElementTree.tostring(root, "unicode"))
    svg_renderer = QSvgRenderer()
    svg_renderer.load(xml_stream_reader)

    # Paint the SVG into the OutlinePaintDevice which will capture the set of
    # line segments which make up the SVG as rendered.
    outline_paint_device = OutlinePaintDevice(width_mm, height_mm,
                                              pixels_per_mm)
    painter = QPainter(outline_paint_device)
    try:
        svg_renderer.render(painter)
    finally:
        painter.end()

    return outline_paint_device.getOutlines()