Browse Source

Fix headers for generated enums when QML enabled

- Fix issue with missed QmlEngine header for multi-file global enums
- Minor generated code improvements
Alexey Edelev 5 years ago
parent
commit
f31f9bd58b

+ 26 - 12
README.md

@@ -94,11 +94,31 @@ cmake --build . [--config <RELEASE|DEBUG>] -- /m:<N>
 ## Usage
 
 ### Direct usage of generator
-
+                
 ```bash
-protoc --plugin=protoc-gen-qtprotobuf=<path/to/bin>/qtprotobufgen --qtprotobuf_out=<output_dir> <protofile>.proto [--qtprotobuf_opt=out=<output_dir>]
+[QT_PROTOBUF_OPTIONS="[SINGLE|MULTI]:QML"] protoc --plugin=protoc-gen-qtprotobuf=<path/to/bin/>qtprotobufgen --qtprotobuf_out=<output_dir> [-I/extra/proto/include/path] <protofile>.proto
+```
+
+**QT_PROTOBUF_OPTIONS**
+
+For protoc command you also may specify extra options using QT_PROTOBUF_OPTIONS environment variable and colon-separated format:
+
+``` bash
+[QT_PROTOBUF_OPTIONS="[SINGLE|MULTI]:QML"] protoc --plugin=protoc-gen-qtprotobuf=<path/to/bin/>qtprotobufgen --qtprotobuf_out=<output_dir> [-I/extra/proto/include/path] <protofile>.proto
 ```
 
+Following options are supported:
+
+*SINGLE* - enables single-file generation when for each *.proto* file single pair of *.h* *.cpp* files is generated
+
+**Note:** Enabled by default. Excluded by SINGLE
+
+*MULTI* - enables multi-file generation when for each message separate pair of *.h* *.cpp*
+
+**Note:** Has higher priority than SINGLE
+
+*QML* - enables QML code generation in protobuf classes. If is set QML-related code for lists and QML registration to be generated.
+
 ### Integration with project
 
 You can integrate QtProtobuf as submodule in your project or as installed in system package. Add following line in your project CMakeLists.txt:
@@ -111,13 +131,11 @@ file(GLOB PROTO_FILES ABSOLUTE ${CMAKE_CURRENT_SOURCE_DIR}/path/to/protofile1.pr
                                ...
                                ${CMAKE_CURRENT_SOURCE_DIR}/path/to/protofileN.proto)
 # Function below generates source files for specified PROTO_FILES,
-# writes result to STATIC library target and saves its name to 
-# ${QtProtobuf_GENERATED} variable
+# and link them to the MyTarget as static library 
+add_executable(MyTarget main.cpp) # Add your target here
 qtprotobuf_generate(TARGET MyTarget
     OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated
     PROTO_FILES ${PROTO_FILES})
-add_executable(MyTarget main.cpp) # Add your target here
-target_link_libraries(MyTarget ${QtProtobuf_GENERATED})
 
 ```
 
@@ -206,11 +224,7 @@ Due to cmake restrictions it's required to specify resulting artifacts manually
 
 **Note:** multi-files generation mode is defined as deprecated by QtProtobuf team, and might have poor support in future
 
-*QML* - Enables/disables QML code generation in protobuf classes. If set to TRUE qml related code for lists and qml registration to be generated.
-
-**Outcome:**
-
-*QtProtobuf_GENERATED* - variable that will contain generated STATIC library target name
+*QML* - Enables/disables QML code generation in protobuf classes. If set to TRUE QML related code for lists and QML registration to be generated.
 
 ### qtprotobuf_link_archive
 
@@ -262,7 +276,7 @@ qtprotobuf_generate is qmake helper [test function](https://doc.qt.io/qt-5/qmake
 
 **Parameters:**
 
-*generate_qml* - Enables/disables QML code generation in protobuf classes. If set to `true` qml related code for lists and qml registration to be generated.
+*generate_qml* - Enables/disables QML code generation in protobuf classes. If set to `true` QML-related code for lists and QML registration to be generated.
 
 
 **Note:** see examples/qmakeexample for details

+ 0 - 9
src/generator/enumssourcegenerator.cpp

@@ -43,15 +43,6 @@ EnumsSourceGenerator::EnumsSourceGenerator(const google::protobuf::EnumDescripto
     ClassGeneratorBase(enumDesctiptor->full_name() + Templates::EnumClassSuffix, printer)
   , mEnumDescriptor(enumDesctiptor) {}
 
-
-void  EnumsSourceGenerator::printHeaders() {
-    mPrinter->Print("#include \"globalenums.h\"\n\n"
-                    "#include <QProtobufObject>\n\n");
-    if (GeneratorOptions::instance().hasQml()) {
-        mPrinter->Print("#include <QQmlEngine>");
-    }
-}
-
 void EnumsSourceGenerator::run() {
     printNamespaces(mNamespaces);
     printRegisterBody();

+ 0 - 1
src/generator/enumssourcegenerator.h

@@ -45,7 +45,6 @@ public:
     virtual ~EnumsSourceGenerator() = default;
 
     void run() override;
-    void printHeaders();
     void printRegisterBody();
 };
 

+ 5 - 1
src/generator/generator.cpp

@@ -34,6 +34,7 @@
 #include "clientgenerator.h"
 #include "clientsourcegenerator.h"
 #include "utils.h"
+#include "generatoroptions.h"
 
 #include <iostream>
 #include <set>
@@ -136,8 +137,11 @@ bool QtGenerator::GenerateAll(const std::vector<const FileDescriptor *> &files,
     outHeaderPrinter->Print(Templates::PreambleTemplate);
     outHeaderPrinter->Print(Templates::DefaultProtobufIncludesTemplate);
 
-    outSourcePrinter->Print({{"include", globalEnumsFilename}}, Templates::InternalIncludeTemplate);
     outSourcePrinter->Print(Templates::DisclaimerTemplate);
+    outSourcePrinter->Print({{"include", globalEnumsFilename}}, Templates::InternalIncludeTemplate);
+    if (GeneratorOptions::instance().hasQml()) {
+        outSourcePrinter->Print({{"include", "QQmlEngine"}}, Templates::ExternalIncludeTemplate);
+    }
 
     for (auto file : files) { //TODO: Each should be printed to separate file
         for(int i = 0; i < file->enum_type_count(); i++) {

+ 3 - 0
src/generator/singlefilegenerator.cpp

@@ -107,6 +107,9 @@ bool SingleFileGenerator::GenerateMessages(const ::google::protobuf::FileDescrip
         outHeaderPrinter->Print({{"include", include}}, Templates::InternalIncludeTemplate);
     }
 
+    if (GeneratorOptions::instance().hasQml()) {
+        outSourcePrinter->Print({{"include", "QQmlEngine"}}, Templates::ExternalIncludeTemplate);
+    }
     outSourcePrinter->Print({{"namespace", "QtProtobuf"}}, Templates::UsingNamespaceTemplate);