Marry C enums, Qt's metaobject system and QML
authorunc0rr
Sat, 06 Jul 2019 00:31:54 +0200
changeset 15222 b32c52c76977
parent 15221 387345a14b3f
child 15223 b2c086629fb8
Marry C enums, Qt's metaobject system and QML
qmlfrontend/Page1.qml
qmlfrontend/engine_instance.cpp
qmlfrontend/engine_instance.h
qmlfrontend/engine_interface.h
qmlfrontend/main.cpp
--- a/qmlfrontend/Page1.qml	Fri Jul 05 23:40:03 2019 +0300
+++ b/qmlfrontend/Page1.qml	Sat Jul 06 00:31:54 2019 +0200
@@ -57,13 +57,11 @@
 
   Keys.onPressed: {
     if (event.key === Qt.Key_Enter)
-      gameView.engineInstance.longEvent(EngineInstance.Attack,
-                                        EngineInstance.Set)
+      gameView.engineInstance.longEvent(Engine.Attack, Engine.Set)
   }
 
   Keys.onReleased: {
     if (event.key === Qt.Key_Enter)
-      gameView.engineInstance.longEvent(EngineInstance.Attack,
-                                        EngineInstance.Unset)
+      gameView.engineInstance.longEvent(Engine.Attack, Engine.Unset)
   }
 }
--- a/qmlfrontend/engine_instance.cpp	Fri Jul 05 23:40:03 2019 +0300
+++ b/qmlfrontend/engine_instance.cpp	Sat Jul 06 00:31:54 2019 +0200
@@ -87,16 +87,17 @@
   move_camera(m_instance, delta.x(), delta.y());
 }
 
-void EngineInstance::simpleEvent(SimpleEventType event_type) {
+void EngineInstance::simpleEvent(Engine::SimpleEventType event_type) {
   simple_event(m_instance, event_type);
 }
 
-void EngineInstance::longEvent(LongEventType event_type, LongEventState state) {
+void EngineInstance::longEvent(Engine::LongEventType event_type,
+                               Engine::LongEventState state) {
   long_event(m_instance, event_type, state);
 }
 
-void EngineInstance::positionedEvent(PositionedEventType event_type, qint32 x,
-                                     qint32 y) {
+void EngineInstance::positionedEvent(Engine::PositionedEventType event_type,
+                                     qint32 x, qint32 y) {
   positioned_event(m_instance, event_type, x, y);
 }
 
--- a/qmlfrontend/engine_instance.h	Fri Jul 05 23:40:03 2019 +0300
+++ b/qmlfrontend/engine_instance.h	Sat Jul 06 00:31:54 2019 +0200
@@ -12,15 +12,6 @@
   Q_OBJECT
 
  public:
-  using SimpleEventType = Engine::SimpleEventType;
-  Q_ENUMS(SimpleEventType)
-  using LongEventType = Engine::LongEventType;
-  Q_ENUMS(LongEventType)
-  using LongEventState = Engine::LongEventState;
-  Q_ENUMS(LongEventState)
-  using PositionedEventType = Engine::PositionedEventType;
-  Q_ENUMS(PositionedEventType)
-
   explicit EngineInstance(const QString& libraryPath,
                           QObject* parent = nullptr);
   ~EngineInstance();
@@ -40,9 +31,11 @@
  public slots:
   void advance(quint32 ticks);
   void moveCamera(const QPoint& delta);
-  void simpleEvent(SimpleEventType event_type);
-  void longEvent(LongEventType event_type, LongEventState state);
-  void positionedEvent(PositionedEventType event_type, qint32 x, qint32 y);
+  void simpleEvent(Engine::SimpleEventType event_type);
+  void longEvent(Engine::LongEventType event_type,
+                 Engine::LongEventState state);
+  void positionedEvent(Engine::PositionedEventType event_type, qint32 x,
+                       qint32 y);
 
  private:
   Engine::EngineInstance* m_instance;
@@ -65,9 +58,4 @@
   bool m_isValid;
 };
 
-Q_DECLARE_METATYPE(EngineInstance::SimpleEventType)
-Q_DECLARE_METATYPE(EngineInstance::LongEventType)
-Q_DECLARE_METATYPE(EngineInstance::LongEventState)
-Q_DECLARE_METATYPE(EngineInstance::PositionedEventType)
-
 #endif  // ENGINEINSTANCE_H
--- a/qmlfrontend/engine_interface.h	Fri Jul 05 23:40:03 2019 +0300
+++ b/qmlfrontend/engine_interface.h	Sat Jul 06 00:31:54 2019 +0200
@@ -6,10 +6,23 @@
 
 #ifdef __cplusplus
 #define ENUM_CLASS enum
+
+#ifndef Q_NAMESPACE
+#define Q_NAMESPACE
+#endif
+
+#ifndef Q_ENUM_NS
+#define Q_ENUM_NS(x)
+#endif
+
+#ifndef Q_DECLARE_METATYPE
+#define Q_DECLARE_METATYPE(x)
+#endif
+
 namespace Engine {
 extern "C" {
 #else
-#define ENUM_CLASS enum
+#define ENUM_CLASS enum class
 #endif
 
 typedef struct _EngineInstance EngineInstance;
@@ -68,9 +81,22 @@
 typedef void positioned_event_t(EngineInstance* engine_state,
                                 PositionedEventType event_type, int32_t x,
                                 int32_t y);
+}  // extern "C"
+
 #ifdef __cplusplus
-}
-};
+Q_NAMESPACE
+
+Q_ENUM_NS(SimpleEventType)
+Q_ENUM_NS(LongEventType)
+Q_ENUM_NS(LongEventState)
+Q_ENUM_NS(PositionedEventType)
+
+};  // namespace
+
+Q_DECLARE_METATYPE(Engine::SimpleEventType)
+Q_DECLARE_METATYPE(Engine::LongEventType)
+Q_DECLARE_METATYPE(Engine::LongEventState)
+Q_DECLARE_METATYPE(Engine::PositionedEventType)
 #endif
 
 #endif  // ENGINE_H
--- a/qmlfrontend/main.cpp	Fri Jul 05 23:40:03 2019 +0300
+++ b/qmlfrontend/main.cpp	Sat Jul 06 00:31:54 2019 +0200
@@ -23,10 +23,10 @@
 
   QQmlApplicationEngine engine;
 
-  qRegisterMetaType<EngineInstance::SimpleEventType>();
-  qRegisterMetaType<EngineInstance::LongEventType>();
-  qRegisterMetaType<EngineInstance::LongEventState>();
-  qRegisterMetaType<EngineInstance::PositionedEventType>();
+  qRegisterMetaType<Engine::SimpleEventType>();
+  qRegisterMetaType<Engine::LongEventType>();
+  qRegisterMetaType<Engine::LongEventState>();
+  qRegisterMetaType<Engine::PositionedEventType>();
 
   qmlRegisterSingletonType<PreviewAcceptor>(
       "Hedgewars.Engine", 1, 0, "PreviewAcceptor",
@@ -38,6 +38,9 @@
                                              "EngineInstance",
                                              "Create by HWEngine run methods");
 
+  qmlRegisterUncreatableMetaObject(Engine::staticMetaObject, "Hedgewars.Engine",
+                                   1, 0, "Engine", "Namespace: only enums");
+
   engine.load(QUrl(QLatin1String("qrc:/main.qml")));
   if (engine.rootObjects().isEmpty()) return -1;