CSWorker.h 1.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#pragma once

#include <QGeoCoordinate>
#include <QList>
#include <QSharedPointer>
#include <QThread>

#include "snake.h"
#include <atomic>
#include <condition_variable>
#include <mutex>

//!
//! \brief The CSWorker class
//! \note Don't call QThread::start, QThread::quit, etc. onyl use Worker
//! members!
class CSWorker : public QThread {
  Q_OBJECT
  using Lock = std::unique_lock<std::mutex>;

public:
  using Route = QList<QGeoCoordinate>;
  using PtrRoute = QSharedPointer<Route>;

  CSWorker(QObject *parent = nullptr);
  ~CSWorker() override;

  bool calculating();

public slots:
  void update(const QList<QGeoCoordinate> &polygon,
              const QGeoCoordinate &origin, snake::Length deltaR,
              snake::Length minLength, snake::Angle deltaAlpha);

signals:
  void ready(PtrRoute pTransects);
  void calculatingChanged();

protected:
  void run() override;

private:
  mutable std::mutex _mutex;
  mutable std::condition_variable _cv;
  // Internal data
  QList<QGeoCoordinate> _polygon;
  QGeoCoordinate _origin;
  snake::Length _deltaR;
  snake::Angle _deltaAlpha;
  snake::Length _minLength;
  std::size_t _maxWaypoints;

  // State
  std::atomic_bool _calculating;
  std::atomic_bool _stop;
  std::atomic_bool _restart;
};