class MonitorItem(QGraphicsRectItem, QObject): z = 0 def __init__(self, *a, **kw): data = kw.pop("data") self.name = kw.pop("name") self.window = kw.pop("window") super().__init__(0, 0, 0, 0) self.setAcceptedMouseButtons(Qt.LeftButton) self.label = QGraphicsTextItem("", self) self.bottom_edge = QGraphicsRectItem(0, 0, 0, 0, self) self.bottom_edge.setBrush(QBrush("red", Qt.SolidPattern)) self.update_visuals(data) def update_visuals(self, monitor): self.setRect(0, 0, monitor.res_x, monitor.res_y) self.setPos(monitor.pos_x, monitor.pos_y) if monitor.orientation == "normal": self.bottom_edge.setRect(0, monitor.res_y - 50, monitor.res_x, 50) elif monitor.orientation == "left": self.bottom_edge.setRect(monitor.res_x - 50, 0, 50, monitor.res_y) elif monitor.orientation == "inverted": self.bottom_edge.setRect(0, 0, monitor.res_x, 50) elif monitor.orientation == "right": self.bottom_edge.setRect(0, 0, 50, monitor.res_y) if monitor.replica_of: label_text = f"{self.name} [{','.join(monitor.replica_of)}]" else: label_text = self.name self.label.setPlainText(label_text) label_scale = min( self.rect().width() / self.label.boundingRect().width(), self.rect().height() / self.label.boundingRect().height(), ) self.label.setScale(label_scale) if monitor.enabled: if monitor.primary: color = QColor("#eee8d5") color.setAlpha(200) self.setBrush(QBrush(color, Qt.SolidPattern)) self.setZValue(1000) else: color = QColor("#ffffff") color.setAlpha(200) self.setBrush(QBrush(color, Qt.SolidPattern)) self.setZValue(self.z) self.z -= 1 self.show() else: color = QColor("#f1f1f1") color.setAlpha(200) self.setBrush(QBrush(color, Qt.SolidPattern)) self.setZValue(-1000) self.hide() def mousePressEvent(self, event): self.window.pos_label.show() self.setCursor(Qt.ClosedHandCursor) self.orig_pos = self.pos() self.window.ui.screenCombo.setCurrentText(self.name) def mouseReleaseEvent(self, event): self.setCursor(Qt.OpenHandCursor) self.window.pos_label.hide() self.window.monitor_moved() def mouseMoveEvent(self, event): snaps_x, snaps_y = self.window.possible_snaps(self.name) view = event.widget().parent() click_pos = event.buttonDownScreenPos(Qt.LeftButton) current_pos = event.screenPos() new_pos = view.mapFromScene(self.orig_pos) + current_pos - click_pos new_pos = view.mapToScene(new_pos) delta = view.mapToScene(0, 20).y() if not event.modifiers() & Qt.AltModifier: # Check for snaps x = new_pos.x() delta_x = min((abs(x - sx), i) for i, sx in enumerate(snaps_x)) if delta_x[0] < delta: # snap new_pos.setX(int(snaps_x[delta_x[1]])) y = new_pos.y() delta_y = min((abs(y - sy), i) for i, sy in enumerate(snaps_y)) if delta_y[0] < delta: # snap new_pos.setY(int(snaps_y[delta_y[1]])) self.setPos(new_pos) self.window.show_pos(int(self.pos().x()), int(self.pos().y()))
class ExecutionIcon(QGraphicsEllipseItem): """An icon to show information about the item's execution.""" _CHECK = "\uf00c" _CROSS = "\uf00d" _CLOCK = "\uf017" def __init__(self, parent): """ Args: parent (ProjectItemIcon): the parent item """ super().__init__(parent) self._parent = parent self._execution_state = "not started" self._text_item = QGraphicsTextItem(self) font = QFont('Font Awesome 5 Free Solid') self._text_item.setFont(font) parent_rect = parent.rect() self.setRect(0, 0, 0.5 * parent_rect.width(), 0.5 * parent_rect.height()) self.setPen(Qt.NoPen) # pylint: disable=undefined-variable self.normal_brush = qApp.palette().window() self.selected_brush = qApp.palette().highlight() self.setBrush(self.normal_brush) self.setAcceptHoverEvents(True) self.setFlag(QGraphicsItem.ItemIsSelectable, enabled=False) self.hide() def item_name(self): return self._parent.name() def _repaint(self, text, color): self._text_item.prepareGeometryChange() self._text_item.setPos(0, 0) self._text_item.setPlainText(text) self._text_item.setDefaultTextColor(color) size = self._text_item.boundingRect().size() dim_max = max(size.width(), size.height()) rect_w = self.rect().width() self._text_item.setScale(rect_w / dim_max) self._text_item.setPos(self.sceneBoundingRect().center() - self._text_item.sceneBoundingRect().center()) self.show() def mark_execution_wating(self): self._execution_state = "waiting for dependencies" self._repaint(self._CLOCK, QColor("orange")) def mark_execution_started(self): self._execution_state = "in progress" self._repaint(self._CHECK, QColor("orange")) def mark_execution_finished(self, success, skipped): if success: self._execution_state = "skipped" if skipped else "completed" colorname = "orange" if skipped else "green" self._repaint(self._CHECK, QColor(colorname)) else: self._execution_state = "failed" self._repaint(self._CROSS, QColor("red")) def hoverEnterEvent(self, event): tip = f"<p><b>Execution {self._execution_state}</b>. Select this item to see Console and Log messages.</p>" QToolTip.showText(event.screenPos(), tip) def hoverLeaveEvent(self, event): QToolTip.hideText()
class MonitorItem(QGraphicsRectItem, QObject): z = 0 def __init__(self, *a, **kw): data = kw.pop("data") self.name = kw.pop("name") self.window = kw.pop("window") super().__init__(0, 0, 0, 0) self.setAcceptedMouseButtons(Qt.LeftButton) self.label = QGraphicsTextItem("", self) self.bottom_edge = QGraphicsRectItem(0, 0, 0, 0, self) self.bottom_edge.setBrush(QBrush("red", Qt.SolidPattern)) self.update_visuals(data) def update_visuals(self, data): self.setRect(0, 0, data["res_x"], data["res_y"]) self.setPos(data["pos_x"], data["pos_y"]) if data["orientation"] == 0: self.bottom_edge.setRect(0, data["res_y"] - 50, data["res_x"], 50) elif data["orientation"] == 1: self.bottom_edge.setRect(data["res_x"] - 50, 0, 50, data["res_y"]) elif data["orientation"] == 2: self.bottom_edge.setRect(0, 0, data["res_x"], 50) elif data["orientation"] == 3: self.bottom_edge.setRect(0, 0, 50, data["res_y"]) if data["replica_of"]: label_text = f"{self.name} [{','.join(data['replica_of'])}]" else: label_text = self.name self.label.setPlainText(label_text) label_scale = min( self.rect().width() / self.label.boundingRect().width(), self.rect().height() / self.label.boundingRect().height(), ) self.label.setScale(label_scale) if data["enabled"]: if data["primary"]: color = QColor("#eee8d5") color.setAlpha(200) self.setBrush(QBrush(color, Qt.SolidPattern)) self.setZValue(1000) else: color = QColor("#ffffff") color.setAlpha(200) self.setBrush(QBrush(color, Qt.SolidPattern)) self.setZValue(self.z) self.z -= 1 else: color = QColor("#f1f1f1") color.setAlpha(200) self.setBrush(QBrush(color, Qt.SolidPattern)) self.setZValue(-1000) if not data["current_mode"]: # Disconnected or disabled self.hide() else: self.show() def mousePressEvent(self, event): self.window.pos_label.show() self.setCursor(Qt.ClosedHandCursor) self.orig_pos = self.pos() self.window.ui.screenCombo.setCurrentText(self.name) def mouseReleaseEvent(self, event): self.setCursor(Qt.OpenHandCursor) self.window.monitor_moved() self.window.pos_label.hide() def mouseMoveEvent(self, event): view = event.widget().parent() click_pos = event.buttonDownScreenPos(Qt.LeftButton) current_pos = event.screenPos() self.setPos( view.mapToScene( view.mapFromScene(self.orig_pos) + current_pos - click_pos)) self.window.show_pos(int(self.pos().x()), int(self.pos().y()))