-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCourierToGotoMapRoute.cpp
111 lines (90 loc) · 3.71 KB
/
CourierToGotoMapRoute.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "CourierToGotoMapRoute.h"
#include <QTimer>
#include <QtConcurrent/QtConcurrent>
#include "Models/CouriersModel.h"
#include "Models/GotosModel.h"
CourierToGotoMapRoute::CourierToGotoMapRoute(QObject *parent) : QObject(parent)
{
// auto timer = new QTimer(this);
// timer->setSingleShot(false);
// connect(timer, &QTimer::timeout, this, &MapRoute::startChanged);
// timer->start(2000);
}
void CourierToGotoMapRoute::setModels(CouriersModel *couriersModel,
GotosModel *gotosModel)
{
m_couriersModel = couriersModel;
m_gotosModel = gotosModel;
connect(couriersModel, &QAbstractItemModel::dataChanged,
this, &CourierToGotoMapRoute::onCouriersModelDatatChanged);
connect(gotosModel, &QAbstractItemModel::dataChanged,
this, &CourierToGotoMapRoute::onGotosModelDatatChanged);
// connect(&startModel, &QAbstractItemModel::rowsRemoved,
// this, &MapRoute::onStartModelRowRemoved);
// connect(&finishModel, &QAbstractItemModel::rowsRemoved,
// this, &MapRoute::onFinishModelRowsRemoved);
}
void CourierToGotoMapRoute::setGotoId(const QString &gotoId)
{
if(gotoId.isEmpty()){
m_gotoId.clear();
m_courierId.clear();
m_routeEnds.clear();
emit routeEndsChanged();
return;
}
m_gotoId = gotoId;
m_courierId = m_gotosModel->getGotoById(m_gotoId).value("courier_id").toString();
m_routeEnds.clear();
emit routeEndsChanged();
if(!m_couriersModel || !m_gotosModel) return;
QtConcurrent::run([this]{
const auto courier = m_couriersModel->getCourierById(m_courierId);
if(courier.isEmpty()) return;
const auto goto_ = m_gotosModel->getGotoById(m_gotoId);
if(goto_.isEmpty()) return;
QVariantMap endsMap;
endsMap.insert("courier_latitude", courier.value("latitude"));
endsMap.insert("courier_longitude", courier.value("longitude"));
endsMap.insert("goto_latitude", goto_.value("latitude"));
endsMap.insert("goto_longitude", goto_.value("longitude"));
if(endsMap != m_routeEnds){
m_routeEnds = endsMap;
emit routeEndsChanged();
}
});
}
void CourierToGotoMapRoute::onCouriersModelDatatChanged(const QModelIndex &topLeft,
const QModelIndex &bottomRight,
const QVector<int> &roles)
{
if(m_gotoId.isEmpty() || m_courierId.isEmpty()) return;
if(topLeft != bottomRight) return;
if(!roles.contains(CouriersModel::LATITUDE)
|| !roles.contains(CouriersModel::LONGITUDE))
return;
if(topLeft.data(CouriersModel::_ID).toString() != m_courierId ) return;
QVariantMap endsMap = m_routeEnds;
endsMap.insert("courier_latitude", topLeft.data(CouriersModel::LATITUDE));
endsMap.insert("courier_longitude", topLeft.data(CouriersModel::LONGITUDE));
if(endsMap != m_routeEnds){
m_routeEnds = endsMap;
emit routeEndsChanged();
}
}
void CourierToGotoMapRoute::onGotosModelDatatChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
{
if(m_gotoId.isEmpty() || m_courierId.isEmpty()) return;
if(topLeft != bottomRight) return;
if(!roles.contains(CouriersModel::LATITUDE)
|| !roles.contains(CouriersModel::LONGITUDE))
return;
if(topLeft.data(GotosModel::_ID).toString() != m_gotoId ) return;
QVariantMap endsMap = m_routeEnds;
endsMap.insert("goto_latitude", topLeft.data(CouriersModel::LATITUDE));
endsMap.insert("goto_longitude", topLeft.data(CouriersModel::LONGITUDE));
if(endsMap != m_routeEnds){
m_routeEnds = endsMap;
emit routeEndsChanged();
}
}