2018-03-05 17:40:30 +08:00
|
|
|
/* proxymodel.cpp
|
|
|
|
|
* Copyright (C) 2018 Tianjin KYLIN Information Technology Co., Ltd.
|
2018-03-05 16:58:46 +08:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
|
* any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
|
|
|
* 02110-1301, USA.
|
|
|
|
|
**/
|
2018-03-05 17:40:30 +08:00
|
|
|
#include "proxymodel.h"
|
2018-02-08 17:48:39 +08:00
|
|
|
#include <QDebug>
|
2018-03-05 17:40:30 +08:00
|
|
|
ProxyModel::ProxyModel(QObject *parent)
|
2018-02-08 17:48:39 +08:00
|
|
|
: QAbstractListModel(parent),
|
2018-05-25 14:22:01 +08:00
|
|
|
m_model(nullptr),
|
2018-02-08 17:48:39 +08:00
|
|
|
m_extraModel(new QStandardItemModel(this))
|
|
|
|
|
{
|
|
|
|
|
connect(m_extraModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
|
|
|
|
|
this, SLOT(onExtraRowsInserted(const QModelIndex&, int, int)));
|
|
|
|
|
connect(m_extraModel, SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
|
|
|
|
|
this, SLOT(onExtraRowsRemoved(const QModelIndex&, int, int)));
|
|
|
|
|
connect(m_extraModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
|
|
|
|
|
this, SLOT(onExtraDataChanged(const QModelIndex&, const QModelIndex&)));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 17:40:30 +08:00
|
|
|
QVariant ProxyModel::data(const QModelIndex &index, int role) const
|
2018-02-08 17:48:39 +08:00
|
|
|
{
|
|
|
|
|
if(index.row() < sourceRowCount())
|
2018-05-25 14:22:01 +08:00
|
|
|
return m_model->index(index.row(), 0).data(role);
|
2018-02-08 17:48:39 +08:00
|
|
|
else
|
|
|
|
|
return m_extraModel->index(index.row() - sourceRowCount(), 0).data(role);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 17:40:30 +08:00
|
|
|
int ProxyModel::rowCount(const QModelIndex &parent) const
|
2018-02-08 17:48:39 +08:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
return sourceRowCount() + m_extraModel->rowCount();
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-25 14:22:01 +08:00
|
|
|
QHash<int, QByteArray> ProxyModel::roleNames() const
|
|
|
|
|
{
|
|
|
|
|
return m_model == nullptr ? QHash<int, QByteArray>() : m_model->roleNames();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 17:40:30 +08:00
|
|
|
QStandardItemModel* ProxyModel::extraRowModel()
|
2018-02-08 17:48:39 +08:00
|
|
|
{
|
|
|
|
|
return m_extraModel;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 17:40:30 +08:00
|
|
|
void ProxyModel::setSourceModel(QAbstractListModel *sourceModel)
|
2018-02-08 17:48:39 +08:00
|
|
|
{
|
2018-05-25 14:22:01 +08:00
|
|
|
if(m_model)
|
2018-02-08 17:48:39 +08:00
|
|
|
{
|
2018-05-25 14:22:01 +08:00
|
|
|
disconnect(m_model, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
|
2018-02-08 17:48:39 +08:00
|
|
|
this, SLOT(onSourceRowsInserted(const QModelIndex&, int, int)));
|
2018-05-25 14:22:01 +08:00
|
|
|
disconnect(m_model, SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
|
2018-02-08 17:48:39 +08:00
|
|
|
this, SLOT(onSourceRowsRemoved(const QModelIndex&, int, int)));
|
2018-05-25 14:22:01 +08:00
|
|
|
disconnect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
|
2018-02-08 17:48:39 +08:00
|
|
|
this, SLOT(onSourceDataChanged(const QModelIndex&, const QModelIndex&)));
|
|
|
|
|
|
|
|
|
|
}
|
2018-05-25 14:22:01 +08:00
|
|
|
// m_model = QWeakPointer<QAbstractListModel>(sourceModel);
|
|
|
|
|
m_model = sourceModel;
|
|
|
|
|
// reset();
|
|
|
|
|
beginResetModel();
|
|
|
|
|
endResetModel();
|
2018-02-08 17:48:39 +08:00
|
|
|
|
2018-05-25 14:22:01 +08:00
|
|
|
// setRoleNames(m_model->roleNames());
|
|
|
|
|
|
|
|
|
|
connect(m_model, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
|
2018-02-08 17:48:39 +08:00
|
|
|
this, SLOT(onSourceRowsInserted(const QModelIndex&, int, int)));
|
2018-05-25 14:22:01 +08:00
|
|
|
connect(m_model, SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
|
2018-02-08 17:48:39 +08:00
|
|
|
this, SLOT(onSourceRowsRemoved(const QModelIndex&, int, int)));
|
2018-05-25 14:22:01 +08:00
|
|
|
connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
|
2018-02-08 17:48:39 +08:00
|
|
|
this, SLOT(onSourceDataChanged(const QModelIndex&, const QModelIndex&)));
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 17:40:30 +08:00
|
|
|
int ProxyModel::sourceRowCount() const
|
2018-02-08 17:48:39 +08:00
|
|
|
{
|
2018-05-25 14:22:01 +08:00
|
|
|
return m_model == nullptr ? 0 : m_model->rowCount();
|
2018-02-08 17:48:39 +08:00
|
|
|
}
|
|
|
|
|
|
2018-03-05 17:40:30 +08:00
|
|
|
void ProxyModel::onSourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
|
2018-02-08 17:48:39 +08:00
|
|
|
{
|
|
|
|
|
dataChanged(createIndex(topLeft.row(), 0), createIndex(bottomRight.row(), 0));
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 17:40:30 +08:00
|
|
|
void ProxyModel::onSourceRowsInserted(const QModelIndex &parent, int start, int end)
|
2018-02-08 17:48:39 +08:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
beginInsertRows(parent, start, end);
|
|
|
|
|
endInsertRows();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 17:40:30 +08:00
|
|
|
void ProxyModel::onSourceRowsRemoved(const QModelIndex &parent, int start, int end)
|
2018-02-08 17:48:39 +08:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
beginRemoveRows(parent, start, end);
|
|
|
|
|
endRemoveRows();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 17:40:30 +08:00
|
|
|
void ProxyModel::onExtraDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
|
2018-02-08 17:48:39 +08:00
|
|
|
{
|
|
|
|
|
dataChanged(createIndex(sourceRowCount() + topLeft.row(), 0), createIndex(sourceRowCount() + bottomRight.row(), 0));
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 17:40:30 +08:00
|
|
|
void ProxyModel::onExtraRowsInserted(const QModelIndex &parent, int start, int end)
|
2018-02-08 17:48:39 +08:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
beginInsertRows(parent, sourceRowCount() + start, sourceRowCount() + end);
|
|
|
|
|
endInsertRows();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 17:40:30 +08:00
|
|
|
void ProxyModel::onExtraRowsRemoved(const QModelIndex &parent, int start, int end)
|
2018-02-08 17:48:39 +08:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
beginRemoveRows(parent, sourceRowCount() + start, sourceRowCount() + end);
|
|
|
|
|
endRemoveRows();
|
|
|
|
|
}
|