Utils.h 6.56 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
#pragma once

#include "SimpleMissionItem.h"
#include "QmlObjectListModel.h"

#include <QVariant>

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>
23
bool extract(const QmlObjectListModel &missionItems,
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 58 59 60 61
             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>
62
bool extract(const QmlObjectListModel &missionItems,
63 64 65 66 67 68 69 70 71 72 73 74 75
             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>
76
bool extract(const ContainerType<CoordinateType>  &source,
77 78 79 80 81 82 83 84 85 86 87 88
             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,
89
                          std::size_t(long(source.size())-1)) /*recursion*/)
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
                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;
}


//!
//! \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);

//! @brief Creates (from QGeoCoordinate) and Inserts a SimpleMissionItem at the given index to list.
//! \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);

bool doUpdate(QmlObjectListModel &list);

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

    //! @brief Updates the sequence numbers of the VisualMissionItems inside \p list beginning from \p startIdx.
    //!
    //! Updates the sequence numbers of the VisualMissionItems inside \p list beginning from \pstartIdx.
    //! \param list List containing VisualMissionItems.
    //! \param startIdx Start index.
    //! \return Returns true if successfull, false either.
    //!
    //! \note The function returns false immediatelly if the list \p list ist empty or
    //! the \p startIdx is out of bounds.
    //! \note If the list \p list contains only one VisualMissionItem it's sequence number is
    //! set to 0.
    bool updateSequenceNumbers(QmlObjectListModel &list, size_t startIdx=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);

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