Support disabled monitors, other bits

This commit is contained in:
Roberto Alsina 2020-01-31 15:54:01 -03:00
parent 7a9d699399
commit d71a677ee9
3 changed files with 144 additions and 41 deletions

49
main.py
View File

@ -1,3 +1,4 @@
from copy import deepcopy
import subprocess import subprocess
import sys import sys
@ -12,10 +13,14 @@ def parse_monitor(line):
parts = line.split() parts = line.split()
name = parts[0] name = parts[0]
primary = "primary" in parts primary = "primary" in parts
w_in_mm, h_in_mm = [p.split("mm")[0] for p in parts if p.endswith("mm")] if '+' in line: # Is enabled
enabled = True
res_x, res_y = [p for p in parts if "x" in p][0].split("+")[0].split("x") res_x, res_y = [p for p in parts if "x" in p][0].split("+")[0].split("x")
pos_x, pos_y = [p for p in parts if "x" in p][0].split("+")[1:] pos_x, pos_y = [p for p in parts if "x" in p][0].split("+")[1:]
print(name, pos_x, pos_y) w_in_mm, h_in_mm = [p.split("mm")[0] for p in parts if p.endswith("mm")]
else:
enabled = False
res_x = res_y = pos_x = pos_y = w_in_mm = h_in_mm = 0
return ( return (
name, name,
primary, primary,
@ -25,6 +30,7 @@ def parse_monitor(line):
int(h_in_mm), int(h_in_mm),
int(pos_x), int(pos_x),
int(pos_y), int(pos_y),
enabled
) )
@ -34,8 +40,11 @@ class Window(QObject):
self.ui = ui self.ui = ui
ui.show() ui.show()
self.ui.screenCombo.currentTextChanged.connect(self.monitor_selected) self.ui.screenCombo.currentTextChanged.connect(self.monitor_selected)
self.ui.horizontalScale.valueChanged.connect(self.updateScaleLabels)
self.ui.verticalScale.valueChanged.connect(self.updateScaleLabels)
self.xrandr_info = {} self.xrandr_info = {}
self.get_xrandr_info() self.get_xrandr_info()
self.orig_xrandr_info = deepcopy(self.xrandr_info)
self.fill_ui() self.fill_ui()
def fill_ui(self): def fill_ui(self):
@ -46,7 +55,7 @@ class Window(QObject):
for name, monitor in self.xrandr_info.items(): for name, monitor in self.xrandr_info.items():
self.ui.screenCombo.addItem(name) self.ui.screenCombo.addItem(name)
mon_item = MonitorItem(0, 0, monitor["res_x"], monitor["res_y"], name=name) mon_item = MonitorItem(0, 0, monitor["res_x"], monitor["res_y"], name=name, primary=monitor['primary'])
mon_item.setPos(monitor["pos_x"], monitor["pos_y"]) mon_item.setPos(monitor["pos_x"], monitor["pos_y"])
self.scene.addItem(mon_item) self.scene.addItem(mon_item)
monitor["item"] = mon_item monitor["item"] = mon_item
@ -62,10 +71,11 @@ class Window(QObject):
def get_xrandr_info(self): def get_xrandr_info(self):
data = subprocess.check_output(["xrandr"]).decode("utf-8").splitlines() data = subprocess.check_output(["xrandr"]).decode("utf-8").splitlines()
outputs = [x for x in data if x and x[0] not in "S \t"] name = None
for o in outputs: for line in data:
name, primary, res_x, res_y, w_in_mm, h_in_mm, pos_x, pos_y = parse_monitor( if line and line[0] not in "S \t": # Output line
o name, primary, res_x, res_y, w_in_mm, h_in_mm, pos_x, pos_y, enabled = parse_monitor(
line
) )
self.xrandr_info[name] = dict( self.xrandr_info[name] = dict(
primary=primary, primary=primary,
@ -75,11 +85,34 @@ class Window(QObject):
h_in_mm=h_in_mm, h_in_mm=h_in_mm,
pos_x=pos_x, pos_x=pos_x,
pos_y=pos_y, pos_y=pos_y,
modes=[],
current_mode=None,
enabled=enabled,
) )
elif line[0] == ' ': # A mode
mode_name = line.strip().split()[0]
self.xrandr_info[name]['modes'].append(mode_name)
if '*' in line:
print(f'Current mode for {name}: {mode_name}')
self.xrandr_info[name]['current_mode'] = mode_name
def monitor_selected(self, name): def monitor_selected(self, name):
print(name) # Show modes
self.ui.modes.clear()
for mode in self.xrandr_info[name]['modes']:
self.ui.modes.addItem(mode)
self.ui.modes.setCurrentText(self.xrandr_info[name]['current_mode'])
mod_x, mod_y = [int(x) for x in self.xrandr_info[name]['current_mode'].split('x')]
h_scale = self.xrandr_info[name]['res_x'] / mod_x
v_scale = self.xrandr_info[name]['res_y'] / mod_y
self.ui.horizontalScale.setValue(h_scale * 100)
self.ui.verticalScale.setValue(v_scale * 100)
self.ui.primary.setChecked(self.xrandr_info[name]['primary'])
self.ui.enabled.setChecked(self.xrandr_info[name]['enabled'])
def updateScaleLabels(self):
self.ui.horizontalScaleLabel.setText(f'{self.ui.horizontalScale.value()}%')
self.ui.verticalScaleLabel.setText(f'{self.ui.verticalScale.value()}%')
if __name__ == "__main__": if __name__ == "__main__":
app = QApplication(sys.argv) app = QApplication(sys.argv)

102
main.ui
View File

@ -20,9 +20,9 @@
<item> <item>
<widget class="QTabWidget" name="tabWidget"> <widget class="QTabWidget" name="tabWidget">
<property name="currentIndex"> <property name="currentIndex">
<number>0</number> <number>1</number>
</property> </property>
<widget class="QWidget" name="tabWidgetPage1" native="true"> <widget class="QWidget" name="tabWidgetPage1">
<attribute name="title"> <attribute name="title">
<string>Global Settings</string> <string>Global Settings</string>
</attribute> </attribute>
@ -72,7 +72,7 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<widget class="QWidget" name="tabWidgetPage2" native="true"> <widget class="QWidget" name="tabWidgetPage2">
<property name="layoutDirection"> <property name="layoutDirection">
<enum>Qt::LeftToRight</enum> <enum>Qt::LeftToRight</enum>
</property> </property>
@ -91,17 +91,21 @@
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="0" column="1">
<widget class="QComboBox" name="screenCombo"/> <widget class="QComboBox" name="screenCombo">
<property name="sizeAdjustPolicy">
<enum>QComboBox::AdjustToContents</enum>
</property>
</widget>
</item> </item>
<item row="1" column="1"> <item row="1" column="1">
<widget class="QCheckBox" name="checkBox"> <widget class="QCheckBox" name="enabled">
<property name="text"> <property name="text">
<string>Enabled</string> <string>Enabled</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item row="2" column="1">
<widget class="QCheckBox" name="checkBox_2"> <widget class="QCheckBox" name="primary">
<property name="text"> <property name="text">
<string>Primary</string> <string>Primary</string>
</property> </property>
@ -115,7 +119,11 @@
</widget> </widget>
</item> </item>
<item row="3" column="1"> <item row="3" column="1">
<widget class="QComboBox" name="comboBox_2"/> <widget class="QComboBox" name="modes">
<property name="sizeAdjustPolicy">
<enum>QComboBox::AdjustToContents</enum>
</property>
</widget>
</item> </item>
<item row="4" column="0"> <item row="4" column="0">
<widget class="QLabel" name="label_3"> <widget class="QLabel" name="label_3">
@ -125,7 +133,11 @@
</widget> </widget>
</item> </item>
<item row="4" column="1"> <item row="4" column="1">
<widget class="QComboBox" name="comboBox_3"/> <widget class="QComboBox" name="comboBox_3">
<property name="sizeAdjustPolicy">
<enum>QComboBox::AdjustToContents</enum>
</property>
</widget>
</item> </item>
<item row="5" column="0"> <item row="5" column="0">
<widget class="QLabel" name="label_4"> <widget class="QLabel" name="label_4">
@ -135,34 +147,88 @@
</widget> </widget>
</item> </item>
<item row="5" column="1"> <item row="5" column="1">
<widget class="QComboBox" name="replicaOf"/> <widget class="QComboBox" name="replicaOf">
</item> <property name="sizeAdjustPolicy">
<item row="6" column="1"> <enum>QComboBox::AdjustToContents</enum>
<widget class="QSlider" name="horizontalSlider_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property> </property>
</widget> </widget>
</item> </item>
<item row="6" column="0"> <item row="6" column="0">
<widget class="QLabel" name="label_6"> <widget class="QLabel" name="label_6">
<property name="text"> <property name="text">
<string>Horizontal Scale</string> <string>Horizontal Scale:</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="7" column="0"> <item row="7" column="0">
<widget class="QLabel" name="label_7"> <widget class="QLabel" name="label_7">
<property name="text"> <property name="text">
<string>Vertical Scale</string> <string>Vertical Scale:</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="7" column="1"> <item row="6" column="1">
<widget class="QSlider" name="horizontalSlider_3"> <widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QSlider" name="horizontalScale">
<property name="maximum">
<number>400</number>
</property>
<property name="singleStep">
<number>5</number>
</property>
<property name="pageStep">
<number>25</number>
</property>
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </property>
<property name="tickPosition">
<enum>QSlider::TicksBelow</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="horizontalScaleLabel">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="7" column="1">
<widget class="QWidget" name="widget_2" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QSlider" name="verticalScale">
<property name="maximum">
<number>400</number>
</property>
<property name="singleStep">
<number>5</number>
</property>
<property name="pageStep">
<number>25</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="tickPosition">
<enum>QSlider::TicksBelow</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="verticalScaleLabel">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget> </widget>
</item> </item>
</layout> </layout>

View File

@ -1,9 +1,11 @@
from PySide2.QtCore import Qt from PySide2.QtCore import Qt
from PySide2.QtWidgets import QGraphicsRectItem, QGraphicsTextItem from PySide2.QtWidgets import QGraphicsRectItem, QGraphicsTextItem
from PySide2.QtGui import QBrush, QPen
class MonitorItem(QGraphicsRectItem): class MonitorItem(QGraphicsRectItem):
def __init__(self, *a, **kw): def __init__(self, *a, **kw):
primary = kw.pop('primary')
super().__init__(*a, **kw) super().__init__(*a, **kw)
self.setAcceptedMouseButtons(Qt.LeftButton) self.setAcceptedMouseButtons(Qt.LeftButton)
self.label = QGraphicsTextItem(kw["name"], self) self.label = QGraphicsTextItem(kw["name"], self)
@ -12,6 +14,8 @@ class MonitorItem(QGraphicsRectItem):
self.rect().height() / self.label.boundingRect().height(), self.rect().height() / self.label.boundingRect().height(),
) )
self.label.setScale(label_scale) self.label.setScale(label_scale)
if primary:
self.setBrush(QBrush('#eee8d5', Qt.SolidPattern))
def mousePressEvent(self, event): def mousePressEvent(self, event):
self.setCursor(Qt.ClosedHandCursor) self.setCursor(Qt.ClosedHandCursor)