AntiClipSettings/DeviceListModel.h

59 lines
1.7 KiB
C
Raw Normal View History

2024-08-16 16:24:15 +08:00
#ifndef DEVICELISTMODEL_H
#define DEVICELISTMODEL_H
#include <QAbstractListModel>
#include <QQmlEngine>
class QUdpSocket;
2024-08-20 09:29:49 +08:00
class DeviceConnection;
2024-08-16 16:24:15 +08:00
class DeviceListModel : public QAbstractListModel {
Q_OBJECT
QML_ELEMENT
QML_UNCREATABLE("Only used in C++...")
Q_PROPERTY(bool isSearching READ isSearching NOTIFY isSearchingChanged)
Q_PROPERTY(float searchProgress READ searchProgress NOTIFY searchProgressChanged)
public:
static constexpr uint16_t BroadcastPort = 40000;
static constexpr uint16_t ListenPort = 40001;
static constexpr int RetryCount = 10;
enum InfoRoles {
DeviceIdRole = Qt::UserRole + 1,
FirmwareVersionRole,
SoftwareVersionRole,
IpRole,
2024-08-20 09:29:49 +08:00
OnlineStatusRole,
2024-08-16 16:24:15 +08:00
};
DeviceListModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const final;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const final;
QHash<int, QByteArray> roleNames() const final;
Q_INVOKABLE QVariantMap get(int index) const;
2024-08-20 09:29:49 +08:00
std::shared_ptr<DeviceConnection> device(int index);
2024-08-21 09:26:06 +08:00
void startSearchDevice();
2024-08-16 16:24:15 +08:00
bool isSearching() const;
float searchProgress() const;
signals:
void isSearchingChanged();
void searchProgressChanged();
protected:
2024-08-20 09:29:49 +08:00
void onDeviceConnected();
void onDeviceDisconnected();
2024-08-16 16:24:15 +08:00
void onDeviceReplyReadyRead();
void broadcast();
void stopSearchDevice();
void timerEvent(QTimerEvent *event);
private:
2024-08-20 09:29:49 +08:00
std::vector<std::shared_ptr<DeviceConnection>> m_devices;
2024-08-16 16:24:15 +08:00
std::list<QUdpSocket *> m_udpSockets;
QUdpSocket *m_broadcastSocket = nullptr;
int m_timerId = -1;
int m_retries = 0;
};
#endif // DEVICELISTMODEL_H