Skip to content

Instantly share code, notes, and snippets.

@kheaactua
Created April 25, 2017 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kheaactua/13e3943e10a25f3df295148b1d2f1f86 to your computer and use it in GitHub Desktop.
Save kheaactua/13e3943e10a25f3df295148b1d2f1f86 to your computer and use it in GitHub Desktop.
ListView not displaying
#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 : */
#ifndef BLOBSMODEL_H_VDJELPI1
#define BLOBSMODEL_H_VDJELPI1
#include <QAbstractListModel>
#include <QList>
#include "app/qobjs/Blob.h"
class BlobModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(unsigned int count READ rowCount NOTIFY countChanged)
public:
enum BlobRoles {
CentroidRole = Qt::UserRole + 1,
ExtentsRole,
IdRole
};
BlobModel(QObject* parent = nullptr);
/** Deletes all the blobs in the container */
virtual ~BlobModel();
/** Adds or updates a blob in the list */
auto addBlob(const BlobPointDataPtr& data) -> void;
/** Returns number of blobs in the list */
auto rowCount(const QModelIndex& parent = QModelIndex()) const -> int;
/** Fetch blob from the list */
auto data(const QModelIndex& index, int role = Qt::DisplayRole) const -> QVariant;
/** Set max number of blobs */
auto setMaxBlobs(const unsigned int n) -> void { max_blobs_ = n; };
/** Set max number of blobs */
auto setMaxBufferPointCapacity(const unsigned int n) -> void { max_buffer_point_capacity_ = n; };
/** Remove rows (blobs) from the model */
auto removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) -> bool;
/**
* Check whether the UUID exists in the list. Returns the index if the
* collection contains the blob, otherwise returns -1 for now found.
*/
auto Contains(const QString& uuid) -> int;
signals:
void countChanged(unsigned int count);
protected:
QHash<int, QByteArray> roleNames() const;
private:
Q_DISABLE_COPY(BlobModel);
/**
* List of blobs.
*/
QList<Blob*> blobs_;
/**
* Maximum allowed number of blob objects at a time.
**/
unsigned int max_blobs_ = 100;
/**
* Max buffer point capacity.
**/
unsigned int max_buffer_point_capacity_ = 1024 * 100;
unsigned int id_count_ = 0;
};
#endif /* end of include guard: BLOBSMODEL_H_VDJELPI1 */
/* vim: set ts=4 sw=4 sts=4 expandtab ffs=unix,dos : */
import QtQuick 2.8
import QtQuick.Layouts 1.3
import QtQuick.Controls 1.4 as Qtc1
import QtQuick.Controls 2.1 as Qtc2
/**
* Info window to display debug data to the user.
**/
Rectangle {
Component {
id: delegate
Row {
spacing: 10
Text { text: "id: " + id + ", loc = " + centroid.x.toFixed(2); color: "white" }
}
}
RowLayout {
anchors.fill: parent
Rectangle {
Layout.fillHeight: true
Layout.preferredWidth: 200
Layout.maximumWidth: 200
color: "transparent"
ColumnLayout {
anchors.fill: parent
Text {
text: "nBlobs = " + sceneGraph.blobs.count
color: 'white';
}
Rectangle {
Layout.fillWidth: true
Layout.minimumWidth: 150
Layout.preferredWidth: 180
Layout.maximumWidth: 200
Layout.minimumHeight: 200
Layout.fillHeight: true
color: Qt.rgba(0.1, 0.1, 0.3, 0.4)
ListView {
id: blobsView
Layout.fillWidth: true
Layout.minimumWidth: 150
Layout.preferredWidth: 180
Layout.maximumWidth: 200
Layout.minimumHeight: 200
Layout.fillHeight: true
anchors.fill: parent
model: sceneGraph.blobs
delegate: delegate
}
}
}
}
}
}
// vim: ts=3 sw=3 sts=0 noet ffs=unix ft=qml :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment