Skip to content
This repository has been archived by the owner on Jul 22, 2021. It is now read-only.

Commit

Permalink
Put Connection behind QSharedPointer.
Browse files Browse the repository at this point in the history
In a later stage, we need to store multiple references to shared
`Connection`s.
  • Loading branch information
rubdos committed Dec 9, 2019
1 parent 9b64d39 commit ea665ea
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ Liveboard *API::parseLiveboard(QJsonObject json)
ConnectionListModel* API::parseConnections(QJsonObject json)
{
qDebug() << "Parsing connections";
QList<Connection*> connectionList;
QList<QSharedPointer<Connection>> connectionList;
QJsonArray connectionArray = json["connection"].toArray();
QDateTime timestampConnections;
timestampConnections.setTime_t(json["timestamp"].toString().toInt());
Expand Down Expand Up @@ -827,7 +827,7 @@ ConnectionListModel* API::parseConnections(QJsonObject json)

IRail::Occupancy connectionOccupancy = this->parseOccupancy(connectionOccupancyObj["name"].toString());
// TO DO: enable disturbances and remarks for the whole connection
Connection* connection = new Connection(connectionId, fromStop, toStop, fromVehicleId, toVehicleId, disturbancesConnection, remarksConnection, connectionOccupancy, connectionDuration, new ViaListModel(viaList), timestampConnections);
QSharedPointer<Connection> connection(new Connection(connectionId, fromStop, toStop, fromVehicleId, toVehicleId, disturbancesConnection, remarksConnection, connectionOccupancy, connectionDuration, new ViaListModel(viaList), timestampConnections));
connectionList.append(connection);
qDebug() << "CONNECTION:";
qDebug() << "\tFrom:" << connection->from()->station()->name();
Expand Down
12 changes: 4 additions & 8 deletions src/models/connectionlistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@
* along with BeRail. If not, see <http://www.gnu.org/licenses/>.
*/
#include "connectionlistmodel.h"
#include <QSharedPointer>

ConnectionListModel::ConnectionListModel(QList<Connection *> connectionList)
ConnectionListModel::ConnectionListModel(QList<QSharedPointer<Connection>> connectionList)
{
this->setConnectionList(connectionList);
}

ConnectionListModel::~ConnectionListModel()
{
if(!this->connectionList().isEmpty()) {
foreach(Connection* item, this->connectionList()) {
item->deleteLater();
}
}
}

int ConnectionListModel::rowCount(const QModelIndex &) const
Expand Down Expand Up @@ -136,12 +132,12 @@ QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
}
}

QList<Connection *> ConnectionListModel::connectionList() const
QList<QSharedPointer<Connection>> ConnectionListModel::connectionList() const
{
return m_connectionList;
}

void ConnectionListModel::setConnectionList(const QList<Connection *> &connectionList)
void ConnectionListModel::setConnectionList(const QList<QSharedPointer<Connection>> &connectionList)
{
m_connectionList = connectionList;
}
Expand Down
8 changes: 4 additions & 4 deletions src/models/connectionlistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ class ConnectionListModel : public QAbstractListModel
TimestampRole = Qt::UserRole + 11
};

explicit ConnectionListModel(QList<Connection *> connectionList);
explicit ConnectionListModel(QList<QSharedPointer<Connection>> connectionList);
explicit ConnectionListModel();
~ConnectionListModel();

virtual int rowCount(const QModelIndex&) const;
virtual QVariant data(const QModelIndex &index, int role) const;
QList<Connection *> connectionList() const;
void setConnectionList(const QList<Connection *> &connectionList);
QList<QSharedPointer<Connection>> connectionList() const;
void setConnectionList(const QList<QSharedPointer<Connection>> &connectionList);

Station *from() const;
void setFrom(Station *from);
Expand All @@ -65,7 +65,7 @@ class ConnectionListModel : public QAbstractListModel
QHash<int, QByteArray> roleNames() const;

private:
QList<Connection *> m_connectionList;
QList<QSharedPointer<Connection>> m_connectionList;
Station* m_from;
Station* m_to;
QDateTime m_time;
Expand Down

0 comments on commit ea665ea

Please sign in to comment.