Example #1
0
class ExecDialog(QtGui.QDialog):
    def __init__(self, images, args=None, code="", parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.setWindowTitle("rotation")
        self.parent = parent
        self.codeBefore = code

        ### widget #####################################################
        ### image ###
        self.imW = QtGui.QComboBox(self)
        self.images = []
        if images:
            for i, v in enumerate(images):
                f = os.path.split(v)[1]
                self.imW.addItem(f)
                self.images.append(v)

        ### zoom buttons ###
        self.zoomInW = QtGui.QToolButton()
        self.zoomInW.setAutoRaise(True)
        self.zoomInW.setIcon(QtGui.QIcon(QtGui.QPixmap("icons/black_zoom_in.svg")))
        self.zoomOutW = QtGui.QToolButton()
        self.zoomOutW.setAutoRaise(True)
        self.zoomOutW.setIcon(QtGui.QIcon(QtGui.QPixmap("icons/black_zoom_out.svg")))
        self.zoomOneW = QtGui.QToolButton()
        self.zoomOneW.setAutoRaise(True)
        self.zoomOneW.setIcon(QtGui.QIcon(QtGui.QPixmap("icons/black_zoom_one.svg")))

        ### couleur ###
        if args:
            self.color = args["color"]
        else:
            self.color = QtGui.QColor(0, 0, 0)
        self.colorIcon = QtGui.QPixmap(22, 22)
        self.colorIcon.fill(self.color)

        self.colorW = QtGui.QToolButton(self)
        self.colorW.setAutoRaise(True)
        self.colorW.setIcon(QtGui.QIcon(self.colorIcon))

        ### action ###
        self.actionW = QtGui.QComboBox(self)
        self.actionW.addItem("tracer horizontal")
        self.actionW.addItem("tracer vertical")
        self.actionW.addItem("entrer angle")

        ### rotate 90, 180, 370 ###
        self.rotate90W = QtGui.QToolButton()
        self.rotate90W.setAutoRaise(True)
        self.rotate90W.setIcon(QtGui.QIcon(QtGui.QPixmap("icons/90.svg")))
        self.rotate270W = QtGui.QToolButton()
        self.rotate270W.setAutoRaise(True)
        self.rotate270W.setIcon(QtGui.QIcon(QtGui.QPixmap("icons/-90.svg")))
        self.rotate180W = QtGui.QToolButton()
        self.rotate180W.setAutoRaise(True)
        self.rotate180W.setIcon(QtGui.QIcon(QtGui.QPixmap("icons/180.svg")))

        ### labels info ###
        self.angle = 0
        self.degreL = QtGui.QLabel("angle :")
        self.degreW = QtGui.QLineEdit("0")
        self.degreW.setValidator(QtGui.QIntValidator(self.degreW))
        self.degreW.setDisabled(True)

        ### reset ###
        self.resetW = QtGui.QPushButton("reset")

        ### apercu ###
        self.apercuW = QtGui.QPushButton("apercu")

        ### viewer ###
        self.painting = Painting(self)
        self.painting.set_fig(Line(self.painting), self.color)
        self.viewer = Viewer(self)
        self.viewer.setWidget(self.painting)

        ### apply, undo ###
        self.okW = QtGui.QPushButton('apply', self)
        self.undoW = QtGui.QPushButton('undo', self)

        ### function ###################################################
        if images:
            self.im_changed(self.images[0])

        ### connexion ##################################################
        self.imW.activated[str].connect(self.im_changed)

        self.rotate90W.clicked.connect(self.rot90)
        self.rotate180W.clicked.connect(self.rot180)
        self.rotate270W.clicked.connect(self.rot270)
        self.colorW.clicked.connect(self.color_clicked)

        self.actionW.activated[str].connect(self.action_changed)
        self.degreW.textChanged.connect(self.degre_changed)
        self.apercuW.clicked.connect(self.apercu_clicked)
        self.resetW.clicked.connect(self.reset_clicked)

        self.okW.clicked.connect(self.ok_clicked)
        self.undoW.clicked.connect(self.undo_clicked)

        self.zoomInW.clicked.connect(self.zoom_in)
        self.viewer.zoomIn.connect(self.zoom_in)
        self.zoomOutW.clicked.connect(self.zoom_out)
        self.viewer.zoomOut.connect(self.zoom_out)
        self.zoomOneW.clicked.connect(self.zoom_one)

        ### args #######################################################
        if args:
            self.actionW.setCurrentIndex(args["action"])
            self.action_changed()
            self.painting.zoomN = args["zoom"]
            self.angle = args["angle"]
            self.apercu_clicked(self.angle)
        else:
            pass

        ### layout #####################################################
        toolBox = QtGui.QHBoxLayout()
        toolBox.addWidget(self.zoomInW)
        toolBox.addWidget(self.zoomOutW)
        toolBox.addWidget(self.zoomOneW)
        toolBox.addWidget(self.colorW)
        toolBox.addWidget(self.rotate90W)
        toolBox.addWidget(self.rotate270W)
        toolBox.addWidget(self.rotate180W)
        toolBox.addStretch(0)

        grid = QtGui.QGridLayout()
        grid.setSpacing(2)
        grid.addWidget(self.imW, 0, 0)
        grid.addLayout(toolBox, 1, 0, 1, 2)
        grid.addWidget(self.actionW, 0, 1)
        grid.addWidget(self.degreL, 0, 2)
        grid.addWidget(self.degreW, 0, 3)
        grid.addWidget(self.apercuW, 1, 3)
        grid.addWidget(self.resetW, 1, 2)

        ### ok, undo ###
        okBox = QtGui.QHBoxLayout()
        okBox.addStretch(0)
        okBox.addWidget(self.okW)
        okBox.addWidget(self.undoW)

        ### layout ###
        layout = QtGui.QVBoxLayout()
        layout.setSpacing(2)
        layout.addLayout(grid)
        layout.addWidget(self.viewer)
        layout.addLayout(okBox)

        self.setLayout(layout)
        self.exec_()

    def zoom_in(self):
        self.painting.zoom(2.0)

    def zoom_out(self):
        self.painting.zoom(0.5)

    def zoom_one(self):
        self.painting.zoom(0)

    def im_changed(self, text):
        im = self.images[self.imW.currentIndex()]
        self.painting.change_image(im, self.codeBefore)

    def color_clicked(self):
        color = QtGui.QColorDialog.getColor(self.color)
        if color.isValid():
            self.color = color
            self.colorIcon.fill(self.color)
            self.colorW.setIcon(QtGui.QIcon(self.colorIcon))
            self.painting.color = self.color
        self.painting.draw()

    def rot90(self):
        self.apercu_clicked(self.angle + 90)

    def rot180(self):
        self.apercu_clicked(self.angle + 180)

    def rot270(self):
        self.apercu_clicked(self.angle + 270)

    def action_changed(self, text=None):
        if self.actionW.currentIndex() == 0:
            self.degreW.setDisabled(True)
            self.painting.fig.setDisabled(False)
        elif self.actionW.currentIndex() == 1:
            self.degreW.setDisabled(True)
            self.painting.fig.setDisabled(False)
        elif self.actionW.currentIndex() == 2:
            self.degreW.setDisabled(False)
            self.painting.fig.setDisabled(True)
        self.painting.draw()

    def apercu_clicked(self, angle=False):
        angle = angle or self.return_angle()
        self.angle = self.display_deg(angle)
        self.degreW.setText(str(self.angle))
        self.rotate()

    def return_angle(self):
        angle = self.angle
        if self.actionW.currentIndex() == 0 and self.painting.fig.visible:
                x1, x2 = self.painting.fig.x1, self.painting.fig.x2
                y1, y2 = self.painting.fig.y1, self.painting.fig.y2
                hypotenuse = math.sqrt(((x2 - x1)*(x2 - x1)) + ((y1 - y2)*(y1 - y2)))
                cos = (x2 - x1) / hypotenuse
                angle = math.degrees(math.acos(cos))
                if y2 < y1 :
                    angle = -angle
                if x2 < x1 :
                    angle = angle - 180
                angle = self.angle - angle

        elif self.actionW.currentIndex() == 1 and self.painting.fig.visible:
            if self.painting.fig.visible:
                x1, x2 = self.painting.fig.x1, self.painting.fig.x2
                y1, y2 = self.painting.fig.y1, self.painting.fig.y2
                hypotenuse = math.sqrt(((x2 - x1)*(x2 - x1)) + ((y1 - y2)*(y1 - y2)))
                cos = (y2 - y1) / hypotenuse
                angle = math.degrees(math.acos(cos))
                if x1 < x2 :
                    angle = -angle
                if y2 < y1 :
                    angle = angle - 180
                angle = self.angle - angle

        elif self.actionW.currentIndex() == 2:
            angle = float(self.degreW.text())
        return angle

    def rotate(self):
        if self.angle != 0:
            code = """
matrix = QtGui.QMatrix()
matrix.rotate(%s)
$i = $i.transformed(matrix, QtCore.Qt.FastTransformation)""" %(self.angle,)
            self.painting.fig.hide()
            self.painting.apply_rotation(code)
        else:
            self.painting.reset()

    def reset_clicked(self):
        self.painting.reset()
        self.degreW.setText("0")
        self.angle = 0

    def display_deg(self, angle=0):
        while angle >= 360:
            angle -= 360
        while angle <= -360:
            angle += 360
        return angle

    def degre_changed(self):
        pass

    def ok_clicked(self):
        self.accept()

    def undo_clicked(self):
        self.reject()

    def get_return(self):
        if self.result():
            self.angle = self.display_deg(self.return_angle())
            if self.angle != 0:
                code = """
matrix = QtGui.QMatrix()
matrix.rotate(%s)
$i = $i.transformed(matrix, QtCore.Qt.SmoothTransformation)""" %(self.angle,)
            else:
                code = ""
            desc = "angle = %s" %(self.angle,)
            args = {"action": self.actionW.currentIndex(),
                    "color": self.color,
                    "zoom": self.painting.zoomN,
                    "angle": self.angle,}
            return True , code, desc, args
        else:
            return False, None, None, None
Example #2
0
class ExecDialog(QtGui.QDialog):
    def __init__(self, images, args=None, code="", parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.setWindowTitle("crop")
        self.parent = parent
        self.codeBefore = code

        ### widget #####################################################
        ### image ###
        self.imW = QtGui.QComboBox(self)
        self.images = []
        for i, v in enumerate(images):
            f = os.path.split(v)[1]
            self.imW.addItem(f)
            self.images.append(v)

        ### effacer ###
        self.eraseW = QtGui.QToolButton(self)
        self.eraseW.setAutoRaise(True)
        self.eraseW.setIcon(QtGui.QIcon(QtGui.QPixmap('icons/black_eraser.svg')))

        ### couleur ###
        if args:
            self.color = args[4]
        else:
            self.color = QtGui.QColor(0, 0, 0)
        self.colorIcon = QtGui.QPixmap(22, 22)
        self.colorIcon.fill(self.color)

        self.colorW = QtGui.QToolButton(self)
        self.colorW.setAutoRaise(True)
        self.colorW.setIcon(QtGui.QIcon(self.colorIcon))

        ### zoom buttons ###
        self.zoomInW = QtGui.QToolButton()
        self.zoomInW.setAutoRaise(True)
        self.zoomInW.setIcon(QtGui.QIcon(QtGui.QPixmap("icons/black_zoom_in.svg")))
        self.zoomOutW = QtGui.QToolButton()
        self.zoomOutW.setAutoRaise(True)
        self.zoomOutW.setIcon(QtGui.QIcon(QtGui.QPixmap("icons/black_zoom_out.svg")))
        self.zoomOneW = QtGui.QToolButton()
        self.zoomOneW.setAutoRaise(True)
        self.zoomOneW.setIcon(QtGui.QIcon(QtGui.QPixmap("icons/black_zoom_one.svg")))

        ### labels info ###
        self.oriL = QtGui.QLabel("original size")
        self.oriWL = QtGui.QLabel("")
        self.oriHL = QtGui.QLabel("")
        self.newL = QtGui.QLabel("new size")
        self.newWL = QtGui.QLabel("")
        self.newHL = QtGui.QLabel("")

        ### action ###
        self.actionL = QtGui.QLabel("action")
        self.actionW = QtGui.QComboBox(self)
        self.actionW.addItem("ignore aspect ratio")
        self.actionW.addItem("keep aspect ratio")
        self.actionW.addItem("keep aspect ratio and resize")
        self.actionW.addItem("define width and height")

        ### w ###
        self.wL = QtGui.QLabel("width : ")
        self.wW = QtGui.QLineEdit("0")
        self.wW.setValidator(QtGui.QIntValidator(self.wW))

        ### h ###
        self.hL = QtGui.QLabel("height : ")
        self.hW = QtGui.QLineEdit("0")
        self.hW.setValidator(QtGui.QIntValidator(self.hW))

        ### edit ###
        self.editW = QtGui.QPushButton('edit', self)

        ### viewer ###
        self.painting = Painting(self)
        if args:
            self.painting.set_fig(Rect(self.painting, args[5]), self.color)
        else:
            self.painting.set_fig(Rect(self.painting, args), self.color)
        self.viewer = Viewer(self)
        self.viewer.setWidget(self.painting)

        ### apply, undo ###
        self.okW = QtGui.QPushButton('apply', self)
        self.undoW = QtGui.QPushButton('undo', self)

        ### function ###################################################
        self.im_changed(self.images[0])
        self.rect_changed()

        ### connexion ##################################################
        self.imW.activated[str].connect(self.im_changed)
        self.eraseW.clicked.connect(self.painting.fig.erase)
        self.colorW.clicked.connect(self.color_clicked)

        self.actionW.activated[str].connect(self.action_changed)
        self.wW.textChanged.connect(self.action_changed)
        self.hW.textChanged.connect(self.action_changed)
        self.editW.clicked.connect(self.edit_clicked)
        self.painting.fig.figChanged.connect(self.rect_changed)

        self.okW.clicked.connect(self.ok_clicked)
        self.undoW.clicked.connect(self.undo_clicked)

        self.zoomInW.clicked.connect(self.zoom_in)
        self.viewer.zoomIn.connect(self.zoom_in)
        self.zoomOutW.clicked.connect(self.zoom_out)
        self.viewer.zoomOut.connect(self.zoom_out)
        self.zoomOneW.clicked.connect(self.zoom_one)

        ### args #######################################################
        if args:
            self.wW.setText(str(args[1]))
            self.hW.setText(str(args[2]))
            self.actionW.setCurrentIndex(int(args[0]))
            self.painting.zoom(args[3])
        else:
            self.wW.setText(str(self.painting.imW))
            self.hW.setText(str(self.painting.imH))

        ### layout #####################################################
        toolBox = QtGui.QHBoxLayout()
        toolBox.addWidget(self.zoomInW)
        toolBox.addWidget(self.zoomOutW)
        toolBox.addWidget(self.zoomOneW)
        toolBox.addWidget(self.eraseW)
        toolBox.addWidget(self.colorW)
        toolBox.addStretch(0)

        grid = QtGui.QGridLayout()
        grid.setSpacing(2)
        grid.setColumnMinimumWidth(1, 10)
        grid.setColumnMinimumWidth(5, 10)
        grid.setColumnMinimumWidth(6, 100)
        grid.setColumnMinimumWidth(7, 100)

        grid.addWidget(self.imW, 0, 0)
        grid.addWidget(self.editW, 1, 0)
        grid.addLayout(toolBox, 2, 0)

        grid.addWidget(self.actionW, 0, 3, 1, 2)
        grid.addWidget(self.wL, 1, 3)
        grid.addWidget(self.hL, 2, 3)
        grid.addWidget(self.wW, 1, 4)
        grid.addWidget(self.hW, 2, 4)

        grid.addWidget(self.oriL, 0, 6)
        grid.addWidget(self.oriWL, 1, 6)
        grid.addWidget(self.oriHL, 2, 6)

        grid.addWidget(self.newL, 0, 7)
        grid.addWidget(self.newWL, 1, 7)
        grid.addWidget(self.newHL, 2, 7)

        ### ok, undo ###
        okBox = QtGui.QHBoxLayout()
        okBox.addStretch(0)
        okBox.addWidget(self.okW)
        okBox.addWidget(self.undoW)

        ### layout ###
        layout = QtGui.QVBoxLayout()
        layout.setSpacing(2)
        layout.addLayout(grid)
        layout.addWidget(self.viewer)
        layout.addLayout(okBox)

        self.setLayout(layout)
        self.exec_()

    def zoom_in(self):
        self.painting.zoom(2.0)

    def zoom_out(self):
        self.painting.zoom(0.5)

    def zoom_one(self):
        self.painting.zoom(0)

    def im_changed(self, text):
        im = self.images[self.imW.currentIndex()]
        self.painting.change_image(im, self.codeBefore)

    def color_clicked(self):
        self.color = QtGui.QColorDialog.getColor(self.color)
        if self.color.isValid():
            self.colorIcon.fill(self.color)
            self.colorW.setIcon(QtGui.QIcon(self.colorIcon))
            self.painting.color = self.color
        self.painting.draw()

    def action_changed(self, text=""):
        if self.hW.text() == "" or self.wW.text() == "":
            return
        #~ elif int(self.hW.text()) == 0 or int(self.wW.text()) == 0:
            #~ self.actionW.setCurrentIndex(0)
            #~ error = QtGui.QMessageBox(self)
            #~ error.setWindowTitle("error")
            #~ error.setText("The size must be greater than 0.")
            #~ error.setIcon(2)
            #~ error.exec()
            #~ return
        #~ if self.actionW.currentIndex() == 0:
            #~ self.wW.setDisabled(True)
            #~ self.hW.setDisabled(True)
        #~ else:
            #~ self.wW.setDisabled(False)
            #~ self.hW.setDisabled(False)
        if not self.painting.fig.action_changed(self.actionW.currentIndex(),
                                                    int(self.wW.text()),
                                                    int(self.hW.text())):
            self.actionW.setCurrentIndex(0)
            error = QtGui.QMessageBox(self)
            error.setWindowTitle("error")
            error.setText("The size must be greater than 0.")
            error.setIcon(2)
            error.exec()


    def edit_clicked(self):
        ok, x, y, w, h =  EditDialog(self, self.painting.fig.x,
                                           self.painting.fig.y,
                                           self.painting.fig.w,
                                           self.painting.fig.h).get_return()
        if ok:
            if w == "" or h == "":
                return
            x = int(x)
            y = int(y)
            w = int(w)
            h = int(h)

            if w == 0 or h == 0:
                self.actionW.setCurrentIndex(0)
                error = QtGui.QMessageBox(self)
                error.setWindowTitle("error")
                error.setText("The size must be greater than 0.")
                error.setIcon(2)
                error.exec()
            else:
                self.actionW.setCurrentIndex(3)
                self.wW.setText(str(w))
                self.hW.setText(str(h))
                self.painting.fig.set_xywh(x, y, w, h)

    def rect_changed(self):
        self.oriWL.setText(str(self.painting.imW))
        self.oriHL.setText(str(self.painting.imH))
        if self.actionW.currentIndex() == 2: #keep aspect ratio and resize
            self.newWL.setText(self.wW.text())
            self.newHL.setText(self.hW.text())
        else:
            self.newWL.setText(str(self.painting.fig.w or ""))
            self.newHL.setText(str(self.painting.fig.h or ""))

    def ok_clicked(self):
        self.accept()

    def undo_clicked(self):
        self.reject()

    def get_return(self):
        if self.result():
            x = self.painting.fig.x
            y = self.painting.fig.y
            w = self.painting.fig.w
            h = self.painting.fig.h
            if self.painting.fig.exist:
                code = "$i = $i.copy(%s, %s, %s, %s)" %(x, y, w, h)
                desc = """x = %s y = %s
w = %s h = %s""" %(x, y, w, h)

            else:
                code = ""
                desc = ""
            # (windows args), (fig args)
            # (action, w, h, color, x, y, w, h, zoom)
            args = (self.actionW.currentIndex(),
                     int(self.wW.text()),
                     int(self.hW.text()),
                     self.painting.zoomN,
                     self.color,
                     (x, y, w, h))
            return True , code, desc, args
        else:
            return False, None, None, None