Beispiel #1
0
    def drawItems(self, painter, items, options, widget):

        LicGLHelpers.clear([0.62, 0.62, 0.65, 1.0])

        # First draw all items that are not annotations
        if self.renderMode == 'full' or self.renderMode == 'background':
            for i, item in enumerate(items):
                if item.isVisible() and (not hasattr(item, 'isAnnotation') or not item.isAnnotation):
                    self.drawOneItem(painter, item, options[i], widget)

        if widget and self.renderMode == 'full':

            # Build list of pages to be drawn (if any)
            rect = QRectF(self.views()[0].mapToScene(QPoint()), QSizeF(widget.size()) / self.scaleFactor)
            pagesToDraw = []
            for page in self.pages:
                if page.isVisible() and rect.intersects(page.rect().translated(page.pos())):
                    pagesToDraw.append(page)

            if pagesToDraw:
                # Setup the GL items to be drawn & the necessary context
                painter.beginNativePainting()
                LicGLHelpers.initFreshContext(False)
    
                # Draw all GL items
                for page in pagesToDraw:
                    page.drawGLItems(rect)
    
                LicGLHelpers.setupForQtPainter()  # Reset all GL lighting, so that subsequent drawing is not affected
                painter.endNativePainting()
            
        # Draw all annotation
        if self.renderMode == 'full' or self.renderMode == 'foreground':
            for i, item in enumerate(items):
                if item.isVisible() and (hasattr(item, 'isAnnotation') and item.isAnnotation):
                    self.drawOneItem(painter, item, options[i], widget)
    def setItemtoMove(self, part=None):
        self.destItem = None
        self._item = part
        step = part
        while step and not isinstance(step, Step):
            step = step.parent()
        self._step.setText(step.data(Qt.DisplayRole))
        self._page.setText(step.parentItem().data(Qt.DisplayRole))
        self._warning.clear()
        if part:
            pItem = None
            if step and step.hasPLI():
                for pliItem in step.pli.pliItems:
                    if pliItem.abstractPart.filename == part.abstractPart.filename:
                        pItem = pliItem
                        break

            sRect = self._thumbnail.scene().sceneRect()
            if isinstance(pItem, (Part, PLIItem)):
                a = pItem.abstractPart
                filename = os.path.join(config.grayscalePath(), os.path.splitext(a.filename)[0] + ".png").lower()
                if not os.path.exists(filename):
                    pRect = pItem.sceneBoundingRect().toRect()
                    wt = Page.PageSize.width()
                    ht = Page.PageSize.height()
                    mx = int(PLI.margin.x() / 2)
                    bufferManager = LicGLHelpers.FrameBufferManager(wt, ht)
                    try:
                        bufferManager.bindMSFB()
                        LicGLHelpers.initFreshContext(True)

                        step.parentItem().drawGLItemsOffscreen(QRectF(0, 0, wt, ht), 1.0)
                        bufferManager.blitMSFB()
                        temp_data = bufferManager.readFB()
                        temp_cord = (
                            pRect.left() - mx,
                            pRect.top() - mx,
                            a.width + pRect.left() + mx,
                            a.height + pRect.top() + mx,
                        )
                        temp_name = tempfile.TemporaryFile().name + ".png".lower()
                        temp = Image.fromstring("RGBA", (wt, ht), temp_data)
                        temp = temp.transpose(Image.FLIP_TOP_BOTTOM)
                        temp = temp.crop(temp_cord)
                        temp.save(temp_name)
                    finally:
                        image = QImage(temp_name, "LA")
                        # convertToGrayscale
                        for i in range(0, image.width()):
                            for j in range(0, image.height()):
                                pix = image.pixel(i, j)
                                if pix > 0:
                                    color = qGray(pix)
                                    image.setPixel(i, j, qRgb(color, color, color))
                        # saveResult
                        image.save(filename, "PNG")
                        # cleanUp
                        bufferManager.cleanup()
                        os.remove(temp_name)
                else:
                    image = QImage(filename, "LA")
                image = image.scaledToHeight(sRect.height(), Qt.SmoothTransformation)
            else:
                image = QImage(sRect.width(), sRect.height(), QImage.Format_Mono)
                painter = QPainter(image)
                painter.fillRect(sRect, Qt.white)
                painter.setFont(QFont("Helvetica [Cronyx]", 10, QFont.Bold))
                painter.drawLine(1, 1, sRect.width() - 1, sRect.height() - 1)
                painter.drawLine(sRect.width() - 1, 1, 1, sRect.height() - 1)
                painter.drawText(sRect, Qt.TextSingleLine | Qt.AlignVCenter, self._noPLIText)
                painter.end()

            self._thumbnail.setPixmap(QPixmap.fromImage(image))

        if not self.isVisible():
            self.show()
    def setItemtoMove(self, part=None):
        self.destItem = None
        self._item = part
        step = part
        while step and not isinstance(step, Step):
            step = step.parent()
        self._step.setText(step.data(Qt.DisplayRole))
        self._page.setText(step.parentItem().data(Qt.DisplayRole))
        self._warning.clear()
        if part:
            pItem = None
            if step and step.hasPLI():
                for pliItem in step.pli.pliItems:
                    if pliItem.abstractPart.filename == part.abstractPart.filename:
                        pItem = pliItem
                        break

            sRect = self._thumbnail.scene().sceneRect()
            if isinstance(pItem, (Part, PLIItem)):
                a = pItem.abstractPart
                filename = os.path.join(
                    config.grayscalePath(),
                    os.path.splitext(a.filename)[0] + ".png").lower()
                if not os.path.exists(filename):
                    pRect = pItem.sceneBoundingRect().toRect()
                    wt = Page.PageSize.width()
                    ht = Page.PageSize.height()
                    mx = int(PLI.margin.x() / 2)
                    bufferManager = LicGLHelpers.FrameBufferManager(wt, ht)
                    try:
                        bufferManager.bindMSFB()
                        LicGLHelpers.initFreshContext(True)

                        step.parentItem().drawGLItemsOffscreen(
                            QRectF(0, 0, wt, ht), 1.0)
                        bufferManager.blitMSFB()
                        temp_data = bufferManager.readFB()
                        temp_cord = (pRect.left() - mx, pRect.top() - mx,
                                     a.width + pRect.left() + mx,
                                     a.height + pRect.top() + mx)
                        temp_name = tempfile.TemporaryFile(
                        ).name + ".png".lower()
                        temp = Image.fromstring("RGBA", (wt, ht), temp_data)
                        temp = temp.transpose(Image.FLIP_TOP_BOTTOM)
                        temp = temp.crop(temp_cord)
                        temp.save(temp_name)
                    finally:
                        image = QImage(temp_name, "LA")
                        #convertToGrayscale
                        for i in range(0, image.width()):
                            for j in range(0, image.height()):
                                pix = image.pixel(i, j)
                                if pix > 0:
                                    color = qGray(pix)
                                    image.setPixel(i, j,
                                                   qRgb(color, color, color))
                        #saveResult
                        image.save(filename, "PNG")
                        #cleanUp
                        bufferManager.cleanup()
                        os.remove(temp_name)
                else:
                    image = QImage(filename, "LA")
                image = image.scaledToHeight(sRect.height(),
                                             Qt.SmoothTransformation)
            else:
                image = QImage(sRect.width(), sRect.height(),
                               QImage.Format_Mono)
                painter = QPainter(image)
                painter.fillRect(sRect, Qt.white)
                painter.setFont(QFont("Helvetica [Cronyx]", 10, QFont.Bold))
                painter.drawLine(1, 1, sRect.width() - 1, sRect.height() - 1)
                painter.drawLine(sRect.width() - 1, 1, 1, sRect.height() - 1)
                painter.drawText(sRect, Qt.TextSingleLine | Qt.AlignVCenter,
                                 self._noPLIText)
                painter.end()

            self._thumbnail.setPixmap(QPixmap.fromImage(image))

        if not self.isVisible():
            self.show()