Skip to content
WimaPlaner.cc.orig 32.9 KiB
Newer Older
#include "WimaPlaner.h"

#include "MissionController.h"
#include "MissionSettingsItem.h"
#include "PlanMasterController.h"
#include "QGCApplication.h"
#include "QGCLoggingCategory.h"
#include "QGCMapPolygon.h"
#include "SimpleMissionItem.h"

#include "Geometry/GeoUtilities.h"
#include "Geometry/PlanimetryCalculus.h"
#include "OptimisationTools.h"

#include "CircularSurvey.h"
#include "Geometry/WimaArea.h"
#include "Geometry/WimaAreaData.h"
#include "WimaBridge.h"

#include "StateMachine.h"
using namespace wima_planer_detail;

#include <functional>

QGC_LOGGING_CATEGORY(WimaPlanerLog, "WimaPlanerLog")

class CommandRAII {
  std::function<void(void)> f;

public:
  CommandRAII(const std::function<void(void)> &fun) : f(fun) {}
  ~CommandRAII() { f(); }
};

const char *WimaPlaner::wimaFileExtension = "wima";
const char *WimaPlaner::areaItemsName = "AreaItems";
const char *WimaPlaner::missionItemsName = "MissionItems";

WimaPlaner::WimaPlaner(QObject *parent)
    : QObject(parent), _masterController(nullptr), _missionController(nullptr),
      _currentAreaIndex(-1), _copyMAreaToSurvey(true), _copySAreaToSurvey(true),
      _corridorChanged(true), _joinedArea(this), _arrivalPathLength(0),
      _returnPathLength(0), _survey(nullptr), _surveyChanged(true),
      _synchronized(false), _nemoInterface(this),
      _stateMachine(new StateMachine), _areasMonitored(false),
      _missionControllerMonitored(false), _progressLocked(false) {

  connect(this, &WimaPlaner::currentPolygonIndexChanged, this,
          &WimaPlaner::updatePolygonInteractivity);

  // Monitoring.
  enableAreaMonitoring();
  // Mission controller not set at this point. Not enabling monitoring.

#ifndef NDEBUG
  // for debugging and testing purpose, remove if not needed anymore
  connect(&_autoLoadTimer, &QTimer::timeout, this,
          &WimaPlaner::autoLoadMission);
  _autoLoadTimer.setSingleShot(true);
  _autoLoadTimer.start(300);
#endif

  // NemoInterface
  connect(&this->_nemoInterface, &NemoInterface::progressChanged, this,
          &WimaPlaner::nemoInterfaceProgressChangedHandler);

  // StateMachine
  connect(this->_stateMachine.get(), &StateMachine::upToDateChanged, this,
          &WimaPlaner::needsUpdateChanged);
  connect(this->_stateMachine.get(), &StateMachine::surveyReadyChanged, this,
          &WimaPlaner::readyForSynchronizationChanged);
  connect(this->_stateMachine.get(), &StateMachine::surveyReadyChanged, this,
          &WimaPlaner::surveyReadyChanged);
}

WimaPlaner::~WimaPlaner() {}

PlanMasterController *WimaPlaner::masterController() {
  return _masterController;
}

MissionController *WimaPlaner::missionController() {
  return _missionController;
}

QmlObjectListModel *WimaPlaner::visualItems() { return &_visualItems; }

int WimaPlaner::currentPolygonIndex() const { return _currentAreaIndex; }

QString WimaPlaner::currentFile() const { return _currentFile; }

QStringList WimaPlaner::loadNameFilters() const {
  QStringList filters;

  filters << tr("Supported types (*.%1 *.%2)")
                 .arg(wimaFileExtension)
                 .arg(AppSettings::planFileExtension)
          << tr("All Files (*.*)");
  return filters;
}

QStringList WimaPlaner::saveNameFilters() const {
  QStringList filters;

  filters << tr("Supported types (*.%1 *.%2)")
                 .arg(wimaFileExtension)
                 .arg(AppSettings::planFileExtension);
  return filters;
}

QString WimaPlaner::fileExtension() const { return wimaFileExtension; }

QGeoCoordinate WimaPlaner::joinedAreaCenter() const {
  return _joinedArea.center();
}

NemoInterface *WimaPlaner::nemoInterface() { return &_nemoInterface; }

void WimaPlaner::setMasterController(PlanMasterController *masterC) {
  if (_masterController != masterC) {
    _masterController = masterC;
    emit masterControllerChanged();
  }
}

void WimaPlaner::setMissionController(MissionController *missionC) {
  if (_missionController != missionC) {
    disableMissionControllerMonitoring();
    _missionController = missionC;
    enableMissionControllerMonitoring();
    emit missionControllerChanged();
  }
}

void WimaPlaner::setCurrentPolygonIndex(int index) {
  if (index >= 0 && index < _visualItems.count() &&
      index != _currentAreaIndex) {
    _currentAreaIndex = index;

    emit currentPolygonIndexChanged(index);
  }
}

void WimaPlaner::setProgressLocked(bool l) {
  if (this->_progressLocked != l) {
    this->_progressLocked = l;
    emit progressLockedChanged();
    if (!this->_progressLocked) {
      if (this->_measurementArea.setProgress(this->_nemoInterface.progress()))
        this->_update();
    }
  }
}

bool WimaPlaner::synchronized() { return _synchronized; }

bool WimaPlaner::needsUpdate() { return !this->_stateMachine->upToDate(); }

bool WimaPlaner::readyForSynchronization() {
  return this->_stateMachine->surveyReady();
}

bool WimaPlaner::surveyReady() { return this->_stateMachine->surveyReady(); }

<<<<<<< HEAD
bool WimaPlaner::progressLocked() { return this->_progressLocked; }

=======
>>>>>>> 118a8e164c4fec6026709eb5bee77aa75fd3de95
void WimaPlaner::removeArea(int index) {
  if (index >= 0 && index < _visualItems.count()) {
    WimaArea *area = qobject_cast<WimaArea *>(_visualItems.removeAt(index));

    if (area == nullptr) {
      qCWarning(WimaPlanerLog)
          << "removeArea(): nullptr catched, internal error.";
      return;
    }
    area->clear();
    area->borderPolygon()->clear();

    emit visualItemsChanged();

    if (_visualItems.count() == 0) {
      // this branch is reached if all items are removed
      // to guarentee proper behavior, _currentAreaIndex must be set to a
      // invalid value, as on constructor init.
      resetAllInteractive();
      _currentAreaIndex = -1;
      return;
    }

    if (_currentAreaIndex >= _visualItems.count()) {
      setCurrentPolygonIndex(_visualItems.count() - 1);
    } else {
      updatePolygonInteractivity(_currentAreaIndex);
    }
  } else {
    qCWarning(WimaPlanerLog) << "removeArea(): Index out of bounds!";
  }
Loading
Loading full blame...