示例#1
0
class BlockBox(QFrame):
    """Provide the equivalent of a Tk LabelFrame: a group box that has a
    definite frame around it.
    """
    def __init__(self, parent, text, font, config):
        if config.get('frames', True):
            QFrame.__init__(self,
                            parent,
                            frameShape=QFrame.Panel,
                            frameShadow=QFrame.Raised,
                            lineWidth=2)
        else:
            QFrame.__init__(self, parent, frameShape=QFrame.NoFrame)
        self._label = None
        if text:
            self._label = QLabel(' ' + text + ' ',
                                 parent,
                                 autoFillBackground=True,
                                 font=font)
            self._label.resize(self._label.sizeHint())
            self._label.show()

        self._onlyfields = []
        self.setups = config.get('setups', None)

    def moveEvent(self, event):
        self._repos()
        return QFrame.moveEvent(self, event)

    def resizeEvent(self, event):
        self._repos()
        return QFrame.resizeEvent(self, event)

    def _repos(self):
        if self._label:
            mps = self.pos()
            msz = self.size()
            lsz = self._label.size()
            self._label.move(mps.x() + 0.5 * (msz.width() - lsz.width()),
                             mps.y() - 0.5 * lsz.height())

    def enableDisplay(self, layout, isvis):
        QFrame.setVisible(self, isvis)
        if self._label:
            self._label.setVisible(isvis)
        if not isvis:
            layout.removeWidget(self)
        else:
            layout.insertWidget(1, self)
        self.updateGeometry()
示例#2
0
文件: display.py 项目: ess-dmsc/nicos
class PictureDisplay(NicosWidget, QWidget):
    """A display widget to show a picture."""

    designer_description = 'Widget to display a picture file'

    filepath = PropDef('filepath', str, '', 'Path to the picture that should '
                       'be displayed')
    name = PropDef('name', str, '', 'Name (caption) to be displayed above '
                   'the picture')
    refresh = PropDef('refresh', int, 0, 'Interval to check for updates '
                      'in seconds')
    height = PropDef('height', int, 0)
    width = PropDef('width', int, 0)

    def __init__(self, parent=None, designMode=False, **kwds):
        QWidget.__init__(self, parent, **kwds)
        NicosWidget.__init__(self)
        self._last_mtime = None
        self.namelabel = QLabel(self)
        self.namelabel.setAlignment(Qt.AlignHCenter)
        self.piclabel = QLabel(self)
        self.piclabel.setScaledContents(True)
        layout = QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(self.piclabel, 1)
        self.setLayout(layout)

    def registerKeys(self):
        pass

    def setPicture(self):
        size = QSize(self.props['width'] * self._scale,
                     self.props['height'] * self._scale)
        if isfile(self._filePath):
            pixmap = QPixmap(self._filePath)
        else:
            pixmap = QPixmap(size)
            pixmap.fill()
        if size.isEmpty():
            self.piclabel.setPixmap(pixmap)
        else:
            self.piclabel.setPixmap(pixmap.scaled(size))
            self.piclabel.resize(self.piclabel.sizeHint())

    def updatePicture(self):
        if not isfile(self._filePath):
            return

        # on first iteration self._last_mtime is None -> always setPicture()
        mtime = getmtime(self._filePath)
        if self._last_mtime != mtime:
            self._last_mtime = mtime
            self.setPicture()

    def propertyUpdated(self, pname, value):
        NicosWidget.propertyUpdated(self, pname, value)
        if pname == 'filepath':
            self._filePath = findResource(value)
            self.setPicture()
        elif pname == 'name':
            layout = QVBoxLayout()
            if value:
                layout.addWidget(self.namelabel)
                layout.addSpacing(5)
            layout.addWidget(self.piclabel, 1)
            sip.delete(self.layout())
            self.setLayout(layout)
            self.namelabel.setText(value)
        elif pname in ('width', 'height'):
            self.setPicture()
        elif pname == 'refresh':
            if value:
                self._refreshTimer = QTimer()
                self._refreshTimer.setInterval(value * 1000)
                self._refreshTimer.timeout.connect(self.updatePicture)
                self._refreshTimer.start()
示例#3
0
 def sizeHint(self):
     if self.layout() is None:
         return QSize(24, 24)
     return QLabel.sizeHint(self)