mirror of
https://github.com/ralsina/xrandroll.git
synced 2024-11-23 11:32:22 +00:00
refactors
This commit is contained in:
parent
0c4db3e7d0
commit
7a9d699399
98
main.py
98
main.py
@ -1,10 +1,11 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from PySide2.QtCore import QFile, QPoint, Qt
|
from PySide2.QtCore import QFile, QObject
|
||||||
from PySide2.QtUiTools import QUiLoader
|
from PySide2.QtUiTools import QUiLoader
|
||||||
from PySide2.QtWidgets import (QApplication, QGraphicsRectItem, QGraphicsScene,
|
from PySide2.QtWidgets import QApplication, QGraphicsScene
|
||||||
QGraphicsTextItem)
|
|
||||||
|
from monitor_item import MonitorItem
|
||||||
|
|
||||||
|
|
||||||
def parse_monitor(line):
|
def parse_monitor(line):
|
||||||
@ -27,15 +28,46 @@ def parse_monitor(line):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
xrandr_info = {}
|
class Window(QObject):
|
||||||
|
def __init__(self, ui):
|
||||||
|
super().__init__()
|
||||||
|
self.ui = ui
|
||||||
|
ui.show()
|
||||||
|
self.ui.screenCombo.currentTextChanged.connect(self.monitor_selected)
|
||||||
|
self.xrandr_info = {}
|
||||||
|
self.get_xrandr_info()
|
||||||
|
self.fill_ui()
|
||||||
|
|
||||||
|
def fill_ui(self):
|
||||||
|
"""Load data from xrandr and setup the whole thing."""
|
||||||
|
self.scene = QGraphicsScene(self)
|
||||||
|
self.ui.sceneView.setScene(self.scene)
|
||||||
|
self.ui.screenCombo.clear()
|
||||||
|
|
||||||
def get_xrandr_info():
|
for name, monitor in self.xrandr_info.items():
|
||||||
|
self.ui.screenCombo.addItem(name)
|
||||||
|
mon_item = MonitorItem(0, 0, monitor["res_x"], monitor["res_y"], name=name)
|
||||||
|
mon_item.setPos(monitor["pos_x"], monitor["pos_y"])
|
||||||
|
self.scene.addItem(mon_item)
|
||||||
|
monitor["item"] = mon_item
|
||||||
|
self.adjust_view()
|
||||||
|
|
||||||
|
def adjust_view(self):
|
||||||
|
self.ui.sceneView.ensureVisible(self.scene.sceneRect(), 100, 100)
|
||||||
|
scale_factor = 0.7 * min(
|
||||||
|
self.ui.sceneView.width() / self.scene.sceneRect().width(),
|
||||||
|
self.ui.sceneView.height() / self.scene.sceneRect().height(),
|
||||||
|
)
|
||||||
|
self.ui.sceneView.scale(scale_factor, scale_factor)
|
||||||
|
|
||||||
|
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"]
|
outputs = [x for x in data if x and x[0] not in "S \t"]
|
||||||
for o in outputs:
|
for o in outputs:
|
||||||
name, primary, res_x, res_y, w_in_mm, h_in_mm, pos_x, pos_y = parse_monitor(o)
|
name, primary, res_x, res_y, w_in_mm, h_in_mm, pos_x, pos_y = parse_monitor(
|
||||||
xrandr_info[name] = dict(
|
o
|
||||||
|
)
|
||||||
|
self.xrandr_info[name] = dict(
|
||||||
primary=primary,
|
primary=primary,
|
||||||
res_x=res_x,
|
res_x=res_x,
|
||||||
res_y=res_y,
|
res_y=res_y,
|
||||||
@ -45,51 +77,8 @@ def get_xrandr_info():
|
|||||||
pos_y=pos_y,
|
pos_y=pos_y,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def monitor_selected(self, name):
|
||||||
class MonitorItem(QGraphicsRectItem):
|
print(name)
|
||||||
def __init__(self, *a, **kw):
|
|
||||||
super().__init__(*a, **kw)
|
|
||||||
self.setAcceptedMouseButtons(Qt.LeftButton)
|
|
||||||
self.label = QGraphicsTextItem(kw['name'], self)
|
|
||||||
label_scale = min(
|
|
||||||
self.rect().width() / self.label.boundingRect().width(),
|
|
||||||
self.rect().height() / self.label.boundingRect().height())
|
|
||||||
self.label.setScale(label_scale)
|
|
||||||
|
|
||||||
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)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def fill_ui(data, window):
|
|
||||||
global scene
|
|
||||||
scene = QGraphicsScene(window)
|
|
||||||
window.sceneView.setScene(scene)
|
|
||||||
window.screenCombo.clear()
|
|
||||||
for name, monitor in xrandr_info.items():
|
|
||||||
window.screenCombo.addItem(name)
|
|
||||||
mon_item = MonitorItem(0, 0, monitor["res_x"], monitor["res_y"], name=name)
|
|
||||||
mon_item.setPos(monitor["pos_x"], monitor["pos_y"])
|
|
||||||
scene.addItem(mon_item)
|
|
||||||
|
|
||||||
print(scene.sceneRect())
|
|
||||||
window.sceneView.ensureVisible(scene.sceneRect(), 100, 100)
|
|
||||||
scale_factor = 0.7 * min(
|
|
||||||
window.sceneView.width() / scene.sceneRect().width(),
|
|
||||||
window.sceneView.height() / scene.sceneRect().height(),
|
|
||||||
)
|
|
||||||
window.sceneView.scale(scale_factor, scale_factor)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@ -99,9 +88,6 @@ if __name__ == "__main__":
|
|||||||
ui_file.open(QFile.ReadOnly)
|
ui_file.open(QFile.ReadOnly)
|
||||||
|
|
||||||
loader = QUiLoader()
|
loader = QUiLoader()
|
||||||
window = loader.load(ui_file)
|
window = Window(loader.load(ui_file))
|
||||||
window.show()
|
|
||||||
get_xrandr_info()
|
|
||||||
fill_ui(xrandr_info, window)
|
|
||||||
|
|
||||||
sys.exit(app.exec_())
|
sys.exit(app.exec_())
|
||||||
|
2
main.ui
2
main.ui
@ -37,7 +37,7 @@
|
|||||||
<number>6</number>
|
<number>6</number>
|
||||||
</property>
|
</property>
|
||||||
<item row="0" column="1">
|
<item row="0" column="1">
|
||||||
<widget class="QCheckBox" name="checkBox_3">
|
<widget class="QCheckBox" name="usePhysModel">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Use physical model</string>
|
<string>Use physical model</string>
|
||||||
</property>
|
</property>
|
||||||
|
29
monitor_item.py
Normal file
29
monitor_item.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
from PySide2.QtCore import Qt
|
||||||
|
from PySide2.QtWidgets import QGraphicsRectItem, QGraphicsTextItem
|
||||||
|
|
||||||
|
|
||||||
|
class MonitorItem(QGraphicsRectItem):
|
||||||
|
def __init__(self, *a, **kw):
|
||||||
|
super().__init__(*a, **kw)
|
||||||
|
self.setAcceptedMouseButtons(Qt.LeftButton)
|
||||||
|
self.label = QGraphicsTextItem(kw["name"], self)
|
||||||
|
label_scale = min(
|
||||||
|
self.rect().width() / self.label.boundingRect().width(),
|
||||||
|
self.rect().height() / self.label.boundingRect().height(),
|
||||||
|
)
|
||||||
|
self.label.setScale(label_scale)
|
||||||
|
|
||||||
|
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)
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user