Introduce the ColumnProxyModel

This component will let the developer access different parts of the
QAbstractItemModel implementations that are impossible to be accessed
from QML. i.e. columns!=0 and the tree branches.

REVIEW: 106272
This commit is contained in:
Aleix Pol 2012-09-04 01:18:55 +02:00
parent 24f7593788
commit b8b474068e
7 changed files with 422 additions and 1 deletions

View File

@ -2,12 +2,15 @@ project(qtextracomponents)
include(KDE4Defaults)
add_subdirectory(tests)
set(qtextracomponents_SRCS
qtextracomponentsplugin.cpp
qpixmapitem.cpp
qimageitem.cpp
qiconitem.cpp
mouseeventlistener.cpp
columnproxymodel.cpp
)
INCLUDE_DIRECTORIES(

View File

@ -0,0 +1,236 @@
/*
* Copyright 2012 by Aleix Pol Gonzalez <aleixpol@blue-systems.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library 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 Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "columnproxymodel.h"
#include <KDebug>
ColumnProxyModel::ColumnProxyModel(QObject* parent)
: QAbstractListModel(parent)
, m_column(0)
, m_sourceModel(0)
{}
void ColumnProxyModel::setSourceModel(QAbstractItemModel* sourceModel)
{
if(sourceModel==m_sourceModel) {
return;
}
beginResetModel();
if(m_sourceModel) {
disconnect(m_sourceModel, SIGNAL(destroyed(QObject*)),
this, SLOT(sourceDestroyed(QObject*)));
disconnect(m_sourceModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(considerDataChanged(QModelIndex,QModelIndex)));
disconnect(m_sourceModel, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
this, SLOT(considerRowsAboutToBeInserted(QModelIndex,int,int)));
disconnect(m_sourceModel, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
this, SLOT(considerRowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
disconnect(m_sourceModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
this, SLOT(considerRowsAboutToBeRemoved(QModelIndex,int,int)));
disconnect(m_sourceModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
this, SLOT(considerRowsInserted(QModelIndex,int,int)));
disconnect(m_sourceModel, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
this, SLOT(considerRowsMoved(QModelIndex,int,int,QModelIndex,int)));
disconnect(m_sourceModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(considerRowsRemoved(QModelIndex,int,int)));
disconnect(m_sourceModel, SIGNAL(modelAboutToBeReset()),
this, SIGNAL(modelAboutToBeReset()));
disconnect(m_sourceModel, SIGNAL(modelReset()),
this, SIGNAL(modelReset()));
disconnect(m_sourceModel, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
this, SIGNAL(headerDataChanged(Qt::Orientation,int,int)));
disconnect(m_sourceModel, SIGNAL(layoutAboutToBeChanged()),
this, SIGNAL(layoutAboutToBeChanged()));
disconnect(m_sourceModel, SIGNAL(layoutChanged()),
this, SIGNAL(layoutChanged()));
}
m_sourceModel = sourceModel;
if(m_sourceModel) {
setRoleNames(m_sourceModel->roleNames());
connect(m_sourceModel, SIGNAL(destroyed(QObject*)),
this, SLOT(sourceDestroyed(QObject*)));
connect(m_sourceModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(considerDataChanged(QModelIndex,QModelIndex)));
connect(m_sourceModel, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
this, SLOT(considerRowsAboutToBeInserted(QModelIndex,int,int)));
connect(m_sourceModel, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
this, SLOT(considerRowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
connect(m_sourceModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
this, SLOT(considerRowsAboutToBeRemoved(QModelIndex,int,int)));
connect(m_sourceModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
this, SLOT(considerRowsInserted(QModelIndex,int,int)));
connect(m_sourceModel, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
this, SLOT(considerRowsMoved(QModelIndex,int,int,QModelIndex,int)));
connect(m_sourceModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(considerRowsRemoved(QModelIndex,int,int)));
connect(m_sourceModel, SIGNAL(modelAboutToBeReset()),
this, SIGNAL(modelAboutToBeReset()));
connect(m_sourceModel, SIGNAL(modelReset()),
this, SIGNAL(modelReset()));
connect(m_sourceModel, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
this, SIGNAL(headerDataChanged(Qt::Orientation,int,int)));
connect(m_sourceModel, SIGNAL(layoutAboutToBeChanged()),
this, SIGNAL(layoutAboutToBeChanged()));
connect(m_sourceModel, SIGNAL(layoutChanged()),
this, SIGNAL(layoutChanged()));
}
endResetModel();
}
void ColumnProxyModel::setColumn(int col)
{
beginResetModel();
m_column = col;
endResetModel();
}
int ColumnProxyModel::column() const
{
return m_column;
}
QModelIndex ColumnProxyModel::rootIndex() const
{
return m_index;
}
void ColumnProxyModel::setRootIndex(const QModelIndex& index)
{
if(index.isValid()) {
setSourceModel(const_cast<QAbstractItemModel*>(index.model()));
}
beginResetModel();
m_index = index;
endResetModel();
emit rootIndexChanged();
}
QModelIndex ColumnProxyModel::indexFromModel(QAbstractItemModel* model, int row, int column, const QModelIndex& parent)
{
return model->index(row, column, parent);
}
QVariant ColumnProxyModel::data(const QModelIndex& index, int role) const
{
return m_sourceModel ? m_sourceModel->data(sourceIndex(index), role) : QVariant();
}
QVariant ColumnProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
return m_sourceModel ? m_sourceModel->headerData(section, orientation, role) : QVariant();
}
QModelIndex ColumnProxyModel::sourceIndex(const QModelIndex& proxyIndex) const
{
return m_sourceModel ? m_sourceModel->index(proxyIndex.row(), m_column, m_index) : QModelIndex();
}
int ColumnProxyModel::rowCount(const QModelIndex& parent) const
{
return (!m_sourceModel || parent.isValid()) ? 0 : m_sourceModel->rowCount(m_index);
}
QModelIndex ColumnProxyModel::proxyIndex(const QModelIndex& sourceIndex) const
{
if(sourceIndex.parent()==m_index)
return index(sourceIndex.row(), sourceIndex.column(), QModelIndex());
return QModelIndex();
}
void ColumnProxyModel::sourceDestroyed(QObject* source)
{
Q_ASSERT(source==m_sourceModel);
beginResetModel();
m_sourceModel = 0;
endResetModel();
}
QModelIndex ColumnProxyModel::indexAt(int row, const QModelIndex& parent) const
{
return m_sourceModel ? m_sourceModel->index(row, m_column, parent) : QModelIndex();
}
/////////////////
void ColumnProxyModel::considerDataChanged(const QModelIndex& idxA, const QModelIndex& idxB)
{
if(idxA.parent()==m_index && idxB.parent()==m_index) {
emit dataChanged(proxyIndex(idxA), proxyIndex(idxB));
}
}
void ColumnProxyModel::considerRowsAboutToBeInserted(const QModelIndex& parent, int rA, int rB)
{
if(parent==m_index) {
beginInsertRows(QModelIndex(), rA, rB);
}
}
void ColumnProxyModel::considerRowsAboutToBeMoved(const QModelIndex &sourceParent, int rA, int rB, const QModelIndex& destParent, int rD)
{
if(sourceParent==m_index && destParent==m_index) {
beginMoveRows(QModelIndex(), rA, rB, QModelIndex(), rD);
} else if(sourceParent==m_index) {
beginRemoveRows(sourceParent, rA, rB);
} else if(destParent==m_index) {
beginInsertRows(destParent, rD, rD+(rB-rA));
}
}
void ColumnProxyModel::considerRowsAboutToBeRemoved(const QModelIndex& parent, int rA, int rB)
{
if(parent==m_index) {
beginRemoveRows(QModelIndex(), rA, rB);
}
}
void ColumnProxyModel::considerRowsInserted(const QModelIndex& parent, int , int )
{
if(parent==m_index) {
endInsertRows();
}
}
void ColumnProxyModel::considerRowsMoved(const QModelIndex& sourceParent, int , int , const QModelIndex& destParent, int )
{
if(sourceParent==m_index && destParent==m_index) {
endMoveRows();
} else if(sourceParent==m_index) {
endRemoveRows();
} else if(destParent==m_index) {
endInsertRows();
}
}
void ColumnProxyModel::considerRowsRemoved(const QModelIndex& parent, int , int )
{
if(parent==m_index) {
endInsertRows();
}
}
#include "columnproxymodel.moc"

View File

@ -0,0 +1,72 @@
/*
* Copyright 2012 by Aleix Pol Gonzalez <aleixpol@blue-systems.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library 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 Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef COLUMNPROXYMODEL_H
#define COLUMNPROXYMODEL_H
#include <QAbstractListModel>
class ColumnProxyModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QModelIndex rootIndex READ rootIndex WRITE setRootIndex NOTIFY rootIndexChanged)
// Q_PROPERTY(QAbstractItemModel* sourceModel READ sourceModel WRITE setSourceModel) //rootIndex sets the model
Q_PROPERTY(int column READ column WRITE setColumn)
public:
ColumnProxyModel(QObject* parent = 0);
void setRootIndex(const QModelIndex& idx);
QModelIndex rootIndex() const;
void setSourceModel(QAbstractItemModel* sourceModel);
QAbstractItemModel* sourceModel() const { return m_sourceModel; }
int column() const;
void setColumn(int col);
Q_SCRIPTABLE static QModelIndex indexFromModel(QAbstractItemModel* model, int row, int column=0, const QModelIndex& parent=QModelIndex());
Q_SCRIPTABLE QModelIndex indexAt(int row, const QModelIndex& parent = QModelIndex()) const;
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
signals:
void rootIndexChanged();
private:
QModelIndex proxyIndex(const QModelIndex& sourceIndex) const;
QModelIndex sourceIndex(const QModelIndex& proxyIndex) const;
int m_column;
QModelIndex m_index;
QAbstractItemModel* m_sourceModel;
private slots:
void considerRowsAboutToBeInserted(const QModelIndex&,int,int);
void considerRowsAboutToBeMoved(const QModelIndex& sourceParent, int rA, int rB, const QModelIndex& destParent, int rD);
void considerRowsAboutToBeRemoved(const QModelIndex&,int,int);
void considerRowsRemoved(const QModelIndex&,int,int);
void considerRowsMoved(const QModelIndex&,int,int,const QModelIndex&,int);
void considerRowsInserted(const QModelIndex&,int,int);
void considerDataChanged(const QModelIndex& idxA, const QModelIndex& idxB);
void sourceDestroyed(QObject* source);
};
#endif

View File

@ -27,7 +27,7 @@
#include "qimageitem.h"
#include "qiconitem.h"
#include "mouseeventlistener.h"
#include "columnproxymodel.h"
void QtExtraComponentsPlugin::registerTypes(const char *uri)
{
@ -37,6 +37,10 @@ void QtExtraComponentsPlugin::registerTypes(const char *uri)
qmlRegisterType<QImageItem>(uri, 0, 1, "QImageItem");
qmlRegisterType<QIconItem>(uri, 0, 1, "QIconItem");
qmlRegisterType<MouseEventListener>(uri, 0, 1, "MouseEventListener");
qmlRegisterType<ColumnProxyModel>(uri, 0, 1, "ColumnProxyModel");
qmlRegisterType<QAbstractItemModel>();
qRegisterMetaType<QModelIndex>("QModelIndex");
}

View File

@ -0,0 +1,9 @@
INCLUDE_DIRECTORIES(.. .)
kde4_add_unit_test(fullmodelaccesstest columnproxymodeltest.cpp ../columnproxymodel.cpp ../../krunnermodel/test/modeltest.cpp)
target_link_libraries(fullmodelaccesstest
${QT_QTCORE_LIBRARY}
${QT_QTGUI_LIBRARY}
${QT_QTTEST_LIBRARY}
${KDE4_KDECORE_LIBS}
)

View File

@ -0,0 +1,65 @@
/*
* Copyright 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <../tests/columnproxymodeltest.h>
#include <columnproxymodel.h>
#include <../../krunnermodel/test/modeltest.h>
#include <qtest_kde.h>
#include <QStandardItemModel>
QTEST_KDEMAIN_CORE(ColumnProxyModelTest)
void ColumnProxyModelTest::testInit()
{
qRegisterMetaType<QModelIndex>("QModelIndex");
ColumnProxyModel* listify = new ColumnProxyModel;
QSignalSpy spy(listify, SIGNAL(rowsInserted(QModelIndex, int, int)));
new ModelTest(listify, listify);
QStandardItemModel* m = new QStandardItemModel(listify);
listify->setRootIndex(QModelIndex());
listify->setSourceModel(m);
m->appendRow(new QStandardItem("lalalal"));
m->appendRow(new QStandardItem("lalalal"));
m->appendRow(new QStandardItem("lalalal"));
m->appendRow(new QStandardItem("lalalal"));
QStandardItem* item = new QStandardItem("lalalal");
item->appendRow(new QStandardItem("lelele"));
item->appendRow(new QStandardItem("lelele"));
m->appendRow(item);
item->appendRow(new QStandardItem("lelele"));
QCOMPARE(listify->rowCount(), 5);
QCOMPARE(spy.count(), 5);
ColumnProxyModel* listifyB = new ColumnProxyModel;
new ModelTest(listifyB, listifyB);
listifyB->setSourceModel(m);
QCOMPARE(listifyB->rowCount(), 5);
ColumnProxyModel* listifyC = new ColumnProxyModel;
new ModelTest(listifyC, listifyC);
listifyC->setRootIndex(item->index());
QCOMPARE(listifyC->rowCount(), 3);
delete listify;
delete listifyB;
delete listifyC;
}

View File

@ -0,0 +1,32 @@
/*
* Copyright 2012 Aleix Pol Gonzalez <aleixpol@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#ifndef COLUMNPROXYMODELTEST_H
#define COLUMNPROXYMODELTEST_H
#include <QObject>
class ColumnProxyModelTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testInit();
};
#endif