Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "app/qobjs/BlobModel.h"
- #include <QtGlobal>
- BlobModel::BlobModel(QObject* parent)
- : QAbstractListModel(parent) { }
- BlobModel::~BlobModel()
- {
- removeRows(0, rowCount());
- }
- auto BlobModel::Contains(const QString& uuid) -> int
- {
- // Using canonical for here in order to get the index of an element
- for (int i = 0; i<rowCount(); i++)
- {
- auto b = blobs_[i];
- if (b->uuid() == uuid)
- return i;
- }
- return -1;
- }
- auto BlobModel::addBlob(const BlobPointDataPtr& data) -> void
- {
- // Read the ID
- stBlobPointInfo info;
- data->GetBlobPointInfo(info);
- auto uuid = QString::fromStdString(info.uuid);
- const auto idx = Contains(uuid);
- if (-1 != idx)
- {
- blobs_[idx]->Update(data);
- Q_EMIT dataChanged(createIndex(idx, 0), createIndex(idx, 0));
- }
- else
- {
- if (blobs_.size() < static_cast<int>(max_blobs_)) {
- beginInsertRows(QModelIndex(), rowCount(), rowCount());
- blobs_ << new Blob{data, id_count_}; id_count_++;
- endInsertRows(); // responsible for the QQmlChangeSet
- Q_EMIT dataChanged(createIndex(rowCount(), 0), createIndex(rowCount(), 0));
- }
- }
- }
- auto BlobModel::removeRows(int row, int count, const QModelIndex& parent) -> bool
- {
- if (count < 0)
- {
- qFatal("Invalid count provided in BlobModel::removeRows");
- return false;
- }
- if (row + count > rowCount())
- return false;
- if (!count)
- return true;
- beginRemoveRows(parent, row, row+count - 1);
- while (count > 0)
- {
- auto b = blobs_.takeAt(row);
- delete b;
- count--;
- }
- endRemoveRows();
- Q_EMIT countChanged(rowCount());
- return true;
- }
- auto BlobModel::rowCount(const QModelIndex& parent) const -> int
- {
- Q_UNUSED(parent);
- return blobs_.size();
- }
- auto BlobModel::data(const QModelIndex& index, int role) const -> QVariant
- {
- if(!index.isValid())
- {
- qWarning("Invalid index used.");
- return QVariant();
- }
- if (index.row() < 0 || index.row() >= blobs_.count())
- {
- qWarning("Invalid row requested.");
- return QVariant();
- }
- auto blob = blobs_[index.row()];
- if (role == CentroidRole)
- return blob->centroid();
- else if (role == IdRole)
- return blob->id();
- else if (role == ExtentsRole)
- return blob->extents();
- return QVariant();
- }
- auto BlobModel::roleNames() const -> QHash<int, QByteArray>
- {
- QHash<int, QByteArray> roles;
- roles[CentroidRole] = "centroid";
- roles[ExtentsRole] = "extents";
- roles[IdRole] = "id";
- return roles;
- }
- /* vim: set ts=4 sw=4 sts=4 expandtab ffs=unix,dos : */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement