From a07fecd5740a1ea9c4ab08779733bc1dd24e82c6 Mon Sep 17 00:00:00 2001 From: Bumsik Kim Date: Mon, 7 May 2018 23:34:11 -0400 Subject: [PATCH] QML: VNC Server state parsing and updating --- main.qml | 52 ++++++++++++++++++++++++++++++++++++++------------- virtscreen.py | 27 +++++++++++++------------- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/main.qml b/main.qml index 4243108..7787ee4 100644 --- a/main.qml +++ b/main.qml @@ -1,6 +1,6 @@ import QtQuick 2.10 import QtQuick.Controls 2.3 -// import QtQuick.Controls.Material 2.3 +import QtQuick.Controls.Material 2.3 import QtQuick.Layouts 1.3 import QtQuick.Window 2.2 @@ -15,8 +15,8 @@ ApplicationWindow { flags: Qt.FramelessWindowHint title: "Basic layouts" - // Material.theme: Material.Light - // Material.accent: Material.Teal + Material.theme: Material.Light + Material.accent: Material.Teal property int margin: 11 width: 380 @@ -29,7 +29,7 @@ ApplicationWindow { } } - // virtscreen.py hackend. + // virtscreen.py backend. Backend { id: backend } @@ -169,16 +169,16 @@ ApplicationWindow { busyDialog.open(); // Give a very short delay to show busyDialog. timer.setTimeout (function() { - if (!backend.virtScreenCreated) { - backend.createVirtScreen(); - } else { - backend.deleteVirtScreen(); - } + if (!backend.virtScreenCreated) { + backend.createVirtScreen(); + } else { + backend.deleteVirtScreen(); + } }, 200); } Component.onCompleted: { - backend.virtScreenChanged.connect(function(created) { + backend.onVirtScreenCreatedChanged.connect(function(created) { busyDialog.close(); virtScreenButton.enabled = true; if (created) { @@ -237,7 +237,9 @@ ApplicationWindow { } Button { + id: vncButton text: "Start VNC Server" + enabled: false Layout.fillWidth: true // Material.background: Material.Teal // Material.foreground: Material.Grey @@ -248,6 +250,23 @@ ApplicationWindow { backend.stopVNC() } } + + Component.onCompleted: { + backend.onVncStateChanged.connect(function(state) { + if (state == "Off") { + vncButton.text = "Start VNC Server"; + } else { + vncButton.text = "Stop VNC Server"; + } + }); + backend.onVirtScreenCreatedChanged.connect(function(created) { + if (created) { + vncButton.enabled = true; + } else { + vncButton.enabled = false; + } + }); + } } } } @@ -256,6 +275,7 @@ ApplicationWindow { RowLayout { anchors.margins: spacing Label { + id: vncStateLabel text: backend.vncState } Item { Layout.fillWidth: true } @@ -265,6 +285,12 @@ ApplicationWindow { checked: true } } + + Component.onCompleted: { + backend.onVncStateChanged.connect(function(state) { + vncStateLabel.text = state; + }); + } } // Sytray Icon @@ -293,9 +319,9 @@ ApplicationWindow { var y_mid = height / 2; window.x = (backend.cursor_x > x_mid)? width - window.width : 0; window.y = (backend.cursor_y > y_mid)? height - window.height : 0; - window.show() - window.raise() - window.requestActivate() + window.show(); + window.raise(); + window.requestActivate(); } menu: Labs.Menu { diff --git a/virtscreen.py b/virtscreen.py index 4297b22..ef020f7 100755 --- a/virtscreen.py +++ b/virtscreen.py @@ -229,8 +229,8 @@ class VNCState(Enum): class Backend(QObject): """ Backend class for QML frontend """ # Signals - virtScreenChanged = pyqtSignal(bool) - vncStateChanged = pyqtSignal(str) + onVirtScreenCreatedChanged = pyqtSignal(bool) + onVncStateChanged = pyqtSignal(str) def __init__(self, parent=None): super(Backend, self).__init__(parent) @@ -281,13 +281,13 @@ class Backend(QObject): def hidpi(self, hidpi): self._hidpi = hidpi - @pyqtProperty(bool) - def virtScreenCreated(self, notify=virtScreenChanged): + @pyqtProperty(bool, notify=onVirtScreenCreatedChanged) + def virtScreenCreated(self): return self._virtScreenCreated @virtScreenCreated.setter def virtScreenCreated(self, value): self._virtScreenCreated = value - self.virtScreenChanged.emit(value) + self.onVirtScreenCreatedChanged.emit(value) @pyqtProperty(int) def vncPort(self): @@ -302,15 +302,14 @@ class Backend(QObject): @vncPassword.setter def vncPassword(self, vncPassword): self._vncPassword = vncPassword - print(self._vncPassword) - @pyqtProperty(str) - def vncState(self, notify=vncStateChanged): + @pyqtProperty(str, notify=onVncStateChanged) + def vncState(self): return self._vncState.value @vncState.setter def vncState(self, state): self._vncState = state - self.vncStateChanged.emit(self._vncState.value) + self.onVncStateChanged.emit(self._vncState.value) @pyqtProperty(int) def cursor_x(self): @@ -361,15 +360,17 @@ class Backend(QObject): if not self.virtScreenCreated: print("Virtual Screen not crated.") return + # regex used in callbacks + re_connection = re.compile(r"^.*Got connection from client.*$", re.M) # define callbacks def _onConnected(): print("VNC started.") self.vncState = VNCState.WAITING def _onReceived(data): data = data.decode("utf-8") - for line in data.splitlines(): - # TODO: Update state of the server - pass + if (self._vncState is not VNCState.CONNECTED) and re_connection.search(data): + print("VNC connected.") + self.vncState = VNCState.CONNECTED def _onEnded(exitCode): print("VNC Exited.") self.vncState = VNCState.OFF @@ -402,7 +403,7 @@ class Backend(QObject): # Usually called from atexit(). self.vncServer.kill() time.sleep(2) # Make sure X11VNC shutdown before execute next atexit. - if self.vncState in (VNCState.WAITING.value, VNCState.CONNECTED.value): + if self._vncState in (VNCState.WAITING, VNCState.CONNECTED): self.vncServer.kill() else: print("stopVNC called while it is not running")