MultiSignalSpyV2.h 2.78 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 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 68 69 70 71 72 73 74 75 76 77 78 79
/****************************************************************************
 *
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#pragma once

#include <QObject>
#include <QSignalSpy>
#include <QGeoCoordinate>

/// @file
///     @brief Works just like MultiSignalSpy but the signal arrays are setup automatically through introspection on
///     QMetaObject information. So no need to set up array an index/mask enums.

class MultiSignalSpyV2 : public QObject
{
    Q_OBJECT
    
public:
    MultiSignalSpyV2(QObject* parent = nullptr);
    ~MultiSignalSpyV2();

    bool init(QObject* signalEmitter);

    quint32 signalNameToMask(const char* signalName);

    /// @param mask bit mask specifying which signals to check. The lowest order bit represents
    ///     index 0 into the rgSignals array and so on up the bit mask.
    /// @return true if signal count = 1 for the specified signals
    bool checkSignalByMask(quint32 mask);

    /// @return true if signal count = 1 for specified signals and signal count of 0
    ///     for all other signals
    bool checkOnlySignalByMask(quint32 mask);

    /// @param mask bit mask specifying which signals to check. The lowest order bit represents
    ///     index 0 into the rgSignals array and so on up the bit mask.
    /// @return true if signal count >= 1 for the specified signals
    bool checkSignalsByMask(quint32 mask);

    /// @return true if signal count >= 1 for specified signals and signal count of 0
    ///     for all other signals
    bool checkOnlySignalsByMask(quint32 mask);

    bool checkNoSignalByMask(quint32 mask);
    bool checkNoSignals(void);

    void clearSignal(const char* signalName);
    void clearSignalsByMask(quint32 mask);
    void clearAllSignals(void);

    bool waitForSignal(const char* signalName, int msec);
    
    QSignalSpy* getSpy(const char* signalName);

    // Returns the value type for the first parameter of the signal
    bool            pullBoolFromSignal          (const char* signalName);
    int             pullIntFromSignal           (const char* signalName);
    QGeoCoordinate  pullQGeoCoordinateFromSignal(const char* signalName);

private:
    // QObject overrides
    void timerEvent(QTimerEvent * event);
    
    void _printSignalState              (quint32 mask);
    bool _checkSignalByMaskWorker       (quint32 mask, bool multipleSignalsAllowed);
    bool _checkOnlySignalByMaskWorker   (quint32 mask, bool multipleSignalsAllowed);

    QObject*            _signalEmitter = nullptr;
    QStringList         _rgSignalNames;
    QList<QSignalSpy*>  _rgSpys;
    bool                _timeout;
};