Skip to content
Snippets Groups Projects
export_trace_manager.py 2.62 KiB
from PyQt5.QtCore import Qt
from PyQt5.QtCore import QRectF
from PyQt5.QtGui import QFont
from PyQt5.QtGui import QFontMetrics
from PyQt5.QtGui import QPixmap
#from PyQt5.QtGui import QImage
from PyQt5.QtGui import QPainter
from PyQt5.QtGui import QColor, QLinearGradient, QBrush, QRadialGradient, QPen
from PyQt5.QtWidgets import QStyleOptionViewItem
from PyQt5.QtWidgets import QStyleOption
from settings_manager import settings
from paint_graph_item import paint_graph_item

# export trace as image file.  Return (status)
def export_trace(trace_filename, graph_item, graph_index):
    """export_trace_manager manages export of trace image files."""
    # painting is much like GraphListItemDelegate.paint().

    if graph_item == None:
        return "Error: graph not available"

    # font height and banner height
    font_height = QFontMetrics(QFont()).height()
    banner_height = int(font_height * 1.5)

    # find the corners of this graph
    bounding_rect = graph_item.bounding_rect()

    # maybe make bounds wider if this is the global view to fit its title
    if graph_index == 0:
        bounding_rect = bounding_rect.united(QRectF(0, 0, 90, 0))

    # add border padding
    bounding_rect.adjust(-banner_height, -banner_height,
                         banner_height, banner_height)

    # define the bounding rectangle for pixmap for painter to paint in
    pixmap = QPixmap(bounding_rect.width(), bounding_rect.height())
    painter = QPainter(pixmap)

    # fill background
    fill_rect = QRectF(0, 0, bounding_rect.width(), bounding_rect.height())
    gradient = QLinearGradient(fill_rect.topLeft(),
                               fill_rect.bottomRight())
    c = QColor(settings["background_c"])
    gradient.setColorAt(0, c.lighter(settings["background_gradient"]))
    gradient.setColorAt(0.5, c)
    gradient.setColorAt(1, c.darker(settings["background_gradient"]))
    painter.fillRect(fill_rect, QBrush(gradient))

    # center the graph
    painter.save()
    painter.translate(banner_height, 0)

    # paint the graph item
    scale = 1
    # get a default option object.
    option = QStyleOption()
    paint_graph_item(painter, graph_item, graph_index, scale, option,
                                        font_height, banner_height)

    painter.restore()
    painter.end()

    # export the JSON multigraph project
    try:
        success = pixmap.save(trace_filename)
        if success:
            return ""
        else:
            return "Error: Export failed"

    except Exception as e:
        status = "Error exporting trace image file '%s': %s" % (
                                         trace_filename, str(e))
        return status