From 55d8522c4ba111631413e4c5925ca0b36be88ce9 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Fri, 31 Jan 2020 16:53:36 -0300 Subject: [PATCH] Disconnect replicas when moved --- main.py | 20 +++++++++++++++++--- monitor_item.py | 31 +++++++++++++++++++------------ 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 64ac814..ee3b911 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ from copy import deepcopy import subprocess import sys -from PySide2.QtCore import QFile, QObject +from PySide2.QtCore import QFile, QObject, Slot, SIGNAL from PySide2.QtUiTools import QUiLoader from PySide2.QtWidgets import QApplication, QGraphicsScene @@ -73,15 +73,26 @@ class Window(QObject): 0, monitor["res_x"], monitor["res_y"], + data=monitor, + window=self, name=name, - replica_of=monitor['replica_of'], - primary=monitor["primary"], ) mon_item.setPos(monitor["pos_x"], monitor["pos_y"]) self.scene.addItem(mon_item) monitor["item"] = mon_item self.adjust_view() + @Slot() + def monitor_moved(self): + "Update xrandr_info with new monitor positions" + for _, mon in self.xrandr_info.items(): + item = mon['item'] + mon['pos_x'] = item.x() + mon['pos_y'] = item.y() + self.set_replica_of() + for _, mon in self.xrandr_info.items(): + mon['item'].update_visuals(mon) + def adjust_view(self): self.ui.sceneView.ensureVisible(self.scene.sceneRect(), 100, 100) scale_factor = 0.7 * min( @@ -125,8 +136,11 @@ class Window(QObject): if "*" in line: print(f"Current mode for {name}: {mode_name}") self.xrandr_info[name]["current_mode"] = mode_name + self.set_replica_of() + def set_replica_of(self): for a in self.xrandr_info: + self.xrandr_info[a]['replica_of'] = [] for b in self.xrandr_info: if a !=b and is_replica_of(self.xrandr_info[a], self.xrandr_info[b]): self.xrandr_info[a]['replica_of'].append(b) diff --git a/monitor_item.py b/monitor_item.py index aa0a06d..f6c06f0 100644 --- a/monitor_item.py +++ b/monitor_item.py @@ -1,31 +1,37 @@ -from PySide2.QtCore import Qt +from PySide2.QtCore import Qt, Signal, QObject from PySide2.QtWidgets import QGraphicsRectItem, QGraphicsTextItem from PySide2.QtGui import QBrush, QPen -class MonitorItem(QGraphicsRectItem): +class MonitorItem(QGraphicsRectItem, QObject): z = 0 + + def __init__(self, *a, **kw): - primary = kw.pop('primary') - name = kw.pop('name') - replica_of=kw.pop('replica_of') + data = kw.pop("data") + self.name = kw.pop("name") + self.window = kw.pop("window") super().__init__(*a, **kw) self.setAcceptedMouseButtons(Qt.LeftButton) - if replica_of: - label_text = f"{name} [{','.join(replica_of)}]" + 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'])}]" else: - label_text = name - self.label = QGraphicsTextItem(label_text, self) + 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 primary: - self.setBrush(QBrush('#eee8d5', Qt.SolidPattern)) + if data['primary']: + self.setBrush(QBrush("#eee8d5", Qt.SolidPattern)) self.setZValue(1) else: - self.setBrush(QBrush('white', Qt.SolidPattern)) + self.setBrush(QBrush("white", Qt.SolidPattern)) self.setZValue(self.z) self.z -= 1 @@ -35,6 +41,7 @@ class MonitorItem(QGraphicsRectItem): def mouseReleaseEvent(self, event): self.setCursor(Qt.OpenHandCursor) + self.window.monitor_moved() def mouseMoveEvent(self, event): view = event.widget().parent()