xrandroll/monitor_item.py

46 lines
1.5 KiB
Python
Raw Normal View History

2020-01-31 17:27:59 +00:00
from PySide2.QtCore import Qt
from PySide2.QtWidgets import QGraphicsRectItem, QGraphicsTextItem
2020-01-31 18:54:01 +00:00
from PySide2.QtGui import QBrush, QPen
2020-01-31 17:27:59 +00:00
class MonitorItem(QGraphicsRectItem):
2020-01-31 19:29:04 +00:00
z = 0
2020-01-31 17:27:59 +00:00
def __init__(self, *a, **kw):
2020-01-31 18:54:01 +00:00
primary = kw.pop('primary')
2020-01-31 19:29:04 +00:00
name = kw.pop('name')
replica_of=kw.pop('replica_of')
2020-01-31 17:27:59 +00:00
super().__init__(*a, **kw)
self.setAcceptedMouseButtons(Qt.LeftButton)
2020-01-31 19:29:04 +00:00
if replica_of:
label_text = f"{name} [{','.join(replica_of)}]"
else:
label_text = name
self.label = QGraphicsTextItem(label_text, self)
2020-01-31 17:27:59 +00:00
label_scale = min(
self.rect().width() / self.label.boundingRect().width(),
self.rect().height() / self.label.boundingRect().height(),
)
self.label.setScale(label_scale)
2020-01-31 18:54:01 +00:00
if primary:
self.setBrush(QBrush('#eee8d5', Qt.SolidPattern))
2020-01-31 19:29:04 +00:00
self.setZValue(1)
else:
self.setBrush(QBrush('white', Qt.SolidPattern))
self.setZValue(self.z)
self.z -= 1
2020-01-31 17:27:59 +00:00
def mousePressEvent(self, event):
self.setCursor(Qt.ClosedHandCursor)
self.orig_pos = self.pos()
def mouseReleaseEvent(self, event):
self.setCursor(Qt.OpenHandCursor)
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)
)