From 851ec98b341ed8ad67caac32653178e0bb189bfe Mon Sep 17 00:00:00 2001 From: Tamas Martinec Date: Tue, 26 Oct 2021 16:13:51 +0300 Subject: QtSensors: Create a sensors showcase example Create an example that demonstrates the sensor module sensors more visually than just displaying the sensor reading values. Pick-to: 6.2 Task-number: QTBUG-72329 Change-Id: I7383722124c3aae333cf9218613d7001c9d04690 Reviewed-by: Juha Vuolle --- examples/sensors/CMakeLists.txt | 1 + examples/sensors/sensorsshowcase/CMakeLists.txt | 63 ++++++ examples/sensors/sensorsshowcase/Info.plist | 32 +++ examples/sensors/sensorsshowcase/accelerometer.qml | 169 ++++++++++++++++ .../sensorsshowcase/android/AndroidManifest.xml | 47 +++++ examples/sensors/sensorsshowcase/compass.qml | 111 +++++++++++ .../doc/images/sensorsshowcase-gyroscope.png | Bin 0 -> 29205 bytes .../doc/images/sensorsshowcase-mainview.png | Bin 0 -> 33722 bytes .../sensorsshowcase/doc/src/sensorsshowcase.qdoc | 95 +++++++++ examples/sensors/sensorsshowcase/gyroscope.qml | 221 ++++++++++++++++++++ .../sensors/sensorsshowcase/images/compass.svg | 222 +++++++++++++++++++++ examples/sensors/sensorsshowcase/images/magnet.svg | 98 +++++++++ .../sensors/sensorsshowcase/images/qt_logo.png | Bin 0 -> 6208 bytes examples/sensors/sensorsshowcase/magnetometer.qml | 150 ++++++++++++++ examples/sensors/sensorsshowcase/main.cpp | 61 ++++++ examples/sensors/sensorsshowcase/proximity.qml | 118 +++++++++++ .../sensors/sensorsshowcase/sensorsshowcase.pro | 27 +++ .../sensors/sensorsshowcase/sensorsshowcase.qml | 123 ++++++++++++ .../sensors/sensorsshowcase/sensorsshowcase.qrc | 13 ++ 19 files changed, 1551 insertions(+) create mode 100644 examples/sensors/sensorsshowcase/CMakeLists.txt create mode 100644 examples/sensors/sensorsshowcase/Info.plist create mode 100644 examples/sensors/sensorsshowcase/accelerometer.qml create mode 100644 examples/sensors/sensorsshowcase/android/AndroidManifest.xml create mode 100644 examples/sensors/sensorsshowcase/compass.qml create mode 100644 examples/sensors/sensorsshowcase/doc/images/sensorsshowcase-gyroscope.png create mode 100644 examples/sensors/sensorsshowcase/doc/images/sensorsshowcase-mainview.png create mode 100644 examples/sensors/sensorsshowcase/doc/src/sensorsshowcase.qdoc create mode 100644 examples/sensors/sensorsshowcase/gyroscope.qml create mode 100644 examples/sensors/sensorsshowcase/images/compass.svg create mode 100644 examples/sensors/sensorsshowcase/images/magnet.svg create mode 100644 examples/sensors/sensorsshowcase/images/qt_logo.png create mode 100644 examples/sensors/sensorsshowcase/magnetometer.qml create mode 100644 examples/sensors/sensorsshowcase/main.cpp create mode 100644 examples/sensors/sensorsshowcase/proximity.qml create mode 100644 examples/sensors/sensorsshowcase/sensorsshowcase.pro create mode 100644 examples/sensors/sensorsshowcase/sensorsshowcase.qml create mode 100644 examples/sensors/sensorsshowcase/sensorsshowcase.qrc (limited to 'examples') diff --git a/examples/sensors/CMakeLists.txt b/examples/sensors/CMakeLists.txt index 329ae504..7fe6c4e9 100644 --- a/examples/sensors/CMakeLists.txt +++ b/examples/sensors/CMakeLists.txt @@ -7,6 +7,7 @@ if(TARGET Qt::Quick) # add_subdirectory(shakeit) if(TARGET Qt::Svg) add_subdirectory(accelbubble) + add_subdirectory(sensorsshowcase) endif() endif() # if(TARGET Qt::Widgets) diff --git a/examples/sensors/sensorsshowcase/CMakeLists.txt b/examples/sensors/sensorsshowcase/CMakeLists.txt new file mode 100644 index 00000000..b034dff6 --- /dev/null +++ b/examples/sensors/sensorsshowcase/CMakeLists.txt @@ -0,0 +1,63 @@ +cmake_minimum_required(VERSION 3.16) +project(sensorsshowcase LANGUAGES CXX) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_AUTOUIC ON) + +if(NOT DEFINED INSTALL_EXAMPLESDIR) + set(INSTALL_EXAMPLESDIR "examples") +endif() + +set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/sensors/sensorsshowcase") + +find_package(Qt6 COMPONENTS Core Gui Quick Sensors Svg) + +qt_add_executable(sensorsshowcase + main.cpp +) +set_target_properties(sensorsshowcase PROPERTIES + WIN32_EXECUTABLE TRUE + MACOSX_BUNDLE TRUE +) + +if(ANDROID) +set_property(TARGET sensorsshowcase APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR + ${CMAKE_CURRENT_SOURCE_DIR}/android) +endif() + +target_link_libraries(sensorsshowcase PUBLIC + Qt::Core + Qt::Gui + Qt::Quick + Qt::Sensors + Qt::Svg +) + +# Resources: +set(sensorsshowcase_resource_files + "sensorsshowcase.qml" + "accelerometer.qml" + "proximity.qml" + "compass.qml" + "magnetometer.qml" + "gyroscope.qml" + "images/magnet.svg" + "images/compass.svg" + "images/qt_logo.png" +) + +qt6_add_resources(sensorsshowcase "sensorsshowcase" + PREFIX + "/" + FILES + ${sensorsshowcase_resource_files} +) + +install(TARGETS sensorsshowcase + RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" + LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" +) diff --git a/examples/sensors/sensorsshowcase/Info.plist b/examples/sensors/sensorsshowcase/Info.plist new file mode 100644 index 00000000..61d01e5a --- /dev/null +++ b/examples/sensors/sensorsshowcase/Info.plist @@ -0,0 +1,32 @@ + + + + + CFBundleDisplayName + sensorsshowcase + CFBundleExecutable + sensorsshowcase + CFBundleGetInfoString + Created by Qt/QMake + CFBundleIdentifier + com.qt.sensorsshowcase + CFBundleName + sensorsshowcase + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1.0 + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + + + diff --git a/examples/sensors/sensorsshowcase/accelerometer.qml b/examples/sensors/sensorsshowcase/accelerometer.qml new file mode 100644 index 00000000..df7c7f1e --- /dev/null +++ b/examples/sensors/sensorsshowcase/accelerometer.qml @@ -0,0 +1,169 @@ +/**************************************************************************** +** +** 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. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick +import QtQuick.Controls +import QtQuick.Layouts +import QtSensors + +Rectangle { + id: root + color: "dimgray" + + function resetRotations() { + imageXRotation.angle = 0 + imageYRotation.angle = 0 + imageZRotation.angle = 0 + } + +//! [0] + Accelerometer { + id: accelerometer + active: true + dataRate: 25 + + property real x: 0 + property real y: 0 + property real z: 0 + + onReadingChanged: { + x = reading.x + y = reading.y + z = reading.z + + imageTranslation.x = -reading.x * 10 + imageTranslation.y = reading.y * 10 + } + } +//! [0] + + ColumnLayout { + anchors.fill: parent + id: layout + + Text { + Layout.topMargin: titleTopMargin + Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter + Layout.preferredHeight: textHeight + color: "White" + text: "Accelerometer" + font.pixelSize: titleFontSize + } + + Image { + id: image + Layout.alignment: Qt.AlignCenter + Layout.fillHeight: true + Layout.preferredWidth: root.width/2 + Layout.preferredHeight: root.height/3 + source: "qrc:/images/qt_logo.png" + fillMode: Image.PreserveAspectFit + + transform: [ + Translate { + id: imageTranslation + x: 0 + y: 0 + } + ] + } + + Text { + Layout.preferredWidth: root.width + Layout.preferredHeight: textHeight + Layout.leftMargin: layout.spacing + color: "White" + text: "X: " + accelerometer.x.toFixed(2) + font.pixelSize: textFontSize + } + + ProgressBar { + id: xbar + value: 0.5 + (accelerometer.x / 100) + Layout.preferredWidth: root.width + } + + Text { + Layout.preferredWidth: root.width + Layout.preferredHeight: textHeight + Layout.leftMargin: layout.spacing + color: "White" + text: "Y: " + accelerometer.y.toFixed(2) + font.pixelSize: textFontSize + } + ProgressBar { + id: ybar + value: 0.5 + (accelerometer.y / 100) + Layout.preferredWidth: root.width + } + Text { + Layout.preferredWidth: root.width + Layout.preferredHeight: textHeight + Layout.leftMargin: layout.spacing + color: "White" + text: "Z: " + accelerometer.z.toFixed(2) + font.pixelSize: textFontSize + } + ProgressBar { + id: zbar + value: 0.5 + (accelerometer.z / 100) + Layout.preferredWidth: root.width + } + Button { + Layout.alignment: Qt.AlignBottom + Layout.preferredWidth: root.width + Layout.preferredHeight: buttonHeight + text:"Back" + font.pixelSize: buttonFontSize + onClicked:stack.pop() + } + } +} + diff --git a/examples/sensors/sensorsshowcase/android/AndroidManifest.xml b/examples/sensors/sensorsshowcase/android/AndroidManifest.xml new file mode 100644 index 00000000..3c152a90 --- /dev/null +++ b/examples/sensors/sensorsshowcase/android/AndroidManifest.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/sensors/sensorsshowcase/compass.qml b/examples/sensors/sensorsshowcase/compass.qml new file mode 100644 index 00000000..b0fee5e4 --- /dev/null +++ b/examples/sensors/sensorsshowcase/compass.qml @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** 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. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick +import QtQuick.Controls +import QtQuick.Layouts +import QtSensors + +Rectangle { + id: root + color: "dimgray" + property real azimuth: 30 + readonly property real buttonHeight: height/8 + + Compass { + id: compass + active: true + dataRate: 7 + onReadingChanged: { + root.azimuth = -reading.azimuth + } + } + + ColumnLayout { + anchors.fill: parent + id: layout + + Text { + Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter + Layout.preferredHeight: titleHeight + Layout.topMargin: titleTopMargin + verticalAlignment: Text.AlignVCenter + color: "White" + text: "Compass" + font.pixelSize: titleFontSize + } + Image { + Layout.alignment: Qt.AlignCenter + Layout.fillHeight: true + Layout.preferredWidth: root.width + Layout.preferredHeight: root.height + id: arrow + source: "qrc:/images/compass.svg" + fillMode: Image.PreserveAspectFit + rotation:root.azimuth + } + Text { + Layout.preferredWidth: root.width + Layout.preferredHeight: textHeight + Layout.leftMargin: layout.spacing + color: "White" + text: "Azimuth: " + root.azimuth.toFixed(2) + "°" + font.pixelSize: textFontSize + } + Button { + Layout.alignment: Qt.AlignBottom + Layout.preferredWidth: root.width + Layout.preferredHeight: buttonHeight + text:"Back" + font.pixelSize: buttonFontSize + onClicked:stack.pop() + } + } +} diff --git a/examples/sensors/sensorsshowcase/doc/images/sensorsshowcase-gyroscope.png b/examples/sensors/sensorsshowcase/doc/images/sensorsshowcase-gyroscope.png new file mode 100644 index 00000000..b26aa459 Binary files /dev/null and b/examples/sensors/sensorsshowcase/doc/images/sensorsshowcase-gyroscope.png differ diff --git a/examples/sensors/sensorsshowcase/doc/images/sensorsshowcase-mainview.png b/examples/sensors/sensorsshowcase/doc/images/sensorsshowcase-mainview.png new file mode 100644 index 00000000..838b39c9 Binary files /dev/null and b/examples/sensors/sensorsshowcase/doc/images/sensorsshowcase-mainview.png differ diff --git a/examples/sensors/sensorsshowcase/doc/src/sensorsshowcase.qdoc b/examples/sensors/sensorsshowcase/doc/src/sensorsshowcase.qdoc new file mode 100644 index 00000000..b845d5d7 --- /dev/null +++ b/examples/sensors/sensorsshowcase/doc/src/sensorsshowcase.qdoc @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: https://www.gnu.org/licenses/fdl-1.3.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example sensorsshowcase + \title Qt Sensors - Sensors Showcase + \brief The Sensors Showcase example demonstrates sensor usage with visual examples. + \ingroup qtsensors-examples + + \image sensorsshowcase-mainview.png + + \section1 Overview + + On startup, the application shows a menu with buttons for the subviews for each sensor. + The sensor views instantiate the given sensor, display the sensor's values as numbers, + and also visualize them with a simple graphical representation. + + \section1 Main Menu + + The main view shows the title with the name of the application and a button for each + subview laid out evenly by a \c ColumnLayout. The navigation between the subviews + and the main menu is managed by a \c StackView. + + \section1 Accelerometer View + + The accelerometer view shows the current device acceleration values and moves around + an image with an amount that is opposite of the device acceleration giving the image + an inertia effect that is proportional with the movement of the device. + + Moving around the image happens in the accelerometer \c onReadingChanged method. + + \snippet sensorsshowcase/accelerometer.qml 0 + + Whenever there is a new accelerometer value the image translation coordinates are + updated accordingly. + + \section1 Proximity View + + The proximity view shows an image that is enlarged whenever the proximity sensor of + the device is covered. + + \section1 Compass View + + The compass view shows a compass image that is rotated according to the Compass sensor + reading value making the compass turn towards north. + + \section1 Magnetometer View + + The magnetometer view displays a magnet image that is rotated around an amount that is + decided by the rotation angle of the vector given by the x and y magnetometer values. + This results in general in the same rotation as the compass gives, demonstrating one use + case of how the magnetometer readings can be used. Since the magnetometer provides + readings along all three axes, there is more freedom with how these readings can be used. + + \snippet sensorsshowcase/magnetometer.qml 0 + + \section1 Gyroscope View + + \image sensorsshowcase-gyroscope.png + + The gyroscope view also shows an image that is rotated around three axes with an amount + that is calculated from the gyroscope readings. Since the gyroscope provides relative + rotational change around the three spatial axes and the time between reading updates + can vary, the time of the readings are stored and the rotational change is normalized + based on the time passed between reading updates. + + \snippet sensorsshowcase/gyroscope.qml 0 + + By pressing the reset button the image rotation is reset to 0. +*/ + diff --git a/examples/sensors/sensorsshowcase/gyroscope.qml b/examples/sensors/sensorsshowcase/gyroscope.qml new file mode 100644 index 00000000..1575255c --- /dev/null +++ b/examples/sensors/sensorsshowcase/gyroscope.qml @@ -0,0 +1,221 @@ +/**************************************************************************** +** +** 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. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick +import QtQuick.Controls +import QtQuick.Layouts +import QtSensors + +Rectangle { + id: root + color: "dimgray" + + function resetRotations() { + imageXRotation.angle = 0 + imageYRotation.angle = 0 + imageZRotation.angle = 0 + } + +//! [0] + Gyroscope { + id: gyroscope + active: true + dataRate: 25 + + property variant lastTimeStamp: 0 + + property real x: 0 + property real y: 0 + property real z: 0 + + onReadingChanged: { + + x = reading.x + y = reading.y + z = reading.z + + var firstCall = false + if (lastTimeStamp == 0) { + firstCall = true + } + + var timeSinceLast = reading.timestamp - lastTimeStamp + lastTimeStamp = reading.timestamp + + //Skipping the initial time jump from 0 + if (firstCall === true) return + + var normalizedX = reading.x * (timeSinceLast/1000000) + imageXRotation.angle += normalizedX + + var normalizedY = reading.y * (timeSinceLast/1000000) + imageYRotation.angle -= normalizedY + + var normalizedZ = reading.z * (timeSinceLast/1000000) + imageZRotation.angle += normalizedZ + } + } +//! [0] + + ColumnLayout { + anchors.fill: parent + id: layout + + Text { + Layout.topMargin: titleTopMargin + Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter + Layout.preferredHeight: textHeight + color: "White" + text: "Gyroscope" + font.pixelSize: titleFontSize + } + + Image { + id: image + Layout.alignment: Qt.AlignCenter + Layout.fillHeight: true + Layout.preferredWidth: root.height/3 + Layout.preferredHeight: root.height/3 + source: "qrc:/images/qt_logo.png" + fillMode: Image.PreserveAspectFit + + transform: [ + Rotation { + id: imageXRotation + origin.x: layout.width/2 + origin.y: layout.height/3 + axis.x: 1 + axis.y: 0 + axis.z: 0 + angle: 0 + }, + Rotation { + id: imageYRotation + origin.x: layout.width/2 + origin.y: layout.height/3 + axis.x: 0 + axis.y: 1 + axis.z: 0 + angle: 0 + }, + Rotation { + id: imageZRotation + origin.x: layout.width/2 + origin.y: layout.height/3 + axis.x: 0 + axis.y: 0 + axis.z: 1 + angle: 0 + } + ] + } + + Text { + Layout.preferredWidth: root.width + Layout.preferredHeight: textHeight + Layout.leftMargin: layout.spacing + color: "White" + text: "X: " + gyroscope.x.toFixed(2) + font.pixelSize: textFontSize + } + + ProgressBar { + id: xbar + value: 0.5 + (gyroscope.x / 1000) + Layout.preferredWidth: root.width + } + + Text { + Layout.preferredWidth: root.width + Layout.preferredHeight: textHeight + Layout.leftMargin: layout.spacing + color: "White" + text: "Y: " + gyroscope.y.toFixed(2) + font.pixelSize: textFontSize + } + ProgressBar { + id: ybar + value: 0.5 + (gyroscope.y / 1000) + Layout.preferredWidth: root.width + } + Text { + Layout.preferredWidth: root.width + Layout.preferredHeight: textHeight + Layout.leftMargin: layout.spacing + color: "White" + text: "Z: " + gyroscope.z.toFixed(2) + font.pixelSize: textFontSize + } + ProgressBar { + id: zbar + value: 0.5 + (gyroscope.z / 1000) + Layout.preferredWidth: root.width + } + Button { + Layout.alignment: Qt.AlignBottom + Layout.preferredWidth: root.width + Layout.preferredHeight: buttonHeight + text:"Reset rotation" + font.pixelSize: buttonFontSize + onClicked: { + resetRotations() + } + } + Button { + Layout.alignment: Qt.AlignBottom + Layout.preferredWidth: root.width + Layout.preferredHeight: buttonHeight + text:"Back" + font.pixelSize: buttonFontSize + onClicked:stack.pop() + } + } +} + diff --git a/examples/sensors/sensorsshowcase/images/compass.svg b/examples/sensors/sensorsshowcase/images/compass.svg new file mode 100644 index 00000000..009e6ffd --- /dev/null +++ b/examples/sensors/sensorsshowcase/images/compass.svg @@ -0,0 +1,222 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + N + S + E + W + + diff --git a/examples/sensors/sensorsshowcase/images/magnet.svg b/examples/sensors/sensorsshowcase/images/magnet.svg new file mode 100644 index 00000000..21d9f46b --- /dev/null +++ b/examples/sensors/sensorsshowcase/images/magnet.svg @@ -0,0 +1,98 @@ + + + + + + + + + + + + + + + diff --git a/examples/sensors/sensorsshowcase/images/qt_logo.png b/examples/sensors/sensorsshowcase/images/qt_logo.png new file mode 100644 index 00000000..30c621c9 Binary files /dev/null and b/examples/sensors/sensorsshowcase/images/qt_logo.png differ diff --git a/examples/sensors/sensorsshowcase/magnetometer.qml b/examples/sensors/sensorsshowcase/magnetometer.qml new file mode 100644 index 00000000..17732c7f --- /dev/null +++ b/examples/sensors/sensorsshowcase/magnetometer.qml @@ -0,0 +1,150 @@ +/**************************************************************************** +** +** 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. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick +import QtQuick.Controls +import QtQuick.Layouts +import QtSensors + +Rectangle { + id: root + color: "dimgray" + + property real magnetRotation: 40 + property real magnetometerX: 0 + property real magnetometerY: 0 + property real magnetometerZ: 0 + property int barScaleFactor: 10000 + +//! [0] + Magnetometer { + id: magnetometer + active: true + dataRate: 25 + onReadingChanged: { + root.magnetometerX = reading.x + root.magnetometerY = reading.y + root.magnetometerZ = reading.z + root.magnetRotation = ((Math.atan2(reading.x, reading.y) / Math.PI) * 180) + } + } +//! [0] + + ColumnLayout { + anchors.fill: parent + id: layout + + Text { + Layout.topMargin: titleTopMargin + Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter + Layout.preferredHeight: titleHeight + verticalAlignment: Text.AlignVCenter + color: "White" + text: "Magnetometer" + font.pixelSize: titleFontSize + } + Image { + Layout.alignment: Qt.AlignCenter + Layout.fillHeight: true + Layout.preferredWidth: root.width / 2 + Layout.preferredHeight: root.height / 2 + source: "qrc:/images/magnet.svg" + fillMode: Image.PreserveAspectFit + rotation: magnetRotation + } + Text { + Layout.preferredWidth: root.width + Layout.preferredHeight: textHeight + Layout.leftMargin: layout.spacing + color: "White" + text: "X: " + root.magnetometerX.toFixed(9) + font.pixelSize: textFontSize + } + ProgressBar { + id: xbar + value: 0.5 + (root.magnetometerX * barScaleFactor) + Layout.preferredWidth: root.width + } + Text { + Layout.preferredWidth: root.width + Layout.preferredHeight: textHeight + Layout.leftMargin: layout.spacing + color: "White" + text: "Y: " + root.magnetometerY.toFixed(9) + font.pixelSize: textFontSize + } + ProgressBar { + id: ybar + value: 0.5 + (root.magnetometerY * barScaleFactor) + Layout.preferredWidth: root.width + } + Text { + Layout.preferredWidth: root.width + Layout.preferredHeight: textHeight + Layout.leftMargin: layout.spacing + color: "White" + text: "Z: " + root.magnetometerZ.toFixed(9) + font.pixelSize: textFontSize + } + ProgressBar { + id: zbar + value: 0.5 + (root.magnetometerZ * barScaleFactor) + Layout.preferredWidth: root.width + } + Button { + Layout.alignment: Qt.AlignBottom + Layout.preferredWidth: root.width + Layout.preferredHeight: buttonHeight + text:"Back" + font.pixelSize: buttonFontSize + onClicked:stack.pop() + } + } +} diff --git a/examples/sensors/sensorsshowcase/main.cpp b/examples/sensors/sensorsshowcase/main.cpp new file mode 100644 index 00000000..4ee79963 --- /dev/null +++ b/examples/sensors/sensorsshowcase/main.cpp @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** 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. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include +#include + +int main(int argc, char *argv[]) +{ + QGuiApplication app(argc,argv); + QQmlApplicationEngine engine(QUrl("qrc:///sensorsshowcase.qml")); + + return app.exec(); +} diff --git a/examples/sensors/sensorsshowcase/proximity.qml b/examples/sensors/sensorsshowcase/proximity.qml new file mode 100644 index 00000000..3a235490 --- /dev/null +++ b/examples/sensors/sensorsshowcase/proximity.qml @@ -0,0 +1,118 @@ +/**************************************************************************** +** +** 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. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick +import QtQuick.Controls +import QtQuick.Layouts +import QtSensors + +Rectangle { + id: root + color: "dimgray" + property bool near: false + + ProximitySensor { + id: proximity + active: true + onReadingChanged: { + root.near = reading.near + } + } + + ColumnLayout { + anchors.fill: parent + id: layout + + Text { + Layout.topMargin: titleTopMargin + Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter + Layout.preferredHeight: textHeight + color: "White" + text: "Proximity" + font.pixelSize: titleFontSize + } + Image { + Layout.alignment: Qt.AlignCenter + Layout.fillHeight: true + Layout.preferredWidth: root.near ? root.height/3 : root.height/4 + Layout.preferredHeight: root.near ? root.height/3 : root.height/4 + source: "qrc:/images/qt_logo.png" + fillMode: Image.PreserveAspectFit + } + Text { + visible: !proximity + Layout.preferredWidth: root.width + Layout.preferredHeight: textHeight + Layout.leftMargin: layout.spacing + font.pixelSize: textFontSize + verticalAlignment: Text.AlignVCenter + wrapMode: Text.WordWrap + color: "White" + text: "The proximity sensor is not available on this device!" + } + Text { +// visible: typeof proximity.available !== 'undefined' + Layout.preferredWidth: root.width + Layout.preferredHeight: textHeight + Layout.leftMargin: layout.spacing + color: "White" + text: "Near: " + root.near + font.pixelSize: textFontSize + } + Button { + Layout.alignment: Qt.AlignBottom + Layout.preferredWidth: root.width + Layout.preferredHeight: buttonHeight + text:"Back" + font.pixelSize: buttonFontSize + onClicked:stack.pop() + } + } +} diff --git a/examples/sensors/sensorsshowcase/sensorsshowcase.pro b/examples/sensors/sensorsshowcase/sensorsshowcase.pro new file mode 100644 index 00000000..f57733f5 --- /dev/null +++ b/examples/sensors/sensorsshowcase/sensorsshowcase.pro @@ -0,0 +1,27 @@ +TEMPLATE = app +TARGET = sensorsshowcase +QT += quick sensors svg +SOURCES = main.cpp + +RESOURCES += \ + sensorsshowcase.qrc + +OTHER_FILES = \ + $$files(*.qml) \ + images \ + android/AndroidManifest.xml + +target.path = $$[QT_INSTALL_EXAMPLES]/sensors/sensorsshowcase +INSTALLS += target + +ios { +QMAKE_INFO_PLIST = Info.plist + +# manual plugin loading needed with older Qt +# QTPLUGIN += qsvg qtsensors_ios qtsensors_generic +} + +ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android + +EXAMPLE_FILES += \ + Info.plist diff --git a/examples/sensors/sensorsshowcase/sensorsshowcase.qml b/examples/sensors/sensorsshowcase/sensorsshowcase.qml new file mode 100644 index 00000000..dda6cda5 --- /dev/null +++ b/examples/sensors/sensorsshowcase/sensorsshowcase.qml @@ -0,0 +1,123 @@ +/**************************************************************************** +** +** 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. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick +import QtQuick.Controls +import QtQuick.Layouts +import QtSensors + +ApplicationWindow { + title: "Sensors Showcase" + id: mainWindow + width: 540 + height: 1200 + visible: true + color: "dimgray" + + property int titleHeight: height/7 + readonly property real buttonHeight: height/7 + property int textHeight: height/24 + readonly property int buttonFontSize: 30 + readonly property int textFontSize: 20 + readonly property int titleFontSize: 35 + readonly property int titleTopMargin: mainWindow.height/24 + + StackView { + id: stack + anchors.fill: parent + initialItem: ColumnLayout { + width: stack.width + Text { + Layout.topMargin: mainWindow.height/12 + Layout.alignment: Qt.AlignCenter + Layout.preferredHeight: titleHeight + text: "Sensors Showcase" + color: "White" + font.pixelSize: titleFontSize + } + + Button { + Layout.fillHeight: true + Layout.preferredWidth: stack.width + text: "Accelerometer" + font.pixelSize: buttonFontSize + onClicked: stack.push("qrc:/accelerometer.qml") + } + Button { + Layout.fillHeight: true + Layout.preferredWidth: stack.width + text: "Proximity" + font.pixelSize: buttonFontSize + onClicked: stack.push("qrc:/proximity.qml") + } + Button { + Layout.fillHeight: true + Layout.preferredWidth: stack.width + text: "Compass" + font.pixelSize: buttonFontSize + onClicked: stack.push("qrc:/compass.qml") + } + Button { + Layout.fillHeight: true + Layout.preferredWidth: stack.width + text: "Magnetometer" + font.pixelSize: buttonFontSize + onClicked: stack.push("qrc:/magnetometer.qml") + } + Button { + Layout.fillHeight: true + Layout.preferredWidth: stack.width + text: "Gyroscope" + font.pixelSize: buttonFontSize + onClicked: stack.push("qrc:/gyroscope.qml") + } + } + } +} diff --git a/examples/sensors/sensorsshowcase/sensorsshowcase.qrc b/examples/sensors/sensorsshowcase/sensorsshowcase.qrc new file mode 100644 index 00000000..e647c384 --- /dev/null +++ b/examples/sensors/sensorsshowcase/sensorsshowcase.qrc @@ -0,0 +1,13 @@ + + + sensorsshowcase.qml + accelerometer.qml + proximity.qml + compass.qml + magnetometer.qml + gyroscope.qml + images/magnet.svg + images/compass.svg + images/qt_logo.png + + -- cgit v1.2.3