Utils.h 6.89 KB
Newer Older
1 2 3
#pragma once

#include "QmlObjectListModel.h"
4
#include "SimpleMissionItem.h"
5 6 7

#include <QVariant>

8
class PlanMasterController;
9 10
class QGeoCoordinate;

11 12 13
namespace WaypointManager {
namespace Utils {

14 15 16
template <class CoordinateType>
CoordinateType getCoordinate(const SimpleMissionItem *item) {
  return item->coordinate();
17 18
}

19 20 21
template <> QVariant getCoordinate(const SimpleMissionItem *item) {
  return QVariant::fromValue(item->coordinate());
}
22

23 24
/// extracts the coordinates stored in missionItems (list of MissionItems) and
/// stores them in coordinateList.
25
template <class CoordinateType, template <class, class...> class ContainerType>
26
bool extract(const QmlObjectListModel &missionItems,
27
             ContainerType<CoordinateType> &coordinateList,
28 29 30 31 32 33 34 35
             std::size_t startIndex, std::size_t endIndex) {

  if (startIndex < std::size_t(missionItems.count()) &&
      endIndex < std::size_t(missionItems.count()) &&
      missionItems.count() > 0) {
    if (startIndex > endIndex) {
      if (!extract(missionItems, coordinateList, startIndex,
                   std::size_t(missionItems.count() - 1)) /*recursion*/)
36
        return false;
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
      if (!extract(missionItems, coordinateList, 0, endIndex) /*recursion*/)
        return false;
    } else {
      for (std::size_t i = startIndex; i <= endIndex; ++i) {
        const QObject *obj = missionItems[int(i)];
        auto *mItem = qobject_cast<const SimpleMissionItem *>(obj);

        if (mItem == nullptr) {
          coordinateList.clear();
          return false;
        }
        coordinateList.append(getCoordinate<CoordinateType>(mItem));
      }
    }
  } else
    return false;
53

54
  return true;
55 56
}

57 58
/// extracts the coordinates stored in missionItems (list of MissionItems) and
/// stores them in coordinateList.
59
template <class CoordinateType, template <class, class...> class ContainerType>
60
bool extract(const QmlObjectListModel &missionItems,
61
             ContainerType<CoordinateType> &coordinateList) {
62

63 64 65
  return extract(missionItems, coordinateList, std::size_t(0),
                 std::size_t(missionItems.count()));
}
66

67 68
/// extracts the coordinates stored in missionItems (list of MissionItems) and
/// stores them in coordinateList.
69
template <class CoordinateType, template <class, class...> class ContainerType>
70 71 72 73 74 75 76 77 78
bool extract(const ContainerType<CoordinateType> &source,
             ContainerType<CoordinateType> &destination, std::size_t startIndex,
             std::size_t endIndex) {

  if (startIndex < std::size_t(source.size()) &&
      endIndex < std::size_t(source.size()) && source.size() > 0) {
    if (startIndex > endIndex) {
      if (!extract(source, destination, startIndex,
                   std::size_t(long(source.size()) - 1)) /*recursion*/)
79
        return false;
80 81 82 83 84 85 86 87 88 89 90
      if (!extract(source, destination, 0, endIndex) /*recursion*/)
        return false;
    } else {
      for (std::size_t i = startIndex; i <= endIndex; ++i) {
        destination.append(source[i]);
      }
    }
  } else
    return false;

  return true;
91 92
}

93 94 95 96 97
//!
//! \brief Makes the SimpleMissionItem \p item a takeoff command.
//! \param item SimpleMissionItem
//! \return Returns true if successfull, false either.
//!
98 99
bool makeTakeOffCmd(SimpleMissionItem *item,
                    PlanMasterController *masterController);
100

101 102 103 104 105
//!
//! \brief Makes the SimpleMissionItem \p item a land command.
//! \param item SimpleMissionItem
//! \return Returns true if successfull, false either.
//!
106 107
bool makeLandCmd(SimpleMissionItem *item,
                 PlanMasterController *masterController);
108

109 110 111 112 113 114 115
//!
//! \brief Makes the SimpleMissionItem \p item a MAV_CMD_DO_CHANGE_SPEED item.
//! \param item SimpleMissionItem.
//! \param speed Speed (must be greater zero).
//!
void makeSpeedCmd(SimpleMissionItem *item, double speed);

116 117 118 119 120 121 122
//!
//! \brief Initializes the list \p list.
//!
//! Adds a MissionSettingsItem to \p list.
//! \param list List of VisualMissionItems.
//! \param flyView Fly- or planview.
void initialize(QmlObjectListModel &list,
123
                PlanMasterController *masterController, bool flyView = true);
124

125 126
//! @brief Creates (from QGeoCoordinate) and inserts a SimpleMissionItem at the
//! given index to list. \param coordinate Coordinate of the SimpleMissionItem.
127 128 129 130
//! \param index Index to insert SimpleMissionItem.
//! \param list List of SimpleMissionItems.
//! \param flyView Fly- or planview.
//! \param parent Parent of the SimpleMissionItem (Set to &list if nullptr).
131 132 133
//! \param doUpdates Neccessary update are performed to the list \p list if set
//! to true. Set this to true for the last item only, if inserting multiple
//! items. \return Returns true on success, false either.
134 135
//!
//! @note The list must be initialized. (\see \fn initialize)
136 137 138 139 140
//! @note This function doesn't establish any signal-slot connections (\see \fn
//! MissionController::_initVisualItem).
//! @note This function doesn't set the MissionFlightStatus_t (\see \fn
//! VisualMissionItem::setMissionFlightStatus).
bool insertMissionItem(const QGeoCoordinate &coordinate, long index,
141
                       QmlObjectListModel &list,
142 143 144
                       PlanMasterController *masterController,
                       bool flyView = true, QObject *parent = nullptr,
                       bool doUpdates = true);
145 146 147
//!
//! \brief Performs a list update.
//!
148 149 150
//! Updates the home position of the MissionSettingsItem, the parent child
//! hirarchy and the sequence numbers. \param list List containing
//! VisualMissionItems. \return Returns true if no errors occured, false either.
151 152
bool updateList(QmlObjectListModel &list);

153 154
//! @brief Updates the sequence numbers of the VisualMissionItems inside \p
//! list.
155 156
//!
//! \param list List containing VisualMissionItems.
157 158
//! \param firstSeqNumber The sequence number of the fisrt list entry (defaul =
//! 0). \return Returns true if successfull, false either.
159
//!
160 161
//! \note If the list \p list contains only one VisualMissionItem it's sequence
//! number is set to 0.
162 163
bool updateSequenceNumbers(QmlObjectListModel &list, size_t firstSeqNumber = 0);

164 165
//! @brief Update the parent child item hierarchy of the VisualMissionItems
//! inside \p list.
166 167 168 169 170 171 172
//!
//! \param list List containing VisualMissionItems.
//! \return Returns true if successfull, false either.
//!
//! \note Returns true on success, false either.
bool updateHirarchy(QmlObjectListModel &list);

173 174
//! @brief Updates the home position to the first valid coordinate of the
//! VisualMissionItems inside \p list.
175 176 177 178
//!
//! \param list List containing VisualMissionItems.
//! \return Returns true if the home position was updated, false either.
bool updateHomePosition(QmlObjectListModel &list);
179

180 181 182 183
//!
//! \brief Prints the \p list.
//! \param list List containing VisualMissionItems.
void printList(QmlObjectListModel &list);
184 185

namespace detail {
186 187 188
size_t nextSequenceNumber(QmlObjectListModel &list);
bool previousAltitude(QmlObjectListModel &list, size_t index, double &altitude,
                      int &altitudeMode);
189 190 191 192

} // namespace detail
} // namespace Utils
} // namespace WaypointManager