1
0
Fork 0
mirror of https://github.com/kbumsik/VirtScreen.git synced 2025-03-09 15:40:18 +00:00
VirtScreen/main.qml

452 lines
15 KiB
QML
Raw Normal View History

2018-05-06 12:07:47 -04:00
import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Controls.Material 2.3
2018-05-06 12:07:47 -04:00
import QtQuick.Layouts 1.3
2018-05-07 16:15:41 -04:00
import QtQuick.Window 2.2
2018-05-06 12:07:47 -04:00
import Qt.labs.platform 1.0 as Labs
import VirtScreen.DisplayProperty 1.0
2018-05-06 12:07:47 -04:00
import VirtScreen.Backend 1.0
ApplicationWindow {
id: window
2018-05-07 16:15:41 -04:00
visible: false
flags: Qt.FramelessWindowHint
2018-05-06 12:07:47 -04:00
title: "Basic layouts"
Material.theme: Material.Light
2018-05-10 20:58:26 -04:00
Material.primary: Material.Teal
Material.accent: Material.Teal
2018-05-10 20:58:26 -04:00
// Material.background: Material.Grey
2018-05-06 12:07:47 -04:00
property int margin: 11
width: 380
2018-05-10 20:58:26 -04:00
height: 550
2018-05-06 12:07:47 -04:00
2018-05-07 16:15:41 -04:00
// hide screen when loosing focus
onActiveFocusItemChanged: {
if ((!activeFocusItem) && (!sysTrayIcon.clicked)) {
2018-05-07 16:15:41 -04:00
this.hide();
}
}
// virtscreen.py backend.
Backend {
id: backend
}
property bool vncAutoStart: false
function switchVNC () {
if ((backend.vncState == Backend.OFF) && backend.virtScreenCreated) {
backend.startVNC();
}
}
onVncAutoStartChanged: {
if (vncAutoStart) {
backend.onVirtScreenCreatedChanged.connect(switchVNC);
backend.onVncStateChanged.connect(switchVNC);
} else {
backend.onVirtScreenCreatedChanged.disconnect(switchVNC);
backend.onVncStateChanged.disconnect(switchVNC);
}
}
2018-05-06 12:07:47 -04:00
// Timer object and function
Timer {
id: timer
function setTimeout(cb, delayTime) {
timer.interval = delayTime;
timer.repeat = false;
timer.triggered.connect(cb);
timer.triggered.connect(function() {
timer.triggered.disconnect(cb);
});
timer.start();
}
2018-05-06 12:07:47 -04:00
}
2018-05-10 20:58:26 -04:00
// menuBar: MenuBar {
// }
2018-05-06 12:07:47 -04:00
header: TabBar {
id: tabBar
2018-05-10 20:58:26 -04:00
position: TabBar.Footer
// Material.primary: Material.Teal
2018-05-06 12:07:47 -04:00
currentIndex: 0
TabButton {
text: qsTr("Display")
}
TabButton {
text: qsTr("VNC")
}
}
2018-05-10 20:58:26 -04:00
menuBar: ToolBar {
id: toolbar
font.weight: Font.Medium
font.pointSize: 11 //parent.font.pointSize + 1
RowLayout {
anchors.fill: parent
2018-05-10 20:58:26 -04:00
anchors.leftMargin: margin + 10
Label {
id: vncStateLabel
text: !backend.virtScreenCreated ? "Enable Virtual Screen first." :
backend.vncState == Backend.OFF ? "Turn on VNC Server in the VNC tab." :
backend.vncState == Backend.WAITING ? "VNC Server is waiting for a client..." :
backend.vncState == Backend.CONNECTED ? "Connected." :
"Server state error!"
}
}
}
// footer: ToolBar {
// font.weight: Font.Medium
// font.pointSize: 11 //parent.font.pointSize + 1
// anchors { horizontalCenter: parent.horizontalCenter }
// width: 200
// }
Popup {
id: busyDialog
modal: true
closePolicy: Popup.NoAutoClose
x: (parent.width - width) / 2
y: (parent.height - height) / 2
BusyIndicator {
anchors.fill: parent
2018-05-10 20:58:26 -04:00
Material.accent: Material.Cyan
running: true
}
background: Rectangle {
color: "transparent"
implicitWidth: 100
implicitHeight: 100
// border.color: "#444"
}
}
2018-05-06 12:07:47 -04:00
StackLayout {
width: parent.width
currentIndex: tabBar.currentIndex
ColumnLayout {
anchors.fill: parent
anchors.margins: margin
2018-05-06 12:07:47 -04:00
GroupBox {
title: "Virtual Display"
// font.bold: true
anchors.left: parent.left
anchors.right: parent.right
enabled: backend.virtScreenCreated ? false : true
2018-05-06 12:07:47 -04:00
ColumnLayout {
anchors.left: parent.left
anchors.right: parent.right
2018-05-06 12:07:47 -04:00
RowLayout {
Label { text: "Width"; Layout.fillWidth: true }
SpinBox {
value: backend.virt.width
2018-05-06 12:07:47 -04:00
from: 640
to: 1920
stepSize: 1
editable: true
onValueModified: {
backend.virt.width = value;
}
textFromValue: function(value, locale) { return value; }
2018-05-06 12:07:47 -04:00
}
}
RowLayout {
Label { text: "Height"; Layout.fillWidth: true }
SpinBox {
value: backend.virt.height
2018-05-06 12:07:47 -04:00
from: 360
to: 1080
stepSize : 1
editable: true
onValueModified: {
backend.virt.height = value;
}
textFromValue: function(value, locale) { return value; }
2018-05-06 12:07:47 -04:00
}
}
RowLayout {
Label { text: "Portrait Mode"; Layout.fillWidth: true }
Switch {
checked: backend.portrait
onCheckedChanged: {
backend.portrait = checked;
}
}
2018-05-06 12:07:47 -04:00
}
RowLayout {
Label { text: "HiDPI (2x resolution)"; Layout.fillWidth: true }
Switch {
checked: backend.hidpi
onCheckedChanged: {
backend.hidpi = checked;
}
}
2018-05-06 12:07:47 -04:00
}
RowLayout {
anchors.left: parent.left
anchors.right: parent.right
Label { id: deviceLabel; text: "Device"; }
ComboBox {
id: deviceComboBox
anchors.left: deviceLabel.right
anchors.right: parent.right
anchors.leftMargin: 100
textRole: "name"
2018-05-10 12:15:28 -04:00
model: backend.screens
currentIndex: backend.virtScreenIndex
onActivated: function(index) {
backend.virtScreenIndex = index
}
delegate: ItemDelegate {
width: deviceComboBox.width
text: modelData.name
font.weight: deviceComboBox.currentIndex === index ? Font.DemiBold : Font.Normal
highlighted: ListView.isCurrentItem
enabled: modelData.connected ? false : true
}
}
}
2018-05-06 12:07:47 -04:00
}
}
Button {
id: virtScreenButton
text: backend.virtScreenCreated ? "Disable Virtual Screen" : "Enable Virtual Screen"
2018-05-10 20:58:26 -04:00
highlighted: true
anchors.left: parent.left
anchors.right: parent.right
2018-05-10 20:58:26 -04:00
// Material.accent: Material.Teal
// Material.theme: Material.Dark
enabled: window.vncAutoStart ? true :
backend.vncState == Backend.OFF ? true : false
onClicked: {
busyDialog.open();
// Give a very short delay to show busyDialog.
timer.setTimeout (function() {
if (!backend.virtScreenCreated) {
backend.createVirtScreen();
} else {
function autoOff() {
console.log("autoOff called here", backend.vncState);
if (backend.vncState == Backend.OFF) {
console.log("Yes. Delete it");
backend.deleteVirtScreen();
window.vncAutoStart = true;
}
}
if (window.vncAutoStart && (backend.vncState != Backend.OFF)) {
window.vncAutoStart = false;
backend.onVncStateChanged.connect(autoOff);
backend.onVncStateChanged.connect(function() {
backend.onVncStateChanged.disconnect(autoOff);
});
backend.stopVNC();
} else {
backend.deleteVirtScreen();
}
}
}, 200);
}
Component.onCompleted: {
backend.onVirtScreenCreatedChanged.connect(function(created) {
busyDialog.close();
});
}
2018-05-06 12:07:47 -04:00
}
}
ColumnLayout {
anchors.fill: parent
anchors.margins: margin
2018-05-06 12:07:47 -04:00
GroupBox {
title: "VNC Server"
anchors.left: parent.left
anchors.right: parent.right
enabled: backend.vncState == Backend.OFF ? true : false
2018-05-06 12:07:47 -04:00
ColumnLayout {
anchors.left: parent.left
anchors.right: parent.right
2018-05-06 12:07:47 -04:00
RowLayout {
Label { text: "Port"; Layout.fillWidth: true }
SpinBox {
value: backend.vncPort
2018-05-06 12:07:47 -04:00
from: 1
to: 65535
stepSize: 1
editable: true
onValueModified: {
backend.vncPort = value;
}
textFromValue: function(value, locale) { return value; }
2018-05-06 12:07:47 -04:00
}
}
RowLayout {
anchors.left: parent.left
anchors.right: parent.right
Label { id: passwordLabel; text: "Password" }
2018-05-06 12:07:47 -04:00
TextField {
anchors.left: passwordLabel.right
anchors.right: parent.right
anchors.margins: margin
2018-05-06 12:07:47 -04:00
placeholderText: "Password";
text: backend.vncPassword;
2018-05-06 12:07:47 -04:00
echoMode: TextInput.Password;
onTextEdited: {
backend.vncPassword = text;
}
2018-05-06 12:07:47 -04:00
}
}
}
}
Button {
id: vncButton
anchors.left: parent.left
anchors.right: parent.right
anchors.bottomMargin: 0
2018-05-10 20:58:26 -04:00
highlighted: true
text: window.vncAutoStart ? "Auto start enabled" :
backend.vncState == Backend.OFF ? "Start VNC Server" : "Stop VNC Server"
enabled: window.vncAutoStart ? false :
backend.virtScreenCreated ? true : false
2018-05-06 12:07:47 -04:00
// Material.background: Material.Teal
// Material.foreground: Material.Grey
onClicked: backend.vncState == Backend.OFF ? backend.startVNC() : backend.stopVNC()
2018-05-06 12:07:47 -04:00
}
2018-05-10 12:15:28 -04:00
RowLayout {
anchors.top: vncButton.top
anchors.right: parent.right
anchors.topMargin: vncButton.height - 10
Label { text: "Auto start"; }
Switch {
checked: window.vncAutoStart
onCheckedChanged: {
if ((checked == true) && (backend.vncState == Backend.OFF) &&
backend.virtScreenCreated) {
backend.startVNC();
}
window.vncAutoStart = checked;
}
}
}
2018-05-10 12:15:28 -04:00
ListView {
2018-05-10 20:58:26 -04:00
id: ipListView
2018-05-10 12:15:28 -04:00
height: 200
anchors.left: parent.left
anchors.right: parent.right
model: backend.ipAddresses
delegate: Text {
text: modelData
}
}
}
2018-05-06 12:07:47 -04:00
}
// Sytray Icon
Labs.SystemTrayIcon {
id: sysTrayIcon
iconSource: "icon/icon.png"
visible: true
property bool clicked: false
2018-05-06 12:07:47 -04:00
onMessageClicked: console.log("Message clicked")
Component.onCompleted: {
// without delay, the message appears in a wierd place
timer.setTimeout (function() {
2018-05-07 23:45:17 -04:00
showMessage("VirtScreen is running",
"The program will keep running in the system tray.\n" +
"To terminate the program, choose \"Quit\" in the \n" +
"context menu of the system tray entry.");
}, 1500);
2018-05-06 12:07:47 -04:00
}
onActivated: function(reason) {
console.log(reason);
if (reason == Labs.SystemTrayIcon.Context) {
return;
}
2018-05-07 16:15:41 -04:00
if (window.visible) {
window.hide();
return;
}
sysTrayIcon.clicked = true;
2018-05-07 16:15:41 -04:00
// Move window to the corner of the primary display
var primary = backend.primary;
var width = primary.width;
var height = primary.height;
2018-05-10 00:22:29 -04:00
var cursor_x = backend.cursor_x - primary.x_offset;
var cursor_y = backend.cursor_y - primary.y_offset;
2018-05-07 16:15:41 -04:00
var x_mid = width / 2;
var y_mid = height / 2;
2018-05-10 00:22:29 -04:00
var x = width - window.width; //(cursor_x > x_mid)? width - window.width : 0;
var y = (cursor_y > y_mid)? height - window.height : 0;
x += primary.x_offset;
y += primary.y_offset;
window.x = x;
window.y = y;
window.show();
window.raise();
window.requestActivate();
timer.setTimeout (function() {
sysTrayIcon.clicked = false;
}, 200);
2018-05-06 12:07:47 -04:00
}
menu: Labs.Menu {
Labs.MenuItem {
text: qsTr("&Quit")
2018-05-11 15:25:20 -04:00
onTriggered: {
backend.onVirtScreenCreatedChanged.disconnect(window.switchVNC);
backend.onVncStateChanged.disconnect(window.switchVNC);
backend.quitProgram();
}
2018-05-06 12:07:47 -04:00
}
}
}
}