From 9ec6256fc17d8220ce72ad7f15549cb4c7170aea Mon Sep 17 00:00:00 2001 From: Bumsik Kim Date: Sat, 9 Jun 2018 11:28:34 -0400 Subject: [PATCH] Separated Cursor and Network classes out of Backend class --- virtscreen/assets/VncPage.qml | 8 ++++- virtscreen/assets/main.qml | 10 ++++-- virtscreen/virtscreen.py | 62 ++++++++++++++++++++++------------- 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/virtscreen/assets/VncPage.qml b/virtscreen/assets/VncPage.qml index 5d17d5e..19f1627 100644 --- a/virtscreen/assets/VncPage.qml +++ b/virtscreen/assets/VncPage.qml @@ -3,8 +3,14 @@ import QtQuick.Controls 2.3 import QtQuick.Layouts 1.3 import VirtScreen.Backend 1.0 +import VirtScreen.Network 1.0 ColumnLayout { + // virtscreen.py Network interfaces backend. + Network { + id: network + } + GroupBox { title: "VNC Server" Layout.fillWidth: true @@ -98,7 +104,7 @@ ColumnLayout { anchors.bottom: ipListView.bottom policy: ScrollBar.AlwaysOn } - model: backend.ipAddresses + model: network.ipAddresses delegate: TextEdit { text: modelData readOnly: true diff --git a/virtscreen/assets/main.qml b/virtscreen/assets/main.qml index 6c1f7e1..1760b2f 100644 --- a/virtscreen/assets/main.qml +++ b/virtscreen/assets/main.qml @@ -4,6 +4,7 @@ import Qt.labs.platform 1.0 import VirtScreen.DisplayProperty 1.0 import VirtScreen.Backend 1.0 +import VirtScreen.Cursor 1.0 Item { property alias window: mainLoader.item @@ -61,6 +62,11 @@ Item { } } + // virtscreen.py Cursor class. + Cursor { + id: cursor + } + // Timer object and function Timer { id: timer @@ -109,8 +115,8 @@ Item { } }); // Move window to the corner of the primary display - var cursor_x = (backend.cursor_x / window.screen.devicePixelRatio) - window.screen.virtualX; - var cursor_y = (backend.cursor_y / window.screen.devicePixelRatio) - window.screen.virtualY; + var cursor_x = (cursor.x / window.screen.devicePixelRatio) - window.screen.virtualX; + var cursor_y = (cursor.y / window.screen.devicePixelRatio) - window.screen.virtualY; var x_mid = window.screen.width / 2; var y_mid = window.screen.height / 2; var x = window.screen.width - window.width; //(cursor_x > x_mid)? width - window.width : 0; diff --git a/virtscreen/virtscreen.py b/virtscreen/virtscreen.py index 0413c7c..fe09630 100755 --- a/virtscreen/virtscreen.py +++ b/virtscreen/virtscreen.py @@ -416,7 +416,6 @@ class Backend(QObject): onVirtScreenCreatedChanged = pyqtSignal(bool) onVncUsePasswordChanged = pyqtSignal(bool) onVncStateChanged = pyqtSignal(VNCState) - onIPAddressesChanged = pyqtSignal() onDisplaySettingClosed = pyqtSignal() onError = pyqtSignal(str) @@ -525,28 +524,6 @@ class Backend(QObject): self._vncState = state self.onVncStateChanged.emit(self._vncState) - @pyqtProperty('QStringList', notify=onIPAddressesChanged) - def ipAddresses(self): - for interface in interfaces(): - if interface == 'lo': - continue - addresses = ifaddresses(interface).get(AF_INET, None) - if addresses is None: - continue - for link in addresses: - if link is not None: - yield link['addr'] - - @pyqtProperty(int) - def cursor_x(self): - cursor = QCursor().pos() - return cursor.x() - - @pyqtProperty(int) - def cursor_y(self): - cursor = QCursor().pos() - return cursor.y() - # Qt Slots @pyqtSlot(str, int, int, bool, bool) def createVirtScreen(self, device, width, height, portrait, hidpi, pos=''): @@ -707,6 +684,43 @@ class Backend(QObject): QApplication.instance().quit() +class Cursor(QObject): + """ Global mouse cursor position """ + + def __init__(self, parent=None): + super(Cursor, self).__init__(parent) + + @pyqtProperty(int) + def x(self): + cursor = QCursor().pos() + return cursor.x() + + @pyqtProperty(int) + def y(self): + cursor = QCursor().pos() + return cursor.y() + + +class Network(QObject): + """ Backend class for network interfaces """ + onIPAddressesChanged = pyqtSignal() + + def __init__(self, parent=None): + super(Network, self).__init__(parent) + + @pyqtProperty('QStringList', notify=onIPAddressesChanged) + def ipAddresses(self): + for interface in interfaces(): + if interface == 'lo': + continue + addresses = ifaddresses(interface).get(AF_INET, None) + if addresses is None: + continue + for link in addresses: + if link is not None: + yield link['addr'] + + # ------------------------------------------------------------------------------- # Main Code # ------------------------------------------------------------------------------- @@ -800,6 +814,8 @@ def main_gui(): # will be called 'Person' in QML. qmlRegisterType(DisplayProperty, 'VirtScreen.DisplayProperty', 1, 0, 'DisplayProperty') qmlRegisterType(Backend, 'VirtScreen.Backend', 1, 0, 'Backend') + qmlRegisterType(Cursor, 'VirtScreen.Cursor', 1, 0, 'Cursor') + qmlRegisterType(Network, 'VirtScreen.Network', 1, 0, 'Network') # Create a component factory and load the QML script. engine = QQmlApplicationEngine()