Browse Source

Migrate to string-numeric conversion for int32/fixed32/sfixed32 types

- Enable quick plugin
- Add registration
- Update tests
Alexey Edelev 5 years ago
parent
commit
165bea1188

+ 3 - 1
src/grpc/CMakeLists.txt

@@ -6,6 +6,7 @@ set(TARGET_CONFIG ${TARGET}Config)
 set(TARGET_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${TARGET})
 set(TARGET_LIB_DIR ${CMAKE_INSTALL_LIBDIR})
 set(TARGET_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
+set(TARGET_BIN_DIR ${CMAKE_INSTALL_BINDIR})
 
 set(CMAKE_AUTOMOC ON)
 set(CMAKE_AUTORCC ON)
@@ -53,7 +54,8 @@ install(TARGETS ${TARGET}
     EXPORT ${TARGET_EXPORT}
     ARCHIVE DESTINATION ${TARGET_LIB_DIR}
     PUBLIC_HEADER DESTINATION ${TARGET_INCLUDE_DIR}
-    LIBRARY DESTINATION ${TARGET_LIB_DIR})
+    LIBRARY DESTINATION ${TARGET_LIB_DIR}
+    RUNTIME DESTINATION ${TARGET_BIN_DIR})
 
 install(EXPORT ${TARGET_EXPORT} NAMESPACE ${QTPROTOBUF_COMMON_NAMESPACE}:: FILE ${TARGET_EXPORT}.cmake DESTINATION ${TARGET_CMAKE_DIR})
 

+ 8 - 6
src/protobuf/CMakeLists.txt

@@ -6,6 +6,7 @@ set(TARGET_CONFIG ${TARGET}Config)
 set(TARGET_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${TARGET})
 set(TARGET_LIB_DIR ${CMAKE_INSTALL_LIBDIR})
 set(TARGET_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
+set(TARGET_BIN_DIR ${CMAKE_INSTALL_BINDIR})
 
 set(CMAKE_AUTOMOC ON)
 set(CMAKE_AUTORCC ON)
@@ -37,12 +38,13 @@ file(GLOB HEADERS
     qprotobufregistrationhelper.h)
 
 add_library(${TARGET} SHARED ${SOURCES})
-target_compile_definitions(${TARGET} PRIVATE QT_BUILD_PROTOBUF_LIB)
+target_compile_definitions(${TARGET} PRIVATE QT_BUILD_PROTOBUF_LIB PUBLIC QTPROTOBUF_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}
+    QTPROTOBUF_VERSION_MINOR=${PROJECT_VERSION_MINOR})
 
 add_library(${QTPROTOBUF_COMMON_NAMESPACE}::${TARGET} ALIAS ${TARGET})
 set_target_properties(${TARGET} PROPERTIES VERSION ${PROJECT_VERSION} PUBLIC_HEADER "${HEADERS}" OUTPUT_NAME ${TARGET})
-target_include_directories(${TARGET} PUBLIC 
-    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> 
+target_include_directories(${TARGET} PUBLIC
+    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
     $<INSTALL_INTERFACE:${TARGET_INCLUDE_DIR}>
 )
 target_link_libraries(${TARGET} PUBLIC Qt5::Core Qt5::Qml)
@@ -59,7 +61,8 @@ install(TARGETS ${TARGET}
     EXPORT ${TARGET_EXPORT}
     ARCHIVE DESTINATION ${TARGET_LIB_DIR}
     PUBLIC_HEADER DESTINATION ${TARGET_INCLUDE_DIR}
-    LIBRARY DESTINATION ${TARGET_LIB_DIR})
+    LIBRARY DESTINATION ${TARGET_LIB_DIR}
+    RUNTIME DESTINATION ${TARGET_BIN_DIR})
 
 install(EXPORT ${TARGET_EXPORT} NAMESPACE ${QTPROTOBUF_COMMON_NAMESPACE}:: FILE ${TARGET_EXPORT}.cmake DESTINATION ${TARGET_CMAKE_DIR})
 
@@ -78,7 +81,6 @@ install(FILES "${CMAKE_BINARY_DIR}/QtProtobufGen.cmake" DESTINATION "${TARGET_CM
 configure_file("${CMAKE_SOURCE_DIR}/cmake/ProtobufLookup.cmake" "${CMAKE_BINARY_DIR}/ProtobufLookup.cmake" COPYONLY)
 install(FILES "${CMAKE_BINARY_DIR}/ProtobufLookup.cmake" DESTINATION "${TARGET_CMAKE_DIR}")
 
-# TODO: keep quick plugin for future development, but it's useless for now
-#add_subdirectory("quick")
+add_subdirectory("quick")
 
 add_coverage_target(TARGET ${TARGET})

+ 14 - 4
src/protobuf/quick/CMakeLists.txt

@@ -8,12 +8,21 @@ set(CMAKE_AUTORCC ON)
 
 find_package(Qt5 COMPONENTS Core Qml REQUIRED)
 
+if(NOT DEFINED QT_QMAKE_EXECUTABLE)
+    find_program(QT_QMAKE_EXECUTABLE "qmake")
+    if(QT_QMAKE_EXECUTABLE STREQUAL QT_QMAKE_EXECUTABLE-NOTFOUND)
+        message(FATAL_ERROR "Could not find qmake executable")
+    endif()
+endif()
+
 execute_process(
     COMMAND ${QT_QMAKE_EXECUTABLE} -query QT_INSTALL_QML
     OUTPUT_VARIABLE TARGET_IMPORTS_DIR
     OUTPUT_STRIP_TRAILING_WHITESPACE
 )
 
+set(TARGET_IMPORTS_DIR ${TARGET_IMPORTS_DIR}/QtProtobuf)
+
 file(GLOB SOURCES
     qtprotobufquickplugin.cpp)
 
@@ -26,13 +35,14 @@ target_link_libraries(${TARGET} PRIVATE Qt5::Core Qt5::Qml ${QTPROTOBUF_COMMON_N
 set_target_properties(${TARGET} PROPERTIES
     LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/QtProtobuf")
 target_compile_definitions(${TARGET} PRIVATE QTPROTOBUFQUICK_LIB)
-target_include_directories(${TARGET} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../)
+target_include_directories(${TARGET} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../ ${Qt5Qml_PRIVATE_INCLUDE_DIRS})
 install(TARGETS ${TARGET}
-    PUBLIC_HEADER DESTINATION ${TARGET_INCLUDE_DIR}
-    LIBRARY DESTINATION ${TARGET_IMPORTS_DIR})
+    PUBLIC_HEADER DESTINATION "${TARGET_INCLUDE_DIR}"
+    RUNTIME DESTINATION "${TARGET_IMPORTS_DIR}"
+    LIBRARY DESTINATION "${TARGET_IMPORTS_DIR}")
 
 add_custom_command(TARGET ${TARGET}
     COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/qmldir $<TARGET_FILE_DIR:${TARGET}>/qmldir
     COMMENT "Copying qmldir to binary directory")
 
-install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/qmldir DESTINATION "${TARGET_IMPORTS_DIR}/QtProtobuf")
+install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/qmldir DESTINATION "${TARGET_IMPORTS_DIR}")

+ 13 - 0
src/protobuf/quick/qtprotobufquickplugin.cpp

@@ -24,9 +24,22 @@
  */
 
 #include "qtprotobufquickplugin.h"
+#include "qtprotobuftypes.h"
+#include <private/qqmlmetatype_p.h>
 #include <QDebug>
 
+template<typename T>
+static QVariant coverter(const QString &str) {
+    QVariant value = QVariant::fromValue(str.toDouble());
+    value.convert(qMetaTypeId<T>());
+    return value;
+}
+
 void QtProtobufQuickPlugin::registerTypes(const char *uri)
 {
     Q_ASSERT(uri == QLatin1String("QtProtobuf"));
+    qmlRegisterModule(uri, QTPROTOBUF_VERSION_MAJOR, QTPROTOBUF_VERSION_MINOR);
+    QQmlMetaType::registerCustomStringConverter(qMetaTypeId<QtProtobuf::int32>(), coverter<QtProtobuf::int32>);
+    QQmlMetaType::registerCustomStringConverter(qMetaTypeId<QtProtobuf::fixed32>(), coverter<QtProtobuf::fixed32>);
+    QQmlMetaType::registerCustomStringConverter(qMetaTypeId<QtProtobuf::sfixed32>(), coverter<QtProtobuf::sfixed32>);
 }

+ 2 - 0
tests/test_qml/CMakeLists.txt

@@ -15,3 +15,5 @@ add_target_qml(TARGET ${TARGET} QML_FILES ${QML_FILES})
 add_target_windeployqt(TARGET ${TARGET} QML_DIR ${CMAKE_CURRENT_SOURCE_DIR}/qml)
 
 add_test(NAME ${TARGET} COMMAND ${TARGET})
+set_tests_properties(${TARGET} PROPERTIES
+    ENVIRONMENT QML2_IMPORT_PATH=$<TARGET_FILE_DIR:protobufquickplugin>/../)

+ 6 - 0
tests/test_qml/qml/tst_simple.qml

@@ -26,6 +26,7 @@
 import QtQuick 2.12
 import QtTest 1.0
 
+import QtProtobuf 0.1
 import qtprotobufnamespace.tests 1.0
 
 TestCase {
@@ -67,6 +68,7 @@ TestCase {
     }
 
     function test_simpleintmessage() {
+        compare(int32Msg.testFieldInt == 2147483647, true, "SimpleIntMessage.testFieldInt: 2147483647: " + int32Msg.testFieldInt)
         int32Msg.testFieldInt = 0;
         compare(int32Msg.testFieldInt == 0, true, "SimpleIntMessage == 0")
         int32Msg.testFieldInt = -128;
@@ -92,6 +94,7 @@ TestCase {
     }
 
     function test_simplesintmessage() {
+        compare(sint32Msg.testFieldInt == 2147483647, true, "SimpleSIntMessage.testFieldInt: 2147483647: " + sint32Msg.testFieldInt)
         sint32Msg.testFieldInt = 0;
         compare(sint32Msg.testFieldInt, 0, "SimpleSIntMessage == 0")
         sint32Msg.testFieldInt = -128;
@@ -117,6 +120,7 @@ TestCase {
     }
 
     function test_simpleuintmessage() {
+        compare(uint32Msg.testFieldInt == 4294967295, true, "SimpleUIntMessage.testFieldInt: 4294967295: " + uint32Msg.testFieldInt)
         uint32Msg.testFieldInt = 0;
         compare(uint32Msg.testFieldInt == 0, true, "SimpleUIntMessage == 0")
         uint32Msg.testFieldInt = 127;
@@ -134,6 +138,7 @@ TestCase {
     }
 
     function test_simplefixed32message() {
+        compare(fixed32Msg.testFieldFixedInt32 == 4294967295, true, "SimpleFixedInt32Message.testFieldInt: 4294967295: " + fixed32Msg.testFieldFixedInt32)
         fixed32Msg.testFieldFixedInt32 = 0;
         compare(fixed32Msg.testFieldFixedInt32 == 0, true, "SimpleFixedInt32Message == 0")
         fixed32Msg.testFieldFixedInt32 = 127;
@@ -151,6 +156,7 @@ TestCase {
     }
 
     function test_simplesfixed32message() {
+        compare(sfixed32Msg.testFieldFixedInt32 == 2147483647, true, "SimpleSFixedInt32Message.testFieldInt: 2147483647: " + sfixed32Msg.testFieldFixedInt32)
         sfixed32Msg.testFieldFixedInt32 = 0;
         compare(sfixed32Msg.testFieldFixedInt32 == 0, true, "SimpleSFixedInt32Message == 0")
         sfixed32Msg.testFieldFixedInt32 = -128;