640 lines
17 KiB
QML
Raw Normal View History

2020-10-18 06:50:26 -05:00
/*
* Copyright (c) 2020-2021 Alex Spataru <https://github.com/alex-spataru>
2020-10-18 06:50:26 -05:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import QtQuick 2.12
import QtQuick.Dialogs 1.1
2020-12-26 01:30:18 -06:00
import QtQuick.Window 2.12
2020-10-18 06:50:26 -05:00
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12
2020-12-27 17:21:19 -06:00
import Qt.labs.settings 1.0
2020-12-26 01:30:18 -06:00
import "Windows"
2020-10-18 06:50:26 -05:00
2020-12-27 17:21:19 -06:00
ApplicationWindow {
2020-10-18 06:50:26 -05:00
id: app
//
// Global properties
//
readonly property int spacing: 8
2020-12-27 23:53:09 -06:00
property bool firstValidPacket: false
2021-01-13 14:38:12 -05:00
readonly property color windowBackgroundColor: "#121920"
2020-10-18 06:50:26 -05:00
readonly property string monoFont: {
switch (Qt.platform.os) {
case "osx":
2021-02-20 15:19:35 -05:00
return "Monaco"
2020-10-18 06:50:26 -05:00
case "windows":
return "Consolas"
default:
return "Monospace"
}
}
//
// We use this variable to ask the user if he/she wants to enable/disable
// automatic update checking on the second run
//
property int appLaunchStatus: 0
property bool automaticUpdates: false
//
// Hacks to fix window maximized behavior
//
property bool firstChange: true
property bool windowMaximized: false
onVisibilityChanged: {
if (visibility == Window.Maximized) {
if (!windowMaximized)
firstChange = false
windowMaximized = true
2021-02-18 23:41:10 -05:00
fullScreen = false
}
else if (visibility === Window.FullScreen) {
if (!fullScreen)
firstChange = false
windowMaximized = false
fullScreen = true
}
else if (visibility !== Window.Hidden) {
2021-02-18 23:41:10 -05:00
if (windowMaximized || fullScreen && firstChange) {
app.x = 100
app.y = 100
app.width = app.minimumWidth
app.height = app.minimumHeight
}
2021-02-18 23:41:10 -05:00
fullScreen = false
windowMaximized = false
}
2021-02-18 23:41:10 -05:00
}
2021-02-17 14:36:39 -05:00
//
// Application UI status variables (used for the menubar)
//
property alias vt100emulation: terminal.vt100emulation
readonly property bool setupVisible: setup.visible
readonly property bool dashboardVisible: data.visible
readonly property bool widgetsVisible: widgets.visible
readonly property bool consoleVisible: terminal.visible
readonly property bool dashboardAvailable: Cpp_UI_Provider.groupCount > 0
readonly property bool widgetsAvailable: Cpp_UI_WidgetProvider.totalWidgetCount > 0
2021-02-17 14:47:09 -05:00
//
// Menubar status
//
property bool menubarEnabled: true
2021-02-18 23:41:10 -05:00
//
// Fullscreen status
//
property bool fullScreen: false
2021-02-17 14:36:39 -05:00
//
// Check for updates (non-silent mode)
//
function checkForUpdates() {
Cpp_Updater.setNotifyOnFinish(Cpp_AppUpdaterUrl, true)
Cpp_Updater.checkForUpdates(Cpp_AppUpdaterUrl)
}
//
// Display about dialog
//
function showAbout() {
about.show()
}
//
// Display the console
//
function showConsole() {
toolbar.consoleClicked()
}
//
// Display the dashboard
//
function showDashboard() {
toolbar.dataClicked()
}
//
// Display the widgets
//
function showWidgets() {
toolbar.widgetsClicked()
}
//
// Toggle preferences pane
//
function togglePreferences() {
toolbar.setupClicked()
}
//
// Clears console output
//
function clearConsole() {
terminal.clearConsole()
}
//
// Copy console selection
//
function copyConsole() {
terminal.copy()
}
2021-02-17 14:47:09 -05:00
//
// Hide/show menubar
//
function toggleMenubar() {
app.menubarEnabled = !app.menubarEnabled
}
2021-02-17 14:36:39 -05:00
//
// Select all console text
//
function selectAllConsole() {
terminal.selectAll()
}
2021-02-18 23:41:10 -05:00
//
// Toggle fullscreen state
//
function toggleFullscreen() {
app.fullScreen = !app.fullScreen
if (app.fullScreen)
app.showFullScreen()
else
app.showNormal()
}
2020-10-18 06:50:26 -05:00
//
2020-12-27 17:21:19 -06:00
// Window geometry
2020-10-18 06:50:26 -05:00
//
2020-12-27 17:21:19 -06:00
visible: false
minimumWidth: 1040
title: Cpp_AppName + " v" + Cpp_AppVersion
2021-03-01 17:50:29 -05:00
minimumHeight: Qt.platform.os == "osx" ? 660 : 680
2020-10-18 06:50:26 -05:00
//
2020-12-27 17:21:19 -06:00
// Theme options
2020-10-18 06:50:26 -05:00
//
2021-01-13 14:38:12 -05:00
palette.text: "#fff"
palette.buttonText: "#fff"
palette.windowText: "#fff"
palette.window: app.windowBackgroundColor
2021-02-20 15:19:35 -05:00
background: Rectangle {
color: app.windowBackgroundColor
}
2020-10-18 06:50:26 -05:00
//
2020-12-27 17:21:19 -06:00
// Startup code
2020-12-26 01:30:18 -06:00
//
2020-12-27 17:21:19 -06:00
Component.onCompleted: {
// Hide dialogs, show devices pane
2020-12-27 17:21:19 -06:00
about.hide()
2021-01-20 15:06:40 -05:00
setup.show()
csvPlayer.hide()
// Hide everything except the terminal
2020-12-27 17:21:19 -06:00
data.opacity = 0
widgets.opacity = 0
terminal.opacity = 1
2020-12-27 17:21:19 -06:00
// Load welcome guide
terminal.showWelcomeGuide()
// Load JSON map file (if any)
Cpp_JSON_Generator.readSettings()
// Display the window & check for updates in 500 ms (we do this so that
// we wait for the window to read settings before showing it)
timer.start()
2020-12-26 01:30:18 -06:00
}
2020-10-18 06:50:26 -05:00
2021-02-06 21:58:08 -05:00
//
2021-02-17 14:36:39 -05:00
// Application menubar loader (we need to use a different version in macOS)
2021-02-06 21:58:08 -05:00
//
2021-02-17 14:36:39 -05:00
Loader {
asynchronous: false
source: {
if (Qt.platform.os === "osx")
return "qrc:/qml/PlatformDependent/MenubarMacOS.qml"
2021-02-06 21:58:08 -05:00
2021-02-17 14:36:39 -05:00
return "qrc:/qml/PlatformDependent/Menubar.qml"
2021-02-09 21:03:20 -05:00
}
2021-02-06 21:58:08 -05:00
}
2020-10-18 06:50:26 -05:00
//
2020-12-27 17:21:19 -06:00
// Startup timer
//
Timer {
id: timer
interval: 500
onTriggered: {
// Startup verifications to ensure that bad settings
// do not make our app reside outside screen
if (x < 0 || x >= Screen.desktopAvailableWidth)
x = 100
if (y < 0 || y >= Screen.desktopAvailableHeight)
y = 100
2020-10-18 06:50:26 -05:00
2020-12-27 17:21:19 -06:00
// Startup verifications to ensure that app fits in current screen
if (width > Screen.desktopAvailableWidth) {
x = 100
width = Screen.desktopAvailableWidth - x
}
2020-12-26 01:30:18 -06:00
2020-12-27 17:21:19 -06:00
// Startup verifications to ensure that app fits in current screen
if (height > Screen.desktopAvailableHeight) {
y = 100
height = Screen.desktopAvailableHeight - y
2020-10-18 06:50:26 -05:00
}
2020-12-27 17:21:19 -06:00
// Show app window
2021-02-18 23:41:10 -05:00
if (app.fullScreen)
app.showFullScreen()
else if (app.windowMaximized)
app.showMaximized()
else
app.showNormal()
// Increment app launch count until 3:
// Value & meaning:
// - 1: first launch
// - 2: second launch, ask to enable automatic updater
// - 3: we don't care the number of times the user launched the app
if (appLaunchStatus < 3)
++appLaunchStatus
// Second launch ask user if he/she wants to enable automatic updates
if (appLaunchStatus == 2)
2021-02-05 08:34:47 -05:00
automaticUpdatesMessageDialog.visible = Cpp_UpdaterEnabled
// Check for updates (if we are allowed)
2021-02-05 08:34:47 -05:00
if (automaticUpdates && Cpp_UpdaterEnabled)
Cpp_Updater.checkForUpdates(Cpp_AppUpdaterUrl)
2020-10-18 06:50:26 -05:00
}
}
2020-12-27 23:53:09 -06:00
//
// Hide console & device manager when we receive first valid packet
//
Connections {
2021-02-17 20:18:27 -05:00
target: Cpp_UI_Provider
2020-12-27 23:53:09 -06:00
enabled: !app.firstValidPacket
2021-02-17 20:18:27 -05:00
function onUpdated() {
if ((Cpp_IO_Manager.connected || Cpp_CSV_Player.isOpen) && Cpp_UI_Provider.frameValid()) {
2021-02-13 21:03:30 -05:00
app.firstValidPacket = true
setup.hide()
2021-02-17 20:18:27 -05:00
app.showDashboard()
2021-02-13 21:03:30 -05:00
} else {
toolbar.consoleClicked()
setup.show()
app.firstValidPacket = false
}
2020-12-27 23:53:09 -06:00
}
}
2021-01-05 23:20:38 -05:00
//
// Show console tab on serial disconnect
//
Connections {
target: Cpp_UI_Provider
function onDataReset() {
2021-01-05 23:20:38 -05:00
toolbar.consoleClicked()
2021-01-20 15:06:40 -05:00
setup.show()
app.firstValidPacket = false
2021-01-05 23:20:38 -05:00
}
}
2020-10-18 06:50:26 -05:00
//
2020-12-27 17:21:19 -06:00
// Save window size & position
2020-10-18 06:50:26 -05:00
//
2021-02-14 14:14:29 -05:00
Settings {
2020-12-27 17:21:19 -06:00
property alias appX: app.x
property alias appY: app.y
property alias appW: app.width
property alias appH: app.height
property alias appStatus: app.appLaunchStatus
2021-02-18 23:41:10 -05:00
property alias windowFullScreen: app.fullScreen
property alias autoUpdater: app.automaticUpdates
property alias appMaximized: app.windowMaximized
2021-02-17 14:47:09 -05:00
property alias menubarVisible: app.menubarEnabled
2021-02-14 14:14:29 -05:00
}
2020-12-26 01:30:18 -06:00
//
2020-12-27 17:21:19 -06:00
// Main layout
2020-12-26 01:30:18 -06:00
//
2020-12-27 17:21:19 -06:00
ColumnLayout {
spacing: 0
anchors.fill: parent
Toolbar {
z: 1
id: toolbar
Layout.fillWidth: true
Layout.minimumHeight: 48
Layout.maximumHeight: 48
2021-02-17 14:36:39 -05:00
setupChecked: app.setupVisible
dataChecked: app.dashboardVisible
widgetsChecked: app.widgetsVisible
consoleChecked: app.consoleVisible
2021-01-20 15:06:40 -05:00
onSetupClicked: setup.visible ? setup.hide() : setup.show()
2020-12-27 17:21:19 -06:00
onDataClicked: {
2021-02-17 14:36:39 -05:00
if (app.dashboardAvailable) {
data.opacity = 1
terminal.opacity = 0
widgets.opacity = 0
dataChecked = true
consoleChecked = false
widgetsChecked = false
}
else
app.showConsole()
2020-12-27 17:21:19 -06:00
}
onConsoleClicked: {
2021-02-17 14:36:39 -05:00
data.opacity = 0
terminal.opacity = 1
2021-02-17 14:36:39 -05:00
widgets.opacity = 0
consoleChecked = true
dataChecked = false
widgetsChecked = false
2020-12-27 17:21:19 -06:00
}
onWidgetsClicked: {
2021-02-17 14:36:39 -05:00
if (app.widgetsAvailable) {
data.opacity = 0
terminal.opacity = 0
widgets.opacity = 1
dataChecked = false
widgetsChecked = true
consoleChecked = false
}
else
app.showConsole()
2020-12-27 17:21:19 -06:00
}
}
RowLayout {
spacing: 0
clip: true
Layout.fillWidth: true
Layout.fillHeight: true
Item {
Layout.fillWidth: true
Layout.fillHeight: true
Console {
id: terminal
2020-12-27 17:21:19 -06:00
anchors.fill: parent
// Animate on show
enabled: opacity > 0
visible: opacity > 0
Behavior on opacity {NumberAnimation{}}
2021-01-04 22:11:59 -06:00
// Show translated welcome text on lang. change
Connections {
target: Cpp_Misc_Translator
2021-01-04 22:11:59 -06:00
function onLanguageChanged() {
terminal.showWelcomeGuide()
2021-01-04 22:11:59 -06:00
}
}
2020-12-27 17:21:19 -06:00
}
DataGrid {
id: data
anchors.fill: parent
// Animate on show
visible: opacity > 0
Behavior on opacity {NumberAnimation{}}
}
Widgets {
id: widgets
anchors.fill: parent
// Animate on show
enabled: opacity > 0
visible: opacity > 0
Behavior on opacity {NumberAnimation{}}
}
}
2021-01-20 15:06:40 -05:00
Setup {
id: setup
2021-02-01 18:55:15 -05:00
property int displayedWidth: 340
2020-12-27 17:21:19 -06:00
function show() {
opacity = 1
displayedWidth = 320
}
function hide() {
opacity = 0
displayedWidth = 0
}
visible: opacity > 0
Layout.fillHeight: true
Layout.minimumWidth: displayedWidth
Layout.maximumWidth: displayedWidth
Behavior on opacity {NumberAnimation{}}
Behavior on displayedWidth {NumberAnimation{}}
}
2020-12-26 01:53:38 -06:00
}
2020-10-18 06:50:26 -05:00
}
2021-01-04 23:33:50 -06:00
//
// About window
//
About {
id: about
}
2021-01-04 23:33:50 -06:00
//
// CSV player window
//
CsvPlayer {
id: csvPlayer
}
//
// Enable/disable automatic updates dialog
//
MessageDialog {
id: automaticUpdatesMessageDialog
title: Cpp_AppName
icon: StandardIcon.Question
modality: Qt.ApplicationModal
standardButtons: StandardButton.Yes | StandardButton.No
text: "<h3>" + qsTr("Check for updates automatically?") + "</h3>"
informativeText: qsTr("Should %1 automatically check for updates? " +
"You can always check for updates manually from " +
2021-02-17 14:36:39 -05:00
"the \"Help\" menu").arg(Cpp_AppName);
// Behavior when the user clicks on "Yes"
onAccepted: {
app.automaticUpdates = true
2021-02-05 08:34:47 -05:00
Cpp_Updater.checkForUpdates(Cpp_AppUpdaterUrl)
}
// Behavior when the user clicks on "No"
onRejected: {
app.automaticUpdates = false
}
}
//
// Intuitive UI stuff for drag and drop
//
Rectangle {
id: dropRectangle
function hide() {
rectTimer.start()
}
opacity: 0
border.width: 1
border.color: "#fff"
color: palette.highlight
anchors.centerIn: parent
width: dropLayout.implicitWidth + 6 * app.spacing
height: dropLayout.implicitHeight + 6 * app.spacing
ColumnLayout {
id: dropLayout
spacing: app.spacing * 2
anchors.centerIn: parent
ToolButton {
flat: true
enabled: false
icon.width: 128
icon.height: 128
icon.color: "#fff"
Layout.alignment: Qt.AlignHCenter
icon.source: "qrc:/icons/drag-drop.svg"
}
Label {
color: "#fff"
font.bold: true
font.pixelSize: 24
Layout.alignment: Qt.AlignHCenter
text: qsTr("Drop JSON and CSV files here")
}
}
Timer {
id: rectTimer
interval: 200
onTriggered: dropRectangle.opacity = 0
}
Behavior on opacity {NumberAnimation{}}
}
//
// File drop area
//
DropArea {
id: dropArea
anchors.fill: parent
enabled: terminal.visible
//
// Show rectangle and set color based on file extension on drag enter
//
onEntered: {
// Get file name & set color of rectangle accordingly
var path = drag.urls[0].toString()
if (path.endsWith(".json") || path.endsWith(".csv")) {
drag.accept(Qt.LinkAction)
dropRectangle.color = Qt.darker(palette.highlight, 1.4)
}
// Invalid file name, show red rectangle
else
2021-01-22 12:46:11 -05:00
dropRectangle.color = "#d72d60"
// Show drag&drop rectangle
dropRectangle.opacity = 0.8
}
//
// Open *.json & *.csv files on drag drop
//
onDropped: {
// Hide rectangle
dropRectangle.hide()
// Get dropped file URL and remove prefixed "file://"
var path = drop.urls[0].toString()
if (Qt.platform.os != "windows")
path = path.replace(/^(file:\/{2})/,"");
else
path = path.replace(/^(file:\/{3})/,"");
// Unescape html codes like '%23' for '#'
var cleanPath = decodeURIComponent(path);
// Process JSON files
if (cleanPath.endsWith(".json")) {
Cpp_JSON_Generator.setOperationMode(0)
Cpp_JSON_Generator.loadJsonMap(cleanPath, false)
}
// Process CSV files
else if (cleanPath.endsWith(".csv"))
Cpp_CSV_Player.openFile(cleanPath)
}
//
// Hide drag & drop rectangle on drag exit
//
onExited: {
dropRectangle.hide()
}
}
2020-10-18 06:50:26 -05:00
}