def __init__(self, pixmap, title="", parentItem=None, **kwargs): super().__init__(parentItem, **kwargs) self.setFocusPolicy(Qt.StrongFocus) self._title = None self._size = QSizeF() layout = QGraphicsLinearLayout(Qt.Vertical, self) layout.setSpacing(2) layout.setContentsMargins(5, 5, 5, 5) self.setContentsMargins(0, 0, 0, 0) self.pixmapWidget = GraphicsPixmapWidget(pixmap, self) self.labelWidget = GraphicsTextWidget(title, self) layout.addItem(self.pixmapWidget) layout.addItem(self.labelWidget) layout.addStretch() layout.setAlignment(self.pixmapWidget, Qt.AlignCenter) layout.setAlignment(self.labelWidget, Qt.AlignHCenter | Qt.AlignBottom) self.setLayout(layout) self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.setFlag(QGraphicsItem.ItemIsSelectable, True) self.setTitle(title) self.setTitleWidth(100)
def __init__(self, pixmap, parentItem=None, crop=False, in_subset=True, **kwargs): super().__init__(parentItem, **kwargs) self.setFocusPolicy(Qt.StrongFocus) self._size = QSizeF() layout = QGraphicsLinearLayout(Qt.Vertical, self) layout.setSpacing(1) layout.setContentsMargins(5, 5, 5, 5) self.setContentsMargins(0, 0, 0, 0) self.pixmapWidget = GraphicsPixmapWidget(pixmap, self) self.pixmapWidget.setCrop(crop) self.pixmapWidget.setSubset(in_subset) self.selectionBrush = DEFAULT_SELECTION_BRUSH self.selectionPen = DEFAULT_SELECTION_PEN layout.addItem(self.pixmapWidget) layout.setAlignment(self.pixmapWidget, Qt.AlignCenter) self.setLayout(layout) self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
def __init__(self, palette, values, parent, font=None, orientation=Qt.Vertical): if orientation == Qt.Vertical: super().__init__(Qt.Horizontal) else: super().__init__(Qt.Vertical) self.__parent = parent self.__palette = palette self.__values = values self.__gradient = LegendGradient(palette, parent, orientation) self.__labels_layout = QGraphicsLinearLayout(orientation) str_vals = self._format_values(values) self.__start_label = LegendItemTitle(str_vals[0], parent, font=font) self.__end_label = LegendItemTitle(str_vals[1], parent, font=font) self.__labels_layout.addItem(self.__start_label) self.__labels_layout.addStretch(1) self.__labels_layout.addItem(self.__end_label) # Gradient should be to the left, then labels on the right if vertical if orientation == Qt.Vertical: self.addItem(self.__gradient) self.addItem(self.__labels_layout) # Gradient should be on the bottom, labels on top if horizontal elif orientation == Qt.Horizontal: self.addItem(self.__labels_layout) self.addItem(self.__gradient)
def __init__(self, pixmap, parentItem=None, crop=False, in_subset=True, add_label=False, text="", **kwargs): super().__init__(parentItem, **kwargs) self.setFocusPolicy(Qt.StrongFocus) self._size = QSizeF() layout = QGraphicsLinearLayout(Qt.Vertical, self) layout.setSpacing(1) layout.setContentsMargins(5, 5, 5, 5) self.setContentsMargins(0, 0, 0, 0) self.pixmapWidget = GraphicsPixmapWidget(pixmap, self) self.pixmapWidget.setCrop(crop) self.pixmapWidget.setSubset(in_subset) self.selectionBrush = DEFAULT_SELECTION_BRUSH self.selectionPen = DEFAULT_SELECTION_PEN layout.addItem(self.pixmapWidget) self.label = None if add_label: l1 = ElidedLabel(text) l1.setStyleSheet("background-color: rgba(255, 255, 255, 10);") l1.setAlignment(Qt.AlignCenter) l1.setFixedHeight(16) self.label = l1 gs = QGraphicsScene() w = gs.addWidget(l1) layout.addItem(w) layout.setAlignment(self.pixmapWidget, Qt.AlignCenter) self.setLayout(layout) self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
def _draw_histogram(self): if self.distributions.ndim > 1: largest_bin_count = self.distributions.sum(axis=1).max() else: largest_bin_count = self.distributions.max() bar_size = self._plot_width / self.n_bins for distr, bin_colors in zip(self.distributions, self._get_colors()): bin_count = distr.sum() bar_height = bin_count / largest_bin_count * self._plot_height bar_layout = QGraphicsLinearLayout(Qt.Vertical) bar_layout.setSpacing(0) bar_layout.addStretch() self.__layout.addItem(bar_layout) bar = ProportionalBarItem( distribution=distr, colors=bin_colors, height=bar_height, bar_size=bar_size, ) bar_layout.addItem(bar) self.layout()
def _setup_layout(self): self._clear_layout() self._layout = QGraphicsLinearLayout(self.orientation) self._layout.setContentsMargins(10, 5, 10, 5) # If horizontal, there needs to be horizontal space between the items if self.orientation == Qt.Horizontal: self._layout.setSpacing(10) # If vertical spacing, vertical space is provided by child layouts else: self._layout.setSpacing(0) self.setLayout(self._layout)
def __init__(self, parent=None, direction=Qt.LeftToRight, node=None, icon=None, iconSize=None, **args): super().__init__(parent, **args) self.setAcceptedMouseButtons(Qt.NoButton) self.__direction = direction self.setLayout(QGraphicsLinearLayout(Qt.Horizontal)) # Set the maximum size, otherwise the layout can't grow beyond its # sizeHint (and we need it to grow so the widget can grow and keep the # contents centered vertically. self.layout().setMaximumSize(QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)) self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding) self.__iconSize = iconSize or QSize(64, 64) self.__icon = icon self.__iconItem = QGraphicsPixmapItem(self) self.__iconLayoutItem = GraphicsItemLayoutItem(item=self.__iconItem) self.__channelLayout = QGraphicsGridLayout() self.channelAnchors = [] if self.__direction == Qt.LeftToRight: self.layout().addItem(self.__iconLayoutItem) self.layout().addItem(self.__channelLayout) channel_alignemnt = Qt.AlignRight else: self.layout().addItem(self.__channelLayout) self.layout().addItem(self.__iconLayoutItem) channel_alignemnt = Qt.AlignLeft self.layout().setAlignment(self.__iconLayoutItem, Qt.AlignCenter) self.layout().setAlignment(self.__channelLayout, Qt.AlignVCenter | channel_alignemnt) self.node: Optional[SchemeNode] = None self.channels: Union[List[InputSignal], List[OutputSignal]] = [] if node is not None: self.setSchemeNode(node)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setAcceptedMouseButtons(Qt.LeftButton | Qt.RightButton) self.source = None self.sink = None # QGraphicsWidget/Items in the scene. self.sourceNodeWidget = None self.sourceNodeTitle = None self.sinkNodeWidget = None self.sinkNodeTitle = None self.__links = [] # type: List[IOPair] self.__textItems = [] self.__iconItems = [] self.__tmpLine = None self.__dragStartItem = None self.setLayout(QGraphicsLinearLayout(Qt.Vertical)) self.layout().setContentsMargins(0, 0, 0, 0)
def _draw_histogram(self): # In case the data for the variable were all NaNs, then the # distributions will be empty, and we don't need to display any bars if self.x.size == 0: return # In case we have a target var, but the values are all NaNs, then there # is no sense in displaying anything if self.target_var: y_nn = self.y[~np.isnan(self.y)] if y_nn.size == 0: return if self.distributions.ndim > 1: largest_bin_count = self.distributions.sum(axis=1).max() else: largest_bin_count = self.distributions.max() bar_size = self._plot_width / self.n_bins for distr, bin_colors in zip(self.distributions, self._get_colors()): bin_count = distr.sum() bar_height = bin_count / largest_bin_count * self._plot_height bar_layout = QGraphicsLinearLayout(Qt.Vertical) bar_layout.setSpacing(0) bar_layout.addStretch() self.__layout.addItem(bar_layout) bar = ProportionalBarItem( # pylint: disable=blacklisted-name distribution=distr, colors=bin_colors, height=bar_height, bar_size=bar_size, ) bar_layout.addItem(bar) self.layout()
def __init__(self): super().__init__() self.__height = None # type: int self.__range = None # type: Tuple[float, float] self.__layout = QGraphicsLinearLayout() self.__layout.setOrientation(Qt.Horizontal) self.__layout.setSpacing(self.SPACING) self.__layout.setContentsMargins(self.HMARGIN, self.VMARGIN, self.HMARGIN, self.VMARGIN) self.setLayout(self.__layout) self.__stripe_item = StripeItem(self) self.__left_axis = AxisItem([ self.__stripe_item.model_output_ind, self.__stripe_item.base_value_ind ], parent=self, orientation="left", maxTickLength=7, pen=QPen(Qt.black)) self.__layout.addItem(self.__left_axis) self.__layout.addItem(self.__stripe_item)
def __init__(self, data, variable, parent=None, height=200, width=300, side_padding=5, top_padding=20, bottom_padding=0, bar_spacing=4, border=0, border_color=None, color_attribute=None, n_bins=10): super().__init__(parent) self.height, self.width = height, width self.padding = side_padding self.bar_spacing = bar_spacing self.data = data self.attribute = data.domain[variable] self.x = data.get_column_view(self.attribute)[0].astype(np.float64) self.x_nans = np.isnan(self.x) self.x = self.x[~self.x_nans] if self.attribute.is_discrete: self.n_bins = len(self.attribute.values) elif self.attribute.is_continuous: # If the attribute is continuous but contains fewer values than the # bins, it is better to assign each their own bin. We will require # at least 2 bins so that the histogram still visually makes sense # except if there is only a single value, then we use 3 bins for # symmetry num_unique = ut.nanunique(self.x).shape[0] if num_unique == 1: self.n_bins = 3 else: self.n_bins = min(max(2, num_unique), n_bins) # Handle target variable index self.color_attribute = color_attribute if self.color_attribute is not None: self.target_var = data.domain[color_attribute] self.y = data.get_column_view(color_attribute)[0] self.y = self.y[~self.x_nans] if not np.issubdtype(self.y.dtype, np.number): self.y = self.y.astype(np.float64) else: self.target_var, self.y = None, None # Borders self.border_color = border_color if border_color is not None else '#000' if isinstance(border, tuple): assert len(border) == 4, 'Border tuple must be of size 4.' self.border = border else: self.border = (border, border, border, border) t, r, b, l = self.border def _draw_border(point_1, point_2, border_width, parent): pen = QPen(QColor(self.border_color)) pen.setCosmetic(True) pen.setWidth(border_width) line = QGraphicsLineItem(QLineF(point_1, point_2), parent) line.setPen(pen) return line top_left = QPointF(0, 0) bottom_left = QPointF(0, self.height) top_right = QPointF(self.width, 0) bottom_right = QPointF(self.width, self.height) self.border_top = _draw_border(top_left, top_right, t, self) if t else None self.border_bottom = _draw_border(bottom_left, bottom_right, b, self) if b else None self.border_left = _draw_border(top_left, bottom_left, l, self) if l else None self.border_right = _draw_border(top_right, bottom_right, r, self) if r else None # _plot_`dim` accounts for all the paddings and spacings self._plot_height = self.height self._plot_height -= top_padding + bottom_padding self._plot_height -= t / 4 + b / 4 self._plot_width = self.width self._plot_width -= 2 * side_padding self._plot_width -= (self.n_bins - 2) * bar_spacing self._plot_width -= l / 4 + r / 4 self.__layout = QGraphicsLinearLayout(Qt.Horizontal, self) self.__layout.setContentsMargins(side_padding + r / 2, top_padding + t / 2, side_padding + l / 2, bottom_padding + b / 2) self.__layout.setSpacing(bar_spacing) # If the data contains any non-NaN values, we can draw a histogram if self.x.size > 0: self.edges, self.distributions = self._histogram() self._draw_histogram()
def __init__(self): super().__init__() self._items = [] self.layout = QGraphicsLinearLayout(Qt.Vertical) self.setLayout(self.layout)