mirror of
https://github.com/kbumsik/VirtScreen.git
synced 2025-02-14 12:21:50 +00:00
Load JSON settings directly in QML
This commit is contained in:
parent
09cf4cb72f
commit
ce1debd8ad
3 changed files with 69 additions and 131 deletions
60
main.qml
60
main.qml
|
@ -7,28 +7,28 @@ import VirtScreen.Backend 1.0
|
|||
|
||||
Item {
|
||||
property alias window: mainLoader.item
|
||||
|
||||
property var settings: JSON.parse(backend.settings)
|
||||
property bool autostart: settings.vnc.autostart
|
||||
|
||||
function switchVNC () {
|
||||
if ((backend.vncState == Backend.OFF) && backend.virtScreenCreated) {
|
||||
backend.startVNC(settings.vnc.port);
|
||||
}
|
||||
}
|
||||
|
||||
onAutostartChanged: {
|
||||
if (autostart) {
|
||||
backend.onVirtScreenCreatedChanged.connect(switchVNC);
|
||||
backend.onVncStateChanged.connect(switchVNC);
|
||||
} else {
|
||||
backend.onVirtScreenCreatedChanged.disconnect(switchVNC);
|
||||
backend.onVncStateChanged.disconnect(switchVNC);
|
||||
}
|
||||
}
|
||||
|
||||
// virtscreen.py backend.
|
||||
Backend {
|
||||
id: backend
|
||||
function switchVNC () {
|
||||
if ((backend.vncState == Backend.OFF) && backend.virtScreenCreated) {
|
||||
backend.startVNC();
|
||||
}
|
||||
}
|
||||
onVncAutoStartChanged: {
|
||||
if (vncAutoStart) {
|
||||
onVirtScreenCreatedChanged.connect(switchVNC);
|
||||
onVncStateChanged.connect(switchVNC);
|
||||
} else {
|
||||
onVirtScreenCreatedChanged.disconnect(switchVNC);
|
||||
onVncStateChanged.disconnect(switchVNC);
|
||||
}
|
||||
}
|
||||
Component.onCompleted: {
|
||||
// force emit signal on load
|
||||
vncAutoStart = vncAutoStart;
|
||||
}
|
||||
}
|
||||
|
||||
// Timer object and function
|
||||
|
@ -140,23 +140,24 @@ Item {
|
|||
MenuItem {
|
||||
id: virtScreenAction
|
||||
text: backend.virtScreenCreated ? "Disable Virtual Screen" : "Enable Virtual Screen"
|
||||
enabled:backend.vncAutoStart ? true :
|
||||
backend.vncState == Backend.OFF ? true : false
|
||||
enabled: autostart ? true :
|
||||
backend.vncState == Backend.OFF ? true : false
|
||||
onTriggered: {
|
||||
// Give a very short delay to show busyDialog.
|
||||
timer.setTimeout (function() {
|
||||
if (!backend.virtScreenCreated) {
|
||||
backend.createVirtScreen();
|
||||
backend.createVirtScreen(settings.virt.width, settings.virt.height,
|
||||
settings.virt.portrait, settings.virt.hidpi);
|
||||
} else {
|
||||
// If auto start enabled, stop VNC first then
|
||||
if (backend.vncAutoStart && (backend.vncState != Backend.OFF)) {
|
||||
backend.vncAutoStart = false;
|
||||
if (autostart && (backend.vncState != Backend.OFF)) {
|
||||
autostart = false;
|
||||
connectOnce(backend.onVncStateChanged, function() {
|
||||
console.log("autoOff called here", backend.vncState);
|
||||
if (backend.vncState == Backend.OFF) {
|
||||
console.log("Yes. Delete it");
|
||||
backend.deleteVirtScreen();
|
||||
backend.vncAutoStart = true;
|
||||
autostart = true;
|
||||
}
|
||||
});
|
||||
backend.stopVNC();
|
||||
|
@ -169,18 +170,21 @@ Item {
|
|||
}
|
||||
MenuItem {
|
||||
id: vncAction
|
||||
text: backend.vncAutoStart ? "Auto start enabled" :
|
||||
text: autostart ? "Auto start enabled" :
|
||||
backend.vncState == Backend.OFF ? "Start VNC Server" : "Stop VNC Server"
|
||||
enabled: backend.vncAutoStart ? false :
|
||||
enabled: autostart ? false :
|
||||
backend.virtScreenCreated ? true : false
|
||||
onTriggered: backend.vncState == Backend.OFF ? backend.startVNC() : backend.stopVNC()
|
||||
onTriggered: backend.vncState == Backend.OFF ? backend.startVNC(settings.vnc.port) : backend.stopVNC()
|
||||
}
|
||||
MenuItem {
|
||||
separator: true
|
||||
}
|
||||
MenuItem {
|
||||
id: quitAction
|
||||
text: qsTr("&Quit")
|
||||
onTriggered: {
|
||||
settings.vnc.autostart = autostart;
|
||||
backend.settings = JSON.stringify(settings, null, 4);
|
||||
backend.quitProgram();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,9 +75,7 @@ ApplicationWindow {
|
|||
|
||||
MenuItem {
|
||||
text: qsTr("&Quit")
|
||||
onTriggered: {
|
||||
backend.quitProgram();
|
||||
}
|
||||
onTriggered: quitAction.onTriggered()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -227,13 +225,13 @@ ApplicationWindow {
|
|||
RowLayout {
|
||||
Label { text: "Width"; Layout.fillWidth: true }
|
||||
SpinBox {
|
||||
value: backend.virt.width
|
||||
value: settings.virt.width
|
||||
from: 640
|
||||
to: 1920
|
||||
stepSize: 1
|
||||
editable: true
|
||||
onValueModified: {
|
||||
backend.virt.width = value;
|
||||
settings.virt.width = value;
|
||||
}
|
||||
textFromValue: function(value, locale) { return value; }
|
||||
}
|
||||
|
@ -241,13 +239,13 @@ ApplicationWindow {
|
|||
RowLayout {
|
||||
Label { text: "Height"; Layout.fillWidth: true }
|
||||
SpinBox {
|
||||
value: backend.virt.height
|
||||
value: settings.virt.height
|
||||
from: 360
|
||||
to: 1080
|
||||
stepSize : 1
|
||||
editable: true
|
||||
onValueModified: {
|
||||
backend.virt.height = value;
|
||||
settings.virt.height = value;
|
||||
}
|
||||
textFromValue: function(value, locale) { return value; }
|
||||
}
|
||||
|
@ -255,18 +253,18 @@ ApplicationWindow {
|
|||
RowLayout {
|
||||
Label { text: "Portrait Mode"; Layout.fillWidth: true }
|
||||
Switch {
|
||||
checked: backend.portrait
|
||||
checked: settings.virt.portrait
|
||||
onCheckedChanged: {
|
||||
backend.portrait = checked;
|
||||
settings.virt.portrait = checked;
|
||||
}
|
||||
}
|
||||
}
|
||||
RowLayout {
|
||||
Label { text: "HiDPI (2x resolution)"; Layout.fillWidth: true }
|
||||
Switch {
|
||||
checked: backend.hidpi
|
||||
checked: settings.virt.hidpi
|
||||
onCheckedChanged: {
|
||||
backend.hidpi = checked;
|
||||
settings.virt.hidpi = checked;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -327,8 +325,8 @@ ApplicationWindow {
|
|||
if (backend.vncState != Backend.OFF) {
|
||||
console.log("vnc is running");
|
||||
var restoreVNC = true;
|
||||
if (backend.vncAutoStart) {
|
||||
backend.vncAutoStart = false;
|
||||
if (autostart) {
|
||||
autostart = false;
|
||||
var restoreAutoStart = true;
|
||||
}
|
||||
}
|
||||
|
@ -336,10 +334,10 @@ ApplicationWindow {
|
|||
window.autoClose = true;
|
||||
busyDialog.close();
|
||||
if (restoreAutoStart) {
|
||||
backend.vncAutoStart = true;
|
||||
autostart = true;
|
||||
}
|
||||
if (restoreVNC) {
|
||||
backend.startVNC();
|
||||
backend.startVNC(settings.vnc.port);
|
||||
}
|
||||
});
|
||||
backend.stopVNC();
|
||||
|
@ -362,13 +360,13 @@ ApplicationWindow {
|
|||
RowLayout {
|
||||
Label { text: "Port"; Layout.fillWidth: true }
|
||||
SpinBox {
|
||||
value: backend.vncPort
|
||||
value: settings.vnc.port
|
||||
from: 1
|
||||
to: 65535
|
||||
stepSize: 1
|
||||
editable: true
|
||||
onValueModified: {
|
||||
backend.vncPort = value;
|
||||
settings.vnc.port = value;
|
||||
}
|
||||
textFromValue: function(value, locale) { return value; }
|
||||
}
|
||||
|
@ -413,13 +411,13 @@ ApplicationWindow {
|
|||
anchors.topMargin: vncButton.height - 10
|
||||
Label { text: "Auto start"; }
|
||||
Switch {
|
||||
checked: backend.vncAutoStart
|
||||
checked: autostart
|
||||
onToggled: {
|
||||
autostart = checked;
|
||||
if ((checked == true) && (backend.vncState == Backend.OFF) &&
|
||||
backend.virtScreenCreated) {
|
||||
backend.startVNC();
|
||||
backend.startVNC(settings.vnc.port);
|
||||
}
|
||||
backend.vncAutoStart = checked;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
102
virtscreen.py
102
virtscreen.py
|
@ -359,18 +359,12 @@ class Backend(QObject):
|
|||
Q_ENUMS(VNCState)
|
||||
# Virtual screen properties
|
||||
xrandr: XRandR
|
||||
_virt: Display = Display()
|
||||
_virtProp: DisplayProperty
|
||||
_portrait: bool
|
||||
_hidpi: bool
|
||||
_virtScreenCreated: bool = False
|
||||
screens: List[DisplayProperty]
|
||||
_virtScreenIndex: int
|
||||
# VNC server properties
|
||||
_vncPort: int
|
||||
_vncUsePassword: bool = False
|
||||
_vncState: VNCState
|
||||
_vncAutoStart: bool
|
||||
_vncState: VNCState = VNCState.OFF
|
||||
# Primary screen and mouse posistion
|
||||
_primaryProp: DisplayProperty
|
||||
cursor_x: int
|
||||
|
@ -388,53 +382,23 @@ class Backend(QObject):
|
|||
|
||||
def __init__(self, parent=None):
|
||||
super(Backend, self).__init__(parent)
|
||||
# Read JSON to load variables
|
||||
try:
|
||||
with open(CONFIG_PATH, "r") as f:
|
||||
settings = json.load(f)
|
||||
self._virt.width = settings['virt']['width']
|
||||
self._virt.height = settings['virt']['height']
|
||||
self._portrait = settings['virt']['portrait']
|
||||
self._hidpi = settings['virt']['hidpi']
|
||||
self._vncPort = settings['vnc']['port']
|
||||
self._vncAutoStart = settings['vnc']['autostart']
|
||||
except (FileNotFoundError, json.JSONDecodeError, KeyError):
|
||||
print("Default Setting used.")
|
||||
with open(DEFAULT_CONFIG_PATH, "r") as f:
|
||||
settings = json.load(f)
|
||||
self._virt.width = settings['virt']['width']
|
||||
self._virt.height = settings['virt']['height']
|
||||
self._portrait = settings['virt']['portrait']
|
||||
self._hidpi = settings['virt']['hidpi']
|
||||
self._vncPort = settings['vnc']['port']
|
||||
self._vncAutoStart = settings['vnc']['autostart']
|
||||
# create objects
|
||||
self._virtProp = DisplayProperty(self._virt)
|
||||
self._vncState = self.VNCState.OFF
|
||||
self.xrandr = XRandR()
|
||||
self._virtScreenIndex = self.xrandr.virt_idx
|
||||
|
||||
# Qt properties
|
||||
@pyqtProperty(DisplayProperty)
|
||||
def virt(self):
|
||||
return self._virtProp
|
||||
@virt.setter
|
||||
def virt(self, virt):
|
||||
self._virtProp = virt
|
||||
|
||||
@pyqtProperty(bool)
|
||||
def portrait(self):
|
||||
return self._portrait
|
||||
@portrait.setter
|
||||
def portrait(self, portrait):
|
||||
self._portrait = portrait
|
||||
|
||||
@pyqtProperty(bool)
|
||||
def hidpi(self):
|
||||
return self._hidpi
|
||||
@hidpi.setter
|
||||
def hidpi(self, hidpi):
|
||||
self._hidpi = hidpi
|
||||
@pyqtProperty(str, constant=True)
|
||||
def settings(self):
|
||||
try:
|
||||
with open(CONFIG_PATH, "r") as f:
|
||||
return f.read()
|
||||
except FileNotFoundError:
|
||||
with open(DEFAULT_CONFIG_PATH, "r") as f:
|
||||
return f.read()
|
||||
@settings.setter
|
||||
def settings(self, json_str):
|
||||
with open(CONFIG_PATH, "w") as f:
|
||||
f.write(json_str)
|
||||
|
||||
@pyqtProperty(bool, notify=onVirtScreenCreatedChanged)
|
||||
def virtScreenCreated(self):
|
||||
|
@ -444,7 +408,7 @@ class Backend(QObject):
|
|||
self._virtScreenCreated = value
|
||||
self.onVirtScreenCreatedChanged.emit(value)
|
||||
|
||||
@pyqtProperty(QQmlListProperty)
|
||||
@pyqtProperty(QQmlListProperty, constant=True)
|
||||
def screens(self):
|
||||
return QQmlListProperty(DisplayProperty, self, [DisplayProperty(x) for x in self.xrandr.screens])
|
||||
|
||||
|
@ -458,13 +422,6 @@ class Backend(QObject):
|
|||
self.xrandr.virt = self.xrandr.screens[self.xrandr.virt_idx]
|
||||
self._virtScreenIndex = virtScreenIndex
|
||||
|
||||
@pyqtProperty(int)
|
||||
def vncPort(self):
|
||||
return self._vncPort
|
||||
@vncPort.setter
|
||||
def vncPort(self, port):
|
||||
self._vncPort = port
|
||||
|
||||
@pyqtProperty(bool, notify=onVncUsePasswordChanged)
|
||||
def vncUsePassword(self):
|
||||
if os.path.isfile(X11VNC_PASSWORD_PATH):
|
||||
|
@ -485,14 +442,6 @@ class Backend(QObject):
|
|||
def vncState(self, state):
|
||||
self._vncState = state
|
||||
self.onVncStateChanged.emit(self._vncState)
|
||||
|
||||
@pyqtProperty(bool, notify=onVncAutoStartChanged)
|
||||
def vncAutoStart(self):
|
||||
return self._vncAutoStart
|
||||
@vncAutoStart.setter
|
||||
def vncAutoStart(self, vncAutoStart):
|
||||
self._vncAutoStart = vncAutoStart
|
||||
self.onVncAutoStartChanged.emit(vncAutoStart)
|
||||
|
||||
@pyqtProperty('QStringList', notify=onIPAddressesChanged)
|
||||
def ipAddresses(self):
|
||||
|
@ -522,10 +471,10 @@ class Backend(QObject):
|
|||
return cursor.y()
|
||||
|
||||
# Qt Slots
|
||||
@pyqtSlot()
|
||||
def createVirtScreen(self):
|
||||
@pyqtSlot(int, int, bool, bool)
|
||||
def createVirtScreen(self, width, height, portrait, hidpi):
|
||||
print("Creating a Virtual Screen...")
|
||||
self.xrandr.create_virtual_screen(self._virt.width, self._virt.height, self.portrait, self.hidpi)
|
||||
self.xrandr.create_virtual_screen(width, height, portrait, hidpi)
|
||||
self.virtScreenCreated = True
|
||||
|
||||
@pyqtSlot()
|
||||
|
@ -560,8 +509,8 @@ class Backend(QObject):
|
|||
else:
|
||||
print("Failed deleting the password file")
|
||||
|
||||
@pyqtSlot()
|
||||
def startVNC(self):
|
||||
@pyqtSlot(int)
|
||||
def startVNC(self, port):
|
||||
# Check if a virtual screen created
|
||||
if not self.virtScreenCreated:
|
||||
print("Virtual Screen not crated.")
|
||||
|
@ -590,7 +539,6 @@ class Backend(QObject):
|
|||
atexit.unregister(self.stopVNC)
|
||||
logfile = open(X11VNC_LOG_PATH, "wb")
|
||||
self.vncServer = ProcessProtocol(_onConnected, _onReceived, _onReceived, _onEnded, logfile)
|
||||
port = self.vncPort
|
||||
virt = self.xrandr.get_virtual_screen()
|
||||
clip = f"{virt.width}x{virt.height}+{virt.x_offset}+{virt.y_offset}"
|
||||
arg = f"x11vnc -multiptr -repeat -rfbport {port} -clip {clip}"
|
||||
|
@ -635,18 +583,6 @@ class Backend(QObject):
|
|||
|
||||
@pyqtSlot()
|
||||
def quitProgram(self):
|
||||
# Save settings first
|
||||
with open(CONFIG_PATH, 'w') as f:
|
||||
settings = {}
|
||||
settings['virt'] = {}
|
||||
settings['virt']['width'] = self._virt.width
|
||||
settings['virt']['height'] = self._virt.height
|
||||
settings['virt']['portrait'] = self._portrait
|
||||
settings['virt']['hidpi'] = self._hidpi
|
||||
settings['vnc'] = {}
|
||||
settings['vnc']['port'] = self._vncPort
|
||||
settings['vnc']['autostart'] = self._vncAutoStart
|
||||
json.dump(settings, f, sort_keys=True, indent=4)
|
||||
self.blockSignals(True) # This will prevent invoking auto-restart or etc.
|
||||
QApplication.instance().quit()
|
||||
|
||||
|
|
Loading…
Reference in a new issue