mirror of
https://github.com/kbumsik/VirtScreen.git
synced 2025-03-09 15:40:18 +00:00
Backend: Use Q_ENUM
This commit is contained in:
parent
81b6d8919b
commit
df362568f6
2 changed files with 22 additions and 27 deletions
10
main.qml
10
main.qml
|
@ -275,7 +275,7 @@ ApplicationWindow {
|
||||||
// Material.background: Material.Teal
|
// Material.background: Material.Teal
|
||||||
// Material.foreground: Material.Grey
|
// Material.foreground: Material.Grey
|
||||||
onClicked: {
|
onClicked: {
|
||||||
if (backend.vncState == 'Off') {
|
if (backend.vncState == Backend.OFF) {
|
||||||
backend.startVNC()
|
backend.startVNC()
|
||||||
} else {
|
} else {
|
||||||
backend.stopVNC()
|
backend.stopVNC()
|
||||||
|
@ -284,7 +284,7 @@ ApplicationWindow {
|
||||||
|
|
||||||
Component.onCompleted: {
|
Component.onCompleted: {
|
||||||
backend.onVncStateChanged.connect(function(state) {
|
backend.onVncStateChanged.connect(function(state) {
|
||||||
if (state == "Off") {
|
if (state == Backend.OFF) {
|
||||||
vncButton.text = "Start VNC Server";
|
vncButton.text = "Start VNC Server";
|
||||||
} else {
|
} else {
|
||||||
vncButton.text = "Stop VNC Server";
|
vncButton.text = "Stop VNC Server";
|
||||||
|
@ -316,12 +316,6 @@ ApplicationWindow {
|
||||||
checked: true
|
checked: true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Component.onCompleted: {
|
|
||||||
backend.onVncStateChanged.connect(function(state) {
|
|
||||||
vncStateLabel.text = state;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sytray Icon
|
// Sytray Icon
|
||||||
|
|
|
@ -5,8 +5,7 @@ from enum import Enum
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from PyQt5.QtWidgets import QApplication
|
from PyQt5.QtWidgets import QApplication
|
||||||
from PyQt5.QtCore import QObject, QUrl, Qt
|
from PyQt5.QtCore import QObject, QUrl, Qt, pyqtProperty, pyqtSlot, pyqtSignal, Q_ENUMS
|
||||||
from PyQt5.QtCore import pyqtProperty, pyqtSlot, pyqtSignal
|
|
||||||
from PyQt5.QtGui import QIcon, QCursor
|
from PyQt5.QtGui import QIcon, QCursor
|
||||||
from PyQt5.QtQml import qmlRegisterType, QQmlApplicationEngine, QQmlListProperty
|
from PyQt5.QtQml import qmlRegisterType, QQmlApplicationEngine, QQmlListProperty
|
||||||
|
|
||||||
|
@ -318,17 +317,19 @@ class ProcessProtocol(protocol.ProcessProtocol):
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
# QML Backend class
|
# QML Backend class
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
class VNCState(Enum):
|
|
||||||
""" Enum to indicate a state of the VNC server """
|
|
||||||
OFF = "Off"
|
|
||||||
WAITING = "Waiting"
|
|
||||||
CONNECTED = "Connected"
|
|
||||||
|
|
||||||
class Backend(QObject):
|
class Backend(QObject):
|
||||||
""" Backend class for QML frontend """
|
""" Backend class for QML frontend """
|
||||||
|
class VNCState:
|
||||||
|
""" Enum to indicate a state of the VNC server """
|
||||||
|
OFF = 0
|
||||||
|
WAITING = 1
|
||||||
|
CONNECTED = 2
|
||||||
|
|
||||||
|
Q_ENUMS(VNCState)
|
||||||
|
|
||||||
# Signals
|
# Signals
|
||||||
onVirtScreenCreatedChanged = pyqtSignal(bool)
|
onVirtScreenCreatedChanged = pyqtSignal(bool)
|
||||||
onVncStateChanged = pyqtSignal(str)
|
onVncStateChanged = pyqtSignal(VNCState)
|
||||||
|
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
super(Backend, self).__init__(parent)
|
super(Backend, self).__init__(parent)
|
||||||
|
@ -346,7 +347,7 @@ class Backend(QObject):
|
||||||
# VNC server properties
|
# VNC server properties
|
||||||
self._vncPort = 5900
|
self._vncPort = 5900
|
||||||
self._vncPassword = ""
|
self._vncPassword = ""
|
||||||
self._vncState = VNCState.OFF
|
self._vncState = Backend.VNCState.OFF
|
||||||
# Primary screen and mouse posistion
|
# Primary screen and mouse posistion
|
||||||
self._primary: DisplayProperty() = self.xrandr.get_primary_screen()
|
self._primary: DisplayProperty() = self.xrandr.get_primary_screen()
|
||||||
self._cursor_x: int
|
self._cursor_x: int
|
||||||
|
@ -410,13 +411,13 @@ class Backend(QObject):
|
||||||
def vncPassword(self, vncPassword):
|
def vncPassword(self, vncPassword):
|
||||||
self._vncPassword = vncPassword
|
self._vncPassword = vncPassword
|
||||||
|
|
||||||
@pyqtProperty(str, notify=onVncStateChanged)
|
@pyqtProperty(VNCState, notify=onVncStateChanged)
|
||||||
def vncState(self):
|
def vncState(self):
|
||||||
return self._vncState.value
|
return self._vncState
|
||||||
@vncState.setter
|
@vncState.setter
|
||||||
def vncState(self, state):
|
def vncState(self, state):
|
||||||
self._vncState = state
|
self._vncState = state
|
||||||
self.onVncStateChanged.emit(self._vncState.value)
|
self.onVncStateChanged.emit(self._vncState)
|
||||||
|
|
||||||
@pyqtProperty(DisplayProperty)
|
@pyqtProperty(DisplayProperty)
|
||||||
def primary(self):
|
def primary(self):
|
||||||
|
@ -445,7 +446,7 @@ class Backend(QObject):
|
||||||
@pyqtSlot()
|
@pyqtSlot()
|
||||||
def deleteVirtScreen(self):
|
def deleteVirtScreen(self):
|
||||||
print("Deleting the Virtual Screen...")
|
print("Deleting the Virtual Screen...")
|
||||||
if self.vncState != VNCState.OFF.value:
|
if self.vncState is not Backend.VNCState.OFF:
|
||||||
print("Turn off the VNC server first")
|
print("Turn off the VNC server first")
|
||||||
self.virtScreenCreated = True
|
self.virtScreenCreated = True
|
||||||
return
|
return
|
||||||
|
@ -463,15 +464,15 @@ class Backend(QObject):
|
||||||
# define callbacks
|
# define callbacks
|
||||||
def _onConnected():
|
def _onConnected():
|
||||||
print("VNC started.")
|
print("VNC started.")
|
||||||
self.vncState = VNCState.WAITING
|
self.vncState = Backend.VNCState.WAITING
|
||||||
def _onReceived(data):
|
def _onReceived(data):
|
||||||
data = data.decode("utf-8")
|
data = data.decode("utf-8")
|
||||||
if (self._vncState is not VNCState.CONNECTED) and re_connection.search(data):
|
if (self._vncState is not Backend.VNCState.CONNECTED) and re_connection.search(data):
|
||||||
print("VNC connected.")
|
print("VNC connected.")
|
||||||
self.vncState = VNCState.CONNECTED
|
self.vncState = Backend.VNCState.CONNECTED
|
||||||
def _onEnded(exitCode):
|
def _onEnded(exitCode):
|
||||||
print("VNC Exited.")
|
print("VNC Exited.")
|
||||||
self.vncState = VNCState.OFF
|
self.vncState = Backend.VNCState.OFF
|
||||||
atexit.unregister(self.stopVNC)
|
atexit.unregister(self.stopVNC)
|
||||||
# Set password
|
# Set password
|
||||||
password = False
|
password = False
|
||||||
|
@ -501,7 +502,7 @@ class Backend(QObject):
|
||||||
# Usually called from atexit().
|
# Usually called from atexit().
|
||||||
self.vncServer.kill()
|
self.vncServer.kill()
|
||||||
time.sleep(2) # Make sure X11VNC shutdown before execute next atexit.
|
time.sleep(2) # Make sure X11VNC shutdown before execute next atexit.
|
||||||
if self._vncState in (VNCState.WAITING, VNCState.CONNECTED):
|
if self._vncState in (Backend.VNCState.WAITING, Backend.VNCState.CONNECTED):
|
||||||
self.vncServer.kill()
|
self.vncServer.kill()
|
||||||
else:
|
else:
|
||||||
print("stopVNC called while it is not running")
|
print("stopVNC called while it is not running")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue