Serial-Studio/assets/qml/Windows/MainWindow.qml

392 lines
11 KiB
QML
Raw Normal View History

2021-09-05 02:41:48 -05:00
/*
* Copyright (c) 2020-2021 Alex Spataru <https://github.com/alex-spataru>
*
* 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.
*/
2021-09-30 19:07:39 -05:00
import QtQuick
import QtQuick.Window
import QtQuick.Layouts
import QtQuick.Controls
import Qt.labs.settings
2021-09-05 02:41:48 -05:00
2021-09-25 21:48:06 -05:00
import "../Panes"
import "../Windows"
import "../Widgets"
import "../JsonEditor"
2021-09-05 02:41:48 -05:00
ApplicationWindow {
2021-09-25 21:48:06 -05:00
id: root
2021-09-05 02:41:48 -05:00
//
// Global properties
//
2021-09-25 21:48:06 -05:00
readonly property bool setupVisible: setup.visible
readonly property bool consoleVisible: terminal.visible
readonly property bool dashboardVisible: dashboard.visible
2021-09-05 02:41:48 -05:00
//
2021-09-25 21:48:06 -05:00
// Custom properties
2021-09-05 02:41:48 -05:00
//
property int appLaunchCount: 0
property bool firstChange: true
2021-09-25 21:48:06 -05:00
property bool fullScreen: false
property bool menubarEnabled: true
2021-09-05 02:41:48 -05:00
property bool windowMaximized: false
2021-09-25 21:48:06 -05:00
property bool firstValidFrame: false
property bool automaticUpdates: false
property alias vt100emulation: terminal.vt100emulation
2021-09-05 02:41:48 -05:00
//
2021-09-25 21:48:06 -05:00
// Toolbar functions aliases
2021-09-05 02:41:48 -05:00
//
2021-09-25 21:48:06 -05:00
function showSetup() { toolbar.setupClicked() }
function showConsole() { toolbar.consoleClicked() }
function showDashboard() { toolbar.dashboardClicked() }
2021-09-05 02:41:48 -05:00
//
2021-09-25 21:48:06 -05:00
// Console-related functions
2021-09-05 02:41:48 -05:00
//
2021-09-25 21:48:06 -05:00
function consoleCopy() { terminal.copy() }
function consoleClear() { terminal.clear() }
function consoleSelectAll() { terminal.selectAll() }
2021-09-05 02:41:48 -05:00
//
2021-09-25 21:48:06 -05:00
// Hide/show menubar
2021-09-05 02:41:48 -05:00
//
2021-09-25 21:48:06 -05:00
function toggleMenubar() {
root.menubarEnabled = !root.menubarEnabled
2021-09-05 02:41:48 -05:00
}
//
2021-09-25 21:48:06 -05:00
// Toggle fullscreen state
2021-09-05 02:41:48 -05:00
//
2021-09-25 21:48:06 -05:00
function toggleFullscreen() {
root.fullScreen = !root.fullScreen
if (root.fullScreen)
root.showFullScreen()
else
root.showNormal()
2021-09-05 02:41:48 -05:00
}
//
2021-09-25 21:48:06 -05:00
// Displays the main window & checks for updates
2021-09-05 02:41:48 -05:00
//
2021-09-25 21:48:06 -05:00
function showMainWindow() {
// Startup verifications to ensure that app is displayed inside the screen
if (x < 0 || x >= Screen.desktopAvailableWidth)
x = 100
if (y < 0 || y >= Screen.desktopAvailableHeight)
y = 100
2021-09-05 02:41:48 -05:00
2021-09-25 21:48:06 -05:00
// Startup verifications to ensure that app fits in current screen
if (width > Screen.desktopAvailableWidth) {
x = 100
width = Screen.desktopAvailableWidth - x
}
2021-09-05 02:41:48 -05:00
2021-09-25 21:48:06 -05:00
// Startup verifications to ensure that app fits in current screen
if (height > Screen.desktopAvailableHeight) {
y = 100
height = Screen.desktopAvailableHeight - y
}
2021-09-05 02:41:48 -05:00
2021-09-25 21:48:06 -05:00
// Show app window
if (root.fullScreen)
root.showFullScreen()
else if (root.windowMaximized)
root.showMaximized()
else
root.showNormal()
2021-09-05 02:41:48 -05:00
2021-09-25 21:48:06 -05:00
// Increment app launch count
++appLaunchCount
2021-09-05 02:41:48 -05:00
2021-09-25 21:48:06 -05:00
// Show donations dialog every 15 launches
if (appLaunchCount % 15 == 0 && !donations.doNotShowAgain)
donations.showAutomatically()
2021-09-05 02:41:48 -05:00
2021-09-25 21:48:06 -05:00
// Ask user if he/she wants to enable automatic updates
if (appLaunchCount == 2 && Cpp_UpdaterEnabled) {
if (Cpp_Misc_Utilities.askAutomaticUpdates()) {
root.automaticUpdates = true
Cpp_Updater.checkForUpdates(Cpp_AppUpdaterUrl)
}
2021-09-05 02:41:48 -05:00
2021-09-25 21:48:06 -05:00
else
root.automaticUpdates = false
}
2021-09-05 02:41:48 -05:00
2021-09-25 21:48:06 -05:00
// Check for updates (if we are allowed)
if (automaticUpdates && Cpp_UpdaterEnabled)
Cpp_Updater.checkForUpdates(Cpp_AppUpdaterUrl)
2021-09-05 02:41:48 -05:00
}
//
2021-09-13 11:51:35 -05:00
// Window geometry (different minimum size for non-macOS
// operating systems because of the global menubar in macOS)
2021-09-05 02:41:48 -05:00
//
visible: false
2021-09-13 11:51:35 -05:00
minimumWidth: 1100
2021-09-12 17:54:26 -05:00
title: Cpp_AppName
width: minimumWidth
height: minimumHeight
2021-09-28 14:43:06 -05:00
minimumHeight: Qt.platform.os === "osx" ? 720 : 740
2021-09-05 02:41:48 -05:00
//
// Theme options
//
palette.text: Cpp_ThemeManager.text
palette.buttonText: Cpp_ThemeManager.text
palette.windowText: Cpp_ThemeManager.text
palette.window: Cpp_ThemeManager.windowBackground
background: Rectangle {
color: Cpp_ThemeManager.windowBackground
}
//
// Startup code
//
Component.onCompleted: {
// Load welcome guide
terminal.showWelcomeGuide()
// 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()
}
//
2021-09-25 21:48:06 -05:00
// Hacks to fix window maximized behavior
2021-09-05 02:41:48 -05:00
//
2021-09-28 14:43:06 -05:00
Connections {
target: root
2021-09-05 02:41:48 -05:00
2021-09-28 14:43:06 -05:00
function onVisibilityChanged(visibility) {
if (visibility === Window.Maximized) {
if (!windowMaximized)
firstChange = false
2021-09-05 02:41:48 -05:00
2021-09-28 14:43:06 -05:00
windowMaximized = true
fullScreen = false
}
2021-09-05 02:41:48 -05:00
2021-09-28 14:43:06 -05:00
else if (visibility === Window.FullScreen) {
if (!fullScreen)
firstChange = false
2021-09-24 13:46:56 -05:00
2021-09-28 14:43:06 -05:00
windowMaximized = false
fullScreen = true
2021-09-24 13:46:56 -05:00
}
2021-09-05 02:41:48 -05:00
2021-09-28 14:43:06 -05:00
else if (visibility !== Window.Hidden) {
if (windowMaximized || fullScreen && firstChange) {
root.x = 100
root.y = 100
root.width = root.minimumWidth
root.height = root.minimumHeight
}
fullScreen = false
windowMaximized = false
}
2021-09-25 21:48:06 -05:00
2021-09-28 14:43:06 -05:00
// Hide splash screen
Cpp_ModuleManager.hideSplashscreen()
}
2021-09-05 02:41:48 -05:00
}
//
// Hide console & device manager when we receive first valid frame
2021-09-05 02:41:48 -05:00
//
Connections {
2021-09-25 00:03:27 -05:00
target: Cpp_UI_Dashboard
2021-09-25 21:48:06 -05:00
enabled: !root.firstValidFrame
2021-09-05 02:41:48 -05:00
function onUpdated() {
2021-09-25 21:48:06 -05:00
if ((Cpp_IO_Manager.connected || Cpp_CSV_Player.isOpen) && Cpp_UI_Dashboard.frameValid()) {
2021-09-05 02:41:48 -05:00
setup.hide()
2021-09-25 21:48:06 -05:00
root.showDashboard()
root.firstValidFrame = true
}
else {
2021-09-05 02:41:48 -05:00
setup.show()
2021-09-25 21:48:06 -05:00
root.showConsole()
root.firstValidFrame = false
2021-09-05 02:41:48 -05:00
}
}
}
//
// Show console tab on serial disconnect
//
Connections {
2021-09-25 00:03:27 -05:00
target: Cpp_UI_Dashboard
2021-09-05 02:41:48 -05:00
function onDataReset() {
setup.show()
2021-09-25 21:48:06 -05:00
root.showConsole()
root.firstValidFrame = false
2021-09-05 02:41:48 -05:00
}
}
//
// Save window size & position
//
Settings {
2021-09-25 21:48:06 -05:00
property alias appX: root.x
property alias appY: root.y
property alias appW: root.width
property alias appH: root.height
property alias appStatus: root.appLaunchCount
property alias windowFullScreen: root.fullScreen
property alias autoUpdater: root.automaticUpdates
property alias appMaximized: root.windowMaximized
property alias menubarVisible: root.menubarEnabled
}
//
// Application menubar loader (we need to use a different version in macOS)
//
Loader {
asynchronous: false
source: {
if (Qt.platform.os === "osx")
return "qrc:/qml/PlatformDependent/MenubarMacOS.qml"
return "qrc:/qml/PlatformDependent/Menubar.qml"
}
}
//
// Startup timer
//
Timer {
id: timer
interval: 500
onTriggered: root.showMainWindow()
2021-09-05 02:41:48 -05:00
}
//
// Main layout
//
ColumnLayout {
spacing: 0
anchors.fill: parent
//
// Application toolbar
//
2021-09-05 02:41:48 -05:00
Toolbar {
z: 1
id: toolbar
Layout.fillWidth: true
Layout.minimumHeight: 48
Layout.maximumHeight: 48
2021-09-25 21:48:06 -05:00
setupChecked: root.setupVisible
consoleChecked: root.consoleVisible
dashboardChecked: root.dashboardVisible
2021-09-17 01:41:50 -05:00
onJsonEditorClicked: jsonEditor.show()
2021-09-05 02:41:48 -05:00
onSetupClicked: setup.visible ? setup.hide() : setup.show()
onDashboardClicked: {
2021-09-25 00:03:27 -05:00
if (Cpp_UI_Dashboard.available) {
consoleChecked = 0
dashboardChecked = 1
2021-09-30 19:07:39 -05:00
stack.push(dashboard)
2021-09-05 02:41:48 -05:00
}
else
2021-09-25 21:48:06 -05:00
root.showConsole()
2021-09-05 02:41:48 -05:00
}
onConsoleClicked: {
consoleChecked = 1
dashboardChecked = 0
2021-09-30 19:07:39 -05:00
stack.pop()
2021-09-05 02:41:48 -05:00
}
}
//
// Console, dashboard & setup panel
//
2021-09-05 02:41:48 -05:00
RowLayout {
spacing: 0
clip: true
Layout.fillWidth: true
Layout.fillHeight: true
2021-09-30 19:07:39 -05:00
StackView {
id: stack
clip: true
2021-09-30 19:07:39 -05:00
initialItem: terminal
2021-09-05 02:41:48 -05:00
Layout.fillWidth: true
Layout.fillHeight: true
2021-09-30 19:07:39 -05:00
onCurrentItemChanged: {
if (currentItem === terminal) {
terminal.opacity = 1
dashboard.opacity = 0
}
2021-09-05 02:41:48 -05:00
else {
terminal.opacity = 0
dashboard.opacity = 1
2021-09-05 02:41:48 -05:00
}
}
Console {
id: terminal
enabled: opacity > 0
2021-09-05 02:41:48 -05:00
visible: opacity > 0
Layout.fillWidth: true
Layout.fillHeight: true
2021-09-05 02:41:48 -05:00
}
Dashboard {
2021-09-30 19:07:39 -05:00
opacity: 0
id: dashboard
2021-09-05 02:41:48 -05:00
enabled: opacity > 0
visible: opacity > 0
Layout.fillWidth: true
Layout.fillHeight: true
2021-09-05 02:41:48 -05:00
}
}
Setup {
id: setup
2021-09-30 19:07:39 -05:00
opacity: 1
setupMargin: 0
2021-09-05 02:41:48 -05:00
Layout.fillHeight: true
Layout.rightMargin: setupMargin
2021-09-05 02:41:48 -05:00
Layout.minimumWidth: displayedWidth
Layout.maximumWidth: displayedWidth
}
}
}
//
// JSON project drop area
2021-09-05 02:41:48 -05:00
//
JSONDropArea {
2021-09-05 02:41:48 -05:00
anchors.fill: parent
2021-09-28 14:43:06 -05:00
enabled: !Cpp_IO_Manager.connected
2021-09-05 02:41:48 -05:00
}
}