diff options
21 files changed, 487 insertions, 1303 deletions
diff --git a/examples/sensors/sensor_explorer/CMakeLists.txt b/examples/sensors/sensor_explorer/CMakeLists.txt index 3440f79b..32a2ab8a 100644 --- a/examples/sensors/sensor_explorer/CMakeLists.txt +++ b/examples/sensors/sensor_explorer/CMakeLists.txt @@ -13,11 +13,13 @@ endif() set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/sensors/sensor_explorer") -find_package(Qt6 COMPONENTS Qml Quick) +find_package(Qt6 COMPONENTS Qml Quick Sensors) qt_add_executable(sensor_explorer main.cpp + sensormodels.cpp sensormodels.h ) + set_target_properties(sensor_explorer PROPERTIES WIN32_EXECUTABLE TRUE MACOSX_BUNDLE TRUE @@ -25,9 +27,9 @@ set_target_properties(sensor_explorer PROPERTIES target_link_libraries(sensor_explorer PUBLIC Qt::Qml Qt::Quick + Qt::Sensors ) - # Resources: set(qml_resource_files "sensor_explorer.qml" @@ -40,15 +42,15 @@ qt6_add_resources(sensor_explorer "qml" ${qml_resource_files} ) -if(TARGET Qt::Widgets) - target_link_libraries(sensor_explorer PUBLIC - Qt::Widgets - ) -endif() - install(TARGETS sensor_explorer RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" ) -add_subdirectory(import) + +set_target_properties(sensor_explorer PROPERTIES + QT_QML_MODULE_VERSION 1.0 + QT_QML_MODULE_URI SensorModels +) + +qt_qml_type_registration(sensor_explorer) diff --git a/examples/sensors/sensor_explorer/doc/images/qtsensors-examples-explorer.png b/examples/sensors/sensor_explorer/doc/images/qtsensors-examples-explorer.png Binary files differdeleted file mode 100644 index 888fc3c6..00000000 --- a/examples/sensors/sensor_explorer/doc/images/qtsensors-examples-explorer.png +++ /dev/null diff --git a/examples/sensors/sensor_explorer/doc/images/qtsensors-examples-sensor-explorer.png b/examples/sensors/sensor_explorer/doc/images/qtsensors-examples-sensor-explorer.png Binary files differnew file mode 100644 index 00000000..830f9597 --- /dev/null +++ b/examples/sensors/sensor_explorer/doc/images/qtsensors-examples-sensor-explorer.png diff --git a/examples/sensors/sensor_explorer/doc/src/sensor_explorer.qdoc b/examples/sensors/sensor_explorer/doc/src/sensor_explorer.qdoc index 367711b1..aef88c39 100644 --- a/examples/sensors/sensor_explorer/doc/src/sensor_explorer.qdoc +++ b/examples/sensors/sensor_explorer/doc/src/sensor_explorer.qdoc @@ -31,74 +31,113 @@ \ingroup qtsensors-examples \brief Demonstrates how to read the meta-data of available sensors. - \image qtsensors-examples-explorer.png + \image qtsensors-examples-sensor-explorer.png - This example is divided into two parts: + The example is implemented as a typical model-view application. The + models are written with C++ and exposed to QML, and the views are + implemented as QML types in the QML application. - \list - \li A \l{Sensor Explorer QML Import}{C++ plugin} that provides QML - alternatives for \c QSensorExplorer, \c QPropertyInfo and - \c QSensorItem C++ classes. - \li A \l{Sensor Explorer QML Application}{QML Application} that uses - the QML types to read the sensor meta-data and present it. - \endlist + \section1 Exposing and Importing the Models - This example is built as an executable with C++ code that runs the QML, - but it can also be launched directly using the \c qmlscene tool. You - should build the top-level \e sensor_explorer project before trying to - run this example or it will not be able to find its dependencies. + The QML models written in C++ are exposed in the project build files + as a \c SensorModels QML module. + \e CMake: \code - qmlscene -I . sensor_explorer.qml + set_target_properties(sensor_explorer PROPERTIES + QT_QML_MODULE_VERSION 1.0 + QT_QML_MODULE_URI SensorModels + ) + qt_qml_type_registration(sensor_explorer) \endcode - Above, the -I . parameter adds the current directory as a module import - path to locate the Explorer QML module. + \e qmake: + \code + CONFIG += qmltypes + QML_IMPORT_NAME = SensorModels + QML_IMPORT_MAJOR_VERSION = 1 + \endcode - \section1 Sensor Explorer QML Import + The indvidual model elements are exposed as part of the model + code by using the \c QML_ELEMENT macro as illustrated here: + \snippet sensor_explorer/sensormodels.h 0 - The Sensor Explorer QML import defines the \e Explorer QML module, - exporting \c QSensorExplorer, \c QPropertyInfo and \c QSensorItem C++ - classes as QML types. The source code is available in the - \c sensor_explorer/import subdirectory. + To access the models in the QML application, the module is imported + by the application QML: + \snippet sensor_explorer/sensor_explorer.qml 0 - \section1 Sensor Explorer QML Application + \section1 Populating the Model of Available Sensors - To write a QML application that will use the QML types exposed by the - Explorer module, following steps are needed: + The \c AvailableSensorsModel is a list model that provides information on + available sensors on the device. The model is populated once at the + element's construction time: - Import the Explorer 1.0 declarative plugin: + \snippet sensor_explorer/sensormodels.cpp 0 - \snippet sensor_explorer/sensor_explorer.qml 0 + The model's \c data() function returns a pointer to the requested sensor + object. - Create a SensorExplorer QML item: + \snippet sensor_explorer/sensormodels.cpp 1 - \snippet sensor_explorer/sensor_explorer.qml 1 + Since the sensor (QSensor) is a QObject, the QML is then able to directly + access all metaproperties and -functions directly. - You can retrieve a list of all available sensors using - \c SensorExplorer.availableSensors: + \note It would be possible to refresh the sensor list later at will, but for + the simplicity of the example such functionality is not exposed to QML. - \snippet sensor_explorer/sensor_explorer.qml 2 + \section1 Populating the Model of Sensor Properties + + The \c SensorPropertyModel is a table model that provides individual + sensor's property-value pairs as columns. The column \c 0 provides the + property's name and the column \c 1 provides the property's value. The + population of the properties is done by reading the metadata of the sensors. + The model reads both the sensor's metadata as well as the sensor's reading's + metadata. The code below illustrates the reading of the \c reading metadata: + + \snippet sensor_explorer/sensormodels.cpp 2 + + This metadata access allows providing the model data for all sensors + without prior compile-time understanding of their properties. - The example uses the returned list as a model to populate a view of - available sensors. + Once the metadata is set, the code then subscribes to the + QSensor::readingChanged() signal to detect sensor reading changes. + Upon such changes (for example a rotation value changes), the model data + is updated accordingly. - To retrieve the properties of a sensor, use \c SensorItem.properties: + \section1 Viewing the Models + + The QML application is based on two views. The first view shows the available + sensors as a selectable list. The second view shows the selected sensor's + properties and their values. The delegates for viewing the individual items + are simplistic \e {rectangle and text} items. + + Binding the two views functionally together is done by binding the property + model's \c sensor property to the current selection of the available sensors + model: + + \snippet sensor_explorer/sensor_explorer.qml 1 + + When the selected sensor changes, the \c sensor of the property model changes + accordingly. + + The following snippet illustrates how the property view is implemented. For + more details about QML models and views, please see + \l{Models and Views in Qt Quick}. \snippet sensor_explorer/sensor_explorer.qml 3 - The property list is used as a model for another view that displays the - property names and values. + For clarity it should be mentioned that the \c display attribute used by + the text element refers to the Qt::DisplayRole role of the model, which is + provided by default by Qt models. - It is possible to edit the values of certain sensor properties. Selecting - a writable property value will open an editor. \c SensorExplorer QML - type allows you to pass a new value for a sensor property value as - follows: + \section1 Activating the Sensors - \snippet sensor_explorer/sensor_explorer.qml 4 + The example has a button for activating and deactivating the currently + selected sensor. The button is enabled only if a sensor is currently + selected, as illustrated below. + + \snippet sensor_explorer/sensor_explorer.qml 2 - Starting and stopping a sensor can be done by setting the - \c SensorItem.start property: + On clicking the button, the sensor's active property is toggled on/off. - \snippet sensor_explorer/sensor_explorer.qml 5 */ diff --git a/examples/sensors/sensor_explorer/import/CMakeLists.txt b/examples/sensors/sensor_explorer/import/CMakeLists.txt deleted file mode 100644 index 306e1b7c..00000000 --- a/examples/sensors/sensor_explorer/import/CMakeLists.txt +++ /dev/null @@ -1,58 +0,0 @@ -cmake_minimum_required(VERSION 3.14) -project(declarative_explorer 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/sensor_explorer/Explorer") - -find_package(Qt6 COMPONENTS Core Gui Qml Sensors) - -# Hack to convince qt6_add_qml_module to respect the shared-ness of a Qt build -# TODO Qt 6.2 FIXME when a proper fix is available -set(BUILD_SHARED_LIBS "${QT6_IS_SHARED_LIBS_BUILD}") - -if (APPLE AND NOT IOS) - set(PLUGIN_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../sensor_explorer.app/Contents/MacOS/Explorer") -else() - set(PLUGIN_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../Explorer") -endif() - -qt6_add_qml_module(declarative_explorer - OUTPUT_DIRECTORY "${PLUGIN_OUTPUT_DIRECTORY}" - VERSION 1.0 - URI "Explorer" - INSTALL_LOCATION ${INSTALL_EXAMPLEDIR} -) - -target_sources(declarative_explorer PRIVATE - explorer.cpp explorer.h - main.cpp main.h - propertyinfo.cpp propertyinfo.h - sensoritem.cpp sensoritem.h -) - -set_target_properties(declarative_explorer PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) - -target_link_libraries(declarative_explorer PUBLIC - Qt::Core - Qt::Gui - Qt::Qml - Qt::Sensors -) - -install(TARGETS declarative_explorer - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/sensors/sensor_explorer/import/explorer.cpp b/examples/sensors/sensor_explorer/import/explorer.cpp deleted file mode 100644 index 0665a98f..00000000 --- a/examples/sensors/sensor_explorer/import/explorer.cpp +++ /dev/null @@ -1,152 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 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 "explorer.h" -#include <QtCore/QDebug> -#include <qsensor.h> -#include <QMetaObject> -#include <QMetaProperty> - -QT_BEGIN_NAMESPACE - -/* - \class QSensorExplorer - \brief The QSensorExplorer type provides an easy access for reading all avalaible sensors installed on the system. -*/ - -/* - Construct a QSensorExplorer object with parent \a parent -*/ -QSensorExplorer::QSensorExplorer(QObject* parent) - : QObject(parent) - , _selectedSensorItem(0) -{ - loadSensors(); -} - -/* - Destructor of a QSensorExplorer -*/ -QSensorExplorer::~QSensorExplorer() -{ -} - -/* - Load all available sensors and store it in a list. -*/ -void QSensorExplorer::loadSensors() -{ - //! [0] - _availableSensors.clear(); - - foreach (const QByteArray &type, QSensor::sensorTypes()) { - qDebug() << "Found type" << type; - foreach (const QByteArray &identifier, QSensor::sensorsForType(type)) { - qDebug() << "Found identifier" << identifier; - // Don't put in sensors we can't connect to - QSensor* sensor = new QSensor(type, this); - sensor->setIdentifier(identifier); - if (!sensor->connectToBackend()) { - qDebug() << "Couldn't connect to" << identifier; - continue; - } - - qDebug() << "Adding identifier" << identifier; - _availableSensors.append(new QSensorItem(sensor, this)); - } - } - emit availableSensorsChanged(); - //! [0] -} - -/* - \fn QSensorExplorer::availableSensorsChanged() - Notifies the client if the list of the available sensors was changed -*/ - -/* - \property QSensorExplorer::availableSensors - Returns a list of all available sensor. -*/ -QQmlListProperty<QSensorItem> QSensorExplorer::availableSensors() -{ - return QQmlListProperty<QSensorItem>(this, &_availableSensors); -} - -/* - \fn QSensorExplorer::selectedSensorItemChanged() - Notifies the client if the selected sensor has been changed -*/ - -/* - \property QSensorExplorer::selectedSensorItem - Returns the current selected sensor item. -*/ -QSensorItem* QSensorExplorer::selectedSensorItem() -{ - return _selectedSensorItem; -} - -/* - \fn QSensorExplorer::setSelectedSensorItem(QSensorItem* selitem) - Sets the QSensorItem \a selitem as the current selected QSensorItem. -*/ -void QSensorExplorer::setSelectedSensorItem(QSensorItem* selitem) -{ - if (selitem && _selectedSensorItem != selitem) { - if (_selectedSensorItem) - _selectedSensorItem->unSelect(); - _selectedSensorItem = selitem; - _selectedSensorItem->select(); - emit selectedSensorItemChanged(); - } -} - -QT_END_NAMESPACE diff --git a/examples/sensors/sensor_explorer/import/import.json b/examples/sensors/sensor_explorer/import/import.json deleted file mode 100644 index 0967ef42..00000000 --- a/examples/sensors/sensor_explorer/import/import.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/examples/sensors/sensor_explorer/import/import.pro b/examples/sensors/sensor_explorer/import/import.pro deleted file mode 100644 index 53bf224b..00000000 --- a/examples/sensors/sensor_explorer/import/import.pro +++ /dev/null @@ -1,39 +0,0 @@ -TEMPLATE = lib -CONFIG += plugin -QML_IMPORT_VERSION = $$QT_VERSION - -TARGET = $$qtLibraryTarget(declarative_explorer) - -macos: DESTDIR = ../sensor_explorer.app/Contents/MacOS/Explorer -else: DESTDIR = ../Explorer - -QT += qml sensors - -SOURCES = \ - main.cpp \ - explorer.cpp \ - sensoritem.cpp \ - propertyinfo.cpp - -HEADERS = \ - main.h \ - explorer.h \ - sensoritem.h \ - propertyinfo.h - -DESTPATH=$$[QT_INSTALL_EXAMPLES]/sensors/sensor_explorer/Explorer - -target.path=$$DESTPATH -qmldir.files=$$PWD/qmldir -qmldir.path=$$DESTPATH -INSTALLS += target qmldir - -CONFIG += install_ok # Do not cargo-cult this! - -OTHER_FILES += \ - import.json qmldir - -# Copy the qmldir file to the same folder as the plugin binary -cpqmldir.files = $$PWD/qmldir -cpqmldir.path = $$DESTDIR -COPIES += cpqmldir diff --git a/examples/sensors/sensor_explorer/import/main.cpp b/examples/sensors/sensor_explorer/import/main.cpp deleted file mode 100644 index 4199b2f4..00000000 --- a/examples/sensors/sensor_explorer/import/main.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2019 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 "main.h" -#include "explorer.h" - -QT_BEGIN_NAMESPACE - -void SensorExplorerDeclarativeModule::registerTypes(const char *uri) -{ - Q_ASSERT(QLatin1String(uri) == QLatin1String("Explorer")); - // @uri Explorer - qmlRegisterType<QSensorExplorer>(uri, 1, 0, "SensorExplorer"); - qmlRegisterType<QSensorItem>(uri, 1, 0, "SensorItem"); - qmlRegisterType<QPropertyInfo>(uri, 1, 0, "PropertyInfo"); -} - -QT_END_NAMESPACE diff --git a/examples/sensors/sensor_explorer/import/main.h b/examples/sensors/sensor_explorer/import/main.h deleted file mode 100644 index 6964bec6..00000000 --- a/examples/sensors/sensor_explorer/import/main.h +++ /dev/null @@ -1,64 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2019 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 <QtQml/QQmlExtensionPlugin> -#include <QtQml/QtQml> - -QT_BEGIN_NAMESPACE - -class SensorExplorerDeclarativeModule : public QQmlExtensionPlugin -{ - Q_OBJECT - Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid FILE "import.json") -public: - void registerTypes(const char *uri) override; -}; - -QT_END_NAMESPACE diff --git a/examples/sensors/sensor_explorer/import/propertyinfo.cpp b/examples/sensors/sensor_explorer/import/propertyinfo.cpp deleted file mode 100644 index 4704a6a1..00000000 --- a/examples/sensors/sensor_explorer/import/propertyinfo.cpp +++ /dev/null @@ -1,149 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 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 "sensoritem.h" -#include <QtCore/QDebug> -#include <QtSensors> - -QT_BEGIN_NAMESPACE - -/* - \class QPropertyInfo - \brief The QPropertyInfo type provides an easy access for reading and writing the property values. -*/ - -/* - Construct a QPropertyInfo object with parent \a parent -*/ -QPropertyInfo::QPropertyInfo(QObject* parent) - : QObject(parent) - , _index(0) - , _isWriteable(false) - , _name("") - , _typeName("") - , _value("") -{} - -/* - Construct a QPropertyInfo object with parent \a parent, property name \a name, property index \a index, - property write access \a writeable, property type \a typeName and property value \a value -*/ -QPropertyInfo::QPropertyInfo(const QString& name, int index, bool writeable, const QString& typeName, const QString& value, QObject* parent) - : QObject(parent) - , _index(index) - , _isWriteable(writeable) - , _name(name) - , _typeName(typeName) - , _value(value) -{} - -/* - \property QPropertyInfo::name - Returns the name of the property -*/ -QString QPropertyInfo::name() -{ - return _name; -} - -/* - \property QPropertyInfo::typeName - Returns the type of the property -*/ -QString QPropertyInfo::typeName() -{ - return _typeName; -} - -/* - \property QPropertyInfo::value - Returns the current value of the property -*/ -QString QPropertyInfo::value() -{ - return _value; -} - -/* - \fn void QPropertyInfo::valueChanged() - Signal that notifies the client if the property value was changed. -*/ - -/* - \fn QPropertyInfo::setValue(const QString& value) - Sets the value \a value of the property -*/ -void QPropertyInfo::setValue(const QString& value) -{ - if (value != _value){ - _value = value; - emit valueChanged(); - } -} - -/* - \fn QPropertyInfo::index() - Returns the meta-data index of the property -*/ -int QPropertyInfo::index() -{ - return _index; -} - -/* - \property QPropertyInfo::isWriteable - Returns true if the property is writeable false if property is read only -*/ -bool QPropertyInfo::isWriteable() -{ - return _isWriteable; -} - -QT_END_NAMESPACE diff --git a/examples/sensors/sensor_explorer/import/propertyinfo.h b/examples/sensors/sensor_explorer/import/propertyinfo.h deleted file mode 100644 index 53b194a3..00000000 --- a/examples/sensors/sensor_explorer/import/propertyinfo.h +++ /dev/null @@ -1,92 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 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$ -** -****************************************************************************/ - -#ifndef QPROPERTYINFO_H -#define QPROPERTYINFO_H - -#include <QtQml/QtQml> -#include <QtCore/QString> - -QT_BEGIN_NAMESPACE - -class QPropertyInfo : public QObject -{ - Q_OBJECT - Q_PROPERTY(QString name READ name CONSTANT) - Q_PROPERTY(QString typeName READ typeName CONSTANT) - Q_PROPERTY(QString value READ value NOTIFY valueChanged) - Q_PROPERTY(bool isWriteable READ isWriteable CONSTANT) - -public: - QPropertyInfo(QObject* parent = 0); - QPropertyInfo(const QString& name, int index, bool writeable, const QString& typeName, const QString& value, QObject* parent=0); - QString name(); - QString typeName(); - QString value(); - void setValue(const QString& value); - int index(); - bool isWriteable(); - -Q_SIGNALS: - void valueChanged(); - -private: - int _index; - bool _isWriteable; - QString _name; - QString _typeName; - QString _value; -}; - -QT_END_NAMESPACE - -QML_DECLARE_TYPE(QPropertyInfo) - -#endif // QPROPERTYINFO_H diff --git a/examples/sensors/sensor_explorer/import/qmldir b/examples/sensors/sensor_explorer/import/qmldir deleted file mode 100644 index 31c037fa..00000000 --- a/examples/sensors/sensor_explorer/import/qmldir +++ /dev/null @@ -1,2 +0,0 @@ -module Explorer -plugin declarative_explorer diff --git a/examples/sensors/sensor_explorer/import/sensoritem.cpp b/examples/sensors/sensor_explorer/import/sensoritem.cpp deleted file mode 100644 index 40fb847e..00000000 --- a/examples/sensors/sensor_explorer/import/sensoritem.cpp +++ /dev/null @@ -1,331 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 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 "sensoritem.h" -#include <QtCore/QDebug> -#include <QtSensors> - -QT_BEGIN_NAMESPACE - -/* - \class QSensorItem - \brief The QSensorItem type provides information about the metadata from a sensors installed on the system. -*/ - -/* - Construct a QSensorItem object with parent \a parent -*/ -QSensorItem::QSensorItem(QObject* parent) - : QObject(parent) - , _qsensor(0) -{ -} - -/* - Construct a QSensorItem object with QSensor \a sensor and parent \a parent -*/ -QSensorItem::QSensorItem(QSensor* sensor, QObject* parent) - : QObject(parent) - , _qsensor(sensor) -{ -} - -/* - Destructor of a QSensorItem -*/ -QSensorItem::~QSensorItem() -{ -} - -/* - \fn QSensorItem::startChanged() - Notifies the client if the sensors has changed its start active status -*/ - -/* - \property QSensorItem::start - This property starts or stops the sensor -*/ -bool QSensorItem::start() -{ - return (_qsensor ? _qsensor->isActive() : false); -} - -void QSensorItem::setStart(bool run) -{ - if (_qsensor) - _qsensor->setActive(run); -} - -/* - \property QSensorItem::id - Returns the sensor id of the sensor item -*/ -QString QSensorItem::id() -{ - return (_qsensor ? _qsensor->identifier() : QStringLiteral("")); -} - -/* - Set the new value \a val to the QPropertyInfo object \a property -*/ -void QSensorItem::changePropertyValue(QPropertyInfo* property, const QString& val) -{ - if (_qsensor && _sensorProperties.contains(property)){ - if ( _qsensor->setProperty(property->name().toLatin1().constData(), QVariant(val))) - updateSensorPropertyValues(); - else - qWarning() << "new property value couldn't be set"; - } -} - -/* - Reading the metadata and activates the sensor. -*/ -void QSensorItem::select() -{ - if (_sensorProperties.isEmpty()) { - // Probe the reading using Qt's meta-object facilities - //Read properties from reader - QSensorReading *reading = _qsensor->reading(); - const QMetaObject *mo = reading->metaObject(); - int firstProperty = QSensorReading::staticMetaObject.propertyOffset(); - - for (int i = firstProperty; i < mo->propertyCount(); ++i) { - QString typeName = QLatin1String(mo->property(i).typeName()); - int crap = typeName.lastIndexOf("::"); - if (crap != -1) - typeName = typeName.mid(crap + 2); - - QPropertyInfo* pi = new QPropertyInfo(mo->property(i).name() - , i - , isWriteable(mo->property(i).name()) - , typeName - , "-" - , this); - _readerProperties.append(pi); - } - - //Read properties from sensor - const QMetaObject *mo1 = _qsensor->metaObject(); - firstProperty = QSensorReading::staticMetaObject.propertyOffset(); - - for (int i = firstProperty; i < mo1->propertyCount(); ++i) { - QString propertyname = mo1->property(i).name(); - if (ignoreProperty(propertyname)) - continue; - - QString typeName = QLatin1String(mo1->property(i).typeName()); - int crap = typeName.lastIndexOf("::"); - if (crap != -1) - typeName = typeName.mid(crap + 2); - - QPropertyInfo* pi = new QPropertyInfo(propertyname - , i - , isWriteable(propertyname) - , typeName - , "-" - , this); - _sensorProperties.append(pi); - } - updateSensorPropertyValues(); - connect(_qsensor, SIGNAL(readingChanged()), this, SLOT(sensorReadingChanged())); - } - connect(_qsensor, SIGNAL(activeChanged()), SIGNAL(startChanged())); -} - -/* - Unselect the sensor by stopping the sensor. -*/ -void QSensorItem::unSelect() -{ - _qsensor->stop(); - disconnect(_qsensor, SIGNAL(activeChanged()), this , SIGNAL(startChanged())); -} - -/* - Updates the property values from QSensor -*/ -void QSensorItem::updateSensorPropertyValues() -{ - if (_qsensor){ - const QMetaObject *mo = _qsensor->metaObject(); - for (int i = 0; i < _sensorProperties.count(); i++){ - QVariant val = mo->property(_sensorProperties[i]->index()).read(_qsensor); - _sensorProperties[i]->setValue(convertValue(_sensorProperties[i]->typeName(), val)); - } - } -} - -/* - \fn QSensorItem::propertiesChanged() - Notifies the client if the list of the properties was changed -*/ - -/* - Updates the property values from the QSensorReader -*/ -void QSensorItem::sensorReadingChanged() -{ - QSensorReading *reading = _qsensor->reading(); - const QMetaObject *mo = reading->metaObject(); - for (int i = 0; i < _readerProperties.count(); i++){ - QVariant val = mo->property(_readerProperties[i]->index()).read(reading); - _readerProperties[i]->setValue(convertValue(_readerProperties[i]->typeName(), val)); - } -} - -/* - Returns true if the property with the name \a propertyname should be ignored -*/ -bool QSensorItem::ignoreProperty(const QString& propertyname) -{ - if (propertyname == "reading" || - propertyname == "active" || - propertyname == "identifier" || - propertyname == "connectedToBackend" || - propertyname == "busy") - return true; - - return false; -} - -/* - Returns true if the property with the name \a propertyname is writeable -*/ -bool QSensorItem::isWriteable(const QString& propertyname) -{ - if (_qsensor){ - const QMetaObject *mo = _qsensor->metaObject(); - int propertyindex = mo->indexOfProperty(propertyname.toLocal8Bit().constData()); - if (propertyindex >= 0){ - QMetaProperty prop = mo->property(propertyindex); - return prop.isWritable(); - } - else { - QSensorReading *reading = _qsensor->reading(); - const QMetaObject *moreader = reading->metaObject(); - propertyindex = moreader->indexOfProperty(propertyname.toLocal8Bit().constData()); - if (propertyindex >= 0){ - QMetaProperty prop = mo->property(propertyindex); - return prop.isWritable(); - } - } - } - - return false; -} - -/* - Convert the variant \a val dependent on the type \a type and returns the converted value as a QString -*/ -QString QSensorItem::convertValue(const QString& type, const QVariant& val) -{ - if (type == "LightLevel"){ - switch (val.toInt()) { - case 1: return "Dark"; - case 2: return "Twilight"; - case 3: return "Light"; - case 4: return "Bright"; - case 5: return "Sunny"; - default: return "Undefined"; - } - } - else if (type == "Orientation"){ - switch (val.toInt()) { - case 1: return "TopUp"; - case 2: return "TopDown"; - case 3: return "LeftUp"; - case 4: return "RightUp"; - case 5: return "FaceUp"; - case 6: return "FaceDown"; - default: return "Undefined"; - } - } - else if (type == "qrangelist"){ - qrangelist rangelist = val.value<qrangelist>(); - QStringList ranges; - foreach (const qrange &r, rangelist) { - if (r.first == r.second) - ranges << QString("%1 Hz").arg(r.first); - else - ranges << QString("%1-%2 Hz").arg(r.first).arg(r.second); - } - if (ranges.count() > 0) - return ranges.join(", "); - return "-"; - } - else if (type == "qoutputrangelist") { - qoutputrangelist rangelist = val.value<qoutputrangelist>(); - QStringList ranges; - foreach (const qoutputrange &r, rangelist) { - ranges << QString("(%1, %2) += %3").arg(r.minimum).arg(r.maximum).arg(r.accuracy); - } - if (ranges.count() > 0) - return ranges.join(", "); - return "-"; - } - - return val.toString(); -} - -/* - \property QSensorItem::properties - Returns a list of all properties from the sensor -*/ -QQmlListProperty<QPropertyInfo> QSensorItem::properties() -{ - _properties.clear(); - _properties.append(_sensorProperties); - _properties.append(_readerProperties); - return QQmlListProperty<QPropertyInfo> (this, &_properties); -} - -QT_END_NAMESPACE diff --git a/examples/sensors/sensor_explorer/import/sensoritem.h b/examples/sensors/sensor_explorer/import/sensoritem.h deleted file mode 100644 index f67b6bda..00000000 --- a/examples/sensors/sensor_explorer/import/sensoritem.h +++ /dev/null @@ -1,105 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 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$ -** -****************************************************************************/ - -#ifndef QSENSORITEM_H -#define QSENSORITEM_H - -#include <QtQml/QtQml> -#include <QtCore/QString> -#include "propertyinfo.h" - -QT_BEGIN_NAMESPACE - -class QSensor; -class QSensorItem : public QObject -{ - Q_OBJECT - Q_PROPERTY(bool start READ start WRITE setStart NOTIFY startChanged) - Q_PROPERTY(QString id READ id CONSTANT) - Q_PROPERTY(QQmlListProperty<QPropertyInfo> properties READ properties NOTIFY propertiesChanged) -public: - QSensorItem(QObject* parent = 0); - QSensorItem(QSensor* sensor, QObject* parent = 0); - virtual ~QSensorItem(); - -public slots: - void select(); - void unSelect(); - void changePropertyValue(QPropertyInfo* property, const QString& val); - -private slots: - void sensorReadingChanged(); - -private: - QString id(); - bool start(); - void setStart(bool run); - QQmlListProperty<QPropertyInfo> properties(); - QString convertValue(const QString& type, const QVariant& val); - bool isWriteable(const QString& propertyname); - bool ignoreProperty(const QString& propertyname); - void updateSensorPropertyValues(); - -Q_SIGNALS: - void propertiesChanged(); - void startChanged(); - -private: - QSensor* _qsensor; - QList<QPropertyInfo*> _properties; - QList<QPropertyInfo*> _readerProperties; - QList<QPropertyInfo*> _sensorProperties; -}; - -QT_END_NAMESPACE - -QML_DECLARE_TYPE(QSensorItem) - -#endif // QSENSORITEM_H diff --git a/examples/sensors/sensor_explorer/qml.pro b/examples/sensors/sensor_explorer/qml.pro deleted file mode 100644 index 1407d5b2..00000000 --- a/examples/sensors/sensor_explorer/qml.pro +++ /dev/null @@ -1,21 +0,0 @@ -TEMPLATE = app -TARGET = sensor_explorer -QT += qml quick - -qtHaveModule(widgets) { - QT += widgets -} - -# Avoid going to release/debug subdirectory -win32: DESTDIR = ./ - -SOURCES = main.cpp - -OTHER_FILES = \ - sensor_explorer.qml - -target.path = $$[QT_INSTALL_EXAMPLES]/sensors/sensor_explorer -INSTALLS += target - -RESOURCES += \ - qml.qrc diff --git a/examples/sensors/sensor_explorer/sensor_explorer.pro b/examples/sensors/sensor_explorer/sensor_explorer.pro index 85ba45ef..a27681e2 100644 --- a/examples/sensors/sensor_explorer/sensor_explorer.pro +++ b/examples/sensors/sensor_explorer/sensor_explorer.pro @@ -1,6 +1,22 @@ -TEMPLATE = subdirs -CONFIG += ordered +TEMPLATE = app +TARGET = sensor_explorer +QT += qml quick sensors -SUBDIRS = \ - import \ - qml.pro +# Avoid going to release/debug subdirectory +win32: DESTDIR = ./ + +SOURCES = main.cpp sensormodels.cpp +HEADERS = sensormodels.h + +CONFIG += qmltypes +QML_IMPORT_NAME = SensorModels +QML_IMPORT_MAJOR_VERSION = 1 + +OTHER_FILES = \ + sensor_explorer.qml + +target.path = $$[QT_INSTALL_EXAMPLES]/sensors/sensor_explorer +INSTALLS += target + +RESOURCES += \ + qml.qrc 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] } - */ } } diff --git a/examples/sensors/sensor_explorer/sensormodels.cpp b/examples/sensors/sensor_explorer/sensormodels.cpp new file mode 100644 index 00000000..4766fc58 --- /dev/null +++ b/examples/sensors/sensor_explorer/sensormodels.cpp @@ -0,0 +1,238 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 "sensormodels.h" +#include "qsensor.h" +#include "qorientationsensor.h" +#include <QtCore/QDebug> +#include <qsensor.h> +#include <QMetaObject> +#include <QMetaProperty> + +QT_BEGIN_NAMESPACE + +QByteArray rangelistToByteArray(const qrangelist& list) +{ + QStringList ranges; + for (const qrange &r : list) { + if (r.first == r.second) + ranges << QString("%1 Hz").arg(r.first); + else + ranges << QString("%1-%2 Hz").arg(r.first).arg(r.second); + } + if (ranges.count() > 0) + return ranges.join(", ").toLatin1(); + return "-"; +} + +QByteArray outputrangelistToByteArray(const qoutputrangelist& list) +{ + QStringList ranges; + for (const qoutputrange &r : list) { + ranges << QString("(%1, %2) += %3").arg(r.minimum).arg(r.maximum).arg(r.accuracy); + } + if (ranges.count() > 0) + return ranges.join(", ").toLatin1(); + return "-"; +} + +AvailableSensorsModel::AvailableSensorsModel(QObject* parent) : QAbstractListModel(parent) +{ + // Some valuetypes do not convert nicely to presentable strings, add converters for them + QMetaType::registerConverter<qrangelist, QByteArray>(rangelistToByteArray); + QMetaType::registerConverter<qoutputrangelist, QByteArray>(outputrangelistToByteArray); + + // Populate the available sensors list + loadSensors(); +} + +/* + Load all available sensors and store them in a list. +*/ +void AvailableSensorsModel::loadSensors() +{ + //! [0] + beginResetModel(); + m_availableSensors.clear(); + + for (const QByteArray &type : QSensor::sensorTypes()) { + for (const QByteArray &identifier : QSensor::sensorsForType(type)) { + QSensor* sensor = new QSensor(type, this); + sensor->setIdentifier(identifier); + // Don't put in sensors we can't connect to + if (!sensor->connectToBackend()) + continue; + m_availableSensors.append(sensor); + } + } + endResetModel(); + //! [0] +} + +int AvailableSensorsModel::rowCount(const QModelIndex&) const +{ + return m_availableSensors.count(); +} + +//! [1] +QVariant AvailableSensorsModel::data(const QModelIndex &index, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + return QVariant::fromValue<QSensor*>(m_availableSensors.at(index.row())); +} +//! [1] + +QSensor* AvailableSensorsModel::get(int index) const +{ + if (index < 0 || index >= m_availableSensors.count()) + return nullptr; + return m_availableSensors[index]; +} + +// -- SensorPropertyModel + +static QSet<QByteArray> ignoredProperties = {"reading", "identifier", "active", + "connectedToBackend", "busy"}; + +SensorPropertyModel::SensorPropertyModel(QObject* parent) : QAbstractTableModel(parent) +{ +} + +int SensorPropertyModel::rowCount(const QModelIndex&) const +{ + if (!m_sensor) + return 0; + return m_values.count(); +} + +int SensorPropertyModel::columnCount(const QModelIndex&) const +{ + return 2; // 2 = property name + value columns +} + +QVariant SensorPropertyModel::data(const QModelIndex &index, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + return (index.column() == 0) ? std::get<0>(m_values.at(index.row())) + : std::get<1>(m_values.at(index.row())); +} + +void SensorPropertyModel::setSensor(QSensor *sensor) +{ + if (m_sensor == sensor) + return; + if (m_sensor) + m_sensor->disconnect(this); + m_sensor = sensor; + + beginResetModel(); + m_values.clear(); + if (m_sensor) { + // Use metobject to read the available properties. This allows the model to support all + // available sensors without knowing their properties in advance / compile-time. + + // 1. Read properties of the 'reading' object if available + //! [2] + int firstProperty = QSensorReading::staticMetaObject.propertyOffset(); + QSensorReading *reading = m_sensor->reading(); + if (reading) { + const QMetaObject *mo = reading->metaObject(); + for (int i = firstProperty; i < mo->propertyCount(); ++i) { + QByteArray name = mo->property(i).name(); + m_values.append(std::tuple<QByteArray, QByteArray> + (name, reading->property(name).toByteArray())); + } + } + //! [2] + + // 2. Read properties of the 'sensor' object + const QMetaObject *mo1 = m_sensor->metaObject(); + firstProperty = QSensorReading::staticMetaObject.propertyOffset(); + for (int i = firstProperty; i < mo1->propertyCount(); ++i) { + QByteArray name = mo1->property(i).name(); + if (ignoredProperties.contains(name)) + continue; + m_values.append(std::tuple<QByteArray, QByteArray> + (name, m_sensor->property(name).toByteArray())); + } + QObject::connect(m_sensor, &QSensor::readingChanged, + this, &SensorPropertyModel::onReadingChanged); + } + endResetModel(); + emit sensorChanged(); +} + +QSensor* SensorPropertyModel::sensor() const +{ + return m_sensor; +} + +void SensorPropertyModel::onReadingChanged() +{ + QSensorReading *reading = m_sensor->reading(); + const QMetaObject *mo = reading->metaObject(); + int firstProperty = QSensorReading::staticMetaObject.propertyOffset(); + + int valueMapIndex = 0; + for (int i = firstProperty; i < mo->propertyCount(); ++i) { + QByteArray name = mo->property(i).name(); + // Update the value and signal the change. Note: here we rely that the "reading" + // properties are first on the m_values, and in same order as after the initial + // population. This should be true as we access the static metabobject (dynamic + // property changes shouldn't impact) + m_values[valueMapIndex++] = std::tuple<QByteArray, QByteArray> + (name, reading->property(name).toByteArray()); + } + emit dataChanged(createIndex(0,1), createIndex(valueMapIndex,1), {Qt::DisplayRole}); +} + +QT_END_NAMESPACE diff --git a/examples/sensors/sensor_explorer/import/explorer.h b/examples/sensors/sensor_explorer/sensormodels.h index 2e583f81..3f5bbf29 100644 --- a/examples/sensors/sensor_explorer/import/explorer.h +++ b/examples/sensors/sensor_explorer/sensormodels.h @@ -51,38 +51,60 @@ #ifndef QSEONSOREXPLORER_H #define QSEONSOREXPLORER_H -#include <QtQml/QtQml> -#include <QtQml/QQmlListProperty> -#include "sensoritem.h" +#include <QtSensors/qsensor.h> + +#include <QtQml/qqml.h> +#include <QtCore/QAbstractListModel> +#include <QtCore/QAbstractTableModel> QT_BEGIN_NAMESPACE -class QSensorExplorer : public QObject +//! [0] +class AvailableSensorsModel: public QAbstractListModel { Q_OBJECT - Q_PROPERTY(QQmlListProperty<QSensorItem> availableSensors READ availableSensors NOTIFY availableSensorsChanged) - Q_PROPERTY(QSensorItem* selectedSensorItem READ selectedSensorItem WRITE setSelectedSensorItem NOTIFY selectedSensorItemChanged) + QML_ELEMENT +//! [0] public: - QSensorExplorer(QObject* parent = 0); - virtual ~QSensorExplorer(); + explicit AvailableSensorsModel(QObject* parent = nullptr); + int rowCount(const QModelIndex & = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role) const override; + Q_INVOKABLE QSensor* get(int index) const; private: - QQmlListProperty<QSensorItem> availableSensors(); void loadSensors(); - void setSelectedSensorItem(QSensorItem* selitem); - QSensorItem* selectedSensorItem(); + QList<QSensor*> m_availableSensors; +}; + +class SensorPropertyModel: public QAbstractTableModel +{ + Q_OBJECT + Q_PROPERTY(QSensor* sensor READ sensor WRITE setSensor NOTIFY sensorChanged) + QML_ELEMENT -Q_SIGNALS: - void availableSensorsChanged(); - void selectedSensorItemChanged(); +public: + explicit SensorPropertyModel(QObject* parent = nullptr); + + int rowCount(const QModelIndex & = QModelIndex()) const override; + int columnCount(const QModelIndex & = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role) const override; + + void setSensor(QSensor* sensor); + QSensor* sensor() const; + +signals: + void sensorChanged(); + +private slots: + void onReadingChanged(); private: - QList<QSensorItem*> _availableSensors; - QSensorItem* _selectedSensorItem; + QSensor* m_sensor = nullptr; + // m_values is used to cache sensor property values to avoid + // full metaobject iteration on every sensor reading change + QList<std::tuple<QByteArray, QByteArray>> m_values; }; QT_END_NAMESPACE -QML_DECLARE_TYPE(QSensorExplorer) - #endif // QSEONSOREXPLORER_H diff --git a/src/sensors/doc/qtsensors.qdocconf b/src/sensors/doc/qtsensors.qdocconf index 0845e707..32ff03d0 100644 --- a/src/sensors/doc/qtsensors.qdocconf +++ b/src/sensors/doc/qtsensors.qdocconf @@ -51,7 +51,7 @@ examplesinstallpath = sensors imagedirs += images -depends += qtcore qtdoc qtgui +depends += qtcore qtdoc qtgui qtquick navigation.landingpage = "Qt Sensors" navigation.cppclassespage = "Qt Sensors C++ Classes" |