MissionItem.h 14.3 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
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

#ifndef MissionItem_H
#define MissionItem_H

#include <QObject>
#include <QString>
#include <QtQml>
#include <QTextStream>
31
#include <QJsonObject>
32 33 34 35 36 37 38 39 40
#include <QGeoCoordinate>

#include "QGCMAVLink.h"
#include "QGC.h"
#include "MavlinkQmlSingleton.h"
#include "QmlObjectListModel.h"
#include "Fact.h"
#include "QGCLoggingCategory.h"
#include "QmlObjectListModel.h"
Don Gagne's avatar
Don Gagne committed
41
#include "MissionCommands.h"
42

43
// Abstract base class for Simple and Complex MissionItem obejcts.
44 45 46 47 48
class MissionItem : public QObject
{
    Q_OBJECT
    
public:
49
    MissionItem(Vehicle* vehicle, QObject* parent = NULL);
50

51 52
    MissionItem(Vehicle*        vehicle,
                int             sequenceNumber,
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
                MAV_CMD         command,
                MAV_FRAME       frame,
                double          param1,
                double          param2,
                double          param3,
                double          param4,
                double          param5,
                double          param6,
                double          param7,
                bool            autoContinue,
                bool            isCurrentItem,
                QObject*        parent = NULL);

    MissionItem(const MissionItem& other, QObject* parent = NULL);

    ~MissionItem();

    const MissionItem& operator=(const MissionItem& other);
    
72
    Q_PROPERTY(double           altDifference           READ altDifference          WRITE setAltDifference      NOTIFY altDifferenceChanged)        ///< Change in altitude from previous waypoint
73
    Q_PROPERTY(double           altPercent              READ altPercent             WRITE setAltPercent         NOTIFY altPercentChanged)           ///< Percent of total altitude change in mission altitude
74 75 76 77 78 79 80 81 82 83 84
    Q_PROPERTY(double           azimuth                 READ azimuth                WRITE setAzimuth            NOTIFY azimuthChanged)              ///< Azimuth to previous waypoint
    Q_PROPERTY(QString          category                READ category                                           NOTIFY commandChanged)
    Q_PROPERTY(MavlinkQmlSingleton::Qml_MAV_CMD command READ command                WRITE setCommand            NOTIFY commandChanged)
    Q_PROPERTY(QString          commandDescription      READ commandDescription                                 NOTIFY commandChanged)
    Q_PROPERTY(QString          commandName             READ commandName                                        NOTIFY commandChanged)
    Q_PROPERTY(bool             dirty                   READ dirty                  WRITE setDirty              NOTIFY dirtyChanged)
    Q_PROPERTY(double           distance                READ distance               WRITE setDistance           NOTIFY distanceChanged)             ///< Distance to previous waypoint
    Q_PROPERTY(bool             friendlyEditAllowed     READ friendlyEditAllowed                                NOTIFY friendlyEditAllowedChanged)
    Q_PROPERTY(bool             homePosition            READ homePosition                                       CONSTANT)                           ///< true: This item is being used as a home position indicator
    Q_PROPERTY(bool             isCurrentItem           READ isCurrentItem          WRITE setIsCurrentItem      NOTIFY isCurrentItemChanged)
    Q_PROPERTY(bool             rawEdit                 READ rawEdit                WRITE setRawEdit            NOTIFY rawEditChanged)              ///< true: raw item editing with all params
85
    Q_PROPERTY(bool             relativeAltitude        READ relativeAltitude                                   NOTIFY frameChanged)
86 87 88
    Q_PROPERTY(int              sequenceNumber          READ sequenceNumber         WRITE setSequenceNumber     NOTIFY sequenceNumberChanged)
    Q_PROPERTY(bool             standaloneCoordinate    READ standaloneCoordinate                               NOTIFY commandChanged)
    Q_PROPERTY(bool             specifiesCoordinate     READ specifiesCoordinate                                NOTIFY commandChanged)
89
    Q_PROPERTY(bool             showHomePosition        READ showHomePosition       WRITE setShowHomePosition   NOTIFY showHomePositionChanged)
90

91 92 93 94 95 96 97 98 99 100
    // Mission item has two coordinates associated with them:
    //  coordinate:     This is the entry point for a waypoint line into the item. For a simple item it is also the location of the item
    //  exitCoordinate  This is the exit point for a waypoint line coming out of the item. For a SimpleMissionItem this will be the same as
    //                  coordinate. For a ComplexMissionItem it may be different than the entry coordinate.
    Q_PROPERTY(QGeoCoordinate   coordinate              READ coordinate             WRITE setCoordinate         NOTIFY coordinateChanged)
    Q_PROPERTY(QGeoCoordinate   exitCoordinate          READ exitCoordinate                                     NOTIFY exitCoordinateChanged)

    /// @return true: SimpleMissionItem, false: ComplexMissionItem
    Q_PROPERTY(bool             simpleItem              READ simpleItem                                         NOTIFY simpleItemChanged)

101 102 103 104 105 106 107 108 109 110 111
    // These properties are used to display the editing ui
    Q_PROPERTY(QmlObjectListModel*  checkboxFacts   READ checkboxFacts  NOTIFY uiModelChanged)
    Q_PROPERTY(QmlObjectListModel*  comboboxFacts   READ comboboxFacts  NOTIFY uiModelChanged)
    Q_PROPERTY(QmlObjectListModel*  textFieldFacts  READ textFieldFacts NOTIFY uiModelChanged)

    /// List of child mission items. Child mission item are subsequent mision items which do not specify a coordinate. They
    /// are shown next to the part item in the ui.
    Q_PROPERTY(QmlObjectListModel*  childItems      READ childItems     CONSTANT)

    // Property accesors
    
Don Gagne's avatar
Don Gagne committed
112
    double          altDifference       (void) const    { return _altDifference; }
113
    double          altPercent          (void) const    { return _altPercent; }
114
    double          azimuth             (void) const    { return _azimuth; }
Don Gagne's avatar
Don Gagne committed
115
    QString         category            (void) const;
Don Gagne's avatar
Don Gagne committed
116
    MavlinkQmlSingleton::Qml_MAV_CMD command(void) const { return (MavlinkQmlSingleton::Qml_MAV_CMD)_commandFact.cookedValue().toInt(); };
117 118 119 120 121 122 123 124 125 126
    QString         commandDescription  (void) const;
    QString         commandName         (void) const;
    QGeoCoordinate  coordinate          (void) const;
    bool            dirty               (void) const    { return _dirty; }
    double          distance            (void) const    { return _distance; }
    bool            friendlyEditAllowed (void) const;
    bool            homePosition        (void) const    { return _homePositionSpecialCase; }
    bool            isCurrentItem       (void) const    { return _isCurrentItem; }
    bool            rawEdit             (void) const;
    int             sequenceNumber      (void) const    { return _sequenceNumber; }
127
    bool            standaloneCoordinate(void) const;
128
    bool            specifiesCoordinate (void) const;
129
    bool            showHomePosition    (void) const    { return _showHomePosition; }
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149


    QmlObjectListModel* textFieldFacts  (void);
    QmlObjectListModel* checkboxFacts   (void);
    QmlObjectListModel* comboboxFacts   (void);
    QmlObjectListModel* childItems      (void) { return &_childItems; }

    void setRawEdit(bool rawEdit);
    void setDirty(bool dirty);
    void setSequenceNumber(int sequenceNumber);
    
    void setIsCurrentItem(bool isCurrentItem);
    
    void setCoordinate(const QGeoCoordinate& coordinate);
    
    void setCommandByIndex(int index);

    void setCommand(MavlinkQmlSingleton::Qml_MAV_CMD command);

    void setHomePositionSpecialCase(bool homePositionSpecialCase) { _homePositionSpecialCase = homePositionSpecialCase; }
150
    void setShowHomePosition(bool showHomePosition);
151

152 153 154 155
    void setAltDifference   (double altDifference);
    void setAltPercent      (double altPercent);
    void setAzimuth         (double azimuth);
    void setDistance        (double distance);
156 157 158

    // C++ only methods

Don Gagne's avatar
Don Gagne committed
159 160
    MAV_FRAME   frame       (void)  const { return (MAV_FRAME)_frameFact.rawValue().toInt(); }
    bool        autoContinue(void)  const { return _autoContinueFact.rawValue().toBool(); }
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    double      param1      (void)  const { return _param1Fact.rawValue().toDouble(); }
    double      param2      (void)  const { return _param2Fact.rawValue().toDouble(); }
    double      param3      (void)  const { return _param3Fact.rawValue().toDouble(); }
    double      param4      (void)  const { return _param4Fact.rawValue().toDouble(); }
    double      param5      (void)  const { return _param5Fact.rawValue().toDouble(); }
    double      param6      (void)  const { return _param6Fact.rawValue().toDouble(); }
    double      param7      (void)  const { return _param7Fact.rawValue().toDouble(); }

    void setCommand     (MAV_CMD command);
    void setFrame       (MAV_FRAME frame);
    void setAutoContinue(bool autoContinue);
    void setParam1      (double param1);
    void setParam2      (double param2);
    void setParam3      (double param3);
    void setParam4      (double param4);
    void setParam5      (double param5);
    void setParam6      (double param6);
    void setParam7      (double param7);

    // C++ only methods
    
182
    void save(QJsonObject& json);
183
    bool load(QTextStream &loadStream);
184
    bool load(const QJsonObject& json, QString& errorString);
185 186 187 188 189

    bool relativeAltitude(void) { return frame() == MAV_FRAME_GLOBAL_RELATIVE_ALT; }

    static const double defaultAltitude;

190 191 192 193
    // Pure virtuals which must be provides by derived classes
    virtual bool            simpleItem(void) const = 0;
    virtual QGeoCoordinate  exitCoordinate(void) const = 0;

Don Gagne's avatar
Don Gagne committed
194 195 196
public slots:
    void setDefaultsForCommand(void);

197
signals:
Don Gagne's avatar
Don Gagne committed
198
    void altDifferenceChanged       (double altDifference);
199
    void altPercentChanged          (double altPercent);
200
    void azimuthChanged             (double azimuth);
201 202
    void commandChanged             (MavlinkQmlSingleton::Qml_MAV_CMD command);
    void coordinateChanged          (const QGeoCoordinate& coordinate);
203
    void exitCoordinateChanged      (const QGeoCoordinate& exitCoordinate);
204
    void dirtyChanged               (bool dirty);
205
    void distanceChanged            (double distance);
206 207 208 209 210 211 212
    void frameChanged               (int frame);
    void friendlyEditAllowedChanged (bool friendlyEditAllowed);
    void headingDegreesChanged      (double heading);
    void isCurrentItemChanged       (bool isCurrentItem);
    void rawEditChanged             (bool rawEdit);
    void sequenceNumberChanged      (int sequenceNumber);
    void uiModelChanged             (void);
213
    void showHomePositionChanged    (bool showHomePosition);
214
    void simpleItemChanged          (bool simpleItem);
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    
private slots:
    void _setDirtyFromSignal(void);
    void _sendCommandChanged(void);
    void _sendCoordinateChanged(void);
    void _sendFrameChanged(void);
    void _sendFriendlyEditAllowedChanged(void);
    void _sendUiModelChanged(void);
    void _syncAltitudeRelativeToHomeToFrame(const QVariant& value);
    void _syncFrameToAltitudeRelativeToHome(void);

private:
    void _clearParamMetaData(void);
    void _connectSignals(void);
    void _setupMetaData(void);

private:
232
    Vehicle*    _vehicle;                   ///< Vehicle associated with this item, NULL for offline mode
233 234 235 236
    bool        _rawEdit;
    bool        _dirty;
    int         _sequenceNumber;
    bool        _isCurrentItem;
Don Gagne's avatar
Don Gagne committed
237
    double      _altDifference;             ///< Difference in altitude from previous waypoint
238
    double      _altPercent;                ///< Percent of total altitude change in mission
239
    double      _azimuth;                   ///< Azimuth to previous waypoint
240 241
    double      _distance;                  ///< Distance to previous waypoint
    bool        _homePositionSpecialCase;   ///< true: This item is being used as a ui home position indicator
242
    bool        _showHomePosition;
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

    Fact    _altitudeRelativeToHomeFact;
    Fact    _autoContinueFact;
    Fact    _commandFact;
    Fact    _frameFact;
    Fact    _param1Fact;
    Fact    _param2Fact;
    Fact    _param3Fact;
    Fact    _param4Fact;
    Fact    _param5Fact;
    Fact    _param6Fact;
    Fact    _param7Fact;
    Fact    _supportedCommandFact;
    
    static FactMetaData*    _altitudeMetaData;
    static FactMetaData*    _commandMetaData;
    static FactMetaData*    _defaultParamMetaData;
    static FactMetaData*    _frameMetaData;
    static FactMetaData*    _latitudeMetaData;
    static FactMetaData*    _longitudeMetaData;

    FactMetaData    _param1MetaData;
    FactMetaData    _param2MetaData;
    FactMetaData    _param3MetaData;
    FactMetaData    _param4MetaData;
268 269 270
    FactMetaData    _param5MetaData;
    FactMetaData    _param6MetaData;
    FactMetaData    _param7MetaData;
271 272 273 274 275 276 277

    /// This is used to reference any subsequent mission items which do not specify a coordinate.
    QmlObjectListModel  _childItems;

    bool _syncingAltitudeRelativeToHomeAndFrame;    ///< true: already in a sync signal, prevents signal loop
    bool _syncingHeadingDegreesAndParam4;           ///< true: already in a sync signal, prevents signal loop

278
    const MissionCommands*  _missionCommands;
279 280 281 282 283 284 285 286 287 288 289 290

    static const char*  _itemType;
    static const char*  _jsonTypeKey;
    static const char*  _jsonIdKey;
    static const char*  _jsonFrameKey;
    static const char*  _jsonCommandKey;
    static const char*  _jsonParam1Key;
    static const char*  _jsonParam2Key;
    static const char*  _jsonParam3Key;
    static const char*  _jsonParam4Key;
    static const char*  _jsonAutoContinueKey;
    static const char*  _jsonCoordinateKey;
291 292 293
};

#endif