// visitormodel.h
{
Q_OBJECT
Q_PROPERTY(int count READ getCount() NOTIFY countChanged())
public:
explicit VisitorModel
(VisitorItem
* prototype,
QObject* parent
= 0);
~VisitorModel();
void appendRow(VisitorItem* item);
void appendRows(const QList<VisitorItem*> &items);
void insertRow(int row, VisitorItem* item);
VisitorItem* takeRow(int row);
VisitorItem
* find
(const QString &id
) const;
QModelIndex indexFromItem
( const VisitorItem
* item
) const;
void clear();
int getCount() const;
private slots:
void handleItemChange();
public slots:
void anprCompleted(const QString& licensePlateNumber, const QDateTime& timestamp);
void visitorImageReceived(const QString& vehicleImageSource, const QDateTime& timestamp);
void anprFailed(const QDateTime& timestamp);
void vinScanned(int modelIndex, const QString& vin, const QDateTime& timestamp);
signals:
void visitorAdded();
void countChanged();
private:
VisitorItem* m_prototype;
QList<VisitorItem*> m_list;
int m_count;
};
// visitormodel.h
class VisitorModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(int count READ getCount() NOTIFY countChanged())
public:
explicit VisitorModel(VisitorItem* prototype, QObject* parent = 0);
~VisitorModel();
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
void appendRow(VisitorItem* item);
void appendRows(const QList<VisitorItem*> &items);
void insertRow(int row, VisitorItem* item);
bool removeRow(int row, const QModelIndex &parent = QModelIndex());
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
VisitorItem* takeRow(int row);
VisitorItem* find(const QString &id) const;
QModelIndex indexFromItem( const VisitorItem* item) const;
void clear();
int getCount() const;
Q_INVOKABLE QVariant get(int row);
private slots:
void handleItemChange();
public slots:
void anprCompleted(const QString& licensePlateNumber, const QDateTime& timestamp);
void visitorImageReceived(const QString& vehicleImageSource, const QDateTime& timestamp);
void anprFailed(const QDateTime& timestamp);
void vinScanned(int modelIndex, const QString& vin, const QDateTime& timestamp);
signals:
void visitorAdded();
void countChanged();
private:
VisitorItem* m_prototype;
QList<VisitorItem*> m_list;
int m_count;
};
To copy to clipboard, switch view to plain text mode
// visitormodel.cpp
#include "visitormodel.h"
VisitorModel
::VisitorModel(VisitorItem
* prototype,
QObject *parent
) :{
setRoleNames(m_prototype->roleNames());
}
int VisitorModel
::rowCount(const QModelIndex &parent
) const {
Q_UNUSED(parent);
return m_list.size();
}
{
if(index.row() < 0 || index.row() >= m_list.size())
return m_list.at(index.row())->data(role);
}
VisitorModel::~VisitorModel()
{
delete m_prototype;
clear();
}
void VisitorModel::appendRow(VisitorItem *item)
{
appendRows(QList<VisitorItem*>() << item);
}
void VisitorModel::appendRows(const QList<VisitorItem *> &items)
{
beginInsertRows
(QModelIndex(), rowCount
(), rowCount
()+items.
size()-1);
foreach(VisitorItem *item, items)
{
connect(item, SIGNAL(dataChanged()), SLOT(handleItemChange()));
m_list.append(item);
emit visitorAdded();
emit countChanged();
}
endInsertRows();
}
void VisitorModel::insertRow(int row, VisitorItem *item)
{
connect(item, SIGNAL(dataChanged()), SLOT(handleItemChange()));
m_list.insert(row, item);
emit countChanged();
endInsertRows();
}
void VisitorModel::handleItemChange()
{
VisitorItem* item = static_cast<VisitorItem*>(sender());
if(index.isValid()) {
qDebug() << "Emitting data changed for " << index;
emit dataChanged(index, index);
}
}
void VisitorModel
::vinScanned(int modelIndex,
const QString &vin,
const QDateTime ×tamp
) {
qDebug() << "Got vinScanned with " << modelIndex << vin << timestamp;
m_list.at(modelIndex)->setVehicleVin(vin);
}
VisitorItem
* VisitorModel
::find(const QString &id
) const{
foreach(VisitorItem* item, m_list)
{
if(item->id() == id) return item;
}
return 0;
}
QModelIndex VisitorModel
::indexFromItem(const VisitorItem
*item
) const {
Q_ASSERT(item);
for(int row=0; row<m_list.size(); ++row)
{
if(m_list.at(row) == item) return index(row);
}
}
void VisitorModel::clear()
{
qDeleteAll(m_list);
m_list.clear();
emit countChanged();
}
int VisitorModel::getCount() const
{
return rowCount();
}
{
if(row >= 0)
{
VisitorItem * item = m_list.at(row);
QMap<QString, QVariant> itemData;
QHashIterator<int, QByteArray> hashItr(item->roleNames());
while(hashItr.hasNext())
{
hashItr.next();
itemData.insert(hashItr.value(),item->data(hashItr.key()).toString());
}
}
}
bool VisitorModel
::removeRow(int row,
const QModelIndex &parent
) {
Q_UNUSED(parent);
if(row < 0 || row >= m_list.size()) return false;
delete m_list.takeAt(row);
endRemoveRows();
return true;
}
bool VisitorModel
::removeRows(int row,
int count,
const QModelIndex &parent
) {
Q_UNUSED(parent);
if(row < 0 || (row+count) >= m_list.size()) return false;
for(int i=0; i<count; ++i)
{
delete m_list.takeAt(row);
}
endRemoveRows();
return true;
}
VisitorItem * VisitorModel::takeRow(int row)
{
VisitorItem* item = m_list.takeAt(row);
emit countChanged();
endRemoveRows();
return item;
}
// visitormodel.cpp
#include "visitormodel.h"
VisitorModel::VisitorModel(VisitorItem* prototype, QObject *parent) :
QAbstractListModel(parent), m_prototype(prototype)
{
setRoleNames(m_prototype->roleNames());
}
int VisitorModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_list.size();
}
QVariant VisitorModel::data(const QModelIndex &index, int role) const
{
if(index.row() < 0 || index.row() >= m_list.size())
return QVariant();
return m_list.at(index.row())->data(role);
}
VisitorModel::~VisitorModel()
{
delete m_prototype;
clear();
}
void VisitorModel::appendRow(VisitorItem *item)
{
appendRows(QList<VisitorItem*>() << item);
}
void VisitorModel::appendRows(const QList<VisitorItem *> &items)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount()+items.size()-1);
foreach(VisitorItem *item, items)
{
connect(item, SIGNAL(dataChanged()), SLOT(handleItemChange()));
m_list.append(item);
emit visitorAdded();
emit countChanged();
}
endInsertRows();
}
void VisitorModel::insertRow(int row, VisitorItem *item)
{
beginInsertRows(QModelIndex(), row, row);
connect(item, SIGNAL(dataChanged()), SLOT(handleItemChange()));
m_list.insert(row, item);
emit countChanged();
endInsertRows();
}
void VisitorModel::handleItemChange()
{
VisitorItem* item = static_cast<VisitorItem*>(sender());
QModelIndex index = indexFromItem(item);
if(index.isValid()) {
qDebug() << "Emitting data changed for " << index;
emit dataChanged(index, index);
}
}
void VisitorModel::vinScanned(int modelIndex, const QString &vin, const QDateTime ×tamp)
{
qDebug() << "Got vinScanned with " << modelIndex << vin << timestamp;
m_list.at(modelIndex)->setVehicleVin(vin);
}
VisitorItem * VisitorModel::find(const QString &id) const
{
foreach(VisitorItem* item, m_list)
{
if(item->id() == id) return item;
}
return 0;
}
QModelIndex VisitorModel::indexFromItem(const VisitorItem *item) const
{
Q_ASSERT(item);
for(int row=0; row<m_list.size(); ++row)
{
if(m_list.at(row) == item) return index(row);
}
return QModelIndex();
}
void VisitorModel::clear()
{
qDeleteAll(m_list);
m_list.clear();
emit countChanged();
}
int VisitorModel::getCount() const
{
return rowCount();
}
QVariant VisitorModel::get(int row)
{
if(row >= 0)
{
VisitorItem * item = m_list.at(row);
QMap<QString, QVariant> itemData;
QHashIterator<int, QByteArray> hashItr(item->roleNames());
while(hashItr.hasNext())
{
hashItr.next();
itemData.insert(hashItr.value(),item->data(hashItr.key()).toString());
}
return QVariant(itemData);
}
return QVariant();
}
bool VisitorModel::removeRow(int row, const QModelIndex &parent)
{
Q_UNUSED(parent);
if(row < 0 || row >= m_list.size()) return false;
beginRemoveRows(QModelIndex(), row, row);
delete m_list.takeAt(row);
endRemoveRows();
return true;
}
bool VisitorModel::removeRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent);
if(row < 0 || (row+count) >= m_list.size()) return false;
beginRemoveRows(QModelIndex(), row, row+count-1);
for(int i=0; i<count; ++i)
{
delete m_list.takeAt(row);
}
endRemoveRows();
return true;
}
VisitorItem * VisitorModel::takeRow(int row)
{
beginRemoveRows(QModelIndex(), row, row);
VisitorItem* item = m_list.takeAt(row);
emit countChanged();
endRemoveRows();
return item;
}
To copy to clipboard, switch view to plain text mode
Bookmarks