def _format_value(self, datum): value = self._convert_value(datum) if not self.format_number: return value if isinstance(value, (str, int, bool)): return str(value) if isinstance(value, float): if np.isnan(value): return "none" unit = datum["unit"] tolerance = datum["tolerance"] if tolerance is not None: if unit is not None: tolerance = self._change_unit(tolerance, unit) precision = tolerance_to_decimals(tolerance) return "{0:.{precision}f}".format(value, precision=precision) else: return "{:g}".format(value) return "{}".format(value)
def data(self, index, role=QtCore.Qt.DisplayRole): if not index.isValid(): return None row = index.row() column = index.column() position = self._positions[row] if role == QtCore.Qt.DisplayRole: if self.toleranceMeter() is not None: precision = tolerance_to_decimals(self.toleranceMeter()) - 9 fmt = "{0:.{precision}f}" else: fmt = "{0:g}" if column == 0: return fmt.format(position.x_m * 1e9, precision=precision) elif column == 1: return fmt.format(position.y_m * 1e9, precision=precision) elif role == QtCore.Qt.UserRole: return position elif role == QtCore.Qt.TextAlignmentRole: return QtCore.Qt.AlignCenter
def format_value(self, value, unit=_default, tolerance=_default): if tolerance is self._default: tolerance = self.tolerance if unit is self._default: unit = self.unit if unit is not None: q = pymontecarlo.unit_registry.Quantity(value, unit) q = pymontecarlo.settings.to_preferred_unit(q) value = q.magnitude if isinstance(value, float): if tolerance is not None: if unit is not None: q_tolerance = pymontecarlo.unit_registry.Quantity( tolerance, unit) q_tolerance = pymontecarlo.settings.to_preferred_unit( q_tolerance) tolerance = q_tolerance.magnitude precision = tolerance_to_decimals(tolerance) return '{0:.{precision}f}'.format(value, precision=precision) else: return '{:g}'.format(value) else: return '{}'.format(value)
def data(self, index, role=QtCore.Qt.DisplayRole): if not index.isValid() or not (0 <= index.row() < len(self._builders)): return None row = index.row() column = index.column() builder = self._builders[row] if role == QtCore.Qt.DisplayRole: if column == 0: if not builder.materials: return "none" else: return ", ".join( map(operator.attrgetter("name"), builder.materials) ) elif column == 1: if len(builder.thicknesses_m) > 0: values = np.array(sorted(builder.thicknesses_m)) * 1e9 locale = QtCore.QLocale.system() precision = tolerance_to_decimals(Layer.THICKNESS_TOLERANCE_m * 1e9) return ", ".join(locale.toString(v, "f", precision) for v in values) elif role == QtCore.Qt.TextAlignmentRole: return QtCore.Qt.AlignCenter elif role == QtCore.Qt.UserRole: return builder elif role == QtCore.Qt.BackgroundRole: if len(builder) == 0: brush = QtGui.QBrush() brush.setColor(QtGui.QColor(INVALID_COLOR)) brush.setStyle(QtCore.Qt.SolidPattern) return brush
def __init__(self): super().__init__() # Widgets self._widget = ColoredMultiFloatLineEdit() decimals = tolerance_to_decimals(BeamBase.ENERGY_TOLERANCE_eV) + 3 self._widget.setRange(0, 1000, decimals) self._widget.setValues([20.0]) # Signals self._widget.valuesChanged.connect(self.fieldChanged)
def __init__(self): super().__init__() # Widgets self._widget = ColoredMultiFloatLineEdit() decimals = tolerance_to_decimals(math.degrees(SampleBase.AZIMUTH_TOLERANCE_rad)) self._widget.setRange(0.0, 360.0, decimals) self._widget.setValues([0.0]) # Signals self._widget.valuesChanged.connect(self.fieldChanged)
def __init__(self): super().__init__() # Widgets self._widget = ColoredMultiFloatLineEdit() tolerance = VerticalLayerSample.DEPTH_TOLERANCE_m * 1e9 decimals = tolerance_to_decimals(tolerance) self._widget.setRange(tolerance, float("inf"), decimals) self._widget.setEnabled(False) self._suffix = QtWidgets.QCheckBox("infinite") self._suffix.setChecked(True) # Signals self._widget.valuesChanged.connect(self.fieldChanged) self._suffix.stateChanged.connect(self._on_infinite_changed)
def createEditor(self, parent, option, index): column = index.column() if column == 0: editor = MaterialListWidget(parent) editor.setMaximumHeight(parent.height()) editor.setMinimumSize(editor.sizeHint()) return editor elif column == 1: editor = ColoredMultiFloatLineEdit(parent) tolerance = Layer.THICKNESS_TOLERANCE_m * 1e9 decimals = tolerance_to_decimals(tolerance) editor.setRange(tolerance, float("inf"), decimals) return editor
def __init__(self): super().__init__() # Variables self._composition = {} # Widgets self._widget = ColoredFloatLineEdit() decimals = tolerance_to_decimals( Material.DENSITY_TOLERANCE_kg_per_m3) + 3 self._widget.setRange(0.0, float("inf"), decimals) self._widget.setValue(0.0) self._widget.setEnabled(False) self._suffix = QtWidgets.QCheckBox("user defined") self._suffix.setChecked(False) # Signals self._widget.valueChanged.connect(self.fieldChanged) self._suffix.stateChanged.connect(self._on_user_defined_changed)
def setEditorData(self, editor, index): model = index.model() value = model.data(index, QtCore.Qt.UserRole) column = index.column() if column == 0: zs = set(model.composition().keys()) available_zs = set(range(1, MAX_Z + 1)) - zs available_zs.add(value) editor.setAvailableAtomicNumbers(available_zs) editor.setAtomicNumber(value) elif column == 1: decimals = tolerance_to_decimals( Material.WEIGHT_FRACTION_TOLERANCE) - 2 fmt = "{{:.{}f}}".format(decimals) if value == "?": editor.setText(value) else: editor.setText(fmt.format(value * 100.0)) else: super().setEditorData(editor, index)
def setToleranceMeter(self, tolerance_m): super().setToleranceMeter(tolerance_m) decimals = tolerance_to_decimals(tolerance_m * 1e9) self._widget.setRange(tolerance_m, float("inf"), decimals)
def data(self, index, role=QtCore.Qt.DisplayRole): if not index.isValid(): return None row = index.row() column = index.column() tolerance = Material.WEIGHT_FRACTION_TOLERANCE fmt = "{{:.{:d}f}}".format(max(0, tolerance_to_decimals(tolerance) - 2)) if row < len(self._composition): z = list(self._composition.keys())[row] wf = self._composition[z] af = self._composition_atomic[z] if role == QtCore.Qt.DisplayRole: if column == 0: return pyxray.element_symbol(z) elif column == 1: if wf == "?": return wf else: return fmt.format(wf * 100.0) elif column == 2: return fmt.format(af * 100.0) elif role == QtCore.Qt.UserRole: if column == 0: return z elif column == 1: return wf elif column == 2: return af elif role == QtCore.Qt.TextAlignmentRole: return QtCore.Qt.AlignCenter else: total_wf = sum(self.composition().values()) total_af = 1.0 # Always 100% if role == QtCore.Qt.DisplayRole: if column == 0: return "Total" elif column == 1: return fmt.format(total_wf * 100.0) elif column == 2: return fmt.format(total_af * 100.0) elif role == QtCore.Qt.UserRole: if column == 1: return total_wf elif column == 2: return total_af elif role == QtCore.Qt.TextAlignmentRole: if column == 0: return QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter else: return QtCore.Qt.AlignCenter elif role == QtCore.Qt.FontRole: font = QtGui.QFont() font.setBold(True) return font elif role == QtCore.Qt.BackgroundRole: if not self.isValid(): brush = QtGui.QBrush() brush.setColor(QtGui.QColor(INVALID_COLOR)) brush.setStyle(QtCore.Qt.SolidPattern) return brush