Selaa lähdekoodia

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 vuotta sitten
vanhempi
commit
b34bcb6dd2

+ 21 - 1
README.md

@@ -82,9 +82,29 @@ cmake --build . [--config <RELEASE|DEBUG>] -- /m:<N>
 ### 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:

+ 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);