basic ui layout + dragging monitors

This commit is contained in:
Roberto Alsina 2020-01-31 13:46:06 -03:00
commit 01589211e9
2 changed files with 338 additions and 0 deletions

103
main.py Normal file
View File

@ -0,0 +1,103 @@
import subprocess
import sys
from PySide2.QtWidgets import QApplication, QGraphicsScene, QGraphicsRectItem
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QFile, Qt, QPoint
def parse_monitor(line):
parts = line.split()
name = parts[0]
primary = "primary" in parts
w_in_mm, h_in_mm = [p.split("mm")[0] for p in parts if p.endswith("mm")]
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:]
print(name, pos_x, pos_y)
return (
name,
primary,
int(res_x),
int(res_y),
int(w_in_mm),
int(h_in_mm),
int(pos_x),
int(pos_y),
)
xrandr_info = {}
def get_xrandr_info():
data = subprocess.check_output(["xrandr"]).decode("utf-8").splitlines()
outputs = [x for x in data if x and x[0] not in "S \t"]
for o in outputs:
name, primary, res_x, res_y, w_in_mm, h_in_mm, pos_x, pos_y = parse_monitor(o)
xrandr_info[name] = dict(
primary=primary,
res_x=res_x,
res_y=res_y,
w_in_mm=w_in_mm,
h_in_mm=h_in_mm,
pos_x=pos_x,
pos_y=pos_y,
)
class MonitorItem(QGraphicsRectItem):
def __init__(self, *a, **kw):
super().__init__(*a, **kw)
self.setAcceptedMouseButtons(Qt.LeftButton)
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()
print(current_pos - click_pos)
print(view.mapToScene(view.mapFromScene(self.orig_pos) + current_pos - click_pos))
self.setPos(view.mapToScene(view.mapFromScene(self.orig_pos) + current_pos - click_pos))
# self.setPos(delta.x(), delta.y())
def fill_ui(data, window):
global scene
scene = QGraphicsScene(window)
scene.addText("Hello World!")
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"])
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__":
app = QApplication(sys.argv)
ui_file = QFile("main.ui")
ui_file.open(QFile.ReadOnly)
loader = QUiLoader()
window = loader.load(ui_file)
window.show()
get_xrandr_info()
fill_ui(xrandr_info, window)
sys.exit(app.exec_())

235
main.ui Normal file
View File

@ -0,0 +1,235 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>925</width>
<height>820</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGraphicsView" name="sceneView"/>
</item>
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tabWidgetPage1" native="true">
<attribute name="title">
<string>Global Settings</string>
</attribute>
<layout class="QFormLayout" name="formLayout_2">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::FieldsStayAtSizeHint</enum>
</property>
<property name="formAlignment">
<set>Qt::AlignHCenter|Qt::AlignTop</set>
</property>
<property name="leftMargin">
<number>6</number>
</property>
<item row="0" column="1">
<widget class="QCheckBox" name="checkBox_3">
<property name="text">
<string>Use physical model</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Global scale:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSlider" name="horizontalSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="2" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QWidget" name="tabWidgetPage2" native="true">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<attribute name="title">
<string>Screen Settings</string>
</attribute>
<layout class="QFormLayout" name="formLayout">
<property name="formAlignment">
<set>Qt::AlignHCenter|Qt::AlignTop</set>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Settings for</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="screenCombo"/>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string>Enabled</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QCheckBox" name="checkBox_2">
<property name="text">
<string>Primary</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Resolution</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="comboBox_2"/>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Orientation</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QComboBox" name="comboBox_3"/>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Replica of:</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QComboBox" name="replicaOf"/>
</item>
<item row="6" column="1">
<widget class="QSlider" name="horizontalSlider_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Horizontal Scale</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Vertical Scale</string>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QSlider" name="horizontalSlider_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pushButton_6">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_5">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_4">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton_3">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>