def data(self, model_index, role=None): # check if data is set if not self.__items: return QVariant() # return empty QVariant if model index is unknown if not model_index.isValid() or not (0 <= model_index.row() < len(self.__items)): return QVariant() row_obj = self.__items[model_index.row()] if role == Qt.DisplayRole: return row_obj
def data(self, model_index, role=None): # check if data is set if not self.__items: return QVariant() # return empty QVariant if model index is unknown if not model_index.isValid() or not (0 <= model_index.row() < len( self.__items)): return QVariant() gene_obj = self.__items[model_index.row()] if role == Qt.DisplayRole: return gene_obj elif role == Qt.DecorationRole and self.show_icon: return self.__handle_icon(gene_obj)
def data( self, index, role, _str=str, _Qt_DisplayRole=Qt.DisplayRole, # noqa: N803 _Qt_EditRole=Qt.EditRole, _Qt_FontRole=Qt.FontRole, _Qt_ForegroundRole=Qt.ForegroundRole, _LinkRolee=LinkRole, _recognizedRoles=frozenset([ Qt.DisplayRole, Qt.EditRole, Qt.FontRole, Qt.ForegroundRole, LinkRole ]), ): if role not in _recognizedRoles: return None row, col = index.row(), index.column() if not 0 <= row <= self.rowCount(): return None row = self.mapToSourceRows(row) try: # value = self[row][col] value = self._row_instance(row, col) except IndexError: return if role == Qt.DisplayRole: return QVariant(str(value)) elif role == Qt.ToolTipRole: return QVariant(str(value)) if col == self.entrez_column_index: if role == _Qt_ForegroundRole: return self.color elif role == _Qt_FontRole: return self.font elif role == _LinkRolee: return NCBI_DETAIL_LINK.format(value)
def value(self, key, defaultValue=QVariant(), type=None): """ Returns the value for setting key. If the setting doesn't exist, returns defaultValue. """ if not _QSettings.contains(self, key): return defaultValue value = _QSettings.value(self, key) if type is not None: value = qvariant_to_py(value, type) return value
def data(self, index, role): # type: (QModelIndex, Qt.ItemDataRole) -> Any # Text formatting for various data simply requires a lot of branches. # This is much better than overengineering various formatters... # pylint: disable=too-many-branches if not index.isValid(): return None row, column = self.mapToSourceRows(index.row()), index.column() # Make sure we're not out of range if not 0 <= row <= self.n_attributes: return QVariant() attribute = self.variables[row] if role == Qt.BackgroundRole: if attribute in self.domain.attributes: return self.COLOR_FOR_ROLE[self.ATTRIBUTE] elif attribute in self.domain.metas: return self.COLOR_FOR_ROLE[self.META] elif attribute in self.domain.class_vars: return self.COLOR_FOR_ROLE[self.CLASS_VAR] elif role == Qt.TextAlignmentRole: if column == self.Columns.NAME: return Qt.AlignLeft | Qt.AlignVCenter return Qt.AlignRight | Qt.AlignVCenter output = None if column == self.Columns.ICON: if role == Qt.DecorationRole: return gui.attributeIconDict[attribute] elif column == self.Columns.NAME: if role == Qt.DisplayRole: output = attribute.name elif column == self.Columns.DISTRIBUTION: if role == Qt.DisplayRole: if isinstance(attribute, (DiscreteVariable, ContinuousVariable)): if row not in self.__distributions_cache: scene = QGraphicsScene(parent=self) histogram = Histogram( data=self.table, variable=attribute, color_attribute=self.target_var, border=(0, 0, 2, 0), border_color='#ccc', ) scene.addItem(histogram) self.__distributions_cache[row] = scene return self.__distributions_cache[row] elif column == self.Columns.CENTER: if role == Qt.DisplayRole: if isinstance(attribute, DiscreteVariable): output = self._center[row] if not np.isnan(output): output = attribute.str_val(self._center[row]) elif isinstance(attribute, TimeVariable): output = attribute.str_val(self._center[row]) else: output = self._center[row] elif column == self.Columns.DISPERSION: if role == Qt.DisplayRole: if isinstance(attribute, TimeVariable): output = format_time_diff(self._min[row], self._max[row]) else: output = self._dispersion[row] elif column == self.Columns.MIN: if role == Qt.DisplayRole: if isinstance(attribute, DiscreteVariable): if attribute.ordered: output = attribute.str_val(self._min[row]) elif isinstance(attribute, TimeVariable): output = attribute.str_val(self._min[row]) else: output = self._min[row] elif column == self.Columns.MAX: if role == Qt.DisplayRole: if isinstance(attribute, DiscreteVariable): if attribute.ordered: output = attribute.str_val(self._max[row]) elif isinstance(attribute, TimeVariable): output = attribute.str_val(self._max[row]) else: output = self._max[row] elif column == self.Columns.MISSING: if role == Qt.DisplayRole: output = '%d (%d%%)' % (self._missing[row], 100 * self._missing[row] / self.n_instances) # Consistently format the text inside the table cells # The easiest way to check for NaN is to compare with itself if output != output: # pylint: disable=comparison-with-itself output = '' # Format ∞ properly elif output in (np.inf, -np.inf): output = '%s∞' % ['', '-'][output < 0] elif isinstance(output, int): output = locale.format_string('%d', output, grouping=True) elif isinstance(output, float): output = locale.format_string('%.2f', output, grouping=True) return output
def data(self, index, role): # type: (QModelIndex, Qt.ItemDataRole) -> Any if not index.isValid(): return row, column = self.mapToSourceRows(index.row()), index.column() # Make sure we're not out of range if not 0 <= row <= self.n_attributes: return QVariant() output = None attribute = self._attributes[row] if column == self.Columns.ICON: if role == Qt.DecorationRole: return gui.attributeIconDict[attribute] elif column == self.Columns.NAME: if role == Qt.DisplayRole: output = attribute.name elif column == self.Columns.DISTRIBUTION: if role == self.DistributionRole: if isinstance(attribute, (DiscreteVariable, ContinuousVariable)): return attribute, self._data elif column == self.Columns.CENTER: if role == Qt.DisplayRole: if isinstance(attribute, DiscreteVariable): output = self._center[row] if not np.isnan(output): output = attribute.str_val(self._center[row]) else: output = self._center[row] elif column == self.Columns.DISPERSION: if role == Qt.DisplayRole: output = self._dispersion[row] elif column == self.Columns.MIN: if role == Qt.DisplayRole: if isinstance(attribute, DiscreteVariable): if attribute.ordered: output = attribute.str_val(self._min[row]) else: output = self._min[row] elif column == self.Columns.MAX: if role == Qt.DisplayRole: if isinstance(attribute, DiscreteVariable): if attribute.ordered: output = attribute.str_val(self._max[row]) else: output = self._max[row] elif column == self.Columns.MISSING: if role == Qt.DisplayRole: output = '%d (%d%%)' % ( self._missing[row], 100 * self._missing[row] / self.n_instances ) if role == Qt.BackgroundRole: if attribute in self._domain.attributes: return self.COLOR_FOR_ROLE[self.ATTRIBUTE] elif attribute in self._domain.metas: return self.COLOR_FOR_ROLE[self.META] elif attribute in self._domain.class_vars: return self.COLOR_FOR_ROLE[self.CLASS_VAR] elif role == Qt.TextAlignmentRole: if column == self.Columns.NAME: return Qt.AlignLeft | Qt.AlignVCenter return Qt.AlignRight | Qt.AlignVCenter # Consistently format the text inside the table cells # The easiest way to check for NaN is to compare with itself if output != output: output = 'NaN' # Format ∞ properly elif output in (np.inf, -np.inf): output = '%s∞' % ['', '-'][output < 0] elif isinstance(output, int): output = locale.format('%d', output, grouping=True) elif isinstance(output, float): output = locale.format('%.2f', output, grouping=True) return output
def qwrap(obj): if HAS_QVARIANT and not isinstance(obj, QVariant): return QVariant(obj) else: return obj