#include #include #include "../common/comm.h" #include "appmodel.h" #include "Ego.h" #define WIN23_LEAN_AND_MEAN #include //#include "FishEyeWindow.h" // extern std::array ar; CarData::CarData(QObject *parent) : QObject(parent) {} CarData::CarData(const CarData &other) : QObject(nullptr), m_uid(other.m_uid), m_name(other.m_name), m_type(other.m_type), m_state(other.m_state) {} int32_t CarData::uid() const { return m_uid; } QString CarData::name() const { return m_name; } QString CarData::type() const { return m_type; } QString CarData::state() const { return m_state; } void CarData::setUid(const int32_t &value) { m_uid = value; emit dataChanged(); } void CarData::setCarType(const int32_t& value) { m_carType = value; } void CarData::setName(const QString &value) { m_name = value; emit dataChanged(); } void CarData::setState(const QString &value) { m_state = value; emit dataChanged(); } void CarData::setType(const QString &value) { m_type = value; emit dataChanged(); } class AppModelPrivate { public: QList car; QQmlListProperty *carProp = nullptr; }; static void carAppend(QQmlListProperty *prop, CarData *val) { Q_UNUSED(val); Q_UNUSED(prop); } static CarData *carAt(QQmlListProperty *prop, qsizetype index) { AppModelPrivate *d = static_cast(prop->data); return d->car.at(index); } static qsizetype carCount(QQmlListProperty *prop) { AppModelPrivate *d = static_cast(prop->data); return d->car.size(); } static void carClear(QQmlListProperty *prop) { static_cast(prop->data)->car.clear(); } AppModel::AppModel(IMonitroNotify*n, IRemoteCtrl *c, QObject* qmlobj, QObject *parent) : _notify(n), _control(c), QObject(parent), d(new AppModelPrivate),m_peer(-1),m_qmlObj(qmlobj) { m_account = QString::fromStdString(_control->GetAccount()); m_password = QString::fromStdString(_control->GetPassword()); m_driveName = QString::fromStdString(_control->GetName()); d->carProp = new QQmlListProperty(this, d, carAppend, carCount, carAt, carClear); m_ready = false; // _window = std::make_unique(); //_window->Start("192.168.1.64"); connect(dynamic_cast(_notify), SIGNAL(egoLoginResult(bool)), this, SLOT(onLoginRst(bool))); connect(dynamic_cast(_notify), SIGNAL(egoCarAppend(UserDriver)), this, SLOT(onCarAppend(UserDriver))); connect(dynamic_cast(_notify), SIGNAL(egoNotifyLeave(int32_t)), this, SLOT(onNotifyLeave(int32_t))); connect(dynamic_cast(_notify), SIGNAL(egoNotifyFail(int32_t)), this, SLOT(onNotifyFail(int32_t))); connect(dynamic_cast(_notify), SIGNAL(egoNotifyDel(int32_t)), this, SLOT(onNotifyDel(int32_t))); connect(dynamic_cast(_notify), SIGNAL(egoKickoff()), this, SLOT(onKickoff())); connect(dynamic_cast(_notify), SIGNAL(egoNotifyState(int32_t, UserState)), this, SLOT(onNotifyState(int32_t, UserState))); // connect(dynamic_cast(_notify),SIGNAL(ego) } AppModel::~AppModel() { if (d->carProp) delete d->carProp; if (d) delete d; } //int AppModel::ModelX() const { return m_modelx; } //int AppModel::ModelY() const { return m_modely; } //int AppModel::ModelZ() const { return m_modelz; } bool AppModel::ready() const { return m_ready; } QString AppModel::Account() const { return m_account; } QString AppModel::Password() const { return m_password; } QString AppModel::CarName() const { return m_carName; } QString AppModel::DriveName() const { return m_driveName; } void AppModel::setReady(const bool value) { m_ready = value; emit readyChanged(); } void AppModel::setAccount(const QString& value) { m_account = value; emit accountChanged(); } /* void AppModel::setModelX(const int32_t value) { m_modelx = value; emit modelXChanged(); } void AppModel::setModelZ(const int32_t value) { m_modelz = value+200; emit modelZChanged(); } void AppModel::setModelY(const int32_t value) { m_modely = value; emit modelYChanged(); } */ void AppModel::setPassword(const QString& password) { m_password = password; emit passwordChanged(); } void AppModel::setCarName(const QString& carname) { m_carName = carname; emit carNameChanged(); } void AppModel::setDriveName(const QString& drivename) { m_driveName = drivename; emit driveNameChanged(); } /* void AppModel::SetGear(const int32_t value) { m_gear = value; emit gearChanged(); } */ /* void AppModel::setRadar6(const int32_t& value) { m_radar6 = value; emit radar6Changed(); } void AppModel::setRadar7(const int32_t& value) { m_radar7 = value; emit radar7Changed(); } */ /* void AppModel::setRadar6(const int32_t &value) { m_radar6 = value; emit radar6Changed(); } */ void AppModel::onKickoff() { emit closeWin(); } void AppModel::onNotifyLeave(int32_t peer) { m_peer = -1; } void AppModel::onNotifyFail(int32_t peer) { m_peer = -1; } QQmlListProperty AppModel::car() const { return *(d->carProp); } void AppModel::userLogin(QString useName, QString password) { _control->Login(useName.toStdString(), password.toStdString()); } void AppModel::leaveCar() { if (m_ready == false) return; m_ready = false; m_peer = -1; _control->OnLeave(); } bool AppModel::idle(int id) { for (auto& node : _users) { if (node.uid == id &&((id==m_peer)|| node.state == UserState::Idle)) return true; } return false; } void AppModel::connectCar(int id) { for (auto& node : _users) { if (node.uid == id&&node.state==UserState::Idle) { if (m_ready == true) return; _control->OnConnect(id); m_peer = id; setReady(true); this->setCarName(QString::fromStdString(node.name)); break; } } } void AppModel::controlCar(int id) { connectCar(id); } /* void AppModel::showTrackDlg() { } void AppModel::unTrack() { m_ready = false; _control->SetReady(false); _control->OnCarLeave(); } bool AppModel::autoClose() { return _control->IsAutoClose(); } */ void AppModel::onLoginRst(bool rst) { qDebug() << "AppModel::loginResult >>"; // if (rst) { // delete previous carList // foreach (CarData *inf, d->car) // delete inf; // d->car.clear(); // req new list //_control->ReqCarList(); // } else { // qDebug() << "AppModel::loginResult >>" << rst; // } } void AppModel::onNotifyDel(int32_t peer) { for (int32_t i = 0; i < d->car.count(); i++) { if (d->car.at(i)->uid() == peer) { delete d->car.at(i); d->car.removeAt(i); break; } } emit carChanged(); for (auto it = _users.begin(); it != _users.end(); ++it) { if ((*it).uid == peer) { _users.erase(it); break; } } // d->car.clear(); } void AppModel::onCarAppend(const UserDriver&info) { bool bFind = false; for (auto& node : d->car) { if (node->uid() == info.uid) { node->setType(QString::number(info.type)); node->setName(QString::fromStdString(info.name)); node->setState(QString::number(info.state)); bFind = true; return; } } if (!bFind) { CarData* users = new CarData;// = (CarData*)malloc; users->setUid(info.uid); users->setType(QString::number(info.type)); users->setName(QString::fromStdString(info.name)); users->setState(QString::number(info.state)); d->car.append(users); } bFind = false; for (auto& node : _users) { if (node.uid == info.uid) { bFind = true; } } if(!bFind) _users.push_back(info); emit carChanged(); } void AppModel::onNotifyState(int32_t uid, UserState state) { for (auto& node : d->car) { if (node->uid() == uid) { node->setState(QString::number(state)); break; } } emit carChanged(); for (auto& node : _users) { if (node.uid == uid) { node.state = state; break; } } } void AppModel::logout() { d->car.clear(); m_ready = false; } /* void AppModel::switchDriver() { if (m_peer == -1) return; _control->SwitchDriver(m_peer); } */ /* void AppModel::areaChanged(int32_t area) { m_area = static_cast(area); } */ void AppModel::OnClose() { if (m_ready) { leaveCar(); Sleep(5000); } ::ExitProcess(0); } /* void AppModel::OnPosition(float x, float y, float z) { setModelX(x * 100); setModelY(y * 100); setModelZ(z * 100); } */