Skip to content

Commit

Permalink
Add QML realted conditions
Browse files Browse the repository at this point in the history
- Add conditional compilation of the QML-related code
- Throw fatal error if generating of the QML code is enabled
  but QML is not found

Fixes #239
  • Loading branch information
semlanik committed Sep 14, 2021
1 parent c618516 commit 7225e6e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
6 changes: 5 additions & 1 deletion cmake/QtProtobufGen.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ function(qtprotobuf_generate)
set(GENERATION_OPTIONS ${GENERATION_TYPE})

if(qtprotobuf_generate_QML)
message(STATUS "Enabled QML generation for ${GENERATED_TARGET_NAME}")
if(NOT TARGET Qt5::Qml)
message(FATAL_ERROR "Trying to enable QML support for ${GENERATED_TARGET_NAME}, but Qt5::Qml is not a target."
" find_package(Qt5 COMPONENTS Qml) is missing?")
endif()
message(STATUS "Enabling QML generation for ${GENERATED_TARGET_NAME}")
set(GENERATION_OPTIONS "${GENERATION_OPTIONS}:QML")
endif()

Expand Down
2 changes: 1 addition & 1 deletion src/grpc/qtgrpcglobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#endif

#ifndef Q_GRPC_IMPORT_QUICK_PLUGIN
#ifdef QT_PROTOBUF_STATIC
#if defined(QT_PROTOBUF_STATIC) && defined(QT_QML_LIB) // TODO: Check how detect this in Qt6
#include <QtPlugin>
#include <QQmlExtensionPlugin>
#define Q_GRPC_IMPORT_QUICK_PLUGIN() \
Expand Down
11 changes: 9 additions & 2 deletions src/protobuf/qprotobuflazymessagepointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
#pragma once //QProtobufLazyMessagePointer

#include "qtprotobufglobal.h"
#include <QQmlEngine>
#include <QObject>
#if defined(QT_QML_LIB) // TODO: Check how detect this in Qt6
# include <QQmlEngine>
#endif

#include <memory>
#include <type_traits>
Expand Down Expand Up @@ -96,7 +98,12 @@ class QProtobufLazyMessagePointer {//TODO: final?

private:
void checkAndRelease() const {
if (m_ptr != nullptr && QQmlEngine::objectOwnership(m_ptr.get()) == QQmlEngine::JavaScriptOwnership) {
#if defined(QT_QML_LIB)
bool qmlCheck = QQmlEngine::objectOwnership(m_ptr.get()) == QQmlEngine::JavaScriptOwnership;
#else
bool qmlCheck = true;
#endif
if (m_ptr != nullptr && qmlCheck) {
m_ptr.release();//In case if object owned by QML it's qml responsibility to clean it up
QObject::disconnect(m_destroyed);
}
Expand Down
2 changes: 1 addition & 1 deletion src/protobuf/qtprotobufglobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
* \endcode
*/
#ifndef Q_PROTOBUF_IMPORT_QUICK_PLUGIN
#ifdef QT_PROTOBUF_STATIC
#if defined(QT_PROTOBUF_STATIC) && defined(QT_QML_LIB) // TODO: Check how detect this in Qt6
#include <QtPlugin>
#include <QQmlExtensionPlugin>
#define Q_PROTOBUF_IMPORT_QUICK_PLUGIN() \
Expand Down
11 changes: 10 additions & 1 deletion src/qttypes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,19 @@ qt_protobuf_internal_add_library(ProtobufQtTypes


file(GLOB PROTO_FILE ${CMAKE_CURRENT_SOURCE_DIR}/QtProtobuf/Qt*.proto)

set(qml_enabled "")
if(TARGET Qt5::Qml)
set(qml_enabled QML)
endif()

qtprotobuf_generate(TARGET ProtobufQtTypes
OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated
PROTO_FILES ${PROTO_FILE}
QML FOLDER ${extra_type_libraries_options})
FOLDER
${qml_enabled}
${extra_type_libraries_options}
)

target_compile_definitions(ProtobufQtTypes PRIVATE QT_BUILD_PROTOBUF_QT_TYPES_LIB)

Expand Down
7 changes: 6 additions & 1 deletion src/wellknowntypes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ function(add_wellknowntype type_name)
set(lookup_dirs "${QT_PROTOBUF_SOURCE_DIR}/3rdparty/grpc/third_party/protobuf/src"
${Protobuf_INCLUDE_DIRS}
)

set(qml_enabled "")
if(TARGET Qt5::Qml)
set(qml_enabled QML)
endif()
foreach(dir ${lookup_dirs})
file(GLOB PROTO_FILE "${dir}/google/protobuf/${type_name}.proto")
if(NOT "${PROTO_FILE}" STREQUAL "")
Expand All @@ -24,7 +29,7 @@ function(add_wellknowntype type_name)
OUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated"
PROTO_FILES "${PROTO_FILE}"
PROTO_INCLUDES "-I\"${dir}\""
QML
${qml_enabled}
FOLDER
${extra_type_libraries_options}
)
Expand Down

0 comments on commit 7225e6e

Please sign in to comment.