xrandroll/monitor_item.py

53 lines
1.8 KiB
Python
Raw Normal View History

2020-01-31 19:56:38 +00:00
from PySide2.QtCore import Qt, QObject
2020-01-31 17:27:59 +00:00
from PySide2.QtWidgets import QGraphicsRectItem, QGraphicsTextItem
2020-01-31 19:56:38 +00:00
from PySide2.QtGui import QBrush
2020-01-31 17:27:59 +00:00
2020-01-31 19:53:36 +00:00
class MonitorItem(QGraphicsRectItem, QObject):
2020-01-31 19:29:04 +00:00
z = 0
2020-01-31 19:53:36 +00:00
2020-01-31 17:27:59 +00:00
def __init__(self, *a, **kw):
2020-01-31 19:53:36 +00:00
data = kw.pop("data")
self.name = kw.pop("name")
self.window = kw.pop("window")
2020-01-31 17:27:59 +00:00
super().__init__(*a, **kw)
self.setAcceptedMouseButtons(Qt.LeftButton)
2020-01-31 19:53:36 +00:00
self.label = QGraphicsTextItem("", self)
self.update_visuals(data)
def update_visuals(self, data):
if data['replica_of']:
label_text = f"{self.name} [{','.join(data['replica_of'])}]"
2020-01-31 19:29:04 +00:00
else:
2020-01-31 19:53:36 +00:00
label_text = self.name
self.label.setPlainText(label_text)
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 19:53:36 +00:00
if data['primary']:
self.setBrush(QBrush("#eee8d5", Qt.SolidPattern))
2020-01-31 19:29:04 +00:00
self.setZValue(1)
else:
2020-01-31 19:53:36 +00:00
self.setBrush(QBrush("white", Qt.SolidPattern))
2020-01-31 19:29:04 +00:00
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()
self.window.ui.screenCombo.setCurrentText(self.name)
2020-01-31 17:27:59 +00:00
def mouseReleaseEvent(self, event):
self.setCursor(Qt.OpenHandCursor)
2020-01-31 19:53:36 +00:00
self.window.monitor_moved()
2020-01-31 17:27:59 +00:00
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)
)