xrandroll/monitor_item.py

72 lines
2.6 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 20:21:41 +00:00
super().__init__(0, 0, 0, 0)
2020-01-31 17:27:59 +00:00
self.setAcceptedMouseButtons(Qt.LeftButton)
2020-01-31 19:53:36 +00:00
self.label = QGraphicsTextItem("", self)
2020-01-31 23:42:38 +00:00
self.bottom_edge = QGraphicsRectItem(0, 0, 0, 0, self)
self.bottom_edge.setBrush(QBrush("red", Qt.SolidPattern))
2020-01-31 19:53:36 +00:00
self.update_visuals(data)
def update_visuals(self, data):
2020-01-31 20:21:41 +00:00
if data["replica_of"]:
2020-01-31 19:53:36 +00:00
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
2020-01-31 23:42:38 +00:00
if data["orientation"] in (0, 2):
self.setRect(0, 0, data["res_x"], data["res_y"])
if data["orientation"] == 0:
self.bottom_edge.setRect(
0, data["res_y"] - 50, data["res_x"], 50
)
if data["orientation"] == 2:
self.bottom_edge.setRect(0, 0, data["res_x"], 50)
else:
self.setRect(0, 0, data["res_y"], data["res_x"])
if data["orientation"] == 1:
self.bottom_edge.setRect(
data["res_y"] - 50, 0, 50, data["res_x"]
)
if data["orientation"] == 3:
self.bottom_edge.setRect(0, 0, 50, data["res_x"])
2020-01-31 20:21:41 +00:00
self.setPos(data["pos_x"], data["pos_y"])
2020-01-31 19:53:36 +00:00
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 20:21:41 +00:00
if data["primary"]:
2020-01-31 19:53:36 +00:00
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)
)