Ejemplo n.º 1
0
    def __init__(self, user_settings, parent=None):
        QDialog.__init__(self, parent)

        self.ui = Ui_PrintDialog()
        self.ui.setupUi(self)

        self.user_settings = user_settings
        self.compatibility = user_settings.compatibility

        self.connect(self.ui.logoLoad, SIGNAL('clicked(bool)'), self.openImage)

        self.ui.printButton = QPushButton(tr('Print'))
        self.connect(self.ui.printButton, SIGNAL('clicked(bool)'), self._print_action)
        self.ui.buttonBox.addButton(self.ui.printButton, QDialogButtonBox.ActionRole)

        self._init_options()
Ejemplo n.º 2
0
class PrinterBaseDialog(QDialog):

    RESIZED_LOGO_PATH = QDir.tempPath()

    def __init__(self, user_settings, parent=None):
        QDialog.__init__(self, parent)

        self.ui = Ui_PrintDialog()
        self.ui.setupUi(self)

        self.user_settings = user_settings
        self.compatibility = user_settings.compatibility

        self.connect(self.ui.logoLoad, SIGNAL('clicked(bool)'), self.openImage)

        self.ui.printButton = QPushButton(tr('Print'))
        self.connect(self.ui.printButton, SIGNAL('clicked(bool)'), self._print_action)
        self.ui.buttonBox.addButton(self.ui.printButton, QDialogButtonBox.ActionRole)

        self._init_options()

    def _init_options(self):
        settings = self.user_settings['printer']
        self.ui.contentTableBox.setChecked(settings.content_table)
        self.ui.headerBox.setChecked(settings.header)
        self.ui.footerBox.setChecked(settings.footer)
        self.ui.titleEdit.setText(settings.title)
        self.ui.enterpriseEdit.setText(settings.enterprise)
        self.ui.logoPath.setText(settings.logo)

    def openImage(self, b):
        s = QFileDialog.getOpenFileName(self)
        self.ui.logoPath.setText(s)

    def run(self):
        return self.exec_()

    def checkInput(self):
        """
        Called to know if the users inputs in dialog are corrects.

        @return [bool]
        """
        raise NotImplementedError()

    def buildPrinter(self):
        """
        This method needs to be overloaded. It returns a Printer instance.
        """
        raise NotImplementedError()

    def preparePrinting(self):
        """
        Prepare printing of pages.
        """
        raise NotImplementedError()

    def _print_action(self, b):
        if not self.checkInput():
            return

        self.printer = self.buildPrinter()

        if not self.printer.showDialog(parent=self):
            del self.printer
            return

        # Save settings
        settings = self.user_settings['printer']
        settings.content_table = self.ui.contentTableBox.isChecked()
        settings.header = self.ui.headerBox.isChecked()
        settings.footer = self.ui.footerBox.isChecked()
        settings.title = unicode(self.ui.titleEdit.text())
        settings.enterprise = unicode(self.ui.enterpriseEdit.text())
        settings.logo = unicode(self.ui.logoPath.text())

        # Disable widgets
        if hasattr(self.ui, 'pages_list'):
            self.ui.pages_list.setEnabled(False)
        self.ui.buttonBox.setEnabled(False)
        self.ui.optionsBox.setEnabled(False)
        self.ui.progressBar.setEnabled(True)
        self.ui.progressBar.setValue(0)
        self.ui.progressLabel.setText(self.tr('Initialization...'))

        self.repaint()

        self.preparePrinting()

    def printPages(self, pages):

        self.ui.progressBar.setValue(0)
        self.ui.progressLabel.setText(self.tr('Printing...'))

        # Main title
        nb_printed = 0
        for page, frag_widgets in pages:

            self.printer.newPage()

            frag_no = 0
            for frag_widget in frag_widgets:
                if frag_widget.height() == 0 or frag_widget.width() == 0 or frag_widget.isHidden():
                    nb_printed += 1
                    continue

                self.printer.drawHCenteredTitle(frag_widget.getView().title.text(),
                                                (self.printer.height() + 10) * (frag_no % 2) * 0.5)

                self.printer.drawFragment(frag_widget, self.printer.height() * (0.05 + (frag_no % 2) * 0.5), len(frag_widgets))

                # Add a new page if we need more place
                if frag_no < len(frag_widgets) - 1 and frag_no % 2:
                    self.printer.drawText(page.title, self.printer.height() - 40, halign=Qt.AlignRight, size=15)
                    if nb_printed < len(frag_widgets) - 1:
                        self.printer.newPage()
                frag_no += 1
                nb_printed += 1

                self.ui.progressBar.setValue(nb_printed)

            self.printer.drawText(page.title, self.printer.height() - 40, halign=Qt.AlignRight, size=15)

        self.printer.end()
        self.end()

    def end(self):
        # Enable widgets
        self.ui.buttonBox.setEnabled(True)
        self.ui.optionsBox.setEnabled(True)
        self.ui.progressBar.setEnabled(False)
        self.ui.progressLabel.setText(self.tr('Done.'))

    def _resizeLogo(self, logo):
        img = QImage(logo)

        if img.width() < 200 and img.height() < 200:
            return logo

        scale_on_width = True if img.width() > img.height() else False

        if scale_on_width:
            img = img.scaledToWidth(300)
        else:
            img = img.scaledToHeight(300)

        split_path = logo.split("/")
        filename = split_path[len(split_path) - 1]

        logo = self.RESIZED_LOGO_PATH + "/" + filename
        img.save(logo)

        return logo