StopModel: store station in and out connections side

- Generally improve comments
This commit is contained in:
Filippo Gentile 2022-02-10 22:37:05 +01:00
parent 97cd886109
commit 4dd467212c
2 changed files with 38 additions and 25 deletions

View file

@ -131,8 +131,8 @@ void StopModel::loadJobStops(db_id jobId)
db_id prevOutGateId = 0;
q_selectStops.prepare("SELECT stops.id, stops.station_id, stops.arrival, stops.departure, stops.type,"
"stops.in_gate_conn, g1.gate_id, g1.gate_track, g1.track_id,"
"stops.out_gate_conn, g2.gate_id, g2.gate_track, g2.track_id,"
"stops.in_gate_conn, g1.gate_id, g1.gate_track, g1.track_id, g1.track_side,"
"stops.out_gate_conn, g2.gate_id, g2.gate_track, g2.track_id, g2.track_side,"
"stops.next_segment_conn_id, c.seg_id, c.in_track, c.out_track,"
"seg.in_gate_id, seg.out_gate_id"
" FROM stops"
@ -158,19 +158,21 @@ void StopModel::loadJobStops(db_id jobId)
s.fromGate.gateId = stop.get<db_id>(6);
s.fromGate.trackNum = stop.get<int>(7);
s.trackId = stop.get<db_id>(8);
s.fromGate.stationTrackSide = utils::Side(stop.get<int>(9));
s.toGate.gateConnId = stop.get<db_id>(9);
s.toGate.gateId = stop.get<db_id>(10);
s.toGate.trackNum = stop.get<int>(11);
db_id otherTrackId = stop.get<db_id>(12);
s.toGate.gateConnId = stop.get<db_id>(10);
s.toGate.gateId = stop.get<db_id>(11);
s.toGate.trackNum = stop.get<int>(12);
db_id otherTrackId = stop.get<db_id>(13);
s.toGate.stationTrackSide = utils::Side(stop.get<int>(14));
s.nextSegment.segConnId = stop.get<db_id>(13);
s.nextSegment.segmentId = stop.get<db_id>(14);
s.nextSegment.inTrackNum = stop.get<db_id>(15);
s.nextSegment.outTrackNum = stop.get<db_id>(16);
s.nextSegment.segConnId = stop.get<db_id>(15);
s.nextSegment.segmentId = stop.get<db_id>(16);
s.nextSegment.inTrackNum = stop.get<db_id>(17);
s.nextSegment.outTrackNum = stop.get<db_id>(18);
db_id segInGateId = stop.get<db_id>(17);
db_id segOutGateId = stop.get<db_id>(18);
db_id segInGateId = stop.get<db_id>(19);
db_id segOutGateId = stop.get<db_id>(20);
if(s.toGate.gateId && s.toGate.gateId == segOutGateId)
{
@ -923,7 +925,7 @@ void StopModel::setStopInfo(const QModelIndex &idx, StopItem newStop, StopItem::
startStopsEditing();
//Before Last and AddHere, so there is a stop after this
//Update next station beause next segment changed
//Update next station because "next" segment of previous (current) stop changed
StopItem& nextStop = stops[row + 1];
nextStop.fromGate.gateConnId = 0; //Reset to trigger update
if(!updateCurrentInGate(nextStop, s.nextSegment))
@ -1215,7 +1217,7 @@ bool StopModel::trySelectTrackForStop(StopItem &item)
{
//TODO: choose 'default' track, corretto tracciato
//Try to keep previous selected station track otherwise choose lowest position track possible
q.prepare("SELECT c.id, t.id"
q.prepare("SELECT c.id, c.track_side, t.id"
" FROM station_gate_connections c"
" JOIN station_tracks t ON c.track_id=t.id"
" WHERE c.gate_id=? AND c.gate_track=?"
@ -1229,14 +1231,17 @@ bool StopModel::trySelectTrackForStop(StopItem &item)
auto gate = q.getRows();
item.fromGate.gateConnId = gate.get<db_id>(0);
item.trackId = gate.get<db_id>(1);
item.fromGate.stationTrackSide = utils::Side(gate.get<int>(1));
item.trackId = gate.get<db_id>(2);
return true;
}
q.prepare("SELECT c.id, c.gate_id, c.gate_track, t.id, MIN(t.pos)"
//Select a random gate for first use
q.prepare("SELECT c.id, c.gate_id, c.gate_track, c.track_side, t.id, MIN(t.pos)"
" FROM station_tracks t"
" JOIN station_gate_connections c ON c.track_id=t.id"
" WHERE t.station_id=?");
" WHERE t.station_id=?"
" LIMIT 1");
q.bind(1, item.stationId);
if(q.step() != SQLITE_ROW || q.getRows().column_type(0) == SQLITE_NULL)
return false;
@ -1245,7 +1250,8 @@ bool StopModel::trySelectTrackForStop(StopItem &item)
item.fromGate.gateConnId = gate.get<db_id>(0);
item.fromGate.gateId = gate.get<db_id>(1);
item.fromGate.trackNum = gate.get<int>(2);
item.trackId = gate.get<db_id>(3);
item.fromGate.stationTrackSide = utils::Side(gate.get<int>(3));
item.trackId = gate.get<db_id>(4);
//TODO: should we reset out gate here?
@ -1274,7 +1280,7 @@ bool StopModel::trySetTrackConnections(StopItem &item, db_id trackId, QString *o
if(item.type == StopType::First)
{
//Fake in gate, select one just to set the track
q.prepare("SELECT id,gate_id,gate_track FROM station_gate_connections WHERE track_id=? LIMIT 1");
q.prepare("SELECT id,gate_id,gate_track,track_side FROM station_gate_connections WHERE track_id=? LIMIT 1");
q.bind(1, trackId);
if(q.step() != SQLITE_ROW)
{
@ -1287,9 +1293,10 @@ bool StopModel::trySetTrackConnections(StopItem &item, db_id trackId, QString *o
item.fromGate.gateConnId = gate.get<db_id>(0);
item.fromGate.gateId = gate.get<db_id>(1);
item.fromGate.trackNum = gate.get<int>(2);
item.fromGate.stationTrackSide = utils::Side(gate.get<int>(3));
}
q.prepare("SELECT id FROM station_gate_connections"
q.prepare("SELECT id, track_side FROM station_gate_connections"
" WHERE gate_id=? AND gate_track=? AND track_id=?");
if(item.type != StopType::First)
@ -1303,6 +1310,7 @@ bool StopModel::trySetTrackConnections(StopItem &item, db_id trackId, QString *o
{
//Found a connection
item.fromGate.gateConnId = q.getRows().get<db_id>(0);
item.fromGate.stationTrackSide = utils::Side(q.getRows().get<int>(1));
}else{
if(outErr)
*outErr = tr("Track is not connected to in gate track.\n"
@ -1315,6 +1323,8 @@ bool StopModel::trySetTrackConnections(StopItem &item, db_id trackId, QString *o
if(item.toGate.gateConnId)
{
//For every type of stop, check out gate
//User already chose an out gate but now changed station track
//Check if they are connected together
q.bind(1, item.toGate.gateId);
q.bind(2, item.toGate.trackNum);
q.bind(3, trackId);
@ -1323,6 +1333,7 @@ bool StopModel::trySetTrackConnections(StopItem &item, db_id trackId, QString *o
{
//Found a connection
item.toGate.gateConnId = q.getRows().get<db_id>(0);
item.toGate.stationTrackSide = utils::Side(q.getRows().get<int>(1));
}else{
//Connection not found, inform user and reset out gate
item.toGate = StopItem::Gate{};
@ -1383,7 +1394,7 @@ bool StopModel::trySelectNextSegment(StopItem &item, db_id segmentId, int sugges
{
//Try to find a gate connected to previous track_id
//Prefer suggested gate out track num if possible or lowest one possible
QByteArray sql = "SELECT c.id,c.gate_track,sc.id,sc.%2_track"
QByteArray sql = "SELECT c.id,c.gate_track,c.track_side,sc.id,sc.%2_track"
" FROM station_gate_connections c"
" JOIN railway_connections sc ON sc.seg_id=?3 AND sc.%1_track=c.gate_track"
" WHERE c.track_id=?1 AND c.gate_id=?2"
@ -1409,10 +1420,11 @@ bool StopModel::trySelectNextSegment(StopItem &item, db_id segmentId, int sugges
item.toGate.gateConnId = conn.get<db_id>(0);
item.toGate.gateId = seg_in_gateId;
item.toGate.trackNum = conn.get<int>(1);
item.nextSegment.segConnId = conn.get<db_id>(2);
item.toGate.stationTrackSide = utils::Side(conn.get<int>(2));
item.nextSegment.segConnId = conn.get<db_id>(3);
item.nextSegment.segmentId = segmentId;
item.nextSegment.inTrackNum = item.toGate.trackNum;
item.nextSegment.outTrackNum = conn.get<int>(3);
item.nextSegment.outTrackNum = conn.get<int>(4);
item.nextSegment.reversed = reversed;
}
@ -1668,7 +1680,7 @@ bool StopModel::updateCurrentInGate(StopItem &curStop, const StopItem::Segment &
int segInTrack = r.get<int>(0);
int segOutTrack = r.get<int>(1);
//Segment out gate = next station in gate
//Segment out gate = next station in gate (unless segment is reversed)
curStop.fromGate.gateId = prevSeg.reversed ? segInGate : segOutGate;
curStop.fromGate.trackNum = prevSeg.reversed ? segInTrack : segOutTrack;

View file

@ -7,7 +7,7 @@
#include <QVector>
#include <QSet>
#include "utils/types.h"
#include "stations/station_utils.h"
namespace sqlite3pp {
class database;
@ -20,6 +20,7 @@ struct StopItem
db_id gateConnId = 0;
db_id gateId = 0;
int trackNum = -1;
utils::Side stationTrackSide = utils::Side::NSides;
};
struct Segment