ValuePageWidget.qml 19.4 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9

10
import QtQuick          2.3
11
import QtQuick.Dialogs  1.2
12
import QtQuick.Layouts  1.2
13 14
import QtQuick.Controls 2.5
import QtQml            2.12
15 16 17 18

import QGroundControl.Controls      1.0
import QGroundControl.ScreenTools   1.0
import QGroundControl.FactSystem    1.0
19
import QGroundControl.FactControls  1.0
20 21 22 23
import QGroundControl.Controllers   1.0
import QGroundControl.Palette       1.0
import QGroundControl               1.0

24 25
/// Value page for InstrumentPanel PageView
Column {
26
    id:         _root
27
    width:      pageWidth
28
    spacing:    ScreenTools.defaultFontPixelHeight / 2
29

30
    property bool showSettingsIcon: true
31 32 33 34 35 36 37 38 39
    property bool showLockIcon:     true

    property var    _activeVehicle:                 QGroundControl.multiVehicleManager.activeVehicle ? QGroundControl.multiVehicleManager.activeVehicle : QGroundControl.multiVehicleManager.offlineEditingVehicle
    property real   _margins:                       ScreenTools.defaultFontPixelWidth / 2
    property int    _colMax:                        4
    property bool   _settingsUnlocked:              false
    property var    _valuePickerInstrumentValue:    null
    property int    _valuePickerRowIndex:           0
    property var    _rgFontSizes:                   [ ScreenTools.defaultFontPointSize, ScreenTools.smallFontPointSize, ScreenTools.mediumFontPointSize, ScreenTools.largeFontPointSize ]
40 41 42 43
    property var    _rgFontSizeRatios:              [ 1, ScreenTools.smallFontPointRatio, ScreenTools.mediumFontPointRatio, ScreenTools.largeFontPointRatio ]
    property real   _doubleDescent:                 ScreenTools.defaultFontDescent * 2
    property real   _tightDefaultFontHeight:        ScreenTools.defaultFontPixelHeight - _doubleDescent
    property var    _rgFontSizeTightHeights:        [ _tightDefaultFontHeight * _rgFontSizeRatios[0] + 2, _tightDefaultFontHeight * _rgFontSizeRatios[1] + 2, _tightDefaultFontHeight * _rgFontSizeRatios[2] + 2, _tightDefaultFontHeight * _rgFontSizeRatios[3] + 2 ]
44 45 46 47 48
    property real   _blankEntryHeight:              ScreenTools.defaultFontPixelHeight * 2
    property real   _columnButtonWidth:             ScreenTools.minTouchPixels / 2
    property real   _columnButtonHeight:            ScreenTools.minTouchPixels
    property real   _columnButtonSpacing:           2
    property real   _columnButtonsTotalHeight:      (_columnButtonHeight * 2) + _columnButtonSpacing
49 50 51

    QGCPalette { id:qgcPal; colorGroupEnabled: true }

52
    ValuesWidgetController { id: controller }
53

54 55
    function showSettings(settingsUnlocked) {
        _settingsUnlocked = settingsUnlocked
56 57
    }

58 59 60 61 62 63 64 65 66
    function listContains(list, value) {
        for (var i=0; i<list.length; i++) {
            if (list[i] === value) {
                return true
            }
        }
        return false
    }

67
    ButtonGroup { id: factRadioGroup }
68

69
    Component {
70
        id: valueItemMouseAreaComponent
71

72 73 74 75 76 77 78 79 80 81 82 83 84
        MouseArea {
            anchors.centerIn:   parent
            width:              parent.width
            height:             _columnButtonsTotalHeight
            visible:            _settingsUnlocked

            property var instrumentValue
            property int rowIndex

            onClicked: {
                _valuePickerInstrumentValue = instrumentValue
                _valuePickerRowIndex = rowIndex
                mainWindow.showComponentDialog(valuePickerDialog, qsTr("Select Value"), mainWindow.showDialogDefaultWidth, StandardButton.Ok)
85 86 87 88
            }
        }
    }

89 90 91
    Repeater {
        id:     rowRepeater
        model:  controller.valuesModel
92 93

        Column {
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
            id:             rowRepeaterLayout
            spacing:        1

            property int rowIndex: index

            Row {
                id:         columnRow
                spacing:    1

                Repeater {
                    id:     columnRepeater
                    model:  object

                    property real _interColumnSpacing:  (columnRepeater.count - (_settingsUnlocked ? 0 : 1)) * columnRow.spacing
                    property real columnWidth:          (pageWidth - (_settingsUnlocked ? _columnButtonWidth : 0) - _interColumnSpacing) / columnRepeater.count
109
                    property bool componentCompleted:   false
110

111
                    Component.onCompleted: componentCompleted = true
112 113 114
                    onItemAdded: valueItemMouseAreaComponent.createObject(item, { "instrumentValue": object.get(index), "rowIndex": index })

                    Item {
115 116
                        id:                     columnItem
                        anchors.verticalCenter: parent.verticalCenter
117 118
                        width:                  columnRepeater.columnWidth
                        height:                 value.y + value.height
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

                        property real columnWidth:                  columnRepeater.columnWidth
                        property bool repeaterComponentCompleted:   columnRepeater.componentCompleted

                        // After fighting with using layout and/or anchors I gave up and just do a manual recalc to position items which ends up being much simpler
                        function recalcPositions() {
                            if (!repeaterComponentCompleted) {
                                return
                            }
                            var smallSpacing = 2
                            if (object.icon) {
                                if (object.iconPosition === InstrumentValue.IconAbove) {
                                    valueIcon.x = (width - valueIcon.width) / 2
                                    valueIcon.y = 0
                                    value.x = (width - value.width) / 2
                                    value.y = valueIcon.height + smallSpacing
                                } else {
                                    var iconPlusValueWidth = valueIcon.width + value.width + ScreenTools.defaultFontPixelWidth
                                    valueIcon.x = (width - iconPlusValueWidth) / 2
                                    valueIcon.y = (value.height - valueIcon.height) / 2
                                    value.x = valueIcon.x + valueIcon.width + (ScreenTools.defaultFontPixelWidth / 2)
                                    value.y = 0
                                }
                                label.x = label.y = 0
                            } else {
                                // label above value
                                if (label) {
                                    label.x = (width - label.width) / 2
                                    label.y = 0
                                    value.y = label.height + smallSpacing
                                } else {
                                    value.y = 0
                                }
                                value.x = (width - value.width) / 2
                                valueIcon.x = valueIcon.y = 0
                            }
                        }

                        onRepeaterComponentCompletedChanged:    recalcPositions()
                        onColumnWidthChanged:                   recalcPositions()

                        Connections {
                            target:                 object
                            onIconChanged:          recalcPositions()
                            onIconPositionChanged:  recalcPositions()
                        }

                        QGCColoredImage {
                            id:                         valueIcon
                            height:                     _rgFontSizeTightHeights[object.fontSize]
                            width:                      height
                            source:                     object.icon ? "/InstrumentValueIcons/" + object.icon : ""
                            sourceSize.height:          height
                            fillMode:                   Image.PreserveAspectFit
                            mipmap:                     true
                            smooth:                     true
                            color:                      qgcPal.text
                            visible:                    object.icon
                            onWidthChanged:             columnItem.recalcPositions()
                            onHeightChanged:            columnItem.recalcPositions()
                        }
180 181

                        QGCLabel {
182
                            id:                         blank
183 184 185 186 187 188 189
                            anchors.horizontalCenter:   parent.horizontalCenter
                            height:                     _columnButtonsTotalHeight
                            font.pointSize:             ScreenTools.smallFontPointSize
                            text:                       _settingsUnlocked ? qsTr("BLANK") : ""
                            horizontalAlignment:        Text.AlignHCenter
                            verticalAlignment:          Text.AlignVCenter
                            visible:                    !object.fact
190 191
                            onWidthChanged:             columnItem.recalcPositions()
                            onHeightChanged:            columnItem.recalcPositions()
192 193 194
                        }

                        QGCLabel {
195
                            id:                         label
196
                            height:                     _rgFontSizeTightHeights[InstrumentValue.SmallFontSize]
197 198
                            font.pointSize:             ScreenTools.smallFontPointSize
                            text:                       object.label.toUpperCase()
199 200 201 202
                            verticalAlignment:          Text.AlignVCenter
                            visible:                    object.fact && object.label && !object.icon
                            onWidthChanged:             columnItem.recalcPositions()
                            onHeightChanged:            columnItem.recalcPositions()
203 204 205
                        }

                        QGCLabel {
206 207 208
                            id:                         value
                            font.pointSize:             _rgFontSizes[object.fontSize]
                            text:                       visible ? (object.fact.enumOrValueString + (object.showUnits ? object.fact.units : "")) : ""
209
                            verticalAlignment:          Text.AlignVCenter
210
                            visible:                    object.fact
211 212
                            onWidthChanged:             columnItem.recalcPositions()
                            onHeightChanged:            columnItem.recalcPositions()
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
                        }
                    }
                } // Repeater - columns

                ColumnLayout {
                    id:                 columnsButtonsLayout
                    width:              _columnButtonWidth
                    spacing:            _columnButtonSpacing
                    visible:            _settingsUnlocked

                    QGCButton {
                        Layout.fillHeight:      true
                        Layout.minimumHeight:   ScreenTools.minTouchPixels
                        Layout.preferredWidth:  parent.width
                        text:                   qsTr("+")
                        onClicked:              controller.appendColumn(rowRepeaterLayout.rowIndex)
                    }

                    QGCButton {
                        Layout.fillHeight:      true
                        Layout.minimumHeight:   ScreenTools.minTouchPixels
                        Layout.preferredWidth:  parent.width
                        text:                   qsTr("-")
                        enabled:                index !== 0 || columnRepeater.count !== 1
                        onClicked:              controller.deleteLastColumn(rowRepeaterLayout.rowIndex)
                    }
                }
            } // RowLayout

            RowLayout {
                width:      parent.width
                height:             ScreenTools.defaultFontPixelWidth * 2
                spacing:            1
                visible:            _settingsUnlocked

                QGCButton {
                    Layout.fillWidth:   true
                    Layout.preferredHeight: ScreenTools.defaultFontPixelWidth * 2
                    text:               qsTr("+")
252
                    onClicked:          controller.insertRow(index + 1)
253 254 255 256 257 258 259 260 261
                }

                QGCButton {
                    Layout.fillWidth:   true
                    Layout.preferredHeight: ScreenTools.defaultFontPixelWidth * 2
                    text:               qsTr("-")
                    enabled:            index !== 0
                    onClicked:          controller.deleteRow(index)
                }
262 263
            }
        }
264 265 266 267 268 269 270 271
    } // Repeater - rows

    QGCButton {
        anchors.left:   parent.left
        anchors.right:  parent.right
        text:           qsTr("Reset To Defaults")
        visible:        _settingsUnlocked
        onClicked:      controller.resetToDefaults()
272 273
    }

274
    Component {
275
        id: valuePickerDialog
276 277

        QGCViewDialog {
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
            function accept() {
                if (factRadioGroup.checkedButton) {
                    _valuePickerInstrumentValue.setFact(factRadioGroup.checkedButton.radioFactGroupName, factRadioGroup.checkedButton.radioFact.name, labelTextField.text, fontSizeCombo.currentIndex)
                } else {
                    _valuePickerInstrumentValue.clearFact()
                }

                hideDialog()
            }

            Connections {
                target: factRadioGroup
                onCheckedButtonChanged: labelTextField.text = factRadioGroup.checkedButton.radioFact.shortDescription
            }

            ButtonGroup { id: fontRadioGroup }
294

295 296
            QGCFlickable {
                anchors.fill:       parent
DonLakeFlyer's avatar
DonLakeFlyer committed
297
                contentHeight:      column.height
298
                flickableDirection: Flickable.VerticalFlick
Don Gagne's avatar
Don Gagne committed
299
                clip:               true
300

301
                ColumnLayout {
302 303 304 305 306
                    id:             column
                    anchors.left:   parent.left
                    anchors.right:  parent.right
                    spacing:        _margins

307 308 309 310 311 312 313 314 315 316 317
                    RowLayout {
                        Layout.fillWidth:   true
                        spacing:            ScreenTools.defaultFontPixelWidth

                        QGCLabel { text: qsTr("Label") }
                        QGCTextField {
                            id:                 labelTextField
                            Layout.fillWidth:   true
                            text:               _valuePickerInstrumentValue.label
                        }
                    }
318

319 320 321 322 323 324
                    RowLayout {
                        spacing: ScreenTools.defaultFontPixelWidth

                        QGCLabel { text: qsTr("Font Size (for whole row)") }
                        QGCComboBox {
                            id:             fontSizeCombo
325
                            model:          _valuePickerInstrumentValue.fontSizeNames
326 327 328 329
                            currentIndex:   _valuePickerInstrumentValue.fontSize
                            sizeToContents: true
                            onActivated:    _valuePickerInstrumentValue.fontSize = index
                        }
330 331
                    }

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
                    RowLayout {
                        spacing: ScreenTools.defaultFontPixelWidth

                        QGCLabel { text: qsTr("Icon") }
                        QGCComboBox {
                            model:          _valuePickerInstrumentValue.iconNames
                            sizeToContents: true
                            onActivated:    _valuePickerInstrumentValue.icon = currentText

                            Component.onCompleted: {
                                currentIndex = find(_valuePickerInstrumentValue.icon)
                                if (currentIndex == -1) {
                                    currentIndex = 0
                                }
                            }
                        }
                    }

                    RowLayout {
                        spacing: ScreenTools.defaultFontPixelWidth

                        QGCLabel { text: qsTr("Icon Position") }
                        QGCComboBox {
                            model:          _valuePickerInstrumentValue.iconPositionNames
                            currentIndex:   _valuePickerInstrumentValue.iconPosition
                            sizeToContents: true
                            onActivated:    _valuePickerInstrumentValue.iconPosition = index
                        }
                    }

362 363 364 365
                    QGCCheckBox {
                        text:       qsTr("Show Units")
                        checked:    _valuePickerInstrumentValue.showUnits
                        onClicked:  _valuePickerInstrumentValue.showUnits = checked
366 367
                    }

368 369 370
                    QGCButton {
                        text:       qsTr("Blank Entry")
                        onClicked:  { _valuePickerInstrumentValue.clearFact(); hideDialog() }
371 372 373
                    }

                    Loader {
374
                        Layout.fillWidth:   true
375 376
                        sourceComponent:    factGroupList

377
                        property var    factGroup:     _activeVehicle
378
                        property string factGroupName: "Vehicle"
DonLakeFlyer's avatar
DonLakeFlyer committed
379 380 381 382 383 384
                    }

                    Repeater {
                        model: _activeVehicle.factGroupNames

                        Loader {
385
                            Layout.fillWidth:   true
DonLakeFlyer's avatar
DonLakeFlyer committed
386 387 388 389 390
                            sourceComponent:    factGroupList

                            property var    factGroup:     _activeVehicle.getFactGroup(modelData)
                            property string factGroupName: modelData
                        }
391
                    }
392
                }
393 394 395 396 397 398 399 400 401 402 403 404
            }
        }
    }

    Component {
        id: factGroupList

        // You must push in the following properties from the Loader
        // property var factGroup
        // property string factGroupName

        Column {
DonLakeFlyer's avatar
DonLakeFlyer committed
405 406 407 408 409 410
            SectionHeader {
                id:             header
                anchors.left:   parent.left
                anchors.right:  parent.right
                text:           factGroupName.charAt(0).toUpperCase() + factGroupName.slice(1)
                checked:        false
411 412
            }

DonLakeFlyer's avatar
DonLakeFlyer committed
413
            Column {
414
                visible: header.checked
DonLakeFlyer's avatar
DonLakeFlyer committed
415 416 417

                Repeater {
                    model: factGroup ? factGroup.factNames : 0
418

419 420 421 422
                    QGCRadioButton {
                        text:               radioFact.shortDescription
                        ButtonGroup.group:  factRadioGroup
                        checked:            radioFactGroupName == _valuePickerInstrumentValue.factGroupName && radioFact == _valuePickerInstrumentValue.fact
423

424 425
                        property string radioFactGroupName: factGroupName
                        property var    radioFact:          factGroup.getFact(modelData)
426

427 428 429
                        Component.onCompleted: {
                            if (checked) {
                                header.checked = true
430
                            }
DonLakeFlyer's avatar
DonLakeFlyer committed
431
                        }
432 433 434 435 436 437
                    }
                }
            }
        }
    }
}