diff options
author | Tamas Martinec <tamas.martinec@symbio.com> | 2021-06-10 13:14:04 +0300 |
---|---|---|
committer | Alexandru Croitor <alexandru.croitor@qt.io> | 2021-08-18 15:15:02 +0200 |
commit | a063b9f61ddea7403d36a5288ff6f8dc0d95ddfb (patch) | |
tree | f8b116d977bf3774c5c9c743a253106879f67a19 /examples/sensors/grue/plugin | |
parent | 63fe6819c79c835adf652f8dd99fda54f6d35cdc (diff) |
QtSensors: Fix and simplify the Grue example
Refactoring to use QML cpp class registration instead of a QML plugin.
Pick-to: 6.2
Task-number: QTBUG-92514
Change-Id: I7637232d27575522e83ebdbc5c60cdb5663d2cd0
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
Diffstat (limited to 'examples/sensors/grue/plugin')
-rw-r--r-- | examples/sensors/grue/plugin/CMakeLists.txt | 40 | ||||
-rw-r--r-- | examples/sensors/grue/plugin/gruesensor.cpp | 159 | ||||
-rw-r--r-- | examples/sensors/grue/plugin/gruesensor.h | 90 | ||||
-rw-r--r-- | examples/sensors/grue/plugin/gruesensor_p.h | 76 | ||||
-rw-r--r-- | examples/sensors/grue/plugin/main.cpp | 1 | ||||
-rw-r--r-- | examples/sensors/grue/plugin/plugin.pro | 17 |
6 files changed, 358 insertions, 25 deletions
diff --git a/examples/sensors/grue/plugin/CMakeLists.txt b/examples/sensors/grue/plugin/CMakeLists.txt index d55cc2dc..e8a016e6 100644 --- a/examples/sensors/grue/plugin/CMakeLists.txt +++ b/examples/sensors/grue/plugin/CMakeLists.txt @@ -18,35 +18,47 @@ set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/sensors/grue/sensors") find_package(Qt6 COMPONENTS Core) find_package(Qt6 COMPONENTS Sensors) -qt_add_plugin(qtsensors_grue) +qt_add_plugin(qtsensors_grue + PLUGIN_TYPE sensors +) + target_sources(qtsensors_grue PRIVATE gruesensorimpl.cpp gruesensorimpl.h + gruesensor.cpp gruesensor.h gruesensor_p.h main.cpp ) + set_target_properties(qtsensors_grue PROPERTIES WIN32_EXECUTABLE TRUE MACOSX_BUNDLE TRUE ) -target_include_directories(qtsensors_grue PUBLIC - ../lib -) + +if(WIN32) + set_target_properties(qtsensors_grue PROPERTIES + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/../sensors + ) +endif() + +if(APPLE AND QT6_IS_SHARED_LIBS_BUILD) + set_target_properties(qtsensors_grue PROPERTIES + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/../grue_app.app/Contents/MacOS/../sensors + ) +endif() target_link_libraries(qtsensors_grue PUBLIC Qt::Core Qt::Sensors - gruesensor ) -if(MACOS) - target_link_libraries(qtsensors_grue PUBLIC - # Remove: L${CMAKE_CURRENT_BINARY_DIR}/../grue_app.app/Contents/Frameworks - ) -endif() +if(ANDROID) + add_custom_target(gruesensorplugin_android_copy ALL + COMMAND "${CMAKE_COMMAND}" + -E copy_if_different + "$<TARGET_FILE:qtsensors_grue>" + "${CMAKE_CURRENT_BINARY_DIR}/../android-build/libs/${CMAKE_ANDROID_ARCH_ABI}/$<TARGET_FILE_NAME:qtsensors_grue>" + COMMENT "Copying sensor plugin lib to grue_app android libs folder.") -if(NOT MACOS) - target_link_libraries(qtsensors_grue PUBLIC - # Remove: L${CMAKE_CURRENT_BINARY_DIR}/.. - ) + add_dependencies(gruesensorplugin_android_copy qtsensors_grue) endif() install(TARGETS qtsensors_grue diff --git a/examples/sensors/grue/plugin/gruesensor.cpp b/examples/sensors/grue/plugin/gruesensor.cpp new file mode 100644 index 00000000..23eb0ec4 --- /dev/null +++ b/examples/sensors/grue/plugin/gruesensor.cpp @@ -0,0 +1,159 @@ +/**************************************************************************** +** +** 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 "gruesensor.h" +#include "gruesensor_p.h" + +IMPLEMENT_READING(GrueSensorReading) + +/* + \omit + \class GrueSensorReading + + \brief The GrueSensorReading class holds readings from the Grue sensor. + + The Grue Sensor informs you of your chance of being eaten by a Grue. + + Grues love the dark so as long as your surroundings are relatively light + you are safe. However the more time you spend in the dark, the higher + your chances are of being eaten by a Grue. +*/ + +/* + \property GrueSensorReading::chanceOfBeingEaten + \brief holds your chance of being eaten. + + The value is the probability (from 0 to 100) that a Grue will eat you. + A probability of 100 means you are currently being eaten. The darker + it is, the more likely you are to be eaten by a Grue. The longer you + stay in a dark area, the more likely you are to be eaten by a Grue. + If you are in a lit room, the probability will be 0 as Grues fear light. + \endomit +*/ + +int GrueSensorReading::chanceOfBeingEaten() const +{ + return d->chanceOfBeingEaten; +} + +void GrueSensorReading::setChanceOfBeingEaten(int chanceOfBeingEaten) +{ + d->chanceOfBeingEaten = chanceOfBeingEaten; +} + +// ===================================================================== + +// begin generated code + +/* + \omit + \class GrueFilter + + \brief The GrueFilter class is a convenience wrapper around QSensorFilter. + + The only difference is that the filter() method features a pointer to GrueSensorReading + instead of QSensorReading. + \endomit +*/ + +/* + \omit + \fn GrueFilter::filter(GrueSensorReading *reading) + + Called when \a reading changes. Returns false to prevent the reading from propagating. + + \sa QSensorFilter::filter() + \endomit +*/ + +char const * const GrueSensor::sensorType("GrueSensor"); + +/* + \omit + \class GrueSensor + + \brief The GrueSensor class is a convenience wrapper around QSensor. + + The only behavioural difference is that this class sets the type properly. + + This class also features a reading() function that returns a GrueSensorReading instead of a QSensorReading. + + For details about how the sensor works, see \l GrueSensorReading. + + \sa GrueSensorReading + \endomit +*/ + +/* + \omit + \fn GrueSensor::GrueSensor(QObject *parent) + + Construct the sensor as a child of \a parent. + \endomit +*/ + +/* + \fn GrueSensor::~GrueSensor() + + Destroy the sensor. Stops the sensor if it has not already been stopped. +*/ + +/* + \omit + \fn GrueSensor::reading() const + + Returns the reading class for this sensor. + + \sa QSensor::reading() + \endomit +*/ +// end generated code + +#include "moc_gruesensor.cpp" diff --git a/examples/sensors/grue/plugin/gruesensor.h b/examples/sensors/grue/plugin/gruesensor.h new file mode 100644 index 00000000..c7ec96e3 --- /dev/null +++ b/examples/sensors/grue/plugin/gruesensor.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** 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 GRUESENSOR_H +#define GRUESENSOR_H + +#include <qsensor.h> + +class GrueSensorReadingPrivate; + +class GrueSensorReading : public QSensorReading +{ + Q_OBJECT + Q_PROPERTY(int chanceOfBeingEaten READ chanceOfBeingEaten WRITE setChanceOfBeingEaten) + DECLARE_READING(GrueSensorReading) +public: + int chanceOfBeingEaten() const; + void setChanceOfBeingEaten(int chanceOfBeingEaten); +}; + +// begin generated code + +class GrueFilter : public QSensorFilter +{ +public: + virtual bool filter(GrueSensorReading *reading) = 0; +private: + bool filter(QSensorReading *reading) override { return filter(static_cast<GrueSensorReading*>(reading)); } +}; + +class GrueSensor : public QSensor +{ + Q_OBJECT + Q_PROPERTY(GrueSensorReading* reading READ reading) +public: + explicit GrueSensor(QObject *parent = 0) : QSensor(GrueSensor::sensorType, parent) {} + virtual ~GrueSensor() {} + GrueSensorReading *reading() const { return static_cast<GrueSensorReading*>(QSensor::reading()); } + static char const * const sensorType; +}; +// end generated code + +#endif diff --git a/examples/sensors/grue/plugin/gruesensor_p.h b/examples/sensors/grue/plugin/gruesensor_p.h new file mode 100644 index 00000000..0216d06b --- /dev/null +++ b/examples/sensors/grue/plugin/gruesensor_p.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** 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 GRUESENSOR_P_H +#define GRUESENSOR_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +class GrueSensorReadingPrivate +{ +public: + GrueSensorReadingPrivate() + : chanceOfBeingEaten(-1) + { + } + + int chanceOfBeingEaten; +}; + +#endif diff --git a/examples/sensors/grue/plugin/main.cpp b/examples/sensors/grue/plugin/main.cpp index 6bf86450..a2900cbb 100644 --- a/examples/sensors/grue/plugin/main.cpp +++ b/examples/sensors/grue/plugin/main.cpp @@ -63,7 +63,6 @@ class GrueSensorPlugin : public QObject, public QSensorPluginInterface, public Q public: void registerSensors() override { - qDebug() << "loaded the grue plugin"; } void sensorsChanged() override diff --git a/examples/sensors/grue/plugin/plugin.pro b/examples/sensors/grue/plugin/plugin.pro index e198140d..aa03d6c9 100644 --- a/examples/sensors/grue/plugin/plugin.pro +++ b/examples/sensors/grue/plugin/plugin.pro @@ -1,23 +1,20 @@ TEMPLATE = lib CONFIG += plugin -TARGET = $$qtLibraryTarget(qtsensors_grue) +TARGET = $$qtLibraryTarget(plugins_sensors_qtsensors_grue) PLUGIN_TYPE = sensors QT = core sensors -macos: DESTDIR = ../grue_app.app/Contents/MacOS/$$PLUGIN_TYPE +macos:!qtConfig(static): DESTDIR = ../grue_app.app/Contents/MacOS/$$PLUGIN_TYPE else: DESTDIR = ../$$PLUGIN_TYPE -include(../lib/lib.pri) - -# Shared gruesensor library will be installed in parent directory. -# Define rpath so that this plugin will know where to look for it. -unix:!mac: QMAKE_LFLAGS += -Wl,-rpath,\\\$\$ORIGIN/.. - -HEADERS += gruesensorimpl.h +HEADERS += gruesensorimpl.h \ + gruesensor.h \ + gruesensor_p.h SOURCES += gruesensorimpl.cpp \ - main.cpp + main.cpp \ + gruesensor.cpp # Install the plugin under Grue example directory |