SimsWorld/ComponentBasic/RouteManage.cpp

139 lines
3.6 KiB
C++

#include "RouteManage.h"
PlainRouteManagePlugin::PlainRouteManagePlugin()
{
}
#include <QJsonArray>
void PlainRouteManagePlugin::execute(std::shared_ptr<const NewPlainRoute> in, QList<std::shared_ptr<RespondDefault>>& out)
{
auto resp = std::make_shared<RespondDefault>();
resp->reset(in->targetEntity(), in->sourceEntity());
if(!this->_route_resource_map.contains(in->_route_name)){
resp->_success_mark = true;
this->_route_resource_map[in->_route_name] = RouteResource();
}
else {
resp->_reason_text = "已包含指定名称的路径";
}
out << resp;
}
void PlainRouteManagePlugin::execute(std::shared_ptr<const RemovePlainRoute> in, QList<std::shared_ptr<RespondDefault>>& out)
{
auto resp = std::make_shared<RespondDefault>();
resp->reset(in->targetEntity(), in->sourceEntity());
if (this->_route_resource_map.contains(in->_route_name)) {
resp->_success_mark = true;
this->_route_resource_map.remove(in->_route_name);
}
else {
resp->_reason_text = "不包含指定名称的路径";
}
out << resp;
}
void PlainRouteManagePlugin::execute(std::shared_ptr<const PlainRouteReset> in, QList<std::shared_ptr<RespondDefault>>& out)
{
auto resp = std::make_shared<RespondDefault>();
resp->reset(in->targetEntity(), in->sourceEntity());
if (this->_route_resource_map.contains(in->_route_name)) {
resp->_success_mark = true;
RouteResource res;
res._route_points = in->_lonlat_list;
this->_route_resource_map[in->_route_name] = res;
}
else {
resp->_reason_text = "不包含指定名称的路径";
}
out << resp;
}
void PlainRouteManagePlugin::execute(std::shared_ptr<const PlainRouteQuery> in, QList<std::shared_ptr<PlainRouteInfo>>& out)
{
auto resp = std::make_shared<PlainRouteInfo>();
resp->reset(in->targetEntity(), in->sourceEntity());
if (this->_route_resource_map.contains(in->_route_name)) {
resp->_success_mark = true;
resp->_route_name = in->_route_name;
resp->_lonlat_list = this->_route_resource_map[in->_route_name]._route_points;
}
else {
resp->_reason_text = "不包含指定名称的路径";
}
out << resp;
}
void PlainRouteManagePlugin::recoveryFrom(const QJsonObject& obj)
{
auto route_array = obj["total_route"].toArray();
for (auto idx = 0; idx < route_array.size(); ++idx) {
auto track_record = route_array.at(idx).toObject();
auto track_name = track_record["name"].toString();
auto track_points = track_record["points"].toArray();
RouteResource value;
for (auto tidx = 0; tidx < track_points.size(); tidx++) {
auto point = track_points.at(tidx).toObject();
auto pins = LonLatPos{ point["point_lon"].toDouble(), point["point_lat"].toDouble() };
value._route_points << pins;
}
this->_route_resource_map[track_name] = value;
}
}
void PlainRouteManagePlugin::saveTo(QJsonObject& obj) const
{
QJsonArray route_array;
for (auto route_name : this->_route_resource_map.keys()) {
QJsonObject track_record;
track_record["name"] = route_name;
QJsonArray point_array;
for (auto pointv : this->_route_resource_map[route_name]._route_points) {
QJsonObject point;
point["point_lon"] = pointv._lon_deg;
point["point_lat"] = pointv._lat_deg;
point_array.append(point);
}
track_record["points"] = point_array;
route_array.append(track_record);
}
obj["total_route"] = route_array;
}
std::shared_ptr<Serializable> PlainRouteManagePlugin::newDefault() const
{
return nullptr;
}
void PlainRouteManagePlugin::bindEntity(std::weak_ptr<WsEntity> host)
{
this->_bind_entity = host;
}
QString PlainRouteManagePlugin::name() const
{
return NAME(PlainRouteManagePlugin);
}
RouteResource& RouteResource::operator=(const RouteResource& other)
{
this->_route_points = other._route_points;
return *this;
}