class CameraCalibrationBrick(BlissWidget):
    def __init__(self, parent, name):
        BlissWidget.__init__(self, parent, name)

        """
        variables
        """
        self.firstTime = True
        self.YCalib  = None
        self.ZCalib  = None
        self.calibration = 0
        self.drawing = None
        self.y1 = 0
        self.z1 = 0
        self.y2 = 0
        self.z2 = 0
        self.drawingMgr = None

        """
        property
        """
        self.addProperty("zoom", "string", "")
        self.addProperty("vertical motor", "string", "")
        self.addProperty("horizontal motor", "string", "")
        self.hwZoom = None
        self.hmot   = None
        self.vmot   = None

        """
        signals
        """
        self.defineSignal('getView',())
        self.defineSignal("ChangePixelCalibration", ())

        """
        slots
        """
        self.defineSlot('getCalibration',())
        self.defineSlot('useExpertMode',())

        self.buildInterface()

    def buildInterface(self):
        vlayout = qt.QVBoxLayout(self)

        title = qt.QLabel("<B>Pixel Size Calibration<B>", self)
        title.setSizePolicy(qt.QSizePolicy(qt.QSizePolicy.Expanding,
                                           qt.QSizePolicy.Fixed))
        vlayout.addWidget(title)

        self.calibFrame = qt.QFrame(self)
        self.calibFrame.setFrameShape(qt.QFrame.Box)
        self.calibFrame.setFrameShadow(qt.QFrame.Sunken)
        vlayout.addWidget(self.calibFrame)

        vlayout1 = qt.QVBoxLayout(self.calibFrame)
        vlayout1.setMargin(10)

        self.table = qttable.QTable(0,3, self.calibFrame)
        self.table.setFocusPolicy(qt.QWidget.NoFocus)
        self.table.setSelectionMode(qttable.QTable.NoSelection)
        self.table.setSizePolicy(qt.QSizePolicy(qt.QSizePolicy.Minimum,
                                                qt.QSizePolicy.Minimum))
        self.table.verticalHeader().hide()
        self.table.setLeftMargin(0)
        self.table.setReadOnly(True)

        header = self.table.horizontalHeader()
        header.setLabel(0, "Zoom")
        header.setLabel(1, "Y Size (nm)")
        header.setLabel(2, "Z Size (nm)")

        self.table.clearSelection(True)

        vlayout1.addWidget(self.table)

        vlayout1.addSpacing(10)
        f1 = qt.QFrame(self.calibFrame)
        f1.setFrameShape(qt.QFrame.HLine)
        f1.setFrameShadow(qt.QFrame.Sunken)
        vlayout1.addWidget(f1)
        vlayout1.addSpacing(10)

        self.saveButton = qt.QPushButton("Save Current Calibration",
                                         self.calibFrame)
        self.connect(self.saveButton, qt.SIGNAL("clicked()"),
                     self.saveCalibration)
        vlayout1.addWidget(self.saveButton)

        vlayout1.addSpacing(10)
        f1 = qt.QFrame(self.calibFrame)
        f1.setFrameShape(qt.QFrame.HLine)
        f1.setFrameShadow(qt.QFrame.Sunken)
        vlayout1.addWidget(f1)
        vlayout1.addSpacing(10)

        hlayout1 = qt.QHBoxLayout(vlayout1)
        self.relYLabel = qt.QLabel("Y Move:", self.calibFrame)
        hlayout1.addWidget(self.relYLabel)
        self.relYText = qt.QLineEdit("0.1", self.calibFrame)
        hlayout1.addWidget(self.relYText)

        hlayout2 = qt.QHBoxLayout(vlayout1)
        self.relZLabel = qt.QLabel("Z Move:", self.calibFrame)
        hlayout2.addWidget(self.relZLabel)
        self.relZText = qt.QLineEdit("0.1", self.calibFrame)
        hlayout2.addWidget(self.relZText)

        vlayout1.addSpacing(5)

        self.calibButton = qt.QPushButton("Start New Calibration",
                                          self.calibFrame)
        self.connect(self.calibButton, qt.SIGNAL("clicked()"),
                     self.calibrationAction)
        vlayout1.addWidget(self.calibButton)

        vlayout1.addStretch(1)

    def propertyChanged(self, prop, oldValue, newValue):
        if prop == "zoom":
            if self.hwZoom is not None:
                self.disconnect(self.hwZoom, qt.PYSIGNAL("positionReached"),
                                self.zoomChanged)
                self.disconnect(self.hwZoom, qt.PYSIGNAL("noPosition"),
                                self.zoomChanged)

            self.hwZoom = self.getHardwareObject(newValue)

            if self.hwZoom is not None:
                self.connect(self.hwZoom, qt.PYSIGNAL("positionReached"),
                             self.zoomChanged)
                self.connect(self.hwZoom, qt.PYSIGNAL("noPosition"),
                             self.zoomChanged)

        if prop == "vertical motor":
            self.vmot = self.getHardwareObject(newValue)
            mne = self.vmot.getMotorMnemonic()
            self.relZLabel.setText("Delta on \"%s\" "%mne)
            self.vmotUnit = self.vmot.getProperty("unit")
            if self.vmotUnit is None:
                self.vmotUnit = 1e-3

        if prop == "horizontal motor":
            self.hmot = self.getHardwareObject(newValue)
            mne = self.hmot.getMotorMnemonic()
            self.relYLabel.setText("Delta on \"%s\" "%mne)
            self.hmotUnit = self.hmot.getProperty("unit")
            if self.hmotUnit is  None:
                self.hmotUnit = 1e-3

        if not self.firstTime:
            self.initInterface()
            self.zoomChanged()

    def run(self):
        if self.firstTime:
            self.initInterface()
            self.zoomChanged()

            """
            get view
            """
            view = {}
            self.emit(qt.PYSIGNAL("getView"), (view,))
            try:
                self.drawing = view["drawing"]

                cvs = self.drawing.canvas()
                matrix = self.drawing.matrix()
                drawingobject = QubCanvasTarget(cvs)
                color = self.drawing.foregroundColor()

                self.drawingMgr = QubPointDrawingMgr(cvs, matrix)
                self.drawingMgr.setDrawingEvent(QubMoveNPressed1Point)
                self.drawingMgr.setAutoDisconnectEvent(True)
                self.drawingMgr.addDrawingObject(drawingobject)
                self.drawingMgr.setEndDrawCallBack(self.pointSelected)
                qt.QWidget.connect(self.drawing,
                                   qt.PYSIGNAL("ForegroundColorChanged"),
                                   self.setColor)

                self.drawing.addDrawingMgr(self.drawingMgr)
            except:
                print "No View"

        self.firstTime = False

    def setColor(self, color):
        if self.drawingMgr is not None:
            self.drawingMgr.setColor(color)

    def initInterface(self):
        if self.hwZoom is not None:
            pos = self.hwZoom.positionsIndex
            self.table.setNumRows(len(pos))
            for i in range(len(pos)):
                aux = self.hwZoom.getPositionKeyValue(pos[i], "resox")
                if aux is None:
                    aux = "1"
                resoy = float(aux)
                aux = self.hwZoom.getPositionKeyValue(pos[i], "resoy")
                if aux is None:
                    aux = "1"
                resoz = float(aux)
                self.table.setText(i, 0, pos[i])
                """
                resolution are displayed in nanometer and saved in merter
                """
                if resoy is not None:
                    self.table.setText(i, 1, str(int(resoy * 1e9)))
                else:
                    self.table.setText(i, 1, "Not Defined")
                if resoz is not None:
                    self.table.setText(i, 2, str(int(resoz * 1e9)))
                else:
                    self.table.setText(i, 2, "Not Defined")
            self.table.adjustColumn(0)
            self.table.adjustColumn(1)
            self.table.adjustColumn(2)

    def getZoomIndex(self, position):
        if self.hwZoom is not None:
            positions = self.hwZoom.positionsIndex
            for i in range(len(positions)):
                if position == positions[i]:
                    return(i)
            return(-1)

    def zoomChanged(self):
        if self.hwZoom is not None:
            currentPos = self.hwZoom.getPosition()
            self.currIdx = self.getZoomIndex(currentPos)
            if self.table.numSelections() != -1:
                self.table.clearSelection(True)
            if self.currIdx != -1:
                aux = self.hwZoom.getPositionKeyValue(currentPos, "resox")
                if aux is None:
                    aux = "1"
                resoy = float(aux)
                aux = self.hwZoom.getPositionKeyValue(currentPos, "resoy")
                if aux is None:
                    aux = "1"
                resoz = float(aux)
                self.YCalib = resoy
                self.ZCalib = resoz
                self.table.selectRow(self.currIdx)
                if resoy is not None:
                    self.table.setText(self.currIdx, 1, str(int(resoy * 1e9)))
                else:
                    self.table.setText(self.currIdx, 1,"Not Defined")
                if resoz is not None:
                    self.table.setText(self.currIdx, 2, str(int(resoz * 1e9)))
                else:
                    self.table.setText(self.currIdx, 2, "Not Defined")
            else:
                self.YCalib = None
                self.ZCalib = None
        else:
            self.YCalib = None
            self.ZCalib = None

        self.emit(qt.PYSIGNAL("ChangePixelCalibration"),
                  (self.YCalib, self.ZCalib))

    def saveCalibration(self):
        if self.hwZoom is not None:
            currentPos = self.hwZoom.getPosition()
            self.hwZoom.setPositionKeyValue(currentPos, "resox", str(self.YCalib))
            self.hwZoom.setPositionKeyValue(currentPos, "resoy", str(self.ZCalib))
        else:
            print "CameraCalibrationBrick--ARG--hwZoom is None"

    def calibrationAction(self):
        if self.calibration == 0:

            if self.drawingMgr is not None:
                self.calibration = 1
                self.calibButton.setText("Cancel Calibration")

                self.drawingMgr.startDrawing()

        elif self.calibration == 1:
            self.calibration = 0
            self.calibButton.setText("Start New Calibration")
            self.drawingMgr.stopDrawing()
            self.drawingMgr.hide()

        elif self.calibration == 2:
            self.disconnect(self.vmot, qt.PYSIGNAL("moveDone"),
                            self.moveFinished)
            self.disconnect(self.hmot, qt.PYSIGNAL("moveDone"),
                            self.moveFinished)
            self.hmot.stop()
            self.vmot.stop()
            self.calibration = 0
            self.calibButton.setText("Start New Calibration")
            self.drawingMgr.stopDrawing()
            self.drawingMgr.hide()


    def pointSelected(self, drawingMgr):
        point = drawingMgr.point()
        if self.calibration == 1:
            self.y1 = point[0]
            self.z1 = point[1]

            ymne = self.hmot.getMotorMnemonic()
            zmne = self.vmot.getMotorMnemonic()
            self.deltaY  = float(str(self.relYText.text()))
            self.deltaZ  = float(str(self.relZText.text()))

            self.calibration = 2
            self.calibButton.setText("STOP")
            self.motorArrived = 0

            self.connect(self.hmot, qt.PYSIGNAL("moveDone"), self.moveFinished)
            self.connect(self.vmot, qt.PYSIGNAL("moveDone"), self.moveFinished)

            self.hmot.moveRelative(self.deltaY)
            self.vmot.moveRelative(self.deltaZ)

        elif self.calibration == 3:
            self.y2 = point[0]
            self.z2 = point[1]

            self.calcCalib()

            self.table.setText(self.currIdx, 1, str(int(self.YCalib * 1e9)))
            self.table.setText(self.currIdx, 2, str(int(self.ZCalib * 1e9)))

            self.calibButton.setText("Start New Calibration")
            self.calibration = 0
            self.drawingMgr.hide()

    def moveFinished(self, ver, mne):
        if mne == self.hmot.getMotorMnemonic():
            self.disconnect(self.hmot, qt.PYSIGNAL("moveDone"),
                            self.moveFinished)
            self.motorArrived = self.motorArrived + 1

        if mne == self.vmot.getMotorMnemonic():
            self.disconnect(self.vmot, qt.PYSIGNAL("moveDone"),
                            self.moveFinished)
            self.motorArrived = self.motorArrived + 1

        if self.calibration == 2 and self.motorArrived == 2:
            self.calibration = 3
            self.calibButton.setText("Cancel Calibration")
            self.drawingMgr.startDrawing()

    def calcCalib(self):
        if (abs(self.y1 - self.y2) != 0) and \
           (abs(self.z1 - self.z2) != 0):
            self.YCalib = self.deltaY * self.hmotUnit / float(self.y1 - self.y2)
            self.ZCalib = self.deltaZ * self.vmotUnit / float(self.z1 - self.z2)
            self.emit(qt.PYSIGNAL("ChangePixelCalibration"),
                      (self.YCalib, self.ZCalib))

    """
    SLOTS
    """

    def getCalibration(self, calib):
        calib["ycalib"] = self.YCalib
        calib["zcalib"] = self.ZCalib


    def useExpertMode(self, value):
        if value:
            # print "Someone ask to enable CameraCalibrationBrick."
            self.setEnabled(True)
        else:
            # print "Someone ask to disable CameraCalibrationBrick."
            self.setEnabled(False)
Exemplo 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()
class CameraCalibrationBrick(BlissWidget):
    def __init__(self, parent, name):
        BlissWidget.__init__(self, parent, name)
        """
        variables
        """
        self.firstTime = True
        self.YCalib = None
        self.ZCalib = None
        self.calibration = 0
        self.drawing = None
        self.y1 = 0
        self.z1 = 0
        self.y2 = 0
        self.z2 = 0
        self.drawingMgr = None
        """
        property
        """
        self.addProperty("zoom", "string", "")
        self.addProperty("vertical motor", "string", "")
        self.addProperty("horizontal motor", "string", "")
        self.hwZoom = None
        self.hmot = None
        self.vmot = None
        """
        signals
        """
        self.defineSignal('getView', ())
        self.defineSignal("ChangePixelCalibration", ())
        """
        slots
        """
        self.defineSlot('getCalibration', ())
        self.defineSlot('useExpertMode', ())

        self.buildInterface()

    def buildInterface(self):
        vlayout = qt.QVBoxLayout(self)

        title = qt.QLabel("<B>Pixel Size Calibration<B>", self)
        title.setSizePolicy(
            qt.QSizePolicy(qt.QSizePolicy.Expanding, qt.QSizePolicy.Fixed))
        vlayout.addWidget(title)

        self.calibFrame = qt.QFrame(self)
        self.calibFrame.setFrameShape(qt.QFrame.Box)
        self.calibFrame.setFrameShadow(qt.QFrame.Sunken)
        vlayout.addWidget(self.calibFrame)

        vlayout1 = qt.QVBoxLayout(self.calibFrame)
        vlayout1.setMargin(10)

        self.table = qttable.QTable(0, 3, self.calibFrame)
        self.table.setFocusPolicy(qt.QWidget.NoFocus)
        self.table.setSelectionMode(qttable.QTable.NoSelection)
        self.table.setSizePolicy(
            qt.QSizePolicy(qt.QSizePolicy.Minimum, qt.QSizePolicy.Minimum))
        self.table.verticalHeader().hide()
        self.table.setLeftMargin(0)
        self.table.setReadOnly(True)

        header = self.table.horizontalHeader()
        header.setLabel(0, "Zoom")
        header.setLabel(1, "Y Size (nm)")
        header.setLabel(2, "Z Size (nm)")

        self.table.clearSelection(True)

        vlayout1.addWidget(self.table)

        vlayout1.addSpacing(10)
        f1 = qt.QFrame(self.calibFrame)
        f1.setFrameShape(qt.QFrame.HLine)
        f1.setFrameShadow(qt.QFrame.Sunken)
        vlayout1.addWidget(f1)
        vlayout1.addSpacing(10)

        self.saveButton = qt.QPushButton("Save Current Calibration",
                                         self.calibFrame)
        self.connect(self.saveButton, qt.SIGNAL("clicked()"),
                     self.saveCalibration)
        vlayout1.addWidget(self.saveButton)

        vlayout1.addSpacing(10)
        f1 = qt.QFrame(self.calibFrame)
        f1.setFrameShape(qt.QFrame.HLine)
        f1.setFrameShadow(qt.QFrame.Sunken)
        vlayout1.addWidget(f1)
        vlayout1.addSpacing(10)

        hlayout1 = qt.QHBoxLayout(vlayout1)
        self.relYLabel = qt.QLabel("Y Move:", self.calibFrame)
        hlayout1.addWidget(self.relYLabel)
        self.relYText = qt.QLineEdit("0.1", self.calibFrame)
        hlayout1.addWidget(self.relYText)

        hlayout2 = qt.QHBoxLayout(vlayout1)
        self.relZLabel = qt.QLabel("Z Move:", self.calibFrame)
        hlayout2.addWidget(self.relZLabel)
        self.relZText = qt.QLineEdit("0.1", self.calibFrame)
        hlayout2.addWidget(self.relZText)

        vlayout1.addSpacing(5)

        self.calibButton = qt.QPushButton("Start New Calibration",
                                          self.calibFrame)
        self.connect(self.calibButton, qt.SIGNAL("clicked()"),
                     self.calibrationAction)
        vlayout1.addWidget(self.calibButton)

        vlayout1.addStretch(1)

    def propertyChanged(self, prop, oldValue, newValue):
        if prop == "zoom":
            if self.hwZoom is not None:
                self.disconnect(self.hwZoom, qt.PYSIGNAL("positionReached"),
                                self.zoomChanged)
                self.disconnect(self.hwZoom, qt.PYSIGNAL("noPosition"),
                                self.zoomChanged)

            self.hwZoom = self.getHardwareObject(newValue)

            if self.hwZoom is not None:
                self.connect(self.hwZoom, qt.PYSIGNAL("positionReached"),
                             self.zoomChanged)
                self.connect(self.hwZoom, qt.PYSIGNAL("noPosition"),
                             self.zoomChanged)

        if prop == "vertical motor":
            self.vmot = self.getHardwareObject(newValue)
            mne = self.vmot.getMotorMnemonic()
            self.relZLabel.setText("Delta on \"%s\" " % mne)
            self.vmotUnit = self.vmot.getProperty("unit")
            if self.vmotUnit is None:
                self.vmotUnit = 1e-3

        if prop == "horizontal motor":
            self.hmot = self.getHardwareObject(newValue)
            mne = self.hmot.getMotorMnemonic()
            self.relYLabel.setText("Delta on \"%s\" " % mne)
            self.hmotUnit = self.hmot.getProperty("unit")
            if self.hmotUnit is None:
                self.hmotUnit = 1e-3

        if not self.firstTime:
            self.initInterface()
            self.zoomChanged()

    def run(self):
        if self.firstTime:
            self.initInterface()
            self.zoomChanged()
            """
            get view
            """
            view = {}
            self.emit(qt.PYSIGNAL("getView"), (view, ))
            try:
                self.drawing = view["drawing"]

                cvs = self.drawing.canvas()
                matrix = self.drawing.matrix()
                drawingobject = QubCanvasTarget(cvs)
                color = self.drawing.foregroundColor()

                self.drawingMgr = QubPointDrawingMgr(cvs, matrix)
                self.drawingMgr.setDrawingEvent(QubMoveNPressed1Point)
                self.drawingMgr.setAutoDisconnectEvent(True)
                self.drawingMgr.addDrawingObject(drawingobject)
                self.drawingMgr.setEndDrawCallBack(self.pointSelected)
                qt.QWidget.connect(self.drawing,
                                   qt.PYSIGNAL("ForegroundColorChanged"),
                                   self.setColor)

                self.drawing.addDrawingMgr(self.drawingMgr)
            except:
                print "No View"

        self.firstTime = False

    def setColor(self, color):
        if self.drawingMgr is not None:
            self.drawingMgr.setColor(color)

    def initInterface(self):
        if self.hwZoom is not None:
            pos = self.hwZoom.positionsIndex
            self.table.setNumRows(len(pos))
            for i in range(len(pos)):
                aux = self.hwZoom.getPositionKeyValue(pos[i], "resox")
                if aux is None:
                    aux = "1"
                resoy = float(aux)
                aux = self.hwZoom.getPositionKeyValue(pos[i], "resoy")
                if aux is None:
                    aux = "1"
                resoz = float(aux)
                self.table.setText(i, 0, pos[i])
                """
                resolution are displayed in nanometer and saved in merter
                """
                if resoy is not None:
                    self.table.setText(i, 1, str(int(resoy * 1e9)))
                else:
                    self.table.setText(i, 1, "Not Defined")
                if resoz is not None:
                    self.table.setText(i, 2, str(int(resoz * 1e9)))
                else:
                    self.table.setText(i, 2, "Not Defined")
            self.table.adjustColumn(0)
            self.table.adjustColumn(1)
            self.table.adjustColumn(2)

    def getZoomIndex(self, position):
        if self.hwZoom is not None:
            positions = self.hwZoom.positionsIndex
            for i in range(len(positions)):
                if position == positions[i]:
                    return (i)
            return (-1)

    def zoomChanged(self):
        if self.hwZoom is not None:
            currentPos = self.hwZoom.getPosition()
            self.currIdx = self.getZoomIndex(currentPos)
            if self.table.numSelections() != -1:
                self.table.clearSelection(True)
            if self.currIdx != -1:
                aux = self.hwZoom.getPositionKeyValue(currentPos, "resox")
                if aux is None:
                    aux = "1"
                resoy = float(aux)
                aux = self.hwZoom.getPositionKeyValue(currentPos, "resoy")
                if aux is None:
                    aux = "1"
                resoz = float(aux)
                self.YCalib = resoy
                self.ZCalib = resoz
                self.table.selectRow(self.currIdx)
                if resoy is not None:
                    self.table.setText(self.currIdx, 1, str(int(resoy * 1e9)))
                else:
                    self.table.setText(self.currIdx, 1, "Not Defined")
                if resoz is not None:
                    self.table.setText(self.currIdx, 2, str(int(resoz * 1e9)))
                else:
                    self.table.setText(self.currIdx, 2, "Not Defined")
            else:
                self.YCalib = None
                self.ZCalib = None
        else:
            self.YCalib = None
            self.ZCalib = None

        self.emit(qt.PYSIGNAL("ChangePixelCalibration"),
                  (self.YCalib, self.ZCalib))

    def saveCalibration(self):
        if self.hwZoom is not None:
            currentPos = self.hwZoom.getPosition()
            self.hwZoom.setPositionKeyValue(currentPos, "resox",
                                            str(self.YCalib))
            self.hwZoom.setPositionKeyValue(currentPos, "resoy",
                                            str(self.ZCalib))
        else:
            print "CameraCalibrationBrick--ARG--hwZoom is None"

    def calibrationAction(self):
        if self.calibration == 0:

            if self.drawingMgr is not None:
                self.calibration = 1
                self.calibButton.setText("Cancel Calibration")

                self.drawingMgr.startDrawing()

        elif self.calibration == 1:
            self.calibration = 0
            self.calibButton.setText("Start New Calibration")
            self.drawingMgr.stopDrawing()
            self.drawingMgr.hide()

        elif self.calibration == 2:
            self.disconnect(self.vmot, qt.PYSIGNAL("moveDone"),
                            self.moveFinished)
            self.disconnect(self.hmot, qt.PYSIGNAL("moveDone"),
                            self.moveFinished)
            self.hmot.stop()
            self.vmot.stop()
            self.calibration = 0
            self.calibButton.setText("Start New Calibration")
            self.drawingMgr.stopDrawing()
            self.drawingMgr.hide()

    def pointSelected(self, drawingMgr):
        point = drawingMgr.point()
        if self.calibration == 1:
            self.y1 = point[0]
            self.z1 = point[1]

            ymne = self.hmot.getMotorMnemonic()
            zmne = self.vmot.getMotorMnemonic()
            self.deltaY = float(str(self.relYText.text()))
            self.deltaZ = float(str(self.relZText.text()))

            self.calibration = 2
            self.calibButton.setText("STOP")
            self.motorArrived = 0

            self.connect(self.hmot, qt.PYSIGNAL("moveDone"), self.moveFinished)
            self.connect(self.vmot, qt.PYSIGNAL("moveDone"), self.moveFinished)

            self.hmot.moveRelative(self.deltaY)
            self.vmot.moveRelative(self.deltaZ)

        elif self.calibration == 3:
            self.y2 = point[0]
            self.z2 = point[1]

            self.calcCalib()

            self.table.setText(self.currIdx, 1, str(int(self.YCalib * 1e9)))
            self.table.setText(self.currIdx, 2, str(int(self.ZCalib * 1e9)))

            self.calibButton.setText("Start New Calibration")
            self.calibration = 0
            self.drawingMgr.hide()

    def moveFinished(self, ver, mne):
        if mne == self.hmot.getMotorMnemonic():
            self.disconnect(self.hmot, qt.PYSIGNAL("moveDone"),
                            self.moveFinished)
            self.motorArrived = self.motorArrived + 1

        if mne == self.vmot.getMotorMnemonic():
            self.disconnect(self.vmot, qt.PYSIGNAL("moveDone"),
                            self.moveFinished)
            self.motorArrived = self.motorArrived + 1

        if self.calibration == 2 and self.motorArrived == 2:
            self.calibration = 3
            self.calibButton.setText("Cancel Calibration")
            self.drawingMgr.startDrawing()

    def calcCalib(self):
        if (abs(self.y1 - self.y2) != 0) and \
           (abs(self.z1 - self.z2) != 0):
            self.YCalib = self.deltaY * self.hmotUnit / float(self.y1 -
                                                              self.y2)
            self.ZCalib = self.deltaZ * self.vmotUnit / float(self.z1 -
                                                              self.z2)
            self.emit(qt.PYSIGNAL("ChangePixelCalibration"),
                      (self.YCalib, self.ZCalib))

    """
    SLOTS
    """

    def getCalibration(self, calib):
        calib["ycalib"] = self.YCalib
        calib["zcalib"] = self.ZCalib

    def useExpertMode(self, value):
        if value:
            # print "Someone ask to enable CameraCalibrationBrick."
            self.setEnabled(True)
        else:
            # print "Someone ask to disable CameraCalibrationBrick."
            self.setEnabled(False)
Exemplo n.º 4
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()