GeneratorBase.h 2.49 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() const = 0;
  virtual QString mapVisualQml() const = 0;
32

33 34 35
  virtual bool save(QJsonObject &obj) const; // must be called if overridden
  virtual bool load(const QJsonObject &obj,
                    QString &errorString); // must be called if overridden
36

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

  virtual bool get(Generator &generator) = 0;

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

47
  static const char *typeKey;
48
  static const char *nameKey;
49

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

protected:
  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
#define REGISTER_GENERATOR(type, creator)                                      \
  namespace {                                                                  \
88
  auto registered =                                                            \
89 90
      GeneratorFactory::instance() -> registerGenerator(type, creator);        \
  }
91
} // namespace routing