summaryrefslogtreecommitdiffstats
path: root/examples/sensors/sensor_explorer/sensor_explorer.qml
diff options
context:
space:
mode:
authorJuha Vuolle <juha.vuolle@insta.fi>2021-05-28 09:17:29 +0300
committerJuha Vuolle <juha.vuolle@insta.fi>2021-06-02 08:09:41 +0300
commit4c8ebb02cd14c710208f4514151df1d1f0132a11 (patch)
tree62f9c6054feb6f2c56142a0d38b88e64e848d324 /examples/sensors/sensor_explorer/sensor_explorer.qml
parent657aebc33fc0abff544aeeb5c6d92e5b5bdb582c (diff)
Fix and simplify sensor_explorer example
This commit strives to both fix and simplify the application and bring its intended demonstrative purpose more forward, consequently hopefully also making the example easier to understand and maintain. First, the sensor_explorer example used QQC1 functionalities which are no longer available in Qt6. The removed elements were such that the datamodels needed to be redesigned. This commit changes the example to use bit more standard Qt model-view approach. Second, the sensor_explorer application has not worked in years on Android due to missing QML plugin installation. While this could be fixed, in the name of simplicity the example is now a single binary. Third, the explorer allowed editing of sensor values. While this could be a feature of the example, it perhaps deviates from the gist of the example making it more difficult to understand. Thus the editing was not added as part of the example rewrite. It can be added later though. Task-number: QTBUG-92514 Task-number: QTBUG-92505 Task-number: QTBUG-63054 Change-Id: Icd620ea605a87ba74a42cd1309a3c02d05b146ac Reviewed-by: Lorn Potter <lorn.potter@gmail.com>
Diffstat (limited to 'examples/sensors/sensor_explorer/sensor_explorer.qml')
-rw-r--r--examples/sensors/sensor_explorer/sensor_explorer.qml240
1 files changed, 93 insertions, 147 deletions
diff --git a/examples/sensors/sensor_explorer/sensor_explorer.qml b/examples/sensors/sensor_explorer/sensor_explorer.qml
index c8b9e538..d6666622 100644
--- a/examples/sensors/sensor_explorer/sensor_explorer.qml
+++ b/examples/sensors/sensor_explorer/sensor_explorer.qml
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2017 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtSensors module of the Qt Toolkit.
@@ -48,181 +48,127 @@
**
****************************************************************************/
-import QtQuick 2.1
-import QtQuick.Window 2.1
-import QtQuick.Controls 2.0
+import QtQuick
+import QtQuick.Window
+import QtQuick.Controls
+import QtQuick.Layouts
//! [0]
-import Explorer 1.0
+import SensorModels
//! [0]
Window {
id: window
- width: 320
- height: 480
- minimumWidth: 320
- minimumHeight: 480
+ width: 400
+ height: 600
- //! [1]
- SensorExplorer {
- id: explorer
+ AvailableSensorsModel {
+ id: availableSensorsModel
}
- //! [1]
- Column {
- anchors.fill: parent
- anchors.margins: 8
- spacing: 8
-
- /* TODO Qt 6.2 FIX TableView and TableViewColumn
+ ColumnLayout {
GroupBox {
+ id: availableSensorsModelGroup
title: qsTr("Available Sensors")
- width: parent.width
- height: window.height * 0.4
-
+ Layout.preferredWidth: window.width - 4 // 4 = 2x2 margins
+ Layout.preferredHeight: window.height * 0.4
+ Layout.margins: 2
- TableView {
- id: sensorList
+ ListView {
+ id: sensorsView
anchors.fill: parent
- //! [2]
- model: explorer.availableSensors
- //! [2]
-
- TableViewColumn { role: "id"; title: qsTr("ID"); width: sensorList.width * 0.7 }
- TableViewColumn { role: "start"; title: qsTr("Running"); width: sensorList.width * 0.3 - 5 }
-
- onClicked: {
- explorer.selectedSensorItem = explorer.availableSensors[row]
- //! [3]
- propertyList.model = explorer.selectedSensorItem.properties
- //! [3]
- button.update()
- }
- }
-
- Button {
- id: button
- anchors.margins: 4
- anchors.horizontalCenter: parent.horizontalCenter
- anchors.top: sensorList.bottom
- text: qsTr("Start")
- enabled: explorer.selectedSensorItem !== null
-
- function update() {
- text = (explorer.selectedSensorItem !== null ?
- (explorer.selectedSensorItem.start === true ?
- qsTr("Stop") : qsTr("Start")) : qsTr("Start"))
- }
-
- onClicked: {
- if (explorer.selectedSensorItem !== null) {
- //! [5]
- if (text === "Start") {
- explorer.selectedSensorItem.start = true;
- text = "Stop";
+ currentIndex: -1 // no initial selection
+ spacing: 1
+ clip: true
+ model: availableSensorsModel
+ delegate: Item {
+ id: sensorRow
+ width: sensorsView.width
+ height: 30
+ property color rowColor: {
+ if (sensorsView.currentIndex == index)
+ return "lightsteelblue" // highlight
+ return (index % 2 == 0) ? "#CCCCCC" : "#AAAAAA"
+ }
+ RowLayout {
+ spacing: 1
+ anchors.fill: parent
+ Rectangle {
+ color: sensorRow.rowColor
+ Layout.preferredWidth: sensorRow.width * 0.8
+ Layout.preferredHeight: sensorRow.height
+ Text {
+ anchors.centerIn: parent
+ text: display.type + "::" + display.identifier
+ }
}
- else {
- explorer.selectedSensorItem.start = false;
- text = "Start";
+ Rectangle {
+ color: sensorRow.rowColor
+ Layout.preferredWidth: sensorRow.width * 0.2
+ Layout.preferredHeight: sensorRow.height
+ Text {
+ anchors.centerIn: parent
+ text: display.active ? qsTr("Active") : qsTr("Inactive")
+ }
}
- //! [5]
+ }
+ MouseArea {
+ anchors.fill: parent
+ onClicked: sensorsView.currentIndex = index
}
}
}
+ }
+ //! [1]
+ SensorPropertyModel {
+ id: propertyModel
+ sensor: availableSensorsModel.get(sensorsView.currentIndex)
+ }
+ //! [1]
+
+ //! [2]
+ Button {
+ id: activateButton
+ Layout.preferredHeight: 30
+ Layout.alignment: Qt.AlignCenter
+ enabled: propertyModel.sensor
+ text: !propertyModel.sensor ? qsTr("Select sensor")
+ : (propertyModel.sensor.active ? qsTr("Deactivate sensor")
+ : qsTr("Activate sensor"))
+ onClicked: propertyModel.sensor.active = !propertyModel.sensor.active
}
+ //! [2]
GroupBox {
- title: qsTr("Properties")
- width: parent.width
- height: window.height * 0.55
-
- enabled: explorer.selectedSensorItem != null
+ title: qsTr("Selected sensor's properties")
+ Layout.preferredWidth: window.width - 4 // 4 = 2x2 margins
+ Layout.preferredHeight: window.height * 0.55 - activateButton.height
+ Layout.margins: 2
+ enabled: sensorsView.currentIndex != -1
+ //! [3]
TableView {
- id: propertyList
- property PropertyInfo selectedItem: null
-
+ id: propertyView
anchors.fill: parent
- TableViewColumn { role: "name"; title: qsTr("Name"); width: propertyList.width * 0.5 }
- TableViewColumn { role: "value"; title: qsTr("Value"); width: propertyList.width * 0.5 - 5 }
-
- onClicked: {
- selectedItem = model[row]
- }
-
- itemDelegate: {
- if (selectedItem && selectedItem.isWriteable)
- return editableDelegate;
- return readOnlyDelegate;
- }
-
- Component {
- id: readOnlyDelegate
- Item {
- Text {
- width: parent.width
- anchors.margins: 4
- anchors.left: parent.left
- anchors.verticalCenter: parent.verticalCenter
- elide: styleData.elideMode
- text: styleData.value
- color: propertyList.model[styleData.row].isWriteable ?
- styleData.textColor : Qt.lighter(styleData.textColor)
- }
- }
- }
-
- Component {
- id: editableDelegate
- Item {
- Text {
- width: parent.width
- anchors.margins: 4
- anchors.left: parent.left
- anchors.verticalCenter: parent.verticalCenter
- elide: styleData.elideMode
- text: styleData.value
- color: styleData.textColor
- visible: !styleData.selected || styleData.column === 0
- }
- Loader { // Initialize text editor lazily to improve performance
- id: loaderEditor
- anchors.margins: 4
- anchors.left: parent.left
- anchors.verticalCenter: parent.verticalCenter
- Connections {
- target: loaderEditor.item
- onAccepted: {
- //! [4]
- explorer.selectedSensorItem.changePropertyValue(propertyList.selectedItem, loaderEditor.item.text);
- //! [4]
- }
- }
-
- // Load the editor for selected 'Value' cell
- sourceComponent: (styleData.selected && styleData.column === 1) ? editor : null
-
- Component {
- id: editor
- TextInput {
- id: textinput
- color: styleData.textColor
- text: styleData.value
- MouseArea {
- id: mouseArea
- anchors.fill: parent
- hoverEnabled: true
- onClicked: textinput.forceActiveFocus()
- }
- }
- }
- }
+ model: propertyModel
+ columnSpacing: 1
+ rowSpacing: 1
+ boundsMovement: Flickable.StopAtBounds
+ clip: true
+
+ delegate: Rectangle {
+ implicitHeight: 30
+ implicitWidth: propertyView.width * 0.5
+ color: (model.row % 2 == 0) ? "#CCCCCC" : "#AAAAAA"
+ Text {
+ anchors.centerIn: parent
+ text: display
}
}
}
+ //! [3]
}
- */
}
}