Procházet zdrojové kódy

Complete porting of QtGrpcQuick plugin

- Fix imports of the QtGrpcQuick
- Fix warnings in qml gRPC test
Alexey Edelev před 2 roky
rodič
revize
eb2d45f61d

+ 1 - 1
CMakeLists.txt

@@ -12,7 +12,7 @@ project(QtProtobuf
 set(QT_USE_FIXED_QT_ADD_RESOURCE_BASE TRUE)
 
 find_package(Qt6 ${PROJECT_VERSION} CONFIG REQUIRED COMPONENTS BuildInternals Core Network)
-find_package(Qt6 ${PROJECT_VERSION} CONFIG OPTIONAL_COMPONENTS Quick Qml Test QuickTest)
+find_package(Qt6 ${PROJECT_VERSION} CONFIG OPTIONAL_COMPONENTS Quick Qml QmlIntegration Test QuickTest)
 
 include("${CMAKE_CURRENT_LIST_DIR}/src/protobuf/cmake/Qt6ProtobufMacro.cmake")
 

+ 5 - 1
src/grpcquick/CMakeLists.txt

@@ -2,16 +2,20 @@ qt_internal_add_qml_module(GrpcQuick
     URI "QtGrpc"
     VERSION "${PROJECT_VERSION}"
     CLASS_NAME QtGrpcQuickPlugin
+    PLUGIN_TARGET qtgrpcquick
     NO_GENERATE_PLUGIN_SOURCE
     NO_PLUGIN_OPTIONAL
+    DEPENDENCIES QtQuick
     SOURCES
         qquickgrpcstream.cpp qquickgrpcstream_p.h
-        qtgrpcquickplugin.cpp qtgrpcquickplugin_p.h
         qtgrpcquicklogging.cpp qtgrpcquicklogging_p.h
         qtgrpcquick_global.h
     LIBRARIES
+        Qt::QmlIntegration
         Qt::Qml
         Qt::Quick
         Qt::Protobuf
         Qt::Grpc
 )
+target_sources(qtgrpcquick PRIVATE grpcquick_plugin.cpp)
+target_link_libraries(qtgrpcquick PRIVATE Qt::Grpc)

+ 18 - 8
src/grpcquick/qtgrpcquickplugin_p.h → src/grpcquick/grpcquick_plugin.cpp

@@ -1,7 +1,7 @@
 /*
  * MIT License
  *
- * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
+ * Copyright (c) 2022 Alexey Edelev <semlanik@gmail.com>
  *
  * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
  *
@@ -23,21 +23,31 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
-#pragma once
-
+#include <QtQml/qqmlcomponent.h>
 #include <QtQml/qqmlextensionplugin.h>
+#include <QtGrpc/qgrpcstatus.h>
+
 #include "qtgrpcquick_global.h"
 
 QT_BEGIN_NAMESPACE
 
-class QT_GRPC_QUICK_SHARED_EXPORT QtGrpcQuickPlugin : public QQmlExtensionPlugin
+extern void qml_register_types_QtGrpc();
+Q_GHS_KEEP_REFERENCE(qml_register_types_QtGrpc);
+
+class QT_GRPC_QUICK_SHARED_EXPORT QtGrpcQuickPlugin : public QQmlEngineExtensionPlugin
 {
     Q_OBJECT
-    Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)
+    Q_PLUGIN_METADATA(IID QQmlEngineExtensionInterface_iid)
+
 public:
-    QtGrpcQuickPlugin();
-    ~QtGrpcQuickPlugin() = default;
-    void registerTypes(const char *) override;
+    QtGrpcQuickPlugin(QObject *parent = nullptr) : QQmlEngineExtensionPlugin(parent)
+    {
+        volatile auto registration = &qml_register_types_QtGrpc;
+        qmlRegisterUncreatableType<QGrpcStatus>("QtGrpc", QT_VERSION_MAJOR, QT_VERSION_MINOR, "GrpcStatus", QStringLiteral("GrpcStatus can only be instantiated in C++ context."));
+        Q_UNUSED(registration);
+    }
 };
 
 QT_END_NAMESPACE
+
+#include "grpcquick_plugin.moc"

+ 1 - 1
src/grpcquick/qquickgrpcstream.cpp

@@ -153,7 +153,7 @@ bool QQuickGrpcStream::stream()
 
     QGrpcStreamShared stream = nullptr;
     bool ok = method.invoke(m_client, Qt::DirectConnection,
-                                      QGenericReturnArgument("QtProtobuf::QGrpcStreamShared", static_cast<void *>(&stream)),
+                                      QGenericReturnArgument("QGrpcStreamShared", static_cast<void *>(&stream)),
                                       QGenericArgument(method.parameterTypes().at(0).data(), static_cast<const void *>(&argument)),
                                       QGenericArgument(method.parameterTypes().at(1).data(), static_cast<const void *>(&m_returnValue)));
     if (!ok || stream == nullptr) {

+ 1 - 1
src/grpcquick/qquickgrpcstream_p.h

@@ -28,6 +28,7 @@
 #include <QtCore/qobject.h>
 #include <QtGrpc/qabstractgrpcclient.h>
 #include <QtGrpc/qgrpcstatus.h>
+#include <QtQmlIntegration/qqmlintegration.h>
 
 #include <memory>
 /*!
@@ -102,7 +103,6 @@ class QQuickGrpcStream : public QObject
     Q_PROPERTY(QString method READ method WRITE setMethod NOTIFY methodChanged)
     Q_PROPERTY(QObject *argument READ argument WRITE setArgument NOTIFY argumentChanged)
     Q_PROPERTY(QObject *returnValue READ returnValue NOTIFY returnValueChanged)
-    QML_ELEMENT
     QML_NAMED_ELEMENT(GrpcStream)
 public:
     QQuickGrpcStream(QObject *parent = nullptr);

+ 0 - 45
src/grpcquick/qtgrpcquickplugin.cpp

@@ -1,45 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
- *
- * This file is part of qtprotobuf project https://git.semlanik.org/semlanik/qtprotobuf
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this
- * software and associated documentation files (the "Software"), to deal in the Software
- * without restriction, including without limitation the rights to use, copy, modify,
- * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
- * to permit persons to whom the Software is furnished to do so, subject to the following
- * conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies
- * or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
- * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
-
-#include "qtgrpcquickplugin_p.h"
-
-#include "qquickgrpcstream_p.h"
-
-#include <QDebug>
-#include <QQmlEngine>
-#include <QGrpcStatus>
-
-using namespace QtProtobuf;
-
-QtGrpcQuickPlugin::QtGrpcQuickPlugin()
-{
-    int a = 0;
-}
-void QtGrpcQuickPlugin::registerTypes(const char *uri)
-{
-    // @uri QtGrpc
-    Q_ASSERT(QLatin1String(uri) == QLatin1String("QtGrpc"));
-    qmlRegisterUncreatableType<QGrpcStatus>("QtGrpc", QT_VERSION_MAJOR, QT_VERSION_MINOR, "GrpcStatus", QStringLiteral("GrpcStatus only could be recevied from gRPC calls"));
-}

+ 8 - 8
tests/auto/test_grpc/qml/qml/tst_grpcclient.qml

@@ -44,7 +44,7 @@ TestCase {
 
     QtObject {
         id: returnMsg
-        property string ret: serverStream.returnValue.testFieldString
+        property string ret: serverStream.returnValue ? serverStream.returnValue.testFieldString : ""
     }
 
     GrpcStream {
@@ -56,11 +56,11 @@ TestCase {
         client: TestServiceClient
         method: "testMethodServerStream"
         argument: stringMsg
-        onMessageReceived: {
+        onMessageReceived: function(value) {
             ++updateCount;
             ok = ok && value.testFieldString === ("test_serverStream" + updateCount) && returnMsg.ret == ("test_serverStream" + updateCount)
         }
-        onError: {
+        onError: function(status) {
             console.log("Stream error: " + status.code + " " + status.message)
             ok = false;
         }
@@ -75,14 +75,14 @@ TestCase {
         client: TestServiceClient
         method: "testMethodServerStream"
         argument: stringMsg
-        onMessageReceived: {
+        onMessageReceived: function(value) {
             ++updateCount;
             ok = ok && value.testFieldString === "test_serverStreamCancel" + updateCount
             if (updateCount === 3) {
                 serverStreamCancel.enabled = false;
             }
         }
-        onError: {
+        onError: function(status) {
             console.log("Stream error: " + status.code + " " + status.message)
             ok = false;
         }
@@ -98,7 +98,7 @@ TestCase {
         onMessageReceived: {
             ok = false;
         }
-        onError: {
+        onError: function(status) {
             ok = status.code === GrpcStatus.Unimplemented;
         }
     }
@@ -114,11 +114,11 @@ TestCase {
                 client: TestServiceClient
                 method: "testMethodServerStream"
                 argument: stringMsg
-                onMessageReceived: {
+                onMessageReceived: function(value) {
                     ++streamLoader.updateCount;
                     streamLoader.ok = ok && value.testFieldString === ("test_1loader" + streamLoader.updateCount)
                 }
-                onError: {
+                onError: function(status) {
                     console.log("Stream error: " + status.code + " " + status.message)
                     streamLoader.ok = false;
                 }