19
19
#include <extrarowproxymodel.h>
22
#include <QAbstractItemModel>
23
24
ExtraRowProxyModel::ExtraRowProxyModel(QObject* parent)
24
25
: QAbstractListModel(parent)
28
28
int ExtraRowProxyModel::appendRow()
31
31
m_rows.append(row);
32
32
return m_rows.count() - 1;
46
46
void ExtraRowProxyModel::setSourceModel(QAbstractItemModel* model)
48
if (! m_model.isNull()) {
49
disconnect(m_model.data(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(onSourceRowsInserted(QModelIndex,int,int)));
50
disconnect(m_model.data(), SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(onSourceRowsRemoved(QModelIndex,int,int)));
51
disconnect(m_model.data(), SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(onSourceDataChanged(QModelIndex,QModelIndex)));
54
m_model = QWeakPointer<QAbstractItemModel>(model);
50
setRoleNames(m_model->roleNames());
56
setRoleNames(m_model.data()->roleNames());
58
connect(m_model.data(), SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(onSourceRowsInserted(QModelIndex,int,int)));
59
connect(m_model.data(), SIGNAL(rowsRemoved(QModelIndex, int, int)), SLOT(onSourceRowsRemoved(QModelIndex,int,int)));
60
connect(m_model.data(), SIGNAL(dataChanged(QModelIndex, QModelIndex)), SLOT(onSourceDataChanged(QModelIndex,QModelIndex)));
53
63
int ExtraRowProxyModel::rowCount(const QModelIndex &) const
55
return m_model->rowCount() + m_rows.count();
65
if (m_model.isNull()) {
66
return m_rows.count();
68
return m_model.data()->rowCount() + m_rows.count();
58
73
QVariant ExtraRowProxyModel::data(const QModelIndex &index, int role) const
60
int count = m_model->rowCount();
61
if (index.row() < count) {
62
return m_model->data(index, role);
76
if (!m_model.isNull()) {
77
count = m_model.data()->rowCount();
78
if (index.row() < count) {
79
return m_model.data()->data(index, role);
65
83
int extraRow = index.row() - count;
70
const Item& item = m_rows[extraRow].at(index.column());
88
const Item& item = m_rows[extraRow][index.column()];
71
89
return item.value(role);
92
void ExtraRowProxyModel::onSourceRowsInserted(const QModelIndex &parent, int start, int end)
95
beginInsertRows(QModelIndex(), start, end);
99
void ExtraRowProxyModel::onSourceRowsRemoved(const QModelIndex &parent, int start, int end)
102
beginRemoveRows(QModelIndex(), start, end);
106
void ExtraRowProxyModel::onSourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
108
dataChanged(createIndex(topLeft.row(), 0) , createIndex(bottomRight.row(), 0));