コード例 #1
0
    def paint(self, painter, option, index):
        node = index.internalPointer()

        option = QtWidgets.QStyleOptionViewItem(option)
        self.initStyleOption(option, index)

        if (option.state & QtWidgets.QStyle.State_Selected
                and isinstance(node, (ctm.DeviceNode, htm.DeviceNode))
                and self.director.linked_mode):

            device = node.ref
            color = None

            if device.idc_conflict or device.address_conflict:
                color = QtGui.QColor('darkRed')

            elif device.has_hw and device.has_cfg:
                if device.hw.is_connected():
                    if device.config_applied is True:
                        color = QtGui.QColor('darkGreen')
                    elif device.config_applied is False:
                        color = QtGui.QColor('darkOrange')
                    # else config_applied should be None meaning "unknown"

            if color is not None:
                option.palette.setColor(QtGui.QPalette.Highlight, color)

        super(MCTreeItemDelegate, self).paint(painter, option, index)
コード例 #2
0
    def _get_palette_old(self, rf):
        pal = QtGui.QPalette(self._original_palette)

        try:
            result = rf.result()
            if isinstance(result, bm.SetResult) and not result:
                raise RuntimeError()
        except Exception:
            pal.setColor(QtGui.QPalette.Base, QtGui.QColor('red'))
            log.debug("_get_palette: Exception from result future; setting red background color")
            return pal

        if (self.write_mode == util.COMBINED
                and self.device.has_hw
                and self.device.has_cfg
                and self.profile is not None
                and self.get_write_profile().should_be_stored()):
            try:
                log.debug("_get_palette: comparing hardware and config")
                f_cfg = self.device.cfg.get_parameter(self.write_address)
                f_hw  = self.device.hw.get_parameter(self.read_address)

                if not f_cfg.done():
                    log.debug("_get_palette: adding update callback to config future")
                    f_cfg.add_done_callback(self._update_wrapper)

                if not f_hw.done():
                    log.debug("_get_palette: adding update callback to hardware future")
                    f_hw.add_done_callback(self._update_wrapper)

                if f_cfg.done() and f_hw.done():
                    cfg_value = int(f_cfg)
                    hw_value  = int(f_hw)
                    log.debug("_get_palette: both cfg and hw futures are done; ra=%d, wa=%d, cfg_value=%d, hw_value=%d",
                            self.read_address, self.write_address, cfg_value, hw_value)

                    if cfg_value != hw_value:
                        log.debug("_get_palette: ra=%d, wa=%d, cfg and hw differ; returning orange",
                                self.read_address, self.write_address)
                        pal.setColor(QtGui.QPalette.Base, QtGui.QColor('orange'))

            except (future.IncompleteFuture, KeyError,
                    util.SocketError, util.Disconnected):
                log.exception("_get_palette")

        else:
            log.debug("_get_palette: hw vs cfg condition failed; returning original palette")

        return pal
コード例 #3
0
ファイル: gui_util.py プロジェクト: flueke/mesycontrol
    def setReadOnly(self, ro):
        pal = QtGui.QPalette()
        if ro:
            pal.setColor(QtGui.QPalette.Base, QtGui.QColor('lightgrey'))
        self.setPalette(pal)

        super(NotesTextEdit, self).setReadOnly(ro)
コード例 #4
0
 def handle_exception(self, exc_type, exc_value, exc_trace):
     with QtCore.QMutexLocker(self._mutex):
         try:
             self.setTextColor(QtGui.QColor("#ff0000"))
             lines = exc_value.traceback_lines
             self.append("".join(lines).strip())
         except AttributeError:
             self.append("".join(traceback.format_exception(exc_type, exc_value, exc_trace)).strip())
         finally:
             self.setTextColor(self._original_text_color)
コード例 #5
0
 def handle_log_record(self, log_record):
     with QtCore.QMutexLocker(self._mutex):
         try:
             if log_record.levelno >= logging.ERROR:
                 self.setTextColor(QtGui.QColor("#ff0000"))
                 self.append(self.err_fmt.format(log_record), prepend_time=False)
             else:
                 self.append(self.fmt.format(log_record), prepend_time=False)
         finally:
             self.setTextColor(self._original_text_color)
コード例 #6
0
                def all_done(_):
                    has_param = any(pp.address in (self.write_address, self.read_address) for pp in f_params.result())
                    if has_param:
                        cfg_value = int(f_cfg)
                        hw_value  = int(f_hw)

                        if cfg_value != hw_value:
                            log.debug("_get_palette: ra=%d, wa=%d, cfg and hw differ; returning orange",
                                    self.read_address, self.write_address)
                            pal.setColor(QtGui.QPalette.Base, QtGui.QColor('orange'))

                    ret.set_result(pal)
コード例 #7
0
    def _get_palette(self, rf):
        pal = QtGui.QPalette(self._original_palette)

        try:
            result = rf.result()
            if isinstance(result, bm.SetResult) and not result:
                raise RuntimeError()
        except Exception:
            pal.setColor(QtGui.QPalette.Base, QtGui.QColor('red'))
            log.debug("_get_palette: Exception from result future; setting red background color")
            return future.Future().set_result(pal)

        ret = future.Future()

        if (self.write_mode == util.COMBINED
                and self.device.has_hw
                and self.device.has_cfg
                and self.profile is not None
                and self.get_write_profile().should_be_stored()):
            try:
                f_cfg    = self.device.cfg.get_parameter(self.write_address)
                f_hw     = self.device.hw.get_parameter(self.read_address)
                f_params = self.device.get_config_parameters()

                def all_done(_):
                    has_param = any(pp.address in (self.write_address, self.read_address) for pp in f_params.result())
                    if has_param:
                        cfg_value = int(f_cfg)
                        hw_value  = int(f_hw)

                        if cfg_value != hw_value:
                            log.debug("_get_palette: ra=%d, wa=%d, cfg and hw differ; returning orange",
                                    self.read_address, self.write_address)
                            pal.setColor(QtGui.QPalette.Base, QtGui.QColor('orange'))

                    ret.set_result(pal)

                future.all_done(f_cfg, f_hw, f_params).add_done_callback(all_done)
                return ret

            except (future.IncompleteFuture, KeyError,
                    util.SocketError, util.Disconnected):
                pass

        return ret.set_result(pal)
コード例 #8
0
    def data(self, column, role):
        device  = self.ref   # app_model.Device
        hw      = device.hw  # hardware_model.Device
        mrc     = device.mrc # app_model.MRC

        if column == 0 and role == Qt.DisplayRole:
            if mrc.hw is None or not mrc.hw.is_connected():
                return "%X <no mrc connection>" % device.address
            elif hw is None:
                return "%X <device not connected>" % device.address

            try:
                name = self.model.device_registry.get_device_name(hw.idc)
                data = "%s" % name
            except KeyError:
                name = None
                data = "idc=%d" % hw.idc

            return "%X %s" % (device.address, data)

        if column == COL_RC and hw is not None and mrc.hw.is_connected():
            if role == Qt.DisplayRole:
                return "RC on" if hw.rc else "RC off"
            if role == Qt.EditRole:
                return hw.rc

        if column == 0:
            if hw is not None and hw.address_conflict:
                if role == Qt.DecorationRole:
                    return QtGui.QPixmap(":/warning.png")

                if role in (Qt.ToolTipRole, Qt.StatusTipRole):
                    return "Address conflict"

            if hw is not None and self.model.linked_mode and  device.idc_conflict:
                if role in (Qt.ToolTipRole, Qt.StatusTipRole):
                    return "IDC conflict"

            if self.model.linked_mode and device.has_hw and device.has_cfg and device.hw.is_connected():
                if role in (Qt.ToolTipRole, Qt.StatusTipRole):
                    if device.config_applied is True:
                        return "Hardware matches config"
                    elif device.config_applied is False:
                        return "Hardware and config differ"

            if mrc.hw is None or mrc.hw.is_disconnected():
                if role == Qt.DecorationRole:
                    return QtGui.QPixmap(":/disconnected.png")

                if role in (Qt.ToolTipRole, Qt.StatusTipRole):
                    return "Disconnected from %s" % (mrc.get_display_url(),)

            if mrc.hw is not None and mrc.hw.is_connecting():
                if role == Qt.DecorationRole:
                    return QtGui.QPixmap(":/connecting.png")

                if role in (Qt.ToolTipRole, Qt.StatusTipRole):
                    return "Connecting to %s" % (mrc.get_display_url(),)

            if mrc.hw is not None and mrc.hw.is_connected() and not hw:
                if role == Qt.DecorationRole:
                    return QtGui.QPixmap(":/disconnected.png")

            if mrc.hw is not None and not mrc.hw.is_connected() and mrc.hw.last_connection_error is not None:
                if role == Qt.DecorationRole:
                    return QtGui.QPixmap(":/warning.png")

                if role in (Qt.ToolTipRole, Qt.StatusTipRole):
                    return "%s: %s" % (mrc.get_display_url(), mrc.hw.last_connection_error)

            if mrc.hw is not None and mrc.hw.is_connected() and hw is not None:
                if role == Qt.DecorationRole:
                    return QtGui.QPixmap(":/connected.png")

                if role in (Qt.ToolTipRole, Qt.StatusTipRole):
                    return "%s (idc=%d)" % (device.hw_profile.name, hw.idc)

        if role == Qt.BackgroundRole and self.model.linked_mode:
            if hw is not None and hw.address_conflict:
                return QtGui.QColor('red')

            if device.idc_conflict and self.model.linked_mode:
                return QtGui.QColor('red')

            if device.has_hw and device.has_cfg:
                if device.hw.is_connected():
                    if device.config_applied is True:
                        return QtGui.QColor('green')
                    if device.config_applied is False:
                        return QtGui.QColor('orange')
コード例 #9
0
    def data(self, idx, role=Qt.DisplayRole):
        if not idx.isValid() or self.device is None:
            return None

        row = idx.row()
        col = idx.column()
        hw = self.device.hw
        cfg = self.device.cfg
        profile = self.profile

        try:
            pp = profile[row]  # parameter profile
        except (IndexError, AttributeError):
            pp = None

        if role == Qt.DisplayRole:
            if col == COL_ADDRESS:
                return row

            elif col == COL_NAME and pp is not None and pp.is_named():
                return pp.name

            elif col == COL_HW_VALUE:
                if hw is None:
                    return "<device not present>"

                if hw.is_disconnected():
                    return "<not connected>"

                if hw.is_connecting():
                    return "<connecting>"

                if hw.address_conflict:
                    return "<address conflict>"

                try:
                    value = int(hw.get_parameter(row))
                    if self.display_hex_values:
                        return "0x{:04x}".format(value)
                    return value
                except future.IncompleteFuture:
                    return "<reading>"

            elif col == COL_CFG_VALUE:
                if pp is None or not pp.should_be_stored():
                    return None

                if cfg is None:
                    return "<config not present>"

                try:
                    value = int(cfg.get_parameter(row))
                    if self.display_hex_values:
                        return "0x{:04x}".format(value)
                    return value
                except future.IncompleteFuture:
                    return "<reading>"
                except KeyError:
                    return "<not set>"

            elif col in (COL_HW_UNIT_VALUE, COL_CFG_UNIT_VALUE):
                try:
                    unit = pp.units[1]  # skip the 'raw' unit at index 0
                except (IndexError, AttributeError):
                    return None

                if (col == COL_HW_UNIT_VALUE and hw is not None
                        and hw.is_connected() and not hw.address_conflict):
                    try:
                        raw = int(hw.get_parameter(row))
                    except future.IncompleteFuture:
                        return "<reading>"

                elif col == COL_CFG_UNIT_VALUE and cfg is not None:
                    if pp is None or not pp.should_be_stored() or cfg is None:
                        return None
                    try:
                        raw = int(cfg.get_parameter(row))
                    except future.IncompleteFuture:
                        return "<reading>"
                    except KeyError:
                        return None
                else:
                    return None

                value = unit.unit_value(raw)

                return "%f %s" % (value, unit.label)

        if role == Qt.EditRole:
            if col == COL_HW_VALUE:
                # should succeed as otherwise the editable flag would not be set
                return int(hw.get_parameter(row))

            if col == COL_CFG_VALUE:
                try:
                    # fails if no config exists or the parameter is not set
                    return int(cfg.get_parameter(row))
                except (IndexError, AttributeError, KeyError):
                    if pp is not None:
                        return pp.default
                    return 0

        if role == Qt.BackgroundRole:
            if (self.display_mode == util.COMBINED and hw is not None
                    and cfg is not None and pp is not None
                    and pp.should_be_stored()):
                try:
                    cfg_raw = int(cfg.get_parameter(row))
                    hw_raw = int(hw.get_parameter(row))
                    if cfg_raw != hw_raw:
                        return QtGui.QColor('orange')
                except (future.IncompleteFuture, KeyError, util.SocketError,
                        util.Disconnected):
                    pass

            if col == COL_HW_VALUE and pp is not None and pp.read_only:
                return QtGui.QColor("lightgray")

            if col == COL_CFG_VALUE and pp is not None and not pp.should_be_stored(
            ):
                return QtGui.QColor("lightgray")

        if role in (Qt.ToolTipRole, Qt.StatusTipRole):
            if pp is not None and pp.read_only:
                if col == COL_HW_VALUE:
                    return "%s=%s (read_only)" % (
                        pp.name if pp.is_named() else str(row),
                        self.data(idx, Qt.DisplayRole))

                if col == COL_CFG_VALUE:
                    return "%s (read_only)" % (pp.name
                                               if pp.is_named() else str(row))

            if col == COL_CFG_VALUE and pp is not None and not pp.should_be_stored(
            ):
                return "%s (not stored in config)" % (pp.name if pp.is_named()
                                                      else str(row))

        return None
コード例 #10
0
    def data(self, column, role):
        device = self.ref  # app_model.Device
        hw = device.hw  # hardware_model.Device
        cfg = device.cfg  # config_model.Device

        if column == 0 and role == Qt.DisplayRole:

            if cfg is None:
                return "%X <not present in setup>" % device.address

            try:
                type_name = self.model.device_registry.get_device_name(cfg.idc)
            except KeyError:
                type_name = "idc=%d" % cfg.idc

            if len(cfg.name):
                data = "%s (%s)" % (cfg.name, type_name)
            else:
                data = type_name

            if cfg.modified:
                data += "*"

            return "%X %s" % (device.address, data)

        if column == 0 and role == Qt.EditRole:
            return self.ref.cfg.name

        if column == 0:
            if hw is not None and cfg is not None and self.model.linked_mode and hw.address_conflict:
                if role in (Qt.ToolTipRole, Qt.StatusTipRole):
                    return "Address conflict"

            if hw is not None and self.model.linked_mode and device.idc_conflict:
                if role in (Qt.ToolTipRole, Qt.StatusTipRole):
                    return "IDC conflict"

            if self.model.linked_mode and device.has_hw and device.has_cfg and device.hw.is_connected(
            ):
                if role in (Qt.ToolTipRole, Qt.StatusTipRole):
                    if device.config_applied is True:
                        return "Hardware matches config"
                    elif device.config_applied is False:
                        return "Hardware and config differ"

            if cfg is not None and role in (Qt.ToolTipRole, Qt.StatusTipRole):
                data = str()
                if len(cfg.name):
                    data += cfg.name + " "

                type_name = self.model.device_registry.get_device_name(cfg.idc)
                data += "%s (idc=%d)" % (type_name, cfg.idc)

                if cfg.modified:
                    data += " (modified)"

                return data

        if role == Qt.BackgroundRole and self.model.linked_mode:
            if device.idc_conflict or device.address_conflict:
                return QtGui.QColor('red')

            if device.has_hw and device.has_cfg:
                if device.hw.is_connected():
                    if device.config_applied is True:
                        return QtGui.QColor('green')
                    if device.config_applied is False:
                        return QtGui.QColor('orange')