Example #1
0
    def reposition(self):

        fm = QFontMetricsF(self._font, self)
        rect = fm.boundingRect(QRectF(self.rect()), Qt.AlignCenter,
                               str(self._value))
        self._xpos = -rect.width() / 2.0
        self._ypos = rect.height() / 2.0 - fm.descent()
        self.update()
Example #2
0
    def reposition(self):

        fm = QFontMetricsF(self._font, self)
        rect = fm.boundingRect(QRectF(self.rect()), Qt.AlignCenter,
                str(self._value))
        self._xpos = -rect.width()/2.0
        self._ypos = rect.height()/2.0 - fm.descent()
        self.update()
    def paintEvent(self, e):
        # ref: https://forums.khronos.org/showthread.php/62124
        # self._projection = perspective(30 * self.height() / 512, self.width() / self.height(), 0.01, 100)
        s = 1 / tan(radians(self._state.projection_fov) / 2)
        self._projection = perspective2(s * 512 / self.height(),
                                        s * 512 / self.width(), 0.01, 100)
        self._viewport = viewport(0, self.height(), self.width(),
                                  -self.height())

        self._crop_corners = list(product(*self._state.volume_crop))
        pts = [self._project(c) for c in self._crop_corners]
        top_left = [
            min([c[i] - self._screen_bounds_padding for c in pts])
            for i in range(2)
        ]
        bottom_right = [
            max([c[i] + self._screen_bounds_padding for c in pts])
            for i in range(2)
        ]

        self._screen_bounds_render = [[
            quantized_floor(x, self._scale_render) for x in top_left
        ], [quantized_ceil(x, self._scale_render) for x in bottom_right]]
        self._screen_bounds_display = [[
            quantized_floor(x, self._scale_display) for x in top_left
        ], [quantized_ceil(x, self._scale_display) for x in bottom_right]]

        self._resolution_render = (
            (numpy.array(self._screen_bounds_render[1]) -
             self._screen_bounds_render[0]) // self._scale_render).astype(
                 numpy.intp)

        painter = QPainter(self)

        # background
        if self._transparency:
            painter.fillRect(self.rect(), Qt.magenta)
        else:
            painter.fillRect(self.rect(), Qt.black)

        # volume
        self._draw_volume(painter)

        # annotations
        if self._annotations > 0:
            painter.setRenderHint(QPainter.Antialiasing)
            painter.setRenderHint(QPainter.TextAntialiasing)

            self._draw_volume_axes(painter)
            self._draw_volume_bounding_box(painter)

            metrics = QFontMetricsF(painter.font())

            pen = QPen()
            pen.setColor(QColor(255, 255, 255))
            painter.setPen(pen)

            dt = numpy.mean(self._render_times or [0])
            self._render_times = self._render_times[-10:]

            lines = [
                (2, f'{self._state.display_volume.renderer.type}  {1e3*dt:6.1f} ms  {1/(dt+1e-6):6.1f} fps'),
                (2, f'{self._resolution_display[0]} \u00d7 {self._resolution_display[1]} px'.ljust(15) \
                    + f'{self._state.display_volume.shape[0]} \u00d7 {self._state.display_volume.shape[1]} \u00d7 {self._state.display_volume.shape[2]} vx'),
                (1, f'{self._state.volume_position+1} of {self._state.volume_count}'),
                (1, f'{self._resolution_render[0]} \u00d7 {self._resolution_render[1]} px'),
                (2, f'{self._state.acquired_shape_mm[0]:.2f} \u00d7 {self._state.acquired_shape_mm[1]:.2f} \u00d7 {self._state.acquired_shape_mm[2]:.2f} mm'),
                (2, f'{self._state.acquired_shape_vx[0]} \u00d7 {self._state.acquired_shape_vx[1]} \u00d7 {self._state.acquired_shape_vx[2]} vx'),
                (1, self._state.filename),
            ]

            i = 0
            for (lvl, line) in reversed(lines):
                if self._annotations >= lvl:
                    painter.drawText(
                        3,
                        self.height() - i * metrics.height() -
                        metrics.descent() - 2, line)
                    i += 1

            scale_factor, _, angles, translation, _ = decompose_matrix(
                self._view * self._model)

            lines = [
                (1,
                 f'{degrees(angles[0]):+6.1f}\u00b0  {degrees(angles[1]):+6.1f}\u00b0  {degrees(angles[2]):+6.1f}\u00b0'
                 ),
            ]

            i = 0
            for (lvl, line) in reversed(lines):
                if self._annotations >= lvl:
                    painter.drawText(
                        self.width() - 3 - metrics.boundingRect(line).width(),
                        self.height() - i * metrics.height() -
                        metrics.descent() - 2, line)
                    i += 1

        if self._annotations >= 2:
            if self._status_text:
                rect = metrics.boundingRect(self._status_text)
                painter.drawText(self.width() - rect.width() - 3,
                                 rect.height(), self._status_text)
Example #4
0
def pixmapForLegendNode(legend_node):

    # handles only symbol nodes
    if not isinstance(legend_node, QgsSymbolLegendNode):
        return

    # If size is default, use default implementation
    size = iface.layerTreeView().iconSize()
    if size.width() in (-1, 16):
        size = QSize(18, 18)

    symbol = legend_node.symbol()
    if not symbol:
        return

    # Compute minimum width
    model = iface.layerTreeView().layerTreeModel()
    if not legend_node.layerNode():
        return

    text = legend_node.textOnSymbolLabel()

    minimum_width = max(
        max(
            l_node.minimumIconSize().width() + (8 if text else 0)
            for l_node in model.layerLegendNodes(legend_node.layerNode())
            if isinstance(l_node, QgsSymbolLegendNode)
        ),
        size.width(),
    )

    symbol_size = QSize(minimum_width, size.height())
    context = QgsRenderContext.fromMapSettings(iface.mapCanvas().mapSettings())
    pixmap = QgsSymbolLayerUtils.symbolPreviewPixmap(symbol, symbol_size, 0, context)

    if text:
        painter = QPainter(pixmap)
        text_format = legend_node.textOnSymbolTextFormat()

        try:
            text_context = createTemporaryRenderContext()
            if text_context:
                painter.setRenderHint(QPainter.Antialiasing)
                text_context.setPainter(painter)

                font_metrics = QFontMetricsF(text_format.scaledFont(context))
                y_baseline_v_center = (
                    symbol_size.height()
                    + font_metrics.ascent()
                    - font_metrics.descent()
                ) / 2

                QgsTextRenderer.drawText(
                    QPointF(symbol_size.width() / 2, y_baseline_v_center),
                    0,
                    QgsTextRenderer.AlignCenter,
                    [text],
                    text_context,
                    text_format,
                )
                text_context.setPainter(None)

        except Exception as e:
            QgsMessageLog.logMessage(str(e))

    return pixmap