#include <QAbstractProxyModel> #include <QMap>
class ImageLandmarkTableModel;
{
Q_OBJECT
public:
ImageLandmarkProxyModel
(QObject *parent
=0);
virtual void setSourceModel(ImageLandmarkTableModel *sourceModel);
ImageLandmarkTableModel* sourceModel() const;
protected slots:
void countRowsInserted
(const QModelIndex &parent,
int srcStart,
int srcEnd
);
void countRowsRemoved
(const QModelIndex &parent,
int srcStart,
int srcEnd
);
private:
struct ProxyLink {
int sourceRow; // Source row
QList<int> points; // List of source rows from the childs
};
typedef QMap<int,ProxyLink> MappingData;
MappingData m_mapping;
};
#include <QAbstractProxyModel> #include <QMap>
class ImageLandmarkTableModel;
class ImageLandmarkProxyModel : public QAbstractProxyModel
{
Q_OBJECT
public:
ImageLandmarkProxyModel(QObject *parent=0);
virtual QModelIndex index(int row, int column,
const QModelIndex &parent=QModelIndex()) const;
virtual QModelIndex parent(const QModelIndex &index) const;
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const;
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const;
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
virtual void setSourceModel(ImageLandmarkTableModel *sourceModel);
ImageLandmarkTableModel* sourceModel() const;
QVariant data(const QModelIndex &index, int role) const;
protected slots:
void countRowsInserted(const QModelIndex &parent, int srcStart, int srcEnd);
void countRowsRemoved(const QModelIndex &parent, int srcStart, int srcEnd);
private:
struct ProxyLink {
int sourceRow; // Source row
QList<int> points; // List of source rows from the childs
};
typedef QMap<int,ProxyLink> MappingData;
MappingData m_mapping;
};
To copy to clipboard, switch view to plain text mode
#include "imagelandmarkproxymodel.h" #include <QtSql>
#include "imagelandmarktablemodel.h"
ImageLandmarkProxyModel
::ImageLandmarkProxyModel(QObject *parent
) {
}
QModelIndex ImageLandmarkProxyModel
::index(int row,
int column,
{
if(!parent.isValid()) // Root
return createIndex(row, column);
int id=m_mapping.keys().at(parent.row());
const ProxyLink &link=m_mapping[id];
return createIndex(link.points.at(row), column);
}
{
}
int ImageLandmarkProxyModel
::rowCount(const QModelIndex &parent
) const {
const MappingData &m=m_mapping;
if(parent.isValid())
{
bool ok;
int id=parent.data().toInt(&ok); Q_ASSERT(ok);
return m[id].points.count();
}
return m.count();
}
int ImageLandmarkProxyModel
::columnCount(const QModelIndex &parent
) const {
return 1;
}
{
if(parent.isValid()){
int parentRow=parent.row();
int id=m_mapping.keys().at(parentRow);
const QList<int> &points=m_mapping[id].points;
return sourceModel()->index(points.at(proxyIndex.row()), 1);
}
int row=proxyIndex.row();
int id=m_mapping.keys().at(row);
const ProxyLink &link=m_mapping[id];
return sourceModel()->index(link.sourceRow, 0);
}
{
return index(sourceIndex.row(), sourceIndex.column());
}
void ImageLandmarkProxyModel::setSourceModel(
ImageLandmarkTableModel *sourceModel)
{
disconnect();
connect(sourceModel,
SIGNAL(rowsInserted
(QModelIndex,
int,
int)),
connect(sourceModel,
SIGNAL(rowsRemoved
(QModelIndex,
int,
int)),
}
ImageLandmarkTableModel* ImageLandmarkProxyModel::sourceModel() const
{
return static_cast<ImageLandmarkTableModel*>(ptr);
}
const
{
if(parent.isValid())
return mapToSource(index).data(role);
}
void ImageLandmarkProxyModel
::countRowsInserted(const QModelIndex &parent,
int srcStart, int srcEnd)
{
const int idImageColumn=sourceModel()->column(
ImageLandmarkTableModel::IdImage);
const int idPointColumn=sourceModel()->column(
ImageLandmarkTableModel::IdPoint);
MappingData &m=m_mapping;
for(int i=srcStart;i<=srcEnd;i++)
{
QModelIndex col1
=sourceModel
()->index
(i, idImageColumn
);
QModelIndex col2
=sourceModel
()->index
(i, idPointColumn
);
bool ok;
int id=col1.data().toInt(&ok);
MappingData::iterator it=m.find(id);
Q_ASSERT(ok);
if(it!=m.end()) it->points.append(i);
else if(!col2.data().isNull()){
ProxyLink proxyLink;
proxyLink.sourceRow=i;
proxyLink.points.append(i);
m.insert(id, proxyLink);
}
else{
ProxyLink proxyLink;
proxyLink.sourceRow=i;
m.insert(id, proxyLink);
}
}
}
void ImageLandmarkProxyModel
::countRowsRemoved(const QModelIndex &parent,
int srcStart,
int srcEnd
) {
// TODO: Do this slot
qCritical()<<"ImageLandmarkProxyModel::countRowsRemoved"<<"Unimplemented slot";
}
#include "imagelandmarkproxymodel.h" #include <QtSql>
#include "imagelandmarktablemodel.h"
ImageLandmarkProxyModel::ImageLandmarkProxyModel(QObject *parent)
: QAbstractProxyModel(parent)
{
}
QModelIndex ImageLandmarkProxyModel::index(int row, int column,
const QModelIndex &parent) const
{
if(!parent.isValid()) // Root
return createIndex(row, column);
int id=m_mapping.keys().at(parent.row());
const ProxyLink &link=m_mapping[id];
return createIndex(link.points.at(row), column);
}
QModelIndex ImageLandmarkProxyModel::parent(const QModelIndex &index) const
{
return QModelIndex();
}
int ImageLandmarkProxyModel::rowCount(const QModelIndex &parent) const
{
const MappingData &m=m_mapping;
if(parent.isValid())
{
bool ok;
int id=parent.data().toInt(&ok); Q_ASSERT(ok);
return m[id].points.count();
}
return m.count();
}
int ImageLandmarkProxyModel::columnCount(const QModelIndex &parent) const
{
return 1;
}
QModelIndex ImageLandmarkProxyModel::mapToSource(const QModelIndex &proxyIndex) const
{
QModelIndex parent=proxyIndex.parent();
if(parent.isValid()){
int parentRow=parent.row();
int id=m_mapping.keys().at(parentRow);
const QList<int> &points=m_mapping[id].points;
return sourceModel()->index(points.at(proxyIndex.row()), 1);
}
int row=proxyIndex.row();
int id=m_mapping.keys().at(row);
const ProxyLink &link=m_mapping[id];
return sourceModel()->index(link.sourceRow, 0);
}
QModelIndex ImageLandmarkProxyModel::mapFromSource(
const QModelIndex &sourceIndex) const
{
return index(sourceIndex.row(), sourceIndex.column());
}
void ImageLandmarkProxyModel::setSourceModel(
ImageLandmarkTableModel *sourceModel)
{
disconnect();
QAbstractProxyModel::setSourceModel(sourceModel);
connect(sourceModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
SLOT(countRowsInserted(QModelIndex,int,int)));
connect(sourceModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
SLOT(countRowsRemoved(QModelIndex,int,int)));
}
ImageLandmarkTableModel* ImageLandmarkProxyModel::sourceModel() const
{
QAbstractItemModel *ptr=QAbstractProxyModel::sourceModel();
return static_cast<ImageLandmarkTableModel*>(ptr);
}
QVariant ImageLandmarkProxyModel::data(const QModelIndex &index, int role)
const
{
if(!index.isValid()) return QVariant();
QModelIndex parent=index.parent();
if(parent.isValid())
return mapToSource(index).data(role);
}
void ImageLandmarkProxyModel::countRowsInserted(const QModelIndex &parent,
int srcStart, int srcEnd)
{
const int idImageColumn=sourceModel()->column(
ImageLandmarkTableModel::IdImage);
const int idPointColumn=sourceModel()->column(
ImageLandmarkTableModel::IdPoint);
MappingData &m=m_mapping;
for(int i=srcStart;i<=srcEnd;i++)
{
QModelIndex col1=sourceModel()->index(i, idImageColumn);
QModelIndex col2=sourceModel()->index(i, idPointColumn);
bool ok;
int id=col1.data().toInt(&ok);
MappingData::iterator it=m.find(id);
Q_ASSERT(ok);
if(it!=m.end()) it->points.append(i);
else if(!col2.data().isNull()){
ProxyLink proxyLink;
proxyLink.sourceRow=i;
proxyLink.points.append(i);
m.insert(id, proxyLink);
}
else{
ProxyLink proxyLink;
proxyLink.sourceRow=i;
m.insert(id, proxyLink);
}
}
}
void ImageLandmarkProxyModel::countRowsRemoved(const QModelIndex &parent, int srcStart, int srcEnd)
{
// TODO: Do this slot
qCritical()<<"ImageLandmarkProxyModel::countRowsRemoved"<<"Unimplemented slot";
}
To copy to clipboard, switch view to plain text mode
Bookmarks