Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. #include "app/qobjs/BlobModel.h"
  2.  
  3. #include <QtGlobal>
  4.  
  5. BlobModel::BlobModel(QObject* parent)
  6. : QAbstractListModel(parent) { }
  7.  
  8. BlobModel::~BlobModel()
  9. {
  10. removeRows(0, rowCount());
  11. }
  12.  
  13. auto BlobModel::Contains(const QString& uuid) -> int
  14. {
  15. // Using canonical for here in order to get the index of an element
  16. for (int i = 0; i<rowCount(); i++)
  17. {
  18. auto b = blobs_[i];
  19. if (b->uuid() == uuid)
  20. return i;
  21. }
  22.  
  23. return -1;
  24. }
  25.  
  26. auto BlobModel::addBlob(const BlobPointDataPtr& data) -> void
  27. {
  28. // Read the ID
  29. stBlobPointInfo info;
  30. data->GetBlobPointInfo(info);
  31. auto uuid = QString::fromStdString(info.uuid);
  32.  
  33. const auto idx = Contains(uuid);
  34. if (-1 != idx)
  35. {
  36. blobs_[idx]->Update(data);
  37. Q_EMIT dataChanged(createIndex(idx, 0), createIndex(idx, 0));
  38. }
  39. else
  40. {
  41. if (blobs_.size() < static_cast<int>(max_blobs_)) {
  42.  
  43. beginInsertRows(QModelIndex(), rowCount(), rowCount());
  44. blobs_ << new Blob{data, id_count_}; id_count_++;
  45. endInsertRows(); // responsible for the QQmlChangeSet
  46.  
  47. Q_EMIT dataChanged(createIndex(rowCount(), 0), createIndex(rowCount(), 0));
  48. }
  49. }
  50. }
  51.  
  52. auto BlobModel::removeRows(int row, int count, const QModelIndex& parent) -> bool
  53. {
  54. if (count < 0)
  55. {
  56. qFatal("Invalid count provided in BlobModel::removeRows");
  57. return false;
  58. }
  59. if (row + count > rowCount())
  60. return false;
  61. if (!count)
  62. return true;
  63.  
  64. beginRemoveRows(parent, row, row+count - 1);
  65. while (count > 0)
  66. {
  67. auto b = blobs_.takeAt(row);
  68. delete b;
  69. count--;
  70. }
  71. endRemoveRows();
  72.  
  73. Q_EMIT countChanged(rowCount());
  74.  
  75. return true;
  76. }
  77.  
  78.  
  79. auto BlobModel::rowCount(const QModelIndex& parent) const -> int
  80. {
  81. Q_UNUSED(parent);
  82. return blobs_.size();
  83. }
  84.  
  85. auto BlobModel::data(const QModelIndex& index, int role) const -> QVariant
  86. {
  87. if(!index.isValid())
  88. {
  89. qWarning("Invalid index used.");
  90. return QVariant();
  91. }
  92.  
  93. if (index.row() < 0 || index.row() >= blobs_.count())
  94. {
  95. qWarning("Invalid row requested.");
  96. return QVariant();
  97. }
  98.  
  99. auto blob = blobs_[index.row()];
  100.  
  101. if (role == CentroidRole)
  102. return blob->centroid();
  103. else if (role == IdRole)
  104. return blob->id();
  105. else if (role == ExtentsRole)
  106. return blob->extents();
  107. return QVariant();
  108. }
  109.  
  110. auto BlobModel::roleNames() const -> QHash<int, QByteArray>
  111. {
  112. QHash<int, QByteArray> roles;
  113. roles[CentroidRole] = "centroid";
  114. roles[ExtentsRole] = "extents";
  115. roles[IdRole] = "id";
  116. return roles;
  117. }
  118.  
  119. /* vim: set ts=4 sw=4 sts=4 expandtab ffs=unix,dos : */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement