import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
import QtQuick.Scene3D 2.14
import Qt3D.Core 2.14
import Qt3D.Render 2.14
import Qt3D.Input 2.14
import Qt3D.Extras 2.14

import QtQuick 2.14 as QQ2
import QtQuick.Dialogs 1.3
import QtQuick.Window 2.2
import QtQuick.Controls 2.5

Pandar_root {
    Connections {
        target: selectArea
        onSelectEnabledChanged: {
            modelData.selectEnabled = selectArea.selectEnabled   
        }
    }
    Connections {
        target: selectArea
        onSelectionChanged: {
            modelData.selectionMatrix = selectionMatrix
        }
    }
    
    DefaultMaterial {
        id: pointxyziMaterial
    }

    DefaultMaterialSplit {
        id: pointxyziMaterialSplit
    }

    DefaultGeometryRenderer {
        id: pointxyziGeometry
    }
    DefaultGeometryRenderer {
        id: pointxyziGeometrySplit
    }

    Transform {
        id: pointxyziTransform
    }
    Transform {
        id: pointxyziTransformSplit
    }

    DefaultObjectPicker {
        id: pointxyziPicker
    }

    MouseHandler {
        id: pointxyziMouseHandler
        sourceDevice: mouseDevice
        onWheel: {}
    }
    components: modelData.splitWindow ? [pointxyziMaterial, pointxyziGeometry, pointxyziTransform, pointxyziPicker, pointxyziMouseHandler,layer2] : [pointxyziMaterial, pointxyziGeometry, pointxyziTransform, pointxyziPicker, pointxyziMouseHandler]
    
    Entity {
       components: modelData.splitWindow ? [pointxyziMaterialSplit, pointxyziGeometrySplit, pointxyziTransformSplit,layer1] : [] 
    }

    EntityLoader {
        id: lineEntityLoader
        property var begin: Qt.vector3d(0, 0, 0)
        property var end: Qt.vector3d(0, 0, 0)
    }

    EntityLoader {
        id: lasersEntityLoader
        source: ""
    }

    FileDialog {
        id: fileDialog
        property int mode: 0
        title: "Please choose import file"
        nameFilters: [".dat/.csv/.txt (*.dat *.csv *.txt)", "ALL (*.*)"]
        onAccepted: {
            switch (mode) {
                case 1:
                    if(modelData.importCorrectionsFile(fileDialog.fileUrl)) {
                        console.log("Succeed to import: " + fileDialog.fileUrl)
                        var src = config.url2string(fileDialog.fileUrl)
                        var filename = src.split('/').pop()
                        var dst = config.getDocuments()+"/PandarViewDataFiles/dat/"+filename;
                        if (src != dst) {
                            if(config.exists(dst))
                                config.remove(dst)
                            if(config.copy(src, dst)) {
                                correctionComboBox.refresh()
                                for (var i in correctionComboBox.data_list) {
                                    if (correctionComboBox.data_list[i].text == filename) {
                                        correctionComboBox.currentIndex = i
                                        break
                                    }
                                }
                                console.log("correctionComboBox.currentText:" + correctionComboBox.currentText)
                            }
                            else
                                controlPanel.messageDialogOpen("Unable to copy from: " + src + " to: " + dst)
                        }
                        else {
                            for (var i in correctionComboBox.data_list) {
                                if (correctionComboBox.data_list[i].text == filename) {
                                    correctionComboBox.currentIndex = i
                                    break
                                }
                            }
                            console.log("correctionComboBox.currentText:" + correctionComboBox.currentText)
                        }
                    } else {
                        console.log("Unable to import: " + fileDialog.fileUrl)
                        controlPanel.messageDialogOpen("Unable to import: \n" + fileDialog.fileUrl)
                    }
                    break;
                case 2:
                    if(modelData.exportCorrectionsFile(fileDialog.fileUrl)) {
                        console.log("Succeed to export: " + fileDialog.fileUrl)
                    } else {
                        console.log("Unable to export: " + fileDialog.fileUrl)
                        controlPanel.messageDialogOpen("Unable to export: \n" + fileDialog.fileUrl)
                    }
                    break;
                case 3:
                    if(modelData.importFiretimesFile(fileDialog.fileUrl)) {
                        console.log("Succeed to import: " + fileDialog.fileUrl)
                        var src = config.url2string(fileDialog.fileUrl)
                        var filename = src.split('/').pop()
                        var dst = config.getDocuments()+"/PandarViewDataFiles/csv/firetimes/"+filename;
                        if (src != dst) {
                            if(config.exists(dst))
                                config.remove(dst)
                            if(config.copy(src, dst)) {
                                firetimeComboBox.refresh()
                                for (var i in firetimeComboBox.data_list) {
                                    if (firetimeComboBox.data_list[i].text == filename) {
                                        firetimeComboBox.currentIndex = i
                                        break
                                    }
                                }
                                console.log("firetimeComboBox.currentText:" + firetimeComboBox.currentText)
                            }
                            else
                                controlPanel.messageDialogOpen("Unable to copy from: " + src + " to: " + dst)
                        }
                        else {
                            for (var i in firetimeComboBox.data_list) {
                                if (firetimeComboBox.data_list[i].text == filename) {
                                    firetimeComboBox.currentIndex = i
                                    break
                                }
                            }
                            console.log("firetimeComboBox.currentText:" + firetimeComboBox.currentText)
                        }
                    } else {
                        console.log("Unable to import: " + fileDialog.fileUrl)
                        controlPanel.messageDialogOpen("Unable to import: \n" + fileDialog.fileUrl)
                    }
                    break;
                case 4:
                    if(modelData.exportFiretimesFile(fileDialog.fileUrl)) {
                        console.log("Succeed to export: " + fileDialog.fileUrl)
                    } else {
                        console.log("Unable to export: " + fileDialog.fileUrl)
                        controlPanel.messageDialogOpen("Unable to export: \n" + fileDialog.fileUrl)
                    }
                    break;
                case 5:
                    if(modelData.importCoordinateTransformFile(fileDialog.fileUrl)) {
                        console.log("Succeed to import importCoordinateTransformFile 5: " + fileDialog.fileUrl)
                        var src = config.url2string(fileDialog.fileUrl)
                        var filename = src.split('/').pop()
                        var dst = config.getDocuments()+"/PandarViewDataFiles/txt/corrections/"+filename;
                        if (src != dst) {
                            if(config.exists(dst))
                                config.remove(dst)
                            if(config.copy(src, dst)) {
                                subCorrectionComboBoxCoordinateTransform.refresh()
                                for (var i in subCorrectionComboBoxCoordinateTransform.data_list) {
                                    if (subCorrectionComboBoxCoordinateTransform.data_list[i].text == filename) {
                                        subCorrectionComboBoxCoordinateTransform.currentIndex = i
                                        break
                                    }
                                }
                                console.log("correctionComboBox.currentText:" + subCorrectionComboBoxCoordinateTransform.currentText)
                            }
                            else
                                controlPanel.messageDialogOpen("Unable to copy from: " + src + " to: " + dst)
                        }
                        else {
                            for (var i in subCorrectionComboBoxCoordinateTransform.data_list) {
                                if (subCorrectionComboBoxCoordinateTransform.data_list[i].text == filename) {
                                    subCorrectionComboBoxCoordinateTransform.currentIndex = i
                                    break
                                }
                            }
                            console.log("correctionComboBox.currentText:" + subCorrectionComboBoxCoordinateTransform.currentText)
                        }
                    } else {
                        console.log("Unable to import importCoordinateTransformFile 5: " + fileDialog.fileUrl)
                        controlPanel.messageDialogOpen("Unable to import importCoordinateTransformFile 5: \n" + fileDialog.fileUrl)
                    }
                    break;
                case 6:
                    if(modelData.exportCoordinateTransformFile(fileDialog.fileUrl)) {
                        console.log("Succeed to export: " + fileDialog.fileUrl)
                    } else {
                        console.log("Unable to export: " + fileDialog.fileUrl)
                        controlPanel.messageDialogOpen("Unable to export: \n" + fileDialog.fileUrl)
                    }
                    break;    
                default:
                    break;
            }
                
        }
    }

    FileDialog {
        id: capture2PCDDialog
        title: "Capture a frame and dump as .pcd file"
        nameFilters: ["PCD files (*.pcd)"]
        selectExisting: false
        onAccepted : {
            if (modelData.capture2PCD(capture2PCDDialog.fileUrl)) {
                controlPanel.messageDialogOpen("The current frame is dumped to \n" + capture2PCDDialog.fileUrl)
            } else {
                controlPanel.messageDialogOpen("Unable to dump current frame to \n" + capture2PCDDialog.fileUrl)
            }
        }
    }

    RowLayout {
        parent: leftBottomControlPanel
        spacing: 1
        visible: modelData.controlSelected
        BottomToolButton {
            id: playByFrameButton 
            checkable: true
            icon.source: 'file:png/frame.png'
            highlighted: bottomPanel.playByFrame = modelData.playByFrame
            onClicked: {
                highlighted = bottomPanel.playByFrame = modelData.playByFrame = true
            }
            Action {
                shortcut: "Ctrl+Q"
                onTriggered: playByFrameButton.onClicked()
            }
            ToolTip {
                visible: parent.hovered
                delay: 500
                text: qsTr("Play by Frame (Ctrl+Q)")
            }
        }
        BottomToolButton {
            id: playByTimeButton
            checkable: true
            icon.source: 'file:png/time.png'
            highlighted: !playByFrameButton.highlighted
            onClicked: {
                playByFrameButton.highlighted = bottomPanel.playByFrame = modelData.playByFrame = false
            }
            Action {
                shortcut: "Ctrl+W"
                onTriggered: playByTimeButton.onClicked()
            }
            ToolTip {
                visible: parent.hovered
                delay: 500
                text: qsTr("Play by Time (Ctrl+W)")
            }
        }
        Rectangle {
            width: 18
            height: 36
            color: "transparent"
        }
    }

    RowLayout {
        parent: yellowToolBar
        visible: modelData.controlSelected
        Rectangle {
            implicitWidth: 48
            implicitHeight: 42
            color: "transparent"
            DefaultToolButton {
                id: correctionButton
                icon.source: 'file:png/corrections.png'
                highlighted: correctionsPanel.visible
                onClicked: correctionsPanel.visible = !correctionsPanel.visible
                onDoubleClicked: onClicked()
                Action {
                    shortcut: "Ctrl+C"
                    onTriggered: correctionButton.onClicked()
                }
                ToolTip {
                    visible: parent.hovered
                    delay: 500
                    text: qsTr("Correction (Ctrl+C)")
                }
            }
            Rectangle {
                id: correctionsPanel
                visible: false
                anchors.top: parent.bottom
                color: activePalette.window
                height: fileToolButtons.height
                width: fileToolButtons.width + 40
                ColumnLayout {
                    id: fileToolButtons
                    x: 20
                    spacing: 10
                    Rectangle {
                        height: 10
                    }
                    Label {
                        text: "Angle Correction"
                        color: activePalette.text
                    }
                    DefaultComboBox {
                        implicitHeight: 36
                        implicitWidth: 240
                        focusPolicy: Qt.NoFocus
                        id: correctionComboBox
                        property var dir_path: config.getDocuments()+"/PandarViewDataFiles/dat"
                        property var data_list: []
                        function refresh() {
                            config.mkpath(dir_path)
                            var file_path_list = config.getDirFiles(dir_path)
                            // console.log(tmp)
                            correctionModel.clear()
                            data_list = []
                            for (var i in file_path_list) {
                                // console.log(i)
                                var strList = file_path_list[i].split('/')
                                // console.log(strList.length)
                                var item = {
                                    text: strList.pop(),
                                    path: file_path_list[i]
                                }
                                data_list.push(item)
                                correctionModel.append(item)
                            }
                        }
                        model: ListModel {
                            id: correctionModel
                            ListElement { text: "default_AT"; }
                        }
                        onCurrentIndexChanged: {
                            // console.log(currentIndex, data_list[currentIndex])
                            if (currentIndex >= 0 && currentIndex < data_list.length) {
                                if (modelData.importCorrectionsFile(data_list[currentIndex].path)) {
                                    console.log("Succeed to import: " + data_list[currentIndex].path)
                                    modelData.exportCorrectionsFile(config.getDocuments()+"/PandarViewDataFiles/dat/default_AT.dat")
                                }
                                else {
                                    console.log("Unable to import: " + data_list[currentIndex].path)
                                    controlPanel.messageDialogOpen("Unable to import: " + data_list[currentIndex].path)
                                }
                            }
                        }
                        Component.onCompleted: {
                            refresh()
                            for (var i in data_list) {
                                if(data_list[i].text == "default_AT.dat" && timeline.living == false){
                                    currentIndex = i
                                }
                            }

                        }
                    }
                    RowLayout {
                        spacing: 20
                        DefaultButton {
                            text: "   Import   "
                            onClicked: {
                                fileDialog.mode = 1
                                fileDialog.title = "Please choose import file"
                                fileDialog.selectExisting = true
                                fileDialog.open()
                            }
                        }
                        DefaultButton {
                            text: "   Export   "
                            onClicked: {
                                fileDialog.mode = 2
                                fileDialog.title = "Please choose export file"
                                fileDialog.selectExisting = false
                                fileDialog.open()
                            }
                        }
                    }
                    Rectangle {
                        height: 5
                    }
                    Rectangle {
                        width: correctionComboBox.width
                        height: 2
                        color: activePalette.button
                    }
                    Label {
                        text: "Firetime Correction"
                        color: activePalette.text
                    }
                    RowLayout {
                        Label {
                            text: "OFF"
                            color: activePalette.text
                        }
                        DefaultSwitch {
                            id: firetimeCorrection
                            focusPolicy: Qt.NoFocus
                            onClicked: {
                                if(checked){
                                    modelData.setFiretimeCorrectionState(true)
                                }
                                else{
                                    modelData.setFiretimeCorrectionState(false)
                                }
                            }
                            onDoubleClicked: {
                                if(!checked){
                                    modelData.setFiretimeCorrectionState(true)
                                }
                                else{
                                    modelData.setFiretimeCorrectionState(false)
                                }
                            }
                            ToolTip {
                                visible: parent.hovered
                                delay: 500
                                text: qsTr("Firetime Correction Switch")
                            }
                        }
                        Label {
                            text: "ON"
                            color: activePalette.text
                        }
                    }
                    DefaultComboBox {
                        implicitHeight: 36
                        implicitWidth: 240
                        focusPolicy: Qt.NoFocus
                        id: firetimeComboBox
                        property var dir_path: config.getDocuments()+"/PandarViewDataFiles/csv/firetimes"
                        property var data_list: []
                        function refresh() {
                            config.mkpath(dir_path)
                            var file_path_list = config.getDirFiles(dir_path)
                            // console.log(tmp)
                            firetimeModel.clear()
                            data_list = []
                            for (var i in file_path_list) {
                                // console.log(i)
                                var strList = file_path_list[i].split('/')
                                // console.log(strList.length)
                                var item = {
                                    text: strList.pop(),
                                    path: file_path_list[i]
                                }
                                data_list.push(item)
                                firetimeModel.append(item)
                            }
                        }
                        model: ListModel {
                            id: firetimeModel
                            ListElement { text: "default_PandarAT.csv"; }
                        }
                        onCurrentIndexChanged: {
                            // console.log(currentIndex, data_list[currentIndex])
                            if (currentIndex >= 0 && currentIndex < data_list.length) {
                                if (modelData.importFiretimesFile(data_list[currentIndex].path)) {
                                    console.log("Succeed to import: " + data_list[currentIndex].path)
                                     modelData.exportFiretimesFile(config.getDocuments()+"/PandarViewDataFiles/csv/firetimes/default_PandarAT_firetime.csv")
                                }
                                else {
                                    console.log("Unable to import: " + data_list[currentIndex].path)
                                    controlPanel.messageDialogOpen("Unable to import: \n" + data_list[currentIndex].path)
                                }
                            }
                        }
                        Component.onCompleted: {
                            refresh()
                            for (var i in data_list) {
                                if(data_list[i].text == "default_PandarAT_firetime.csv" && timeline.living == false){
                                    currentIndex = i
                                }
                            }
                        }
                    }
                    RowLayout {
                        spacing: 20
                        DefaultButton {
                            // implicitHeight: 24
                            text: "   Import   "
                            onClicked: {
                                fileDialog.mode = 3
                                fileDialog.title = "Please choose import file"
                                fileDialog.selectExisting = true
                                fileDialog.open()
                            }
                        }
                        DefaultButton {
                            // implicitHeight: 24
                            text: "   Export   "
                            onClicked: {
                                fileDialog.mode = 4
                                fileDialog.title = "Please choose export file"
                                fileDialog.selectExisting = false
                                fileDialog.open()
                            }
                        }
                    }
                    Rectangle {
                        height: 10
                    }
                }
            }
        }
        // Rectangle {
        //     implicitWidth: 48
        //     implicitHeight: 42
        //     color: "transparent"
        //     DefaultToolButton {
        //         id: ptcButton
        //         icon.source: 'file:png/ptc.png'
        //         highlighted: ptcPanel.visible
        //         onClicked: ptcPanel.visible = !ptcPanel.visible
        //         onDoubleClicked: onClicked()
        //         Action {
        //             shortcut: "Ctrl+P"
        //             onTriggered: ptcButton.onClicked()
        //         }
        //         ToolTip {
        //             visible: parent.hovered
        //             delay: 500
        //             text: qsTr("Cybersecurity (Ctrl+P)")
        //         }
        //     }
        //     Rectangle {
        //         id: ptcPanel
        //         visible: false
        //         anchors.top: parent.bottom
        //         color: activePalette.window
        //         height: ptcsControl.height
        //         width: ptcsControl.width + 20
                
        //     }
        // }
    }

    RowLayout {
        id: pandar
        spacing: 1
        parent: blueToolBar
        visible: modelData.controlSelected
        function updateRightHUDWidth() {
            rightHUD.width = (pointxyziSpreadSheet.visible ? pointxyziSpreadSheet.width : 0)
                    + (channelView.visible ? channelView.width : 0)
        }
        DefaultToolButton {
            id: channelButton
            text:'Channels'
            icon.source: 'file:png/clipboard.png'
            highlighted: channelView.visible
            onClicked: {
                channelView.visible = !channelView.visible
                pandar.updateRightHUDWidth()
            }
            onDoubleClicked: onClicked()
            Action {
                id: channelAction
                shortcut: "Ctrl+T"
                onTriggered: channelButton.onClicked()
            }
            ToolTip {
                visible: channelButton.hovered
                delay: 500
                text: qsTr("Channels (Ctrl+T)")
            }
        }
        DefaultToolButton {
            id: spreadSheetButton
            text:'Spread Sheet'
            icon.source: 'file:png/table.png'
            checkable: true
            highlighted: checked
            function enableSpreadSheet( enabled ) {
                pointxyziSpreadSheet.visible = enabled
                modelData.spreadSheetEnabled = enabled
                pandar.updateRightHUDWidth()
            }
            onClicked: {
                enableSpreadSheet(checked)
            }
            onDoubleClicked: {
                enableSpreadSheet(!checked)
            }
            Action {
                id: spreadSheetAction
                shortcut: "Ctrl+D"
                onTriggered: {
                    spreadSheetButton.checked = !spreadSheetButton.checked
                    spreadSheetButton.onClicked()
                }
            }
            ToolTip {
                visible: spreadSheetButton.hovered
                delay: 500
                text: qsTr("Spread Sheet (Ctrl+D)")
            }
        }
        FilterWindowV2 {
            id: filterWindow
        }
        DefaultToolButton {
            id: filterButton
            icon.source: 'file:png/filter.png'
            highlighted: modelData.filterModel.filterEnabled
            onClicked: filterWindow.show()
            Action {
                shortcut: "Ctrl+J"
                onTriggered: filterButton.onClicked()
            }
            ToolTip {
                visible: parent.hovered
                delay: 500
                text: qsTr("Filter (Ctrl+J)")
            }
        }
        DefaultToolButton {
            id: lasersButton
            text:'Laser Tracing'
            icon.source: 'file:png/Sensor.png'
            checkable: true
            highlighted: checked
            onClicked: {
                if (checked) {
                    lasersEntityLoader.source = "LasersEntity.qml"
                }
                else {
                    lasersEntityLoader.source = ""
                }
            }
            onDoubleClicked: {
                if (!checked) {
                    lasersEntityLoader.source = "LasersEntity.qml"
                }
                else {
                    lasersEntityLoader.source = ""
                }
            }
            Action {
                id: lasersAction
                shortcut: "Ctrl+L"
                onTriggered: {
                    lasersButton.checked = !lasersButton.checked
                    lasersButton.onClicked()
                }
            }
            ToolTip {
                visible: lasersButton.hovered
                delay: 500
                text: qsTr("Laser Tracing (Ctrl+L)")
            }
        }
        DefaultToolButton {
            id: stateInfoButton
            text:'State info'
            icon.source: 'file:png/stateInfo.png'
            highlighted: pointxyziStatusText.visible
            onClicked: {
                modelData.statusEnabled =  !modelData.statusEnabled
                pointxyziStatusText.visible =  !pointxyziStatusText.visible
            }
            onDoubleClicked: {
                modelData.statusEnabled =  !modelData.statusEnabled
                pointxyziStatusText.visible =  !pointxyziStatusText.visible
            }
            Action {
                id: stateInfoAction
                shortcut: "Ctrl+I"
                onTriggered: {
                    stateInfoButton.onClicked()
                }
            }
            ToolTip {
                visible: stateInfoButton.hovered
                delay: 500
                text: qsTr("State Info (Ctrl+I)")
            }
        }
        DefaultToolButton {
            id: pcdButton
            text:'PCD'
            icon.source: 'file:png/pcd.png'
            onClicked: capture2PCDDialog.open()
            ToolTip {
                visible: parent.hovered
                delay: 500
                text: qsTr("Dump PCD")
            }
        }
    }
    RowLayout{
        spacing: 1
        parent:aftercolorbar
        visible: modelData.controlSelected
        Rectangle {
            id:colormapbar
            implicitWidth: 48
            implicitHeight: 42
            color: "transparent"
            DefaultToolButton {
                id: colorEditorButton
                highlighted: colorEditorPanel.visible
                icon.source: 'file:png/colormap.png'
                onClicked: colorEditorPanel.visible = !colorEditorPanel.visible
                onDoubleClicked: onClicked()
                Action {
                    shortcut: "Ctrl+K"
                    onTriggered: colorEditorButton.onClicked()
                }
                ToolTip {
                    visible: parent.hovered
                    delay: 500
                    text: qsTr("Color Map (Ctrl+K)")
                } 
            }
            ColorEditor {
                id:colorEditorPanel 
            }
        }

        Rectangle {
            implicitWidth: 48
            implicitHeight: 42
            color: "transparent"
            visible: modelData.splitWindow
            DefaultToolButton {
                id: colorEditorButton2
                highlighted: colorEditorPanel2.visible
                icon.source: 'file:png/colormap.png'
                onClicked: colorEditorPanel2.visible = !colorEditorPanel2.visible
                onDoubleClicked: onClicked()
                Action {
                    shortcut: "Ctrl+;"
                    onTriggered: colorEditorButton2.onClicked()
                }
                ToolTip {
                    visible: parent.hovered
                    delay: 500
                    text: qsTr("Color Map 2 (Ctrl+;)")
                } 
            }
            ColorEditorSplit {
                id: colorEditorPanel2
            }
        }
        PointSizeSpinBox {}
        PointSizeSpinBoxSplit {}
        Rectangle {
            implicitWidth: 12
            implicitHeight: 36
            color: "transparent"
        }
        ReturnModeEditor {}
        Rectangle {
            implicitWidth: 18
            implicitHeight: 36
            color: "transparent"
        }
        DefaultToolButton {
            id: pandarATToolbox
            icon.source: 'file:png/toolbox.png'
            checked: false
            checkable: true
            onClicked: {
                pandarATHUD.visible = !pandarATHUD.visible
            }
            Action {
                shortcut: "Ctrl+X"
                onTriggered: pandarATToolbox.onClicked()
            }
            ToolTip {
                visible: parent.hovered
                delay: 500
                text: qsTr("Show/Hide AT Toolbox(Ctrl+X)")
            } 
        }
    }
    ColumnLayout {
        id: pandarATHUD
        property alias picker: pointxyziPicker
        property QtObject renderBuffer: modelData
        parent: hud
        visible: false
        Rectangle {
            width: 2
            height: 32
            color: "transparent"
        }
        Button {
            text: "AT Toolbox"
        }
        Label {
            text: "Display Mode"
            color: activePalette.windowText
        }
        ComboBox {
            focusPolicy: Qt.NoFocus
            model: ListModel {
                    ListElement { text: "Take Turns" }
                    ListElement { text: "Tradition" }
                    ListElement { text: "Combination"}
                }
            currentIndex: 0
            onActivated: {
                modelData.setCurrentMode((currentIndex <= 0) ? currentIndex : currentIndex + 1)
                frameDuration.currentIndex = (currentIndex <= 0) ? 2 : 4
                frameDuration.setFrameDuration(frameDuration.currentText)
                // simultaneousFrameRowLayout.visible = (currentIndex == 1)
            }
        }
        // Button {
        //     focusPolicy: Qt.NoFocus
        //     text: "Get correction"
        //     implicitWidth: 120
        //     implicitHeight: 40
        //     onClicked: {
        //         modelData.getCorrection()
        //     }
        // }
        ColumnLayout {
            Label {
                text: "Frame Duration"
                color: activePalette.windowText
            }
            ComboBox {
                id: frameDuration
                focusPolicy: Qt.NoFocus
                editable: false
                function setFrameDuration( frameDurationText ) {
                    if (frameDurationText == "25ms") {
                        modelData.frameSeconds = 0.025
                    } else if (frameDurationText == "50ms") {
                        modelData.frameSeconds = 0.050
                    } else if (frameDurationText == "100ms") {
                        modelData.frameSeconds = 0.100
                    } else if (frameDurationText == "200ms") {
                        modelData.frameSeconds = 0.200
                    } else if (frameDurationText == "300ms") {
                        modelData.frameSeconds = 0.300
                    } 
                }

                model: ListModel {
                    ListElement { text: "25ms" }
                    ListElement { text: "50ms" }
                    ListElement { text: "100ms"}
                    ListElement { text: "200ms"}
                    ListElement { text: "300ms"}
                }
                currentIndex: 2
                onActivated: setFrameDuration(currentText)
            }
            RowLayout {
                TextField {
                    id: frameMS
                    implicitWidth: 60
                    text: modelData.frameSeconds * 1000
                }
                Label {
                    text: "ms"
                    color: activePalette.windowText
                }
                Button {
                    text: "Set"
                    implicitWidth: 60
                    onClicked: {
                        modelData.frameSeconds = Number(frameMS.text) / 1000
                    }
                }
            }
        }
        ColumnLayout {
            Label {
                text: "Scan Switches"
                color: activePalette.windowText
            }
            ColumnLayout {
                Switch {
                    focusPolicy: Qt.NoFocus
                    checked: true
                    text: "<font color='white'>Field 1</font>"
                    onToggled: modelData.toggleField(0)
                }
                Switch {
                    focusPolicy: Qt.NoFocus
                    checked: true
                    text: "<font color='white'>Field 2</font>"
                    onToggled: modelData.toggleField(1)
                }
                Switch {
                    focusPolicy: Qt.NoFocus
                    checked: true
                    text: "<font color='white'>Field 3</font>"
                    onToggled: modelData.toggleField(2)
                }
                Switch {
                    focusPolicy: Qt.NoFocus
                    checked: true
                    text: "<font color='white'>Field 4</font>"
                    onToggled: modelData.toggleField(3)
                }
            }
            Switch {
                focusPolicy: Qt.NoFocus
                text: "<font color='white'>Field Start/End</font>"
                onToggled: {
                    fieldConfig.visible = !fieldConfig.visible
                    if (fieldConfig.visible)
                        resetFieldConfig.onClicked()
                }
            }
            ColumnLayout {
                id: fieldConfig
                visible: false
                RowLayout {
                    DoubleSpinBox {
                        from: 0
                        to: 360 * 100
                        id: start1
                    }
                    DoubleSpinBox {
                        from: 0
                        to: 360 * 100
                        id: end1
                    }
                }
                RowLayout {
                    DoubleSpinBox {
                        from: 0
                        to: 360 * 100
                        id: start2
                    }
                    DoubleSpinBox {
                        from: 0
                        to: 360 * 100
                        id: end2
                    }
                }
                RowLayout {
                    DoubleSpinBox {
                        from: 0
                        to: 360 * 100
                        id: start3
                    }
                    DoubleSpinBox {
                        from: 0
                        to: 360 * 100
                        id: end3
                    }
                }
                RowLayout {
                    DoubleSpinBox {
                        from: 0
                        to: 360 * 100
                        id: start4
                    }
                    DoubleSpinBox {
                        from: 0
                        to: 360 * 100
                        id: end4
                    }
                }
                Label {
                    text: "Frame Split Azimuth"
                    color: activePalette.windowText
                }
                DoubleSpinBox {
                    from: 0
                    to: 360 * 100 - 1
                    id: frameSplitAzimuth
                }
                RowLayout {
                    Button {
                        focusPolicy: Qt.NoFocus
                        id: resetFieldConfig
                        text: "Reset" 
                        
                        implicitWidth: 70
                        implicitHeight: 30
                        onClicked: {
                            var valueList = modelData.frameSpan
                            start1.value = valueList[0]
                            end1.value = valueList[1]
                            start2.value = valueList[2]
                            end2.value = valueList[3]
                            start3.value = valueList[4]
                            end3.value = valueList[5]
                            start4.value = valueList[6]
                            end4.value = valueList[7]
                            frameSplitAzimuth.value = valueList[8]
                        }
                    }
                    Button {
                        focusPolicy: Qt.NoFocus
                        text: "Set"
                        
                        implicitWidth: 70
                        implicitHeight: 30
                        onClicked: {
                            modelData.frameSpan = [start1.value, end1.value, 
                                start2.value, end2.value, 
                                start3.value, end3.value, 
                                start4.value, end4.value,
                                frameSplitAzimuth.value]
                            modelData.renderStateChanged()
                        }
                    }
                }
            }
        }

    }
    Text {
        id: distanceMeasureText
        parent: topHUD
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter
        wrapMode: Text.WordWrap
        visible: false
        font.bold: true
        font.pointSize: 14
        color: "white"
        
    }
    Text {
        id: pointxyziStatusText
        visible: true
        parent: bottomHUD
        anchors.left: parent.left
        anchors.bottom: parent.bottom
        wrapMode: Text.WordWrap
        text: modelData.status
        
        font.pointSize: 12
        color: activePalette.windowText
    }

    ChannelView {
        id: channelView
        parent: rightHUD
        visible: false
    }
    SpreadSheet {
        id: pointxyziSpreadSheet
        parent: rightHUD
        visible: false
    }

    Timer {
        interval: 10
        running: true
        repeat: false
        onTriggered: {
            if (timeline.living == false){
                timeline.cursorSeconds += 0.2
            }
        }
    }
    PtcsConnectionControl {
        id: ptcsControl
    }


    Component.onCompleted: {
        console.log("pandarAT control construct")
    }

}
