示例#1
0
    def __init__(self, client, ldevname):
        # pylint: disable=super-with-arguments
        super(Window, self).__init__()
        parent = Path(__file__).resolve().parent
        uipath = parent.joinpath('roieditor.ui')
        uic.loadUi(uipath, self)
        self.client = client
        self.ldevname = ldevname
        params = self.client.getDeviceParams(ldevname)
        self.backgroundimage = params['backgroundimage']

        self.image = None
        self.bZoom.setChecked(True)
        self.filter1.setChecked(True)
        self.zoom = self.bZoom.isChecked()
        self.noredrawblack = False
        # pylint: disable=invalid-name
        self.tf = QTextCharFormat()
        self.polylist = []
        self.polylistindex = len(self.polylist)
        self.RoicomboBox.insertItem(0, "add")
        self.writeWKT.setEnabled(False)
        self.editRoiBtn.setEnabled(False)
        self.addPolygon()
        self.RoicomboBox.currentIndexChanged.connect(
            self.on_roi_active_changed)
        self.ineditroi = False
        self.editRoiBtn.clicked.connect(self.on_editRoiBtn_clicked)
        self.wktText.textChanged.connect(self.on_wktText_textchanged)
        self.on_readWKT_clicked()

        self.on_pollData()
示例#2
0
 def formatMessage(self, message, actions=True):
     # message is a sequence:
     # (logger, time, levelno, message, exc_text, reqid)
     fmt = None
     levelno = message[2]
     if message[0] == 'nicos':
         name = ''
     else:
         name = '%-10s: ' % message[0]
     if message[5] == '0':  # simulation result started by console
         name = '(sim) ' + name
     if levelno == ACTION:
         if actions and self._actionlabel:
             action = message[3].strip()
             if action:
                 self._actionlabel.setText('Status: ' + action)
                 self._actionlabel.show()
             else:
                 self._actionlabel.hide()
         return '', None
     elif levelno <= DEBUG:
         text = name + message[3]
         fmt = grey
     elif levelno <= INFO:
         if message[3].startswith('  > '):
             fmt = QTextCharFormat(bold)
             fmt.setAnchor(True)
             command = to_utf8(message[3][4:].strip())
             fmt.setAnchorHref('exec:' + urllib.parse.quote(command))
             return name + message[3], fmt
         text = name + message[3]
     elif levelno == INPUT:
         m = command_re.match(message[3])
         if m:
             fmt = QTextCharFormat(bold)
             fmt.setAnchor(True)
             command = to_utf8(m.group(2))
             fmt.setAnchorHref('exec:' + urllib.parse.quote(command))
             if m.group(1) != self._currentuser:
                 fmt.setForeground(QBrush(QColor('#0000C0')))
             return message[3], fmt
         m = script_re.match(message[3])
         if m:
             fmt = QTextCharFormat(bold)
             if m.group(2):
                 command = to_utf8(m.group(2))
                 fmt.setAnchor(True)
                 fmt.setAnchorHref('edit:' + urllib.parse.quote(command))
             if m.group(1) != self._currentuser:
                 fmt.setForeground(QBrush(QColor('#0000C0')))
             return message[3], fmt
         m = update_re.match(message[3])
         if m:
             fmt = QTextCharFormat(bold)
             if m.group(2):
                 command = to_utf8(m.group(2))
                 fmt.setAnchor(True)
                 fmt.setAnchorHref('edit:' + urllib.parse.quote(command))
             if m.group(1) != self._currentuser:
                 fmt.setForeground(QBrush(QColor('#006090')))
             else:
                 fmt.setForeground(QBrush(QColor('#00A000')))
             return message[3], fmt
         return message[3], bold
     elif levelno <= WARNING:
         text = levels[levelno] + ': ' + name + message[3]
         fmt = magenta
     else:
         text = levels[levelno] + self.formatImportantTime(message[1]) + \
             name + message[3]
         fmt = redbold
     if message[4] and fmt:
         # need to construct a new unique object for this
         fmt = QTextCharFormat(fmt)
         # show traceback info on click
         fmt.setAnchor(True)
         tbinfo = to_utf8(message[4])
         fmt.setAnchorHref('trace:' + urllib.parse.quote(tbinfo))
     return text, fmt
示例#3
0
class Window(QWidget):
    def __init__(self, client, ldevname):
        # pylint: disable=super-with-arguments
        super(Window, self).__init__()
        parent = Path(__file__).resolve().parent
        uipath = parent.joinpath('roieditor.ui')
        uic.loadUi(uipath, self)
        self.client = client
        self.ldevname = ldevname
        params = self.client.getDeviceParams(ldevname)
        self.backgroundimage = params['backgroundimage']

        self.image = None
        self.bZoom.setChecked(True)
        self.filter1.setChecked(True)
        self.zoom = self.bZoom.isChecked()
        self.noredrawblack = False
        # pylint: disable=invalid-name
        self.tf = QTextCharFormat()
        self.polylist = []
        self.polylistindex = len(self.polylist)
        self.RoicomboBox.insertItem(0, "add")
        self.writeWKT.setEnabled(False)
        self.editRoiBtn.setEnabled(False)
        self.addPolygon()
        self.RoicomboBox.currentIndexChanged.connect(
            self.on_roi_active_changed)
        self.ineditroi = False
        self.editRoiBtn.clicked.connect(self.on_editRoiBtn_clicked)
        self.wktText.textChanged.connect(self.on_wktText_textchanged)
        self.on_readWKT_clicked()

        self.on_pollData()
        #self.resize(rv[0],rv[1])

    # pylint: disable=C:invalid-name
    def closeEvent(self, event):
        self.deleteLater()
        event.accept()
        # pylint: disable=C:invalid-name
        # pylint: disable=W:global-statement
        global win
        win = None

    def addPolygon(self):
        self.polylist.append(polygon.Polygon())
        index = len(self.polylist)
        self.RoicomboBox.insertItem(index, str(index))
        self.polylistindex = index
        self.RoicomboBox.setCurrentIndex(self.polylistindex)

    def removePolygon(self, index):
        i = 0
        items = len(self.polylist)
        for cur in self.polylist:
            if i == index:
                self.polylist.remove(cur)
                self.RoicomboBox.removeItem(items)
                self.wktText.setPlainText("")
                break
            i = i + 1

    def on_roi_active_changed(self, value):
        if value == 0:  # ='add'
            self.addPolygon()
        self.polylistindex = self.RoicomboBox.currentIndex()
        self.on_readWKT_clicked()
        if self.polylistindex == 1:
            self.writeWKT.setEnabled(False)
            self.editRoiBtn.setEnabled(False)
        else:
            self.writeWKT.setEnabled(True)
            self.editRoiBtn.setEnabled(True)
        self.repaint()

    @pyqtSlot()
    def on_editRoiBtn_clicked(self):
        if self.ineditroi:
            return
        height, width = self.mat.shape[:2]
        if height < 2 or width < 2:
            return
        if self.polylistindex == 1:
            return
        self.ineditroi = True
        self.polylist[self.polylistindex - 1].vertices().clear()

    def on_wktText_textchanged(self):
        if not self.noredrawblack:
            self.noredrawblack = True
            txt = self.wktText.toPlainText()
            self.wktText.setCurrentCharFormat(self.tf)
            self.wktText.setPlainText(txt)
            self.wktText.update()

    @pyqtSlot()
    def on_updateBG_clicked(self):
        self.on_pollData()

    @pyqtSlot()
    def on_writeWKT_clicked(self):
        wkt = str(self.wktText.toPlainText())
        arg = 'ROI' + str(self.polylistindex - 1) + ':' + wkt
        jcurwkt = self.client.eval(self.ldevname + ".roi('" + arg + "')")
        if wkt != "":
            self.on_readWKT_clicked()

    @pyqtSlot()
    def on_readWKT_clicked(self):

        self.noredrawblack = True
        self.tf.setForeground(QBrush(Qt.red))
        self.wktText.setCurrentCharFormat(self.tf)
        arg = 'ROI' + str(self.polylistindex - 1)
        jcurwkt = self.client.eval(self.ldevname + ".roi('" + arg + "')")
        if not jcurwkt:
            self.wktText.setPlainText('')
        else:
            curwkt = json.loads(jcurwkt)
            self.wktText.setPlainText(curwkt[0])

        self.polylist[self.polylistindex - 1].readWKT(
            self.wktText.toPlainText())
        self.tf.setForeground(QBrush(Qt.black))
        self.noredrawblack = False
        self.wktText.update()

    # pylint: disable=unused-argument
    def paintEvent(self, event):
        img = self.image
        self.zoom = self.bZoom.isChecked()
        if img.width() > 256:
            self.zoom = False
            self.bZoom.setChecked(False)
        if self.zoom:
            img = self.image.scaledToWidth(self.image.width() * 2)
        painter = QPainter()
        painter.begin(self)

        painter.drawImage(self.image_frame.x(), self.image_frame.y(), img)

        i = 0
        for poly in self.polylist:
            if i + 1 == self.polylistindex:
                pen = QPen(Qt.red, 2)
            else:
                pen = QPen(Qt.black, 2)
            i = i + 1
            painter.setPen(pen)
            lastdp = None
            for qp in poly.vertices():
                qpz = copy.copy(qp)
                if self.zoom:
                    qpz.setX(qpz.x() * 2)
                    qpz.setY(qpz.y() * 2)

                adp = self.image_frame.mapToParent(qpz)
                if not lastdp is None:
                    painter.drawLine(lastdp, adp)
                lastdp = adp
        painter.end()

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Escape:
            if self.ineditroi is True:
                if self.polylistindex > 1:
                    arg = 'ROI' + str(self.polylistindex - 1) + ':'
                    jcurwkt = self.client.eval(self.ldevname + ".roi('" + arg +
                                               "')")
                    self.removePolygon(self.polylistindex - 1)
            #self.wktText.setPlainText("")
            self.ineditroi = False

    def mousePressEvent(self, event):
        if self.ineditroi:
            if event.button() == Qt.LeftButton:
                mpos = self.image_frame.mapFromParent(event.pos())
                # pylint: disable=invalid-name
                zf = 1
                if self.zoom:
                    mpos /= 2
                    zf = 2

                doprint = True
                if not (mpos.x() >= 0 and mpos.x() <= self.image.width() * zf):
                    doprint = False
                if not (mpos.y() >= 0
                        and mpos.y() <= self.image.size().height() * zf):
                    doprint = False
                if doprint:
                    self.polylist[self.polylistindex - 1].addPoint(mpos)
                    self.update()

            if event.button() == Qt.RightButton:
                self.polylist[self.polylistindex - 1].close()
                self.ineditroi = False
                wkt = self.polylist[self.polylistindex - 1].WKT()
                self.wktText.setPlainText(wkt)
                self.update()

    def on_pollData(self):
        shape = self.client.eval(self.backgroundimage + '.size')
        data = self.client.eval(self.backgroundimage + ".readArray('live')")
        #print(shape)
        self.mat = data.reshape(shape)

        if shape[0] > 1 and shape[1] > 1:
            img = cv.normalize(np.int32(self.mat),
                               None,
                               0,
                               255,
                               norm_type=cv.NORM_MINMAX,
                               dtype=cv.CV_8U)
            #cv.normalize need an image with x and y > 1: It gives best
            #normalization also with high counts per pixel
        else:
            img = (self.mat * 255 / max(1, np.amax(val))).astype(np.uint8)

        if self.filter1.isChecked():
            #https://docs.opencv.org/3.4/d5/daf/tutorial_py_histogram_equalization.html
            clahe = cv.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
            cl1 = clahe.apply(img)
            img = cl1

        img = cv.applyColorMap(img, cv.COLORMAP_JET)
        self.image = img
        self.image = QImage(self.image.data, self.image.shape[0],
                            self.image.shape[1],
                            QImage.Format_RGB888).rgbSwapped()
        self.image_frame.repaint()
示例#4
0
    QTextCursor, QTextEdit
# pylint: disable=redefined-builtin
from nicos.pycompat import from_maybe_utf8, to_utf8, urllib, xrange as range
from nicos.utils.loggers import ACTION, INPUT

levels = {
    DEBUG: 'DEBUG',
    INFO: 'INFO',
    WARNING: 'WARNING',
    ERROR: 'ERROR',
    FATAL: 'FATAL'
}

# text formats for the output view

std = QTextCharFormat()

grey = QTextCharFormat()
grey.setForeground(QBrush(QColor('grey')))

red = QTextCharFormat()
red.setForeground(QBrush(QColor('red')))

magenta = QTextCharFormat()
magenta.setForeground(QBrush(QColor('#C000C0')))

bold = QTextCharFormat()
bold.setFontWeight(QFont.Bold)

redbold = QTextCharFormat()
redbold.setForeground(QBrush(QColor('red')))