Esempio n. 1
0
class _centeringPlug:
    def __init__(
        self,
        brick,
        widgetTree,
        xoryMotor,
        zMotor,
        rMotor,
        tableyMotor,
        tablezMotor,
        table_y_inverted,
        table_z_inverted,
    ):
        self.__brick = brick
        self.__zMotor, self.__xoryMotor, self.__rotationMotor = (
            zMotor,
            xoryMotor,
            rMotor,
        )
        self.__widgetTree = widgetTree
        self.endAlignementFlag = False
        self.alignementProcedureOff = True
        self.hLines = []
        self.motorReadyFlag = False
        self.bx = None
        self.by = None
        self.__tableYMotor = tableyMotor
        self.__tableZMotor = tablezMotor
        self.table_y_inverted = table_y_inverted
        self.table_z_inverted = table_z_inverted

        alignementTable = self.__widgetTree.child("alignementTable")
        header = alignementTable.horizontalHeader()
        header.setLabel(0, rMotor.userName())

        phiStepLineEditor = self.__widgetTree.child("__deltaPhiCalib")
        phiStepLineEditor.setValidator(qt.QDoubleValidator(self.__widgetTree))

        ####### ALIGNMENT TYPE #######
        self.nbPoint4Alignement = 3
        self.alignementProcessState = _cancelAlignementProcess(self, self.__widgetTree)

        self.__centerRotation = None
        self.__helpLines = []

        ####### QT SIGNAL #######
        startAlignementButton = self.__widgetTree.child("__startAlignement")
        qt.QObject.connect(
            startAlignementButton, qt.SIGNAL("clicked()"), self.__startAlignement
        )

        cancelAlignementButton = self.__widgetTree.child("__cancelAlignement")
        qt.QObject.connect(
            cancelAlignementButton,
            qt.SIGNAL("clicked()"),
            self.__cancelAlignementButton,
        )

        showCenter = self.__widgetTree.child("__showCenter")
        qt.QObject.connect(showCenter, qt.SIGNAL("toggled(bool)"), self.__showCenter)

        showHelpLines = self.__widgetTree.child("__helpLines")
        qt.QObject.connect(
            showHelpLines, qt.SIGNAL("toggled(bool)"), self.__showHelpLines
        )

        ####### BRICK CONNECTION #######
        self.__brick.connect(
            self.__rotationMotor, qt.PYSIGNAL("stateChanged"), self.__updateMotorState
        )

    def __del__(self):
        self.__brick.disconnect(
            self.__rotationMotor, qt.PYSIGNAL("stateChanged"), self.__updateMotorState
        )

    def __updateMotorState(self, state):
        self.motorReadyFlag = state == self.__rotationMotor.READY

    def __startAlignement(self):
        try:
            self.alignementProcessState.begin()
        except BaseException:
            logging.getLogger().exception("_centeringPlug: could not start alignment")

    def __cancelAlignementButton(self):
        try:
            self.alignementProcessState.cancel()
        except BaseException:
            logging.getLogger().exception(
                "_centeringPlug: exception in cancel alignment"
            )

    def endAlignementDraw(self, aDrawingMgr):
        self.alignementProcessState.endDraw(aDrawingMgr)

    def endHline(self, aDrawingMgr):
        self.alignementProcessState.endHline(aDrawingMgr)

    def setView(self, view):
        self.alignementProcessState.setView(view)
        ####### CENTER DRAWING #######
        self.__centerRotation = QubPointDrawingMgr(view.canvas(), view.matrix())
        self.__centerRotation.setCanBeModify(False)
        target = QubCanvasTarget(view.canvas())
        self.__centerRotation.addDrawingObject(target)
        view.addDrawingMgr(self.__centerRotation)
        self.__centerRotation.setColor(qt.QColor("red"))

        ####### Help lines #######
        self.__helpLines = []
        for i in range(3):
            dMgr = QubPointDrawingMgr(view.canvas(), view.matrix())
            view.addDrawingMgr(dMgr)
            if self.alignementProcessState.verticalPhi():
                line = QubCanvasVLine(view.canvas())
            else:
                line = QubCanvasHLine(view.canvas())
            dMgr.addDrawingObject(line)
            if not i:
                dMgr.setCanBeModify(False)
                dMgr.setPen(qt.QPen(qt.Qt.red, 1, qt.Qt.DashLine))
            else:
                dMgr.setPen(qt.QPen(qt.Qt.red, 2))
                dMgr.setEndDrawCallBack(self.__helpLineMoved)
            self.__helpLines.append(dMgr)

    def getPhi(self):
        return self.__rotationMotor.getPosition()

    def mvPhi(self, step):
        self.__rotationMotor.moveRelative(step)

    def setVerticalPhi(self, aFlag):
        self.alignementProcessState.setVerticalPhi(aFlag)

    def __showCenter(self, actifFlag):
        if actifFlag:
            if self.endAlignementFlag:
                self.__centerRotation.show()
        else:
            self.hideCenter()

    def showCenterIfNeed(self):
        try:
            showCenterFlag = self.__widgetTree.child("__showCenter")
            if self.endAlignementFlag:
                at = self.__widgetTree.child("alignementTable")
                phis = []
                z = []
                averagePos = 0
                invertSigned = 1
                if self.__brick["clockwise"]:
                    invertSigned = -1

                for rowid in range(self.nbPoint4Alignement):
                    phiPos = str(at.text(rowid, 0))
                    phis.append(float(phiPos) * numpy.pi / 180.0 * invertSigned)

                    x = str(at.text(rowid, 1))
                    y = str(at.text(rowid, 2))
                    if self.alignementProcessState.verticalPhi():
                        z.append(float(x))
                        averagePos += float(y) / self.nbPoint4Alignement
                    else:
                        z.append(float(y))
                        averagePos += float(x) / self.nbPoint4Alignement

                rayon, alpha, offset = self.__brick.multiPointCentre(z, phis)
                xSize, ySize = self.__brick.getPixelSize()
                try:
                    stepUnit = self.__xoryMotor.getProperty("unit")
                except BaseException:
                    logging.getLogger().error("motors must have a unit field")
                    return

                xSize /= stepUnit
                ySize /= stepUnit

                if self.alignementProcessState.verticalPhi():  # VERTICAL
                    motx = rayon * numpy.sin(alpha)
                    motz = rayon * numpy.cos(alpha)
                    self.__xoryMotor.moveRelative(float(motx * xSize))
                    self.__zMotor.moveRelative(float(motz * xSize))
                    self.__centerRotation.setPoint(offset, averagePos)
                else:
                    moty = rayon * numpy.sin(alpha)
                    motz = rayon * numpy.cos(alpha)
                    self.__xoryMotor.moveRelative(float(moty * ySize))
                    self.__zMotor.moveRelative(float(motz * ySize))
                    self.__centerRotation.setPoint(averagePos, offset)

                if showCenterFlag.isOn():
                    self.__centerRotation.show()

                if self.bx is not None and self.by is not None:
                    if (
                        self.__tableYMotor is not None
                        and self.__tableZMotor is not None
                    ):
                        # move center of rotation to beam
                        x, y = self.__centerRotation.point()
                        # logging.getLogger().info("CENTER OF ROTATION IS %f,%f", x, y)
                        self.__tableYMotor.moveRelative(
                            float(
                                (self.table_y_inverted and xSize or -xSize)
                                * (x - self.bx)
                            )
                        )
                        self.__tableZMotor.moveRelative(
                            float(
                                (self.table_z_inverted and ySize or -ySize)
                                * (y - self.by)
                            )
                        )
        except BaseException:
            logging.getLogger().exception("_centeringPlug: could not center")

    def hideCenter(self):
        self.__centerRotation.hide()

    def __helpLineMoved(self, aDrawingMgr):
        if aDrawingMgr == self.__helpLines[1]:
            fx, fy = aDrawingMgr.point()
            sx, sy = self.__helpLines[2].point()
        else:
            fx, fy = aDrawingMgr.point()
            sx, sy = self.__helpLines[1].point()
        self.__helpLines[0].setPoint((fx + sx) / 2, (fy + sy) / 2)

    def __showHelpLines(self, aFlag):
        for i, line in enumerate(self.__helpLines):
            if aFlag:
                line.show()
                if not i:
                    line.setPen(qt.QPen(qt.Qt.red, 1, qt.Qt.DashLine))
                else:
                    line.setPen(qt.QPen(qt.Qt.red, 2))
            else:
                line.hide()
Esempio n. 2
0
class _centeringPlug:
    def __init__(
        self,
        brick,
        widgetTree,
        xoryMotor,
        zMotor,
        rMotor,
        tableyMotor,
        tablezMotor,
        table_y_inverted,
        table_z_inverted,
    ):
        self.__brick = brick
        self.__zMotor, self.__xoryMotor, self.__rotationMotor = (
            zMotor,
            xoryMotor,
            rMotor,
        )
        self.__widgetTree = widgetTree
        self.endAlignementFlag = False
        self.alignementProcedureOff = True
        self.hLines = []
        self.motorReadyFlag = False
        self.bx = None
        self.by = None
        self.__tableYMotor = tableyMotor
        self.__tableZMotor = tablezMotor
        self.table_y_inverted = table_y_inverted
        self.table_z_inverted = table_z_inverted

        alignementTable = self.__widgetTree.child("alignementTable")
        header = alignementTable.horizontalHeader()
        header.setLabel(0, rMotor.userName())

        phiStepLineEditor = self.__widgetTree.child("__deltaPhiCalib")
        phiStepLineEditor.setValidator(qt.QDoubleValidator(self.__widgetTree))

        ####### ALIGNMENT TYPE #######
        self.nbPoint4Alignement = 3
        self.alignementProcessState = _cancelAlignementProcess(
            self, self.__widgetTree)

        self.__centerRotation = None
        self.__helpLines = []

        ####### QT SIGNAL #######
        startAlignementButton = self.__widgetTree.child("__startAlignement")
        qt.QObject.connect(startAlignementButton, qt.SIGNAL("clicked()"),
                           self.__startAlignement)

        cancelAlignementButton = self.__widgetTree.child("__cancelAlignement")
        qt.QObject.connect(
            cancelAlignementButton,
            qt.SIGNAL("clicked()"),
            self.__cancelAlignementButton,
        )

        showCenter = self.__widgetTree.child("__showCenter")
        qt.QObject.connect(showCenter, qt.SIGNAL("toggled(bool)"),
                           self.__showCenter)

        showHelpLines = self.__widgetTree.child("__helpLines")
        qt.QObject.connect(showHelpLines, qt.SIGNAL("toggled(bool)"),
                           self.__showHelpLines)

        ####### BRICK CONNECTION #######
        self.__brick.connect(self.__rotationMotor, qt.PYSIGNAL("stateChanged"),
                             self.__updateMotorState)

    def __del__(self):
        self.__brick.disconnect(self.__rotationMotor,
                                qt.PYSIGNAL("stateChanged"),
                                self.__updateMotorState)

    def __updateMotorState(self, state):
        self.motorReadyFlag = state == self.__rotationMotor.READY

    def __startAlignement(self):
        try:
            self.alignementProcessState.begin()
        except BaseException:
            logging.getLogger().exception(
                "_centeringPlug: could not start alignment")

    def __cancelAlignementButton(self):
        try:
            self.alignementProcessState.cancel()
        except BaseException:
            logging.getLogger().exception(
                "_centeringPlug: exception in cancel alignment")

    def endAlignementDraw(self, aDrawingMgr):
        self.alignementProcessState.endDraw(aDrawingMgr)

    def endHline(self, aDrawingMgr):
        self.alignementProcessState.endHline(aDrawingMgr)

    def setView(self, view):
        self.alignementProcessState.setView(view)
        ####### CENTER DRAWING #######
        self.__centerRotation = QubPointDrawingMgr(view.canvas(),
                                                   view.matrix())
        self.__centerRotation.setCanBeModify(False)
        target = QubCanvasTarget(view.canvas())
        self.__centerRotation.addDrawingObject(target)
        view.addDrawingMgr(self.__centerRotation)
        self.__centerRotation.setColor(qt.QColor("red"))

        ####### Help lines #######
        self.__helpLines = []
        for i in range(3):
            dMgr = QubPointDrawingMgr(view.canvas(), view.matrix())
            view.addDrawingMgr(dMgr)
            if self.alignementProcessState.verticalPhi():
                line = QubCanvasVLine(view.canvas())
            else:
                line = QubCanvasHLine(view.canvas())
            dMgr.addDrawingObject(line)
            if not i:
                dMgr.setCanBeModify(False)
                dMgr.setPen(qt.QPen(qt.Qt.red, 1, qt.Qt.DashLine))
            else:
                dMgr.setPen(qt.QPen(qt.Qt.red, 2))
                dMgr.setEndDrawCallBack(self.__helpLineMoved)
            self.__helpLines.append(dMgr)

    def getPhi(self):
        return self.__rotationMotor.getPosition()

    def mvPhi(self, step):
        self.__rotationMotor.moveRelative(step)

    def setVerticalPhi(self, aFlag):
        self.alignementProcessState.setVerticalPhi(aFlag)

    def __showCenter(self, actifFlag):
        if actifFlag:
            if self.endAlignementFlag:
                self.__centerRotation.show()
        else:
            self.hideCenter()

    def showCenterIfNeed(self):
        try:
            showCenterFlag = self.__widgetTree.child("__showCenter")
            if self.endAlignementFlag:
                at = self.__widgetTree.child("alignementTable")
                phis = []
                z = []
                averagePos = 0
                invertSigned = 1
                if self.__brick["clockwise"]:
                    invertSigned = -1

                for rowid in range(self.nbPoint4Alignement):
                    phiPos = str(at.text(rowid, 0))
                    phis.append(
                        float(phiPos) * numpy.pi / 180.0 * invertSigned)

                    x = str(at.text(rowid, 1))
                    y = str(at.text(rowid, 2))
                    if self.alignementProcessState.verticalPhi():
                        z.append(float(x))
                        averagePos += float(y) / self.nbPoint4Alignement
                    else:
                        z.append(float(y))
                        averagePos += float(x) / self.nbPoint4Alignement

                rayon, alpha, offset = self.__brick.multiPointCentre(z, phis)
                xSize, ySize = self.__brick.getPixelSize()
                try:
                    stepUnit = self.__xoryMotor.getProperty("unit")
                except BaseException:
                    logging.getLogger().error("motors must have a unit field")
                    return

                xSize /= stepUnit
                ySize /= stepUnit

                if self.alignementProcessState.verticalPhi():  # VERTICAL
                    motx = rayon * numpy.sin(alpha)
                    motz = rayon * numpy.cos(alpha)
                    self.__xoryMotor.moveRelative(float(motx * xSize))
                    self.__zMotor.moveRelative(float(motz * xSize))
                    self.__centerRotation.setPoint(offset, averagePos)
                else:
                    moty = rayon * numpy.sin(alpha)
                    motz = rayon * numpy.cos(alpha)
                    self.__xoryMotor.moveRelative(float(moty * ySize))
                    self.__zMotor.moveRelative(float(motz * ySize))
                    self.__centerRotation.setPoint(averagePos, offset)

                if showCenterFlag.isOn():
                    self.__centerRotation.show()

                if self.bx is not None and self.by is not None:
                    if (self.__tableYMotor is not None
                            and self.__tableZMotor is not None):
                        # move center of rotation to beam
                        x, y = self.__centerRotation.point()
                        # logging.getLogger().info("CENTER OF ROTATION IS %f,%f", x, y)
                        self.__tableYMotor.moveRelative(
                            float((self.table_y_inverted and xSize or -xSize) *
                                  (x - self.bx)))
                        self.__tableZMotor.moveRelative(
                            float((self.table_z_inverted and ySize or -ySize) *
                                  (y - self.by)))
        except BaseException:
            logging.getLogger().exception("_centeringPlug: could not center")

    def hideCenter(self):
        self.__centerRotation.hide()

    def __helpLineMoved(self, aDrawingMgr):
        if aDrawingMgr == self.__helpLines[1]:
            fx, fy = aDrawingMgr.point()
            sx, sy = self.__helpLines[2].point()
        else:
            fx, fy = aDrawingMgr.point()
            sx, sy = self.__helpLines[1].point()
        self.__helpLines[0].setPoint((fx + sx) / 2, (fy + sy) / 2)

    def __showHelpLines(self, aFlag):
        for i, line in enumerate(self.__helpLines):
            if aFlag:
                line.show()
                if not i:
                    line.setPen(qt.QPen(qt.Qt.red, 1, qt.Qt.DashLine))
                else:
                    line.setPen(qt.QPen(qt.Qt.red, 2))
            else:
                line.hide()