Example #1
0
 def copie_texte(self):
     if self.pix is None:
         msg = QtGui.QMessageBox(QtGui.QMessageBox.Warning, 'Impossible',
                                 'Vous devez d\'abord choisir une image source',
                                  QtGui.QMessageBox.Ok)
         icon = QtGui.QIcon()
         icon.addPixmap(QtGui.QPixmap(_fromUtf8("resources/PL128.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
         msg.setWindowIcon(icon)
         msg.exec_()
         return
     if self.lineEdit_texte.text():
         self.texte = str(self.lineEdit_texte.text())
     else :
         msg = QtGui.QMessageBox(QtGui.QMessageBox.Warning, 'Impossible', 'Vous n\'avez pas saisi de texte', QtGui.QMessageBox.Ok)
         icon = QtGui.QIcon()
         icon.addPixmap(QtGui.QPixmap(_fromUtf8("resources/PL128.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
         msg.setWindowIcon(icon)
         msg.exec_()
         return
     self.police = self.lineEdit_texte.font()
     self.mgnf = Magnifier(self, graphic=self.graphicsView, img=self.pix, scene=self.scene,
                                 police=self.police, texte=self.texte)
     self.mgnf.setup()
     self.mgnf.setFocus()
Example #2
0
class PL(QtGui.QDialog, Ui_Dialog):

    def __init__(self):
        QtGui.QDialog.__init__(self)
        Ui_Dialog.__init__(self)
        self.setupUi(self)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("resources/PL128.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.setWindowIcon(icon)
        self.pix = None
        self.newimage = None
        self.newimagepath = None
        self.image = None
        self.mgnf = None
        self.label.setText(str(self.horizontalSlider_quality.value()))

        self.horizontalSlider_quality.valueChanged.connect(self._slotvaleur)
        self.commandLinkButton.clicked.connect(self._choisiFichier)
        self.Button_valide.clicked.connect(self._sortie)
        self.Button_quit.clicked.connect(self.quitter)

        self.graphicsView.setAutoFillBackground(False)
        self.graphicsView.setStyleSheet("background: transparent")
        self.graphicsView.viewport().setAutoFillBackground(False)
        self.lineEdit_texte.setFocus()
        self.pushButton_source.clicked.connect(self.choix_source)
        QtCore.QObject.connect(self.pushButton_police, QtCore.SIGNAL(_fromUtf8("clicked()")), self.choix_police)
        QtCore.QObject.connect(self.pushButton_valide, QtCore.SIGNAL(_fromUtf8("clicked()")), self.copie_texte)
        QtCore.QObject.connect(self.pushButton_efface, QtCore.SIGNAL(_fromUtf8("clicked()")), self.effacer)
        QtCore.QObject.connect(self.pushButton_enreg, QtCore.SIGNAL(_fromUtf8("clicked()")), self.enreg)
        QtCore.QMetaObject.connectSlotsByName(self)
        path = LECHEMIN + '\\PersonnaLiseur.ini'
        with open(path, mode='r', encoding='utf-8') as f:
            self.options_texte = f.read(-1).split(',')
        self.check_init(self.options_texte)

    def check_init(self, options_texte):
        self.options = self.tab_lot.findChildren(QtGui.QGroupBox)
        for elt in self.options :
            if elt.title() in self.options_texte:
                elt.setChecked(True)
                for widg in elt.findChildren(QtGui.QWidget):
                    if isinstance(widg, QtGui.QComboBox):
                        try:
                            widg.setEditText(self.options_texte[2])
                        except:
                            pass
                    elif isinstance(widg, QtGui.QCheckBox)  and (widg.text() in self.options_texte):
                        widg.setChecked(True)
                try:
                    self.label.setText(self.options_texte[5])
                    self.horizontalSlider_quality.setSliderPosition(int(self.options_texte[5]))
                except :
                    pass
            else :
                elt.setChecked(False)



    def _slotvaleur(self) :
        self.label.setText(str(self.horizontalSlider_quality.value()))

    def _sortie(self) :
        if nb == 0 :
            msg = QtGui.QMessageBox(QtGui.QMessageBox.Warning, 'Attention', 'Vous n\'avez pas sélectionné de fichiers', \
                                     QtGui.QMessageBox.Ok)
            icon = QtGui.QIcon()
            icon.addPixmap(QtGui.QPixmap(_fromUtf8("resources/PL128.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            msg.setWindowIcon(icon)
            msg.setWindowModality(QtCore.Qt.ApplicationModal)
            msg.exec()
            Main.show()
        else :
            self.options = self.tab_lot.findChildren(QtGui.QGroupBox)  # selectionne les GroupBox
            self.options_child = []
            self.options_texte = []
            for elt in self.options :
                if elt.isChecked() :
                    self.options_child = elt.findChildren(QtGui.QWidget)
                    self.options_texte.append(elt.title())
                    for widg in self.options_child :
                        if isinstance(widg, QtGui.QLabel) :
                            self.options_texte.append(widg.text())
                        elif isinstance(widg, QtGui.QComboBox):
                            self.options_texte.append(widg.currentText())
                        elif isinstance(widg, QtGui.QCheckBox) and widg.isChecked() :
                                self.options_texte.append(widg.text())
            if self.checkBox_sauv.isChecked():
                self.options_texte.append(self.checkBox_sauv.text())
            print('self.options_texte : ', self.options_texte)

            if 'Insérer un logo' in self.options_texte :
                if not self.newimagepath:
                    self.newimagepath = QFileDialog.getOpenFileName(None, "Sélectionnez un logo à insérer :", os.getcwd(),
                                                                     "Images (*.png *.gif *.jpg *.jpeg *.bmp)")
                    self.newimage = QtGui.QImage(self.newimagepath)

            jobPD = job.ProgressDialog(bookdb, self.options_texte, nb, self.newimage, self.newimagepath)
            self.label_nb.setText('Aucun fichier sélectionné')
            self.commandLinkButton.setEnabled(True)
            self.faire_rapport()



    def _choisiFichier (self):
        self.new = QtGui.QFileDialog()
        self.new = MyFileDialog()
        self.new.setup(self.new)
        self.new.filesSelected.connect(self._nbfichiers)
        self.new.show()
        c = conn.cursor()
        c.execute('DELETE FROM ebook')

    def _nbfichiers(self, selected)  :
        global nb
        if os.path.isdir(selected[0]) :
            self.label_nb.setText('1 dossier sélectionné (' + str(nb) + ' fichiers)')
        else :
            self.label_nb.setText(str(nb) + ' fichier(s) sélectionné(s)')
        self.commandLinkButton.setEnabled(False)


    def faire_rapport (self):
        self.rap = QtGui.QDialog()
        self.rap = message_box_ui.Ui_Dialog2()
        self.rap.setupUi(self.rap)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(os.path.normpath(LECHEMIN + '/resources/PL128.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.rap.setWindowIcon(icon)
        self.rap.icon_label.setPixmap(QtGui.QPixmap(os.path.normpath(LECHEMIN + "/resources/dialog_error.png")))
        self.errlog = 'file:///' + LECHEMIN.replace('\\', '/') + '/errorlog.txt'
        self.rap.txt_bas.setText(_fromUtf8('Pour plus de détails ouvrez <a href="' + self.errlog + '">le fichier errorlog</a>'))
        self.rap.txt_bas.linkActivated.connect(self.show_log)
        self.rap.setWindowModality(QtCore.Qt.ApplicationModal)

        f = open(LECHEMIN + '/tmp2/bad.txt', mode='r', encoding='utf-8')
        texte = f.readlines()
        f.close()
        texte_det = ''.join(texte)
        if len(texte) >= 1:
            self.rap.msg.setText('Certains epub n\'ont pu être modifiés ({}) :'.format(len(texte)))
            self.rap.det_msg.setPlainText(texte_det)
            self.rap.exec_()
        else:
            if 'Détatouage' in self.options_texte :
                self.affiche_tatoolog()
            return

    def affiche_tatoolog(self):
        self.tatoolog = QtGui.QDialog()
        self.tatoolog = logtatoo.Ui_Dialog()
        self.tatoolog.setupUi(self.tatoolog)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(os.path.normpath(LECHEMIN + '/resources/PL128.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.tatoolog.setWindowIcon(icon)
        f = open(LECHEMIN + '/logtatoo.txt', mode='r')
        texte = f.read()
        f.close()
        self.tatoolog.textEdit.setPlainText(texte)
        self.tatoolog.exec_()
        # return

    def show_log(self, errlog):
        url = QUrl.fromLocalFile(errlog)
        print(url)
        print(url.isValid())
        QDesktopServices.openUrl(QUrl(url), QUrl.TolerantMode)
        return

#===============================================================================
# Traitement des images
#===============================================================================

    def choix_source(self):
        fichier = QFileDialog.getOpenFileName(None, "Ouvrir un fichier", os.getcwd(), "Images (*.png *.gif *.jpg *.jpeg *.bmp)")
        self.image = QtGui.QImage(fichier)
        w_img, h_img = self.image.width(), self.image.height()
        self.pix = QtGui.QPixmap.fromImage(self.image.scaled(w_img, h_img, QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation),
                                            QtCore.Qt.DiffuseAlphaDither)
        self.scene = QtGui.QGraphicsScene()
        self.scene.clear()
        self.scene.setSceneRect(0, 0, w_img, h_img)
        self.scene.addPixmap(self.pix)
        self.graphicsView.setScene(self.scene)
        self.graphicsView.fitInView(0, 0, w_img, h_img, QtCore.Qt.KeepAspectRatioByExpanding)
        PL.show(self.graphicsView)
        QtCore.QCoreApplication.processEvents()
        return

    def enreg(self):
        rect = QtCore.QRect(0, 0, 0, 0)
        self.newimage = self.image.copy(rect)
        if self.newimage.format() == QtGui.QImage.Format_Indexed8:
            self.newimage = self.newimage.convertToFormat(QtGui.QImage.Format_ARGB32_Premultiplied,
                                                            QtCore.Qt.DiffuseAlphaDither | QtCore.Qt.AvoidDither)
        if self.mgnf:
            x = self.mgnf.pos().x() - 1
            y = self.mgnf.pos().y() + self.mgnf.height * 0.11
            painter = QtGui.QPainter()
            painter.begin(self.newimage)
            painter.setFont(self.mgnf.police)
            painter.drawStaticText(x, y, self.mgnf.montexte)
            painter.end()
        self.newimagepath = QFileDialog.getSaveFileName(None, "Enregistrer l'image", os.getcwd(), 'Images(*.png)')
        self.newimage.save(self.newimagepath, format=None, quality=0)
        return self.newimage

    def effacer(self):
        if self.pix:
            self.scene.clear()
            self.scene.setSceneRect(0, 0, self.image.width(), self.image.height())
            self.scene.addPixmap(self.pix)
            self.graphicsView.setScene(self.scene)
            PL.show(self.graphicsView)
        self.lineEdit_texte.clear()
        self.lineEdit_texte.setFocus()

    def choix_police(self):
        self.police = QFontDialog.getFont(QFont('Times New Roman', 11), self, 'Choisissez une police')
        self.lineEdit_texte.setFont(self.police[0])
        self.lineEdit_texte.setFocus()
        self.lineEdit_texte.adjustSize()

    def copie_texte(self):
        if self.pix is None:
            msg = QtGui.QMessageBox(QtGui.QMessageBox.Warning, 'Impossible',
                                    'Vous devez d\'abord choisir une image source',
                                     QtGui.QMessageBox.Ok)
            icon = QtGui.QIcon()
            icon.addPixmap(QtGui.QPixmap(_fromUtf8("resources/PL128.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            msg.setWindowIcon(icon)
            msg.exec_()
            return
        if self.lineEdit_texte.text():
            self.texte = str(self.lineEdit_texte.text())
        else :
            msg = QtGui.QMessageBox(QtGui.QMessageBox.Warning, 'Impossible', 'Vous n\'avez pas saisi de texte', QtGui.QMessageBox.Ok)
            icon = QtGui.QIcon()
            icon.addPixmap(QtGui.QPixmap(_fromUtf8("resources/PL128.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            msg.setWindowIcon(icon)
            msg.exec_()
            return
        self.police = self.lineEdit_texte.font()
        self.mgnf = Magnifier(self, graphic=self.graphicsView, img=self.pix, scene=self.scene,
                                    police=self.police, texte=self.texte)
        self.mgnf.setup()
        self.mgnf.setFocus()
#===============================================================================
# QUITTER
#===============================================================================
    def quitter(self, options_texte):
        path = os.path.normpath(LECHEMIN + '/PersonnaLiseur.ini')
        with open(path, mode='w', encoding='utf-8', errors='strict') as f:
            for elt in self.options_texte:
                f.write(str(elt + ','))
        top = os.path.normpath(LECHEMIN + '/tmp')
        for root, dirs, files in os.walk(top, topdown=False):
            for name in files:
                try:
                    os.remove(os.path.join(root, name))
                except:
                    continue
            for name in dirs:
                try:
                    os.rmdir(os.path.join(root, name))
                except:
                    pass
        QtGui.QApplication.quit()