GeneratorBase.h 2.43 KB
Newer Older
1 2
#pragma once

3
#include <QJsonObject>
4
#include <QObject>
5
#include <QString>
6 7 8 9

#include <functional>
#include <memory>

10
#include "geometry/snake.h"
11

12
#include "AreaData.h"
13 14 15 16 17 18

namespace routing {

class GeneratorBase : public QObject {
  Q_OBJECT
public:
19
  using Data = AreaData *;
20 21 22 23 24 25 26 27
  using Generator = std::function<bool(snake::Transects &)>;

  explicit GeneratorBase(QObject *parent = nullptr);
  explicit GeneratorBase(Data d, QObject *parent = nullptr);
  ~GeneratorBase();

  Q_PROPERTY(QString editorQml READ editorQml CONSTANT)
  Q_PROPERTY(QString mapVisualQml READ mapVisualQml CONSTANT)
28
  Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
29

30 31
  virtual QString editorQml() = 0;
  virtual QString mapVisualQml() = 0;
32

33
  virtual bool save(QJsonObject &obj) const = 0;
34
  virtual bool load(const QJsonObject &obj, QString &errorString) = 0;
35

36 37
  QString name();
  void setName(const QString &name);
38
  virtual QString abbreviation() = 0;
39
  virtual QString type() = 0;
40 41 42 43

  virtual bool get(Generator &generator) = 0;

  Data data() const;
44
  void setData(Data d);
45

46 47
  static const char *typeKey;

48 49
signals:
  void generatorChanged();
50
  void dataChanged();
51
  void nameChanged();
52 53 54 55 56

protected:
  virtual void establishConnections();
  virtual void deleteConnections();
  Data _d;
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  QString _name;

private:
};

class GeneratorFactory {
  GeneratorFactory();
  GeneratorFactory(GeneratorFactory &other) = delete;
  static GeneratorFactory *createInstance();

public:
  typedef std::function<GeneratorBase *(QObject *)> Creator;
  ~GeneratorFactory();
  static GeneratorFactory *instance();

  bool registerGenerator(const QString &type,
                         Creator creator); // use REGISTER_GENERATOR to register
                                           // a generator befor main()
  bool registered(const QString &type);
  void unregisterGenerator(const QString &type);

  GeneratorBase *create(const QString &type, QObject *parent = nullptr);
  GeneratorBase *create(const QJsonObject &jsonGenerator, QString &errorMessage,
                        QObject *parent = nullptr);
81 82

private:
83
  std::map<QString, Creator> _creatorMap;
84 85
};

86 87 88 89 90
#define REGISTER_GENERATOR(type, creator)                                      \
  namespace {                                                                  \
  auto registered_##type =                                                     \
      GeneratorFactory::instance() -> registerGenerator(type, creator);        \
  }
91
} // namespace routing