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

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

#include <QVariant>

8 9 10 11 12 13

class Vehicle;
class QGeoCoordinate;



14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
namespace WaypointManager {
namespace Utils {


template<class CoordinateType>
CoordinateType getCoordinate(const SimpleMissionItem* item){
    return item->coordinate();
}

template<>
QVariant getCoordinate(const SimpleMissionItem* item);


/// extracts the coordinates stored in missionItems (list of MissionItems) and stores them in coordinateList.
template <class CoordinateType, template <class, class...> class ContainerType>
29
bool extract(const QmlObjectListModel &missionItems,
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 58 59 60 61 62 63 64 65 66 67
             ContainerType<CoordinateType> &coordinateList,
             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*/)
                return false;
            if ( !extract(missionItems,
                          coordinateList,
                          0,
                          endIndex) /*recursion*/)
                return false;
        } else {
            for (std::size_t i = startIndex; i <= endIndex; ++i) {
                SimpleMissionItem *mItem = missionItems.value<SimpleMissionItem *>(int(i));

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

    return true;
}

/// extracts the coordinates stored in missionItems (list of MissionItems) and stores them in coordinateList.
template <class CoordinateType, template <class, class...> class ContainerType>
68
bool extract(const QmlObjectListModel &missionItems,
69 70 71 72 73 74 75 76 77 78 79 80 81
             ContainerType<CoordinateType> &coordinateList)
{

    return extract(missionItems,
                   coordinateList,
                   std::size_t(0),
                   std::size_t(missionItems.count())
                   );
}


/// extracts the coordinates stored in missionItems (list of MissionItems) and stores them in coordinateList.
template <class CoordinateType, template <class, class...> class ContainerType>
82
bool extract(const ContainerType<CoordinateType>  &source,
83 84 85 86 87 88 89 90 91 92 93 94
             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,
95
                          std::size_t(long(source.size())-1)) /*recursion*/)
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
                return false;
            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;
}

113 114 115 116 117 118 119 120
//!
//! \brief Makes the SimpleMissionItem \p item a takeoff command.
//! \param item SimpleMissionItem
//! \param vehilce Vehicle.
//! \return Returns true if successfull, false either.
//!
bool makeTakeOffCmd(SimpleMissionItem *item, Vehicle *vehicle);

121 122 123 124 125 126 127 128
//!
//! \brief Makes the SimpleMissionItem \p item a land command.
//! \param item SimpleMissionItem
//! \param vehilce Vehicle.
//! \return Returns true if successfull, false either.
//!
bool makeLandCmd(SimpleMissionItem *item, Vehicle *vehicle);

129 130 131 132 133 134 135
//!
//! \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);

136 137 138 139 140 141 142 143 144 145 146 147

//!
//! \brief Initializes the list \p list.
//!
//! Adds a MissionSettingsItem to \p list.
//! \param list List of VisualMissionItems.
//! \param vehicle Vehicle.
//! \param flyView Fly- or planview.
void initialize(QmlObjectListModel &list,
                Vehicle *vehicle,
                bool flyView=true);

148
//! @brief Creates (from QGeoCoordinate) and inserts a SimpleMissionItem at the given index to list.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
//! \param coordinate Coordinate of the SimpleMissionItem.
//! \param index Index to insert SimpleMissionItem.
//! \param list List of SimpleMissionItems.
//! \param vehicle Vehicle.
//! \param flyView Fly- or planview.
//! \param parent Parent of the SimpleMissionItem (Set to &list if nullptr).
//! \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.
//!
//! @note The list must be initialized. (\see \fn initialize)
//! @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,
                       QmlObjectListModel &list,
                       Vehicle *vehicle,
                       bool flyView=true,
                       QObject *parent=nullptr,
                       bool doUpdates=true);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
//!
//! \brief Performs a list update.
//!
//! 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.
bool updateList(QmlObjectListModel &list);

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

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

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

201 202 203 204
//!
//! \brief Prints the \p list.
//! \param list List containing VisualMissionItems.
void printList(QmlObjectListModel &list);
205 206 207 208 209 210 211 212 213 214 215

namespace detail {
    size_t nextSequenceNumber(QmlObjectListModel &list);
    bool previousAltitude(QmlObjectListModel &list,
                          size_t index,
                          double &altitude,
                          int &altitudeMode);

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